-
Notifications
You must be signed in to change notification settings - Fork 1
/
ReadRootTree.cpp
444 lines (366 loc) · 13.1 KB
/
ReadRootTree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#include "ReadRootTree.h"
#ifdef DEBUG
#include <iostream>
#endif
#undef DEBUG
//uncomment the following line if it is desirable to use integer
//for computation; currently only float can be used in expressions which are
//evaluated using mu::Parser. Be careful with defining the following symbol,
//however, as converting from float to int will result in losts in precision
//
//#define TREAT_INTEGER_AS_FLOAT
using namespace std;
using namespace mu;
const vector<string> ReadRootTree::getRootTreeNames(string rootFile)
{
TFile f(rootFile.c_str());
vector<string> names;
TIter next(f.GetListOfKeys());
TKey *key;
while ((key = (TKey*) next()))
{
if (string(key->GetClassName()) == "TTree")
names.push_back(key->GetName());
}
return names;
}
void ReadRootTree::getRootBranchNames (std::string rootFile,
std::string treeName,
std::vector<std::string>& names,
std::vector<char>& types)
{
TFile f(rootFile.c_str());
TTree * t = (TTree*) f.Get(treeName.c_str());
getBranchNames(t,names,types);
}
ReadRootTree::ReadRootTree(vector<string> root_files, const char * tree_name,
const char* branchNamees_file)
{
if (root_files.empty())
{
cerr << "Fatal error! cannot initialize without valid rootfile(s)"
<< endl;
return;
}
if (!tree_name)
// then find and read the first TTree found in the first root file
{
vector<string> treeNames = getRootTreeNames(root_files[0]);
if (!treeNames.empty())
tree_name = treeNames[0].c_str();
else
{
cerr << "Fatal error! there is no TTree existing in"
"the given root file: " << root_files[0] << endl;
return;
}
}
chain = new TChain(tree_name);
// copy root_files into listOfFiles for later reference
listOfFiles.clear();
listOfFiles.assign(root_files.begin(),root_files.end());
// adding the root files into the TChain
for (int i = 0; i < (int) root_files.size(); i++)
chain->Add(root_files[i].c_str());
// get the initial count of events
entries = (int)chain->GetEntries();
// get the branch names either from file or from TTree/TChain
if ((branchNamees_file) && fileExists(branchNamees_file))
{
branchNameFile = new string(branchNamees_file);
getBranches(branchNamees_file);
}
else
{
branchNameFile = NULL;
getBranches();
}
initBranches();
setEventCut("");
}
ReadRootTree::~ReadRootTree()
{
delete chain;
delete eventCut;
branch_address.clear();
}
const void ReadRootTree::getBranchNames(TTree * t,
std::vector<std::string>& names,
std::vector<char>& types)
{
TIter next(t->GetListOfBranches());
TBranch * branch;
TLeaf * leaf;
// read each branch; if the branch has type float (TLeafF) or int (TLeafI)
// then process the branch into the vectors names and types
while ((branch = (TBranch*) next()))
{
leaf = (TLeaf*) branch->GetListOfLeaves()->At(0);
if (string(leaf->ClassName()) == "TLeafF")
{
names.push_back(branch->GetName());
types.push_back('F');
}
else if (string(leaf->ClassName()) == "TLeafI")
{
names.push_back(branch->GetName());
types.push_back('I');
}
}
}
void ReadRootTree::getBranches()
{
getBranchNames((TTree*) chain,branches,branch_types);
}
void ReadRootTree::getBranches(const char *filename)
{
ifstream infile (filename);
string s;
while (getline(infile, s, '\n'))
{
if (s.at(0) != CFG_COMMENT_CHAR) //don't read the comment line
{
//each line is formated as: T/branch_name
//where T is the type, only support F=float and I=integer for now
branch_types.push_back(s.at(0));
branches.push_back (s.substr(2));
}
}
}
void ReadRootTree::initBranches()
{
// allot memory for the branch values; todo using float for the largest
// size for now. IMPORTANT: do not delete this vector<float>; "static"
// is important! take care with multi-threading of multiple ReadRootTree
// objects, as each call to TTree.GetEntry will mess up the values
// pointing by branch_address
static vector<float> f (branches.size());
for (int i = 0; i<(int) branches.size() ; i++)
branch_address.push_back(&f[i]);
for (int i = 0; i<(int) branches.size() ; i++)
chain->SetBranchAddress(branches[i].c_str(),branch_address[i]);
#ifdef DEBUG //print the first entry for debugging purpose
chain->GetEntry(0);
cout << "First entry from root file:" << endl;
for (int i = 0; i<(int) branches.size() ; i++)
{
cout << branches[i] << "(" << branch_types[i] << ") \t\t\t = ";
if (branch_types[i] == 'F') // read as float
cout << *((float*) branch_address[i]) << endl;
else if (branch_types[i] == 'I') // read as int
cout << *((int*) branch_address[i]) << endl;
}
#endif
}
void ReadRootTree::initParsers(int n)
{
Parser *p;
// substantiate parser as needed and set variable addresses for the new
// parsers
while (n > (int) parsers.size())
{
p = new Parser();
for (int i = 0; i<(int) branches.size() ; i++)
{
// Warning! only add variable of type float for expression
// evaluation since mu::parser is designed only for float; for
// other types garbages values might occur!
#ifndef TREAT_INTEGER_AS_FLOAT
if (branch_types[i] == 'F')
#endif
p->DefineVar(branches[i].c_str(),
(float*) branch_address[i]);
}
parsers.push_back(*p);
}
}
void ReadRootTree::setEventCut(const char * filter,const char * sort_by,
bool sort_desc)
{
// reset the previous eventcut
chain->SetEventList(NULL);
eventlist.clear();
if (filter)
eventCut = new string(filter);
entries = chain->Draw(">>eventlist",eventCut->c_str());
if (entries == -1) return; // somehow Draw() fails; run away...
TEventList * teventlist = (TEventList*)gDirectory->Get("eventlist");
Long64_t* tmplist = teventlist->GetList();
if (sort_by) // also sort the events
{
teventlist->SetReapplyCut(true);
chain->SetEventList(teventlist);
// drawing with variable sort_by with no graphic
entries = chain->Draw(sort_by,"","goff");
if (entries == -1) return; // somehow Draw() fails; run away...
// to hold the event indices that will be sorted using sort_by
int * indices = new int[entries];
// and then sort...
TMath::Sort(entries,chain->GetV1(),indices,sort_desc? kTRUE : kFALSE);
for (int i = 0; i < entries ; i++)
eventlist.push_back( tmplist[indices[i]]);
delete [] indices;
}
else // no need to sort, just add the event numbers to eventlist
{
for (int i = 0; i < entries ; i++)
eventlist.push_back(tmplist[i]);
}
// #ifdef DEBUG
// cout << "Selected Events after cut: " << endl;
// for (int i = 0; i < entries ; i++)
// cout << eventlist[i] << " ";
// cout << endl;
// cout << entries << endl;
// #endif
}
int ReadRootTree::fillValues(int (*callback)(void*,int&,vector<float>&,
long& total_n ),
void* obj,
vector<string> expressions)
{
if (!callback) return 0;
return fillValues_all((void*)callback,CALLBACK_FLOAT,obj,expressions);
}
int ReadRootTree::fillValues_str(int (*callback)(void*,int&,vector<string>&,
long& total_n ),
void* obj,
vector<string> expressions)
{
if (!callback) return 0;
return fillValues_all((void*)callback,CALLBACK_STR,obj,expressions);
}
int ReadRootTree::fillValues_all(void* callback_func,callback_type type,
void* obj,
vector<string> expressions)
{
if (!callback_func) return 0;
vector<float> values_f(expressions.size());
vector<string> values_s(expressions.size());
// keep track of whether the expression corresponds to an integer type;
// positive values corresponds to index of the corresponding branch, -1
// signify that the expression should be evaluated (instead of simple
// substitution)
vector<int> iexp(expressions.size());
ostringstream ss;
int ret_code;
int (*callback)(void*,int&,vector<float>&,long&) = 0;
int (*callback_str) (void*,int&,vector<string>&,long&) = 0;
// cast the callback function to the correct type
if (type == CALLBACK_STR)
callback_str = (int (*) (void*,int&,vector<string>&,long&))
callback_func;
else
callback = (int (*)(void*,int&,vector<float>&,long&)) callback_func;
// init parsers based on how many needed
initParsers(expressions.size());
// loop through the expressions and prepare the parsers
for (int j = 0; j < (int) expressions.size(); j++)
{
if (type == CALLBACK_STR)
{
for (int i = 0; i < (int) branches.size(); i++)
{
// if a expression corresponds to an integer branch type then
// mark the expression to be not evaluated
if (branch_types[i] == 'I' && expressions[j] == branches[i])
{
iexp[j] = i;
break;
}
else
iexp[j] = -1;
}
}
else
iexp[j] = -1; //the expression is to be evaluated
if (expressions[j] != "")
parsers[j].SetExpr(expressions[j]);
}
// time to loop through the events and call the callback func!
for (int i = 0;i < getNumberEntries() ; i++)
{
// will fill values into the addresses in branch_address
chain->GetEntry(eventlist[i]);
#ifdef TREAT_INTEGER_AS_FLOAT
for (int k = 0; k < (int) branches.size(); k++)
{
if (branch_types[k] == 'I')
{
cout << branches[k] << " ";
cout << *((int*) branch_address[k]) << endl;
*(float*) branch_address[k] =
(float) *((int*) branch_address[k]);
cout << *((float*) branch_address[k]) << endl;
}
}
#endif
#ifdef DEBUG
cout << i << " (Event #" << eventlist[i]
<< "): -------------------------------" <<endl;
#endif
for (int j =0; j <(int) expressions.size(); j++)
{
if (expressions[j] != "" && iexp[j] == -1) //evaluate expression
{
try
{
if (type == CALLBACK_STR)
{
ss.str("");
ss << parsers[j].Eval();
values_s[j] = ss.str();
}
else
values_f[j] = parsers[j].Eval();
}
catch (mu::Parser::exception_type e)
{
cerr << e.GetMsg() << endl;
message = e.GetMsg();
return 1;
// todo: don't go on when the expression can't be parsed
}
}
else if (expressions[j] != "") // substitution (for integer)
{
ss.str(""); // clean up the stringstream
ss << *((int*) branch_address[iexp[j]]);
values_s[j]=ss.str();
}
else if (i == 0) // if it's the first, clean up the previous data
{
if (type == CALLBACK_STR)
values_s[j] = "#ERROR#";
else
values_f[j] = 0;
}
// else - do nothing
#ifdef DEBUG
if (type == CALLBACK_STR)
cout << expressions[j] << " = " << values_s [j] << endl;
else
cout << expressions[j] << " = " << values_f [j] << endl;
#endif
}
// call the appropriate callback function
ret_code = (type == CALLBACK_STR)?
callback_str(obj,i,values_s,entries):
callback(obj,i,values_f,entries);
if (ret_code != 0) return ret_code; // aborted by callback
}
return 0;
}
void ReadRootTree::saveToNewFile(string filename)
{
TFile *file = new TFile(filename.c_str(),"recreate");
TTree *newtree = chain->CloneTree(0);
for (long i = 0; i < entries; ++i)
{
chain->GetEntry(eventlist[i]);
newtree->Fill();
}
newtree->Print();
newtree->AutoSave();
delete file;
}