ATLAS Offline Software
Loading...
Searching...
No Matches
ClusterizationAlg.icc
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4#include "AthenaMonitoringKernel/Monitored.h"
5#include "xAODInDetMeasurement/ContainerAccessor.h"
6#include "ActsInterop/TableUtils.h"
7#include "AthAllocators/DataPool.h"
8
9namespace ActsTrk {
10
11template <typename IClusteringTool, bool useCache>
12ClusterizationAlg<IClusteringTool, useCache>::ClusterizationAlg(const std::string& name,
13 ISvcLocator* pSvcLocator)
14 : AthReentrantAlgorithm(name, pSvcLocator)
15{}
16
17template <typename IClusteringTool, bool useCache>
18StatusCode ClusterizationAlg<IClusteringTool, useCache>::initialize()
19{
20 ATH_MSG_DEBUG("Initializing " << name() << " ...");
21
22 ATH_CHECK(m_rdoContainerKey.initialize());
23 ATH_CHECK(m_clusterContainerKey.initialize());
24 ATH_CHECK(m_roiCollectionKey.initialize());
25
26 ATH_CHECK(m_detEleCollKey.initialize());
27 ATH_CHECK(m_detElStatus.initialize());
28
29 ATH_CHECK(m_clusteringTool.retrieve());
30 ATH_CHECK(m_regionSelector.retrieve());
31
32 ATH_CHECK(detStore()->retrieve(m_idHelper, m_idHelperName));
33
34 // Monitoring
35 ATH_CHECK(m_monTool.retrieve(EnableTool{not m_monTool.empty()}));
36
37 //caching
38 ATH_CHECK(m_ClusterCache.initialize(useCache));
39 ATH_CHECK(m_ClusterCacheBackend.initialize(useCache));
40
41 return StatusCode::SUCCESS;
42}
43
44template <typename IClusteringTool, bool useCache>
45StatusCode ClusterizationAlg<IClusteringTool, useCache>::finalize()
46{
47 ATH_MSG_INFO("Clusterization statistics" << std::endl << makeTable(m_stat,
48 std::array<std::string, kNStat>{
49 "RDOs",
50 "Clusters"
51 }).columnWidth(10));
52
53 return StatusCode::SUCCESS;
54}
55
56template <typename IClusteringTool, bool useCache>
57StatusCode ClusterizationAlg<IClusteringTool, useCache>::execute(const EventContext& ctx) const
58{
59 auto timer = Monitored::Timer<std::chrono::milliseconds>( "TIME_execute" );
60 auto timer_rdoReading = Monitored::Timer<std::chrono::milliseconds>( "TIME_readingRDOs" );
61 auto timer_processing = Monitored::Timer<std::chrono::milliseconds>( "TIME_clusterization" );
62 auto mon_nclusters = Monitored::Scalar<int>( "NClustersCreated" );
63 auto mon = Monitored::Group( m_monTool, timer, timer_rdoReading, timer_processing, mon_nclusters );
64
65 timer_rdoReading.start();
66 SG::ReadHandle<RDOContainer> rdoContainerHandle = SG::makeHandle(m_rdoContainerKey, ctx);
67 ATH_CHECK(rdoContainerHandle.isValid());
68 const RDOContainer* rdoContainer = rdoContainerHandle.cptr();
69 timer_rdoReading.stop();
70
71 SG::ReadHandle<TrigRoiDescriptorCollection> roiCollectionHandle = SG::makeHandle( m_roiCollectionKey, ctx );
72 ATH_CHECK(roiCollectionHandle.isValid());
73 const TrigRoiDescriptorCollection *roiCollection = roiCollectionHandle.cptr();
74
75 SG::ReadCondHandle< InDetDD::SiDetectorElementCollection > detEleHandle = SG::makeHandle( m_detEleCollKey, ctx );
76 ATH_CHECK(detEleHandle.isValid());
77 const InDetDD::SiDetectorElementCollection* elements = detEleHandle.cptr();
78
79 SG::ReadHandle< InDet::SiDetectorElementStatus > detEleStatusHandle = SG::makeHandle(m_detElStatus, ctx);
80 ATH_CHECK(detEleStatusHandle.isValid());
81 const InDet::SiDetectorElementStatus* detEleStatus = detEleStatusHandle.cptr();
82
83 timer_processing.start();
84
85 Cache_WriteHandle cacheHandle;
86 if constexpr (useCache) {
87 cacheHandle = Cache_WriteHandle(m_ClusterCache, ctx);
88 auto updateHandle = Cache_BackendUpdateHandle(m_ClusterCacheBackend, ctx);
89 ATH_CHECK(updateHandle.isValid());
90 ATH_CHECK(cacheHandle.record(std::make_unique<Cache_IDC>(updateHandle.ptr())));
91 ATH_CHECK(cacheHandle.isValid());
92 }
93
94 // count number of elements and store some collections
95 std::vector<IdentifierHash> listOfIds;
96 listOfIds.reserve(elements->size());
97
98 std::vector<bool> toBeProcessedIds(elements->size(), false);
99
100 std::vector<Cache_IDCLock> idclocks;
101
102 unsigned int n_rdos_total=0u;
103 unsigned int n_cluster_total=0u;
104 unsigned int n_modules=0u;
105 // numbers not double counting corrected.
106 // So, in case of large overlap of ROIs, will overallocate
107 // the temporary cluster collection
108 for (const auto* roi : *roiCollection) {
109 // get list of ids
110 listOfIds.clear();
111 m_regionSelector->lookup(ctx)->HashIDList(*roi, listOfIds);
112 auto [roi_n_cluster, roi_n_rdos] = m_clusteringTool->countCells(*rdoContainer, listOfIds,*elements );
113 n_cluster_total += roi_n_cluster;
114 n_rdos_total += roi_n_rdos;
115 n_modules += listOfIds.size();
116 }
117
118 typename IClusteringTool::CellContainer cellContainer(n_modules, n_cluster_total, n_rdos_total);
119
120 // loop on ROIs
121 unsigned int nRDOs = 0u;
122 std::size_t nClusters = 0ul;
123 // if the roiCollection is larger than one than listOfIds does not contain the correct
124 // HashIDList and the lists have to be requested again.
125 bool request_id_list_again = roiCollection->size()!=1;
126 for (const auto* roi : *roiCollection) {
127 // get list of ids
128 // if the roi collection contains a single element listOfIds
129 // already contains the result
130 if (request_id_list_again) {
131 listOfIds.clear();
132 m_regionSelector->lookup(ctx)->HashIDList(*roi, listOfIds);
133 }
134
135 // loop on IDs
136 for (const IdentifierHash& id : listOfIds) {
137 if (not id.is_valid()) {
138 ATH_MSG_ERROR("Id hash is not valid: " << id);
139 return StatusCode::FAILURE;
140 }
141
142 // check if already considered
143 if (toBeProcessedIds[id]) continue;
144 toBeProcessedIds[id] = true;
145
146 Cache_IDCLock cache_wh;
147 if constexpr (useCache) {
148 //obtain a write handle
149 auto tmp = cacheHandle->getWriteHandle(id);
150 Cache_IDCLock::Swap(cache_wh, tmp);
151 //check if already available
152 if(cache_wh.OnlineAndPresentInAnotherView()) continue;
153 }
154
155 const InDetDD::SiDetectorElement* element = elements->getDetectorElement(id);
156 if (not element) {
157 ATH_MSG_ERROR("Cannot retrieve Detector Element for id hash: " << id);
158 return StatusCode::FAILURE;
159 }
160
161 // unpack RDOs and store them
162 const RawDataCollection* rdos = rdoContainer->indexFindPtr(id);
163 if (not rdos or rdos->empty()) {
164 continue;
165 }
166
167 if constexpr (useCache) {
168 idclocks.push_back( std::move(cache_wh) );
169 }
170 // make ACTS clusters
171 ATH_CHECK( m_clusteringTool->clusterize(ctx,
172 *rdos,
173 *detEleStatus,
174 *element,
175 cellContainer) );
176 nRDOs+=rdos->size();
177 nClusters += cellContainer.m_moduleClusterRange.back().nClusters();
178 } // loop on IDs
179 } // loop on ROIs
180
181 mon_nclusters = nClusters;
182
183 SG::WriteHandle<ClusterContainer> clusterHandle = SG::makeHandle(m_clusterContainerKey, ctx);
184 ATH_CHECK(clusterHandle.record( std::make_unique<ClusterContainer>(SG::VIEW_ELEMENTS, SG::ALWAYS_TRACK_INDICES),
185 std::make_unique<ClusterAuxContainer>() ));
186 ClusterContainer *clusterContainer = clusterHandle.ptr();
187
188 using xAODCluster_t = typename ClusterContainer::base_value_type;
189 DataPool<xAODCluster_t> pool (nClusters);
190 clusterContainer->push_new(nClusters, [&pool]() {return pool.nextElementPtr();});
191
192 std::any cache = m_clusteringTool->createEventDataCache (*clusterHandle,cellContainer.nCellsTotal());
193
194 // Fill the collection to xAOD format, computing necessary quantities
195 std::size_t xaodCounter = 0ul;
196 for (std::size_t i(0); i<cellContainer.size(); ++i) {
197 const InDetDD::SiDetectorElement* element = elements->getDetectorElement(cellContainer.moduleClusterRange(i).idHash);
198
199 ATH_CHECK( m_clusteringTool->makeClusters(ctx,
200 *rdoContainer,
201 cellContainer,
202 i,
203 *element,
204 xaodCounter,
205 *clusterContainer,
206 cache) );
207 assert( i < cellContainer.nModules() ) ;
208 unsigned int n_new_clusters=cellContainer.moduleClusterRange(i).nClusters();
209 if constexpr (useCache) {
210 ATH_CHECK(Cache::Helper<BaseClusterType>::insert(idclocks[i],
211 clusterContainer,
212 xaodCounter,
213 xaodCounter + n_new_clusters));
214 }
215 xaodCounter+=n_new_clusters;
216 }
217
218 m_stat[kNRdo] += nRDOs;
219 m_stat[kNClusters] += clusterContainer->size();
220 ATH_MSG_DEBUG("Clusters produced size: "<<clusterContainer->size());
221 timer_processing.stop();
222 return StatusCode::SUCCESS;
223}
224
225} // namespace ActsTrk