DAS  3.0
Das Analysis System
GenericHistSFApplier< Object, Context >abstract

Description

template<class Object, class... Context>
class DAS::GenericHistSFApplier< Object, Context >

A generic base class to simplify applying scale factors.

Template Parameters
ObjectThe object type to apply scale factors for.
ContextA set of object types that are needed to derive cuts and scale factors, but to which no corrections is applied.

GenericHistSFApplier can be used in situations where one wants to apply a cut to a collection of objects (e.g. photons) and correct the efficiency of this cut in MC with scale factors. This class must be subclassed before it can be used, and two methods need to be implemented in the subclass:

  • passes() to check whether an object should pass the cut;
  • binIndex() to retrieve the bin index in the histogram with the scale factors.

In addition, the constructor should load scale factors using loadNominal() and systematic uncertainties using loadBinWiseUnc() and loadGlobalUnc() (which differ in how the uncertainty will be correlated between bins of the scale factors).

Instances of classes derived from this one are used by calling the call operator on individual objects or whole object collections. Objects that do not pass the selection will be given a weight of zero. Objects passing the selection will be reweighted by the scale factor (if the correction is enabled) and new weights will be appended for systematics (again only if enabled).

Sometimes, additional inputs are needed to derive the cut and/or the scale factor. We call this contextual information. An arbitrary number of extra arguments can be passed to the call operator for context. The applier passes them directly to passes() and binIndex(), where they can be used to implement the selection or the correction. The types of context objects must be provided as class template arguments. The functions take constant references to these types.

Each uncertainty variation must be given a unique name. The names of all weights added by this class can be retrieved using the weightNames() method. This is useful to store them in the metainfo.

#include <GenericSFApplier.h>

+ Collaboration diagram for GenericHistSFApplier< Object, Context >:

Public Member Functions

 GenericHistSFApplier (const std::filesystem::path &filePath, bool correction, bool uncertainties)
 
virtual ~GenericHistSFApplier () noexcept=default
 
void operator() (Object &object, const Context &... ctx) const
 
void operator() (std::vector< Object > &objects, const Context &... ctx) const
 
std::vector< std::string > weightNames () const
 

Protected Types

enum  Interpretation { UseBinContent, UseBinError }
 

Protected Member Functions

void loadNominal (const std::string &histPath)
 
void loadBinWiseUnc (const std::string &name, const std::string &histPath, Interpretation intp=UseBinContent)
 
void loadGlobalUnc (const std::string &name, const std::string &histPath, Interpretation intp=UseBinContent)
 
virtual bool passes (const Object &obj, const Context &... ctx) const =0
 
virtual int binIndex (const Object &obj, const Context &... ctx, const std::unique_ptr< TH1 > &hist) const =0
 

Private Attributes

std::unique_ptr< TFile > m_file
 
bool m_correction
 
bool m_uncertainties
 
std::unique_ptr< TH1 > m_nominal
 
std::map< std::string, std::unique_ptr< TH1 > > m_bin_wise_uncs
 
std::map< std::string, std::unique_ptr< TH1 > > m_global_uncs
 

Member Enumeration Documentation

◆ Interpretation

enum Interpretation
protected

How loaded histograms should be interpreted.

Enumerator
UseBinContent 

The histogram contains the uncertainty in the scale factors.

UseBinError 

The histogram errors contain the uncertainty in the scale factors.

101  {
105  UseBinError,
106  };

Constructor & Destructor Documentation

◆ GenericHistSFApplier()

GenericHistSFApplier ( const std::filesystem::path &  filePath,
bool  correction,
bool  uncertainties 
)

Constructor.

Parameters
filePathThe path of a ROOT file to load scale factors from
correctionWhether to apply scale factors
uncertaintiesWhether to store uncertainties
147  : m_correction(correction)
148  , m_uncertainties(uncertainties)
149 {
150  namespace fs = std::filesystem;
151 
152  if (!correction)
153  return;
154 
155  // Check that the file exists
156  if (!fs::exists(filePath))
157  BOOST_THROW_EXCEPTION(
158  fs::filesystem_error("Not found",
159  filePath,
160  make_error_code(std::errc::no_such_file_or_directory)));
161 
162  m_file = std::make_unique<TFile>(filePath.c_str(), "READ");
163 }

◆ ~GenericHistSFApplier()

virtual ~GenericHistSFApplier ( )
virtualdefaultnoexcept

Member Function Documentation

◆ binIndex()

virtual int binIndex ( const Object &  obj,
const Context &...  ctx,
const std::unique_ptr< TH1 > &  hist 
) const
protectedpure virtual

Called to retrieve the bin to use in the scale factor histograms.

The histogram passed as second parameter is always the nominal one, and it is assumed that the bin index is the same for uncertainty variations (that is, we assume that the binnings match).

◆ loadBinWiseUnc()

void loadBinWiseUnc ( const std::string &  name,
const std::string &  histPath,
Interpretation  intp = UseBinContent 
)
protected

Loads a systematic with bin-by-bin correlations.

See also
loadGlobalUnc()

The histogram is expected to contain the (symmetric) uncertainty in the scale factor. This function has no effect when not applying the correction or when uncertainties are disabled.

266 {
267  if (!m_correction || !m_uncertainties) return;
268  auto hist = getHistSafe(m_file, histPath);
269  if (intp == UseBinError) // We always store variations in the bin contents
270  for (int i = 0; i <= hist->GetNcells(); ++i)
271  hist->SetBinContent(i, hist->GetBinError(i));
272  m_bin_wise_uncs[name] = std::move(hist);
273 }

◆ loadGlobalUnc()

void loadGlobalUnc ( const std::string &  name,
const std::string &  histPath,
Interpretation  intp = UseBinContent 
)
protected

Loads a fully correlated systematic.

See also
loadGlobalUnc()

The histogram is expected to contain the (symmetric) uncertainty in the scale factor. This function has no effect when not applying the correction or when uncertainties are disabled.

286 {
287  if (!m_correction || !m_uncertainties) return;
288  auto hist = getHistSafe(m_file, histPath);
289  if (intp == UseBinError) // We always store variations in the bin contents
290  for (int i = 0; i <= hist->GetNcells(); ++i)
291  hist->SetBinContent(i, hist->GetBinError(i));
292  m_global_uncs[name] = std::move(hist);
293 }

◆ loadNominal()

void loadNominal ( const std::string &  histPath)
protected

Loads the histogram with the nominal scale factor. Has no effect when not applying the correction.

250 {
251  if (!m_correction) return;
252  m_nominal = getHistSafe(m_file, histPath);
253 }

◆ operator()() [1/2]

void operator() ( Object &  object,
const Context &...  ctx 
) const

Applies the selection and scale factors to a single object.

172 {
173  auto& weights = obj.weights;
174 
175  // For safety
176  if (weights.empty()) weights.emplace_back(DAS::Weight{1, 0});
177 
178  const float old = weights.front().v;
179 
180  // Apply the cut
181  if (!passes(obj, ctx...)) {
182  weights *= 0;
183  return; // Skip applying weights
184  }
185 
186  // Nominal correction
187  if (!m_correction) return;
188 
189  const int bin = binIndex(obj, ctx..., m_nominal);
190  const float sf = m_nominal->GetBinContent(bin);
191  weights *= sf;
192 
193  // Systematics
194  if (!m_uncertainties) return;
195 
196  // The order of the next two blocks must match weightNames()
197  for (const auto& [_, hist] : m_bin_wise_uncs) {
198  const float variation = hist->GetBinContent(bin);
199  weights.push_back({old * (sf + variation), bin});
200  weights.push_back({old * (sf - variation), bin});
201  }
202 
203  for (const auto& [_, hist] : m_global_uncs) {
204  const float variation = hist->GetBinContent(bin);
205  weights.push_back({old * (sf + variation), 0});
206  weights.push_back({old * (sf - variation), 0});
207  }
208 }

◆ operator()() [2/2]

void operator() ( std::vector< Object > &  objects,
const Context &...  ctx 
) const

Applies the selection and scale factors to objects in a vector.

216 {
217  for (auto& obj: objects) {
218  (*this)(obj, ctx...);
219  }
220 }

◆ passes()

virtual bool passes ( const Object &  obj,
const Context &...  ctx 
) const
protectedpure virtual

Called to check whether an object passes the selection.

Note
As an optimization, this is only called when the object weight is not already zero.

◆ weightNames()

std::vector< std::string > weightNames

Retrieves the name of weights added by this applier. Returns an empty vector when uncertainties are disabled.

229 {
230  std::vector<std::string> names;
231  // The order of the next two blocks must match the call operator
232  for (const auto& [name, _] : m_bin_wise_uncs) {
233  names.push_back(name + SysUp);
234  names.push_back(name + SysDown);
235  }
236  for (const auto& [name, _] : m_global_uncs) {
237  names.push_back(name + SysUp);
238  names.push_back(name + SysDown);
239  }
240  return names;
241 }

Member Data Documentation

◆ m_bin_wise_uncs

std::map<std::string, std::unique_ptr<TH1> > m_bin_wise_uncs
private

Histograms with uncertainties correlated bin-by-bin.

◆ m_correction

bool m_correction
private

Apply the scale factors?

◆ m_file

std::unique_ptr<TFile> m_file
private

SFs are loaded from this file.

◆ m_global_uncs

std::map<std::string, std::unique_ptr<TH1> > m_global_uncs
private

Histograms with uncertainties correlated.

◆ m_nominal

std::unique_ptr<TH1> m_nominal
private

Histogram with the nominal SFs.

◆ m_uncertainties

bool m_uncertainties
private

Calculate uncertainties?


The documentation for this class was generated from the following file:
DYToLL_M-50_13TeV_pythia8_cff_GEN_SIM_RECOBEFMIX_DIGI_L1_DIGI2RAW_L1Reco_RECO.name
name
Definition: DYToLL_M-50_13TeV_pythia8_cff_GEN_SIM_RECOBEFMIX_DIGI_L1_DIGI2RAW_L1Reco_RECO.py:48
DAS::GenericHistSFApplier::m_bin_wise_uncs
std::map< std::string, std::unique_ptr< TH1 > > m_bin_wise_uncs
Histograms with uncertainties correlated bin-by-bin.
Definition: GenericSFApplier.h:86
DAS::GenericHistSFApplier::UseBinError
@ UseBinError
The histogram errors contain the uncertainty in the scale factors.
Definition: GenericSFApplier.h:105
DAS::GenericHistSFApplier::m_correction
bool m_correction
Apply the scale factors?
Definition: GenericSFApplier.h:80
DAS::SysUp
const std::string SysUp
Suffix used for "up" uncertainties. Follows the Combine convention.
Definition: Format.h:8
DAS::GenericHistSFApplier::binIndex
virtual int binIndex(const Object &obj, const Context &... ctx, const std::unique_ptr< TH1 > &hist) const =0
Called to retrieve the bin to use in the scale factor histograms.
DAS::GenericHistSFApplier::m_global_uncs
std::map< std::string, std::unique_ptr< TH1 > > m_global_uncs
Histograms with uncertainties correlated.
Definition: GenericSFApplier.h:88
DAS::GenericHistSFApplier::passes
virtual bool passes(const Object &obj, const Context &... ctx) const =0
Called to check whether an object passes the selection.
DAS::getHistSafe
std::unique_ptr< TH1 > getHistSafe(std::unique_ptr< TFile > &f, const std::string &name)
Gets an histogram from a TFile.
Definition: GenericSFApplier.h:24
DAS::Weight
Definition: Weight.h:16
DAS::SysDown
const std::string SysDown
Suffix used for "down" uncertainties. Follows the Combine convention.
Definition: Format.h:10
DAS::GenericHistSFApplier::UseBinContent
@ UseBinContent
The histogram contains the uncertainty in the scale factors.
Definition: GenericSFApplier.h:103
jercExample.sf
sf
Definition: jercExample.py:112
weights
DAS::Weights weights
Definition: classes.h:12
DAS::GenericHistSFApplier::m_nominal
std::unique_ptr< TH1 > m_nominal
Histogram with the nominal SFs.
Definition: GenericSFApplier.h:84
DAS::GenericHistSFApplier::m_file
std::unique_ptr< TFile > m_file
SFs are loaded from this file.
Definition: GenericSFApplier.h:79
DAS::GenericHistSFApplier::m_uncertainties
bool m_uncertainties
Calculate uncertainties?
Definition: GenericSFApplier.h:81