ATLAS Offline Software
Loading...
Searching...
No Matches
EFTrackingDataStreamLoaderAlgorithm.cxx
Go to the documentation of this file.
1/*
2 * Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 */
4
7
8#include <vector>
9#include <fstream>
10#include <format>
11#include <string>
12
14 const std::string& name,
15 ISvcLocator* pSvcLocator
16) : AthReentrantAlgorithm(name, pSvcLocator)
17{}
18
20 ATH_CHECK(m_GHITZTxtInputKeys.initialize());
21 ATH_CHECK(m_GHITZTxtOutputKeys.initialize());
23
24 for (std::size_t index = 0; index < m_GHITZTxtInputKeys.size(); index++) {
25 m_GHITZTxtInputEvents.push_back({});
27 m_GHITZTxtInputPaths.value().at(index),
28 [](std::ifstream& fileStream) {
29 std::string line{};
30 std::getline(fileStream, line);
31 return std::stoul(line, nullptr, 16);
32 },
33 [](const uint64_t word) { return FPGADataFormatUtilities::get_bitfields_GHITZ_w1(word).last; },
34 1,
36 ));
37 }
38
39 for (std::size_t index = 0; index < m_GHITZTxtOutputKeys.size(); index++) {
40 m_GHITZTxtOutputEvents.push_back({});
41 }
42
43 ATH_CHECK(m_GHITZBinInputKeys.initialize());
44 ATH_CHECK(m_GHITZBinOutputKeys.initialize());
45 ATH_CHECK(m_GHITZBinInputKeys.size() == m_GHITZBinInputPaths.size());
46
47 for (std::size_t index = 0; index < m_GHITZBinInputKeys.size(); index++) {
48 m_GHITZBinInputEvents.push_back({});
50 m_GHITZBinInputPaths.value().at(index),
51 [](std::ifstream& fileStream) {
52 std::array<char, 8> line;
53 fileStream.read(line.data(), 8);
54 return *std::bit_cast<uint64_t*>(line.data());
55 },
56 [](const uint64_t word) { return FPGADataFormatUtilities::get_bitfields_GHITZ_w1(word).last; },
57 1,
58 m_GHITZBinInputEvents.back()
59 ));
60 }
61
62 for (std::size_t index = 0; index < m_GHITZBinOutputKeys.size(); index++) {
63 m_GHITZBinOutputEvents.push_back({});
64 }
65
66 ATH_CHECK(m_CLUSTERTxtInputKeys.initialize());
67 ATH_CHECK(m_CLUSTERTxtOutputKeys.initialize());
68 ATH_CHECK(m_CLUSTERTxtInputKeys.size() == m_CLUSTERTxtInputPaths.size());
69
70 for (std::size_t index = 0; index < m_CLUSTERTxtInputKeys.size(); index++) {
71 m_CLUSTERTxtInputEvents.push_back({});
73 m_CLUSTERTxtInputPaths.value().at(index),
74 [](std::ifstream& fileStream) {
75 std::string line{};
76 std::getline(fileStream, line);
77 return std::stoul(line, nullptr, 16);
78 },
80 0,
81 m_CLUSTERTxtInputEvents.back()
82 ));
83 }
84
85 for (std::size_t index = 0; index < m_CLUSTERTxtOutputKeys.size(); index++) {
86 m_CLUSTERTxtOutputEvents.push_back({});
87 }
88
89 ATH_CHECK(m_CLUSTERBinInputKeys.initialize());
90 ATH_CHECK(m_CLUSTERBinOutputKeys.initialize());
91 ATH_CHECK(m_CLUSTERBinInputKeys.size() == m_CLUSTERBinInputPaths.size());
92
93 for (std::size_t index = 0; index < m_CLUSTERBinInputKeys.size(); index++) {
94 m_CLUSTERBinInputEvents.push_back({});
96 m_CLUSTERBinInputPaths.value().at(index),
97 [](std::ifstream& fileStream) {
98 std::array<char, 8> line;
99 fileStream.read(line.data(), 8);
100 return *std::bit_cast<uint64_t*>(line.data());
101 },
102 [](const uint64_t word) { return FPGADataFormatUtilities::get_bitfields_STRIP_CLUSTER(word).last; },
103 0,
104 m_CLUSTERBinInputEvents.back()
105 ));
106 }
107
108 for (std::size_t index = 0; index < m_CLUSTERBinOutputKeys.size(); index++) {
109 m_CLUSTERBinOutputEvents.push_back({});
110 }
111
112 return StatusCode::SUCCESS;
113}
114
115StatusCode EFTrackingDataStreamLoaderAlgorithm::execute(const EventContext& ctx) const {
117 for (std::size_t index = 0; index < m_GHITZTxtInputKeys.size(); index++) {
118 ATH_CHECK(ctx.evt() < m_GHITZTxtInputEvents.at(index).size());
120 ATH_CHECK(GHITZTxtInputHandle.record(std::make_unique<std::vector<uint64_t>>(
121 m_GHITZTxtInputEvents.at(index).at(ctx.evt())
122 )));
123 }
124
126 for (std::size_t index = 0; index < m_GHITZBinInputKeys.size(); index++) {
127 ATH_CHECK(ctx.evt() < m_GHITZBinInputEvents.at(index).size());
129 ATH_CHECK(GHITZBinInputHandle.record(std::make_unique<std::vector<uint64_t>>(
130 m_GHITZBinInputEvents.at(index).at(ctx.evt())
131 )));
132 }
133
135 for (std::size_t index = 0; index < m_CLUSTERTxtInputKeys.size(); index++) {
136 ATH_CHECK(ctx.evt() < m_CLUSTERTxtInputEvents.at(index).size());
138 ATH_CHECK(CLUSTERTxtInputHandle.record(std::make_unique<std::vector<uint64_t>>(
139 m_CLUSTERTxtInputEvents.at(index).at(ctx.evt())
140 )));
141 }
142
144 for (std::size_t index = 0; index < m_CLUSTERBinInputKeys.size(); index++) {
145 ATH_CHECK(ctx.evt() < m_CLUSTERBinInputEvents.at(index).size());
147 ATH_CHECK(CLUSTERBinInputHandle.record(std::make_unique<std::vector<uint64_t>>(
148 m_CLUSTERBinInputEvents.at(index).at(ctx.evt())
149 )));
150 }
151
152 {
153 std::lock_guard lock(m_mutex);
154 for (std::size_t index = 0; index < m_GHITZTxtOutputKeys.size(); index++) {
155 if (m_GHITZTxtOutputEvents.at(index).size() < (ctx.evt() + 1)) {
156 m_GHITZTxtOutputEvents.at(index).resize(ctx.evt() + 1);
157 }
158
159 ATH_CHECK(ctx.evt() < m_GHITZTxtOutputEvents.at(index).size());
161 m_GHITZTxtOutputEvents.at(index).at(ctx.evt()) = *GHITZTxtOutputHandle;
162 }
163
164 for (std::size_t index = 0; index < m_GHITZBinOutputKeys.size(); index++) {
165 if (m_GHITZBinOutputEvents.at(index).size() < (ctx.evt() + 1)) {
166 m_GHITZBinOutputEvents.at(index).resize(ctx.evt() + 1);
167 }
168
169 ATH_CHECK(ctx.evt() < m_GHITZBinOutputEvents.at(index).size());
171 m_GHITZBinOutputEvents.at(index).at(ctx.evt()) = *GHITZBinOutputHandle;
172 }
173
174 for (std::size_t index = 0; index < m_CLUSTERTxtOutputKeys.size(); index++) {
175 if (m_CLUSTERTxtOutputEvents.at(index).size() < (ctx.evt() + 1)) {
176 m_CLUSTERTxtOutputEvents.at(index).resize(ctx.evt() + 1);
177 }
178
179 ATH_CHECK(ctx.evt() < m_CLUSTERTxtOutputEvents.at(index).size());
181 m_CLUSTERTxtOutputEvents.at(index).at(ctx.evt()) = *CLUSTERTxtOutputHandle;
182 }
183
184 for (std::size_t index = 0; index < m_CLUSTERBinOutputKeys.size(); index++) {
185 if (m_CLUSTERBinOutputEvents.at(index).size() < (ctx.evt() + 1)) {
186 m_CLUSTERBinOutputEvents.at(index).resize(ctx.evt() + 1);
187 }
188
189 ATH_CHECK(ctx.evt() < m_CLUSTERBinOutputEvents.at(index).size());
191 m_CLUSTERBinOutputEvents.at(index).at(ctx.evt()) = *CLUSTERBinOutputHandle;
192 }
193 }
194
195 return StatusCode::SUCCESS;
196}
197
200
201 for (std::size_t index = 0; index < m_GHITZTxtOutputKeys.size(); index++) {
203 m_GHITZTxtOutputPaths.value().at(index),
204 [](
205 std::ofstream& fileStream,
206 const uint64_t word
207 ) {
208 fileStream << std::format("{:016x}\n", word);
209 },
210 [](const uint64_t word) { return FPGADataFormatUtilities::get_bitfields_GHITZ_w1(word).last; },
211 1,
212 m_GHITZTxtOutputEvents.at(index)
213 ));
214 }
215
217
218 for (std::size_t index = 0; index < m_GHITZBinOutputKeys.size(); index++) {
219 //coverity[ARRAY_VS_SINGLETON]
221 m_GHITZBinOutputPaths.value().at(index),
222 [](
223 std::ofstream& fileStream,
224 const uint64_t word
225 ) {
226 fileStream.write(std::bit_cast<char*>(&word), 8);
227 },
228 [](const uint64_t word) { return FPGADataFormatUtilities::get_bitfields_GHITZ_w1(word).last; },
229 1,
230 m_GHITZBinOutputEvents.at(index)
231 ));
232 }
233
235
236 for (std::size_t index = 0; index < m_GHITZTxtOutputKeys.size(); index++) {
238 m_GHITZTxtOutputPaths.value().at(index),
239 [](
240 std::ofstream& fileStream,
241 const uint64_t word
242 ) {
243 fileStream << std::format("{:016x}\n", word);
244 },
245 [](const uint64_t word) { return FPGADataFormatUtilities::get_bitfields_GHITZ_w1(word).last; },
246 1,
247 m_GHITZTxtOutputEvents.at(index)
248 ));
249 }
250
252
253 for (std::size_t index = 0; index < m_GHITZBinOutputKeys.size(); index++) {
254 //coverity[ARRAY_VS_SINGLETON]
256 m_GHITZBinOutputPaths.value().at(index),
257 [](
258 std::ofstream& fileStream,
259 const uint64_t word
260 ) {
261 fileStream.write(std::bit_cast<char*>(&word), 8);
262 },
263 [](const uint64_t word) { return FPGADataFormatUtilities::get_bitfields_GHITZ_w1(word).last; },
264 1,
265 m_GHITZBinOutputEvents.at(index)
266 ));
267 }
268
270
271 for (std::size_t index = 0; index < m_CLUSTERTxtOutputKeys.size(); index++) {
273 m_CLUSTERTxtOutputPaths.value().at(index),
274 [](
275 std::ofstream& fileStream,
276 const uint64_t word
277 ) {
278 fileStream << std::format("{:016x}\n", word);
279 },
280 [](const uint64_t word) { return FPGADataFormatUtilities::get_bitfields_STRIP_CLUSTER(word).last; },
281 0,
282 m_CLUSTERTxtOutputEvents.at(index)
283 ));
284 }
285
287
288 for (std::size_t index = 0; index < m_CLUSTERBinOutputKeys.size(); index++) {
289 //coverity[ARRAY_VS_SINGLETON]
291 m_CLUSTERBinOutputPaths.value().at(index),
292 [](
293 std::ofstream& fileStream,
294 const uint64_t word
295 ) {
296 fileStream.write(std::bit_cast<char*>(&word), 8);
297 },
298 [](const uint64_t word) { return FPGADataFormatUtilities::get_bitfields_STRIP_CLUSTER(word).last; },
299 0,
300 m_CLUSTERBinOutputEvents.at(index)
301 ));
302 }
303
304 return StatusCode::SUCCESS;
305}
306
308 const std::string& path,
309 const auto& fileReadFunction,
310 const auto& endOfBlockCondition,
311 const int32_t hitCountDown,
312 std::vector<std::vector<uint64_t>>& events
313) {
315 "\nreadFile:\n" <<
316 "\tpath: " << path << "\n"
317 );
318
319 std::ifstream fileStream(path, std::ios::binary);
320 ATH_CHECK(fileStream.is_open());
321
322 FileState fileState{};
323
324 while (fileStream.peek() != EOF) {
325 const uint64_t word = fileReadFunction(fileStream);
326 DataFormatAction action = dataFormatStateMachine(
327 word,
328 endOfBlockCondition,
329 hitCountDown,
330 fileState
331 );
332
333 if (action == DataFormatAction::KEEP) {
334 events.back().push_back(word);
335 }
336 else if (action == DataFormatAction::NEW_EVENT) {
337 events.push_back({word});
338 }
339 else {
340 ATH_CHECK(action != DataFormatAction::ERROR);
341 ATH_CHECK(action == DataFormatAction::DISCARD);
342 }
343 }
344
345 ATH_CHECK(fileState.dataFormatState == FileState::DataFormatState::FOOTER);
346 ATH_CHECK(fileState.countDown == 0);
347
348 return StatusCode::SUCCESS;
349}
350
352 const std::string& path,
353 const auto& fileWriteFunction,
354 const auto& endOfBlockCondition,
355 const int32_t hitCountDown,
356 const std::vector<std::vector<uint64_t>>& events
357) {
359 "\nwriteFile:\n" <<
360 "\tpath: " << path << "\n"
361 );
362
363 std::ofstream fileStream(path, std::ios::binary);
364 ATH_CHECK(fileStream.is_open());
365 FileState fileState{};
366
367 for (const auto& event : events) {
368 bool firstWord = true;
369
370 for (const auto& word : event) {
371 DataFormatAction action = dataFormatStateMachine(
372 word,
373 endOfBlockCondition,
374 hitCountDown,
375 fileState
376 );
377
378 if (
379 action == DataFormatAction::NEW_EVENT &&
380 !firstWord
381 ) {
382 fileWriteFunction(fileStream, word);
383 break;
384 }
385 else if (
386 action == DataFormatAction::NEW_EVENT ||
387 action == DataFormatAction::KEEP
388 ) {
389 fileWriteFunction(fileStream, word);
390 }
391 else {
392 ATH_CHECK(action != DataFormatAction::ERROR);
393 ATH_CHECK(action == DataFormatAction::DISCARD);
394 }
395
396 firstWord = false;
397 }
398 }
399
400 ATH_CHECK(fileState.dataFormatState == FileState::DataFormatState::FOOTER);
401 ATH_CHECK(fileState.countDown == 0);
402
403 return StatusCode::SUCCESS;
404}
405
407 const uint64_t word,
408 const auto& endOfBlockCondition,
409 const int32_t hitCountDown,
410 FileState& fileState
411) {
413 "\ndataFormatStateMachine:\n" <<
414 "\tword: " << std::hex << word << std::dec << "\n" <<
415 "\tfileState.countDown: " << fileState.countDown << "\n" <<
416 "\tfileState.dataFormatState: " << static_cast<int>(fileState.dataFormatState)
417 );
418
419 if (fileState.countDown != 0) {
420 fileState = {
421 .countDown = fileState.countDown - 1,
422 .dataFormatState = fileState.dataFormatState,
423 };
424
425 return DataFormatAction::KEEP;
426 }
427 else if (
428 fileState.dataFormatState == FileState::DataFormatState::FOOTER &&
429 fileState.countDown == 0 &&
431 ) {
432 fileState = {
433 .countDown = 2,
434 .dataFormatState = FileState::DataFormatState::HEADER,
435 };
436
437 return DataFormatAction::NEW_EVENT;
438 }
439 else if (
440 fileState.dataFormatState == FileState::DataFormatState::FOOTER &&
441 fileState.countDown == 0
442 ) {
443 fileState = {
444 .countDown = 0,
445 .dataFormatState = FileState::DataFormatState::FOOTER,
446 };
447
448 return DataFormatAction::DISCARD;
449 }
450 else if (
451 fileState.dataFormatState == FileState::DataFormatState::HEADER &&
452 fileState.countDown == 0 &&
454 ) {
455 fileState = {
456 .countDown = 2,
457 .dataFormatState = FileState::DataFormatState::FOOTER,
458 };
459
460 return DataFormatAction::KEEP;
461 }
462 else if (
463 fileState.dataFormatState == FileState::DataFormatState::HEADER &&
464 fileState.countDown == 0 &&
466 ) {
467 fileState = {
468 .countDown = 0,
469 .dataFormatState = FileState::DataFormatState::HEADER,
470 };
471
472 return DataFormatAction::KEEP;
473 }
474 else if (
475 fileState.dataFormatState == FileState::DataFormatState::HEADER &&
476 fileState.countDown == 0 &&
478 ) {
479 fileState = {
480 .countDown = 0,
481 .dataFormatState = FileState::DataFormatState::HITS,
482 };
483
484 return DataFormatAction::KEEP;
485 }
486 else if (
487 fileState.dataFormatState == FileState::DataFormatState::HEADER &&
488 fileState.countDown == 0 &&
490 ) {
491 fileState = {
492 .countDown = 2,
493 .dataFormatState = FileState::DataFormatState::HITS,
494 };
495
496 return DataFormatAction::KEEP;
497 }
498 else if (
499 fileState.dataFormatState == FileState::DataFormatState::HEADER &&
500 fileState.countDown == 0 &&
502 ) {
503 fileState = {
504 .countDown = 1,
505 .dataFormatState = FileState::DataFormatState::HITS,
506 };
507
508 return DataFormatAction::KEEP;
509 }
510 else if (
511 fileState.dataFormatState == FileState::DataFormatState::HEADER &&
512 fileState.countDown == 0 &&
514 ) {
515 fileState = {
516 .countDown = 8,
517 .dataFormatState = FileState::DataFormatState::HITS,
518 };
519
520 return DataFormatAction::KEEP;
521 }
522 else if (
523 fileState.dataFormatState == FileState::DataFormatState::HEADER &&
524 fileState.countDown == 0 &&
526 ) {
527 fileState = {
528 .countDown = 9,
529 .dataFormatState = FileState::DataFormatState::HITS,
530 };
531
532 return DataFormatAction::KEEP;
533 }
534 else if (
535 fileState.dataFormatState == FileState::DataFormatState::HITS &&
536 fileState.countDown == 0 &&
537 endOfBlockCondition(word)
538 ) {
539 fileState = {
540 .countDown = hitCountDown,
541 .dataFormatState = FileState::DataFormatState::HEADER,
542 };
543
544 return DataFormatAction::KEEP;
545 }
546 else if (
547 fileState.dataFormatState == FileState::DataFormatState::HITS &&
548 fileState.countDown == 0
549 ) {
550 fileState = {
551 .countDown = hitCountDown,
552 .dataFormatState = FileState::DataFormatState::HITS,
553 };
554
555 return DataFormatAction::KEEP;
556 }
557
558 return DataFormatAction::ERROR;
559}
560
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_DEBUG(x)
An algorithm that can be simultaneously executed in multiple threads.
DataFormatAction dataFormatStateMachine(const uint64_t word, const auto &endOfBlockCondition, const int32_t hitCountDown, FileState &fileState)
SG::WriteHandleKeyArray< std::vector< uint64_t > > m_GHITZTxtInputKeys
EFTrackingDataStreamLoaderAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
SG::WriteHandleKeyArray< std::vector< uint64_t > > m_GHITZBinInputKeys
SG::ReadHandleKeyArray< std::vector< uint64_t > > m_CLUSTERTxtOutputKeys
std::vector< std::vector< std::vector< uint64_t > > > m_CLUSTERBinInputEvents
Gaudi::Property< std::vector< std::string > > m_GHITZBinOutputPaths
Gaudi::Property< std::vector< std::string > > m_GHITZTxtOutputPaths
SG::ReadHandleKeyArray< std::vector< uint64_t > > m_CLUSTERBinOutputKeys
SG::ReadHandleKeyArray< std::vector< uint64_t > > m_GHITZBinOutputKeys
SG::WriteHandleKeyArray< std::vector< uint64_t > > m_CLUSTERBinInputKeys
std::vector< std::vector< std::vector< uint64_t > > > m_CLUSTERTxtInputEvents
virtual StatusCode execute(const EventContext &ctx) const override final
StatusCode readFile(const std::string &path, const auto &fileReadFunction, const auto &endOfBlockCondition, const int32_t hitCountDown, std::vector< std::vector< uint64_t > > &events)
StatusCode writeFile(const std::string &path, const auto &fileWriteFunction, const auto &endOfBlockCondition, const int32_t hitCountDown, const std::vector< std::vector< uint64_t > > &events)
SG::WriteHandleKeyArray< std::vector< uint64_t > > m_CLUSTERTxtInputKeys
Gaudi::Property< std::vector< std::string > > m_GHITZTxtInputPaths
std::vector< std::vector< std::vector< uint64_t > > > m_GHITZBinInputEvents
std::vector< std::vector< std::vector< uint64_t > > > m_GHITZTxtInputEvents
Gaudi::Property< std::vector< std::string > > m_CLUSTERBinOutputPaths
Gaudi::Property< std::vector< std::string > > m_CLUSTERTxtOutputPaths
SG::ReadHandleKeyArray< std::vector< uint64_t > > m_GHITZTxtOutputKeys
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
M_HDR_w1 get_bitfields_M_HDR_w1(const uint64_t &in)
EDM_STRIPCLUSTER_w1 get_bitfields_EDM_STRIPCLUSTER_w1(const uint64_t &in)
GTRACK_HDR_w1 get_bitfields_GTRACK_HDR_w1(const uint64_t &in)
EVT_HDR_w1 get_bitfields_EVT_HDR_w1(const uint64_t &in)
GHITZ_w1 get_bitfields_GHITZ_w1(const uint64_t &in)
SLICE_HDR_w1 get_bitfields_SLICE_HDR_w1(const uint64_t &in)
EVT_FTR_w1 get_bitfields_EVT_FTR_w1(const uint64_t &in)
STRIP_CLUSTER get_bitfields_STRIP_CLUSTER(const uint64_t &in)
EDM_PIXELCLUSTER_w1 get_bitfields_EDM_PIXELCLUSTER_w1(const uint64_t &in)
RD_HDR_w1 get_bitfields_RD_HDR_w1(const uint64_t &in)
readFile(name)
Definition FileMerger.py:27
Definition index.py:1