ATLAS Offline Software
Loading...
Searching...
No Matches
TruthTrackCreation.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3*/
4
6// TruthTrackCreation.cxx, (c) ATLAS Detector software
8
10// Trk includes
16// HepMC
19
20//================ Constructor =================================================
21
22Trk::TruthTrackCreation::TruthTrackCreation(const std::string& name, ISvcLocator* pSvcLocator)
23 :
24 AthAlgorithm(name,pSvcLocator),
25 m_prdTruthTrajectoryBuilder("Trk::PRD_TruthTrajectoryBuilder/InDetPRD_TruthTrajectoryBuilder"),
26 m_truthTrackBuilder("Trk::TruthTrackBuilder/InDetTruthTrackBuilder"),
28{
29 // Trk Truth Tools
30 declareProperty("PRD_TruthTrajectoryBuilder", m_prdTruthTrajectoryBuilder, "Truth Trajectory Builder Tool");
31 declareProperty("TruthTrackBuilder", m_truthTrackBuilder, "Truth Track Builder Tool");
32 declareProperty("PRD_TruthTrajectorySelectors", m_prdTruthTrajectorySelectors, "PRD truth trajectory selectors");
33 // Trk Tools
34 declareProperty("TrackSelectors", m_trackSelectors, "Track selectors for a posteriori track selection");
35 declareProperty("TrackSummaryTool", m_trackSummaryTool, "Summary tool for completing the track with its summary info");
36}
37
38//================ Destructor =================================================
39
41 = default;
42
43
44//================ Initialisation =================================================
45
47{
48 // trajectory creator
49 if (m_prdTruthTrajectoryBuilder.retrieve().isFailure()){
50 ATH_MSG_ERROR("Could not retrieve " << m_prdTruthTrajectoryBuilder << ". Aborting ...");
51 return StatusCode::FAILURE;
52 }
53 // truth track builder
54 if (m_truthTrackBuilder.retrieve().isFailure()){
55 ATH_MSG_ERROR("Could not retrieve " << m_truthTrackBuilder << ". Aborting ...");
56 return StatusCode::FAILURE;
57 }
58 // PRD truth trajectory selectors
59 if (!m_prdTruthTrajectorySelectors.empty() && m_prdTruthTrajectorySelectors.retrieve().isFailure()){
60 ATH_MSG_ERROR("Could not retrieve " << m_prdTruthTrajectorySelectors << ". Aborting ...");
61 return StatusCode::FAILURE;
62 }
63 ATH_CHECK( m_assoTool.retrieve( DisableTool {m_assoTool.empty()} ));
64
65 // track summary tool if configured
66 if (!m_trackSummaryTool.empty() && m_trackSummaryTool.retrieve().isFailure()){
67 ATH_MSG_ERROR("Could not retrieve " << m_trackSummaryTool << ". Aborting ...");
68 return StatusCode::FAILURE;
69 }
70 // track selector if configured
71 if (!m_trackSelectors.empty() && m_trackSelectors.retrieve().isFailure()){
72 ATH_MSG_ERROR("Could not retrieve " << m_trackSelectors << ". Aborting ...");
73 return StatusCode::FAILURE;
74 }
75
78
79 return StatusCode::SUCCESS;
80}
81
82//================ Finalisation =================================================
83
85{
86 // Code entered here will be executed once at the end of the program run.
87 return StatusCode::SUCCESS;
88}
89
90//================ Execution ====================================================
91
93{
94 std::unique_ptr<Trk::PRDtoTrackMap> prd_to_track_map(!m_assoTool.empty()
95 ? m_assoTool->createPRDtoTrackMap()
96 : std::unique_ptr<Trk::PRDtoTrackMap>());
97 // create the track collection
98 std::unique_ptr<TrackCollection> outputTrackCollection = std::make_unique<TrackCollection>();
99 std::unique_ptr<TrackCollection> skippedTrackCollection = std::make_unique<TrackCollection>();
102
103 std::vector<std::unique_ptr<Trk::Track> > tmp_track_collection;
104
105 // set up the PRD trajectory builder
106 if ( m_prdTruthTrajectoryBuilder->refreshEvent().isFailure() ){
107 ATH_MSG_INFO("Could not refresh the PRD truth trajectory builder. No truth track creation.");
108 return StatusCode::SUCCESS;
109 }
110
111 // ----------------------------------- main loop ------------------------------------------------------------------
112 // get the PRD truth trajectories
113 const std::map< HepMC::ConstGenParticlePtr, PRD_TruthTrajectory >& truthTraj =
114 m_prdTruthTrajectoryBuilder->truthTrajectories();
115 // some screen output
116 ATH_MSG_VERBOSE("PRD_TruthTrajectoryBuilder delivered " << truthTraj.size() << " PRD truth trajectories, starting track creation.");
117 // loop over truth trajectories and create track
118 auto ttIter = truthTraj.begin();
119 auto ttIterE = truthTraj.end();
120 for ( ; ttIter != ttIterE; ++ttIter){
121 // run through the selector chain
122 if (!m_prdTruthTrajectorySelectors.empty()){
123 ToolHandleArray<Trk::IPRD_TruthTrajectorySelector>::const_iterator prdTTSelIter = m_prdTruthTrajectorySelectors.begin();
124 ToolHandleArray<Trk::IPRD_TruthTrajectorySelector>::const_iterator prdTTSelIterE = m_prdTruthTrajectorySelectors.end();
125 bool passed = true;
126 for ( ; prdTTSelIter != prdTTSelIterE && passed; ++prdTTSelIter ){
127 if (!(*prdTTSelIter)->pass((*ttIter).second)){
128 ATH_MSG_VERBOSE("PRD truth trajectory did not pass the selection cuts. Skipping ... ");
129 passed = false;
130 }
131 }
132 // the consequence of the selection loop
133 if (!passed) continue;
134 }
135 // create the truth track
136 std::unique_ptr<Trk::Track> truthTrack( m_truthTrackBuilder->createTrack(ttIter->second));
137 if (!truthTrack){
138 ATH_MSG_VERBOSE("Track creation for PRD truth trajectory with size " << (*ttIter).second.prds.size() << " failed. Skipping ...");
139 continue;
140 }
141 ATH_MSG_VERBOSE("Track creation for PRD truth trajectory with size " << (*ttIter).second.prds.size() << " successful.");
142 // If configured : update the track summary
143 if (m_trackSummaryTool.isEnabled()){
144 ATH_MSG_VERBOSE("Updating the TrackSummary.");
145 m_trackSummaryTool->computeAndReplaceTrackSummary(*truthTrack,
146 false /* DO NOT suppress hole search*/);
147
148 }
149 // If configured : check with the TrackSelectors
150 bool passed = m_trackSelectors.empty();
151 if ( !m_trackSelectors.empty() ) {
152 ToolHandleArray<Trk::ITrackSelectorTool>::iterator tsIter = m_trackSelectors.begin();
153 ToolHandleArray<Trk::ITrackSelectorTool>::iterator tsIterE = m_trackSelectors.end();
154 for ( ; ( tsIter != tsIterE && !passed ); ++tsIter){
155 passed = (*tsIter)->decision(*truthTrack);
156 }
157 }
158 // now check the result
159 if (passed){
160 // everything good
161 ATH_MSG_VERBOSE("Track created and pushed into the output track collection.");
162 if (prd_to_track_map && m_assoTool->addPRDs(*prd_to_track_map, *truthTrack).isFailure()) {
163 ATH_MSG_WARNING("Failed to add PRDs to map");
164 }
165 tmp_track_collection.push_back(std::move(truthTrack));
166 } else {
167 ATH_MSG_VERBOSE("Track did not pass the track selection. Putting it into skipped track collection.");
168 skippedTrackCollection->push_back(truthTrack.release());
169 }
170 }
171
172 // If configured : update the track summary
173 if (m_trackSummaryTool.isEnabled()) {
174 outputTrackCollection->reserve(tmp_track_collection.size());
175 for (std::unique_ptr<Trk::Track> &track : tmp_track_collection) {
176 ATH_MSG_VERBOSE("Updating the TrackSummary with shared hits.");
177 m_trackSummaryTool->computeAndReplaceTrackSummary(*track, false /* DO NOT suppress hole search*/);
178 outputTrackCollection->push_back(std::move(track));
179 }
180 }
181 else {
182 outputTrackCollection->reserve(tmp_track_collection.size());
183 for (std::unique_ptr<Trk::Track> &track : tmp_track_collection) {
184 outputTrackCollection->push_back(std::move(track));
185 }
186 }
187
188 // ----------------------------------- record & cleanup ---------------------------------------------------------------
189
190 ATH_CHECK(outputTrackCollectionHandle.record(std::move(outputTrackCollection)));
191
192 ATH_MSG_VERBOSE("Truth TrackCollection with name " << m_outputTrackCollectionName << " and size " << outputTrackCollectionHandle->size() << " recorded.");
193
194 ATH_CHECK(skippedTrackCollectionHandle.record(std::move(skippedTrackCollection)));
195
196 ATH_MSG_VERBOSE("Truth TrackCollection with name " << m_skippedTrackCollectionName << " and size " << skippedTrackCollectionHandle->size() << " recorded.");
197
198 // job done
199 return StatusCode::SUCCESS;
200}
201
202//============================================================================================
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
bool passed(DecisionID id, const DecisionIDContainer &)
checks if required decision ID is in the set of IDs in the container
AthAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Constructor with parameters:
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
ToolHandle< Trk::ITruthTrackBuilder > m_truthTrackBuilder
truth tools
SG::WriteHandleKey< TrackCollection > m_skippedTrackCollectionName
TruthTrackCreation(const std::string &name, ISvcLocator *pSvcLocator)
Standard Athena-Algorithm Constructor.
~TruthTrackCreation()
Default Destructor.
StatusCode initialize()
standard Athena-Algorithm method
SG::WriteHandleKey< TrackCollection > m_outputTrackCollectionName
StatusCode finalize()
standard Athena-Algorithm method
ToolHandle< Trk::IPRDtoTrackMapTool > m_assoTool
StatusCode execute()
standard Athena-Algorithm method
ToolHandle< Trk::IPRD_TruthTrajectoryBuilder > m_prdTruthTrajectoryBuilder
truth tools
ToolHandleArray< Trk::ITrackSelectorTool > m_trackSelectors
track selectors for a posteriory track selection
ToolHandle< Trk::IExtendedTrackSummaryTool > m_trackSummaryTool
summary tool for completing the track
ToolHandleArray< Trk::IPRD_TruthTrajectorySelector > m_prdTruthTrajectorySelectors
PRD truth trajectory selectors.