ATLAS Offline Software
EGElectronLikelihoodToolWrapper.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 // EGElectronLikelihoodToolWrapper.cxx, (c) ATLAS Detector software
8 // Author: Giovanni Marchiori (giovanni.marchiori@cern.ch)
9 
11 //
12 #include "PATCore/AcceptData.h"
13 #include "PATCore/AcceptInfo.h"
14 //
17 #include "xAODEgamma/Electron.h"
18 #include "xAODEgamma/Photon.h"
19 
20 namespace DerivationFramework {
21 
23  const std::string& t,
24  const std::string& n,
25  const IInterface* p)
26  : AthAlgTool(t, n, p)
27  , m_cut("")
28  , m_sgName("")
29  , m_storeTResult(false)
30  , m_sgMultipleNames({})
31  , m_storeMultipleOutputs(false)
32 {
33  declareInterface<DerivationFramework::IAugmentationTool>(this);
34  declareProperty("CutType", m_cut);
35  declareProperty("StoreGateEntryName", m_sgName);
36  declareProperty("StoreTResult", m_storeTResult);
37  declareProperty("StoreGateEntryMultipleNames", m_sgMultipleNames);
38  declareProperty("StoreMultipleOutputs", m_storeMultipleOutputs);
39 }
40 
43 {
44  if (m_sgName.empty()) {
46  "No SG name provided for the output of EGElectronLikelihoodToolWrapper!");
47  return StatusCode::FAILURE;
48  }
49  ATH_CHECK(m_tool.retrieve());
50 
51  if (!(m_fudgeMCTool.name().empty())) {
52  ATH_CHECK(m_fudgeMCTool.retrieve());
53  } else {
54  m_fudgeMCTool.disable();
55  }
56 
57  ATH_CHECK(m_ContainerName.initialize());
58  //
60  m_decoratorIsEM = m_ContainerName.key() + "." + m_sgName + "IsEMValue";
61  ATH_CHECK(m_decoratorPass.initialize());
62  ATH_CHECK(m_decoratorIsEM.initialize());
63  //
64  if (m_storeTResult) {
65  m_decoratorResult = m_ContainerName.key() + "." + m_sgName + "Result";
67  }
69  for (auto& sgName : m_sgMultipleNames) {
70  m_decoratorMultipleOutputs.emplace_back(m_ContainerName.key() + "." + sgName);
71  }
73  }
74  return StatusCode::SUCCESS;
75 }
76 
79 {
80  // retrieve container
81  const EventContext& ctx = Gaudi::Hive::currentContext();
83 
84  // Decorators
86  m_decoratorPass, ctx
87  };
89  m_decoratorIsEM, ctx
90  };
91 
92  std::unique_ptr<SG::WriteDecorHandle<xAOD::EgammaContainer, float>>
93  decoratorResult = nullptr;
94  if (m_storeTResult) {
95  decoratorResult =
96  std::make_unique<SG::WriteDecorHandle<xAOD::EgammaContainer, float>>(
97  m_decoratorResult, ctx);
98  }
99 
100  auto decoratorMultipleOutputs = m_decoratorMultipleOutputs.makeHandles();
101 
102  bool applyFF = (!m_fudgeMCTool.empty());
103  // Write mask for each element and record to SG for subsequent selection
104  for (const xAOD::Egamma* par : *particles) {
105  // point to the input which we do not own ,
106  // in case we fundge this will become
107  // a corrected one which needs to be deleted
108  // corrected copy can be a bit awkward
109  const xAOD::Egamma* pCopy = par;
110  if (applyFF) {
111  xAOD::Type::ObjectType type = par->type();
112  // apply the shower shape corrections
114  if (type == xAOD::Type::Electron) {
115  const xAOD::Electron* eg = static_cast<const xAOD::Electron*>(par);
116  xAOD::Electron* el = nullptr;
117  correctionCode = m_fudgeMCTool->correctedCopy(*eg, el);
118  pCopy = el;
119  } else {
120  const xAOD::Photon* eg = static_cast<const xAOD::Photon*>(par);
121  xAOD::Photon* ph = nullptr;
122  correctionCode = m_fudgeMCTool->correctedCopy(*eg, ph);
123  pCopy = ph;
124  }
125  if (correctionCode == CP::CorrectionCode::Ok) { // All OK
126  } else if (correctionCode == CP::CorrectionCode::Error) {
127  Error("addBranches()",
128  "Error applying fudge factors to current photon");
129  } else if (correctionCode == CP::CorrectionCode::OutOfValidityRange) {
130  Warning(
131  "addBranches()",
132  "Current object has no valid fudge factors due to out-of-range");
133  } else {
134  Warning(
135  "addBranches()",
136  "Unknown correction code %d from ElectronPhotonShowerShapeFudgeTool",
137  (int)correctionCode);
138  }
139  }
140  // compute the output of the selector
141  asg::AcceptData theAccept(m_tool->accept(ctx, pCopy));
142  const unsigned int isEM =
143  (unsigned int)theAccept.getCutResultInvertedBitSet()
144  .to_ulong(); // this should work for both the
145  // cut-based and the LH selectors
146 
147  // decorate the original object
148  if (m_cut.empty()) {
149  const bool pass_selection = (bool)theAccept;
150  if (pass_selection) {
151  decoratorPass(*par) = 1;
152  } else {
153  decoratorPass(*par) = 0;
154  }
155  decoratorIsEM(*par) = isEM;
156  if (decoratorResult) {
157  (*decoratorResult)(*par) =
158  static_cast<float>(m_tool->calculate(ctx, pCopy));
159  }
161  // calculateMultipleOutputs only supports xAOD::Electron as input
162  const xAOD::Electron *eCopy = static_cast<const xAOD::Electron *>(pCopy);
163  std::vector<float> toolOutput = m_tool->calculateMultipleOutputs(ctx, eCopy);
164  for (size_t i = 0; i < toolOutput.size(); i++){
165  decoratorMultipleOutputs.at(i)(*par) = toolOutput.at(i);
166  }
167  }
168  } else {
169  if (theAccept.getCutResult(m_cut)) {
170  decoratorPass(*par) = 1;
171  } else {
172  decoratorPass(*par) = 0;
173  }
174  decoratorIsEM(*par) = isEM;
175  if (decoratorResult) {
176  static const SG::AuxElement::Decorator<float> decResult(m_sgName +
177  "Result");
178  (*decoratorResult)(*par) =
179  static_cast<float>(m_tool->calculate(ctx, pCopy));
180  }
182  // calculateMultipleOutputs only supports xAOD::Electron as input
183  const xAOD::Electron* eCopy = static_cast<const xAOD::Electron*>(pCopy);
184  std::vector<float> toolOutput = m_tool->calculateMultipleOutputs(ctx, eCopy);
185  for (size_t i = 0; i < toolOutput.size(); i++){
186  decoratorMultipleOutputs.at(i)(*par) = toolOutput.at(i);
187  }
188  }
189  }
190  // delete the particle copy
191  if (applyFF) {
192  delete pCopy;
193  }
194  }
195 
196  return StatusCode::SUCCESS;
197 }
198 }
asg::AcceptData::getCutResultInvertedBitSet
std::bitset< NBITS > getCutResultInvertedBitSet() const
Get an inverted bitset of the cut result.
Definition: AcceptData.h:119
DerivationFramework::EGElectronLikelihoodToolWrapper::m_fudgeMCTool
ToolHandle< IElectronPhotonShowerShapeFudgeTool > m_fudgeMCTool
Definition: EGElectronLikelihoodToolWrapper.h:56
xAOD::Electron
Electron_v1 Electron
Definition of the current "egamma version".
Definition: Event/xAOD/xAODEgamma/xAODEgamma/Electron.h:17
DerivationFramework::EGElectronLikelihoodToolWrapper::m_sgName
std::string m_sgName
Definition: EGElectronLikelihoodToolWrapper.h:73
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
ParticleTest.eg
eg
Definition: ParticleTest.py:29
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
ObjectType
ObjectType
Definition: BaseObject.h:11
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
DerivationFramework::EGElectronLikelihoodToolWrapper::m_decoratorIsEM
SG::WriteDecorHandleKey< xAOD::EgammaContainer > m_decoratorIsEM
Definition: EGElectronLikelihoodToolWrapper.h:67
EGElectronLikelihoodToolWrapper.h
xAOD::Egamma_v1
Definition: Egamma_v1.h:56
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
DerivationFramework::EGElectronLikelihoodToolWrapper::EGElectronLikelihoodToolWrapper
EGElectronLikelihoodToolWrapper(const std::string &t, const std::string &n, const IInterface *p)
Definition: EGElectronLikelihoodToolWrapper.cxx:22
DerivationFramework::EGElectronLikelihoodToolWrapper::addBranches
virtual StatusCode addBranches() const override final
Pass the thinning service
Definition: EGElectronLikelihoodToolWrapper.cxx:78
EgammaContainer.h
CP::CorrectionCode::OutOfValidityRange
@ OutOfValidityRange
Input object is out of validity range.
Definition: CorrectionCode.h:37
CP::CorrectionCode::Error
@ Error
Some error happened during the object correction.
Definition: CorrectionCode.h:36
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
SG::Decorator
Helper class to provide type-safe access to aux data.
Definition: Decorator.h:58
lumiFormat.i
int i
Definition: lumiFormat.py:92
beamspotman.n
n
Definition: beamspotman.py:731
Photon.h
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
DerivationFramework::EGElectronLikelihoodToolWrapper::m_decoratorPass
SG::WriteDecorHandleKey< xAOD::EgammaContainer > m_decoratorPass
Definition: EGElectronLikelihoodToolWrapper.h:65
IParticleContainer.h
SG::WriteDecorHandle
Handle class for adding a decoration to an object.
Definition: StoreGate/StoreGate/WriteDecorHandle.h:99
DerivationFramework::EGElectronLikelihoodToolWrapper::m_storeTResult
bool m_storeTResult
Definition: EGElectronLikelihoodToolWrapper.h:74
DerivationFramework::EGElectronLikelihoodToolWrapper::initialize
virtual StatusCode initialize() override final
Definition: EGElectronLikelihoodToolWrapper.cxx:42
plotIsoValidation.el
el
Definition: plotIsoValidation.py:197
DerivationFramework::EGElectronLikelihoodToolWrapper::m_storeMultipleOutputs
bool m_storeMultipleOutputs
Definition: EGElectronLikelihoodToolWrapper.h:76
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
DerivationFramework::EGElectronLikelihoodToolWrapper::m_cut
std::string m_cut
Definition: EGElectronLikelihoodToolWrapper.h:72
DerivationFramework
THE reconstruction tool.
Definition: ParticleSortingAlg.h:24
DerivationFramework::EGElectronLikelihoodToolWrapper::m_ContainerName
SG::ReadHandleKey< xAOD::EgammaContainer > m_ContainerName
Definition: EGElectronLikelihoodToolWrapper.h:58
AcceptData.h
createCoolChannelIdFile.par
par
Definition: createCoolChannelIdFile.py:29
DerivationFramework::EGElectronLikelihoodToolWrapper::m_decoratorResult
SG::WriteDecorHandleKey< xAOD::EgammaContainer > m_decoratorResult
Definition: EGElectronLikelihoodToolWrapper.h:69
xAOD::Electron_v1
Definition: Electron_v1.h:34
DerivationFramework::EGElectronLikelihoodToolWrapper::m_tool
ToolHandle< IAsgElectronLikelihoodTool > m_tool
Definition: EGElectronLikelihoodToolWrapper.h:49
AcceptInfo.h
DerivationFramework::EGElectronLikelihoodToolWrapper::m_decoratorMultipleOutputs
SG::WriteDecorHandleKeyArray< xAOD::EgammaContainer, float > m_decoratorMultipleOutputs
Definition: EGElectronLikelihoodToolWrapper.h:71
CP::CorrectionCode::Ok
@ Ok
The correction was done successfully.
Definition: CorrectionCode.h:38
xAOD::Photon_v1
Definition: Photon_v1.h:37
asg::AcceptData::getCutResult
bool getCutResult(const std::string &cutName) const
Get the result of a cut, based on the cut name (safer)
Definition: AcceptData.h:98
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
CP::CorrectionCode
Return value from object correction CP tools.
Definition: CorrectionCode.h:31
LArG4FSStartPointFilter.particles
list particles
Definition: LArG4FSStartPointFilter.py:84
DerivationFramework::EGElectronLikelihoodToolWrapper::m_sgMultipleNames
std::vector< std::string > m_sgMultipleNames
Definition: EGElectronLikelihoodToolWrapper.h:75
Electron.h
AthAlgTool
Definition: AthAlgTool.h:26
asg::AcceptData
Definition: AcceptData.h:30
xAOD::bool
setBGCode setTAP setLVL2ErrorBits bool
Definition: TrigDecision_v1.cxx:60