LDMX Software
HistogramPool.h
1#ifndef __FRAMEWORK_HISTOGRAM_POOL_H__
2#define __FRAMEWORK_HISTOGRAM_POOL_H__
3
4//----------------//
5// C++ StdLib //
6//----------------//
7#include <functional>
8#include <unordered_map>
9
10//----------//
11// ROOT //
12//----------//
13#include "Framework/Configure/Parameters.h"
14#include "Framework/Exception/Exception.h"
15#include "TDirectory.h"
16#include "TH1F.h"
17#include "TH2F.h"
18
19namespace framework {
20
108 private:
110 double the_weight_{1.};
112 std::unordered_map<std::string, TH1*> histograms_;
123 std::function<TDirectory*()> get_directory_;
124
142 void insert(const std::string& name, std::function<TH1*()> factory,
143 bool weighted);
144
145 public:
147 HistogramPool(std::function<TDirectory*()> get_directory)
148 : get_directory_{get_directory} {}
149
153 void setWeight(double w) { the_weight_ = w; }
154
158 TH1* get(const std::string& name);
159
163 void create(const config::Parameters& p);
164
175 void create(const std::string& name, const std::string& x_label,
176 const int& bins, const double& xmin, const double& xmax,
177 bool weighted = false);
178
187 void create(const std::string& name, const std::string& x_label,
188 const std::vector<double>& bins, bool weighted = false);
189
198 void create(const std::string& name, const std::string& x_label,
199 const std::vector<std::string>& categories,
200 bool weighted = false);
201
216 void create(const std::string& name, const std::string& x_label,
217 const int& xbins, const double& xmin, const double& xmax,
218 const std::string& y_label, const int& ybins, const double& ymin,
219 const double& ymax, bool weighted = false);
220
234 void create(const std::string& name, const std::string& x_label,
235 const std::vector<double>& xbins, const std::string& y_label,
236 const int& ybins, const double& ymin, const double& ymax,
237 bool weighted = false);
238
252 void create(const std::string& name, const std::string& x_label,
253 const std::vector<std::string>& xcategories,
254 const std::string& y_label, const int& ybins, const double& ymin,
255 const double& ymax, bool weighted = false);
256
270 void create(const std::string& name, const std::string& x_label,
271 const int& xbins, const double& xmin, const double& xmax,
272 const std::string& y_label, const std::vector<double>& ybins,
273 bool weighted = false);
274
286 void create(const std::string& name, const std::string& x_label,
287 const std::vector<double>& xbins, const std::string& y_label,
288 const std::vector<double>& ybins, bool weighted = false);
289
301 void create(const std::string& name, const std::string& x_label,
302 const std::vector<std::string>& xcategories,
303 const std::string& y_label, const std::vector<double>& ybins,
304 bool weighted = false);
305
319 void create(const std::string& name, const std::string& x_label,
320 const int& xbins, const double& xmin, const double& xmax,
321 const std::string& y_label,
322 const std::vector<std::string>& ycategories,
323 bool weighted = false);
324
336 void create(const std::string& name, const std::string& x_label,
337 const std::vector<double>& xbins, const std::string& y_label,
338 const std::vector<std::string>& ycategories,
339 bool weighted = false);
340
352 void create(const std::string& name, const std::string& x_label,
353 const std::vector<std::string>& xcategories,
354 const std::string& y_label,
355 const std::vector<std::string>& ycategories,
356 bool weighted = false);
357
366 template <typename T>
367 void fill(const std::string& name, const T& val) {
368 auto hist = dynamic_cast<TH1F*>(this->get(name));
369 if (hist) {
370 hist->Fill(val, the_weight_);
371 } else {
372 // the `get` method handles checking if the histogram exists
373 // the only way that hist would be null at this point is if its not a TH1F
374 EXCEPTION_RAISE(
375 "BadHistSize",
376 "Attempting to 1D fill a histogram that is not actually 1D.");
377 }
378 }
379
389 template <typename T>
390 void fillw(const std::string& name, const T& val, double w) {
391 auto hist = dynamic_cast<TH1F*>(this->get(name));
392 if (hist) {
393 hist->Fill(val, w);
394 } else {
395 // the `get` method handles checking if the histogram exists
396 // the only way that hist would be null at this point is if its not a TH1F
397 EXCEPTION_RAISE(
398 "BadHistSize",
399 "Attempting to 1D fill a histogram that is not actually 1D.");
400 }
401 }
402
412 template <typename Tx, typename Ty>
413 void fill(const std::string& name, const Tx& valx, const Ty& valy) {
414 auto hist = dynamic_cast<TH2F*>(this->get(name));
415 if (hist) {
416 hist->Fill(valx, valy, the_weight_);
417 } else {
418 // the `get` method handles checking if the histogram exists
419 // the only way that hist would be null at this point is if its not a TH2F
420 EXCEPTION_RAISE(
421 "BadHistSize",
422 "Attempting to 2D fill a histogram that is not actually 2D.");
423 }
424 }
425
436 template <typename Tx, typename Ty>
437 void fillw(const std::string& name, const Tx& valx, const Ty& valy,
438 double w) {
439 auto hist = dynamic_cast<TH2F*>(this->get(name));
440 if (hist) {
441 hist->Fill(valx, valy, w);
442 } else {
443 // the `get` method handles checking if the histogram exists
444 // the only way that hist would be null at this point is if its not a TH2F
445 EXCEPTION_RAISE(
446 "BadHistSize",
447 "Attempting to 2D fill a histogram that is not actually 2D.");
448 }
449 }
450};
451} // namespace framework
452
453#endif // __FRAMEWORK_HISTOGRAM_POOL_H__
Class for holding an EventProcessor's histogram pointers and making sure that they all end up in the ...
void insert(const std::string &name, std::function< TH1 *()> factory, bool weighted)
insert a histogram into this pool by name
void fillw(const std::string &name, const Tx &valx, const Ty &valy, double w)
Fill a 2D histogram.
std::function< TDirectory *()> get_directory_
the callback to get the directory these histograms should go in
void setWeight(double w)
Set the weight for filling the histograms.
void fillw(const std::string &name, const T &val, double w)
Fill a 1D histogram.
HistogramPool(std::function< TDirectory *()> get_directory)
define how we can get the directory we need
void create(const config::Parameters &p)
Create a histogram from the input configuration parameters.
void fill(const std::string &name, const T &val)
Fill a 1D histogram.
void fill(const std::string &name, const Tx &valx, const Ty &valy)
Fill a 2D histogram.
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
double the_weight_
The weight to fill histograms with.
Class encapsulating parameters for configuring a processor.
Definition Parameters.h:29
All classes in the ldmx-sw project use this namespace.