2#include "Framework/HistogramPool.h"
22 "\nMake sure to `histograms_.create` in onProcessStart for any "
23 "histogram you want to `histograms_.fill`.");
30 std::function<TH1*()> factory,
bool weighted) {
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 "
39 "if you want to do some other customizations besides filling.");
50 auto hist = factory();
51 if (weighted) hist->Sumw2();
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);
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());
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", {})};
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());
85 create(name, x_label, category_xbins, weighted);
87 create(name, x_label, numeric_xbins, weighted);
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);
97 create(name, x_label, numeric_xbins, y_label, numeric_ybins, weighted);
103 const std::vector<std::string>& categories,
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);
118 const int& bins,
const double& xmin,
119 const double& xmax,
bool weighted) {
123 auto hist =
new TH1F(name.c_str(),
"", bins, xmin, xmax);
124 hist->GetXaxis()->SetTitle(x_label.c_str());
131 const std::vector<double>& bins,
bool weighted) {
135 auto hist =
new TH1F(name.c_str(),
"", bins.size() - 1, bins.data());
136 hist->GetXaxis()->SetTitle(x_label.c_str());
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) {
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());
160 const std::vector<double>& xbins,
161 const std::string& y_label,
const int& ybins,
162 const double& ymin,
const double& ymax,
167 auto hist =
new TH2F(name.c_str(),
"", xbins.size() - 1, xbins.data(),
169 hist->GetXaxis()->SetTitle(x_label.c_str());
170 hist->GetYaxis()->SetTitle(y_label.c_str());
177 const std::vector<std::string>& xcategories,
178 const std::string& y_label,
const int& ybins,
179 const double& ymin,
const double& ymax,
184 auto [nxbins, xmin, xmax] = categoryBins(xcategories);
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());
196 const int& xbins,
const double& xmin,
197 const double& xmax,
const std::string& y_label,
198 const std::vector<double>& ybins,
bool weighted) {
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());
212 const int& xbins,
const double& xmin,
213 const double& xmax,
const std::string& y_label,
214 const std::vector<std::string>& ycategories,
219 auto [nybins, ymin, ymax] = categoryBins(ycategories);
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());
231 const std::vector<double>& xbins,
232 const std::string& y_label,
233 const std::vector<double>& ybins,
bool weighted) {
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());
247 const std::vector<double>& xbins,
248 const std::string& y_label,
249 const std::vector<std::string>& ycategories,
254 auto [nybins, ymin, ymax] = categoryBins(ycategories);
255 auto hist =
new TH2F(name.c_str(),
"", xbins.size() - 1, xbins.data(),
257 labelAxis(hist->GetYaxis(), ycategories);
258 hist->GetXaxis()->SetTitle(x_label.c_str());
259 hist->GetYaxis()->SetTitle(y_label.c_str());
266 const std::vector<std::string>& xcategories,
267 const std::string& y_label,
268 const std::vector<double>& ybins,
bool weighted) {
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());
284 const std::vector<std::string>& xcategories,
285 const std::string& y_label,
286 const std::vector<std::string>& ycategories,
291 auto [nybins, ymin, ymax] = categoryBins(ycategories);
292 auto [nxbins, xmin, xmax] = categoryBins(xcategories);
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());
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.