ATLAS Offline Software
Loading...
Searching...
No Matches
SFHelpers.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4
5#include "SFHelpers.h"
9#include <cmath>
10#include <xAODEgamma/Electron.h>
11
14namespace asg {
15ANA_MSG_HEADER(SFHelper)
16ANA_MSG_SOURCE(SFHelper, "")
17}
18
19int
22 const xAOD::Electron& el,
23 double& nominalSF,
24 double& totalPos,
25 double& totalNeg,
26 const bool isToys)
27{
28 using namespace asg::SFHelper;
30 setMsgLevel(MSG::INFO);
31
32 CP::SystematicSet systs = tool->recommendedSystematics();
33 if (!isToys) {
34 /*
35 * Split the variation in up and down
36 * Do it before any loop
37 * This is obviously more of a sanity check
38 * as they are currently symmetric so is mainly
39 * about inspecting them both
40 */
41 std::vector<CP::SystematicVariation> positiveVar{};
42 std::vector<CP::SystematicVariation> negativeVar{};
43 for (const auto& sys : systs) {
44 float param = sys.parameter();
45 if (param < 0) {
46 negativeVar.push_back(sys);
47 } else {
48 positiveVar.push_back(sys);
49 }
50 }
51 // Helper function as a lamda
52 auto totalSyst =
53 [&tool](const xAOD::Electron& el,
54 const std::vector<CP::SystematicVariation>& variations,
55 const double nominal) {
56 double total2{};
57 double systematic{};
58 for (const auto& sys : variations) {
59 if (tool->applySystematicVariation({ sys }) != StatusCode::SUCCESS ||
60 tool->getEfficiencyScaleFactor(el, systematic) !=
62 ANA_MSG_ERROR("Error in setting/getting " << sys.name());
63 return -999.0;
64 }
65 total2 += (nominal - systematic) * (nominal - systematic);
66 }
67 return std::sqrt(total2);
68 };
69
70 // Do the work
71 // Empty variation is the nominal
72 ANA_CHECK(tool->applySystematicVariation({}));
73 ANA_CHECK(tool->getEfficiencyScaleFactor(el, nominalSF) ==
75 totalNeg = totalSyst(el, negativeVar, nominalSF);
76 totalPos = totalSyst(el, positiveVar, nominalSF);
77 } else {
79 sysVec.addGroup("toys");
80 sysVec.setToys(tool->getNumberOfToys());
81 sysVec.calc(systs);
82 std::vector<CP::SystematicSet> toys = sysVec.result("toys");
83 std::vector<double> toysVal{};
84 toysVal.reserve(toys.size());
85
86 // Do the work
87 for (const auto& sys : toys) {
88 double systematic{};
89 ANA_CHECK(tool->applySystematicVariation(sys) == StatusCode::SUCCESS &&
90 tool->getEfficiencyScaleFactor(el, systematic) ==
92 ANA_MSG_DEBUG(tool->appliedSystematics().name()
93 << " toy Result : " << systematic);
94 toysVal.push_back(systematic);
95 }
96 /*
97 * B. P. Welford 1962
98 * Donald KnutArt of Computer Programming, Vol 2, page
99 * 232, 3rd edition
100 */
101 // 1st element,initilize
102 double meanK{ toysVal[0] }; // current mean
103 double meanK_1{ toysVal[0] }; // next mean
104 double s{ 0 };
105 size_t k{ 1 };
106 const size_t N = toysVal.size();
107 // subsequent ones
108 for (size_t i = 1; i != N; ++i) {
109 const double x{ toysVal[i] };
110 const double invk{ (1.0 / (++k)) };
111 meanK_1 = meanK + (x - meanK) * invk;
112 s += (x - meanK_1) * (x - meanK);
113 meanK = meanK_1;
114 }
115 const double variance = s / (N - 1);
116 nominalSF = meanK;
117 totalNeg = sqrt(variance);
118 totalPos = sqrt(variance);
119 }
120 return 0;
121}
macros for messaging and checking status codes
#define ANA_MSG_ERROR(xmsg)
Macro printing error messages.
#define ANA_MSG_DEBUG(xmsg)
Macro printing debug messages.
#define ANA_CHECK(EXP)
check whether the given expression was successful
#define ANA_MSG_HEADER(NAME)
for standalone code this creates a new message category
#define ANA_MSG_SOURCE(NAME, TITLE)
the source code part of ANA_MSG_SOURCE
#define ANA_CHECK_SET_TYPE(TYPE)
set the type for ANA_CHECK to report failures
#define x
@ Ok
The correction was done successfully.
This class handles turning the list of systematics into the actual list of nuisance parameter points ...
void addGroup(const std::string &val_label)
finish configuration for this group and add a new one
void setToys(unsigned val_toys)
set the number of toys to run for this group
const std::vector< SystematicSet > & result(const std::string &label) const
the list of nuisance parameter points generated with the given label
void calc(const SystematicSet &sysList)
fill in result
Class to wrap a set of SystematicVariations.
an "initializing" ToolHandle for stand-alone applications
int result(asg::StandaloneToolHandle< IAsgElectronEfficiencyCorrectionTool > &tool, const xAOD::Electron &el, double &nominalSF, double &totalPos, double &totalNeg, const bool isToys)
Definition SFHelpers.cxx:20
Electron_v1 Electron
Definition of the current "egamma version".