ATLAS Offline Software
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 
13 #include "AsgMessaging/MsgStream.h"
14 namespace asg {
15 ANA_MSG_HEADER(SFHelper)
16 ANA_MSG_SOURCE(SFHelper, "")
17 }
18 
19 int
22  const xAOD::Electron& el,
23  double& nominalSF,
24  double& totalPos,
25  double& totalNeg,
26  const bool isToys)
27 {
28  using namespace asg::SFHelper;
29  ANA_CHECK_SET_TYPE(int);
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 }
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
CP::MakeSystematicsVector
This class handles turning the list of systematics into the actual list of nuisance parameter points ...
Definition: MakeSystematicsVector.h:34
yodamerge_tmp.variations
variations
Definition: yodamerge_tmp.py:257
SFHelpers::result
int result(asg::StandaloneToolHandle< IAsgElectronEfficiencyCorrectionTool > &tool, const xAOD::Electron &el, double &nominalSF, double &totalPos, double &totalNeg, const bool isToys)
Definition: SFHelpers.cxx:20
asg
Definition: DataHandleTestTool.h:28
CP::SystematicSet
Class to wrap a set of SystematicVariations.
Definition: SystematicSet.h:31
ANA_MSG_ERROR
#define ANA_MSG_ERROR(xmsg)
Macro printing error messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:294
ANA_CHECK
#define ANA_CHECK(EXP)
check whether the given expression was successful
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:324
JetTiledMap::N
@ N
Definition: TiledEtaPhiMap.h:44
AsgElectronEfficiencyCorrectionTool.h
x
#define x
mapkey::sys
@ sys
Definition: TElectronEfficiencyCorrectionTool.cxx:42
CP::MakeSystematicsVector::result
const std::vector< SystematicSet > & result(const std::string &label) const
the list of nuisance parameter points generated with the given label
Definition: MakeSystematicsVector.cxx:81
CP::MakeSystematicsVector::addGroup
void addGroup(const std::string &val_label)
finish configuration for this group and add a new one
Definition: MakeSystematicsVector.cxx:183
lumiFormat.i
int i
Definition: lumiFormat.py:92
asg::StandaloneToolHandle
an "initializing" ToolHandle for stand-alone applications
Definition: StandaloneToolHandle.h:44
top::nominal
@ nominal
Definition: ScaleFactorRetriever.h:29
MessageCheck.h
macros for messaging and checking status codes
plotIsoValidation.el
el
Definition: plotIsoValidation.py:197
asg::ANA_MSG_HEADER
ANA_MSG_HEADER(msgSTT) ANA_MSG_SOURCE(msgSTT
MakeSystematicsVector.h
ANA_MSG_SOURCE
#define ANA_MSG_SOURCE(NAME, TITLE)
the source code part of ANA_MSG_SOURCE
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:133
AtlCoolConsole.tool
tool
Definition: AtlCoolConsole.py:453
xAOD::Electron_v1
Definition: Electron_v1.h:34
CP::CorrectionCode::Ok
@ Ok
The correction was done successfully.
Definition: CorrectionCode.h:38
ANA_CHECK_SET_TYPE
#define ANA_CHECK_SET_TYPE(TYPE)
set the type for ANA_CHECK to report failures
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:314
CP::MakeSystematicsVector::setToys
void setToys(unsigned val_toys)
set the number of toys to run for this group
Definition: MakeSystematicsVector.cxx:213
CP::MakeSystematicsVector::calc
void calc(const SystematicSet &sysList)
fill in result
Definition: MakeSystematicsVector.cxx:94
Electron.h
fitman.k
k
Definition: fitman.py:528
SFHelpers.h
MsgStream.h
SystematicsUtil.h
ANA_MSG_DEBUG
#define ANA_MSG_DEBUG(xmsg)
Macro printing debug messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:288