|
27 | 27 | #include <iostream> |
28 | 28 | #include <memory> |
29 | 29 | #include <string> |
| 30 | +#include <stdexcept> |
30 | 31 |
|
31 | 32 | using namespace o2::framework; |
32 | 33 |
|
@@ -63,61 +64,64 @@ class DigitSamplerTask : public io::DigitIOBaseTask |
63 | 64 |
|
64 | 65 | void outputAndClear(DataAllocator& out) |
65 | 66 | { |
66 | | - printSummary(mDigits, mROFs, "-> to output"); |
| 67 | + LOGP(info, "Sending {} rofs with {} digits", mROFs.size(), mDigits.size()); |
67 | 68 | out.snapshot(OutputRef{"rofs"}, mROFs); |
68 | 69 | out.snapshot(OutputRef{"digits"}, mDigits); |
69 | 70 | mDigits.clear(); |
70 | 71 | mROFs.clear(); |
71 | 72 | } |
72 | 73 |
|
73 | | - bool shouldEnd() const |
| 74 | + bool shouldEnd() |
74 | 75 | { |
75 | 76 | bool maxTFreached = mNofProcessedTFs >= mMaxNofTimeFrames; |
76 | 77 | bool maxROFreached = mNofProcessedROFs >= mMaxNofROFs; |
77 | | - return !mReadIsOk || maxTFreached || maxROFreached; |
| 78 | + bool lastTF = mInput.peek() == EOF; |
| 79 | + return !mReadIsOk || lastTF || maxTFreached || maxROFreached; |
78 | 80 | } |
79 | 81 |
|
80 | 82 | void run(ProcessingContext& pc) |
81 | 83 | { |
82 | 84 | if (shouldEnd()) { |
83 | | - // output remaining data if any |
84 | | - if (mROFs.size() > 0) { |
85 | | - --mTFid; |
86 | | - outputAndClear(pc.outputs()); |
87 | | - } |
88 | | - pc.services().get<ControlService>().endOfStream(); |
89 | | - return; |
| 85 | + throw std::invalid_argument("process should have ended already"); |
90 | 86 | } |
91 | 87 |
|
92 | 88 | std::vector<ROFRecord> rofs; |
93 | 89 | std::vector<Digit> digits; |
94 | | - mReadIsOk = mDigitSampler->read(digits, rofs); |
95 | | - if (!mReadIsOk) { |
96 | | - return; |
97 | | - } |
| 90 | + while ((mReadIsOk = mDigitSampler->read(digits, rofs))) { |
| 91 | + |
| 92 | + // process the current input TF if requested |
| 93 | + if (shouldProcess()) { |
| 94 | + incNofProcessedTFs(); |
| 95 | + mNofProcessedROFs += rofs.size(); |
| 96 | + // append rofs to mROFs, but shift the indices by the amount of digits |
| 97 | + // we have read so far. |
| 98 | + auto offset = mDigits.size(); |
| 99 | + std::transform(rofs.begin(), rofs.end(), std::back_inserter(mROFs), |
| 100 | + [offset](ROFRecord r) { |
| 101 | + r.setDataRef(r.getFirstIdx() + offset, r.getNEntries()); |
| 102 | + return r; |
| 103 | + }); |
| 104 | + mDigits.insert(mDigits.end(), digits.begin(), digits.end()); |
| 105 | + printSummary(mDigits, mROFs); |
| 106 | + printFull(mDigits, mROFs); |
| 107 | + } |
98 | 108 |
|
99 | | - if (shouldProcess()) { |
100 | | - incNofProcessedTFs(); |
101 | | - mNofProcessedROFs += rofs.size(); |
102 | | - // append rofs to mROFs, but shift the indices by the amount of digits |
103 | | - // we have read so far. |
104 | | - auto offset = mDigits.size(); |
105 | | - std::transform(rofs.begin(), rofs.end(), std::back_inserter(mROFs), |
106 | | - [offset](ROFRecord r) { |
107 | | - r.setDataRef(r.getFirstIdx() + offset, r.getNEntries()); |
108 | | - return r; |
109 | | - }); |
110 | | - mDigits.insert(mDigits.end(), digits.begin(), digits.end()); |
111 | | - printSummary(mDigits, mROFs); |
112 | | - printFull(mDigits, mROFs); |
113 | | - } |
| 109 | + // increment the input TF id for the next one |
| 110 | + incTFid(); |
114 | 111 |
|
115 | | - // output if we've accumulated enough ROFs |
116 | | - if (mROFs.size() >= mMinNumberOfROFsPerTF) { |
117 | | - outputAndClear(pc.outputs()); |
| 112 | + // stop here if we've accumulated enough ROFs or TFs |
| 113 | + if (mROFs.size() >= mMinNumberOfROFsPerTF || shouldEnd()) { |
| 114 | + break; |
| 115 | + } |
118 | 116 | } |
119 | 117 |
|
120 | | - incTFid(); |
| 118 | + // output whatever has been accumulated, even if empty |
| 119 | + outputAndClear(pc.outputs()); |
| 120 | + |
| 121 | + if (shouldEnd()) { |
| 122 | + pc.services().get<ControlService>().endOfStream(); |
| 123 | + pc.services().get<ControlService>().readyToQuit(QuitRequest::Me); |
| 124 | + } |
121 | 125 | } |
122 | 126 | }; |
123 | 127 |
|
|
0 commit comments