ATLAS Offline Software
TrackCollectionMerger.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 // Implementation file for class Trk::TrackCollectionMerger
8 
10 // Version 1.0 11/26/2007 Thomas Koffas
12 #include "GaudiKernel/MsgStream.h"
15 
17 // Constructor
20  ISvcLocator* pSvcLocator)
21  : AthReentrantAlgorithm(name, pSvcLocator)
22  , m_outtracklocation("CombinedInDetTracks")
23  , m_doTrackOverlay(false)
24 {
25  declareProperty("TracksLocation", m_tracklocation);
26  declareProperty("OutputTracksLocation", m_outtracklocation);
27  declareProperty("DoTrackOverlay", m_doTrackOverlay);
28 }
29 
31 // Initialisation
35 {
36 
37  ATH_MSG_DEBUG("Initializing TrackCollectionMerger");
38  ATH_CHECK(m_tracklocation.initialize());
39  ATH_CHECK(m_pileupTRT.initialize(m_doTrackOverlay));
40  ATH_CHECK(m_pileupPixel.initialize(m_doTrackOverlay));
41  ATH_CHECK(m_pileupSCT.initialize(m_doTrackOverlay));
42  ATH_CHECK(m_outtracklocation.initialize());
43  ATH_CHECK(m_assoTool.retrieve(DisableTool{m_assoTool.name().empty()}));
44  ATH_CHECK(m_assoMapName.initialize(!m_assoMapName.key().empty()));
45  return StatusCode::SUCCESS;
46 }
47 
49 // Execute
52 Trk::TrackCollectionMerger::execute(const EventContext& ctx) const{
53 
54  auto outputCol = std::make_unique<ConstDataVector<TrackCollection>>(SG::VIEW_ELEMENTS);
55  ATH_MSG_DEBUG("Number of Track collections " << m_tracklocation.size());
56 
57  // pre-loop to reserve enough space in the output collection
58  std::vector<const TrackCollection*> trackCollections;
59  trackCollections.reserve(m_tracklocation.size());
60  size_t ttNumber = 0;
61  for (const auto& tcname : m_tracklocation){
63  SG::ReadHandle<TrackCollection> trackCol (tcname, ctx);
64  trackCollections.push_back(trackCol.cptr());
65  ttNumber += trackCol->size();
66  }
67  std::unique_ptr<Trk::PRDtoTrackMap> pPrdToTrackMap(m_assoTool ? m_assoTool->createPRDtoTrackMap() : nullptr);
68  // reserve the right number of entries for the output collection
69  outputCol->reserve(ttNumber);
70  // merging loop
71  for (auto& tciter : trackCollections) {
72  // merge them in
73  if (mergeTrack(tciter, pPrdToTrackMap.get(), outputCol.get()).isFailure()) {
74  ATH_MSG_ERROR("Failed to merge tracks! ");
75  }
76  }
77  ATH_MSG_DEBUG("Size of combined tracks " << outputCol->size());
78 
79  auto h_write = SG::makeHandle(m_outtracklocation, ctx);
80  ATH_CHECK(h_write.record(std::move(outputCol)));
81  //
82  if (!m_assoMapName.key().empty()) {
83  SG::WriteHandle<Trk::PRDtoTrackMap> write_handle(m_assoMapName, ctx);
84  if (write_handle.record( m_assoTool->reduceToStorableMap(std::move(pPrdToTrackMap))).isFailure()) {
85  ATH_MSG_FATAL("Failed to add PRD to track association map.");
86  }
87  }
88  //Print common event information
89  ATH_MSG_DEBUG("Done !");
90  return StatusCode::SUCCESS;
91 }
92 
94 // Finalize
98 {
99  return StatusCode::SUCCESS;
100 }
101 
103 // Merge track collections and remove duplicates
107  Trk::PRDtoTrackMap* pPrdToTrackMap,
108  ConstDataVector<TrackCollection>* outputCol) const
109 {
110  // loop over tracks, accept them and add them into association tool
111  if (trackCol && !trackCol->empty()) {
112  ATH_MSG_DEBUG("Size of track collection " << trackCol->size());
113  if (not pPrdToTrackMap)
114  ATH_MSG_WARNING("No valid PRD to Track Map; was the association tool name missing?");
115  // loop over tracks
116  for (const auto* const rf : *trackCol) {
117  outputCol->push_back(rf);
118  // add tracks into PRD tool
119  if (m_assoTool and m_assoTool->addPRDs(*pPrdToTrackMap, *rf).isFailure())
120  ATH_MSG_WARNING("Failed to add PRDs to map");
121  }
122  }
123 
124  return StatusCode::SUCCESS;
125 }
126 
Trk::TrackCollectionMerger::m_outtracklocation
SG::WriteHandleKey< ConstDataVector< TrackCollection > > m_outtracklocation
Combined track collection.
Definition: TrackCollectionMerger.h:91
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
SG::ReadHandle::cptr
const_pointer_type cptr()
Dereference the pointer.
SG::VIEW_ELEMENTS
@ VIEW_ELEMENTS
this data object is a view, it does not own its elmts
Definition: OwnershipPolicy.h:18
Trk::PRDtoTrackMap
Definition: PRDtoTrackMap.h:17
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
AthCommonDataStore< AthCommonMsg< Gaudi::Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
Trk::TrackCollectionMerger::finalize
virtual StatusCode finalize() override final
Definition: TrackCollectionMerger.cxx:97
PrepRawData.h
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:83
TrackCollectionMerger.h
SG::makeHandle
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
Definition: ReadCondHandle.h:270
Trk::TrackCollectionMerger::mergeTrack
StatusCode mergeTrack(const TrackCollection *trackCol, Trk::PRDtoTrackMap *pPrdToTrackMap, ConstDataVector< TrackCollection > *outputCol) const
Protected methods:
Definition: TrackCollectionMerger.cxx:106
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
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
Trk::TrackCollectionMerger::initialize
virtual StatusCode initialize() override final
Definition: TrackCollectionMerger.cxx:34
DataVector< Trk::Track >
Trk::TrackCollectionMerger::execute
virtual StatusCode execute(const EventContext &ctx) const override final
Definition: TrackCollectionMerger.cxx:52
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
ConstDataVector::push_back
value_type push_back(value_type pElem)
Add an element to the end of the collection.
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:76
Trk::TrackCollectionMerger::TrackCollectionMerger
TrackCollectionMerger(const std::string &name, ISvcLocator *pSvcLocator)
Standard Algotithm methods:
Definition: TrackCollectionMerger.cxx:19
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
ConstDataVector
DataVector adapter that acts like it holds const pointers.
Definition: ConstDataVector.h:76
Trk::TrackCollectionMerger::m_tracklocation
SG::ReadHandleKeyArray< TrackCollection > m_tracklocation
Protected data:
Definition: TrackCollectionMerger.h:57
Trk::TrackCollectionMerger::m_doTrackOverlay
bool m_doTrackOverlay
Definition: TrackCollectionMerger.h:117
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
DataVector::empty
bool empty() const noexcept
Returns true if the collection is empty.