2#include "Framework/HistogramPool.h"
24 "\nMake sure to `histograms_.create` in onProcessStart for any "
25 "histogram you want to `histograms_.fill`.");
32 std::function<TH1*()> factory,
bool weighted) {
37 " already exists in histogram pool."
38 "\nMake sure to use distinct names for your different histograms!"
39 "\nYou can use `histograms_.get` to retrieve a pointer to a "
41 "if you want to do some other customizations besides filling.");
52 auto hist = factory();
53 if (weighted) hist->Sumw2();
57std::tuple<std::size_t, double, double> categoryBins(
58 const std::vector<std::string>& categories,
int offset = 0) {
59 std::size_t n_categories = categories.size();
60 double min = offset - 0.5;
61 double max = offset + n_categories + 0.5;
62 return std::make_tuple(n_categories, min, max);
65void labelAxis(TAxis* axis,
const std::vector<std::string>& categories) {
66 for (std::size_t ibin{1}; ibin <= categories.size(); ibin++) {
67 axis->SetBinLabel(ibin, categories[ibin - 1].c_str());
72 auto name{p.
get<std::string>(
"name")};
73 auto x_label{p.
get<std::string>(
"xlabel")};
74 auto y_label{p.
get<std::string>(
"ylabel")};
75 auto weighted{p.
get<
bool>(
"weighted")};
76 auto numeric_xbins{p.
get<std::vector<double>>(
"xbins")};
77 auto category_xbins{p.
get<std::vector<std::string>>(
"xcategories", {})};
78 auto numeric_ybins{p.
get<std::vector<double>>(
"ybins", {})};
79 auto category_ybins{p.
get<std::vector<std::string>>(
"ycategories", {})};
81 bool one_dim = (numeric_ybins.empty() and category_ybins.empty());
82 bool x_is_category = (not category_xbins.empty());
83 bool y_is_category = (not category_ybins.empty());
87 create(name, x_label, category_xbins, weighted);
89 create(name, x_label, numeric_xbins, weighted);
92 if (x_is_category and y_is_category) {
93 create(name, x_label, category_xbins, y_label, category_ybins, weighted);
94 }
else if (x_is_category and not y_is_category) {
95 create(name, x_label, category_xbins, y_label, numeric_ybins, weighted);
96 }
else if (not x_is_category and y_is_category) {
97 create(name, x_label, numeric_xbins, y_label, category_ybins, weighted);
99 create(name, x_label, numeric_xbins, y_label, numeric_ybins, weighted);
105 const std::vector<std::string>& categories,
110 auto [nbins, xmin, xmax] = categoryBins(categories);
111 auto hist =
new TH1F(name.c_str(),
"", nbins, xmin, xmax);
112 hist->GetXaxis()->SetTitle(x_label.c_str());
113 labelAxis(hist->GetXaxis(), categories);
120 const int& bins,
const double& xmin,
121 const double& xmax,
bool weighted) {
125 auto hist =
new TH1F(name.c_str(),
"", bins, xmin, xmax);
126 hist->GetXaxis()->SetTitle(x_label.c_str());
133 const std::vector<double>& bins,
bool weighted) {
137 auto hist =
new TH1F(name.c_str(),
"", bins.size() - 1, bins.data());
138 hist->GetXaxis()->SetTitle(x_label.c_str());
145 const int& xbins,
const double& xmin,
146 const double& xmax,
const std::string& y_label,
147 const int& ybins,
const double& ymin,
148 const double& ymax,
bool weighted) {
153 new TH2F(name.c_str(),
"", xbins, xmin, xmax, ybins, ymin, ymax);
154 hist->GetXaxis()->SetTitle(x_label.c_str());
155 hist->GetYaxis()->SetTitle(y_label.c_str());
162 const std::vector<double>& xbins,
163 const std::string& y_label,
const int& ybins,
164 const double& ymin,
const double& ymax,
169 auto hist =
new TH2F(name.c_str(),
"", xbins.size() - 1, xbins.data(),
171 hist->GetXaxis()->SetTitle(x_label.c_str());
172 hist->GetYaxis()->SetTitle(y_label.c_str());
179 const std::vector<std::string>& xcategories,
180 const std::string& y_label,
const int& ybins,
181 const double& ymin,
const double& ymax,
186 auto [nxbins, xmin, xmax] = categoryBins(xcategories);
188 new TH2F(name.c_str(),
"", nxbins, xmin, xmax, ybins, ymin, ymax);
189 labelAxis(hist->GetXaxis(), xcategories);
190 hist->GetXaxis()->SetTitle(x_label.c_str());
191 hist->GetYaxis()->SetTitle(y_label.c_str());
198 const int& xbins,
const double& xmin,
199 const double& xmax,
const std::string& y_label,
200 const std::vector<double>& ybins,
bool weighted) {
204 auto hist =
new TH2F(name.c_str(),
"", xbins, xmin, xmax,
205 ybins.size() - 1, ybins.data());
206 hist->GetXaxis()->SetTitle(x_label.c_str());
207 hist->GetYaxis()->SetTitle(y_label.c_str());
214 const int& xbins,
const double& xmin,
215 const double& xmax,
const std::string& y_label,
216 const std::vector<std::string>& ycategories,
221 auto [nybins, ymin, ymax] = categoryBins(ycategories);
223 new TH2F(name.c_str(),
"", xbins, xmin, xmax, nybins, ymin, ymax);
224 labelAxis(hist->GetYaxis(), ycategories);
225 hist->GetXaxis()->SetTitle(x_label.c_str());
226 hist->GetYaxis()->SetTitle(y_label.c_str());
233 const std::vector<double>& xbins,
234 const std::string& y_label,
235 const std::vector<double>& ybins,
bool weighted) {
239 auto hist =
new TH2F(name.c_str(),
"", xbins.size() - 1, xbins.data(),
240 ybins.size() - 1, ybins.data());
241 hist->GetXaxis()->SetTitle(x_label.c_str());
242 hist->GetYaxis()->SetTitle(y_label.c_str());
249 const std::vector<double>& xbins,
250 const std::string& y_label,
251 const std::vector<std::string>& ycategories,
256 auto [nybins, ymin, ymax] = categoryBins(ycategories);
257 auto hist =
new TH2F(name.c_str(),
"", xbins.size() - 1, xbins.data(),
259 labelAxis(hist->GetYaxis(), ycategories);
260 hist->GetXaxis()->SetTitle(x_label.c_str());
261 hist->GetYaxis()->SetTitle(y_label.c_str());
268 const std::vector<std::string>& xcategories,
269 const std::string& y_label,
270 const std::vector<double>& ybins,
bool weighted) {
274 auto [nxbins, xmin, xmax] = categoryBins(xcategories);
275 auto hist =
new TH2F(name.c_str(),
"", nxbins, xmin, xmax,
276 ybins.size() - 1, ybins.data());
277 labelAxis(hist->GetXaxis(), xcategories);
278 hist->GetXaxis()->SetTitle(x_label.c_str());
279 hist->GetYaxis()->SetTitle(y_label.c_str());
286 const std::vector<std::string>& xcategories,
287 const std::string& y_label,
288 const std::vector<std::string>& ycategories,
293 auto [nybins, ymin, ymax] = categoryBins(ycategories);
294 auto [nxbins, xmin, xmax] = categoryBins(xcategories);
296 new TH2F(name.c_str(),
"", nxbins, xmin, xmax, nybins, ymin, ymax);
297 labelAxis(hist->GetYaxis(), ycategories);
298 labelAxis(hist->GetXaxis(), xcategories);
299 hist->GetXaxis()->SetTitle(x_label.c_str());
300 hist->GetYaxis()->SetTitle(y_label.c_str());
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.
const T & get(const std::string &name) const
Retrieve the parameter of the given name.
All classes in the ldmx-sw project use this namespace.