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  : base_class(t, n, p)
27  , m_cut("")
28  , m_sgName("")
29  , m_storeTResult(false)
30  , m_sgMultipleNames({})
31  , m_storeMultipleOutputs(false)
32 {
33  declareProperty("CutType", m_cut);
34  declareProperty("StoreGateEntryName", m_sgName);
35  declareProperty("StoreTResult", m_storeTResult);
36  declareProperty("StoreGateEntryMultipleNames", m_sgMultipleNames);
37  declareProperty("StoreMultipleOutputs", m_storeMultipleOutputs);
38 }
39 
42 {
43  if (m_sgName.empty()) {
45  "No SG name provided for the output of EGElectronLikelihoodToolWrapper!");
46  return StatusCode::FAILURE;
47  }
48  ATH_CHECK(m_tool.retrieve());
49 
50  if (!(m_fudgeMCTool.name().empty())) {
51  ATH_CHECK(m_fudgeMCTool.retrieve());
52  } else {
53  m_fudgeMCTool.disable();
54  }
55 
56  ATH_CHECK(m_ContainerName.initialize());
57  //
59  m_decoratorIsEM = m_ContainerName.key() + "." + m_sgName + "IsEMValue";
60  ATH_CHECK(m_decoratorPass.initialize());
61  ATH_CHECK(m_decoratorIsEM.initialize());
62  //
63  if (m_storeTResult) {
64  m_decoratorResult = m_ContainerName.key() + "." + m_sgName + "Result";
66  }
68  for (auto& sgName : m_sgMultipleNames) {
69  m_decoratorMultipleOutputs.emplace_back(m_ContainerName.key() + "." + sgName);
70  }
72  }
73  return StatusCode::SUCCESS;
74 }
75 
77 EGElectronLikelihoodToolWrapper::addBranches(const EventContext& ctx) const
78 {
79  // retrieve container
81 
82  // If we're applying corrections, the correction tools will give us
83  // copies that we need to keep track of. (We want to do all the copies
84  // before we start writing decorations, to avoid warnings about having
85  // unlocked decorations in a copy).
86  // The copies we get back from the tool will have standalone aux stores.
87  // We'll put them in a DataVector to get them deleted, but we don't
88  // need to copy the aux data to the container, so construct it with
89  // @c NEVER_TRACK_INDICES.
91  if (!m_fudgeMCTool.empty()) {
92  pCopies.reserve (particles->size());
93  for (const xAOD::Egamma* par : *particles) {
95  // apply the shower shape corrections
97  xAOD::Egamma* pCopy = nullptr;
98  if (type == xAOD::Type::Electron) {
99  const xAOD::Electron* eg = static_cast<const xAOD::Electron*>(par);
100  xAOD::Electron* el = nullptr;
101  correctionCode = m_fudgeMCTool->correctedCopy(*eg, el);
102  pCopy = el;
103  } else {
104  const xAOD::Photon* eg = static_cast<const xAOD::Photon*>(par);
105  xAOD::Photon* ph = nullptr;
106  correctionCode = m_fudgeMCTool->correctedCopy(*eg, ph);
107  pCopy = ph;
108  }
109  if (correctionCode == CP::CorrectionCode::Ok) { // All OK
110  } else if (correctionCode == CP::CorrectionCode::Error) {
111  Error("addBranches()",
112  "Error applying fudge factors to current photon");
113  } else if (correctionCode == CP::CorrectionCode::OutOfValidityRange) {
114  Warning(
115  "addBranches()",
116  "Current object has no valid fudge factors due to out-of-range");
117  } else {
118  Warning(
119  "addBranches()",
120  "Unknown correction code %d from ElectronPhotonShowerShapeFudgeTool",
121  (int)correctionCode);
122  }
123  pCopies.push_back (pCopy);
124  }
125  }
126  else {
127  pCopies.resize (particles->size());
128  }
129 
130  // Decorators
132  m_decoratorPass, ctx
133  };
135  m_decoratorIsEM, ctx
136  };
137 
138  std::unique_ptr<SG::WriteDecorHandle<xAOD::EgammaContainer, float>>
139  decoratorResult = nullptr;
140  if (m_storeTResult) {
141  decoratorResult =
142  std::make_unique<SG::WriteDecorHandle<xAOD::EgammaContainer, float>>(
143  m_decoratorResult, ctx);
144  }
145 
146  auto decoratorMultipleOutputs = m_decoratorMultipleOutputs.makeHandles(ctx);
147 
148  // Write mask for each element and record to SG for subsequent selection
149  for (size_t ipar = 0; const xAOD::Egamma* par : *particles) {
150  const xAOD::Egamma* pCopy = pCopies[ipar++];
151  if (!pCopy) pCopy = par;
152  // compute the output of the selector
153  asg::AcceptData theAccept(m_tool->accept(ctx, pCopy));
154  const unsigned int isEM =
155  (unsigned int)theAccept.getCutResultInvertedBitSet()
156  .to_ulong(); // this should work for both the
157  // cut-based and the LH selectors
158 
159  // decorate the original object
160  if (m_cut.empty()) {
161  const bool pass_selection = (bool)theAccept;
162  if (pass_selection) {
163  decoratorPass(*par) = 1;
164  } else {
165  decoratorPass(*par) = 0;
166  }
167  decoratorIsEM(*par) = isEM;
168  if (decoratorResult) {
169  (*decoratorResult)(*par) =
170  static_cast<float>(m_tool->calculate(ctx, pCopy));
171  }
173  // calculateMultipleOutputs only supports xAOD::Electron as input
174  const xAOD::Electron *eCopy = static_cast<const xAOD::Electron *>(pCopy);
175  std::vector<float> toolOutput = m_tool->calculateMultipleOutputs(ctx, eCopy);
176  for (size_t i = 0; i < toolOutput.size(); i++){
177  decoratorMultipleOutputs.at(i)(*par) = toolOutput.at(i);
178  }
179  }
180  } else {
181  if (theAccept.getCutResult(m_cut)) {
182  decoratorPass(*par) = 1;
183  } else {
184  decoratorPass(*par) = 0;
185  }
186  decoratorIsEM(*par) = isEM;
187  if (decoratorResult) {
188  static const SG::AuxElement::Decorator<float> decResult(m_sgName +
189  "Result");
190  (*decoratorResult)(*par) =
191  static_cast<float>(m_tool->calculate(ctx, pCopy));
192  }
194  // calculateMultipleOutputs only supports xAOD::Electron as input
195  const xAOD::Electron* eCopy = static_cast<const xAOD::Electron*>(pCopy);
196  std::vector<float> toolOutput = m_tool->calculateMultipleOutputs(ctx, eCopy);
197  for (size_t i = 0; i < toolOutput.size(); i++){
198  decoratorMultipleOutputs.at(i)(*par) = toolOutput.at(i);
199  }
200  }
201  }
202  }
203 
204  return StatusCode::SUCCESS;
205 }
206 }
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:54
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:71
ParticleTest.eg
eg
Definition: ParticleTest.py:29
ObjectType
ObjectType
Definition: BaseObject.h:11
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:67
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:65
EGElectronLikelihoodToolWrapper.h
xAOD::Egamma_v1
Definition: Egamma_v1.h:56
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
python.CaloAddPedShiftConfig.type
type
Definition: CaloAddPedShiftConfig.py:42
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 EventContext &ctx) const override final
Definition: EGElectronLikelihoodToolWrapper.cxx:77
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:209
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:727
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:63
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:72
DerivationFramework::EGElectronLikelihoodToolWrapper::initialize
virtual StatusCode initialize() override final
Definition: EGElectronLikelihoodToolWrapper.cxx:41
DerivationFramework::EGElectronLikelihoodToolWrapper::m_storeMultipleOutputs
bool m_storeMultipleOutputs
Definition: EGElectronLikelihoodToolWrapper.h:74
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
python.getProblemFolderFromLogs.el
dictionary el
Definition: getProblemFolderFromLogs.py:48
DerivationFramework::EGElectronLikelihoodToolWrapper::m_cut
std::string m_cut
Definition: EGElectronLikelihoodToolWrapper.h:70
DerivationFramework
THE reconstruction tool.
Definition: ParticleSortingAlg.h:24
DerivationFramework::EGElectronLikelihoodToolWrapper::m_ContainerName
SG::ReadHandleKey< xAOD::EgammaContainer > m_ContainerName
Definition: EGElectronLikelihoodToolWrapper.h:56
DataVector
Derived DataVector<T>.
Definition: DataVector.h:795
AcceptData.h
DataVector::resize
void resize(size_type sz)
Resizes the collection to the specified number of elements.
createCoolChannelIdFile.par
par
Definition: createCoolChannelIdFile.py:28
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:67
xAOD::Electron_v1
Definition: Electron_v1.h:34
DerivationFramework::EGElectronLikelihoodToolWrapper::m_tool
ToolHandle< IAsgElectronLikelihoodTool > m_tool
Definition: EGElectronLikelihoodToolWrapper.h:47
AcceptInfo.h
DerivationFramework::EGElectronLikelihoodToolWrapper::m_decoratorMultipleOutputs
SG::WriteDecorHandleKeyArray< xAOD::EgammaContainer, float > m_decoratorMultipleOutputs
Definition: EGElectronLikelihoodToolWrapper.h:69
CP::CorrectionCode::Ok
@ Ok
The correction was done successfully.
Definition: CorrectionCode.h:38
python.CaloAddPedShiftConfig.int
int
Definition: CaloAddPedShiftConfig.py:45
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
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:73
Electron.h
asg::AcceptData
Definition: AcceptData.h:30
xAOD::bool
setBGCode setTAP setLVL2ErrorBits bool
Definition: TrigDecision_v1.cxx:60