Logo ROOT   6.10/09
Reference Guide
leafs.cxx
Go to the documentation of this file.
1 #include "TFile.h"
2 #include "TInterpreter.h"
3 #include "TTree.h"
4 #include "TTreeReader.h"
5 #include "TTreeReaderValue.h"
6 #include "TTreeReaderArray.h"
7 
8 #include "gtest/gtest.h"
9 
10 #include "data.h"
11 
12 #include <fstream>
13 
14 TEST(TTreeReaderLeafs, LeafListCaseA) {
15  // From "Case A" of the TTree class doc:
16  const char* str = "This is a null-terminated string literal";
17  signed char SChar = 2;
18  unsigned char UChar = 3;
19  signed short SShort = 4;
20  unsigned short UShort = 5;
21  signed int SInt = 6;
22  unsigned int UInt = 7;
23  float Float = 8.;
24  double Double = 9.;
25  long long SLL = 10;
26  unsigned long long ULL = 11;
27  bool Bool = true;
28 
29  TTree* tree = new TTree("T", "In-memory test tree");
30  tree->Branch("C", &str, "C/C");
31  tree->Branch("B", &SChar, "B/B");
32  tree->Branch("b", &UChar, "b/b");
33  tree->Branch("S", &SShort, "S/S");
34  tree->Branch("s", &UShort, "s/s");
35  tree->Branch("I", &SInt, "I/I");
36  tree->Branch("i", &UInt, "i/i");
37  tree->Branch("F", &Float, "F/F");
38  tree->Branch("D", &Double, "D/D");
39  tree->Branch("L", &SLL, "L/L");
40  tree->Branch("l", &ULL, "l/l");
41  tree->Branch("O", &Bool, "O/O");
42 
43  tree->Fill();
44  tree->Fill();
45  tree->Fill();
46 
47  TTreeReader TR(tree);
48  //TTreeReaderValue<const char*> trStr(TR, "C");
49  TTreeReaderValue<signed char> trSChar(TR, "B");
50  TTreeReaderValue<unsigned char> trUChar(TR, "b");
51  TTreeReaderValue<signed short> trSShort(TR, "S");
52  TTreeReaderValue<unsigned short> trUShort(TR, "s");
53  TTreeReaderValue<signed int> trSInt(TR, "I");
54  TTreeReaderValue<unsigned int> trUInt(TR, "i");
55  TTreeReaderValue<float> trFloat(TR, "F");
56  TTreeReaderValue<double> trDouble(TR, "D");
59  TTreeReaderValue<bool> trBool(TR, "O");
60 
61  TR.SetEntry(1);
62 
63  //EXPECT_STREQ(str, *trStr);
64  EXPECT_EQ(SChar, *trSChar);
65  EXPECT_EQ(UChar, *trUChar);
66  EXPECT_EQ(SShort, *trSShort);
67  EXPECT_EQ(UShort, *trUShort);
68  EXPECT_EQ(SInt, *trSInt);
69  EXPECT_EQ(UInt, *trUInt);
70  EXPECT_FLOAT_EQ(Float, *trFloat);
71  EXPECT_DOUBLE_EQ(Double, *trDouble);
72  EXPECT_EQ(SLL, *trSLL);
73  EXPECT_EQ(ULL, *trULL);
74  EXPECT_EQ(Bool, *trBool);
75 }
76 
77 
78 
79 std::unique_ptr<TTree> CreateTree() {
81  gInterpreter->ProcessLine("#include \"data.h\"", &error);
82  if (error != TInterpreter::kNoError)
83  return {};
84 
85  Data data;
86  std::unique_ptr<TTree> tree(new TTree("T", "test tree"));
87  tree->Branch("Data", &data);
88  data.fArray = new double[4]{12., 13., 14., 15.};
89  data.fSize = 4;
90  data.fUArray = new float[2]{42., 43.};
91  data.fUSize = 2;
92  data.fVec = { 17., 18., 19., 20., 21., 22.};
93  data.fDouble32 = 17.;
94  data.fFloat16 = 44.;
95  tree->Fill();
96 
97  data.fVec.clear();
98  data.fVec.resize(3210, 1001.f); // ROOT-8747
99  tree->Fill();
100 
101  data.fVec.clear();
102  data.fVec.resize(2, 42.f); // ROOT-8747
103  tree->Fill();
104 
105  tree->ResetBranchAddresses();
106  return tree;
107 }
108 
109 TEST(TTreeReaderLeafs, LeafList) {
110  auto tree = CreateTree();
111  ASSERT_NE(nullptr, tree.get());
112 
113  TTreeReader tr(tree.get());
114  TTreeReaderArray<double> arr(tr, "fArray");
115  TTreeReaderArray<float> arrU(tr, "fUArray");
116  TTreeReaderArray<double> vec(tr, "fVec");
117  TTreeReaderValue<Double32_t> d32(tr, "fDouble32");
118  TTreeReaderValue<Float16_t> f16(tr, "fFloat16");
119 
120  tr.Next();
121  EXPECT_EQ(4u, arr.GetSize());
122  EXPECT_EQ(2u, arrU.GetSize());
123  EXPECT_EQ(6u, vec.GetSize());
124  //FAILS EXPECT_FLOAT_EQ(13., arr[1]);
125  //FAILS EXPECT_DOUBLE_EQ(43., arrU[1]);
126  EXPECT_DOUBLE_EQ(19., vec[2]);
127  EXPECT_DOUBLE_EQ(17., vec[0]);
128  // T->Scan("fUArray") claims fUArray only has one instance per row.
129 
130  EXPECT_FLOAT_EQ(17, *d32);
131  EXPECT_FLOAT_EQ(44, *f16);
132 
133  tr.Next();
134  EXPECT_FLOAT_EQ(1001.f, vec[1]); // ROOT-8747
135  EXPECT_EQ(3210u, vec.GetSize());
136 
137  tr.Next();
138  EXPECT_FLOAT_EQ(42.f, vec[1]); // ROOT-8747
139  EXPECT_EQ(2u, vec.GetSize());
140  tree.release();
141 }
142 
143 TEST(TTreeReaderLeafs, TArrayD) {
144  // https://root-forum.cern.ch/t/tarrayd-in-ttreereadervalue/24495
145  TTree* tree = new TTree("TTreeReaderLeafsTArrayD", "In-memory test tree");
146  TArrayD arrD(7);
147  for (int i = 0; i < arrD.GetSize(); ++i)
148  arrD.SetAt(i + 2., i);
149  tree->Branch("arrD", &arrD);
150 
151  tree->Fill();
152  tree->Fill();
153  tree->Fill();
154 
155  tree->ResetBranchAddresses();
156 
157  TTreeReader tr(tree);
158  TTreeReaderValue<TArrayD> arr(tr, "arrD");
159 
160  tr.SetEntry(1);
161 
162  EXPECT_EQ(7, arr->GetSize());
163  EXPECT_DOUBLE_EQ(4., (*arr)[2]);
164  EXPECT_DOUBLE_EQ(2., (*arr)[0]);
165 }
void SetAt(Double_t v, Int_t i)
Definition: TArrayD.h:51
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
#define gInterpreter
Definition: TInterpreter.h:499
const char * UShort
const char * UInt
TEST(TTreeReaderLeafs, LeafListCaseA)
Definition: leafs.cxx:14
std::vector< std::vector< double > > Data
Extracts data from a TTree.
const char * UChar
const char * Float
Int_t GetSize() const
Definition: TArray.h:47
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
Array of doubles (64 bits per element).
Definition: TArrayD.h:27
const char * Bool
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
std::unique_ptr< TTree > CreateTree()
Definition: leafs.cxx:79
Definition: tree.py:1
A TTree object has a header with a name and a title.
Definition: TTree.h:78