ATLAS Offline Software
TRT_HitsTruthRelink.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 "TRT_HitsTruthRelink.h"
7 
8 
9 TRT_HitsTruthRelink::TRT_HitsTruthRelink(const std::string &name, ISvcLocator *pSvcLocator)
10  : HitsTruthRelinkBase(name, pSvcLocator)
11 {
12 }
13 
14 
16 {
18 
19  // Check and initialize keys
20  ATH_CHECK( m_inputHitsKey.initialize() );
21  ATH_MSG_VERBOSE("Initialized ReadHandleKey: " << m_inputHitsKey);
22  ATH_CHECK( m_outputHitsKey.initialize() );
23  ATH_MSG_VERBOSE("Initialized WriteHandleKey: " << m_outputHitsKey);
24  return StatusCode::SUCCESS;
25 }
26 
27 
28 StatusCode TRT_HitsTruthRelink::execute(const EventContext &ctx) const
29 {
30  ATH_MSG_DEBUG("Doing truth relinking");
31 
33  if (!inputCollection.isValid()) {
34  ATH_MSG_ERROR("Could not get input hits collection " << inputCollection.name() << " from store " << inputCollection.store());
35  return StatusCode::FAILURE;
36  }
37  ATH_MSG_DEBUG("Found input hits collection " << inputCollection.name() << " in store " << inputCollection.store());
38 
40  ATH_CHECK(outputCollection.record(std::make_unique<TRTUncompressedHitCollection>()));
41  if (!outputCollection.isValid()) {
42  ATH_MSG_ERROR("Could not record output hits collection " << outputCollection.name() << " to store " << outputCollection.store());
43  return StatusCode::FAILURE;
44  }
45  ATH_MSG_DEBUG("Recorded output hits collection " << outputCollection.name() << " in store " << outputCollection.store());
46 
47  // Do relinking
48  int referenceId{};
49  ATH_CHECK(getReferenceId(ctx, &referenceId));
50 
51  for (const TRTUncompressedHit &hit : *inputCollection) {
52  int pdgID = hit.GetParticleEncoding();
53  HepMcParticleLink particleLink = updatedLink(ctx, hit.particleLink(), referenceId, pdgID);
54  int id = hit.GetHitID();
55  float kineticEnergy = hit.GetKineticEnergy();
56  float energyDeposit = hit.GetEnergyDeposit();
57  float preX = hit.GetPreStepX();
58  float preY = hit.GetPreStepY();
59  float preZ = hit.GetPreStepZ();
60  float postX = hit.GetPostStepX();
61  float postY = hit.GetPostStepY() ;
62  float postZ = hit.GetPostStepZ();
63  float time = hit.GetGlobalTime();
64 
65  outputCollection->Emplace(id, particleLink, pdgID, kineticEnergy, energyDeposit, preX, preY, preZ, postX, postY, postZ, time);
66  }
67 
68  return StatusCode::SUCCESS;
69 }
70 
71 
72 HepMcParticleLink TRT_HitsTruthRelink::updatedLink(const EventContext &ctx, const HepMcParticleLink& oldLink, int referenceId, int pdgId) const {
73  ATH_MSG_DEBUG ("oldLink.id() = " << oldLink.id());
74  int currentId = oldLink.id();
75  // Hits previously linked to truth particles should now be linked to the reference truthParticle
76  if (oldLink.id() != HepMC::UNDEFINED_ID || HepMC::barcode(oldLink) != HepMC::UNDEFINED_ID ) { // FIXME barcode-based for now to work around reading in HepMcParticleLink_p2 based EDM
77  // For the TRT truth electrons may optionally be kept
78  if (!(m_keepElectronsLinkedToTRTHits && std::abs(pdgId) == 11)) {
79  currentId = referenceId;
80  }
81  }
82 
84  if (currentId == referenceId && !(m_keepElectronsLinkedToTRTHits && std::abs(pdgId) == 11)) {
86  }
87  return newLink;
88 }
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
SG::VarHandleBase::name
const std::string & name() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleBase.cxx:75
HitsTruthRelinkBase::getReferenceId
StatusCode getReferenceId(const EventContext &ctx, int *id) const
Definition: HitsTruthRelinkBase.cxx:64
TRTUncompressedHit
Definition: TRTUncompressedHit.h:11
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
Trk::energyDeposit
@ energyDeposit
Definition: MeasurementType.h:32
HitsTruthRelinkBase::initialize
virtual StatusCode initialize() override
Definition: HitsTruthRelinkBase.cxx:14
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
HepMC::barcode
int barcode(const T *p)
Definition: Barcode.h:16
SG::VarHandleBase::store
std::string store() const
Return the name of the store holding the object we are proxying.
Definition: StoreGate/src/VarHandleBase.cxx:376
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
HepMC::UNDEFINED_ID
constexpr int UNDEFINED_ID
Definition: MagicNumbers.h:55
postInclude.outputCollection
outputCollection
Definition: postInclude.SortInput.py:27
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
MagicNumbers.h
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:76
CaloSwCorrections.time
def time(flags, cells_name, *args, **kw)
Definition: CaloSwCorrections.py:242
EBC_PU_SUPPRESSED
@ EBC_PU_SUPPRESSED
Definition: MagicNumbers.h:29
HitsTruthRelinkBase
Definition: HitsTruthRelinkBase.h:15