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

Description

template<class Object, class... Context>
class DAS::GenericSFApplier< 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.

GenericSFApplier 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 is 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 GenericSFApplier< Object, Context >:

Public Member Functions

 GenericSFApplier (const std::filesystem::path &filePath, bool correction, bool uncertainties)
 
virtual ~GenericSFApplier () 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.

111  {
115  UseBinError,
116  };

Constructor & Destructor Documentation

◆ GenericSFApplier()

GenericSFApplier ( 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
157  : m_correction(correction)
158  , m_uncertainties(uncertainties)
159 {
160  namespace fs = std::filesystem;
161 
162  if (!correction)
163  return;
164 
165  // Check that the file exists
166  if (!fs::exists(filePath))
167  BOOST_THROW_EXCEPTION(
168  fs::filesystem_error("Not found",
169  filePath,
170  make_error_code(std::errc::no_such_file_or_directory)));
171 
172  m_file = std::make_unique<TFile>(filePath.c_str(), "READ");
173 }

◆ ~GenericSFApplier()

virtual ~GenericSFApplier ( )
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.

277 {
278  if (!m_correction || !m_uncertainties) return;
279  auto hist = getHistSafe(m_file, histPath);
280  if (intp == UseBinError) // We always store variations in the bin contents
281  for (int i = 0; i <= hist->GetNcells(); ++i)
282  hist->SetBinContent(i, hist->GetBinError(i));
283  m_bin_wise_uncs[name] = std::move(hist);
284 }

◆ 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.

297 {
298  if (!m_correction || !m_uncertainties) return;
299  auto hist = getHistSafe(m_file, histPath);
300  if (intp == UseBinError) // We always store variations in the bin contents
301  for (int i = 0; i <= hist->GetNcells(); ++i)
302  hist->SetBinContent(i, hist->GetBinError(i));
303  m_global_uncs[name] = std::move(hist);
304 }

◆ loadNominal()

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

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

261 {
262  if (!m_correction) return;
263  m_nominal = getHistSafe(m_file, histPath);
264 }

◆ operator()() [1/2]

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

Applies the selection and scale factors to a single object.

182 {
183  auto& weights = weightsRef(obj);
184 
185  // For safety
186  if (weights.empty()) weights.emplace_back(DAS::Weight{1, 0});
187 
188  const float old = weights.front().v;
189  if (old == 0) return; // Don't bother
190 
191  // Apply the cut
192  if (!passes(obj, ctx...)) {
193  weights = {{0, 0}};
194  return; // Skip applying weights
195  }
196 
197  // Nominal correction
198  if (!m_correction) return;
199 
200  const int bin = binIndex(obj, ctx..., m_nominal);
201  const float sf = m_nominal->GetBinContent(bin);
202  weights *= sf;
203 
204  // Systematics
205  if (!m_uncertainties) return;
206 
207  // The order of the next two blocks must match weightNames()
208  for (const auto& [_, hist] : m_bin_wise_uncs) {
209  const float variation = hist->GetBinContent(bin);
210  weights.push_back({old * (sf + variation), bin});
211  weights.push_back({old * (sf - variation), bin});
212  }
213 
214  for (const auto& [_, hist] : m_global_uncs) {
215  const float variation = hist->GetBinContent(bin);
216  weights.push_back({old * (sf + variation), 0});
217  weights.push_back({old * (sf - variation), 0});
218  }
219 }

◆ operator()() [2/2]

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

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

227 {
228  for (auto& obj: objects) {
229  (*this)(obj, ctx...);
230  }
231 }

◆ 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.

240 {
241  std::vector<std::string> names;
242  // The order of the next two blocks must match the call operator
243  for (const auto& [name, _] : m_bin_wise_uncs) {
244  names.push_back(name + SysUp);
245  names.push_back(name + SysDown);
246  }
247  for (const auto& [name, _] : m_global_uncs) {
248  names.push_back(name + SysUp);
249  names.push_back(name + SysDown);
250  }
251  return names;
252 }

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::GenericSFApplier::passes
virtual bool passes(const Object &obj, const Context &... ctx) const =0
Called to check whether an object passes the selection.
DAS::SysUp
const std::string SysUp
Suffix used for "up" uncertainties. Follows the Combine convention.
Definition: Format.h:8
DAS::GenericSFApplier::UseBinContent
@ UseBinContent
The histogram contains the uncertainty in the scale factors.
Definition: GenericSFApplier.h:113
DAS::GenericSFApplier::m_uncertainties
bool m_uncertainties
Calculate uncertainties?
Definition: GenericSFApplier.h:90
DAS::GenericSFApplier::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::GenericSFApplier::m_global_uncs
std::map< std::string, std::unique_ptr< TH1 > > m_global_uncs
Histograms with uncertainties correlated.
Definition: GenericSFApplier.h:97
DAS::GenericSFApplier::m_file
std::unique_ptr< TFile > m_file
SFs are loaded from this file.
Definition: GenericSFApplier.h:88
DAS::GenericSFApplier::UseBinError
@ UseBinError
The histogram errors contain the uncertainty in the scale factors.
Definition: GenericSFApplier.h:115
DAS::GenericSFApplier::m_nominal
std::unique_ptr< TH1 > m_nominal
Histogram with the nominal SFs.
Definition: GenericSFApplier.h:93
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:22
DAS::Weight
Definition: Weight.h:16
DAS::GenericSFApplier::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:95
DAS::SysDown
const std::string SysDown
Suffix used for "down" uncertainties. Follows the Combine convention.
Definition: Format.h:10
DAS::GenericSFApplier::m_correction
bool m_correction
Apply the scale factors?
Definition: GenericSFApplier.h:89
jercExample.sf
sf
Definition: jercExample.py:112
DAS::weightsRef
Weights & weightsRef(Object &obj)
Customization point for how weights are retrieved from an object.
Definition: GenericSFApplier.h:41
weights
DAS::Weights weights
Definition: classes.h:12