testOfflineUtils_ROOT.cc
Go to the documentation of this file.
1 
9 #include <tst/Verify.h>
10 #include <cppunit/extensions/HelperMacros.h>
11 
12 #include <io/TimeDistribution_ROOT.h>
13 #include <utl/TimeDistribution.h>
14 
15 #include <io/Trace_ROOT.h>
16 #include <io/MultiTrace_ROOT.h>
17 #include <utl/Trace.h>
18 #include <utl/MultiTrace.h>
19 #include <utl/System.h>
20 
21 #include <utl/TabulatedFunctionErrors.h>
22 #include <utl/TabulatedFunction.h>
23 #include <io/TabulatedFunctionErrors_ROOT.h>
24 #include <io/TabulatedFunction_ROOT.h>
25 
26 #include <io/MultiTabulatedFunction_ROOT.h>
27 #include <io/MultiTabulatedFunctionErrors_ROOT.h>
28 #include <utl/MultiTabulatedFunction.h>
29 #include <utl/MultiTabulatedFunctionErrors.h>
30 
31 #include <utl/Ignore.h>
32 
33 #include <TFile.h>
34 #include <TROOT.h>
35 
36 #include <iostream>
37 
38 #include <boost/tuple/tuple_io.hpp>
39 
40 using namespace std;
41 using namespace utl;
42 using namespace tst;
43 using namespace io;
44 
45 
46 class TestOfflineUtils_ROOT_io : public CppUnit::TestFixture {
47 
48  CPPUNIT_TEST_SUITE(TestOfflineUtils_ROOT_io);
49  CPPUNIT_TEST(testTimeDistributionD);
50  CPPUNIT_TEST(testTimeDistributionI);
51  CPPUNIT_TEST(testTraceD);
52  CPPUNIT_TEST(testTraceI);
53  CPPUNIT_TEST(testVector);
54  CPPUNIT_TEST(testPoint);
55  CPPUNIT_TEST(testTabulatedFunction);
56  CPPUNIT_TEST(testPhoton);
57  CPPUNIT_TEST(testMultiTabulatedFunction);
58  CPPUNIT_TEST(testMultiTrace);
59  CPPUNIT_TEST_SUITE_END();
60 
61 private:
62  template<typename T>
63  bool IsEqual(const MultiTrace<T>& traces1, const MultiTrace<T>& traces2);
64 
65  template<typename T>
66  bool IsEqual(const TimeDistribution<T>& timeDistribution1,
67  const TimeDistribution<T>& timeDistribution2);
68 
69  bool IsEqual(const TabulatedFunctionErrors& function1,
70  const TabulatedFunctionErrors& function2);
71 
72  bool IsEqual(const TabulatedFunction& function1,
73  const TabulatedFunction& function2);
74 
75  bool IsEqual(const MultiTabulatedFunction& functions1,
76  const MultiTabulatedFunction& functions2);
77 
78  template<typename T>
79  bool IsEqual(const Trace<T>& trace1, const Trace<T>& trace2);
80 
81 public:
82  void setUp()
83  {
84  fFile1 = 0;
85  fFile2 = 0;
86  fFilename = "basura.root";
87  }
88 
89  void tearDown() { }
90 
91  void testPhoton() { }
92 
93  void testVector() { }
94 
95  void testPoint() { }
96 
97  void
99  {
100  fFile1 = new TFile(fFilename.c_str(), "RECREATE");
101  CPPUNIT_ASSERT(fFile1);
102 
103  const int bins = 5;
104  double array[bins] = { 10, 5.1, 3, 2, 1 };
105  double binsize = 3;
106  double array2[bins] = { 10, 5.1, 3, 2, 1 };
107  double binsize2 = 3;
108 
109  Trace<double> trace(bins, array, binsize);
110  trace.SetStart(6);
111  trace.SetStop(16);
112  Trace<double> trace2(bins, array2, binsize2);
113  trace2.SetStart(5);
114  trace2.SetStop(15);
115 
116  MultiTraceD multiTrace;
117  multiTrace.AddTrace(trace, 0);
118  multiTrace.AddTrace(trace2, 1);
119  MultiTraceD_ROOT stream(multiTrace);
120  fFile1->WriteObject(&stream, stream.Class_Name()); // <-- stream.Write();
121 
122  fFile1->Close();
123 
124  fFile2 = new TFile(fFilename.c_str());
125  CPPUNIT_ASSERT(fFile2);
126 
127  MultiTraceD_ROOT* const stream2 =
128  (MultiTraceD_ROOT*)gROOT->FindObject("io::MultiTrace_ROOT<double>");
129  CPPUNIT_ASSERT(stream2);
130 
131  MultiTraceD multiTrace2;
132  *stream2 >> multiTrace2;
133 
134  CPPUNIT_ASSERT(IsEqual(multiTrace, multiTrace2));
135 
136  fFile2->Close();
137 
138  delete fFile1;
139  delete fFile2;
140  fFile1 = nullptr;
141  fFile2 = nullptr;
142 
143  string msg = "rm ";
144  msg.append(fFilename);
145  System(msg);
146  }
147 
148  void
150  {
151  fFile1 = new TFile(fFilename.c_str(), "RECREATE");
152  CPPUNIT_ASSERT(fFile1);
153 
154  TabulatedFunction tabulatedFunction;
155  for (int i = 0; i < 10; ++i)
156  tabulatedFunction.PushBack(i*0.5, 10.0-i);
157 
158  TabulatedFunction_ROOT tabulatedFunction_ROOT(tabulatedFunction);
159  fFile1->WriteObject(&tabulatedFunction_ROOT, tabulatedFunction_ROOT.Class_Name());
160 
161  TabulatedFunctionErrors tabulatedFunctionErrors;
162  for (int i = 0; i < 10; ++i)
163  tabulatedFunctionErrors.PushBack(i*0.5, 10.0-i, 0.01*i, 0.05*i);
164 
165  TabulatedFunctionErrors_ROOT tabulatedFunctionErrors_ROOT(tabulatedFunctionErrors);
166  fFile1->WriteObject(&tabulatedFunctionErrors_ROOT, tabulatedFunctionErrors_ROOT.Class_Name());
167 
168  fFile1->Close();
169 
170  fFile2 = new TFile(fFilename.c_str());
171  CPPUNIT_ASSERT(fFile2);
172 
173  TabulatedFunctionErrors_ROOT* const tabulatedFunctionErrors_ROOT2 =
174  (TabulatedFunctionErrors_ROOT*)gROOT->FindObject("io::TabulatedFunctionErrors_ROOT");
175  CPPUNIT_ASSERT(tabulatedFunctionErrors_ROOT2);
176  TabulatedFunctionErrors tabulatedFunctionErrors2;
177  *tabulatedFunctionErrors_ROOT2 >> tabulatedFunctionErrors2;
178  CPPUNIT_ASSERT(IsEqual(tabulatedFunctionErrors, tabulatedFunctionErrors2));
179 
180  TabulatedFunction_ROOT* const tabulatedFunction_ROOT2 =
181  (TabulatedFunction_ROOT*)gROOT->FindObject("io::TabulatedFunction_ROOT");
182  CPPUNIT_ASSERT(tabulatedFunction_ROOT2);
183  TabulatedFunction tabulatedFunction2;
184  *tabulatedFunction_ROOT2 >> tabulatedFunction2;
185  CPPUNIT_ASSERT(IsEqual(tabulatedFunction, tabulatedFunction2));
186 
187  fFile2->Close();
188 
189  delete fFile1;
190  delete fFile2;
191  fFile1 = nullptr;
192  fFile2 = nullptr;
193 
194  string msg = "rm ";
195  msg.append(fFilename);
196  System(msg);
197  }
198 
199  void
201  {
202  fFile1 = new TFile(fFilename.c_str(), "RECREATE");
203  CPPUNIT_ASSERT(fFile1);
204  TabulatedFunction tabulatedFunction1;
205  for (int i = 0; i < 10; ++i)
206  tabulatedFunction1.PushBack(i*0.5, 10.0-i);
207 
208  TabulatedFunction tabulatedFunction2;
209  for (int i = 10; i < 20; ++i)
210  tabulatedFunction2.PushBack(i*0.5, 10.0-i);
211 
212  TabulatedFunction tabulatedFunction3;
213  for (int i = 20; i < 30; ++i)
214  tabulatedFunction3.PushBack(i*0.5, 10.0-i);
215 
216  MultiTabulatedFunction multiTabulatedFunction;
217  multiTabulatedFunction.AddTabulatedFunction(tabulatedFunction1, 1);
218  multiTabulatedFunction.AddTabulatedFunction(tabulatedFunction2, 2);
219  multiTabulatedFunction.AddTabulatedFunction(tabulatedFunction3, 4);
220 
221  MultiTabulatedFunction_ROOT multiTabulatedFunction_ROOT(multiTabulatedFunction);
222  fFile1->WriteObject(&multiTabulatedFunction_ROOT, multiTabulatedFunction_ROOT.Class_Name());
223 
224  fFile1->Close();
225 
226  fFile2 = new TFile(fFilename.c_str());
227  CPPUNIT_ASSERT(fFile2);
228 
229  MultiTabulatedFunction_ROOT* const multiTabulatedFunction_ROOT2 =
230  (MultiTabulatedFunction_ROOT*)gROOT->FindObject("io::MultiTabulatedFunction_ROOT");
231  CPPUNIT_ASSERT(multiTabulatedFunction_ROOT2);
232 
233  MultiTabulatedFunction multiTabulatedFunction2;
234  *multiTabulatedFunction_ROOT2 >> multiTabulatedFunction2;
235 
236  CPPUNIT_ASSERT(IsEqual(multiTabulatedFunction2, multiTabulatedFunction));
237 
238  fFile2->Close();
239 
240  delete fFile1;
241  delete fFile2;
242  fFile1 = nullptr;
243  fFile2 = nullptr;
244 
245  string msg = "rm ";
246  msg.append(fFilename);
247  System(msg);
248  }
249 
250  void
252  {
253  fFile1 = new TFile(fFilename.c_str(), "RECREATE");
254  CPPUNIT_ASSERT(fFile1);
255 
256  const int bins = 5;
257  const double array[bins] = { 10.0, 5.1, 3, 2, 1 };
258  const double binsize = 3.0;
259 
260  TraceD trace(bins, array, binsize);
261  trace.SetStart(6);
262 
263  TraceD_ROOT stream(trace);
264  fFile1->WriteObject(&stream, stream.Class_Name());
265 
266  fFile1->Close();
267 
268  fFile2 = new TFile(fFilename.c_str());
269  CPPUNIT_ASSERT(fFile2);
270 
271  TraceD_ROOT* const stream2 =
272  (TraceD_ROOT*)gROOT->FindObject("io::Trace_ROOT<double>");
273  CPPUNIT_ASSERT(stream2);
274 
275  TraceD trace2;
276  *stream2 >> trace2;
277 
278  CPPUNIT_ASSERT(IsEqual(trace, trace2));
279 
280  fFile2->Close();
281 
282  delete fFile1;
283  delete fFile2;
284  fFile1 = nullptr;
285  fFile2 = nullptr;
286 
287  string msg = "rm ";
288  msg.append(fFilename);
289  System(msg);
290  }
291 
292  void
294  {
295  fFile1 = new TFile(fFilename.c_str(), "RECREATE");
296  CPPUNIT_ASSERT(fFile1);
297 
298  const int bins = 5;
299  const int array[bins] = { 10, 5, 3, 2, 1 };
300  const double binsize = 3.0; // the bin size is a double ???
301 
302  TraceI trace(bins, array, binsize);
303  trace.SetStart(6);
304 
305  TraceI_ROOT stream(trace);
306  fFile1->WriteObject(&stream, stream.Class_Name());
307 
308  fFile1->Close();
309 
310  fFile2 = new TFile(fFilename.c_str());
311  CPPUNIT_ASSERT(fFile2);
312 
313  TraceI_ROOT* const stream2 =
314  (TraceI_ROOT*)gROOT->FindObject("io::Trace_ROOT<int>");
315  CPPUNIT_ASSERT(stream2);
316 
317  TraceI trace2;
318  *stream2 >> trace2;
319 
320  CPPUNIT_ASSERT(IsEqual(trace, trace2));
321 
322  fFile2->Close();
323 
324  delete fFile1;
325  delete fFile2;
326  fFile1 = nullptr;
327  fFile2 = nullptr;
328 
329  string msg = "rm ";
330  msg.append(fFilename);
331  System(msg);
332  }
333 
334  void
336  {
337  fFile1 = new TFile(fFilename.c_str(), "RECREATE");
338  CPPUNIT_ASSERT(fFile1);
339 
340  fFile1->Close();
341 
342  fFile2 = new TFile(fFilename.c_str());
343  CPPUNIT_ASSERT(fFile2);
344 
345  fFile2->Close();
346 
347  delete fFile1;
348  delete fFile2;
349  fFile1 = nullptr;
350  fFile2 = nullptr;
351 
352  string msg = "rm ";
353  msg.append(fFilename);
354  System(msg);
355  }
356 
357  void
359  {
360  const double binning = 2;
361  TimeDistributionD* const timeDistribution = new TimeDistributionD(binning);
362 
363  timeDistribution->AddTime(11, 5);
364  timeDistribution->AddTime(5, 3);
365 
366  TFile* const eventFile = new TFile("basura.root", "RECREATE");
367  CPPUNIT_ASSERT(eventFile);
368  TimeDistributionD_ROOT* const streamed = new TimeDistributionD_ROOT(*timeDistribution);
369  CPPUNIT_ASSERT(streamed);
370 
371  eventFile->WriteObject(streamed, streamed->Class_Name());
372  eventFile->Close();
373  delete eventFile;
374 
375  TFile* const eventFile2 = new TFile("basura.root");
376  CPPUNIT_ASSERT(eventFile2);
377  TimeDistributionD_ROOT* const streamed2 =
378  (TimeDistributionD_ROOT*)gROOT->FindObject("io::TimeDistribution_ROOT<double>");
379  CPPUNIT_ASSERT(streamed2);
380 
381  TimeDistributionD* const timeDistribution2 = new TimeDistributionD(2);
382 
383  *streamed2 >> *timeDistribution2;
384 
385  CPPUNIT_ASSERT(IsEqual(*timeDistribution, *timeDistribution2));
386 
387  System("rm basura.root");
388  }
389 
390 private:
391  string fFilename;
392  TFile* fFile1;
393  TFile* fFile2;
394 
395 };
396 
397 
398 bool
400  const TabulatedFunction& function2)
401 {
402  for (ConstTabulatedFunctionIterator it = function2.Begin();
403  it != function2.End(); ++it)
404  if (function1.FindX(it->X())->Y() != it->Y())
405  return false;
406  return true;
407 }
408 
409 
410 bool
412  const TabulatedFunctionErrors& function2)
413 {
414  for (ConstTabulatedFunctionErrIterator it = function2.Begin();
415  it != function2.End(); ++it)
416  if (function1.FindX(it->X())->Y() != it->Y())
417  return false;
418  return true;
419 }
420 
421 
422 bool
424  const MultiTabulatedFunction& functions2)
425 {
426  CPPUNIT_ASSERT(Verify<Equal>(functions1.GetNLabels(), functions2.GetNLabels()));
427 
428  for (MultiTabulatedFunction::ConstIterator it = functions1.TabulatedFunctionsBegin();
429  it != functions1.TabulatedFunctionsEnd(); ++it)
430  if (!IsEqual(it->GetTabulatedFunction(), functions2.GetTabulatedFunction(it->GetLabel())))
431  return false;
432 
433  return true;
434 }
435 
436 
437 template<typename T>
438 bool
440  const TimeDistribution<T>& timeDistribution2)
441 {
442  for (typename TimeDistribution<T>::SparseIterator it = timeDistribution2.SparseBegin();
443  it != timeDistribution2.SparseEnd(); ++it)
444  if (timeDistribution1.At(it->template get<0>()) != it->template get<1>())
445  return false;
446 
447  for (typename TimeDistribution<T>::SparseIterator it = timeDistribution1.SparseBegin();
448  it != timeDistribution1.SparseEnd(); ++it)
449  if (timeDistribution2[it->template get<0>()] != it->template get<1>())
450  return false;
451 
452  return true;
453 }
454 
455 
456 template<typename T>
457 bool
459  const Trace<T>& trace2)
460 {
461  if (trace1.GetBinning() != trace2.GetBinning() ||
462  trace1.GetStart() != trace2.GetStart() ||
463  trace1.GetStop() != trace2.GetStop() ||
464  trace1.GetSize() != trace2.GetSize())
465  return false;
466 
467  for (int i = 0; i < int(trace1.GetSize()); ++i)
468  if (trace1[i] != trace2[i])
469  return false;
470 
471  return true;
472 }
473 
474 
475 template<typename T>
476 bool
478  const MultiTrace<T>& traces2)
479 {
480  CPPUNIT_ASSERT(Verify<Equal>(traces1.GetNLabels(), traces2.GetNLabels()));
481 
482  for (typename MultiTrace<T>::ConstIterator it = traces1.Begin();
483  it != traces1.End(); ++it)
484  if (!IsEqual(it->GetTrace(), traces2.GetTrace(it->GetLabel())))
485  return false;
486 
487  return true;
488 }
489 
490 
struct Station * array
T At(const double time) const
void SetStop(const SizeType stop)
Set valid data stop bin.
Definition: Trace.h:151
ROOT streamer implementation for utl::MultiTabulatedFunction.
TimeDistribution< double > TimeDistributionD
void AddTrace(const int size, const double binSize, const int label)
Definition: MultiTrace.h:76
void System(const char *const command, const bool throwOnError, const bool notify)
Definition: System.cc:10
SizeType GetStop() const
Get valid data stop bin.
Definition: Trace.h:148
Class to hold collection (x,y) points and provide interpolation between them.
ConstIterator FindX(const double x) const
ROOT streamer for TimeDistributionD.
double GetBinning() const
size of one slot
Definition: Trace.h:138
Histogram class for time distributions with suppressed empty bins.
void AddTabulatedFunction(const int label)
void PushBack(const double x, const double y)
bool IsEqual(const MultiTrace< T > &traces1, const MultiTrace< T > &traces2)
CPPUNIT_TEST_SUITE_REGISTRATION(testAiresShowerFile)
ROOT streamer implementation for class TabulatedFunction.
TabulatedFunction & GetTabulatedFunction(const int label=0)
Returns the TabulatedFunction for /par source, throws an exception if na.
void SetStart(const SizeType start)
Set valid data start bin.
Definition: Trace.h:145
SizeType GetSize() const
Definition: Trace.h:156
void PushBack(const double x, const double xErr, const double y, const double yErr)
SparseIterator SparseEnd() const
ROOT streamer implementation for class TabulatedFunctionErrors.
double Y() const
Definition: PairErr.h:26
Template class for a FADC data or calibrated data container. Use the typedefs (TraceD, TraceI, etc.) defined in Trace-fwd.h.
Definition: Trace-fwd.h:19
SizeType GetStart() const
Get valid data start bin.
Definition: Trace.h:142
Trace< T > & GetTrace(const int label=0)
Returns the trace for /par source.
Definition: MultiTrace.h:70
A collection of TabulatedFunction.
ConstIteratorErr FindX(const double x) const
static int binsize
Definition: XbAlgo.cc:25
unsigned int GetNLabels() const
Definition: MultiObject.h:89
double Y() const
Definition: Pair.h:32
A collection of Trace&lt;T&gt;,which provides methods to access different sources.
Definition: MultiTrace.h:64
SparseIterator SparseBegin() const
Iterator over time slots with data in them (skips empty slots).
TimeDistribution_ROOT< double > TimeDistributionD_ROOT
void AddTime(const double time, const T weight=T(1))
Add an entry (optionally weighted) for the given time. Slot will be computed.
boost::transform_iterator< InternalMapFunctor, InternalConstIterator, Tuple > SparseIterator

, generated on Tue Sep 26 2023.