ATLAS Offline Software
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 
9 #include "TruthTrackCreation.h"
10 // Trk includes
16 // HepMC
17 #include "AtlasHepMC/GenParticle.h"
19 
20 //================ Constructor =================================================
21 
22 Trk::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"),
27  m_trackSummaryTool("")
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 
76  ATH_CHECK( m_outputTrackCollectionName.initialize() );
77  ATH_CHECK( m_skippedTrackCollectionName.initialize() );
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>();
100  SG::WriteHandle<TrackCollection> outputTrackCollectionHandle(m_outputTrackCollectionName);
101  SG::WriteHandle<TrackCollection> skippedTrackCollectionHandle(m_skippedTrackCollectionName);
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 //============================================================================================
DataVector::reserve
void reserve(size_type n)
Attempt to preallocate enough memory for a specified number of elements.
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
Trk::TruthTrackCreation::TruthTrackCreation
TruthTrackCreation(const std::string &name, ISvcLocator *pSvcLocator)
Standard Athena-Algorithm Constructor.
Definition: TruthTrackCreation.cxx:22
ITruthTrackBuilder.h
TrigCompositeUtils::passed
bool passed(DecisionID id, const DecisionIDContainer &idSet)
checks if required decision ID is in the set of IDs in the container
Definition: TrigCompositeUtilsRoot.cxx:117
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
Trk::TruthTrackCreation::m_trackSummaryTool
ToolHandle< Trk::IExtendedTrackSummaryTool > m_trackSummaryTool
summary tool for completing the track
Definition: TruthTrackCreation.h:76
Trk::TruthTrackCreation::execute
StatusCode execute()
standard Athena-Algorithm method
Definition: TruthTrackCreation.cxx:92
AthCommonDataStore< AthCommonMsg< Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
PRD_TruthTrajectory.h
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
GenParticle.h
Trk::TruthTrackCreation::m_truthTrackBuilder
ToolHandle< Trk::ITruthTrackBuilder > m_truthTrackBuilder
truth tools
Definition: TruthTrackCreation.h:69
Trk::TruthTrackCreation::finalize
StatusCode finalize()
standard Athena-Algorithm method
Definition: TruthTrackCreation.cxx:84
Trk::TruthTrackCreation::m_prdTruthTrajectoryBuilder
ToolHandle< Trk::IPRD_TruthTrajectoryBuilder > m_prdTruthTrajectoryBuilder
truth tools
Definition: TruthTrackCreation.h:68
SimpleVector.h
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
Trk::TruthTrackCreation::initialize
StatusCode initialize()
standard Athena-Algorithm method
Definition: TruthTrackCreation.cxx:46
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
Trk::TruthTrackCreation::~TruthTrackCreation
~TruthTrackCreation()
Default Destructor.
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
AthAlgorithm
Definition: AthAlgorithm.h:47
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
Trk::TruthTrackCreation::m_prdTruthTrajectorySelectors
ToolHandleArray< Trk::IPRD_TruthTrajectorySelector > m_prdTruthTrajectorySelectors
PRD truth trajectory selectors.
Definition: TruthTrackCreation.h:71
DataVector::push_back
value_type push_back(value_type pElem)
Add an element to the end of the collection.
IPRD_TruthTrajectorySelector.h
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:76
SG::WriteHandle::record
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
xAOD::track
@ track
Definition: TrackingPrimitives.h:512
TruthTrackCreation.h
ITrackSelectorTool.h
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
IPRD_TruthTrajectoryBuilder.h
Trk::TruthTrackCreation::m_trackSelectors
ToolHandleArray< Trk::ITrackSelectorTool > m_trackSelectors
track selectors for a posteriory track selection
Definition: TruthTrackCreation.h:72