ATLAS Offline Software
Loading...
Searching...
No Matches
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"
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
37StatusCode
39{
40 m_deltaEta1Pear = std::make_unique<electronPearShapeAlignmentCorrection>();
41 // the data handle keys
46 ATH_CHECK(m_caloDetDescrMgrKey.initialize());
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
68 ATH_CHECK(m_ambiguityTool.retrieve());
69 } else {
70 m_ambiguityTool.disable();
71 }
74 return StatusCode::SUCCESS;
75}
76
77StatusCode
79{
80 return StatusCode::SUCCESS;
81}
82
83StatusCode
84xAODEgammaBuilder::execute(const EventContext& ctx) const
85{
86
87 const EgammaRecContainer* inputElRecs = nullptr;
88 const EgammaRecContainer* inputPhRecs = nullptr;
89 xAOD::ElectronContainer* electrons = 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) {
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 }
135 unsigned int author = xAOD::EgammaParameters::AuthorElectron;
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) {
172 unsigned int author = xAOD::EgammaParameters::AuthorPhoton;
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) {
249 ATH_CHECK(CallTool(ctx, tool, electrons));
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) {
259 egAmbLinkHelper::doAmbiguityLinks(ctx, electrons, photons);
260 egAmbLinkHelper::doAmbiguityLinks(ctx, photons, electrons);
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
276template <typename T>
277StatusCode
279 const EventContext& ctx,
280 const ToolHandle<IegammaBaseTool>& tool,
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
292bool
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
332bool
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}
Scalar deltaPhi(const MatrixBase< Derived > &vec) const
#define ATH_CHECK
Evaluate an expression and check for errors.
Definition of CaloDetDescrManager.
Helpers for checking error return status codes and reporting errors.
Handle class for reading from StoreGate.
Handle class for recording to StoreGate.
xAOD::ElectronContainer * electronContainer
xAOD::PhotonContainer * photonContainer
An algorithm that can be simultaneously executed in multiple threads.
This class provides the client interface for accessing the detector description information common to...
Derived DataVector<T>.
Definition DataVector.h:795
void reserve(size_type n)
Attempt to preallocate enough memory for a specified number of elements.
size_type size() const noexcept
Returns the number of elements in the collection.
SG::Accessor< T, ALLOC > Accessor
Definition AuxElement.h:572
const_pointer_type ptr()
Dereference the pointer.
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
Represent an egamma object for internal egamma usage during reconstruction.
Definition egammaRec.h:31
double deltaPhiLast() const
deltaPhi from Last measurement
const std::vector< ElementLink< xAOD::CaloClusterContainer > > & caloClusterElementLinks() const
Get a reference to the calo cluster links.
Definition egammaRec.cxx:26
const std::vector< ElementLink< xAOD::TrackParticleContainer > > & trackParticleElementLinks() const
Get a reference to the track particle links.
Definition egammaRec.cxx:49
const std::array< double, 4 > & deltaPhiRescaled() const
deltaPhi for rescaled momentum extrapolation from the perigee.
float deltaPhiVtx() const
deltaPhiVtx
const std::vector< ElementLink< xAOD::VertexContainer > > & vertexElementLinks() const
Get a reference to the vertix links.
Definition egammaRec.cxx:71
float deltaEtaVtx() const
deltaEtaVtx
const std::array< double, 4 > & deltaEta() const
deltaEta at pre sampler(0) -> 3rd sampling(3)
const std::array< double, 4 > & deltaPhi() const
deltaPhi at pre sampler(0) -> 3rd sampling(3)
elec/gamma data class.
Definition egamma.h:58
xAODEgammaBuilder(const std::string &name, ISvcLocator *pSvcLocator)
SG::ReadCondHandleKey< CaloDetDescrManager > m_caloDetDescrMgrKey
ToolHandleArray< IegammaBaseTool > m_photonTools
Vector of tools for dressing ONLY photons.
SG::WriteHandleKey< xAOD::PhotonContainer > m_photonOutputKey
Name of the photon output collection.
Gaudi::Property< bool > m_doElectrons
StatusCode initialize() override final
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...
StatusCode finalize() override final
ToolHandle< IEMShowerBuilder > m_ShowerTool
Tool to compute shower shapes.
std::unique_ptr< electronPearShapeAlignmentCorrection > m_deltaEta1Pear
ToolHandle< IEGammaAmbiguityTool > m_ambiguityTool
Tool to resolve electron/photon ambiguity.
SG::WriteHandleKey< xAOD::ElectronContainer > m_electronOutputKey
Name of the electron output collection.
Gaudi::Property< bool > m_isTruth
Option to do truth.
SG::WriteHandleKey< xAOD::ElectronContainer > m_dummyElectronOutputKey
Name of the dummy electron output collection.
StatusCode CallTool(const EventContext &ctx, const ToolHandle< IegammaBaseTool > &tool, DataVector< T > *container) const
Call a tool using contExecute and an electron or photon container.
SG::ReadHandleKey< EgammaRecContainer > m_electronClusterRecContainerKey
Name of input super cluster electron egammaRec container.
ToolHandle< IEMClusterTool > m_clusterTool
Tool to do the final electron/photon cluster building.
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...
SG::ReadHandleKey< EgammaRecContainer > m_photonClusterRecContainerKey
Name of input super cluster photon egammaRec container.
ToolHandleArray< IegammaBaseTool > m_electronTools
Vector of tools for dressing ONLY electrons.
Gaudi::Property< bool > m_doPhotons
StatusCode execute(const EventContext &ctx) const override final
ToolHandle< IegammaOQFlagsBuilder > m_egammaOQTool
Tool to add electron/photon Object Quality info.
flt_t eta0() const
Returns raw of cluster seed.
flt_t phi0() const
Returns raw of cluster seed.
float charge() const
Returns the charge.
DataVector< egammaRec > EgammaRecContainer
The container is a simple typedef for now.
void calculate(xAOD::Electron &electron)
void doAmbiguityLinks(const EventContext &ctx, DataVector< SrcT > *srcContainer, DataVector< DestT > *destContainer)
const uint16_t AuthorPhoton
Object Reconstructed by standard cluster-based algorithm.
Definition EgammaDefs.h:28
@ convMatchDeltaPhi1
difference between the cluster phi and the phi of the first track of the vertex extrapolated to the s...
@ convMatchDeltaEta1
difference between the cluster eta and the eta of the first track of the vertex extrapolated to the s...
const uint16_t AuthorElectron
Object Reconstructed by standard cluster-based algorithm.
Definition EgammaDefs.h:24
const uint16_t AuthorAmbiguous
Object Reconstructed by standard cluster-based algorithm.
Definition EgammaDefs.h:32
PhotonContainer_v1 PhotonContainer
Definition of the current "photon container version".
ElectronContainer_v1 ElectronContainer
Definition of the current "electron container version".
CaloCluster_v1 CaloCluster
Define the latest version of the calorimeter cluster class.
TrackParticle_v1 TrackParticle
Reference the current persistent version:
Photon_v1 Photon
Definition of the current "egamma version".
Electron_v1 Electron
Definition of the current "egamma version".