ATLAS Offline Software
PRD_TruthTrajectoryBuilder.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 // PRD_TruthTrajectoryBuilder.cxx, (c) ATLAS Detector software
8 
9 // package include
11 // Trk
13 // DetectorDescription
15 // HepMC
16 #include "AtlasHepMC/GenParticle.h"
17 #include "AtlasHepMC/GenVertex.h"
18 
19 
21 Trk::PRD_TruthTrajectoryBuilder::PRD_TruthTrajectoryBuilder(const std::string& t, const std::string& n, const IInterface* p) :
22  AthAlgTool(t,n,p),
23  m_idHelper(nullptr)
24 {
25  declareInterface<Trk::IPRD_TruthTrajectoryBuilder>(this);
26  // the PRD providers that turn Identifier -> IdentiferHash and get the PRD
27 }
28 
29 // Athena algtool's Hooks - initialize
31 {
32 
33  // Set up ATLAS ID helper to be able to identify the PRD's det-subsystem
34  if (detStore()->retrieve(m_idHelper, "AtlasID").isFailure()) {
35  ATH_MSG_ERROR ("Could not get AtlasDetectorID helper. Arborting ...");
36  return StatusCode::FAILURE;
37  }
38  // get the ID PRD Provider
39  if ( !m_idPrdProvider.empty() && m_idPrdProvider.retrieve().isFailure()){
40  ATH_MSG_ERROR ("Could not get " << m_idPrdProvider << ". Arborting ..." );
41  return StatusCode::FAILURE;
42  }
43  // get the Muon System PRD Provider
44  if ( !m_msPrdProvider.empty() && m_msPrdProvider.retrieve().isFailure()){
45  ATH_MSG_ERROR ("Could not get " << m_msPrdProvider << ". Arborting ..." );
46  return StatusCode::FAILURE;
47  }
48  // get the manipulators
49  if ( !m_prdTruthTrajectoryManipulators.empty() && m_prdTruthTrajectoryManipulators.retrieve().isFailure()){
50  ATH_MSG_ERROR ("Could not get configured " << m_prdTruthTrajectoryManipulators << ". Arborting ..." );
51  return StatusCode::FAILURE;
52  }
53 
54  ATH_CHECK( m_prdMultiTruthCollectionNames.initialize() );
55 
56  return StatusCode::SUCCESS;
57 }
58 
60 
61  ATH_MSG_INFO("Calling refreshEvent() to reset cache and retrieve collections");
62  // clear the cache & reserve
63  m_prdMultiTruthCollections.clear();
64  m_prdMultiTruthCollections.reserve(m_prdMultiTruthCollectionNames.size());
65  // load the PRD collections from SG
66  for(const auto& pmtCollNameIter:m_prdMultiTruthCollectionNames){
67  // try to retrieve the PRD multi truth collection
68  SG::ReadHandle<PRD_MultiTruthCollection> curColl (pmtCollNameIter);
69  if (!curColl.isValid()){
70  ATH_MSG_WARNING("Could not retrieve " << pmtCollNameIter << ". Ignoring ... ");
71  }
72  else{
73  ATH_MSG_INFO("Added " << pmtCollNameIter << " to collection list for truth track creation.");
74  m_prdMultiTruthCollections.push_back(curColl.cptr());
75  }
76  }
77  // all good
78  return StatusCode::SUCCESS;
79 
80 }
81 
82 std::map<HepMC::ConstGenParticlePtr, Trk::PRD_TruthTrajectory > Trk::PRD_TruthTrajectoryBuilder::truthTrajectories() const {
83  // ndof
84  size_t ndofTotal = 0;
85  size_t ndof = 0;
86 
87  std::map< HepMC::ConstGenParticlePtr, PRD_TruthTrajectory > gpPrdTruthTrajectories;
88 
89  // PART 1 --------------------------------------------------------------------------------------------------------
90  // loop over the PRD_MultiTruthCollection, search for the PRD and create (if necessary and entry in the return map)
91  std::vector<const PRD_MultiTruthCollection*>::const_iterator pmtCollIter = m_prdMultiTruthCollections.begin();
92  std::vector<const PRD_MultiTruthCollection*>::const_iterator pmtCollIterE = m_prdMultiTruthCollections.end();
93  for ( ; pmtCollIter != pmtCollIterE; ++pmtCollIter ){
94  // loop over the map and get the identifier, GenParticle relation
95  PRD_MultiTruthCollection::const_iterator prdMtCIter = (*pmtCollIter)->begin();
96  PRD_MultiTruthCollection::const_iterator prdMtCIterE = (*pmtCollIter)->end();
97  for ( ; prdMtCIter != prdMtCIterE; ++ prdMtCIter ){
98 
99  // check if entry exists and if
100 #ifdef HEPMC3
101  HepMC::ConstGenParticlePtr curGenP = (*prdMtCIter).second.scptr();
102 #else
103 //AV Looks like an implicit conversion
104  HepMC::ConstGenParticlePtr curGenP = (*prdMtCIter).second;
105 #endif
106  Identifier curIdentifier = (*prdMtCIter).first;
107  // apply the min pT cut
108  if ( curGenP->momentum().perp() < m_minPt ) continue;
109  // skip geantinos if required
110  if (!m_geantinos && std::abs(curGenP->pdg_id())==999) continue;
111  // get the associated PRD from the provider
112  const Trk::PrepRawData* prd = m_idHelper->is_indet(curIdentifier) ?
113  m_idPrdProvider->prdFromIdentifier(curIdentifier,ndof) : m_msPrdProvider->prdFromIdentifier(curIdentifier,ndof);
114  // stuff it into the trajectory if you found a PRD
115  if (prd){
116  // try to find the entry for this GenParticle
117  auto prdTrajIter = gpPrdTruthTrajectories.find(curGenP);
118  if ( prdTrajIter == gpPrdTruthTrajectories.end() ){
119  // first PRD associated to this: create PRD_TruthTrajectory object
120  Trk::PRD_TruthTrajectory newPrdTruthTrajectory;
121  newPrdTruthTrajectory.prds.push_back(prd);
122  newPrdTruthTrajectory.nDoF = ndof-5;
123  // register the GenParticle only once
124  newPrdTruthTrajectory.genParticle = curGenP;
125  // fill into map
126  gpPrdTruthTrajectories[curGenP] = newPrdTruthTrajectory;
127  ndofTotal = ndof;
128  } else {
129  // this PRD_TruthTrajectory already exists
130  (prdTrajIter->second).prds.push_back(prd);
131  (prdTrajIter->second).nDoF += ndof;
132  ndofTotal = (prdTrajIter->second).nDoF;
133  }
134  ATH_MSG_DEBUG(" Associating PRD with " << ndof << " degrees of freedom, total N.d.o.F : " << ndofTotal );
135  ATH_MSG_DEBUG(" Associating Identifier " << curIdentifier << " with particle at [ " << curGenP << " ]." );
136  std::string prdtype = m_idHelper->is_pixel(curIdentifier) ? "Pixel" : m_idHelper->is_sct(curIdentifier) ? "SCT" : "TRT";
137  ATH_MSG_DEBUG(" PRD is a " << prdtype);
138  } else {
139  std::string prdtype = m_idHelper->is_pixel(curIdentifier) ? "Pixel" : m_idHelper->is_sct(curIdentifier) ? "SCT" : "TRT";
140  ATH_MSG_DEBUG(" Failed to get " << prdtype << " PRD");
141  }
142  }
143  }
144  // PART 2 --------------------------------------------------------------------------------------------------------
145  // loop through the provided list of manipulators ( sorter is included )
146  auto prdTruthTrajIter = gpPrdTruthTrajectories.begin();
147  auto prdTruthTrajIterE = gpPrdTruthTrajectories.end();
148  for ( ; prdTruthTrajIter != prdTruthTrajIterE; ++prdTruthTrajIter ){
149  if ( !m_prdTruthTrajectoryManipulators.empty() ){
150  ToolHandleArray<IPRD_TruthTrajectoryManipulator>::const_iterator prdTTMIter = m_prdTruthTrajectoryManipulators.begin();
151  ToolHandleArray<IPRD_TruthTrajectoryManipulator>::const_iterator prdTTMIterE = m_prdTruthTrajectoryManipulators.end();
152  for ( ; prdTTMIter != prdTTMIterE; ++prdTTMIter ){
153  if ((*prdTTMIter)->manipulateTruthTrajectory((*prdTruthTrajIter).second))
154  ATH_MSG_DEBUG("PRD truth trajectory got manipulated by: " << (*prdTTMIter).name() );
155  }
156  }
157  }
158  // return the truth trajectories and leave it to the TruthTrack creation to proceed further
159  return gpPrdTruthTrajectories;
160 }
161 
163 {
164  // clear the cache a last time
165  m_prdMultiTruthCollections.clear();
166  return StatusCode::SUCCESS;
167 }
168 
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
IDTPM::ndof
float ndof(const U &p)
Definition: TrackParametersHelper.h:134
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
SG::ReadHandle::cptr
const_pointer_type cptr()
Dereference the pointer.
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
GenVertex.h
Trk::PRD_TruthTrajectoryBuilder::truthTrajectories
virtual std::map< HepMC::ConstGenParticlePtr, PRD_TruthTrajectory > truthTrajectories() const override
return a vector of PrepRawData trajectories - uses internal cache
Definition: PRD_TruthTrajectoryBuilder.cxx:82
Trk::PRD_TruthTrajectoryBuilder::PRD_TruthTrajectoryBuilder
PRD_TruthTrajectoryBuilder(const std::string &t, const std::string &n, const IInterface *p)
Constructor.
Definition: PRD_TruthTrajectoryBuilder.cxx:21
Trk::PRD_TruthTrajectoryBuilder::refreshEvent
virtual StatusCode refreshEvent() override
Event refresh - can't be an IIncident, because it has to run after PRD creation and PRD truth creatio...
Definition: PRD_TruthTrajectoryBuilder.cxx:59
Trk::PRD_TruthTrajectory::genParticle
HepMC::ConstGenParticlePtr genParticle
Definition: PRD_TruthTrajectory.h:31
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
GenParticle.h
AtlasDetectorID.h
This class provides an interface to generate or decode an identifier for the upper levels of the dete...
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
beamspotman.n
n
Definition: beamspotman.py:731
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
PrepRawDataComparisonFunction.h
Trk::PRD_TruthTrajectoryBuilder::finalize
virtual StatusCode finalize() override
Definition: PRD_TruthTrajectoryBuilder.cxx:162
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
Trk::PrepRawData
Definition: PrepRawData.h:62
Trk::PRD_TruthTrajectory
Definition: PRD_TruthTrajectory.h:27
python.PyKernel.detStore
detStore
Definition: PyKernel.py:41
HepMC::ConstGenParticlePtr
const GenParticle * ConstGenParticlePtr
Definition: GenParticle.h:38
Trk::PRD_TruthTrajectory::nDoF
size_t nDoF
Definition: PRD_TruthTrajectory.h:32
PRD_TruthTrajectoryBuilder.h
Trk::PRD_TruthTrajectoryBuilder::initialize
virtual StatusCode initialize() override
Definition: PRD_TruthTrajectoryBuilder.cxx:30
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
AthAlgTool
Definition: AthAlgTool.h:26
Trk::PRD_TruthTrajectory::prds
std::vector< const Trk::PrepRawData * > prds
public members
Definition: PRD_TruthTrajectory.h:30
Identifier
Definition: IdentifierFieldParser.cxx:14