 |
DAS
3.0
Das Analysis System
|
Go to the documentation of this file.
6 #include <correction.h>
24 inline std::unique_ptr<TH1>
getHistSafe (std::unique_ptr<TFile>&
f,
25 const std::string&
name)
28 auto h = std::unique_ptr<TH1>(
f->Get<TH1>(
name.c_str()));
33 h->SetDirectory(
nullptr);
76 template<
class Object,
class... Context>
92 bool correction,
bool uncertainties);
95 void operator() (Object&
object, const Context&... ctx) const;
96 void operator() (std::vector<Object>& objects, const Context&... ctx) const;
110 const std::string& histPath,
113 const std::string& histPath,
122 virtual bool passes (
const Object& obj,
const Context&... ctx)
const = 0;
132 const Context&... ctx,
133 const std::unique_ptr<TH1>& hist)
const = 0;
142 template<
class Object,
class... Context>
144 const std::filesystem::path& filePath,
147 : m_correction(correction)
148 , m_uncertainties(uncertainties)
150 namespace fs = std::filesystem;
156 if (!fs::exists(filePath))
157 BOOST_THROW_EXCEPTION(
158 fs::filesystem_error(
"Not found",
160 make_error_code(std::errc::no_such_file_or_directory)));
162 m_file = std::make_unique<TFile>(filePath.c_str(),
"READ");
169 template<
class Object,
class... Context>
171 Object& obj,
const Context&... ctx)
const
178 const float old =
weights.front().v;
181 if (!passes(obj, ctx...)) {
187 if (!m_correction)
return;
189 const int bin = binIndex(obj, ctx..., m_nominal);
190 const float sf = m_nominal->GetBinContent(bin);
194 if (!m_uncertainties)
return;
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});
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});
213 template<
class Object,
class... Context>
215 std::vector<Object>& objects,
const Context&... ctx)
const
217 for (
auto& obj: objects) {
218 (*this)(obj, ctx...);
226 template<
class Object,
class... Context>
227 std::vector<std::string>
230 std::vector<std::string> names;
232 for (
const auto& [
name, _] : m_bin_wise_uncs) {
236 for (
const auto& [
name, _] : m_global_uncs) {
247 template<
class Object,
class... Context>
249 const std::string& histPath)
251 if (!m_correction)
return;
263 template<
class Object,
class... Context>
267 if (!m_correction || !m_uncertainties)
return;
269 if (intp == UseBinError)
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);
283 template<
class Object,
class... Context>
287 if (!m_correction || !m_uncertainties)
return;
289 if (intp == UseBinError)
290 for (
int i = 0; i <= hist->GetNcells(); ++i)
291 hist->SetBinContent(i, hist->GetBinError(i));
292 m_global_uncs[
name] = std::move(hist);
339 template<
class Object,
class... Context>
359 const std::string&
name,
364 void operator() (Object&
object, const Context&... ctx) const;
365 void operator() (std::vector<Object>& objects, const Context&... ctx) const;
370 const correction::Variable::Type&
input);
372 const correction::Variable::Type&
input);
380 virtual
bool passes (const Object& obj, const Context&... ctx) const = 0;
390 const Context&... ctx) const = 0;
397 virtual std::pair<
double,
double>
399 const correction::Variable::Type& uncname) const = 0;
407 virtual
int binIndex (const Object& obj, const Context&... ctx)
const
417 template<
class Object,
class... Context>
419 const std::filesystem::path& filePath,
420 const std::string&
name,
423 : m_correction(correction)
424 , m_uncertainties(uncertainties)
426 namespace fs = std::filesystem;
432 if (!fs::exists(filePath))
433 BOOST_THROW_EXCEPTION(
434 fs::filesystem_error(
"Not found",
436 make_error_code(std::errc::no_such_file_or_directory)));
438 auto cset = correction::CorrectionSet::from_file(filePath);
445 template<
class Object,
class... Context>
447 Object& obj,
const Context&... ctx)
const
454 const auto old =
weights.front().v;
457 if (!passes(obj, ctx...)) {
463 if (!m_correction)
return;
465 m_cached_inputs.clear();
466 inputs(m_cached_inputs, obj, ctx...);
467 const auto sf = m_cref->evaluate(m_cached_inputs);
472 if (!m_uncertainties)
return;
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});
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});
494 template<
class Object,
class... Context>
496 std::vector<Object>& objects,
const Context&... ctx)
const
498 for (
auto& obj: objects) {
499 (*this)(obj, ctx...);
507 template<
class Object,
class... Context>
508 std::vector<std::string>
511 std::vector<std::string> names;
513 for (
const auto& [
name, _] : m_bin_wise_uncs) {
517 for (
const auto& [
name, _] : m_global_uncs) {
532 template<
class Object,
class... Context>
534 const std::string&
name,
const correction::Variable::Type&
input)
536 if (!m_correction || !m_uncertainties)
return;
537 m_bin_wise_uncs[
name] = std::move(
input);
548 template<
class Object,
class... Context>
550 const std::string&
name,
const correction::Variable::Type&
input)
552 if (!m_correction || !m_uncertainties)
return;
name
Definition: DYToLL_M-50_13TeV_pythia8_cff_GEN_SIM_RECOBEFMIX_DIGI_L1_DIGI2RAW_L1Reco_RECO.py:48
std::map< std::string, std::unique_ptr< TH1 > > m_bin_wise_uncs
Histograms with uncertainties correlated bin-by-bin.
Definition: GenericSFApplier.h:86
cerr
Definition: Ntupliser_cfg.py:105
Definition: applyBTagSF.cc:31
std::vector< std::string > weightNames() const
Retrieves the name of weights added by this applier. Returns an empty vector when uncertainties are d...
Definition: GenericSFApplier.h:509
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
A generic base class to simplify applying scale factors.
Definition: GenericSFApplier.h:341
@ UseBinError
The histogram errors contain the uncertainty in the scale factors.
Definition: GenericSFApplier.h:105
bool m_correction
Apply the scale factors?
Definition: GenericSFApplier.h:346
void addBinWiseUnc(const std::string &name, const correction::Variable::Type &input)
Loads a systematic with bin-by-bin correlations.
Definition: GenericSFApplier.h:533
bool m_correction
Apply the scale factors?
Definition: GenericSFApplier.h:80
void loadGlobalUnc(const std::string &name, const std::string &histPath, Interpretation intp=UseBinContent)
Loads a fully correlated systematic.
Definition: GenericSFApplier.h:284
const std::string SysUp
Suffix used for "up" uncertainties. Follows the Combine convention.
Definition: Format.h:8
cset
Definition: jercExample.py:90
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.
std::map< std::string, std::unique_ptr< TH1 > > m_global_uncs
Histograms with uncertainties correlated.
Definition: GenericSFApplier.h:88
f
Definition: Ntupliser_cfg.py:324
virtual ~CorrectionlibSFApplier() noexcept=default
GenericHistSFApplier(const std::filesystem::path &filePath, bool correction, bool uncertainties)
Constructor.
Definition: GenericSFApplier.h:143
std::vector< correction::Variable::Type > m_cached_inputs
Cached inputs to reduce allocations.
Definition: GenericSFApplier.h:355
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.
virtual void inputs(std::vector< correction::Variable::Type > &inputs, const Object &obj, const Context &... ctx) const =0
Called to retrieve the inputs for correctionlib.
A generic base class to simplify applying scale factors.
Definition: GenericSFApplier.h:78
correction::Correction::Ref m_cref
Correction to be used.
Definition: GenericSFApplier.h:343
CorrectionlibSFApplier(const std::filesystem::path &filePath, const std::string &name, bool correction, bool uncertainties)
Constructor.
Definition: GenericSFApplier.h:418
std::map< std::string, correction::Variable::Type > m_bin_wise_uncs
Inputs for uncertainties correlated bin-by-bin.
Definition: GenericSFApplier.h:350
void loadBinWiseUnc(const std::string &name, const std::string &histPath, Interpretation intp=UseBinContent)
Loads a systematic with bin-by-bin correlations.
Definition: GenericSFApplier.h:264
void operator()(Object &object, const Context &... ctx) const
Applies the selection and scale factors to a single object.
Definition: GenericSFApplier.h:170
input
Definition: DYToLL_M-50_13TeV_pythia8_cff_GEN_SIM_RECOBEFMIX_DIGI_L1_DIGI2RAW_L1Reco_RECO.py:35
std::map< std::string, correction::Variable::Type > m_global_uncs
Inputs for fully correlated uncertainties.
Definition: GenericSFApplier.h:352
bool m_uncertainties
Calculate uncertainties?
Definition: GenericSFApplier.h:347
virtual bool passes(const Object &obj, const Context &... ctx) const =0
Called to check whether an object passes the selection.
virtual bool passes(const Object &obj, const Context &... ctx) const =0
Called to check whether an object passes the selection.
std::vector< std::string > weightNames() const
Retrieves the name of weights added by this applier. Returns an empty vector when uncertainties are d...
Definition: GenericSFApplier.h:228
std::unique_ptr< TH1 > getHistSafe(std::unique_ptr< TFile > &f, const std::string &name)
Gets an histogram from a TFile.
Definition: GenericSFApplier.h:24
void operator()(Object &object, const Context &... ctx) const
Applies the selection and scale factors to a single object.
Definition: GenericSFApplier.h:446
const std::string SysDown
Suffix used for "down" uncertainties. Follows the Combine convention.
Definition: Format.h:10
void loadNominal(const std::string &histPath)
Loads the histogram with the nominal scale factor. Has no effect when not applying the correction.
Definition: GenericSFApplier.h:248
@ UseBinContent
The histogram contains the uncertainty in the scale factors.
Definition: GenericSFApplier.h:103
sf
Definition: jercExample.py:112
def inputs
Definition: jercExample.py:118
void addGlobalUnc(const std::string &name, const correction::Variable::Type &input)
Loads a fully correlated systematic.
Definition: GenericSFApplier.h:549
DAS::Weights weights
Definition: classes.h:12
std::unique_ptr< TH1 > m_nominal
Histogram with the nominal SFs.
Definition: GenericSFApplier.h:84
std::unique_ptr< TFile > m_file
SFs are loaded from this file.
Definition: GenericSFApplier.h:79
bool m_uncertainties
Calculate uncertainties?
Definition: GenericSFApplier.h:81
virtual ~GenericHistSFApplier() noexcept=default
Interpretation
How loaded histograms should be interpreted.
Definition: GenericSFApplier.h:101