rivet is hosted by Hepforge, IPPP Durham
Rivet 4.0.0
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/RivetYODA.hh"
7
8namespace Rivet {
9
10 namespace {
11
15 template <size_t FillDim, typename T, typename AxisT>
16 typename YODA::FillableStorage<FillDim, T, AxisT>::FillAdapterT
17 groupAdapter = [](auto& /* ptr */, auto&& /* coords */, double /* weight */, double /* fraction */) { };
18
19 }
20
21 template <typename GroupAxisT, typename... AxisT>
22 class HistoGroup : public YODA::FillableStorage<sizeof...(AxisT)+1, BinnedHistoPtr<AxisT...>, GroupAxisT>,
23 public YODA::Fillable {
24 public:
25
26 using BaseT = YODA::FillableStorage<sizeof...(AxisT)+1, BinnedHistoPtr<AxisT...>, GroupAxisT>;
27 using BinContentT = BinnedHistoPtr<AxisT...>;
28 using FillDim = std::integral_constant<size_t, sizeof...(AxisT)+1>;
29
30 HistoGroup() : BaseT(groupAdapter<FillDim{}, BinContentT, GroupAxisT>) { }
31
32 HistoGroup(const std::vector<GroupAxisT>& edges)
33 : BaseT(YODA::Axis<GroupAxisT>(edges), groupAdapter<FillDim{}, BinContentT, GroupAxisT>) { }
34
35 HistoGroup(std::initializer_list<GroupAxisT>&& edges)
36 : BaseT(YODA::Axis<GroupAxisT>(std::move(edges)), groupAdapter<FillDim{}, BinContentT, GroupAxisT>) { }
37
38 int fill(GroupAxisT tmpCoord, AxisT... coords, const double weight = 1.0, const double fraction = 1.0) {
39 auto& bin = BaseT::binAt(tmpCoord);
40 if (!bin.raw()) return -1; // nullptr if bin not booked
41 return bin->fill({coords...}, weight, fraction);
42 }
43
46
50 void reset() noexcept { BaseT::reset(); }
51
53
56
58 size_t fillDim() const noexcept { return FillDim{}; }
59
61 size_t dim() const noexcept { return fillDim()+1; }
62
64 double numEntries(const bool includeOverflows=true) const noexcept {
65 double n = 0;
66 for (const auto& b : BaseT::bins(includeOverflows)) {
67 if (!b.get()) continue;
68 n += b->numEntries(includeOverflows);
69 }
70 return n;
71 }
72
74 double effNumEntries(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->effNumEntries(includeOverflows);
79 }
80 return n;
81 }
82
84 double sumW(const bool includeOverflows=true) const noexcept {
85 double sumw = 0;
86 for (const auto& b : BaseT::bins(includeOverflows)) {
87 if (!b.get()) continue;
88 sumw += b->sumW(includeOverflows);
89 }
90 return sumw;
91 }
92
94 double sumW2(const bool includeOverflows=true) const noexcept {
95 double sumw2 = 0;
96 for (const auto& b : BaseT::bins(includeOverflows)) {
97 if (!b.get()) continue;
98 sumw2 += b->sumW2(includeOverflows);
99 }
100 return sumw2;
101 }
102
104 double integral(const bool includeOverflows=true) const noexcept {
105 return sumW(includeOverflows);
106 }
107
109 double integralError(const bool includeOverflows=true) const noexcept {
110 return sqrt(sumW2(includeOverflows));
111 }
112
114
117
118 void divByGroupWidth() const noexcept {
119 for (auto& bin : BaseT::bins(true, true)) {
120 if (!bin.get()) continue;
121 const double bw = bin.dVol();
122 if (bw) bin->scaleW(1.0/bw);
123 }
124 }
125
127 void scaleW(const double scalefactor) noexcept {
128 for (auto& bin : BaseT::bins(true, true)) {
129 if (!bin.get()) continue;
130 bin->scaleW(scalefactor);
131 }
132 }
133
135 void scale(const size_t i, const double scalefactor) noexcept {
136 for (auto& bin : BaseT::bins(true, true)) {
137 if (!bin.get()) continue;
138 bin->scale(i, scalefactor);
139 }
140 }
141
145 void normalize(const double normto=1.0, const bool includeOverflows=true) {
146 for (auto& bin : BaseT::bins(true, true)) {
147 if (!bin.get()) continue;
148 if (bin->integral(includeOverflows) != 0.) bin->normalize(normto, includeOverflows);
149 }
150 }
151
155 void normalizeGroup(const double normto=1.0, const bool includeOverflows=true) {
156 const double oldintegral = integral(includeOverflows);
157 if (oldintegral == 0) throw WeightError("Attempted to normalize a histogram group with null area");
158 scaleW(normto / oldintegral);
159 }
160
162
163 };
164
167
168 template <typename GroupAxisT, typename... AxisT>
169 using HistoGroupPtr = std::shared_ptr<HistoGroup<GroupAxisT, AxisT...>>;
170 using Histo1DGroupPtr = HistoGroupPtr<double,double>;
171 using Histo2DGroupPtr = HistoGroupPtr<double,double,double>;
172
174
175}
176
177#endif
Definition HistoGroup.hh:23
void normalize(const double normto=1.0, const bool includeOverflows=true)
Normalize the (visible) histo "volume" to the normto value.
Definition HistoGroup.hh:145
void scaleW(const double scalefactor) noexcept
Rescale as if all fill weights had been different by factor scalefactor.
Definition HistoGroup.hh:127
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:135
size_t dim() const noexcept
Total dimension of the object (number of fill axes + filled value)
Definition HistoGroup.hh:61
double integralError(const bool includeOverflows=true) const noexcept
Get the total volume error of the histogram group.
Definition HistoGroup.hh:109
double sumW(const bool includeOverflows=true) const noexcept
Calculates sum of weights in histo group.
Definition HistoGroup.hh:84
double integral(const bool includeOverflows=true) const noexcept
Get the total volume of the histogram group.
Definition HistoGroup.hh:104
size_t fillDim() const noexcept
Fill dimension of the object (number of conent axes + temprary axis)
Definition HistoGroup.hh:58
void normalizeGroup(const double normto=1.0, const bool includeOverflows=true)
Normalize the (visible) histo "volume" to the normto value.
Definition HistoGroup.hh:155
double numEntries(const bool includeOverflows=true) const noexcept
Get the number of fills (fractional fills are possible).
Definition HistoGroup.hh:64
double sumW2(const bool includeOverflows=true) const noexcept
Calculates sum of squared weights in histo group.
Definition HistoGroup.hh:94
double effNumEntries(const bool includeOverflows=true) const noexcept
Get the effective number of fills.
Definition HistoGroup.hh:74
void reset() noexcept
Reset the histogram.
Definition HistoGroup.hh:50
Definition RivetYODA.hh:1330
Definition MC_CENT_PPB_Projections.hh:10
Errors relating to event/bin weights.
Definition Exceptions.hh:49