LDMX Software
Histograms.cxx
1
2#include "Framework/Histograms.h"
3
4#include "Framework/Exception/Exception.h"
5
6//----------------//
7// C++ StdLib //
8//----------------//
9#include <stdexcept>
10
11//----------//
12// ROOT //
13//----------//
14#include "TH1.h"
15#include "TStyle.h"
16
17namespace framework {
18
20 gStyle->SetOptStat(1);
21 gStyle->SetGridColor(17);
22 gStyle->SetFrameBorderMode(0);
23 gStyle->SetTitleOffset(1.2, "yx");
24 gStyle->SetTitleFontSize(25);
25
26 gStyle->SetPadBottomMargin(0.1);
27 gStyle->SetPadTopMargin(0.01);
28 gStyle->SetPadLeftMargin(0.1);
29 gStyle->SetPadRightMargin(0.09);
30 gStyle->SetPadGridX(1);
31 gStyle->SetPadGridY(1);
32 gStyle->SetPadTickX(1);
33 gStyle->SetPadTickY(1);
34
35 gStyle->SetHistLineWidth(2);
36}
37
39 // Create an instance of HistogramPool if needed
40 // Guarnteed to be destroyed, instantiaed on first use
41 static HistogramPool instance;
42
43 return instance;
44}
45
46TH1* HistogramPool::get(const std::string& name) {
47 auto histo = histograms_.find(name);
48 if (histo == histograms_.end()) {
49 EXCEPTION_RAISE("InvalidArg", "Histogram " + name + " not found in pool.");
50 }
51
52 return histograms_[name];
53}
54
55void HistogramHelper::create(const std::string& name, const std::string& xLabel,
56 const double& bins, const double& xmin,
57 const double& xmax) {
58 std::string fullName = name_ + "_" + name;
59
60 // Create a histogram of type T
61 auto hist = new TH1F(fullName.c_str(), fullName.c_str(), bins, xmin, xmax);
62
63 // Set the title
64 hist->SetTitle("");
65
66 // Set the x-axis label
67 hist->GetXaxis()->SetTitle(xLabel.c_str());
68 hist->GetXaxis()->CenterTitle();
69
70 // Insert it into the pool of histograms for later use
71 HistogramPool::getInstance().insert(fullName, hist);
72}
73
74void HistogramHelper::create(const std::string& name, const std::string& xLabel,
75 const std::vector<double>& bins) {
76 std::string fullName = name_ + "_" + name;
77
78 // copy bin edges into a C98 form acceptable by ROOT
79 int nbins = bins.size() - 1;
80 double* binEdges = new double[bins.size()];
81 for (unsigned int iBin = 0; iBin < bins.size(); iBin++)
82 binEdges[iBin] = bins.at(iBin);
83
84 auto hist = new TH1F(fullName.c_str(), fullName.c_str(), nbins, binEdges);
85
86 delete[] binEdges; // cleanup
87
88 // Set the title
89 hist->SetTitle("");
90
91 // Set the x-axis label
92 hist->GetXaxis()->SetTitle(xLabel.c_str());
93 hist->GetXaxis()->CenterTitle();
94
95 // Insert it into the pool of histograms for later use
96 HistogramPool::getInstance().insert(fullName, hist);
97}
98
99void HistogramHelper::create(const std::string& name, const std::string& xLabel,
100 const double& xbins, const double& xmin,
101 const double& xmax, const std::string& yLabel,
102 const double& ybins, const double& ymin,
103 const double& ymax) {
104 std::string fullName = name_ + "_" + name;
105
106 // Create a histogram of type T
107 auto hist = new TH2F(fullName.c_str(), fullName.c_str(), xbins, xmin, xmax,
108 ybins, ymin, ymax);
109
110 // Set the title
111 hist->SetTitle("");
112
113 // Set the x-axis label
114 hist->GetXaxis()->SetTitle(xLabel.c_str());
115 hist->GetXaxis()->CenterTitle();
116
117 // Set the x-axis label
118 hist->GetYaxis()->SetTitle(yLabel.c_str());
119 hist->GetYaxis()->CenterTitle();
120
121 // Insert it into the pool of histograms for later use
122 HistogramPool::getInstance().insert(fullName, hist);
123}
124
125void HistogramHelper::create(const std::string& name, const std::string& xLabel,
126 const std::vector<double>& xbins,
127 const std::string& yLabel,
128 const std::vector<double>& ybins) {
129 std::string fullName = name_ + "_" + name;
130
131 // copy bin edges into a C98 form acceptable by ROOT
132 int xNBins = xbins.size() - 1;
133 double* xBinEdges = new double[xbins.size()];
134 for (unsigned int iBin = 0; iBin < xbins.size(); iBin++)
135 xBinEdges[iBin] = xbins.at(iBin);
136
137 int yNBins = ybins.size() - 1;
138 double* yBinEdges = new double[ybins.size()];
139 for (unsigned int iBin = 0; iBin < ybins.size(); iBin++)
140 yBinEdges[iBin] = ybins.at(iBin);
141
142 auto hist = new TH2F(fullName.c_str(), fullName.c_str(), xNBins, xBinEdges,
143 yNBins, yBinEdges);
144
145 delete[] xBinEdges; // cleanup
146 delete[] yBinEdges; // cleanup
147
148 // Set the title
149 hist->SetTitle("");
150
151 // Set the x-axis label
152 hist->GetXaxis()->SetTitle(xLabel.c_str());
153 hist->GetXaxis()->CenterTitle();
154
155 // Set the x-axis label
156 hist->GetYaxis()->SetTitle(yLabel.c_str());
157 hist->GetYaxis()->CenterTitle();
158
159 // Insert it into the pool of histograms for later use
160 HistogramPool::getInstance().insert(fullName, hist);
161}
162} // namespace framework
std::string name_
The name of the processor that this helper is assigned to.
Definition Histograms.h:78
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.
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.
void insert(const std::string &name, TH1 *hist)
Insert a histogram into the pool.
Definition Histograms.h:54
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