Rivet API documentation

Rivet 4.1.3
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 groupAdapter =
18 [](auto& /* ptr */, auto&& /* coords */, double /* weight */, double /* fraction */) {
19 };
20
21 }
22
23 template <typename GroupAxisT, typename... AxisT>
24 class HistoGroup : public YODA::FillableStorage<sizeof...(AxisT) + 1, BinnedHistoPtr<AxisT...>, GroupAxisT>,
25 public YODA::Fillable {
26 public:
27
28 using BaseT = YODA::FillableStorage<sizeof...(AxisT) + 1, BinnedHistoPtr<AxisT...>, GroupAxisT>;
29 using BinContentT = BinnedHistoPtr<AxisT...>;
30 using FillDim = std::integral_constant<size_t, sizeof...(AxisT) + 1>;
31
32 HistoGroup()
33 : BaseT(groupAdapter<FillDim{}, BinContentT, GroupAxisT>) { }
34
35 HistoGroup(const std::vector<GroupAxisT>& edges)
36 : BaseT(YODA::Axis<GroupAxisT>(edges), groupAdapter<FillDim{}, BinContentT, GroupAxisT>) { }
37
38 HistoGroup(std::initializer_list<GroupAxisT>&& edges)
39 : BaseT(YODA::Axis<GroupAxisT>(std::move(edges)), groupAdapter<FillDim{}, BinContentT, GroupAxisT>) {
40 }
41
42 int fill(GroupAxisT tmpCoord, AxisT... coords, const double weight = 1.0, const double fraction = 1.0) {
43 auto& bin = BaseT::binAt(tmpCoord);
44 if (!bin.raw()) return -1; // nullptr if bin not booked
45 return bin->fill({coords...}, weight, fraction);
46 }
47
50
54 void reset() noexcept {
55 BaseT::reset();
56 }
57
59
62
64 size_t fillDim() const noexcept {
65 return FillDim{};
66 }
67
69 size_t dim() const noexcept {
70 return fillDim() + 1;
71 }
72
74 double numEntries(const bool includeOverflows = true) const noexcept {
75 double n = 0;
76 for (const auto& b : BaseT::bins(includeOverflows)) {
77 if (!b.get()) continue;
78 n += b->numEntries(includeOverflows);
79 }
80 return n;
81 }
82
84 double effNumEntries(const bool includeOverflows = true) const noexcept {
85 double n = 0;
86 for (const auto& b : BaseT::bins(includeOverflows)) {
87 if (!b.get()) continue;
88 n += b->effNumEntries(includeOverflows);
89 }
90 return n;
91 }
92
94 double sumW(const bool includeOverflows = true) const noexcept {
95 double sumw = 0;
96 for (const auto& b : BaseT::bins(includeOverflows)) {
97 if (!b.get()) continue;
98 sumw += b->sumW(includeOverflows);
99 }
100 return sumw;
101 }
102
104 double sumW2(const bool includeOverflows = true) const noexcept {
105 double sumw2 = 0;
106 for (const auto& b : BaseT::bins(includeOverflows)) {
107 if (!b.get()) continue;
108 sumw2 += b->sumW2(includeOverflows);
109 }
110 return sumw2;
111 }
112
114 vector<double> sumWGroup(const bool includeOverflows = true) const noexcept {
115 vector<double> rtn;
116 rtn.reserve(BaseT::numBins(true));
117 for (const auto& b : BaseT::bins(true)) {
118 if (!b.get()) {
119 rtn.push_back(0.0);
120 continue;
121 }
122 rtn.push_back(b->sumW(includeOverflows));
123 }
124 return rtn;
125 }
126
128 double integral(const bool includeOverflows = true) const noexcept {
129 return sumW(includeOverflows);
130 }
131
133 double integralError(const bool includeOverflows = true) const noexcept {
134 return sqrt(sumW2(includeOverflows));
135 }
136
138
141
142 void divByGroupWidth() const noexcept {
143 for (auto& bin : BaseT::bins(true, true)) {
144 if (!bin.get()) continue;
145 const double bw = bin.dVol();
146 if (bw) bin->scaleW(1.0 / bw);
147 }
148 }
149
151 void scaleW(const double scalefactor) noexcept {
152 for (auto& bin : BaseT::bins(true, true)) {
153 if (!bin.get()) continue;
154 bin->scaleW(scalefactor);
155 }
156 }
157
159 void scale(const size_t i, const double scalefactor) noexcept {
160 for (auto& bin : BaseT::bins(true, true)) {
161 if (!bin.get()) continue;
162 bin->scale(i, scalefactor);
163 }
164 }
165
169 void normalize(const double normto = 1.0, const bool includeOverflows = true) {
170 for (auto& bin : BaseT::bins(true, true)) {
171 if (!bin.get()) continue;
172 if (bin->integral(includeOverflows) != 0.) bin->normalize(normto, includeOverflows);
173 }
174 }
175
179 void normalizeGroup(const double normto = 1.0, const bool includeOverflows = true) {
180 const double oldintegral = integral(includeOverflows);
181 if (oldintegral == 0) {
182 MSG_DEBUG("Attempted to normalize a histogram group with null area; skipping.");
183 return;
184 }
185 scaleW(normto / oldintegral);
186 }
187
189
190 protected:
191
193 Log& getLog() const {
194 string logname = "Rivet.HistoGroup";
195 return Log::getLog(logname);
196 }
197 };
198
201
202 template <typename GroupAxisT, typename... AxisT>
203 using HistoGroupPtr = std::shared_ptr<HistoGroup<GroupAxisT, AxisT...>>;
204 using Histo1DGroupPtr = HistoGroupPtr<double, double>;
205 using Histo2DGroupPtr = HistoGroupPtr<double, double, double>;
206
208
209}
210
211#endif
void normalize(const double normto=1.0, const bool includeOverflows=true)
Normalize the (visible) histo "volume" to the normto value.
Definition HistoGroup.hh:169
void scaleW(const double scalefactor) noexcept
Rescale as if all fill weights had been different by factor scalefactor.
Definition HistoGroup.hh:151
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:159
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:114
size_t dim() const noexcept
Total dimension of the object (number of fill axes + filled value).
Definition HistoGroup.hh:69
double integralError(const bool includeOverflows=true) const noexcept
Get the total volume error of the histogram group.
Definition HistoGroup.hh:133
Log & getLog() const
Get a Log object based on the name() property of the calling analysis object.
Definition HistoGroup.hh:193
double sumW(const bool includeOverflows=true) const noexcept
Calculates sum of weights in histo group.
Definition HistoGroup.hh:94
double integral(const bool includeOverflows=true) const noexcept
Get the total volume of the histogram group.
Definition HistoGroup.hh:128
size_t fillDim() const noexcept
Fill dimension of the object (number of conent axes + temprary axis).
Definition HistoGroup.hh:64
void normalizeGroup(const double normto=1.0, const bool includeOverflows=true)
Normalize the (visible) histo "volume" to the normto value.
Definition HistoGroup.hh:179
double numEntries(const bool includeOverflows=true) const noexcept
Get the number of fills (fractional fills are possible).
Definition HistoGroup.hh:74
double sumW2(const bool includeOverflows=true) const noexcept
Calculates sum of squared weights in histo group.
Definition HistoGroup.hh:104
double effNumEntries(const bool includeOverflows=true) const noexcept
Get the effective number of fills.
Definition HistoGroup.hh:84
void reset() noexcept
Reset the histogram.
Definition HistoGroup.hh:54
Logging system for controlled & formatted writing to stdout.
Definition Logging.hh:10
static Log & getLog(const std::string &name)
#define MSG_DEBUG(x)
Debug messaging, not enabled by default, using MSG_LVL.
Definition Logging.hh:195
Definition LHCbCommon.hh:9