ATLAS Offline Software
GlobalLeptonTriggerCalculator.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3  */
4 
7 #include "TopEvent/EventTools.h"
9 
11 #include "xAODEgamma/Electron.h"
12 #include "xAODMuon/MuonContainer.h"
13 #include "xAODMuon/Muon.h"
18 #include <vector>
19 
20 
21 namespace top {
23  asg::AsgTool(name),
24  m_config(nullptr),
25 
26  m_globalTriggerSF("TrigGlobalEfficiencyCorrectionTool::TrigGlobal"),
27  m_globalTriggerSFLoose("TrigGlobalEfficiencyCorrectionTool::TrigGlobalLoose"),
28 
29  m_decor_triggerSF("AnalysisTop_Trigger_SF") {
30  declareProperty("config", m_config);
31  }
32 
34  ATH_MSG_INFO(" top::GlobalLeptonTriggerCalculator initialize");
35 
36  if (m_config->doTightEvents()) top::check(m_globalTriggerSF.retrieve(),
37  "Failed to retrieve global trigger SF tool");
38  if (m_config->doLooseEvents()) top::check(
39  m_globalTriggerSFLoose.retrieve(), "Failed to retrieve global trigger SF tool (loose)");
40 
41  // Retrieve the systematic names we stored
42  for (auto& s : m_config->getGlobalTriggerElectronSystematics()) ATH_MSG_DEBUG(" - Electron systematics : " << s);
43  for (auto& s : m_config->getGlobalTriggerMuonSystematics()) ATH_MSG_DEBUG(" - Muon systematics : " << s);
44  for (auto& s : m_config->getGlobalTriggerPhotonSystematics()) ATH_MSG_DEBUG(" - Photon systematics : " << s);
45 
46  {
47  std::vector<ToolHandle<IAsgElectronEfficiencyCorrectionTool> > tools;
48  tools.reserve(m_config->getGlobalTriggerElectronTools().size());
49  for (auto const& s : m_config->getGlobalTriggerElectronTools()) {
50  ATH_MSG_DEBUG(" - Electron tools : " << s);
51  tools.emplace_back(s);
52  top::check(tools.back().retrieve(), "Failed to retrieve electron tool");
53  }
54  m_electronTools.swap(tools);
55  }
56  {
57  std::vector<ToolHandle<CP::IMuonTriggerScaleFactors> > tools;
58  tools.reserve(m_config->getGlobalTriggerMuonTools().size());
59  for (auto const& s : m_config->getGlobalTriggerMuonTools()) {
60  ATH_MSG_DEBUG(" - Muon tools : " << s);
61  tools.emplace_back(s);
62  top::check(tools.back().retrieve(), "Failed to retrieve muon tool");
63  }
64  m_muonTools.swap(tools);
65  }
66  {
67  std::vector<ToolHandle<IAsgPhotonEfficiencyCorrectionTool> > tools;
68  tools.reserve(m_config->getGlobalTriggerPhotonTools().size());
69  for (auto const& s : m_config->getGlobalTriggerPhotonTools()) {
70  ATH_MSG_DEBUG(" - Photon tools : " << s);
71  tools.emplace_back(s);
72  top::check(tools.back().retrieve(), "Failed to retrieve photon tool");
73  }
74  m_photonTools.swap(tools);
75  }
76 
77 
78  return StatusCode::SUCCESS;
79  }
80 
82  ATH_MSG_DEBUG("Entered GlobalLeptonTriggerCalculator::execute");
83 
84  auto const& nominalHash = m_config->nominalHashValue();
85  if (m_config->doTightEvents()) {
86  xAOD::SystematicEventContainer const* systEvents = nullptr;
87  top::check(evtStore()->retrieve(systEvents,
88  m_config->sgKeyTopSystematicEvents()),
89  "Failed to get xAOD::SystematicEventContainer");
90  for (const xAOD::SystematicEvent* systEvent : *systEvents) {
91  bool withScaleFactorVariations = (systEvent->hashValue() == nominalHash);
92  processEvent(systEvent, withScaleFactorVariations);
93  }
94  }
95  if (m_config->doLooseEvents()) {
96  xAOD::SystematicEventContainer const* systEvents = nullptr;
97  top::check(evtStore()->retrieve(systEvents,
98  m_config->sgKeyTopSystematicEventsLoose()),
99  "Failed to get xAOD::SystematicEventContainer");
100  for (const xAOD::SystematicEvent* systEvent : *systEvents) {
101  bool withScaleFactorVariations = (systEvent->hashValue() == nominalHash);
102  processEvent(systEvent, withScaleFactorVariations);
103  }
104  }
105 
106  ATH_MSG_DEBUG("Leaving GlobalLeptonTriggerCalculator::execute");
107  return StatusCode::SUCCESS;
108  }
109 
110  void GlobalLeptonTriggerCalculator::processEvent(const xAOD::SystematicEvent* systEvent, bool withScaleFactorVariations) {
111  auto const& hash = systEvent->hashValue();
112  bool isLoose = systEvent->isLooseEvent();
113 
114  std::string const& systname = m_config->systematicName(hash);
115  ATH_MSG_DEBUG("Calculating trigger SF for variation " << systname << " (" << hash << ") with isLoose=" << isLoose);
116 
117  // Retrieve nominal muons, retrieve nominal electrons
118  const xAOD::MuonContainer* muons(nullptr);
119  const xAOD::ElectronContainer* electrons(nullptr);
120  const xAOD::PhotonContainer* photons(nullptr);
121  std::vector<const xAOD::Muon*> selectedMuons;
122  std::vector<const xAOD::Electron*> selectedElectrons;
123  std::vector<const xAOD::Photon*> selectedPhotons;
124 
125  if (m_config->useMuons()) top::check(evtStore()->retrieve(muons, m_config->sgKeyMuons(hash)), "Failed to retrieve muons");
126 
127  // Put into a vector
128  for (size_t index : systEvent->goodMuons()) {
129  selectedMuons.push_back(muons->at(index));
130  }
131 
132  if (m_config->useElectrons()) top::check(evtStore()->retrieve(electrons, m_config->sgKeyElectrons(hash)), "Failed to retrieve electrons");
133  // Put into a vector
134  for (size_t index : systEvent->goodElectrons()) {
135  selectedElectrons.push_back(electrons->at(index));
136  }
137 
138  if (m_config->usePhotons()) top::check(evtStore()->retrieve(photons, m_config->sgKeyPhotons(hash)), "Failed to retrieve photons");
139  // Put into a vector
140  for (size_t index : systEvent->goodPhotons()) {
141  selectedPhotons.push_back(photons->at(index));
142  }
143 
144  // manage current electron trigger SF variation
145  CP::SystematicSet electronSystSet;
146  auto setCurrentElectronVariation = [this, &electronSystSet](std::string const& parameterName, int value) {
147  electronSystSet.clear();
148  if (!parameterName.empty()) electronSystSet.insert(CP::SystematicVariation(
149  parameterName, value));
150  for (auto&& tool : m_electronTools) {
151  top::check(tool->applySystematicVariation(
152  electronSystSet), "Failed to apply systematic");
153  }
154  };
155 
156  // manage current muon trigger SF variation
157  CP::SystematicSet muonSystSet;
158  auto setCurrentMuonVariation = [this, &muonSystSet](std::string const& parameterName, int value) {
159  muonSystSet.clear();
160  if (!parameterName.empty()) muonSystSet.insert(CP::SystematicVariation(
161  parameterName, value));
162  for (auto&& tool : m_muonTools) {
163  top::check(tool->applySystematicVariation(
164  muonSystSet), "Failed to apply systematic");
165  }
166  };
167 
168  // manage current photon trigger SF variation
169  CP::SystematicSet photonSystSet;
170  auto setCurrentPhotonVariation = [this, &photonSystSet](std::string const& parameterName, int value) {
171  photonSystSet.clear();
172  if (!parameterName.empty()) photonSystSet.insert(CP::SystematicVariation(
173  parameterName, value));
174  for (auto&& tool : m_photonTools) {
175  top::check(tool->applySystematicVariation(
176  photonSystSet), "Failed to apply systematic");
177  }
178  };
179 
180  // compute and store trigger SF for current variation
181  auto&& globalTriggerTool = (isLoose ? m_globalTriggerSFLoose : m_globalTriggerSF);
182  auto decorateEventForCurrentVariation = [&]() {
183  double sf;
184 
185  if (selectedElectrons.empty() && selectedMuons.empty() && selectedPhotons.empty()) {
186  sf = 1.;
187  } else {
188  sf = NAN;
189  globalTriggerTool->getEfficiencyScaleFactor(
190  selectedElectrons,
191  selectedMuons,
192  selectedPhotons,
193  sf).ignore();
194  }
195  std::string auxname(m_decor_triggerSF);
196  auxname += "_";
197  if (!(electronSystSet.empty() && muonSystSet.empty() && photonSystSet.empty())) {
198  CP::SystematicSet systSet;
199  systSet.insert(electronSystSet);
200  systSet.insert(muonSystSet);
201  systSet.insert(photonSystSet);
202  auxname += systSet.name();
203  }
204  ATH_MSG_DEBUG("Adding decoration " << auxname << " = " << sf);
205  systEvent->auxdecor<float>(auxname) = sf;
206  };
207 
209  setCurrentElectronVariation("", 0);
210  setCurrentMuonVariation("", 0);
211  setCurrentPhotonVariation("", 0);
212  decorateEventForCurrentVariation();
213 
215  if (withScaleFactorVariations) {
216  ATH_MSG_DEBUG("Calculating trigger SF variations");
217  std::unordered_set<std::string> variations;
219  for (auto& s : m_config->getGlobalTriggerElectronSystematics()) {
220  for (int val : {1, -1}) {
221  setCurrentElectronVariation(s, val);
222  decorateEventForCurrentVariation();
223  }
224  }
226  setCurrentElectronVariation("", 0);
227  for (auto& s : m_config->getGlobalTriggerMuonSystematics()) {
228  for (int val : {1, -1}) {
229  setCurrentMuonVariation(s, val);
230  decorateEventForCurrentVariation();
231  }
232  }
233  setCurrentMuonVariation("", 0);
234 
235  for (auto& s : m_config->getGlobalTriggerPhotonSystematics()) {
236  for (int val : {1, -1}) {
237  setCurrentPhotonVariation(s, val);
238  decorateEventForCurrentVariation();
239  }
240  }
241  setCurrentPhotonVariation("", 0);
242  }
243  }
244 }
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
xAOD::SystematicEvent::goodMuons
const std::vector< unsigned int > & goodMuons() const
get Muons
xAOD::SystematicEvent::goodPhotons
const std::vector< unsigned int > & goodPhotons() const
get Photons
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
top
TopConfig A simple configuration that is NOT a singleton.
Definition: AnalysisTrackingHelper.cxx:58
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
top::GlobalLeptonTriggerCalculator::m_globalTriggerSFLoose
ToolHandle< ITrigGlobalEfficiencyCorrectionTool > m_globalTriggerSFLoose
Definition: GlobalLeptonTriggerCalculator.h:61
yodamerge_tmp.variations
variations
Definition: yodamerge_tmp.py:257
IAsgElectronEfficiencyCorrectionTool.h
Muon.h
index
Definition: index.py:1
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
CP::SystematicSet::empty
bool empty() const
returns: whether the set is empty
Definition: SystematicSet.h:67
top::GlobalLeptonTriggerCalculator::m_electronTools
std::vector< ToolHandle< IAsgElectronEfficiencyCorrectionTool > > m_electronTools
Definition: GlobalLeptonTriggerCalculator.h:63
asg
Definition: DataHandleTestTool.h:28
CP::SystematicSet
Class to wrap a set of SystematicVariations.
Definition: SystematicSet.h:31
top::GlobalLeptonTriggerCalculator::m_decor_triggerSF
std::string m_decor_triggerSF
Definition: GlobalLeptonTriggerCalculator.h:67
CP::SystematicSet::name
std::string name() const
returns: the systematics joined into a single string.
Definition: SystematicSet.cxx:278
athena.value
value
Definition: athena.py:122
AsgElectronEfficiencyCorrectionTool.h
CP::SystematicVariation
Definition: SystematicVariation.h:47
SG::AuxElement::auxdecor
Decorator< T, ALLOC >::reference_type auxdecor(const std::string &name) const
Fetch an aux decoration, as a non-const reference.
EventTools.h
A few functions for doing operations on particles / events. Currently holds code for dR,...
AthCommonDataStore< AthCommonMsg< AlgTool > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
ElectronContainer.h
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
IMuonTriggerScaleFactors.h
top::GlobalLeptonTriggerCalculator::m_photonTools
std::vector< ToolHandle< IAsgPhotonEfficiencyCorrectionTool > > m_photonTools
Definition: GlobalLeptonTriggerCalculator.h:65
GlobalLeptonTriggerCalculator.h
top::check
void check(bool thingToCheck, const std::string &usefulFailureMessage)
Print an error message and terminate if thingToCheck is false.
Definition: EventTools.cxx:15
top::GlobalLeptonTriggerCalculator::m_globalTriggerSF
ToolHandle< ITrigGlobalEfficiencyCorrectionTool > m_globalTriggerSF
Definition: GlobalLeptonTriggerCalculator.h:60
top::GlobalLeptonTriggerCalculator::GlobalLeptonTriggerCalculator
GlobalLeptonTriggerCalculator(const std::string &name)
Definition: GlobalLeptonTriggerCalculator.cxx:22
xAOD::SystematicEvent::hashValue
std::size_t hashValue() const
get hash value
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
xAOD::SystematicEvent::isLooseEvent
char isLooseEvent() const
get isLooseEvent
CP::SystematicSet::insert
void insert(const SystematicVariation &systematic)
description: insert a systematic into the set
Definition: SystematicSet.cxx:88
tools
Definition: DataQuality/ZLumiScripts/python/tools/__init__.py:1
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
AtlCoolConsole.tool
tool
Definition: AtlCoolConsole.py:453
TopConfig.h
top::GlobalLeptonTriggerCalculator::m_config
std::shared_ptr< top::TopConfig > m_config
Definition: GlobalLeptonTriggerCalculator.h:58
EventInfo.h
MuonContainer.h
CP::SystematicSet::clear
void clear()
description: clear the set
Definition: SystematicSet.cxx:119
xAOD::SystematicEvent
SystematicEvent A simple xAOD class which we can persist into a mini-xAOD The xAOD EDM is way too com...
Definition: SystematicEvent.h:27
CaloCondBlobAlgs_fillNoiseFromASCII.hash
dictionary hash
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:109
mapkey::sf
@ sf
Definition: TElectronEfficiencyCorrectionTool.cxx:38
top::GlobalLeptonTriggerCalculator::processEvent
void processEvent(const xAOD::SystematicEvent *systEvent, bool withScaleFactorVariations)
Definition: GlobalLeptonTriggerCalculator.cxx:110
top::GlobalLeptonTriggerCalculator::m_muonTools
std::vector< ToolHandle< CP::IMuonTriggerScaleFactors > > m_muonTools
Definition: GlobalLeptonTriggerCalculator.h:64
Pythia8_RapidityOrderMPI.val
val
Definition: Pythia8_RapidityOrderMPI.py:14
xAOD::SystematicEvent::goodElectrons
const std::vector< unsigned int > & goodElectrons() const
get Electrons
Electron.h
DataVector::at
const T * at(size_type n) const
Access an element, as an rvalue.
SystematicEventContainer.h
InDetDD::electrons
@ electrons
Definition: InDetDD_Defs.h:17
top::GlobalLeptonTriggerCalculator::initialize
StatusCode initialize()
Dummy implementation of the initialisation function.
Definition: GlobalLeptonTriggerCalculator.cxx:33
top::GlobalLeptonTriggerCalculator::execute
StatusCode execute()
Definition: GlobalLeptonTriggerCalculator.cxx:81