ATLAS Offline Software
EGElectronLikelihoodToolWrapper.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 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  // If we're applying corrections, the correction tools will give us
85  // copies that we need to keep track of. (We want to do all the copies
86  // before we start writing decorations, to avoid warnings about having
87  // unlocked decorations in a copy).
88  // The copies we get back from the tool will have standalone aux stores.
89  // We'll put them in a DataVector to get them deleted, but we don't
90  // need to copy the aux data to the container, so construct it with
91  // @c NEVER_TRACK_INDICES.
93  if (!m_fudgeMCTool.empty()) {
94  pCopies.reserve (particles->size());
95  for (const xAOD::Egamma* par : *particles) {
97  // apply the shower shape corrections
99  xAOD::Egamma* pCopy = nullptr;
100  if (type == xAOD::Type::Electron) {
101  const xAOD::Electron* eg = static_cast<const xAOD::Electron*>(par);
102  xAOD::Electron* el = nullptr;
103  correctionCode = m_fudgeMCTool->correctedCopy(*eg, el);
104  pCopy = el;
105  } else {
106  const xAOD::Photon* eg = static_cast<const xAOD::Photon*>(par);
107  xAOD::Photon* ph = nullptr;
108  correctionCode = m_fudgeMCTool->correctedCopy(*eg, ph);
109  pCopy = ph;
110  }
111  if (correctionCode == CP::CorrectionCode::Ok) { // All OK
112  } else if (correctionCode == CP::CorrectionCode::Error) {
113  Error("addBranches()",
114  "Error applying fudge factors to current photon");
115  } else if (correctionCode == CP::CorrectionCode::OutOfValidityRange) {
116  Warning(
117  "addBranches()",
118  "Current object has no valid fudge factors due to out-of-range");
119  } else {
120  Warning(
121  "addBranches()",
122  "Unknown correction code %d from ElectronPhotonShowerShapeFudgeTool",
123  (int)correctionCode);
124  }
125  pCopies.push_back (pCopy);
126  }
127  }
128  else {
129  pCopies.resize (particles->size());
130  }
131 
132  // Decorators
134  m_decoratorPass, ctx
135  };
137  m_decoratorIsEM, ctx
138  };
139 
140  std::unique_ptr<SG::WriteDecorHandle<xAOD::EgammaContainer, float>>
141  decoratorResult = nullptr;
142  if (m_storeTResult) {
143  decoratorResult =
144  std::make_unique<SG::WriteDecorHandle<xAOD::EgammaContainer, float>>(
145  m_decoratorResult, ctx);
146  }
147 
148  auto decoratorMultipleOutputs = m_decoratorMultipleOutputs.makeHandles();
149 
150  // Write mask for each element and record to SG for subsequent selection
151  for (size_t ipar = 0; const xAOD::Egamma* par : *particles) {
152  const xAOD::Egamma* pCopy = pCopies[ipar++];
153  if (!pCopy) pCopy = par;
154  // compute the output of the selector
155  asg::AcceptData theAccept(m_tool->accept(ctx, pCopy));
156  const unsigned int isEM =
157  (unsigned int)theAccept.getCutResultInvertedBitSet()
158  .to_ulong(); // this should work for both the
159  // cut-based and the LH selectors
160 
161  // decorate the original object
162  if (m_cut.empty()) {
163  const bool pass_selection = (bool)theAccept;
164  if (pass_selection) {
165  decoratorPass(*par) = 1;
166  } else {
167  decoratorPass(*par) = 0;
168  }
169  decoratorIsEM(*par) = isEM;
170  if (decoratorResult) {
171  (*decoratorResult)(*par) =
172  static_cast<float>(m_tool->calculate(ctx, pCopy));
173  }
175  // calculateMultipleOutputs only supports xAOD::Electron as input
176  const xAOD::Electron *eCopy = static_cast<const xAOD::Electron *>(pCopy);
177  std::vector<float> toolOutput = m_tool->calculateMultipleOutputs(ctx, eCopy);
178  for (size_t i = 0; i < toolOutput.size(); i++){
179  decoratorMultipleOutputs.at(i)(*par) = toolOutput.at(i);
180  }
181  }
182  } else {
183  if (theAccept.getCutResult(m_cut)) {
184  decoratorPass(*par) = 1;
185  } else {
186  decoratorPass(*par) = 0;
187  }
188  decoratorIsEM(*par) = isEM;
189  if (decoratorResult) {
190  static const SG::AuxElement::Decorator<float> decResult(m_sgName +
191  "Result");
192  (*decoratorResult)(*par) =
193  static_cast<float>(m_tool->calculate(ctx, pCopy));
194  }
196  // calculateMultipleOutputs only supports xAOD::Electron as input
197  const xAOD::Electron* eCopy = static_cast<const xAOD::Electron*>(pCopy);
198  std::vector<float> toolOutput = m_tool->calculateMultipleOutputs(ctx, eCopy);
199  for (size_t i = 0; i < toolOutput.size(); i++){
200  decoratorMultipleOutputs.at(i)(*par) = toolOutput.at(i);
201  }
202  }
203  }
204  }
205 
206  return StatusCode::SUCCESS;
207 }
208 }
DataVector::reserve
void reserve(size_type n)
Attempt to preallocate enough memory for a specified number of elements.
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
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
SG::NEVER_TRACK_INDICES
@ NEVER_TRACK_INDICES
Never track indices, regardless of the setting of the ownership policy.
Definition: IndexTrackingPolicy.h:46
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
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
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:59
lumiFormat.i
int i
Definition: lumiFormat.py:85
SG::OWN_ELEMENTS
@ OWN_ELEMENTS
this data object owns its elements
Definition: OwnershipPolicy.h:17
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:100
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
DataVector
Derived DataVector<T>.
Definition: DataVector.h:794
AcceptData.h
DataVector::resize
void resize(size_type sz)
Resizes the collection to the specified number of elements.
createCoolChannelIdFile.par
par
Definition: createCoolChannelIdFile.py:29
DataVector::push_back
value_type push_back(value_type pElem)
Add an element to the end of the collection.
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