Project
Loading...
Searching...
No Matches
testTreeStream.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 TreeStream
13#define BOOST_TEST_MAIN
14#define BOOST_TEST_DYN_LINK
15
16#include <TFile.h>
17#include <TRandom.h>
18#include <TVectorD.h>
19#include <boost/test/unit_test.hpp>
20#include <iostream>
25#include <string>
26
27using namespace o2::utils;
28
29bool UnitTestSparse(Double_t scale, Int_t testEntries);
30
31BOOST_AUTO_TEST_CASE(TreeStream_test)
32{
33 // Example test function to show functionality of TreeStreamRedirector
34
35 // create the redirector associated with file (testredirector.root)
36
37 LOG(info) << "Testing TreeStream creation";
38 std::string outFName("testTreeStream.root");
39 int nit = 50;
40 {
41 TreeStreamRedirector tstStream(outFName.data(), "recreate");
42 // write tree named TrackTree of int counter, float and TrackParCov (using pointer)
43 // and another similar tree but using reference of TrackParCov
44 std::array<float, o2::track::kNParams> par{};
45 for (int i = 0; i < nit; i++) {
46 par[o2::track::kQ2Pt] = 0.5 + float(i) / nit;
47 float x = 10. + float(i) / nit * 200.;
48 o2::track::TrackPar trc(0., 0., par);
49 trc.propagateParamTo(x, 0.5);
50 tstStream << "TrackTree"
51 << "id=" << i << "x=" << x << "track=" << &trc << "\n";
52
53 tstStream << "TrackTreeR"
54 << "id=" << i << "x=" << x << "track=" << trc << "\n";
55 }
56 // on destruction of tstTreem the trees will be stored, but we can also force it by
57 tstStream.Close();
58 }
59 //
60 LOG(info) << "Testing reading back tree maid by the TreeStream ";
61 // read back tracks
62 {
63 TFile inpf(outFName.data());
64 BOOST_CHECK(!inpf.IsZombie());
65 auto tree = (TTree*)inpf.GetObjectChecked("TrackTree", "TTree");
67 int nent = tree->GetEntries();
68 BOOST_CHECK(nent == nit);
69 int id;
70 float x;
71 o2::track::TrackPar* trc = nullptr;
72 BOOST_CHECK(!tree->SetBranchAddress("id", &id));
73 BOOST_CHECK(!tree->SetBranchAddress("x", &x));
74 BOOST_CHECK(!tree->SetBranchAddress("track", &trc));
75
76 for (int i = 0; i < nent; i++) {
77 tree->GetEntry(i);
78 BOOST_CHECK(id == i);
79 LOG(info) << "id: " << id << " X: " << x << " Track> ";
80 trc->printParam();
81 BOOST_CHECK(std::abs(x - trc->getX()) < 1e-4);
82 }
83 }
84
85 LOG(info) << "Testing loading tree via RootChain";
86 //
87 auto chain = RootChain::load("TrackTree", outFName);
88 BOOST_CHECK(chain->GetEntries());
89 chain->Print();
90
91 // we can also write the stream to external file open in write mode:
92 {
93 TFile inpf(outFName.data(), "update");
94 TreeStream strm("TreeNamed");
95 for (int i = 0; i < nit; i++) {
96 TNamed nm(Form("obj%d", i), "");
97 strm << "idx=" << i << "named=" << &nm << "\n";
98 }
99 strm.Close(); // flush the tree
100 }
101
102 // run Marian's old unit test
103 LOG(info) << "Doing UnitTestSparse";
104 nit = 1000;
105 BOOST_CHECK(UnitTestSparse(0.5, nit));
106 BOOST_CHECK(UnitTestSparse(0.1, nit));
107 //
108}
109
110//_________________________________________________
111bool UnitTestSparse(Double_t scale, Int_t testEntries)
112{
113 // Unit test for the TreeStreamRedirector
114 // 1.) Test TTreeRedirector
115 // a.) Fill tree with random vectors
116 // b.) Fill downscaled version of vectors
117 // c.) The same skipping first entry
118 // 2.) Check results wtitten to terminale
119 // a.) Disk consumption
120 // skip data should be scale time smaller than full
121 // zerro replaced ata should be compresed time smaller than full
122 // b.) Test invariants
123 // Input parameter scale => downscaling of sprse element
124
125 std::string outFName("testTreeStreamSparse.root");
126 if (scale <= 0) {
127 scale = 1;
128 }
129 if (scale > 1) {
130 scale = 1;
131 }
132 TreeStreamRedirector* pcstream = new TreeStreamRedirector(outFName.data(), "recreate");
133 for (Int_t ientry = 0; ientry < testEntries; ientry++) {
134 TVectorD vecRandom(200);
135 TVectorD vecZerro(200); // zerro vector
136 for (Int_t j = 0; j < 200; j++) {
137 vecRandom[j] = j + ientry + 0.1 * gRandom->Rndm();
138 }
139 Bool_t isSelected = (gRandom->Rndm() < scale);
140 TVectorD* pvecFull = &vecRandom;
141 TVectorD* pvecSparse = isSelected ? &vecRandom : nullptr;
142 TVectorD* pvecSparse0 = isSelected ? &vecRandom : nullptr;
143 TVectorD* pvecSparse1 = isSelected ? &vecRandom : &vecZerro;
144
145 if (ientry == 0) {
146 pvecSparse0 = nullptr;
147 pvecSparse = &vecRandom;
148 }
149 (*pcstream) << "Full" << // stored all vectors
150 "ientry=" << ientry << "vec.=" << pvecFull << "\n";
151 (*pcstream) << "SparseSkip" << // fraction of vectors stored
152 "ientry=" << ientry << "vec.=" << pvecSparse << "\n";
153 (*pcstream) << "SparseSkip0" << // fraction with -pointer
154 "ientry=" << ientry << "vec.=" << pvecSparse0 << "\n";
155 (*pcstream) << "SparseZerro" << // all vectors filled, franction filled with 0
156 "ientry=" << ientry << "vec.=" << pvecSparse1 << "\n";
157 }
158 delete pcstream;
159 //
160 // 2.) check results
161 //
162
163 TFile* f = TFile::Open(outFName.data());
164 if (!f) {
165 printf("Failed to open file: %s\n", outFName.data());
166 return false;
167 }
168 TTree* treeFull = (TTree*)f->Get("Full");
169 TTree* treeSparseSkip = (TTree*)f->Get("SparseSkip");
170 TTree* treeSparseSkip0 = (TTree*)f->Get("SparseSkip0");
171 TTree* treeSparseZerro = (TTree*)f->Get("SparseZerro");
172 // a.) data volume
173 //
174 Double_t ratio = (1. / scale) * treeSparseSkip->GetZipBytes() / Double_t(treeFull->GetZipBytes());
175 Double_t ratio0 = (1. / scale) * treeSparseSkip0->GetZipBytes() / Double_t(treeFull->GetZipBytes());
176 Double_t ratio1 = (1. / scale) * treeSparseZerro->GetZipBytes() / Double_t(treeFull->GetZipBytes());
177 printf("#UnitTest:\tTestSparse(%f)\tRatioSkip\t%f\n", scale, ratio);
178 printf("#UnitTest:\tTestSparse(%f)\tRatioSkip0\t%f\n", scale, ratio0);
179 printf("#UnitTest:\tTestSparse(%f)\tRatioZerro\t%f\n", scale, ratio1);
180 // b.) Integrity
181 Int_t outlyersSparseSkip = treeSparseSkip->Draw("1", "(vec.fElements-ientry-Iteration$-0.5)>0.5", "goff");
182 Int_t outlyersSparseSkip0 = treeSparseSkip0->Draw("1", "(vec.fElements-ientry-Iteration$-0.5)>0.5", "goff");
183 printf("#UnitTest:\tTestSparse(%f)\tOutlyersSkip\t%d\n", scale, outlyersSparseSkip != 0);
184 printf("#UnitTest:\tTestSparse(%f)\tOutlyersSkip0\t%d\n", scale, outlyersSparseSkip0 != 0);
185 // c.) Number of entries
186 //
187 Int_t entries = treeFull->GetEntries();
188 Int_t entries0 = treeSparseSkip0->GetEntries();
189 Bool_t isOKStat = (entries == entries0);
190 printf("#UnitTest:\tTestSparse(%f)\tEntries\t%d\n", scale, isOKStat);
191 //
192 // d.)Reading test
193 TVectorD* pvecRead = nullptr;
194 treeSparseSkip0->SetBranchAddress("vec.", &pvecRead);
195 Bool_t readOK = kTRUE;
196 for (Int_t ientry = 0; ientry < testEntries; ientry++) {
197 if (!pvecRead) {
198 continue;
199 }
200 if (pvecRead->GetNrows() == 0) {
201 continue;
202 }
203 if (TMath::Abs((*pvecRead)[0] - ientry) > 0.5) {
204 readOK = kFALSE;
205 }
206 }
207 printf("#UnitTest:\tTestSparse(%f)\tReadOK\t%d\n", scale, readOK);
208 //
209 // e.)Global test
210 Bool_t isOK = (outlyersSparseSkip0 == 0) && isOKStat && readOK;
211 printf("#UnitTest:\tTestSparse(%f)\tisOk\t%d\n", scale, isOK);
212
213 return isOK;
214}
Base track model for the Barrel, params only, w/o covariance.
int32_t i
GPUChain * chain
uint32_t j
Definition RawData.h:0
static std::unique_ptr< TChain > load(const std::string trName, const std::string inpFile)
Definition RootChain.cxx:21
GLint GLenum GLint x
Definition glcorearb.h:403
GLdouble f
Definition glcorearb.h:310
GLuint id
Definition glcorearb.h:650
BOOST_AUTO_TEST_CASE(TreeStream_test)
bool UnitTestSparse(Double_t scale, Int_t testEntries)
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"
BOOST_CHECK(tree)
std::unique_ptr< TTree > tree((TTree *) flIn.Get(std::string(o2::base::NameConf::CTFTREENAME).c_str()))