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
198
201
202 auto outstreamlambda = [](
203 std::ofstream& fileStream,
204 const uint64_t word
205 ) {
206 std::array<char, 32> buffer;
207 auto result = std::format_to(buffer.begin(), "{:016x}\n", word);
208 fileStream.write(buffer.data(), std::distance(buffer.begin(), result));
209 };
210
211 for (std::size_t index = 0; index < m_GHITZTxtOutputKeys.size(); index++) {
213 m_GHITZTxtOutputPaths.value().at(index),
214 outstreamlambda,
215 [](const uint64_t word) { return FPGADataFormatUtilities::get_bitfields_GHITZ_w1(word).last; },
216 1,
217 m_GHITZTxtOutputEvents.at(index)
218 ));
219 }
220
222
223 for (std::size_t index = 0; index < m_GHITZBinOutputKeys.size(); index++) {
224 //coverity[ARRAY_VS_SINGLETON]
226 m_GHITZBinOutputPaths.value().at(index),
227 outstreamlambda,
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 outstreamlambda,
240 [](const uint64_t word) { return FPGADataFormatUtilities::get_bitfields_GHITZ_w1(word).last; },
241 1,
242 m_GHITZTxtOutputEvents.at(index)
243 ));
244 }
245
247
248 for (std::size_t index = 0; index < m_GHITZBinOutputKeys.size(); index++) {
249 //coverity[ARRAY_VS_SINGLETON]
251 m_GHITZBinOutputPaths.value().at(index),
252 [](
253 std::ofstream& fileStream,
254 const uint64_t word
255 ) {
256 fileStream.write(std::bit_cast<char*>(&word), 8);
257 },
258 [](const uint64_t word) { return FPGADataFormatUtilities::get_bitfields_GHITZ_w1(word).last; },
259 1,
260 m_GHITZBinOutputEvents.at(index)
261 ));
262 }
263
265
266 for (std::size_t index = 0; index < m_CLUSTERTxtOutputKeys.size(); index++) {
268 m_CLUSTERTxtOutputPaths.value().at(index),
269 outstreamlambda,
270 [](const uint64_t word) { return FPGADataFormatUtilities::get_bitfields_STRIP_CLUSTER(word).last; },
271 0,
272 m_CLUSTERTxtOutputEvents.at(index)
273 ));
274 }
275
277
278 for (std::size_t index = 0; index < m_CLUSTERBinOutputKeys.size(); index++) {
279 //coverity[ARRAY_VS_SINGLETON]
281 m_CLUSTERBinOutputPaths.value().at(index),
282 [](
283 std::ofstream& fileStream,
284 const uint64_t word
285 ) {
286 fileStream.write(std::bit_cast<char*>(&word), 8);
287 },
288 [](const uint64_t word) { return FPGADataFormatUtilities::get_bitfields_STRIP_CLUSTER(word).last; },
289 0,
290 m_CLUSTERBinOutputEvents.at(index)
291 ));
292 }
293
294 return StatusCode::SUCCESS;
295}
296
298 const std::string& path,
299 const auto& fileReadFunction,
300 const auto& endOfBlockCondition,
301 const int32_t hitCountDown,
302 std::vector<std::vector<uint64_t>>& events
303) {
305 "\nreadFile:\n" <<
306 "\tpath: " << path << "\n"
307 );
308
309 std::ifstream fileStream(path, std::ios::binary);
310 ATH_CHECK(fileStream.is_open());
311
312 FileState fileState{};
313
314 while (fileStream.peek() != EOF) {
315 const uint64_t word = fileReadFunction(fileStream);
316 DataFormatAction action = dataFormatStateMachine(
317 word,
318 endOfBlockCondition,
319 hitCountDown,
320 fileState
321 );
322
323 if (action == DataFormatAction::KEEP) {
324 events.back().push_back(word);
325 }
326 else if (action == DataFormatAction::NEW_EVENT) {
327 events.push_back({word});
328 }
329 else {
330 ATH_CHECK(action != DataFormatAction::ERROR);
331 ATH_CHECK(action == DataFormatAction::DISCARD);
332 }
333 }
334
335 ATH_CHECK(fileState.dataFormatState == FileState::DataFormatState::FOOTER);
336 ATH_CHECK(fileState.countDown == 0);
337
338 return StatusCode::SUCCESS;
339}
340
342 const std::string& path,
343 const auto& fileWriteFunction,
344 const auto& endOfBlockCondition,
345 const int32_t hitCountDown,
346 const std::vector<std::vector<uint64_t>>& events
347) {
349 "\nwriteFile:\n" <<
350 "\tpath: " << path << "\n"
351 );
352
353 std::ofstream fileStream(path, std::ios::binary);
354 ATH_CHECK(fileStream.is_open());
355 FileState fileState{};
356
357 for (const auto& event : events) {
358 bool firstWord = true;
359
360 for (const auto& word : event) {
361 DataFormatAction action = dataFormatStateMachine(
362 word,
363 endOfBlockCondition,
364 hitCountDown,
365 fileState
366 );
367
368 if (
369 action == DataFormatAction::NEW_EVENT &&
370 !firstWord
371 ) {
372 fileWriteFunction(fileStream, word);
373 break;
374 }
375 else if (
376 action == DataFormatAction::NEW_EVENT ||
377 action == DataFormatAction::KEEP
378 ) {
379 fileWriteFunction(fileStream, word);
380 }
381 else {
382 ATH_CHECK(action != DataFormatAction::ERROR);
383 ATH_CHECK(action == DataFormatAction::DISCARD);
384 }
385
386 firstWord = false;
387 }
388 }
389
390 ATH_CHECK(fileState.dataFormatState == FileState::DataFormatState::FOOTER);
391 ATH_CHECK(fileState.countDown == 0);
392
393 return StatusCode::SUCCESS;
394}
395
397 const uint64_t word,
398 const auto& endOfBlockCondition,
399 const int32_t hitCountDown,
400 FileState& fileState
401) {
403 "\ndataFormatStateMachine:\n" <<
404 "\tword: " << std::hex << word << std::dec << "\n" <<
405 "\tfileState.countDown: " << fileState.countDown << "\n" <<
406 "\tfileState.dataFormatState: " << static_cast<int>(fileState.dataFormatState)
407 );
408
409 if (fileState.countDown != 0) {
410 fileState = {
411 .countDown = fileState.countDown - 1,
412 .dataFormatState = fileState.dataFormatState,
413 };
414
415 return DataFormatAction::KEEP;
416 }
417 else if (
418 fileState.dataFormatState == FileState::DataFormatState::FOOTER &&
419 fileState.countDown == 0 &&
421 ) {
422 fileState = {
423 .countDown = 2,
424 .dataFormatState = FileState::DataFormatState::HEADER,
425 };
426
427 return DataFormatAction::NEW_EVENT;
428 }
429 else if (
430 fileState.dataFormatState == FileState::DataFormatState::FOOTER &&
431 fileState.countDown == 0
432 ) {
433 fileState = {
434 .countDown = 0,
435 .dataFormatState = FileState::DataFormatState::FOOTER,
436 };
437
438 return DataFormatAction::DISCARD;
439 }
440 else if (
441 fileState.dataFormatState == FileState::DataFormatState::HEADER &&
442 fileState.countDown == 0 &&
444 ) {
445 fileState = {
446 .countDown = 2,
447 .dataFormatState = FileState::DataFormatState::FOOTER,
448 };
449
450 return DataFormatAction::KEEP;
451 }
452 else if (
453 fileState.dataFormatState == FileState::DataFormatState::HEADER &&
454 fileState.countDown == 0 &&
456 ) {
457 fileState = {
458 .countDown = 0,
459 .dataFormatState = FileState::DataFormatState::HEADER,
460 };
461
462 return DataFormatAction::KEEP;
463 }
464 else if (
465 fileState.dataFormatState == FileState::DataFormatState::HEADER &&
466 fileState.countDown == 0 &&
468 ) {
469 fileState = {
470 .countDown = 0,
471 .dataFormatState = FileState::DataFormatState::HITS,
472 };
473
474 return DataFormatAction::KEEP;
475 }
476 else if (
477 fileState.dataFormatState == FileState::DataFormatState::HEADER &&
478 fileState.countDown == 0 &&
480 ) {
481 fileState = {
482 .countDown = 2,
483 .dataFormatState = FileState::DataFormatState::HITS,
484 };
485
486 return DataFormatAction::KEEP;
487 }
488 else if (
489 fileState.dataFormatState == FileState::DataFormatState::HEADER &&
490 fileState.countDown == 0 &&
492 ) {
493 fileState = {
494 .countDown = 1,
495 .dataFormatState = FileState::DataFormatState::HITS,
496 };
497
498 return DataFormatAction::KEEP;
499 }
500 else if (
501 fileState.dataFormatState == FileState::DataFormatState::HEADER &&
502 fileState.countDown == 0 &&
504 ) {
505 fileState = {
506 .countDown = 8,
507 .dataFormatState = FileState::DataFormatState::HITS,
508 };
509
510 return DataFormatAction::KEEP;
511 }
512 else if (
513 fileState.dataFormatState == FileState::DataFormatState::HEADER &&
514 fileState.countDown == 0 &&
516 ) {
517 fileState = {
518 .countDown = 9,
519 .dataFormatState = FileState::DataFormatState::HITS,
520 };
521
522 return DataFormatAction::KEEP;
523 }
524 else if (
525 fileState.dataFormatState == FileState::DataFormatState::HITS &&
526 fileState.countDown == 0 &&
527 endOfBlockCondition(word)
528 ) {
529 fileState = {
530 .countDown = hitCountDown,
531 .dataFormatState = FileState::DataFormatState::HEADER,
532 };
533
534 return DataFormatAction::KEEP;
535 }
536 else if (
537 fileState.dataFormatState == FileState::DataFormatState::HITS &&
538 fileState.countDown == 0
539 ) {
540 fileState = {
541 .countDown = hitCountDown,
542 .dataFormatState = FileState::DataFormatState::HITS,
543 };
544
545 return DataFormatAction::KEEP;
546 }
547
548 return DataFormatAction::ERROR;
549}
550
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_DEBUG(x)
virtual void lock()=0
Interface to allow an object to lock itself when made const in SG.
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