ATLAS Offline Software
xAODEgammaBuilder.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3  */
4 
5 #include "xAODEgammaBuilder.h"
6 
8 #include "GaudiKernel/EventContext.h"
9 #include "StoreGate/ReadHandle.h"
10 #include "StoreGate/WriteHandle.h"
11 
13 
16 
18 #include "xAODEgamma/Electron.h"
21 #include "xAODEgamma/Photon.h"
26 
27 #include <algorithm>
28 #include <cmath>
29 #include <memory>
30 #include <vector>
31 
33  ISvcLocator* pSvcLocator)
34  : AthReentrantAlgorithm(name, pSvcLocator)
35 {}
36 
39 {
40  m_deltaEta1Pear = std::make_unique<electronPearShapeAlignmentCorrection>();
41  // the data handle keys
47 
48  // retrieve tools
49  ATH_CHECK(m_clusterTool.retrieve());
50  ATH_CHECK(m_ShowerTool.retrieve());
51 
52  if (m_doElectrons) {
53  ATH_CHECK(m_electronTools.retrieve());
54  }
55  if (m_doPhotons) {
56  ATH_CHECK(m_photonTools.retrieve());
57  }
58  m_doOQ = !m_egammaOQTool.empty();
59  if (m_doOQ) {
60  ATH_CHECK(m_egammaOQTool.retrieve());
61  } else {
62  m_egammaOQTool.disable();
63  }
64 
65  // do we actually do ambiguity
66  m_doAmbiguity = !m_ambiguityTool.empty();
68  ATH_CHECK(m_ambiguityTool.retrieve());
69  } else {
70  m_ambiguityTool.disable();
71  }
74  return StatusCode::SUCCESS;
75 }
76 
79 {
80  return StatusCode::SUCCESS;
81 }
82 
84 xAODEgammaBuilder::execute(const EventContext& ctx) const
85 {
86 
87  const EgammaRecContainer* inputElRecs = nullptr;
88  const EgammaRecContainer* inputPhRecs = nullptr;
90  xAOD::PhotonContainer* photons = nullptr;
91  /*
92  * From here on if a Read/Write handle
93  * is retrieved the above will be !=
94  * nullptr for electron or photons or both
95  */
96  std::optional<SG::WriteHandle<xAOD::ElectronContainer>> electronContainer;
97  std::optional<SG::WriteHandle<xAOD::PhotonContainer>> photonContainer;
98 
99  if (m_doElectrons) {
100  SG::ReadHandle<EgammaRecContainer> electronSuperRecs(
102  inputElRecs = electronSuperRecs.ptr();
103 
106  std::make_unique<xAOD::ElectronContainer>(),
107  std::make_unique<xAOD::ElectronAuxContainer>()));
108 
109  electrons = electronContainer->ptr();
110  electrons->reserve(inputElRecs->size());
111  }
112  if (m_doPhotons) {
113  SG::ReadHandle<EgammaRecContainer> photonSuperRecs(
115  inputPhRecs = photonSuperRecs.ptr();
116 
118  ATH_CHECK(
119  photonContainer->record(std::make_unique<xAOD::PhotonContainer>(),
120  std::make_unique<xAOD::PhotonAuxContainer>()));
121 
122  photons = photonContainer->ptr();
123  photons->reserve(inputPhRecs->size());
124  }
125 
126  /*
127  * Now fill the electrons and photons
128  */
129  if (m_doElectrons) {
130  for (const egammaRec* electronRec : *inputElRecs) {
131  // in case for some reasons we reach here with no trackparticles
132  if (electronRec->getNumberOfTrackParticles() == 0) {
133  continue;
134  }
137  if (m_doPhotons) {
138  // get the hottest cell
139  const xAOD::CaloCluster* const elClus = electronRec->caloCluster();
140  const double elEta0 = elClus->eta0();
141  const double elPhi0 = elClus->phi0();
142  for (const egammaRec* photonRec : *inputPhRecs) {
143  const xAOD::CaloCluster* const phClus = photonRec->caloCluster();
144  // See if they have the same hottest cell
145  if (elEta0 == phClus->eta0() && elPhi0 == phClus->phi0()) {
146  if (m_doAmbiguity) { // should be the default
147  author =
148  m_ambiguityTool->ambiguityResolve(elClus,
149  photonRec->vertex(),
150  electronRec->trackParticle(),
151  type);
152  } else { // in case the ambiguity tool is not set ambiguity is not
153  // resolved
155  }
156  break;
157  }
158  }
159  }
160  // Create Electron xAOD objects
163  if (!getElectron(electronRec, electrons, author, type)) {
164  return StatusCode::FAILURE;
165  }
166  }
167  }
168  }
169 
170  if (m_doPhotons) {
171  for (const egammaRec* photonRec : *inputPhRecs) {
174  if (m_doElectrons) {
175  // get the hottest cell
176  const xAOD::CaloCluster* const phClus = photonRec->caloCluster();
177  const double phEta0 = phClus->eta0();
178  const double phPhi0 = phClus->phi0();
179  for (const egammaRec* electronRec : *inputElRecs) {
180  const xAOD::CaloCluster* const elClus = electronRec->caloCluster();
181  // See if they have the same hottest cell
182  if (phEta0 == elClus->eta0() && phPhi0 == elClus->phi0()) {
183  if (m_doAmbiguity) { // should be the default
184  author =
185  m_ambiguityTool->ambiguityResolve(elClus,
186  photonRec->vertex(),
187  electronRec->trackParticle(),
188  type);
189  } else { // in case the ambiguity tool is not set ambiguity is not
190  // resolved
192  }
193  break;
194  }
195  }
196  }
197  // Create Photon xAOD objects
200  if (!getPhoton(photonRec, photons, author, type)) {
201  return StatusCode::FAILURE;
202  }
203  }
204  }
205  }
206 
207  SG::ReadCondHandle<CaloDetDescrManager> caloDetDescrMgrHandle{
209  };
210  ATH_CHECK(caloDetDescrMgrHandle.isValid());
211  const CaloDetDescrManager* calodetdescrmgr = *caloDetDescrMgrHandle;
212 
213  // Shower Shapes
214  if (electrons) {
215  for (xAOD::Electron* electron : *electrons) {
216  ATH_CHECK(m_ShowerTool->execute(ctx, *calodetdescrmgr, electron));
217  }
218  }
219  if (photons) {
220  for (xAOD::Photon* photon : *photons) {
221  ATH_CHECK(m_ShowerTool->execute(ctx, *calodetdescrmgr, photon));
222  }
223  }
224 
225  // Object Quality
226  if (m_doOQ) {
227  if (electrons) {
228  for (xAOD::Electron* electron : *electrons) {
229  ATH_CHECK(m_egammaOQTool->execute(ctx, *electron));
230  }
231  }
232  if (photons) {
233  for (xAOD::Photon* photon : *photons) {
234  ATH_CHECK(m_egammaOQTool->execute(ctx, *photon));
235  }
236  }
237  }
238 
239  // Energy calibration
240  ATH_CHECK(m_clusterTool->contExecute(ctx, electrons, photons));
241 
242  //Followed by 4-Mom Building
245  //Additional tools fpr electrons/photons
246  //e.g identification
247  if (m_doElectrons) {
248  for (const auto& tool : m_electronTools) {
250  }
251  }
252  if (m_doPhotons) {
253  for (const auto& tool : m_photonTools) {
254  ATH_CHECK(CallTool(ctx, tool, photons));
255  }
256  }
257  // Do the ambiguity Links
258  if (m_doElectrons && m_doPhotons) {
261  }
262 
263  if (m_doDummyElectrons) {
264  SG::WriteHandle<xAOD::ElectronContainer> dummyElectronContainer(
266  ATH_CHECK(dummyElectronContainer.record(
267  std::make_unique<xAOD::ElectronContainer>(),
268  std::make_unique<xAOD::ElectronAuxContainer>()));
269 
270  dummyElectronContainer->push_back(std::unique_ptr<xAOD::Electron>());
271  }
272 
273  return StatusCode::SUCCESS;
274 }
275 
276 template <typename T>
279  const EventContext& ctx,
280  const ToolHandle<IegammaBaseTool>& tool,
281  DataVector<T> *container) const
282 {
283  if (container) {
284  for (T *egamma : *container) {
285  ATH_CHECK(tool->execute(ctx, egamma));
286  }
287  }
288 
289  return StatusCode::SUCCESS;
290 }
291 
292 bool
295  const unsigned int author,
296  const uint8_t type) const
297 {
298  if (!egRec || !electronContainer) {
299  return false;
300  }
301 
302  xAOD::Electron* electron = electronContainer->push_back(std::make_unique<xAOD::Electron>());
303  electron->setAuthor(author);
304 
305  static const SG::AuxElement::Accessor<uint8_t> acc("ambiguityType");
306  acc(*electron) = type;
307 
308  electron->setCaloClusterLinks(egRec->caloClusterElementLinks());
309  electron->setTrackParticleLinks(egRec->trackParticleElementLinks());
310 
311  const xAOD::TrackParticle* trackParticle = electron->trackParticle();
312  if (trackParticle) {
313  electron->setCharge(trackParticle->charge());
314  }
315  // Set DeltaEta, DeltaPhi , DeltaPhiRescaled
316  electron->setTrackCaloMatchValues(
317  egRec->deltaEta(),
318  egRec->deltaPhi(),
319  egRec->deltaPhiRescaled(),
320  egRec->deltaPhiLast()
321  );
322 
323  static const SG::AuxElement::Accessor<float> pear("deltaEta1PearDistortion");
324  pear(*electron) = m_isTruth ? 0.0 : m_deltaEta1Pear->getDeltaEtaDistortion(
325  electron->caloCluster()->etaBE(2),
326  electron->caloCluster()->phiBE(2)
327  );
328 
329  return true;
330 }
331 
332 bool
335  const unsigned int author,
336  const uint8_t type) const
337 {
338  if (!egRec || !photonContainer) {
339  return false;
340  }
341 
342  xAOD::Photon* photon = photonContainer->push_back(std::make_unique<xAOD::Photon>());
343  photon->setAuthor(author);
344  static const SG::AuxElement::Accessor<uint8_t> acc("ambiguityType");
345  acc(*photon) = type;
346 
347  photon->setCaloClusterLinks(egRec->caloClusterElementLinks());
348  photon->setVertexLinks(egRec->vertexElementLinks());
349 
350  // Transfer deltaEta/Phi info
351  float deltaEta = egRec->deltaEtaVtx();
352  float deltaPhi = egRec->deltaPhiVtx();
353  if (!photon->setVertexCaloMatchValue(
355  return false;
356  }
357 
358  if (!photon->setVertexCaloMatchValue(
360  return false;
361  }
362  return true;
363 }
DataVector::reserve
void reserve(size_type n)
Attempt to preallocate enough memory for a specified number of elements.
xAOD::CaloCluster_v1::phi0
flt_t phi0() const
Returns raw of cluster seed.
egammaRec::deltaPhiRescaled
const std::array< double, 4 > & deltaPhiRescaled() const
deltaPhi for rescaled momentum extrapolation from the perigee.
electronContainer
xAOD::ElectronContainer * electronContainer
Definition: TrigGlobEffCorrValidation.cxx:187
xAODEgammaBuilder::m_photonClusterRecContainerKey
SG::ReadHandleKey< EgammaRecContainer > m_photonClusterRecContainerKey
Name of input super cluster photon egammaRec container.
Definition: xAODEgammaBuilder.h:163
SG::ReadCondHandle
Definition: ReadCondHandle.h:44
xAOD::uint8_t
uint8_t
Definition: Muon_v1.cxx:558
xAODEgammaBuilder::m_photonTools
ToolHandleArray< IegammaBaseTool > m_photonTools
Vector of tools for dressing ONLY photons.
Definition: xAODEgammaBuilder.h:121
SG::Accessor
Helper class to provide type-safe access to aux data.
Definition: Control/AthContainers/AthContainers/Accessor.h:68
xAOD::TrackParticle_v1::charge
float charge() const
Returns the charge.
Definition: TrackParticle_v1.cxx:151
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:67
xAODEgammaBuilder::initialize
StatusCode initialize() override final
Definition: xAODEgammaBuilder.cxx:38
xAODEgammaBuilder::m_doPhotons
Gaudi::Property< bool > m_doPhotons
Definition: xAODEgammaBuilder.h:184
xAOD::deltaPhi
setSAddress setEtaMS setDirPhiMS setDirZMS setBarrelRadius setEndcapAlpha setEndcapRadius setInterceptInner setEtaMap setEtaBin setIsTgcFailure setDeltaPt deltaPhi
Definition: L2StandAloneMuon_v1.cxx:161
xAODEgammaBuilder::CallTool
StatusCode CallTool(const EventContext &ctx, const ToolHandle< IegammaBaseTool > &tool, DataVector< T > *container) const
Call a tool using contExecute and an electron or photon container.
Definition: xAODEgammaBuilder.cxx:278
xAODEgammaBuilder::m_electronTools
ToolHandleArray< IegammaBaseTool > m_electronTools
Vector of tools for dressing ONLY electrons.
Definition: xAODEgammaBuilder.h:117
xAODEgammaBuilder::m_clusterTool
ToolHandle< IEMClusterTool > m_clusterTool
Tool to do the final electron/photon cluster building.
Definition: xAODEgammaBuilder.h:125
xAODEgammaBuilder::m_doOQ
bool m_doOQ
Definition: xAODEgammaBuilder.h:188
xAODEgammaBuilder::execute
StatusCode execute(const EventContext &ctx) const override final
Definition: xAODEgammaBuilder.cxx:84
xAODEgammaBuilder::m_doAmbiguity
bool m_doAmbiguity
Definition: xAODEgammaBuilder.h:187
xAODEgammaBuilder::getElectron
bool getElectron(const egammaRec *egRec, xAOD::ElectronContainer *electronContainer, const unsigned int author, const uint8_t type) const
Given an egammaRec object, a pointer to the electron container and the author, create and dress an el...
Definition: xAODEgammaBuilder.cxx:293
egammaRec::trackParticleElementLinks
const std::vector< ElementLink< xAOD::TrackParticleContainer > > & trackParticleElementLinks() const
Get a reference to the track particle links.
Definition: egammaRec.cxx:49
xAODEgammaBuilder::finalize
StatusCode finalize() override final
Definition: xAODEgammaBuilder.cxx:78
SG::VarHandleKey::empty
bool empty() const
Test if the key is blank.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:150
xAODEgammaBuilder::m_electronOutputKey
SG::WriteHandleKey< xAOD::ElectronContainer > m_electronOutputKey
Name of the electron output collection.
Definition: xAODEgammaBuilder.h:147
egammaRec::deltaEta
const std::array< double, 4 > & deltaEta() const
deltaEta at pre sampler(0) -> 3rd sampling(3)
python.CaloAddPedShiftConfig.type
type
Definition: CaloAddPedShiftConfig.py:42
CaloDetDescrManager.h
Definition of CaloDetDescrManager.
xAODEgammaBuilder::m_ambiguityTool
ToolHandle< IEGammaAmbiguityTool > m_ambiguityTool
Tool to resolve electron/photon ambiguity.
Definition: xAODEgammaBuilder.h:135
egamma
Definition: egamma.h:58
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:74
EgammaContainer.h
xAODEgammaBuilder::m_deltaEta1Pear
std::unique_ptr< electronPearShapeAlignmentCorrection > m_deltaEta1Pear
Definition: xAODEgammaBuilder.h:186
WriteHandle.h
Handle class for recording to StoreGate.
xAODEgammaBuilder::xAODEgammaBuilder
xAODEgammaBuilder(const std::string &name, ISvcLocator *pSvcLocator)
Definition: xAODEgammaBuilder.cxx:32
xAOD::CaloCluster_v1
Description of a calorimeter cluster.
Definition: CaloCluster_v1.h:62
egammaRec::deltaPhi
const std::array< double, 4 > & deltaPhi() const
deltaPhi at pre sampler(0) -> 3rd sampling(3)
xAODEgammaBuilder::m_electronClusterRecContainerKey
SG::ReadHandleKey< EgammaRecContainer > m_electronClusterRecContainerKey
Name of input super cluster electron egammaRec container.
Definition: xAODEgammaBuilder.h:157
xAOD::AmbiguityTool::electron
@ electron
Definition: IEGammaAmbiguityTool.h:34
egammaRec::deltaPhiLast
double deltaPhiLast() const
deltaPhi from Last measurement
ElectronContainer.h
P4Helpers::deltaEta
double deltaEta(const I4Momentum &p1, const I4Momentum &p2)
Computes efficiently .
Definition: P4Helpers.h:66
egammaRec::caloClusterElementLinks
const std::vector< ElementLink< xAOD::CaloClusterContainer > > & caloClusterElementLinks() const
Get a reference to the calo cluster links.
Definition: egammaRec.cxx:26
ElectronAuxContainer.h
xAOD::EgammaParameters::AuthorAmbiguous
const uint16_t AuthorAmbiguous
Object Reconstructed by standard cluster-based algorithm.
Definition: EgammaDefs.h:32
Photon.h
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
egammaRec::deltaPhiVtx
float deltaPhiVtx() const
deltaPhiVtx
EMFourMomBuilder.h
AthenaPoolTestRead.acc
acc
Definition: AthenaPoolTestRead.py:16
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
xAODEgammaBuilder::m_dummyElectronOutputKey
SG::WriteHandleKey< xAOD::ElectronContainer > m_dummyElectronOutputKey
Name of the dummy electron output collection.
Definition: xAODEgammaBuilder.h:176
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
xAOD::AmbiguityTool::AmbiguityType
AmbiguityType
Definition: IEGammaAmbiguityTool.h:33
xAODEgammaBuilder::m_isTruth
Gaudi::Property< bool > m_isTruth
Option to do truth.
Definition: xAODEgammaBuilder.h:183
xAOD::EgammaParameters::convMatchDeltaPhi1
@ convMatchDeltaPhi1
difference between the cluster phi and the phi of the first track of the vertex extrapolated to the s...
Definition: EgammaEnums.h:253
DataVector
Derived DataVector<T>.
Definition: DataVector.h:794
xAODEgammaBuilder::m_ShowerTool
ToolHandle< IEMShowerBuilder > m_ShowerTool
Tool to compute shower shapes.
Definition: xAODEgammaBuilder.h:130
photonContainer
xAOD::PhotonContainer * photonContainer
Definition: TrigGlobEffCorrValidation.cxx:189
EMFourMomBuilder::calculate
void calculate(xAOD::Electron &electron)
Definition: EMFourMomBuilder.cxx:69
xAOD::AmbiguityTool::photon
@ photon
Definition: IEGammaAmbiguityTool.h:41
xAODEgammaBuilder::m_doElectrons
Gaudi::Property< bool > m_doElectrons
Definition: xAODEgammaBuilder.h:185
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
errorcheck.h
Helpers for checking error return status codes and reporting errors.
AtlCoolConsole.tool
tool
Definition: AtlCoolConsole.py:452
egAmbLinkHelper.h
DataVector::push_back
value_type push_back(value_type pElem)
Add an element to the end of the collection.
SG::CondHandleKey::initialize
StatusCode initialize(bool used=true)
xAODEgammaBuilder.h
xAOD::Electron_v1
Definition: Electron_v1.h:34
egammaRec::vertexElementLinks
const std::vector< ElementLink< xAOD::VertexContainer > > & vertexElementLinks() const
Get a reference to the vertix links.
Definition: egammaRec.cxx:71
SG::ReadHandle::ptr
const_pointer_type ptr()
Dereference the pointer.
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:73
xAOD::CaloCluster_v1::eta0
flt_t eta0() const
Returns raw of cluster seed.
xAODEgammaBuilder::getPhoton
bool getPhoton(const egammaRec *egRec, xAOD::PhotonContainer *photonContainer, const unsigned int author, uint8_t type) const
Given an egammaRec object, a pointer to the photon container and the author, create and dress a photo...
Definition: xAODEgammaBuilder.cxx:333
xAODEgammaBuilder::m_egammaOQTool
ToolHandle< IegammaOQFlagsBuilder > m_egammaOQTool
Tool to add electron/photon Object Quality info.
Definition: xAODEgammaBuilder.h:140
VertexContainer.h
PhotonAuxContainer.h
xAOD::photon
@ photon
Definition: TrackingPrimitives.h:200
CaloDetDescrManager
This class provides the client interface for accessing the detector description information common to...
Definition: CaloDetDescrManager.h:473
SG::WriteHandle::record
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
xAODEgammaBuilder::m_doDummyElectrons
bool m_doDummyElectrons
Definition: xAODEgammaBuilder.h:189
xAOD::Photon_v1
Definition: Photon_v1.h:37
xAOD::EgammaParameters::convMatchDeltaEta1
@ convMatchDeltaEta1
difference between the cluster eta and the eta of the first track of the vertex extrapolated to the s...
Definition: EgammaEnums.h:243
CaloCondBlobAlgs_fillNoiseFromASCII.author
string author
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:25
xAOD::EgammaParameters::electron
@ electron
Definition: EgammaEnums.h:18
xAOD::EgammaParameters::AuthorPhoton
const uint16_t AuthorPhoton
Object Reconstructed by standard cluster-based algorithm.
Definition: EgammaDefs.h:28
egammaRec
Definition: egammaRec.h:31
DataVector::emplace
iterator emplace(iterator position, value_type pElem)
Add a new element to the collection.
Electron.h
xAOD::TrackParticle_v1
Class describing a TrackParticle.
Definition: TrackParticle_v1.h:43
xAODEgammaBuilder::m_photonOutputKey
SG::WriteHandleKey< xAOD::PhotonContainer > m_photonOutputKey
Name of the photon output collection.
Definition: xAODEgammaBuilder.h:152
ReadHandle.h
Handle class for reading from StoreGate.
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
PhotonContainer.h
TrackParticleContainer.h
egammaRec::deltaEtaVtx
float deltaEtaVtx() const
deltaEtaVtx
InDetDD::electrons
@ electrons
Definition: InDetDD_Defs.h:17
TSU::T
unsigned long long T
Definition: L1TopoDataTypes.h:35
xAOD::EgammaParameters::AuthorElectron
const uint16_t AuthorElectron
Object Reconstructed by standard cluster-based algorithm.
Definition: EgammaDefs.h:24
xAODEgammaBuilder::m_caloDetDescrMgrKey
SG::ReadCondHandleKey< CaloDetDescrManager > m_caloDetDescrMgrKey
Definition: xAODEgammaBuilder.h:168
egAmbLinkHelper::doAmbiguityLinks
void doAmbiguityLinks(const EventContext &ctx, DataVector< SrcT > *srcContainer, DataVector< DestT > *destContainer)
Definition: egAmbLinkHelper.h:15