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