rivet is hosted by Hepforge, IPPP Durham
Rivet 4.0.2
HistoGroup.hh
1// -*- C++ -*-
2#ifndef RIVET_HISTOGROUP_HH
3#define RIVET_HISTOGROUP_HH
4
5#include "Rivet/Config/RivetCommon.hh"
6#include "Rivet/Tools/Logging.hh"
7#include "Rivet/Tools/RivetYODA.hh"
8
9namespace Rivet {
10
11 namespace {
12
16 template <size_t FillDim, typename T, typename AxisT>
17 typename YODA::FillableStorage<FillDim, T, AxisT>::FillAdapterT
18 groupAdapter = [](auto& /* ptr */, auto&& /* coords */, double /* weight */, double /* fraction */) { };
19
20 }
21
22 template <typename GroupAxisT, typename... AxisT>
23 class HistoGroup : public YODA::FillableStorage<sizeof...(AxisT)+1, BinnedHistoPtr<AxisT...>, GroupAxisT>,
24 public YODA::Fillable {
25 public:
26
27 using BaseT = YODA::FillableStorage<sizeof...(AxisT)+1, BinnedHistoPtr<AxisT...>, GroupAxisT>;
28 using BinContentT = BinnedHistoPtr<AxisT...>;
29 using FillDim = std::integral_constant<size_t, sizeof...(AxisT)+1>;
30
31 HistoGroup() : BaseT(groupAdapter<FillDim{}, BinContentT, GroupAxisT>) { }
32
33 HistoGroup(const std::vector<GroupAxisT>& edges)
34 : BaseT(YODA::Axis<GroupAxisT>(edges), groupAdapter<FillDim{}, BinContentT, GroupAxisT>) { }
35
36 HistoGroup(std::initializer_list<GroupAxisT>&& edges)
37 : BaseT(YODA::Axis<GroupAxisT>(std::move(edges)), groupAdapter<FillDim{}, BinContentT, GroupAxisT>) { }
38
39 int fill(GroupAxisT tmpCoord, AxisT... coords, const double weight = 1.0, const double fraction = 1.0) {
40 auto& bin = BaseT::binAt(tmpCoord);
41 if (!bin.raw()) return -1; // nullptr if bin not booked
42 return bin->fill({coords...}, weight, fraction);
43 }
44
47
51 void reset() noexcept { BaseT::reset(); }
52
54
57
59 size_t fillDim() const noexcept { return FillDim{}; }
60
62 size_t dim() const noexcept { return fillDim()+1; }
63
65 double numEntries(const bool includeOverflows=true) const noexcept {
66 double n = 0;
67 for (const auto& b : BaseT::bins(includeOverflows)) {
68 if (!b.get()) continue;
69 n += b->numEntries(includeOverflows);
70 }
71 return n;
72 }
73
75 double effNumEntries(const bool includeOverflows=true) const noexcept {
76 double n = 0;
77 for (const auto& b : BaseT::bins(includeOverflows)) {
78 if (!b.get()) continue;
79 n += b->effNumEntries(includeOverflows);
80 }
81 return n;
82 }
83
85 double sumW(const bool includeOverflows=true) const noexcept {
86 double sumw = 0;
87 for (const auto& b : BaseT::bins(includeOverflows)) {
88 if (!b.get()) continue;
89 sumw += b->sumW(includeOverflows);
90 }
91 return sumw;
92 }
93
95 double sumW2(const bool includeOverflows=true) const noexcept {
96 double sumw2 = 0;
97 for (const auto& b : BaseT::bins(includeOverflows)) {
98 if (!b.get()) continue;
99 sumw2 += b->sumW2(includeOverflows);
100 }
101 return sumw2;
102 }
103
105 vector<double> sumWGroup(const bool includeOverflows=true) const noexcept {
106 vector<double> rtn;
107 rtn.reserve(BaseT::numBins(true));
108 for (const auto& b : BaseT::bins(true)) {
109 if (!b.get()) {
110 rtn.push_back(0.0);
111 continue;
112 }
113 rtn.push_back( b->sumW(includeOverflows) );
114 }
115 return rtn;
116 }
117
119 double integral(const bool includeOverflows=true) const noexcept {
120 return sumW(includeOverflows);
121 }
122
124 double integralError(const bool includeOverflows=true) const noexcept {
125 return sqrt(sumW2(includeOverflows));
126 }
127
129
132
133 void divByGroupWidth() const noexcept {
134 for (auto& bin : BaseT::bins(true, true)) {
135 if (!bin.get()) continue;
136 const double bw = bin.dVol();
137 if (bw) bin->scaleW(1.0/bw);
138 }
139 }
140
142 void scaleW(const double scalefactor) noexcept {
143 for (auto& bin : BaseT::bins(true, true)) {
144 if (!bin.get()) continue;
145 bin->scaleW(scalefactor);
146 }
147 }
148
150 void scale(const size_t i, const double scalefactor) noexcept {
151 for (auto& bin : BaseT::bins(true, true)) {
152 if (!bin.get()) continue;
153 bin->scale(i, scalefactor);
154 }
155 }
156
160 void normalize(const double normto=1.0, const bool includeOverflows=true) {
161 for (auto& bin : BaseT::bins(true, true)) {
162 if (!bin.get()) continue;
163 if (bin->integral(includeOverflows) != 0.) bin->normalize(normto, includeOverflows);
164 }
165 }
166
170 void normalizeGroup(const double normto=1.0, const bool includeOverflows=true) {
171 const double oldintegral = integral(includeOverflows);
172 if (oldintegral == 0) {
173 MSG_DEBUG("Attempted to normalize a histogram group with null area; skipping.");
174 return;
175 }
176 scaleW(normto / oldintegral);
177 }
178
180
181 protected:
182
184 Log& getLog() const {
185 string logname = "Rivet.HistoGroup";
186 return Log::getLog(logname);
187 }
188
189 };
190
193
194 template <typename GroupAxisT, typename... AxisT>
195 using HistoGroupPtr = std::shared_ptr<HistoGroup<GroupAxisT, AxisT...>>;
196 using Histo1DGroupPtr = HistoGroupPtr<double,double>;
197 using Histo2DGroupPtr = HistoGroupPtr<double,double,double>;
198
200
201}
202
203#endif
Definition HistoGroup.hh:24
void normalize(const double normto=1.0, const bool includeOverflows=true)
Normalize the (visible) histo "volume" to the normto value.
Definition HistoGroup.hh:160
void scaleW(const double scalefactor) noexcept
Rescale as if all fill weights had been different by factor scalefactor.
Definition HistoGroup.hh:142
void scale(const size_t i, const double scalefactor) noexcept
Rescale as if all fill weights had been different by factor scalefactor along dimension i.
Definition HistoGroup.hh:150
vector< double > sumWGroup(const bool includeOverflows=true) const noexcept
Return the vector of sum of weights for each histo in the group.
Definition HistoGroup.hh:105
size_t dim() const noexcept
Total dimension of the object (number of fill axes + filled value)
Definition HistoGroup.hh:62
double integralError(const bool includeOverflows=true) const noexcept
Get the total volume error of the histogram group.
Definition HistoGroup.hh:124
Log & getLog() const
Get a Log object based on the name() property of the calling analysis object.
Definition HistoGroup.hh:184
double sumW(const bool includeOverflows=true) const noexcept
Calculates sum of weights in histo group.
Definition HistoGroup.hh:85
double integral(const bool includeOverflows=true) const noexcept
Get the total volume of the histogram group.
Definition HistoGroup.hh:119
size_t fillDim() const noexcept
Fill dimension of the object (number of conent axes + temprary axis)
Definition HistoGroup.hh:59
void normalizeGroup(const double normto=1.0, const bool includeOverflows=true)
Normalize the (visible) histo "volume" to the normto value.
Definition HistoGroup.hh:170
double numEntries(const bool includeOverflows=true) const noexcept
Get the number of fills (fractional fills are possible).
Definition HistoGroup.hh:65
double sumW2(const bool includeOverflows=true) const noexcept
Calculates sum of squared weights in histo group.
Definition HistoGroup.hh:95
double effNumEntries(const bool includeOverflows=true) const noexcept
Get the effective number of fills.
Definition HistoGroup.hh:75
void reset() noexcept
Reset the histogram.
Definition HistoGroup.hh:51
Logging system for controlled & formatted writing to stdout.
Definition Logging.hh:10
static Log & getLog(const std::string &name)
Definition RivetYODA.hh:1330
#define MSG_DEBUG(x)
Debug messaging, not enabled by default, using MSG_LVL.
Definition Logging.hh:182
Definition MC_CENT_PPB_Projections.hh:10