ATLAS Offline Software
HitsTruthRelinkBase.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 "HitsTruthRelinkBase.h"
7 
8 HitsTruthRelinkBase::HitsTruthRelinkBase(const std::string &name, ISvcLocator *pSvcLocator)
9  : AthReentrantAlgorithm(name, pSvcLocator)
10 {
11 }
12 
13 
15 {
16  // Check and initialize keys
18  ATH_MSG_VERBOSE("Initialized ReadHandleKey: " << m_inputTruthCollectionKey);
19  return StatusCode::SUCCESS;
20 }
21 
22 
23 StatusCode HitsTruthRelinkBase::getReferenceBarcode(const EventContext &ctx, int *barcode) const
24 {
26  if (!inputCollection.isValid()) {
27  ATH_MSG_ERROR("Could not get input truth collection " << inputCollection.name() << " from store " << inputCollection.store());
28  return StatusCode::FAILURE;
29  }
30  ATH_MSG_DEBUG("Found input truth collection " << inputCollection.name() << " in store " << inputCollection.store());
31 
32  const HepMC::GenEvent *genEvt = *(inputCollection->begin());
33 
34  size_t nVertices = genEvt->vertices_size();
35  if (nVertices == 0) {
36  ATH_MSG_ERROR("Truth collection should have at least one vertex!");
37  return StatusCode::FAILURE;
38  }
39 #ifdef HEPMC3
40  const HepMC::ConstGenVertexPtr& genVtx = genEvt->vertices().back();
41  size_t nParticles = genVtx->particles_out().size();
42  if (nParticles == 0) {
43  ATH_MSG_ERROR("Truth vertex should have at least one particle!");
44  return StatusCode::FAILURE;
45  }
46  *barcode = HepMC::barcode(genVtx->particles_out().front());
47 #else
48  auto genVtx = *(genEvt->vertices_end());
49  size_t nParticles = genVtx->particles_out_size();
50  if (nParticles == 0) {
51  ATH_MSG_ERROR("Truth vertex should have at least one particle!");
52  return StatusCode::FAILURE;
53  }
54  *barcode = HepMC::barcode(*(genVtx->particles_out_const_begin()));
55 #endif
56 
57  ATH_MSG_DEBUG("Reference barcode: " << *barcode);
58 
59  return StatusCode::SUCCESS;
60 
61 }
62 
63 
64 StatusCode HitsTruthRelinkBase::getReferenceId(const EventContext &ctx, int *id) const
65 {
67  if (!inputCollection.isValid()) {
68  ATH_MSG_ERROR("Could not get input truth collection " << inputCollection.name() << " from store " << inputCollection.store());
69  return StatusCode::FAILURE;
70  }
71  ATH_MSG_DEBUG("Found input truth collection " << inputCollection.name() << " in store " << inputCollection.store());
72  int barcode{0};
73  const HepMC::GenEvent *genEvt = *(inputCollection->begin());
74 
75  size_t nVertices = genEvt->vertices_size();
76  if (nVertices == 0) {
77  ATH_MSG_ERROR("Truth collection should have at least one vertex!");
78  return StatusCode::FAILURE;
79  }
80 #ifdef HEPMC3
81  const HepMC::ConstGenVertexPtr& genVtx = genEvt->vertices().back();
82  size_t nParticles = genVtx->particles_out().size();
83  if (nParticles == 0) {
84  ATH_MSG_ERROR("Truth vertex should have at least one particle!");
85  return StatusCode::FAILURE;
86  }
87  *id = HepMC::uniqueID(genVtx->particles_out().front());
88  barcode = HepMC::barcode(genVtx->particles_out().front());
89 #else
90  auto genVtx = *(genEvt->vertices_end());
91  size_t nParticles = genVtx->particles_out_size();
92  if (nParticles == 0) {
93  ATH_MSG_ERROR("Truth vertex should have at least one particle!");
94  return StatusCode::FAILURE;
95  }
96  *id = HepMC::uniqueID(*(genVtx->particles_out_const_begin()));
97  barcode = HepMC::barcode(*(genVtx->particles_out_const_begin()));
98 #endif
99 
100  ATH_MSG_DEBUG("Reference id: " << *id);
101  ATH_MSG_DEBUG("Reference barcode: " << barcode);
102 
103  return StatusCode::SUCCESS;
104 
105 }
106 
107 HepMcParticleLink HitsTruthRelinkBase::updatedLink(const EventContext &ctx, const HepMcParticleLink& oldLink, int referenceId, int /*pdgId*/) const {
108  ATH_MSG_DEBUG ("oldLink.id() = " << oldLink.id());
109  int currentId{};
110  // Hits previously linked to truth particles should now be linked to the reference truthParticle
111  if ( HepMC::uniqueID(oldLink) != HepMC::UNDEFINED_ID || HepMC::barcode(oldLink) != HepMC::UNDEFINED_ID ) { // FIXME barcode-based for now to work around reading in HepMcParticleLink_p2 based EDM
112  currentId = referenceId;
113  }
114  // TODO test using the ConstGenParticlePtr directly in the HepMcParticleLink constructor
116 }
NSWL1::nVertices
int nVertices(const Polygon &p)
Definition: GeoUtils.cxx:35
SG::ReadHandle< McEventCollection >
HitsTruthRelinkBase::getReferenceId
StatusCode getReferenceId(const EventContext &ctx, int *id) const
Definition: HitsTruthRelinkBase.cxx:64
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
HitsTruthRelinkBase::getReferenceBarcode
StatusCode getReferenceBarcode(const EventContext &ctx, int *barcode) const
Definition: HitsTruthRelinkBase.cxx:23
HitsTruthRelinkBase::initialize
virtual StatusCode initialize() override
Definition: HitsTruthRelinkBase.cxx:14
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:83
HitsTruthRelinkBase::m_inputTruthCollectionKey
SG::ReadHandleKey< McEventCollection > m_inputTruthCollectionKey
Definition: HitsTruthRelinkBase.h:26
PhysDESDM_SmpCaloId.inputCollection
inputCollection
Definition: PhysDESDM_SmpCaloId.py:95
HitsTruthRelinkBase.h
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
HepMC::uniqueID
int uniqueID(const T &p)
Definition: MagicNumbers.h:113
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
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
HitsTruthRelinkBase::updatedLink
virtual HepMcParticleLink updatedLink(const EventContext &ctx, const HepMcParticleLink &oldLink, int referenceId, int pdgID=0) const
Definition: HitsTruthRelinkBase.cxx:107
HepMC::UNDEFINED_ID
constexpr int UNDEFINED_ID
Definition: MagicNumbers.h:55
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
MagicNumbers.h
HepMC::ConstGenVertexPtr
const HepMC::GenVertex * ConstGenVertexPtr
Definition: GenVertex.h:60
HitsTruthRelinkBase::HitsTruthRelinkBase
HitsTruthRelinkBase(const std::string &name, ISvcLocator *pSvcLocator)
Definition: HitsTruthRelinkBase.cxx:8