|
| 1 | +// Copyright 2019-2020 CERN and copyright holders of ALICE O2. |
| 2 | +// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. |
| 3 | +// All rights not expressly granted are reserved. |
| 4 | +// |
| 5 | +// This software is distributed under the terms of the GNU General Public |
| 6 | +// License v3 (GPL Version 3), copied verbatim in the file "COPYING". |
| 7 | +// |
| 8 | +// In applying this license CERN does not waive the privileges and immunities |
| 9 | +// granted to it by virtue of its status as an Intergovernmental Organization |
| 10 | +// or submit itself to any jurisdiction. |
| 11 | +#define BOOST_TEST_MODULE Test EMCAL Reconstruction |
| 12 | +#define BOOST_TEST_MAIN |
| 13 | +#define BOOST_TEST_DYN_LINK |
| 14 | +#include <boost/test/unit_test.hpp> |
| 15 | +#include <vector> |
| 16 | +#include <tuple> |
| 17 | +#include <TRandom.h> |
| 18 | +#include <EMCALReconstruction/FastORTimeSeries.h> |
| 19 | + |
| 20 | +namespace o2 |
| 21 | +{ |
| 22 | + |
| 23 | +namespace emcal |
| 24 | +{ |
| 25 | + |
| 26 | +void printBunch(const gsl::span<const uint16_t> adcs) |
| 27 | +{ |
| 28 | + bool first = true; |
| 29 | + for (auto& adc : adcs) { |
| 30 | + if (!first) { |
| 31 | + std::cout << ", "; |
| 32 | + } else { |
| 33 | + first = false; |
| 34 | + } |
| 35 | + std::cout << adc; |
| 36 | + } |
| 37 | + std::cout << " (size " << adcs.size() << ")" << std::endl; |
| 38 | +} |
| 39 | + |
| 40 | +std::vector<uint16_t> getReversed(const std::vector<uint16_t>& original) |
| 41 | +{ |
| 42 | + std::vector<uint16_t> reversed(13); |
| 43 | + for (std::size_t sample = 0; sample < 13; sample++) { |
| 44 | + reversed[12 - sample] = original[sample]; |
| 45 | + } |
| 46 | + return reversed; |
| 47 | +} |
| 48 | + |
| 49 | +std::tuple<uint8_t, std::vector<uint16_t>, std::vector<uint16_t>> generatePulseTimeReversed() |
| 50 | +{ |
| 51 | + std::vector<uint16_t> pulse(13); |
| 52 | + std::fill(pulse.begin(), pulse.end(), 0); |
| 53 | + // calculate forward pulse |
| 54 | + auto peak_signal = static_cast<uint16_t>(gRandom->Uniform(0, 1024)); |
| 55 | + pulse[4] = peak_signal; |
| 56 | + auto last = peak_signal; |
| 57 | + for (std::size_t sample = 5; sample < 13; sample++) { |
| 58 | + if (last == 0) { |
| 59 | + break; |
| 60 | + } |
| 61 | + auto current = static_cast<uint16_t>(gRandom->Uniform(0, last)); |
| 62 | + pulse[sample] = current; |
| 63 | + last = current; |
| 64 | + } |
| 65 | + last = peak_signal; |
| 66 | + for (std::size_t sample = 3; sample > 0; sample--) { |
| 67 | + if (last == 0) { |
| 68 | + break; |
| 69 | + } |
| 70 | + auto current = static_cast<uint16_t>(gRandom->Uniform(0, last)); |
| 71 | + pulse[sample] = current; |
| 72 | + last = current; |
| 73 | + } |
| 74 | + // find start time |
| 75 | + uint8_t starttime = 12; |
| 76 | + for (std::size_t currenttime = 12; currenttime > 0; currenttime--) { |
| 77 | + starttime = currenttime; |
| 78 | + if (pulse[currenttime]) { |
| 79 | + break; |
| 80 | + } |
| 81 | + } |
| 82 | + // time-reverse pulse |
| 83 | + auto reversed = getReversed(pulse); |
| 84 | + // zero-suppress time series |
| 85 | + std::vector<uint16_t> zerosuppressed; |
| 86 | + bool bunchstart = false; |
| 87 | + for (std::size_t sample = 0; sample < 13; sample++) { |
| 88 | + if (reversed[sample] == 0) { |
| 89 | + if (!bunchstart) { |
| 90 | + continue; |
| 91 | + } |
| 92 | + break; |
| 93 | + } |
| 94 | + bunchstart = true; |
| 95 | + zerosuppressed.push_back(reversed[sample]); |
| 96 | + } |
| 97 | + return std::make_tuple(starttime, zerosuppressed, pulse); |
| 98 | +} |
| 99 | + |
| 100 | +uint16_t calculateTimesum(const std::vector<uint16_t> samplesOrdered, uint8_t l0time) |
| 101 | +{ |
| 102 | + uint16_t timesum = 0; |
| 103 | + uint8_t starttime = l0time - 4; |
| 104 | + for (uint8_t sample = starttime; sample < starttime + 4; sample++) { |
| 105 | + timesum += samplesOrdered[sample]; |
| 106 | + } |
| 107 | + return timesum; |
| 108 | +} |
| 109 | + |
| 110 | +std::vector<uint16_t> generateSmallBunch(uint8_t bunchlength) |
| 111 | +{ |
| 112 | + std::vector<uint16_t> bunch(bunchlength); |
| 113 | + auto peak_signal = static_cast<uint16_t>(gRandom->Uniform(0, 1024)); |
| 114 | + bunch[bunchlength - 2] = peak_signal; |
| 115 | + bunch[bunchlength - 1] = static_cast<uint16_t>(gRandom->Uniform(0, peak_signal)); |
| 116 | + auto last = peak_signal; |
| 117 | + for (int sample = bunchlength - 3; sample >= 0; sample--) { |
| 118 | + auto current = static_cast<uint16_t>(gRandom->Uniform(0, last)); |
| 119 | + bunch[sample] = current; |
| 120 | + last = current; |
| 121 | + } |
| 122 | + return bunch; |
| 123 | +} |
| 124 | + |
| 125 | +void add_bunch_to_buffer(std::vector<uint16_t>& buffer, const std::vector<uint16_t>& bunch, uint8_t starttime) |
| 126 | +{ |
| 127 | + for (int sample = 0; sample < bunch.size(); sample++) { |
| 128 | + buffer[starttime - sample] = bunch[bunch.size() - 1 - sample]; |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +BOOST_AUTO_TEST_CASE(FastORTimeSeries_test) |
| 133 | +{ |
| 134 | + // test fill and integral |
| 135 | + for (int itest = 0; itest < 500; itest++) { |
| 136 | + auto [starttime, zerosuppressed, reference] = generatePulseTimeReversed(); |
| 137 | + FastORTimeSeries testcase(13, zerosuppressed, starttime); |
| 138 | + auto adcs = testcase.getADCs(); |
| 139 | + BOOST_CHECK_EQUAL_COLLECTIONS(adcs.begin(), adcs.end(), reference.begin(), reference.end()); |
| 140 | + BOOST_CHECK_EQUAL(testcase.calculateL1TimeSum(8), calculateTimesum(reference, 8)); |
| 141 | + } |
| 142 | + return; |
| 143 | + |
| 144 | + // test adding 2 bunches |
| 145 | + for (int itest = 0; itest < 500; itest++) { |
| 146 | + auto length_bunch1 = static_cast<int>(gRandom->Uniform(3, 5)), |
| 147 | + length_bunch2 = static_cast<int>(gRandom->Uniform(3, 5)); |
| 148 | + auto sumbunchlength = length_bunch1 + length_bunch2; |
| 149 | + auto offset_bunch1 = static_cast<int>(gRandom->Uniform(0, 13 - sumbunchlength)), |
| 150 | + offset_bunch2 = static_cast<int>(gRandom->Uniform(0, 13 - sumbunchlength - offset_bunch1)); |
| 151 | + auto bunch1 = generateSmallBunch(length_bunch1), |
| 152 | + bunch2 = generateSmallBunch(length_bunch2); |
| 153 | + auto starttime_bunch1 = offset_bunch1 + length_bunch1, |
| 154 | + starttime_bunch2 = starttime_bunch1 + offset_bunch2 + length_bunch2; |
| 155 | + std::vector<uint16_t> buffer_reversed{13}; |
| 156 | + add_bunch_to_buffer(buffer_reversed, bunch2, starttime_bunch2); |
| 157 | + add_bunch_to_buffer(buffer_reversed, bunch1, starttime_bunch1); |
| 158 | + FastORTimeSeries testcase(13, bunch2, starttime_bunch2); |
| 159 | + testcase.setTimeSamples(bunch1, starttime_bunch1); |
| 160 | + auto adcs_timeordered = getReversed(buffer_reversed); |
| 161 | + auto adcs_timeseries_reversed = testcase.getADCs(); |
| 162 | + BOOST_CHECK_EQUAL_COLLECTIONS(adcs_timeseries_reversed.begin(), adcs_timeseries_reversed.end(), adcs_timeordered.begin(), adcs_timeordered.end()); |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +} // namespace emcal |
| 167 | + |
| 168 | +} // namespace o2 |
0 commit comments