ATLAS Offline Software
TrackCollectionMerger.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 // 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 
24 // Initialisation
28 {
29 
30  ATH_MSG_DEBUG("Initializing TrackCollectionMerger");
31  ATH_CHECK(m_tracklocation.initialize());
32  ATH_CHECK(m_pileupTRT.initialize(m_doTrackOverlay));
33  ATH_CHECK(m_pileupPixel.initialize(m_doTrackOverlay));
34  ATH_CHECK(m_pileupSCT.initialize(m_doTrackOverlay));
35  ATH_CHECK(m_outtracklocation.initialize());
36  ATH_CHECK(m_assoTool.retrieve(DisableTool{m_assoTool.name().empty()}));
37  ATH_CHECK(m_assoMapName.initialize(!m_assoMapName.key().empty()));
38  return StatusCode::SUCCESS;
39 }
40 
42 // Execute
45 Trk::TrackCollectionMerger::execute(const EventContext& ctx) const{
46 
47  auto outputCol = std::make_unique<ConstDataVector<TrackCollection>>(SG::VIEW_ELEMENTS);
48  ATH_MSG_DEBUG("Number of Track collections " << m_tracklocation.size());
49 
50  // pre-loop to reserve enough space in the output collection
51  std::vector<const TrackCollection*> trackCollections;
52  trackCollections.reserve(m_tracklocation.size());
53  size_t ttNumber = 0;
54  for (const auto& tcname : m_tracklocation){
56  SG::ReadHandle<TrackCollection> trackCol (tcname, ctx);
57  trackCollections.push_back(trackCol.cptr());
58  ttNumber += trackCol->size();
59  }
60  std::unique_ptr<Trk::PRDtoTrackMap> pPrdToTrackMap(m_assoTool ? m_assoTool->createPRDtoTrackMap() : nullptr);
61  // reserve the right number of entries for the output collection
62  outputCol->reserve(ttNumber);
63  // merging loop
64  for (auto& tciter : trackCollections) {
65  // merge them in
66  if (mergeTrack(tciter, pPrdToTrackMap.get(), outputCol.get()).isFailure()) {
67  ATH_MSG_ERROR("Failed to merge tracks! ");
68  }
69  }
70  ATH_MSG_DEBUG("Size of combined tracks " << outputCol->size());
71 
72  auto h_write = SG::makeHandle(m_outtracklocation, ctx);
73  ATH_CHECK(h_write.record(std::move(outputCol)));
74  //
75  if (!m_assoMapName.key().empty()) {
76  SG::WriteHandle<Trk::PRDtoTrackMap> write_handle(m_assoMapName, ctx);
77  if (write_handle.record( m_assoTool->reduceToStorableMap(std::move(pPrdToTrackMap))).isFailure()) {
78  ATH_MSG_FATAL("Failed to add PRD to track association map.");
79  }
80  }
81  //Print common event information
82  ATH_MSG_DEBUG("Done !");
83  return StatusCode::SUCCESS;
84 }
85 
87 // Finalize
91 {
92  return StatusCode::SUCCESS;
93 }
94 
96 // Merge track collections and remove duplicates
100  Trk::PRDtoTrackMap* pPrdToTrackMap,
101  ConstDataVector<TrackCollection>* outputCol) const
102 {
103  // loop over tracks, accept them and add them into association tool
104  if (trackCol && !trackCol->empty()) {
105  ATH_MSG_DEBUG("Size of track collection " << trackCol->size());
106  if (not pPrdToTrackMap)
107  ATH_MSG_WARNING("No valid PRD to Track Map; was the association tool name missing?");
108  // loop over tracks
109  for (const auto* const rf : *trackCol) {
110  outputCol->push_back(rf);
111  // add tracks into PRD tool
112  if (m_assoTool and m_assoTool->addPRDs(*pPrdToTrackMap, *rf).isFailure())
113  ATH_MSG_WARNING("Failed to add PRDs to map");
114  }
115  }
116 
117  return StatusCode::SUCCESS;
118 }
119 
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
Trk::TrackCollectionMerger::finalize
virtual StatusCode finalize() override final
Definition: TrackCollectionMerger.cxx:90
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:99
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:27
DataVector< Trk::Track >
Trk::TrackCollectionMerger::execute
virtual StatusCode execute(const EventContext &ctx) const override final
Definition: TrackCollectionMerger.cxx:45
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
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
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.