Logo ROOT   6.10/09
Reference Guide
array.cxx
Go to the documentation of this file.
1 #include "TFile.h"
2 #include "TTree.h"
3 #include "TTreeReader.h"
4 #include "TTreeReaderArray.h"
5 
6 #include "gtest/gtest.h"
7 
8 #include <fstream>
9 
11  TTree* tree = new TTree("TTreeReaderArrayTree", "In-memory test tree");
12  std::vector<float> vecf{17.f, 18.f, 19.f, 20.f, 21.f};
13  tree->Branch("vec", &vecf);
14 
15  tree->Fill();
16  tree->Fill();
17  tree->Fill();
18 
19  tree->ResetBranchAddresses();
20 
21  TTreeReader tr(tree);
22  TTreeReaderArray<float> vec(tr, "vec");
23 
24  tr.SetEntry(1);
25 
26  EXPECT_EQ(5u, vec.GetSize());
27  EXPECT_FLOAT_EQ(19.f, vec[2]);
28  EXPECT_FLOAT_EQ(17.f, vec[0]);
29 }
30 
31 
32 TEST(TTreeReaderArray, MultiReaders) {
33  // See https://root.cern.ch/phpBB3/viewtopic.php?f=3&t=22790
34  TTree* tree = new TTree("TTreeReaderArrayTree", "In-memory test tree");
35  double Double[6] = {42.f, 43.f, 44.f, 45.f, 46.f, 47.f};
36  tree->Branch("D", &Double, "D[4]/D");
37 
38  tree->Fill();
39  tree->Fill();
40  tree->Fill();
41 
42  tree->ResetBranchAddresses();
43 
44  TTreeReader TR(tree);
45  TTreeReaderArray<double> trDouble0(TR, "D");
46  TTreeReaderArray<double> trDouble1(TR, "D");
47  TTreeReaderArray<double> trDouble2(TR, "D");
48  TTreeReaderArray<double> trDouble3(TR, "D");
49  TTreeReaderArray<double> trDouble4(TR, "D");
50  TTreeReaderArray<double> trDouble5(TR, "D");
51 
52  TR.SetEntry(1);
53 
54  EXPECT_EQ(4u, trDouble0.GetSize());
55  EXPECT_EQ(4u, trDouble1.GetSize());
56  EXPECT_EQ(4u, trDouble2.GetSize());
57  EXPECT_EQ(4u, trDouble3.GetSize());
58  EXPECT_EQ(4u, trDouble4.GetSize());
59  EXPECT_EQ(4u, trDouble5.GetSize());
60  for (int i = 0; i < 4; ++i) {
61  EXPECT_DOUBLE_EQ(Double[i], trDouble0[i]);
62  EXPECT_DOUBLE_EQ(Double[i], trDouble1[i]);
63  EXPECT_DOUBLE_EQ(Double[i], trDouble2[i]);
64  EXPECT_DOUBLE_EQ(Double[i], trDouble3[i]);
65  EXPECT_DOUBLE_EQ(Double[i], trDouble4[i]);
66  EXPECT_DOUBLE_EQ(Double[i], trDouble5[i]);
67  }
68 }
TTreeReader is a simple, robust and fast interface to read values from a TTree, TChain or TNtuple...
Definition: TTreeReader.h:43
virtual Int_t Fill()
Fill all branches.
Definition: TTree.cxx:4383
EEntryStatus SetEntry(Long64_t entry)
Set the next entry (or index of the TEntryList if that is set).
Definition: TTreeReader.h:170
#define TR(N1, N2)
const char * Double
Extracts array data from a TTree.
double f(double x)
virtual void ResetBranchAddresses()
Tell all of our branches to drop their current objects and allocate new ones.
Definition: TTree.cxx:7629
TEST(TTreeReaderArray, Vector)
Definition: array.cxx:10
virtual Int_t Branch(TCollection *list, Int_t bufsize=32000, Int_t splitlevel=99, const char *name="")
Create one branch for each element in the collection.
Definition: TTree.cxx:1660
Definition: tree.py:1
A TTree object has a header with a name and a title.
Definition: TTree.h:78