LDMX Software
Histograms.h
1#ifndef __FRAMEWORK_HISTOGRAM_POOL_H__
2#define __FRAMEWORK_HISTOGRAM_POOL_H__
3
4//----------------//
5// C++ StdLib //
6//----------------//
7#include <iostream>
8#include <memory>
9#include <unordered_map>
10
11//----------//
12// ROOT //
13//----------//
14#include "TH1F.h"
15#include "TH2F.h"
16
17namespace framework {
18
28 private:
30 std::unordered_map<std::string, TH1*> histograms_;
31
38
39 public:
41 HistogramPool(HistogramPool const&) = delete;
42
44 void operator=(HistogramPool const&) = delete;
45
47 static HistogramPool& getInstance();
48
54 void insert(const std::string& name, TH1* hist) { histograms_[name] = hist; }
55
63 TH1* get(const std::string& name);
64
65}; // HistogramPool
66
73 private:
75 double theWeight_{1.};
76
78 std::string name_;
79
80 public:
86 HistogramHelper(const std::string& name) : name_(name) {}
87
91 void setWeight(double w) { theWeight_ = w; }
92
105 void create(const std::string& name, const std::string& xLabel,
106 const double& bins, const double& xmin, const double& xmax);
107
118 void create(const std::string& name, const std::string& xLabel,
119 const std::vector<double>& bins);
120
137 void create(const std::string& name, const std::string& xLabel,
138 const double& xbins, const double& xmin, const double& xmax,
139 const std::string& yLabel, const double& ybins,
140 const double& ymin, const double& ymax);
141
154 void create(const std::string& name, const std::string& xLabel,
155 const std::vector<double>& xbins, const std::string& yLabel,
156 const std::vector<double>& ybins);
157
166 void fill(const std::string& name, const double& val) {
167 auto hist = dynamic_cast<TH1F*>(this->get(name));
168 if (hist) {
169 hist->Fill(val, theWeight_);
170 }
171 }
172
182 void fill(const std::string& name, const double& valx, const double& valy) {
183 auto hist = dynamic_cast<TH2F*>(this->get(name));
184 if (hist) {
185 hist->Fill(valx, valy, theWeight_);
186 }
187 }
188
194 TH1* get(const std::string& name) {
195 return HistogramPool::getInstance().get(name_ + "_" + name);
196 }
197};
198} // namespace framework
199
200#endif // __FRAMEWORK_HISTOGRAM_POOL_H__
Interface class between an EventProcessor and the HistogramPool.
Definition Histograms.h:72
TH1 * get(const std::string &name)
Get a pointer to a histogram by name.
Definition Histograms.h:194
void fill(const std::string &name, const double &val)
Fill a 1D histogram.
Definition Histograms.h:166
std::string name_
The name of the processor that this helper is assigned to.
Definition Histograms.h:78
double theWeight_
The weight to fill histograms with.
Definition Histograms.h:75
void create(const std::string &name, const std::string &xLabel, const double &bins, const double &xmin, const double &xmax)
Create a ROOT 1D histogram of type TH1F and pool it for later use.
HistogramHelper(const std::string &name)
Constructor.
Definition Histograms.h:86
void fill(const std::string &name, const double &valx, const double &valy)
Fill a 2D histogram.
Definition Histograms.h:182
void setWeight(double w)
Set the weight for filling the histograms.
Definition Histograms.h:91
Singleton class used to create and pool histograms.
Definition Histograms.h:27
static HistogramPool & getInstance()
Access the single instance of HistogramPool by reference.
HistogramPool()
Private constructor to prevent instantiation.
HistogramPool(HistogramPool const &)=delete
Hide copy constructor.
void insert(const std::string &name, TH1 *hist)
Insert a histogram into the pool.
Definition Histograms.h:54
void operator=(HistogramPool const &)=delete
Hide assignment operator.
TH1 * get(const std::string &name)
Get a histogram using its name.
std::unordered_map< std::string, TH1 * > histograms_
Container for all histograms.
Definition Histograms.h:30
All classes in the ldmx-sw project use this namespace.
Definition PerfDict.cxx:45