Project
Loading...
Searching...
No Matches
test_ctf_io_zdc.cxx
Go to the documentation of this file.
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
12#define BOOST_TEST_MODULE Test ZDCCTFIO
13#define BOOST_TEST_MAIN
14#define BOOST_TEST_DYN_LINK
15
16#undef NDEBUG
17#include <cassert>
18
19#include <boost/test/unit_test.hpp>
20#include <boost/test/data/test_case.hpp>
21#include <boost/test/data/dataset.hpp>
24#include "DataFormatsZDC/CTF.h"
25#include "Framework/Logger.h"
26#include <TFile.h>
27#include <TRandom.h>
28#include <TStopwatch.h>
29#include <TSystem.h>
30#include <cstring>
31
32using namespace o2::zdc;
33namespace boost_data = boost::unit_test::data;
34
35inline std::vector<o2::ctf::ANSHeader> ANSVersions{o2::ctf::ANSVersionCompat, o2::ctf::ANSVersion1};
36
37BOOST_DATA_TEST_CASE(CTFTest, boost_data::make(ANSVersions), ansVersion)
38{
39 std::vector<BCData> bcdata;
40 std::vector<ChannelData> chandata;
41 std::vector<OrbitData> pedsdata;
42 // RS: don't understand why, but this library is not loaded automatically, although the dependencies are clearly
43 // indicated. What it more weird is that for similar tests of other detectors the library is loaded!
44 // Absence of the library leads to complains about the StreamerInfo and eventually segm.faul when appending the
45 // CTH to the tree. For this reason I am loading it here manually
46 // gSystem->Load("libO2DetectorsCommonDataFormats");
47 TStopwatch sw;
48 sw.Start();
50 std::array<float, NTimeBinsPerBC> chanVals;
51
52 // BCData and ChannelData
53 for (int irof = 0; irof < 1000; irof++) {
54 ir += 1 + gRandom->Integer(100); // randomly increaing BC
55
56 uint32_t channPatt = 0, triggers = 0;
57 int8_t ich = -1;
58 int firstChEntry = chandata.size();
59 while ((ich += 1 + gRandom->Poisson(2.)) < NDigiChannels) {
60 channPatt |= 0x1 << ich;
61 for (int i = 0; i < NTimeBinsPerBC; i++) {
62 chanVals[i] = gRandom->Integer(0xffff);
63 }
64 if (gRandom->Rndm() > 0.4) {
65 triggers |= 0x1 << ich;
66 }
67 chandata.emplace_back(ich, chanVals);
68 }
69 auto& bcd = bcdata.emplace_back(firstChEntry, chandata.size() - firstChEntry, ir, channPatt, triggers, gRandom->Integer(0xff));
70 for (int im = 0; im < NModules; im++) {
71 bcd.moduleTriggers[im] = gRandom->Rndm() > 0.7 ? gRandom->Integer((0x1 << 10) - 1) : 0;
72 }
73 }
74
75 // OrbitData
76 const auto &irFirst = bcdata.front().ir, irLast = bcdata.back().ir;
78 int norbits = irLast.orbit - irFirst.orbit + 1;
80 for (int i = 0; i < norbits; i++) {
81 pedsdata[i].ir = irPed;
82 for (int ic = 0; ic < NChannels; ic++) {
83 pedsdata[i].data[ic] = gRandom->Integer(0xffff);
84 pedsdata[i].scaler[ic] = (ic > 0 ? pedsdata[i].scaler[ic - 1] : 0) + gRandom->Integer(20);
85 }
86 irPed.orbit++;
87 }
88
89 sw.Start();
90 std::vector<o2::ctf::BufferType> vec;
91 {
93 coder.setANSVersion(ansVersion);
94 coder.encode(vec, bcdata, chandata, pedsdata); // compress
95 }
96 sw.Stop();
97 LOG(info) << "Compressed in " << sw.CpuTime() << " s";
98
99 // writing
100 {
101 sw.Start();
103 TFile flOut("test_ctf_zdc.root", "recreate");
104 TTree ctfTree(std::string(o2::base::NameConf::CTFTREENAME).c_str(), "O2 CTF tree");
105 ctfImage->print();
106 ctfImage->appendToTree(ctfTree, "ZDC");
107 ctfTree.Write();
108 sw.Stop();
109 LOG(info) << "Wrote to tree in " << sw.CpuTime() << " s";
110 }
111
112 // reading
113 vec.clear();
114 {
115 sw.Start();
116 TFile flIn("test_ctf_zdc.root");
117 std::unique_ptr<TTree> tree((TTree*)flIn.Get(std::string(o2::base::NameConf::CTFTREENAME).c_str()));
119 o2::zdc::CTF::readFromTree(vec, *(tree.get()), "ZDC");
120 sw.Stop();
121 LOG(info) << "Read back from tree in " << sw.CpuTime() << " s";
122 }
123
124 std::vector<BCData> bcdataD;
125 std::vector<ChannelData> chandataD;
126 std::vector<OrbitData> pedsdataD;
127
128 sw.Start();
129 const auto ctfImage = o2::zdc::CTF::getImage(vec.data());
130 {
132 coder.decode(ctfImage, bcdataD, chandataD, pedsdataD); // decompress
133 }
134 sw.Stop();
135 LOG(info) << "Decompressed in " << sw.CpuTime() << " s";
136
137 LOG(info) << "Testing BCData: BOOST_CHECK bcdataD.size() " << bcdataD.size() << " bcdata.size() " << bcdata.size();
138 BOOST_CHECK(bcdataD.size() == bcdata.size());
139 for (size_t i = 0; i < bcdata.size(); i++) {
140 bool cmpBCData = (bcdata[i].ir == bcdataD[i].ir &&
141 bcdata[i].ref == bcdataD[i].ref &&
142 bcdata[i].moduleTriggers == bcdataD[i].moduleTriggers &&
143 bcdata[i].channels == bcdataD[i].channels &&
144 bcdata[i].triggers == bcdataD[i].triggers &&
145 bcdata[i].ext_triggers == bcdataD[i].ext_triggers);
146
147 if (!cmpBCData) {
148 LOG(error) << "Mismatch in BC data " << i;
149 bcdata[i].print();
150 bcdataD[i].print();
151 }
152 BOOST_CHECK(cmpBCData);
153 }
154
155 LOG(info) << "Testing ChannelData: BOOST_CHECK(chandataD.size() " << chandataD.size() << " chandata.size()) " << chandata.size();
156 BOOST_CHECK(chandataD.size() == chandata.size());
157
158 for (size_t i = 0; i < chandata.size(); i++) {
159 bool cmpChData = chandata[i].id == chandataD[i].id && chandata[i].data == chandataD[i].data;
160 if (!cmpChData) {
161 LOG(error) << "Mismatch in ChannelData " << i;
162 chandata[i].print();
163 chandataD[i].print();
164 }
165 BOOST_CHECK(cmpChData);
166 }
167
168 LOG(info) << "Testing OrbitData: BOOST_CHECK(pedsdataD.size() " << pedsdataD.size() << " pedsdata.size()) " << pedsdata.size();
169 BOOST_CHECK(pedsdataD.size() == pedsdata.size());
170 for (size_t i = 0; i < pedsdata.size(); i++) {
171 bool cmpPdData = pedsdata[i].ir == pedsdataD[i].ir && pedsdata[i].data == pedsdataD[i].data && pedsdata[i].scaler == pedsdataD[i].scaler;
172 if (!cmpPdData) {
173 LOG(error) << "Mismatch in OrbitData " << i;
174 pedsdata[i].print();
175 pedsdataD[i].print();
176 }
177 BOOST_CHECK(cmpPdData);
178 }
179}
int32_t i
BOOST_DATA_TEST_CASE(DefaultConstructorNofSamplesIsInvariant, boost::unit_test::data::make(nsamples), nofSamples)
Definition testDigit.cxx:50
Definition of the Names Generator class.
Definitions for ZDC CTF data.
class for entropy encoding/decoding of ZDC data
static constexpr std::string_view CTFTREENAME
Definition NameConf.h:95
void setANSVersion(const ctf::ANSHeader &ansVersion) noexcept
void readFromTree(TTree &tree, const std::string &name, int ev=0)
read from tree to non-flat object
static auto get(void *head)
cast arbitrary buffer head to container class. Head is supposed to respect the alignment
static auto getImage(const void *newHead)
get const image of the container wrapper, with pointers in the image relocated to new head
o2::ctf::CTFIOSize decode(const CTF::base &ec, VTRG &trigVec, VCHAN &chanVec, VPED &pedVec)
entropy decode data from buffer with CTF
Definition CTFCoder.h:157
o2::ctf::CTFIOSize encode(VEC &buff, const gsl::span< const BCData > &trgData, const gsl::span< const ChannelData > &chanData, const gsl::span< const OrbitData > &pedData)
entropy-encode data to buffer with CTF
Definition CTFCoder.h:67
constexpr int LHCMaxBunches
constexpr ANSHeader ANSVersionCompat
Definition ANSHeader.h:54
constexpr ANSHeader ANSVersion1
Definition ANSHeader.h:55
constexpr int NModules
Definition Constants.h:68
constexpr int NTimeBinsPerBC
Definition Constants.h:53
constexpr int NChannels
Definition Constants.h:65
constexpr int NDigiChannels
Definition Constants.h:71
uint32_t orbit
LHC orbit.
std::vector< OrbitData > pedsdataD
std::vector< BCData > bcdataD
std::vector< o2::ctf::BufferType > vec
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"
TFile flOut("test_ctf_zdc.root", "recreate")
BOOST_CHECK(tree)
std::vector< o2::ctf::ANSHeader > ANSVersions
o2::InteractionRecord irPed(o2::constants::lhc::LHCMaxBunches - 1, irFirst.orbit)
const auto irLast
o2::InteractionRecord ir(0, 0)
std::vector< ChannelData > chandataD
TStopwatch sw
std::array< float, NTimeBinsPerBC > chanVals
TTree ctfTree(std::string(o2::base::NameConf::CTFTREENAME).c_str(), "O2 CTF tree")
std::vector< ChannelData > chandata
auto * ctfImage
const auto & irFirst
std::vector< OrbitData > pedsdata
TFile flIn("test_ctf_zdc.root")
int norbits
std::unique_ptr< TTree > tree((TTree *) flIn.Get(std::string(o2::base::NameConf::CTFTREENAME).c_str()))