BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
HistSparse1D.hh
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) BDSIM Collaboration, 2001 - 2026.
3
4This file is part of BDSIM.
5
6BDSIM is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published
8by the Free Software Foundation version 3 of the License.
9
10BDSIM is distributed in the hope that it will be useful, but
11WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
17*/
18#ifndef HISTSPARSE1D_H
19#define HISTSPARSE1D_H
20
21#include "Rtypes.h"
22#include "TObject.h"
23
24#include <cmath>
25#include <map>
26#include <string>
27
42template<class T>
43class HistSparse1D: public TObject
44{
45public:
48
50 explicit HistSparse1D(const std::string& nameIn):
51 name(nameIn),
52 entries(0)
53 {;}
54 virtual ~HistSparse1D();
55
57 {
58 double sumWeights = 0;
59 double sumWeightsSquared = 0;
60
61 friend BinWorking operator+(BinWorking& lhs, // passing lhs by value helps optimize chained a+b+c
62 const BinWorking& rhs) // otherwise, both parameters may be const references
63 {
64 lhs += rhs; // reuse compound assignment
65 return lhs;
66 }
67 BinWorking& operator+=(const BinWorking& rhs)
68 {
69 sumWeights += rhs.sumWeights;
70 sumWeightsSquared += rhs.sumWeightsSquared;
71 return *this;
72 }
73 };
74
75 struct Bin
76 {
77 double value = 0;
78 double error = 0;
79 };
80
81 inline void Fill(T x, double weight=1.0)
82 {
83 auto& v = data[x];
84 v.sumWeights += weight;
85 v.sumWeightsSquared += weight*weight;
86 entries++;
87 }
88 inline long long int Entries() const {return entries;}
89 inline size_t size() const {return data.size();}
90 std::map<T, Bin> Result() const
91 {
92 std::map<T, Bin> result;
93 for (const auto& kv : data)
94 {
95 Bin v;
96 v.value = kv.second.sumWeights;
97 v.error = std::sqrt(kv.second.sumWeightsSquared);
98 result[kv.first] = v;
99 }
100 return result;
101 };
102
104 typedef typename std::map<T, BinWorking>::iterator iterator;
105 typedef typename std::map<T, BinWorking>::const_iterator const_iterator;
106 iterator begin() {return data.begin();}
107 iterator end() {return data.end();}
108 const_iterator begin() const {return data.begin();}
109 const_iterator end() const {return data.end();}
110 bool empty() const {return data.empty();}
111
112 BinWorking& operator[](const long long int key) {return data[key];}
113 const BinWorking& operator[](const long long int key) const {return data.at(key);}
114
115 bool HasAbscissa(T value) const {return data.find(value) != data.end();}
116
117 std::string name;
118 std::map<T, BinWorking> data;
119 long long int entries;
120
121 ClassDef(HistSparse1D,1);
122};
123
124#endif
Sparse 1D histogram based on a map.
HistSparse1D(const std::string &nameIn)
Use this constructor.
HistSparse1D()
Public constructor only for compatibility with ROOT - not intended for use.
std::map< T, BinWorking >::iterator iterator
Iterator mechanics.
Base class of a result.
Definition Result.hh:36