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