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

Description

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

CorrectionlibSFApplier 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 3 or 4 methods need to be implemented in the subclass:

  • inputs() to retrieve the correctionlib arguments for an object;
  • passes() to check whether an object should pass the cut;
  • varied() to extract up/down variations;
  • Optionally, binIndex() to retrieve the bin index in the histogram with the scale factors (for bin-wise correlations).

In addition, the constructor should add systematic uncertainties using addBinWiseUnc() and addGlobalUnc() (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(), inputs(), 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.

Note
This class is reentrant but not thread-safe.

#include <GenericSFApplier.h>

+ Collaboration diagram for CorrectionlibSFApplier< Object, Context >:

Public Member Functions

 CorrectionlibSFApplier (const std::filesystem::path &filePath, const std::string &name, bool correction, bool uncertainties)
 
virtual ~CorrectionlibSFApplier () 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 Member Functions

void addBinWiseUnc (const std::string &name, const correction::Variable::Type &input)
 
void addGlobalUnc (const std::string &name, const correction::Variable::Type &input)
 
virtual bool passes (const Object &obj, const Context &... ctx) const =0
 
virtual void inputs (std::vector< correction::Variable::Type > &inputs, const Object &obj, const Context &... ctx) const =0
 
virtual std::pair< double, double > varied (std::vector< correction::Variable::Type > &inputs, double sf, const correction::Variable::Type &uncname) const =0
 
virtual int binIndex (const Object &obj, const Context &... ctx) const
 

Protected Attributes

correction::Correction::Ref m_cref
 

Private Attributes

bool m_correction
 
bool m_uncertainties
 
std::map< std::string, correction::Variable::Type > m_bin_wise_uncs
 
std::map< std::string, correction::Variable::Type > m_global_uncs
 
std::vector< correction::Variable::Type > m_cached_inputs
 

Constructor & Destructor Documentation

◆ CorrectionlibSFApplier()

CorrectionlibSFApplier ( const std::filesystem::path &  filePath,
const std::string &  name,
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
423  : m_correction(correction)
424  , m_uncertainties(uncertainties)
425 {
426  namespace fs = std::filesystem;
427 
428  if (!correction)
429  return;
430 
431  // Check that the file exists
432  if (!fs::exists(filePath))
433  BOOST_THROW_EXCEPTION(
434  fs::filesystem_error("Not found",
435  filePath,
436  make_error_code(std::errc::no_such_file_or_directory)));
437 
438  auto cset = correction::CorrectionSet::from_file(filePath);
439  m_cref = cset->at(name);
440 }

◆ ~CorrectionlibSFApplier()

virtual ~CorrectionlibSFApplier ( )
virtualdefaultnoexcept

Member Function Documentation

◆ addBinWiseUnc()

void addBinWiseUnc ( const std::string &  name,
const correction::Variable::Type &  input 
)
protected

Loads a systematic with bin-by-bin correlations.

See also
addGlobalUnc()

The correction 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.

535 {
536  if (!m_correction || !m_uncertainties) return;
537  m_bin_wise_uncs[name] = std::move(input);
538 }

◆ addGlobalUnc()

void addGlobalUnc ( const std::string &  name,
const correction::Variable::Type &  input 
)
protected

Loads a fully correlated systematic.

See also
addBinWiseUnc()

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.

551 {
552  if (!m_correction || !m_uncertainties) return;
553  m_global_uncs[name] = std::move(input);
554 }

◆ binIndex()

virtual int binIndex ( const Object &  obj,
const Context &...  ctx 
) const
inlineprotectedvirtual

Called to retrieve the bin index to use for bin-wise uncertainties.

This function can be ignored if all uncertainties are global.

408  { return 0; }

◆ inputs()

virtual void inputs ( std::vector< correction::Variable::Type > &  inputs,
const Object &  obj,
const Context &...  ctx 
) const
protectedpure virtual

Called to retrieve the inputs for correctionlib.

The vector passed as first argument should be filled with the inputs to fetch the nominal scale factor with correction::Correction::evaluate.

◆ operator()() [1/2]

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

Applies the selection and scale factors to a single object.

448 {
449  auto& weights = obj.weights;
450 
451  // For safety
452  if (weights.empty()) weights.emplace_back(DAS::Weight{1, 0});
453 
454  const auto old = weights.front().v;
455 
456  // Apply the cut
457  if (!passes(obj, ctx...)) {
458  weights *= 0;
459  return; // Skip applying weights
460  }
461 
462  // Nominal correction
463  if (!m_correction) return;
464 
465  m_cached_inputs.clear();
466  inputs(m_cached_inputs, obj, ctx...);
467  const auto sf = m_cref->evaluate(m_cached_inputs);
468 
469  weights *= sf;
470 
471  // Systematics
472  if (!m_uncertainties) return;
473 
474  // The order of the next two blocks must match weightNames()
475  if (!m_bin_wise_uncs.empty()) {
476  const int bin = binIndex(obj, ctx...);
477  for (const auto& [_, uncname] : m_bin_wise_uncs) {
478  const auto& [up, down] = varied(m_cached_inputs, sf, uncname);
479  weights.push_back({old * up, bin});
480  weights.push_back({old * down, bin});
481  }
482  }
483 
484  for (const auto& [_, uncname] : m_global_uncs) {
485  const auto& [up, down] = varied(m_cached_inputs, sf, uncname);
486  weights.push_back({old * up, 0});
487  weights.push_back({old * down, 0});
488  }
489 }

◆ operator()() [2/2]

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

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

497 {
498  for (auto& obj: objects) {
499  (*this)(obj, ctx...);
500  }
501 }

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

◆ varied()

virtual std::pair<double, double> varied ( std::vector< correction::Variable::Type > &  inputs,
double  sf,
const correction::Variable::Type &  uncname 
) const
protectedpure virtual

Called to retrieve the up and down variations of a scale factor.

The value comes from addBinWiseUnc or addGlobalUnc.

Implemented in IDApplier.

◆ weightNames()

std::vector< std::string > weightNames

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

510 {
511  std::vector<std::string> names;
512  // The order of the next two blocks must match the call operator
513  for (const auto& [name, _] : m_bin_wise_uncs) {
514  names.push_back(name + SysUp);
515  names.push_back(name + SysDown);
516  }
517  for (const auto& [name, _] : m_global_uncs) {
518  names.push_back(name + SysUp);
519  names.push_back(name + SysDown);
520  }
521  return names;
522 }

Member Data Documentation

◆ m_bin_wise_uncs

std::map<std::string, correction::Variable::Type> m_bin_wise_uncs
private

Inputs for uncertainties correlated bin-by-bin.

◆ m_cached_inputs

std::vector<correction::Variable::Type> m_cached_inputs
mutableprivate

Cached inputs to reduce allocations.

◆ m_correction

bool m_correction
private

Apply the scale factors?

◆ m_cref

correction::Correction::Ref m_cref
protected

Correction to be used.

◆ m_global_uncs

std::map<std::string, correction::Variable::Type> m_global_uncs
private

Inputs for fully correlated uncertainties.

◆ 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::CorrectionlibSFApplier::binIndex
virtual int binIndex(const Object &obj, const Context &... ctx) const
Called to retrieve the bin index to use for bin-wise uncertainties.
Definition: GenericSFApplier.h:407
DAS::CorrectionlibSFApplier::m_correction
bool m_correction
Apply the scale factors?
Definition: GenericSFApplier.h:346
DAS::SysUp
const std::string SysUp
Suffix used for "up" uncertainties. Follows the Combine convention.
Definition: Format.h:8
jercExample.cset
cset
Definition: jercExample.py:90
DAS::CorrectionlibSFApplier::m_cached_inputs
std::vector< correction::Variable::Type > m_cached_inputs
Cached inputs to reduce allocations.
Definition: GenericSFApplier.h:355
DAS::CorrectionlibSFApplier::varied
virtual std::pair< double, double > varied(std::vector< correction::Variable::Type > &inputs, double sf, const correction::Variable::Type &uncname) const =0
Called to retrieve the up and down variations of a scale factor.
DAS::CorrectionlibSFApplier::inputs
virtual void inputs(std::vector< correction::Variable::Type > &inputs, const Object &obj, const Context &... ctx) const =0
Called to retrieve the inputs for correctionlib.
DAS::CorrectionlibSFApplier::m_cref
correction::Correction::Ref m_cref
Correction to be used.
Definition: GenericSFApplier.h:343
DAS::CorrectionlibSFApplier::m_bin_wise_uncs
std::map< std::string, correction::Variable::Type > m_bin_wise_uncs
Inputs for uncertainties correlated bin-by-bin.
Definition: GenericSFApplier.h:350
DYToLL_M-50_13TeV_pythia8_cff_GEN_SIM_RECOBEFMIX_DIGI_L1_DIGI2RAW_L1Reco_RECO.input
input
Definition: DYToLL_M-50_13TeV_pythia8_cff_GEN_SIM_RECOBEFMIX_DIGI_L1_DIGI2RAW_L1Reco_RECO.py:35
DAS::CorrectionlibSFApplier::m_global_uncs
std::map< std::string, correction::Variable::Type > m_global_uncs
Inputs for fully correlated uncertainties.
Definition: GenericSFApplier.h:352
DAS::CorrectionlibSFApplier::m_uncertainties
bool m_uncertainties
Calculate uncertainties?
Definition: GenericSFApplier.h:347
DAS::CorrectionlibSFApplier::passes
virtual bool passes(const Object &obj, const Context &... ctx) const =0
Called to check whether an object passes the selection.
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
jercExample.sf
sf
Definition: jercExample.py:112
weights
DAS::Weights weights
Definition: classes.h:12