ATLAS Offline Software
Loading...
Searching...
No Matches
FPGATrackSimDataPrepAlg.cxx
Go to the documentation of this file.
1
2
3// Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
4
6
14
18
20
23
25
26#include "GaudiKernel/IEventProcessor.h"
27
28
29constexpr bool enableBenchmark =
30#ifdef BENCHMARK_FPGATRACKSIM
31 true;
32#else
33 false;
34#endif
35
37// Initialize
38
39FPGATrackSimDataPrepAlg::FPGATrackSimDataPrepAlg (const std::string& name, ISvcLocator* pSvcLocator) :
40 AthAlgorithm(name, pSvcLocator)
41{
42}
43
44
46{
47 std::stringstream ss(m_description);
48 std::string line;
49 ATH_MSG_INFO("Tag config:");
50 if (!m_description.empty()) {
51 while (std::getline(ss, line, '\n')) {
52 ATH_MSG_INFO('\t' << line);
53 }
54 }
55
56 // Dump the configuration to make sure it propagated through right
57 const std::vector<Gaudi::Details::PropertyBase*> props = this->getProperties();
58 for( Gaudi::Details::PropertyBase* prop : props ) {
59 if (prop->ownerTypeName()==this->type()) {
60 ATH_MSG_DEBUG("Property:\t" << prop->name() << "\t : \t" << prop->toString());
61 }
62 }
63
64
65 ATH_CHECK(m_hitSGInputTool.retrieve(EnableTool{!m_hitSGInputTool.empty()}));
66
67 ATH_CHECK(m_hitInputTool.retrieve(EnableTool{!m_hitInputTool.empty()}));
68 ATH_CHECK(m_hitInputTool2.retrieve(EnableTool{m_secondInputToolN > 0 && !m_hitInputTool2.empty()}));
69 ATH_CHECK(m_hitMapTools.retrieve());
70 ATH_CHECK(m_hitFilteringTool.retrieve(EnableTool{m_doHitFiltering}));
71 ATH_CHECK(m_clusteringTool.retrieve(EnableTool{m_clustering > 0}));
72
73 ATH_CHECK(m_writeOutputTool.retrieve());
75
76
77 ATH_MSG_DEBUG("initialize() Instantiating root objects");
79 m_logicEventHeader = m_writeOutputTool->addInputBranch(m_postClusterBranch.value(), true);
80
81 ATH_MSG_DEBUG("initialize() Setting branch");
82
83 if (!m_monTool.empty())
84 ATH_CHECK(m_monTool.retrieve());
85
86 ATH_CHECK( m_FPGAClusterKey.initialize() );
87 ATH_CHECK( m_FPGAHitKey.initialize() );
88 ATH_CHECK( m_FPGAHitUnmappedKey.initialize() );
90 ATH_CHECK( m_truthLinkContainerKey.initialize() );
91 ATH_CHECK( m_FPGATruthTrackKey.initialize() );
92 ATH_CHECK( m_FPGAOfflineTrackKey.initialize() );
93 ATH_CHECK( m_FPGAEventInfoKey.initialize() );
94
95 ATH_CHECK( m_chrono.retrieve() );
96 ATH_MSG_DEBUG("initialize() Finished");
97
98
99 return StatusCode::SUCCESS;
100}
101
102
104// MAIN EXECUTE ROUTINE //
106
108{
109 const EventContext& ctx = getContext();
110
111 // Read inputs
112 bool done = false;
113 ATH_CHECK(readInputs(done));
114
115 if (done) {
116 SmartIF<IEventProcessor> appMgr{service("ApplicationMgr")};
117 if (!appMgr) {
118 ATH_MSG_ERROR("Failed to retrieve ApplicationMgr as IEventProcessor");
119 return StatusCode::FAILURE;
120 }
121 return appMgr->stopRun();
122 }
123
125 ATH_CHECK( FPGAHits.record (std::make_unique<FPGATrackSimHitCollection>()));
126
128 ATH_CHECK( FPGAHitUnmapped.record (std::make_unique<FPGATrackSimHitCollection>()));
129
131 ATH_CHECK( FPGAClusters.record (std::make_unique<FPGATrackSimClusterCollection>()));
132
134 ATH_CHECK(truthLinkVec.record(std::make_unique<xAODTruthParticleLinkVector>()));
135
137 ATH_CHECK(FPGATruthTracks.record(std::make_unique<FPGATrackSimTruthTrackCollection>()));
138
140 ATH_CHECK(FPGAOfflineTracks.record(std::make_unique<FPGATrackSimOfflineTrackCollection>()));
141 // Apply event selection based on truth tracks
142 if (m_doEvtSel) {
143 bool acceptEvent = false;
144 if constexpr (enableBenchmark) m_chrono->chronoStart("DataPrep: EventSelection");
145 for (auto eventSelector : m_eventSelectionTools)
146 {
147 if (eventSelector->selectEvent(m_eventHeader)) {
148 ATH_MSG_DEBUG("Event accepted by: " << eventSelector->name());
149 acceptEvent = true;
150 if ((m_writeRegion>=0)&&(m_writeRegion==eventSelector->getRegionID())) {
151 m_writeOutputTool->activateEventOutput();
152 }
153 }
154 }
155 if constexpr (enableBenchmark) m_chrono->chronoStop("DataPrep: EventSelection");
156 if (m_useInternalTruthTracks && acceptEvent) {
157 if constexpr (enableBenchmark) m_chrono->chronoStart("DataPrep: TruthMatching");
158 SG::ReadHandle<xAOD::TruthParticleContainer> truthParticleContainer(m_inputTruthParticleContainerKey, ctx); // Read offline TruthParticles
159 if (!truthParticleContainer.isValid()) {
160 ATH_MSG_ERROR("No valid truth particle container with key " << truthParticleContainer.key());
161 return StatusCode::FAILURE;
162 }
163 ATH_MSG_DEBUG("making mp of truth particles");
164 std::unordered_map<HepMcParticleLink::barcode_type, std::pair<const xAOD::TruthParticle*, size_t>> truthParticlesMap;
165 size_t truthParticleIndex = 0;
166 for (const xAOD::TruthParticle* truthParticle : *truthParticleContainer) {
167 truthParticlesMap.insert(std::make_pair(HepMC::uniqueID(truthParticle), std::make_pair(truthParticle,truthParticleIndex)));
168 truthParticleIndex++;
169 }
170 const FPGATrackSimTruthTrackCollection& fpgaTruthTracks = m_eventHeader.optional().getTruthTracks();
171 truthLinkVec->reserve(fpgaTruthTracks.size());
172 ATH_MSG_DEBUG("begin truth matching for " << fpgaTruthTracks.size() << " FPGA truth tracks");
173 for (const FPGATrackSimTruthTrack& fpgaTruthTrack : fpgaTruthTracks) {
174 auto it = truthParticlesMap.find(fpgaTruthTrack.getUniqueID()); // TODO FIXME need to check FPGATrackSimTruthTrack uniqueIDs are properly filled
175 if (it != truthParticlesMap.end()) {
176 ElementLink<xAOD::TruthParticleContainer> truthParticleLink(*truthParticleContainer, it->second.second);
177 // TODO: check if we can avoid using the previously-created map and look directly for the unique ID in the link vector container
178 truthLinkVec->push_back(new xAODTruthParticleLink(HepMcParticleLink(HepMC::uniqueID(it->second.first), 0,
180 ATH_MSG_DEBUG("Truth link added");
181 }
182 }
183 if (truthLinkVec->empty()) {
184 ATH_MSG_DEBUG("No truth particles selected. Skipping event...");
185 return StatusCode::SUCCESS;
186 }
187 std::stable_sort(truthLinkVec->begin(), truthLinkVec->end(), SortTruthParticleLink());
188 if constexpr (enableBenchmark) m_chrono->chronoStop("DataPrep: TruthMatching");
189 }
190 }
191 else {
192 ATH_MSG_DEBUG("No Event Selection applied");
193 for (auto eventSelector : m_eventSelectionTools) {
194 eventSelector->setSelectedEvent(true);
195 }
196 }
197
198 // Event passes cuts, count it
199 m_evt++;
200
201 // Map, cluster, and filter hits
202 ATH_CHECK(processInputs(FPGAHitUnmapped, FPGAClusters));
203
204 if constexpr (enableBenchmark) m_chrono->chronoStart("DataPrep: get truth/offline tracks");
205 // Now that this is done, push truth tracks back to storegate.
206 for (const auto& truthtrack : m_logicEventHeader->optional().getTruthTracks()) {
207 FPGATruthTracks->push_back(truthtrack);
208 }
209
210 // Need to do the same for offline tracks.
211 for (const auto& offlineTrack : m_logicEventHeader->optional().getOfflineTracks()) {
212 FPGAOfflineTracks->push_back(offlineTrack);
213 }
214
215 if constexpr (enableBenchmark) m_chrono->chronoStop("DataPrep: get truth/offline tracks");
216
217 // Get reference to hits
218
219 // Recording Data
220 for (auto eventSelector : m_eventSelectionTools)
221 {
222 // Get reference to hits
223 const unsigned& regionID = eventSelector->getRegionID();
224
225 auto mon_regionID = Monitored::Scalar<unsigned>("regionID", regionID);
226 Monitored::Group(m_monTool, mon_regionID);
227 }
228
229 std::vector<FPGATrackSimHit> const& hits = m_logicEventHeader->towers().at(0).hits();
230 if (m_recordHits) {
231 if constexpr (enableBenchmark) m_chrono->chronoStart("DataPrep: record hits");
232 // If and when we set up code to run over more than one region/tower at a time this will need to be updated
233 FPGAHits->reserve(hits.size());
234 for (const auto& hit : hits) {
235 if (hit.isReal()) FPGAHits->push_back(hit);
236 }
237 if constexpr (enableBenchmark) m_chrono->chronoStop("DataPrep: record hits");
238 }
239
240 auto mon_nhits = Monitored::Scalar<unsigned>("nHits", hits.size());
241 auto mon_nhits_unmapped = Monitored::Scalar<unsigned>("nHits_unmapped", m_hits_miss.size());
242 Monitored::Group(m_monTool, mon_nhits, mon_nhits_unmapped);
243
244 // Put the FPGATrackSim event info on storegate so later algorithms can access it easily.
246 ATH_CHECK(FPGAEventInfo.record(std::make_unique<FPGATrackSimEventInfo>(m_eventHeader.event())));
247
248 // Write the output and reset
250 ATH_CHECK(m_writeOutputTool->writeData());
251
252 // Reset data pointers
253 m_eventHeader.reset();
254 m_logicEventHeader->reset();
256
257 return StatusCode::SUCCESS;
258}
259
260
262// INPUT PASSING, READING AND PROCESSING //
264
266{
267
268 if ( !m_hitSGInputTool.empty()) {
269 ATH_CHECK(m_hitSGInputTool->readData(&m_eventHeader, Gaudi::Hive::currentContext()));
270 ATH_MSG_DEBUG("Loaded " << m_eventHeader.nHits() << " hits in event header from SG");
271
272 return StatusCode::SUCCESS;
273 }
274
275 if (m_ev % m_firstInputToolN == 0)
276 {
277 // Read primary input
279 if (done)
280 {
281 ATH_MSG_DEBUG("Cannot read more events from file, returning");
282 return StatusCode::SUCCESS; // end of loop over events
283 }
284 }
285
287
288 // Read secondary input
289 for (int i = 0; i < m_secondInputToolN; i++)
290 {
291 ATH_CHECK(m_hitInputTool2->readData(&m_eventHeader, done, false));
292 if (done)
293 {
294 ATH_MSG_INFO("Cannot read more events from file, returning");
295 return StatusCode::SUCCESS;
296 }
297 }
298
299 m_ev++;
300
301 return StatusCode::SUCCESS;
302}
303
304
305// Applies clustering, mapping, hit filtering, and space points
308{
309 m_clusters->clear();
310 m_hits_miss.clear();
311
312 // Map hits
313 ATH_MSG_DEBUG("Running hits conversion");
314 m_logicEventHeader->reset();
316 if constexpr (enableBenchmark) m_chrono->chronoStart("DataPrep: RawToLogical");
317 for (auto hitMapTool : m_hitMapTools){
318 ATH_CHECK(hitMapTool->convert(1, m_eventHeader, *m_logicEventHeader));
319 }
320 if constexpr (enableBenchmark) m_chrono->chronoStop("DataPrep: RawToLogical");
321
322
323 for (const FPGATrackSimHit& hit : m_hits_miss) FPGAHitUnmapped->push_back(hit);
324
325
326 ATH_MSG_DEBUG("Hits conversion done, #unmapped hists = " << m_hits_miss.size());
327
328 // At this stage, copy the logicEventHeader.
330
331 if constexpr (enableBenchmark) m_chrono->chronoStart("DataPrep: Clustering");
332 // Clustering
333 for (int ic = 0; ic < m_clustering; ic++) {
334 ATH_MSG_DEBUG("Running clustering");
336 // I think I also want to pass m_clusters to random removal (but won't work currently)
337 if (m_doHitFiltering) ATH_CHECK(m_hitFilteringTool->DoRandomRemoval(*m_logicEventHeader, false));
338 unsigned npix(0), nstrip(0);
339 for (const FPGATrackSimCluster& cluster : *m_clusters) {
340 if (cluster.getClusterEquiv().isPixel()) npix++;
341 else nstrip++;
342 }
343 m_nPixClusters += npix;
344 m_nStripClusters += nstrip;
345 if (npix > m_nMaxPixClusters) m_nMaxPixClusters = npix;
346 if (nstrip > m_nMaxStripClusters) m_nMaxStripClusters = nstrip;
347 if (m_clusters->size() > m_nMaxClusters) m_nMaxClusters = m_clusters->size();
348 }
349 FPGAClusters->insert(
350 FPGAClusters->end(),
351 std::make_move_iterator(m_clusters->begin()),
352 std::make_move_iterator(m_clusters->end()));
353
354 if constexpr (enableBenchmark) m_chrono->chronoStop("DataPrep: Clustering");
355
356 return StatusCode::SUCCESS;
357}
358
360// Finalize
361
363{
364 ATH_MSG_INFO("PRINTING FPGATRACKSIM SIMPLE DATAPREP STATS");
365 ATH_MSG_INFO("========================================================================================");
366 ATH_MSG_INFO("Number of pixel clusters/event = " << m_nPixClusters/m_evt);
367 ATH_MSG_INFO("Number of strip clusters/event = " << m_nStripClusters/m_evt);
368 ATH_MSG_INFO("Max number of pixel clusters in an event = " << m_nMaxPixClusters);
369 ATH_MSG_INFO("Max number of strip clusters in an event = " << m_nMaxStripClusters);
370 ATH_MSG_INFO("Max number of clusters in an event = " << m_nMaxClusters);
371
372 return StatusCode::SUCCESS;
373}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_DEBUG(x)
: FPGATrackSim-specific class to represent an hit in the detector.
Utilize NN score to build track candidates.
Overlap removal tool for FPGATrackSimTrack.
Maps ITK module indices to FPGATrackSim regions.
Stores slice definitions for FPGATrackSim regions.
Defines a class for roads.
Structs that store the 5 track parameters.
std::vector< FPGATrackSimTruthTrack > FPGATrackSimTruthTrackCollection
static Double_t ss
AthAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Constructor with parameters:
ToolHandleArray< FPGATrackSimRawToLogicalHitsTool > m_hitMapTools
SG::WriteHandleKey< FPGATrackSimOfflineTrackCollection > m_FPGAOfflineTrackKey
SG::WriteHandleKey< FPGATrackSimHitCollection > m_FPGAHitUnmappedKey
Gaudi::Property< std::string > m_preClusterBranch
Gaudi::Property< std::string > m_postClusterBranch
virtual StatusCode finalize() override
Gaudi::Property< bool > m_doEvtSel
SG::WriteHandleKey< FPGATrackSimTruthTrackCollection > m_FPGATruthTrackKey
FPGATrackSimLogicalEventInputHeader * m_logicEventHeader_precluster
StatusCode processInputs(SG::WriteHandle< FPGATrackSimHitCollection > &FPGAHitUnmapped, SG::WriteHandle< FPGATrackSimClusterCollection > &FPGAClusters)
ToolHandle< IFPGATrackSimInputTool > m_hitSGInputTool
Gaudi::Property< int > m_firstInputToolN
ToolHandle< FPGATrackSimOutputHeaderTool > m_writeOutputTool
virtual StatusCode initialize() override
Gaudi::Property< int > m_secondInputToolN
ToolHandleArray< FPGATrackSim::FPGATrackSimEventSelectionTool > m_eventSelectionTools
ToolHandle< FPGATrackSimReadRawRandomHitsTool > m_hitInputTool2
Gaudi::Property< bool > m_writePreClusterBranch
SG::ReadHandleKey< xAOD::TruthParticleContainer > m_inputTruthParticleContainerKey
ToolHandle< GenericMonitoringTool > m_monTool
virtual StatusCode execute() override
Gaudi::Property< bool > m_useInternalTruthTracks
std::unique_ptr< FPGATrackSimClusterCollection > m_clusters
ToolHandle< IFPGATrackSimEventInputHeaderTool > m_hitInputTool
FPGATrackSimLogicalEventInputHeader * m_logicEventHeader
Gaudi::Property< int > m_writeRegion
SG::WriteHandleKey< FPGATrackSimHitCollection > m_FPGAHitKey
std::vector< FPGATrackSimHit > m_hits_miss
Gaudi::Property< int > m_clustering
SG::WriteHandleKey< FPGATrackSimEventInfo > m_FPGAEventInfoKey
Gaudi::Property< bool > m_writeOutputData
SG::WriteHandleKeyArray< FPGATrackSimClusterCollection > m_FPGAClusterKey
ServiceHandle< IChronoStatSvc > m_chrono
ToolHandle< FPGATrackSimClusteringToolI > m_clusteringTool
FPGATrackSimDataPrepAlg(const std::string &name, ISvcLocator *pSvcLocator)
Gaudi::Property< bool > m_doHitFiltering
StatusCode readInputs(bool &done)
Gaudi::Property< bool > m_recordHits
FPGATrackSimEventInputHeader m_firstInputHeader
FPGATrackSimEventInputHeader m_eventHeader
ToolHandle< IFPGATrackSimHitFilteringTool > m_hitFilteringTool
SG::WriteHandleKey< xAODTruthParticleLinkVector > m_truthLinkContainerKey
Group of local monitoring quantities and retain correlation when filling histograms
Declare a monitored scalar variable.
virtual bool isValid() override final
Can the handle be successfully dereferenced?
virtual const std::string & key() const override final
Return the StoreGate ID for the referenced object.
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
int uniqueID(const T &p)
void stable_sort(DataModel_detail::iterator< DVL > beg, DataModel_detail::iterator< DVL > end)
Specialization of stable_sort for DataVector/List.
TruthParticle_v1 TruthParticle
Typedef to implementation.
constexpr bool enableBenchmark