LDMX Software
HistogramPool.cxx
1
2#include "Framework/HistogramPool.h"
3
4//----------------//
5// C++ StdLib //
6//----------------//
7
8//----------//
9// ROOT //
10//----------//
11#include "TH1.h"
12
13namespace framework {
14
15TH1* HistogramPool::get(const std::string& name) {
16 auto histo = histograms_.find(name);
17 if (histo == histograms_.end()) {
18 EXCEPTION_RAISE(
19 "InvalidArg",
20 "Histogram " + name +
21 " not found in pool."
22 "\nMake sure to `histograms_.create` in onProcessStart for any "
23 "histogram you want to `histograms_.fill`.");
24 }
25
26 return histograms_[name];
27}
28
29void HistogramPool::insert(const std::string& name,
30 std::function<TH1*()> factory, bool weighted) {
31 if (histograms_.find(name) != histograms_.end()) {
32 EXCEPTION_RAISE(
33 "RepeatName",
34 "Histogram " + name +
35 " already exists in histogram pool."
36 "\nMake sure to use distinct names for your different histograms!"
37 "\nYou can use `histograms_.get` to retrieve a pointer to a "
38 "specific histogram "
39 "if you want to do some other customizations besides filling.");
40 }
41
42 get_directory_()->cd();
43
50 auto hist = factory();
51 if (weighted) hist->Sumw2();
52 histograms_[name] = hist;
53}
54
55std::tuple<std::size_t, double, double> categoryBins(
56 const std::vector<std::string>& categories, int offset = 0) {
57 std::size_t n_categories = categories.size();
58 double min = offset - 0.5;
59 double max = offset + n_categories + 0.5;
60 return std::make_tuple(n_categories, min, max);
61}
62
63void labelAxis(TAxis* axis, const std::vector<std::string>& categories) {
64 for (std::size_t ibin{1}; ibin <= categories.size(); ibin++) {
65 axis->SetBinLabel(ibin, categories[ibin - 1].c_str());
66 }
67}
68
70 auto name{p.get<std::string>("name")};
71 auto x_label{p.get<std::string>("xlabel")};
72 auto y_label{p.get<std::string>("ylabel")};
73 auto weighted{p.get<bool>("weighted")};
74 auto numeric_xbins{p.get<std::vector<double>>("xbins")};
75 auto category_xbins{p.get<std::vector<std::string>>("xcategories", {})};
76 auto numeric_ybins{p.get<std::vector<double>>("ybins", {})};
77 auto category_ybins{p.get<std::vector<std::string>>("ycategories", {})};
78
79 bool one_dim = (numeric_ybins.empty() and category_ybins.empty());
80 bool x_is_category = (not category_xbins.empty());
81 bool y_is_category = (not category_ybins.empty());
82 if (one_dim) {
83 // assume 1D histogram
84 if (x_is_category) {
85 create(name, x_label, category_xbins, weighted);
86 } else {
87 create(name, x_label, numeric_xbins, weighted);
88 }
89 } else {
90 if (x_is_category and y_is_category) {
91 create(name, x_label, category_xbins, y_label, category_ybins, weighted);
92 } else if (x_is_category and not y_is_category) {
93 create(name, x_label, category_xbins, y_label, numeric_ybins, weighted);
94 } else if (not x_is_category and y_is_category) {
95 create(name, x_label, numeric_xbins, y_label, category_ybins, weighted);
96 } else /* not x_is_category and not y_is_category */ {
97 create(name, x_label, numeric_xbins, y_label, numeric_ybins, weighted);
98 }
99 }
100}
101
102void HistogramPool::create(const std::string& name, const std::string& x_label,
103 const std::vector<std::string>& categories,
104 bool weighted) {
105 insert(
106 name,
107 [&]() {
108 auto [nbins, xmin, xmax] = categoryBins(categories);
109 auto hist = new TH1F(name.c_str(), "", nbins, xmin, xmax);
110 hist->GetXaxis()->SetTitle(x_label.c_str());
111 labelAxis(hist->GetXaxis(), categories);
112 return hist;
113 },
114 weighted);
115}
116
117void HistogramPool::create(const std::string& name, const std::string& x_label,
118 const int& bins, const double& xmin,
119 const double& xmax, bool weighted) {
120 insert(
121 name,
122 [&]() {
123 auto hist = new TH1F(name.c_str(), "", bins, xmin, xmax);
124 hist->GetXaxis()->SetTitle(x_label.c_str());
125 return hist;
126 },
127 weighted);
128}
129
130void HistogramPool::create(const std::string& name, const std::string& x_label,
131 const std::vector<double>& bins, bool weighted) {
132 insert(
133 name,
134 [&]() {
135 auto hist = new TH1F(name.c_str(), "", bins.size() - 1, bins.data());
136 hist->GetXaxis()->SetTitle(x_label.c_str());
137 return hist;
138 },
139 weighted);
140}
141
142void HistogramPool::create(const std::string& name, const std::string& x_label,
143 const int& xbins, const double& xmin,
144 const double& xmax, const std::string& y_label,
145 const int& ybins, const double& ymin,
146 const double& ymax, bool weighted) {
147 insert(
148 name,
149 [&]() {
150 auto hist =
151 new TH2F(name.c_str(), "", xbins, xmin, xmax, ybins, ymin, ymax);
152 hist->GetXaxis()->SetTitle(x_label.c_str());
153 hist->GetYaxis()->SetTitle(y_label.c_str());
154 return hist;
155 },
156 weighted);
157}
158
159void HistogramPool::create(const std::string& name, const std::string& x_label,
160 const std::vector<double>& xbins,
161 const std::string& y_label, const int& ybins,
162 const double& ymin, const double& ymax,
163 bool weighted) {
164 insert(
165 name,
166 [&]() {
167 auto hist = new TH2F(name.c_str(), "", xbins.size() - 1, xbins.data(),
168 ybins, ymin, ymax);
169 hist->GetXaxis()->SetTitle(x_label.c_str());
170 hist->GetYaxis()->SetTitle(y_label.c_str());
171 return hist;
172 },
173 weighted);
174}
175
176void HistogramPool::create(const std::string& name, const std::string& x_label,
177 const std::vector<std::string>& xcategories,
178 const std::string& y_label, const int& ybins,
179 const double& ymin, const double& ymax,
180 bool weighted) {
181 insert(
182 name,
183 [&]() {
184 auto [nxbins, xmin, xmax] = categoryBins(xcategories);
185 auto hist =
186 new TH2F(name.c_str(), "", nxbins, xmin, xmax, ybins, ymin, ymax);
187 labelAxis(hist->GetXaxis(), xcategories);
188 hist->GetXaxis()->SetTitle(x_label.c_str());
189 hist->GetYaxis()->SetTitle(y_label.c_str());
190 return hist;
191 },
192 weighted);
193}
194
195void HistogramPool::create(const std::string& name, const std::string& x_label,
196 const int& xbins, const double& xmin,
197 const double& xmax, const std::string& y_label,
198 const std::vector<double>& ybins, bool weighted) {
199 insert(
200 name,
201 [&]() {
202 auto hist = new TH2F(name.c_str(), "", xbins, xmin, xmax,
203 ybins.size() - 1, ybins.data());
204 hist->GetXaxis()->SetTitle(x_label.c_str());
205 hist->GetYaxis()->SetTitle(y_label.c_str());
206 return hist;
207 },
208 weighted);
209}
210
211void HistogramPool::create(const std::string& name, const std::string& x_label,
212 const int& xbins, const double& xmin,
213 const double& xmax, const std::string& y_label,
214 const std::vector<std::string>& ycategories,
215 bool weighted) {
216 insert(
217 name,
218 [&]() {
219 auto [nybins, ymin, ymax] = categoryBins(ycategories);
220 auto hist =
221 new TH2F(name.c_str(), "", xbins, xmin, xmax, nybins, ymin, ymax);
222 labelAxis(hist->GetYaxis(), ycategories);
223 hist->GetXaxis()->SetTitle(x_label.c_str());
224 hist->GetYaxis()->SetTitle(y_label.c_str());
225 return hist;
226 },
227 weighted);
228}
229
230void HistogramPool::create(const std::string& name, const std::string& x_label,
231 const std::vector<double>& xbins,
232 const std::string& y_label,
233 const std::vector<double>& ybins, bool weighted) {
234 insert(
235 name,
236 [&]() {
237 auto hist = new TH2F(name.c_str(), "", xbins.size() - 1, xbins.data(),
238 ybins.size() - 1, ybins.data());
239 hist->GetXaxis()->SetTitle(x_label.c_str());
240 hist->GetYaxis()->SetTitle(y_label.c_str());
241 return hist;
242 },
243 weighted);
244}
245
246void HistogramPool::create(const std::string& name, const std::string& x_label,
247 const std::vector<double>& xbins,
248 const std::string& y_label,
249 const std::vector<std::string>& ycategories,
250 bool weighted) {
251 insert(
252 name,
253 [&]() {
254 auto [nybins, ymin, ymax] = categoryBins(ycategories);
255 auto hist = new TH2F(name.c_str(), "", xbins.size() - 1, xbins.data(),
256 nybins, ymin, ymax);
257 labelAxis(hist->GetYaxis(), ycategories);
258 hist->GetXaxis()->SetTitle(x_label.c_str());
259 hist->GetYaxis()->SetTitle(y_label.c_str());
260 return hist;
261 },
262 weighted);
263}
264
265void HistogramPool::create(const std::string& name, const std::string& x_label,
266 const std::vector<std::string>& xcategories,
267 const std::string& y_label,
268 const std::vector<double>& ybins, bool weighted) {
269 insert(
270 name,
271 [&]() {
272 auto [nxbins, xmin, xmax] = categoryBins(xcategories);
273 auto hist = new TH2F(name.c_str(), "", nxbins, xmin, xmax,
274 ybins.size() - 1, ybins.data());
275 labelAxis(hist->GetXaxis(), xcategories);
276 hist->GetXaxis()->SetTitle(x_label.c_str());
277 hist->GetYaxis()->SetTitle(y_label.c_str());
278 return hist;
279 },
280 weighted);
281}
282
283void HistogramPool::create(const std::string& name, const std::string& x_label,
284 const std::vector<std::string>& xcategories,
285 const std::string& y_label,
286 const std::vector<std::string>& ycategories,
287 bool weighted) {
288 insert(
289 name,
290 [&]() {
291 auto [nybins, ymin, ymax] = categoryBins(ycategories);
292 auto [nxbins, xmin, xmax] = categoryBins(xcategories);
293 auto hist =
294 new TH2F(name.c_str(), "", nxbins, xmin, xmax, nybins, ymin, ymax);
295 labelAxis(hist->GetYaxis(), ycategories);
296 labelAxis(hist->GetXaxis(), xcategories);
297 hist->GetXaxis()->SetTitle(x_label.c_str());
298 hist->GetYaxis()->SetTitle(y_label.c_str());
299 return hist;
300 },
301 weighted);
302}
303
304} // namespace framework
void insert(const std::string &name, std::function< TH1 *()> factory, bool weighted)
insert a histogram into this pool by name
std::function< TDirectory *()> get_directory_
the callback to get the directory these histograms should go in
void create(const config::Parameters &p)
Create a histogram from the input configuration parameters.
TH1 * get(const std::string &name)
get a histogram from this pool by name
std::unordered_map< std::string, TH1 * > histograms_
the pool of histogram pointers
Class encapsulating parameters for configuring a processor.
Definition Parameters.h:26
const T & get(const std::string &name) const
Retrieve the parameter of the given name.
Definition Parameters.h:75
All classes in the ldmx-sw project use this namespace.