LDMX Software
framework::HistogramPool Class Reference

Class for holding an EventProcessor's histogram pointers and making sure that they all end up in the same directory in the output histogram file. More...

#include <HistogramPool.h>

Public Member Functions

 HistogramPool (std::function< TDirectory *()> get_directory)
 define how we can get the directory we need
 
void setWeight (double w)
 Set the weight for filling the histograms.
 
TH1 * get (const std::string &name)
 get a histogram from this pool by name
 
void create (const config::Parameters &p)
 Create a histogram from the input configuration parameters.
 
void create (const std::string &name, const std::string &x_label, const int &bins, const double &xmin, const double &xmax, bool weighted=false)
 Create a ROOT 1D histogram with uniform binning.
 
void create (const std::string &name, const std::string &x_label, const std::vector< double > &bins, bool weighted=false)
 Create a ROOT 1D histogram with variable binning.
 
void create (const std::string &name, const std::string &x_label, const std::vector< std::string > &categories, bool weighted=false)
 Create a ROOT 1D histogram with categorical binning.
 
void create (const std::string &name, const std::string &x_label, const int &xbins, const double &xmin, const double &xmax, const std::string &y_label, const int &ybins, const double &ymin, const double &ymax, bool weighted=false)
 Create a ROOT 2D histogram with both axes having uniform binning.
 
void create (const std::string &name, const std::string &x_label, const std::vector< double > &xbins, const std::string &y_label, const int &ybins, const double &ymin, const double &ymax, bool weighted=false)
 Create a ROOT 2D histogram with variable bins on the x axis and uniform bins on the y axis.
 
void create (const std::string &name, const std::string &x_label, const std::vector< std::string > &xcategories, const std::string &y_label, const int &ybins, const double &ymin, const double &ymax, bool weighted=false)
 Create a ROOT 2D histogram with category bins on the x axis and uniform bins on the y axis.
 
void create (const std::string &name, const std::string &x_label, const int &xbins, const double &xmin, const double &xmax, const std::string &y_label, const std::vector< double > &ybins, bool weighted=false)
 Create a ROOT 2D histogram with uniform bins on the x axis and variable bins on the y axis.
 
void create (const std::string &name, const std::string &x_label, const std::vector< double > &xbins, const std::string &y_label, const std::vector< double > &ybins, bool weighted=false)
 Create a ROOT 2D histogram with variable bins on the x axis and variable bins on the y axis.
 
void create (const std::string &name, const std::string &x_label, const std::vector< std::string > &xcategories, const std::string &y_label, const std::vector< double > &ybins, bool weighted=false)
 Create a ROOT 2D histogram with category bins on the x axis and variable bins on the y axis.
 
void create (const std::string &name, const std::string &x_label, const int &xbins, const double &xmin, const double &xmax, const std::string &y_label, const std::vector< std::string > &ycategories, bool weighted=false)
 Create a ROOT 2D histogram with uniform bins on the x axis and category bins on the y axis.
 
void create (const std::string &name, const std::string &x_label, const std::vector< double > &xbins, const std::string &y_label, const std::vector< std::string > &ycategories, bool weighted=false)
 Create a ROOT 2D histogram with variable bins on the x axis and category bins on the y axis.
 
void create (const std::string &name, const std::string &x_label, const std::vector< std::string > &xcategories, const std::string &y_label, const std::vector< std::string > &ycategories, bool weighted=false)
 Create a ROOT 2D histogram with category bins on the x axis and category bins on the y axis.
 
template<typename T >
void fill (const std::string &name, const T &val)
 Fill a 1D histogram.
 
template<typename T >
void fillw (const std::string &name, const T &val, double w)
 Fill a 1D histogram.
 
template<typename Tx , typename Ty >
void fill (const std::string &name, const Tx &valx, const Ty &valy)
 Fill a 2D histogram.
 
template<typename Tx , typename Ty >
void fillw (const std::string &name, const Tx &valx, const Ty &valy, double w)
 Fill a 2D histogram.
 

Private Member Functions

void insert (const std::string &name, std::function< TH1 *()> factory, bool weighted)
 insert a histogram into this pool by name
 

Private Attributes

double the_weight_ {1.}
 The weight to fill histograms with.
 
std::unordered_map< std::string, TH1 * > histograms_
 the pool of histogram pointers
 
std::function< TDirectory *()> get_directory_
 the callback to get the directory these histograms should go in
 

Detailed Description

Class for holding an EventProcessor's histogram pointers and making sure that they all end up in the same directory in the output histogram file.

Note
In v4.5.1 of ldmx-sw and earlier, all of the processors shared the same pool of histograms, so the naming was a little funky. If a processor's name was "myProc" and a histogram's name was "myHist", the histogram would be written to "myProc/myProc_myHist" in the output histogram file. After v4.5.2, this central pool was abandoned and the output histograms are written to the more obvious location "myProc/myHist" in the output histogram file.

Usage

Each of the EventProcessors have a histograms_ member variable that developers can use within their producers and analyzers. You need to create the histograms before filling them. There are three different ways to specify the bins of a histogram:

  • uniform bins: provide the number of bins, the minimum, and the maximum
  • variable bins: provide the full list of bin edges (as a std::vector)
  • categories: provide a list of named categories (as a std::vector<std::string>)

With these three different ways to specify bins, there are three different ways to create a 1D histogram and nine different ways to create a 2D histogram. (Future developer note: if we want to increase the number of ways to specify the bins – "axis type" – * or increase the number of dimensions again, we probably just want to use a builder pattern like Boost.Histogram or just use Boost.Histogram under-the-hood and copy the resulting histograms into ROOT histograms at end of run time.)

In onProcessStart() or in the Python configuration, you create the histograms that you will want to fill. For example,

histograms_.create("my_variable", "Label for Axis", 10, 0.0, 1.0);
std::unordered_map< std::string, TH1 * > histograms_
the pool of histogram pointers

or

analyzer.build1DHistogram("my_variable", "Label for Axis", 10, 0.0, 1.0)

And then in analyze or produce you fill the histograms.

histograms_.fill("my_variable", the_value);

Attempting to create histograms without the Python configuration providing an output histogram file will fail because then there is no place for the histograms to be saved.

The fill function uses the current setting of the weight in the histograms_object. This is helpful if, for example, you are putting one entry in the histogram for each event and you want the histogram to use the event weights. In order to do this event weighting for histograms, you would add the following line and all of the histograms that are filled after this line will use the event weight.

histograms_.setWeight(event.getEventWeight());

If you don't want to use special weighting, that's fine. The default weight is 1 and you may even have separate histograms in your processor that use different weights for example.

histograms_setWeight(1);
histograms_.fill("h_without_event_weight", value);
histograms_.setWeight(event.getEventWeight());
histograms_.fill("h_with_event_weight", value);

Additionally, if you want to use individual weights for the different histograms, you can use the fillw function to provide a weight when calling fill.

Note
By default, the histograms do not store the sum of the square weights in order to save space. If you are filling histograms with weights that are not 1 and you care about correct error estimates using those histograms, make sure to create the histogram with the weighted argument set to true.

Definition at line 107 of file HistogramPool.h.

Constructor & Destructor Documentation

◆ HistogramPool()

framework::HistogramPool::HistogramPool ( std::function< TDirectory *()> get_directory)
inline

define how we can get the directory we need

Definition at line 147 of file HistogramPool.h.

148 : get_directory_{get_directory} {}
std::function< TDirectory *()> get_directory_
the callback to get the directory these histograms should go in

Member Function Documentation

◆ create() [1/13]

void framework::HistogramPool::create ( const config::Parameters & p)

Create a histogram from the input configuration parameters.

Definition at line 71 of file HistogramPool.cxx.

71 {
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", {})};
80
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());
84 if (one_dim) {
85 // assume 1D histogram
86 if (x_is_category) {
87 create(name, x_label, category_xbins, weighted);
88 } else {
89 create(name, x_label, numeric_xbins, weighted);
90 }
91 } else {
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);
98 } else /* not x_is_category and not y_is_category */ {
99 create(name, x_label, numeric_xbins, y_label, numeric_ybins, weighted);
100 }
101 }
102}
void create(const config::Parameters &p)
Create a histogram from the input configuration parameters.

References create(), and framework::config::Parameters::get().

Referenced by create(), dqm::SimObjects::createCalorimeterHists(), dqm::SimObjects::createTrackerHists(), dqm::HCalRawDigi::onProcessStart(), dqm::SimObjects::onProcessStart(), dqm::Trigger::onProcessStart(), dqm::TrigScintClusterDQM::onProcessStart(), dqm::TrigScintDQM::onProcessStart(), dqm::TrigScintHitDQM::onProcessStart(), dqm::TrigScintTrackDQM::onProcessStart(), ldmx::ecal::TrigPrimResolutionAnalyzer::onProcessStart(), and TEST_CASE().

◆ create() [2/13]

void framework::HistogramPool::create ( const std::string & name,
const std::string & x_label,
const int & bins,
const double & xmin,
const double & xmax,
bool weighted = false )

Create a ROOT 1D histogram with uniform binning.

Parameters
nameName of the histogram.
x_labelTitle of the x axis.
binsTotal number of histogram bins.
xminThe lower histogram limit.
xmaxThe upper histogram limit.
weightedwhether to track the sum of squared weights separately

Definition at line 119 of file HistogramPool.cxx.

121 {
122 insert(
123 name,
124 [&]() {
125 auto hist = new TH1F(name.c_str(), "", bins, xmin, xmax);
126 hist->GetXaxis()->SetTitle(x_label.c_str());
127 return hist;
128 },
129 weighted);
130}
void insert(const std::string &name, std::function< TH1 *()> factory, bool weighted)
insert a histogram into this pool by name

References insert().

◆ create() [3/13]

void framework::HistogramPool::create ( const std::string & name,
const std::string & x_label,
const int & xbins,
const double & xmin,
const double & xmax,
const std::string & y_label,
const int & ybins,
const double & ymin,
const double & ymax,
bool weighted = false )

Create a ROOT 2D histogram with both axes having uniform binning.

Parameters
nameName of the histogram.
x_labelTitle of the x axis.
xbinsTotal number of histogram bins in x.
xminThe lower histogram limit in x.
xmaxThe upper histogram limit in x.
y_labelTitle of the x axis.
ybinsTotal number of histogram bins in y.
yminThe lower histogram limit in y.
ymaxThe upper histogram limit in y.
weightedwhether to track the sum of squared weights separately

Definition at line 144 of file HistogramPool.cxx.

148 {
149 insert(
150 name,
151 [&]() {
152 auto hist =
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());
156 return hist;
157 },
158 weighted);
159}

References insert().

◆ create() [4/13]

void framework::HistogramPool::create ( const std::string & name,
const std::string & x_label,
const int & xbins,
const double & xmin,
const double & xmax,
const std::string & y_label,
const std::vector< double > & ybins,
bool weighted = false )

Create a ROOT 2D histogram with uniform bins on the x axis and variable bins on the y axis.

Parameters
nameName of the histogram.
x_labelTitle of the x axis.
xbinsTotal number of histogram bins in x.
xminThe lower histogram limit in x.
xmaxThe upper histogram limit in x.
y_labelTitle of the x axis.
ybinsvector of bin edges
weightedwhether to track the sum of squared weights separately

Definition at line 197 of file HistogramPool.cxx.

200 {
201 insert(
202 name,
203 [&]() {
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());
208 return hist;
209 },
210 weighted);
211}

References insert().

◆ create() [5/13]

void framework::HistogramPool::create ( const std::string & name,
const std::string & x_label,
const int & xbins,
const double & xmin,
const double & xmax,
const std::string & y_label,
const std::vector< std::string > & ycategories,
bool weighted = false )

Create a ROOT 2D histogram with uniform bins on the x axis and category bins on the y axis.

Parameters
nameName of the histogram.
x_labelTitle of the x axis.
xbinsTotal number of histogram bins in x.
xminThe lower histogram limit in x.
xmaxThe upper histogram limit in x.
y_labelTitle of the x axis.
ycategoriesvector of string categories
weightedwhether to track the sum of squared weights separately

Definition at line 213 of file HistogramPool.cxx.

217 {
218 insert(
219 name,
220 [&]() {
221 auto [nybins, ymin, ymax] = categoryBins(ycategories);
222 auto hist =
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());
227 return hist;
228 },
229 weighted);
230}

References insert().

◆ create() [6/13]

void framework::HistogramPool::create ( const std::string & name,
const std::string & x_label,
const std::vector< double > & bins,
bool weighted = false )

Create a ROOT 1D histogram with variable binning.

Parameters
nameName of the histogram.
x_labelTitle of the x axis.
binsvector of bin edges
weightedwhether to track the sum of squared weights separately

Definition at line 132 of file HistogramPool.cxx.

133 {
134 insert(
135 name,
136 [&]() {
137 auto hist = new TH1F(name.c_str(), "", bins.size() - 1, bins.data());
138 hist->GetXaxis()->SetTitle(x_label.c_str());
139 return hist;
140 },
141 weighted);
142}

References insert().

◆ create() [7/13]

void framework::HistogramPool::create ( const std::string & name,
const std::string & x_label,
const std::vector< double > & xbins,
const std::string & y_label,
const int & ybins,
const double & ymin,
const double & ymax,
bool weighted = false )

Create a ROOT 2D histogram with variable bins on the x axis and uniform bins on the y axis.

Parameters
nameName of the histogram.
x_labelTitle of the x axis.
xbinsvector of bin edges
y_labelTitle of the x axis.
ybinsTotal number of histogram bins in y.
yminThe lower histogram limit in y.
ymaxThe upper histogram limit in y.
weightedwhether to track the sum of squared weights separately

Definition at line 161 of file HistogramPool.cxx.

165 {
166 insert(
167 name,
168 [&]() {
169 auto hist = new TH2F(name.c_str(), "", xbins.size() - 1, xbins.data(),
170 ybins, ymin, ymax);
171 hist->GetXaxis()->SetTitle(x_label.c_str());
172 hist->GetYaxis()->SetTitle(y_label.c_str());
173 return hist;
174 },
175 weighted);
176}

References insert().

◆ create() [8/13]

void framework::HistogramPool::create ( const std::string & name,
const std::string & x_label,
const std::vector< double > & xbins,
const std::string & y_label,
const std::vector< double > & ybins,
bool weighted = false )

Create a ROOT 2D histogram with variable bins on the x axis and variable bins on the y axis.

Parameters
nameName of the histogram.
x_labelTitle of the x axis.
xbinsvector of bin edges
y_labelTitle of the x axis.
ybinsvector of bin edges
weightedwhether to track the sum of squared weights separately

Definition at line 232 of file HistogramPool.cxx.

235 {
236 insert(
237 name,
238 [&]() {
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());
243 return hist;
244 },
245 weighted);
246}

References insert().

◆ create() [9/13]

void framework::HistogramPool::create ( const std::string & name,
const std::string & x_label,
const std::vector< double > & xbins,
const std::string & y_label,
const std::vector< std::string > & ycategories,
bool weighted = false )

Create a ROOT 2D histogram with variable bins on the x axis and category bins on the y axis.

Parameters
nameName of the histogram.
x_labelTitle of the x axis.
xbinsvector of bin edges
y_labelTitle of the x axis.
ycategoriesvector of string categories
weightedwhether to track the sum of squared weights separately

Definition at line 248 of file HistogramPool.cxx.

252 {
253 insert(
254 name,
255 [&]() {
256 auto [nybins, ymin, ymax] = categoryBins(ycategories);
257 auto hist = new TH2F(name.c_str(), "", xbins.size() - 1, xbins.data(),
258 nybins, ymin, ymax);
259 labelAxis(hist->GetYaxis(), ycategories);
260 hist->GetXaxis()->SetTitle(x_label.c_str());
261 hist->GetYaxis()->SetTitle(y_label.c_str());
262 return hist;
263 },
264 weighted);
265}

References insert().

◆ create() [10/13]

void framework::HistogramPool::create ( const std::string & name,
const std::string & x_label,
const std::vector< std::string > & categories,
bool weighted = false )

Create a ROOT 1D histogram with categorical binning.

Parameters
nameName of the histogram.
x_labelTitle of the x axis.
categorieslist of string categories to name the bins
weightedwhether to track the sum of squared weights separately

Definition at line 104 of file HistogramPool.cxx.

106 {
107 insert(
108 name,
109 [&]() {
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);
114 return hist;
115 },
116 weighted);
117}

References insert().

◆ create() [11/13]

void framework::HistogramPool::create ( const std::string & name,
const std::string & x_label,
const std::vector< std::string > & xcategories,
const std::string & y_label,
const int & ybins,
const double & ymin,
const double & ymax,
bool weighted = false )

Create a ROOT 2D histogram with category bins on the x axis and uniform bins on the y axis.

Parameters
nameName of the histogram.
x_labelTitle of the x axis.
xcategoriesvector of string categories
y_labelTitle of the x axis.
ybinsTotal number of histogram bins in y.
yminThe lower histogram limit in y.
ymaxThe upper histogram limit in y.
weightedwhether to track the sum of squared weights separately

Definition at line 178 of file HistogramPool.cxx.

182 {
183 insert(
184 name,
185 [&]() {
186 auto [nxbins, xmin, xmax] = categoryBins(xcategories);
187 auto hist =
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());
192 return hist;
193 },
194 weighted);
195}

References insert().

◆ create() [12/13]

void framework::HistogramPool::create ( const std::string & name,
const std::string & x_label,
const std::vector< std::string > & xcategories,
const std::string & y_label,
const std::vector< double > & ybins,
bool weighted = false )

Create a ROOT 2D histogram with category bins on the x axis and variable bins on the y axis.

Parameters
nameName of the histogram.
x_labelTitle of the x axis.
xcategoriesvector of string categories
y_labelTitle of the x axis.
ybinsvector of bin edges
weightedwhether to track the sum of squared weights separately

Definition at line 267 of file HistogramPool.cxx.

270 {
271 insert(
272 name,
273 [&]() {
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());
280 return hist;
281 },
282 weighted);
283}

References insert().

◆ create() [13/13]

void framework::HistogramPool::create ( const std::string & name,
const std::string & x_label,
const std::vector< std::string > & xcategories,
const std::string & y_label,
const std::vector< std::string > & ycategories,
bool weighted = false )

Create a ROOT 2D histogram with category bins on the x axis and category bins on the y axis.

Parameters
nameName of the histogram.
x_labelTitle of the x axis.
xcategoriesvector of string categories
y_labelTitle of the x axis.
ycategoriesvector of string categories
weightedwhether to track the sum of squared weights separately

Definition at line 285 of file HistogramPool.cxx.

289 {
290 insert(
291 name,
292 [&]() {
293 auto [nybins, ymin, ymax] = categoryBins(ycategories);
294 auto [nxbins, xmin, xmax] = categoryBins(xcategories);
295 auto hist =
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());
301 return hist;
302 },
303 weighted);
304}

References insert().

◆ fill() [1/2]

template<typename T >
void framework::HistogramPool::fill ( const std::string & name,
const T & val )
inline

Fill a 1D histogram.

Uses the current setting of the weight.

Parameters
namename of the histogram to fill
valvalue to fill

Definition at line 367 of file HistogramPool.h.

367 {
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 }
TH1 * get(const std::string &name)
get a histogram from this pool by name
double the_weight_
The weight to fill histograms with.

References get(), and the_weight_.

Referenced by dqm::EcalClusterAnalyzer::analyze(), dqm::EcalDigiVerifier::analyze(), dqm::EcalMipTrackingFeatures::analyze(), dqm::EcalPnetVetoResults::analyze(), dqm::EcalShowerFeatures::analyze(), dqm::EcalVetoResults::analyze(), dqm::EcalWABRecResults::analyze(), dqm::HcalGeometryVerifier::analyze(), dqm::HcalInefficiencyAnalyzer::analyze(), dqm::HCalRawDigi::analyze(), dqm::HcalVetoResults::analyze(), dqm::HgcrocPulseTruthAnalyzer::analyze(), dqm::PhotoNuclearDQM::analyze(), dqm::SampleValidation::analyze(), dqm::SimObjects::analyze(), dqm::Trigger::analyze(), dqm::TrigScintClusterDQM::analyze(), dqm::TrigScintDigiVerifier::analyze(), dqm::TrigScintDQM::analyze(), dqm::TrigScintHitDQM::analyze(), dqm::TrigScintTrackDQM::analyze(), dqm::TrkDeDxMassEstFeatures::analyze(), dqm::VisiblesCutflow::analyze(), dqm::VisiblesFeatureProducer::analyze(), ldmx::ecal::TrigPrimResolutionAnalyzer::analyze(), tracking::dqm::StraightTracksDQM::analyze(), tracking::dqm::TrackerDigiDQM::analyze(), tracking::dqm::TrackingRecoDQM::analyze(), dqm::PhotoNuclearDQM::findParticleKinematics(), dqm::PhotoNuclearDQM::findRecoilProperties(), dqm::PhotoNuclearDQM::findSubleadingKinematics(), dqm::DarkBremInteraction::produce(), and tracking::dqm::TrackingRecoDQM::trackStateMonitoring().

◆ fill() [2/2]

template<typename Tx , typename Ty >
void framework::HistogramPool::fill ( const std::string & name,
const Tx & valx,
const Ty & valy )
inline

Fill a 2D histogram.

Uses the current setting of the weight.

Parameters
namename of the histogram to fill
valxx value to fill
valyy value to fill

Definition at line 413 of file HistogramPool.h.

413 {
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 }

References get(), and the_weight_.

◆ fillw() [1/2]

template<typename T >
void framework::HistogramPool::fillw ( const std::string & name,
const T & val,
double w )
inline

Fill a 1D histogram.

Using the input weight.

Parameters
namename of the histogram to fill
valvalue to fill
wweight to fill with

Definition at line 390 of file HistogramPool.h.

390 {
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 }

References get().

◆ fillw() [2/2]

template<typename Tx , typename Ty >
void framework::HistogramPool::fillw ( const std::string & name,
const Tx & valx,
const Ty & valy,
double w )
inline

Fill a 2D histogram.

Using the input weight.

Parameters
namename of the histogram to fill
valxx value to fill
valyy value to fill
wweight to fill with

Definition at line 437 of file HistogramPool.h.

438 {
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 }

References get().

◆ get()

TH1 * framework::HistogramPool::get ( const std::string & name)

get a histogram from this pool by name

Definition at line 17 of file HistogramPool.cxx.

17 {
18 auto histo = histograms_.find(name);
19 if (histo == histograms_.end()) {
20 EXCEPTION_RAISE(
21 "InvalidArg",
22 "Histogram " + name +
23 " not found in pool."
24 "\nMake sure to `histograms_.create` in onProcessStart for any "
25 "histogram you want to `histograms_.fill`.");
26 }
27
28 return histograms_[name];
29}

References histograms_.

Referenced by fill(), fill(), fillw(), and fillw().

◆ insert()

void framework::HistogramPool::insert ( const std::string & name,
std::function< TH1 *()> factory,
bool weighted )
private

insert a histogram into this pool by name

Note
The fill method assumes we are creating TH1F or TH2F histograms which is what is implemented in the create methods.
Parameters
[in]namename to store histogram under in the pool, the create methods below ensure that the name in the pool and the name in the output file are the same
[in]factorythe function that creates the histogram which is called after we go into the appropriate output directory, the returned pointer is then put into the pool after checking that it doesn't exist yet.
[in]weightedtrue if histogram is weighted (we then call Sumw2) or false otherwise
Exceptions
framework::Exceptionif the passed name already exists

The histogram needs to be created after we cd into the appropriate output directory which is why we pass in the factory function that creates the histogram into this function rather than a pointer to the already created histogram itself.

Definition at line 31 of file HistogramPool.cxx.

32 {
33 if (histograms_.find(name) != histograms_.end()) {
34 EXCEPTION_RAISE(
35 "RepeatName",
36 "Histogram " + name +
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 "
40 "specific histogram "
41 "if you want to do some other customizations besides filling.");
42 }
43
44 get_directory_()->cd();
45
52 auto hist = factory();
53 if (weighted) hist->Sumw2();
54 histograms_[name] = hist;
55}

References get_directory_, and histograms_.

Referenced by create(), create(), create(), create(), create(), create(), create(), create(), create(), create(), create(), and create().

◆ setWeight()

void framework::HistogramPool::setWeight ( double w)
inline

Set the weight for filling the histograms.

Definition at line 153 of file HistogramPool.h.

153{ the_weight_ = w; }

References the_weight_.

Referenced by dqm::DarkBremInteraction::produce().

Member Data Documentation

◆ get_directory_

std::function<TDirectory*()> framework::HistogramPool::get_directory_
private

the callback to get the directory these histograms should go in

This needs to be dynamic so that the directory is only created upon request.

Note
the returned TDirectory pointer is immediately de-referenced, so we will get a segmentation fault if the function stored here returns an invalid address (such as nullptr).

Definition at line 123 of file HistogramPool.h.

Referenced by insert().

◆ histograms_

std::unordered_map<std::string, TH1*> framework::HistogramPool::histograms_
private

the pool of histogram pointers

Definition at line 112 of file HistogramPool.h.

Referenced by get(), and insert().

◆ the_weight_

double framework::HistogramPool::the_weight_ {1.}
private

The weight to fill histograms with.

Definition at line 110 of file HistogramPool.h.

110{1.};

Referenced by fill(), fill(), and setWeight().


The documentation for this class was generated from the following files: