forked from sPHENIX-Collaboration/coresoftware
/
PHNodeIOManager.cc
596 lines (535 loc) · 15.3 KB
/
PHNodeIOManager.cc
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
// Implementation of class PHNodeIOManager
// Author: Matthias Messer
#include "PHNodeIOManager.h"
#include "PHCompositeNode.h"
#include "PHIODataNode.h"
#include "PHNodeIterator.h"
#include "phooldefs.h"
#include <TBranch.h> // for TBranch
#include <TBranchElement.h>
#include <TBranchObject.h>
#include <TClass.h>
#include <TDirectory.h> // for TDirectory
#include <TFile.h>
#include <TLeafObject.h>
#include <TObjArray.h> // for TObjArray
#include <TObject.h>
#include <TROOT.h>
#include <TSystem.h>
#include <TTree.h>
#include <boost/algorithm/string.hpp>
#include <cassert>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
PHNodeIOManager::PHNodeIOManager(const string& f,
const PHAccessType a)
{
isFunctionalFlag = setFile(f, "titled by PHOOL", a) ? 1 : 0;
}
PHNodeIOManager::PHNodeIOManager(const string& f, const string& title,
const PHAccessType a)
{
isFunctionalFlag = setFile(f, title, a) ? 1 : 0;
}
PHNodeIOManager::PHNodeIOManager(const string& f, const PHAccessType a,
const PHTreeType treeindex)
{
if (treeindex != PHEventTree)
{
ostringstream temp;
temp << TreeName << treeindex; // create e.g. T1
TreeName = temp.str();
}
isFunctionalFlag = setFile(f, "titled by PHOOL", a) ? 1 : 0;
}
PHNodeIOManager::~PHNodeIOManager()
{
closeFile();
delete file;
}
void PHNodeIOManager::closeFile()
{
if (file)
{
if (accessMode == PHWrite || accessMode == PHUpdate)
{
file->Write();
}
file->Close();
}
}
bool PHNodeIOManager::setFile(const string& f, const string& title,
const PHAccessType a)
{
filename = f;
accessMode = a;
if (file)
{
if (file->IsOpen())
{
closeFile();
}
delete file;
file = nullptr;
}
string currdir = gDirectory->GetPath();
gROOT->cd();
switch (accessMode)
{
case PHWrite:
file = TFile::Open(filename.c_str(), "RECREATE", title.c_str());
if (!file)
{
return false;
}
file->SetCompressionLevel(CompressionLevel);
tree = new TTree(TreeName.c_str(), title.c_str());
tree->SetMaxTreeSize(900000000000LL); // set max size to ~900 GB
gROOT->cd(currdir.c_str());
return true;
break;
case PHReadOnly:
file = TFile::Open(filename.c_str());
tree = 0;
if (!file)
{
return false;
}
selectObjectToRead("*", true);
gROOT->cd(currdir.c_str());
return true;
break;
case PHUpdate:
file = TFile::Open(filename.c_str(), "UPDATE", title.c_str());
if (!file)
{
return false;
}
file->SetCompressionLevel(CompressionLevel);
tree = new TTree(TreeName.c_str(), title.c_str());
gROOT->cd(currdir.c_str());
return true;
break;
}
return false;
}
bool PHNodeIOManager::write(PHCompositeNode* topNode)
{
// The write function of the PHCompositeNode topNode will
// recursively call the write functions of its subnodes, thus
// constructing the path-string which is then stored as name of the
// Root-branch corresponding to the data of each PHRootIODataNode.
topNode->write(this);
// Now all PHRootIODataNodes should have called the write function
// of this I/O-manager and thus created their branch. The tree can
// be filled.
if (file && tree)
{
tree->Fill();
eventNumber++;
return true;
}
return false;
}
bool PHNodeIOManager::write(TObject** data, const string& path, int buffersize, int splitlevel)
{
if (file && tree)
{
TBranch* thisBranch = tree->GetBranch(path.c_str());
if (!thisBranch)
{
// the buffersize and splitlevel are set on the first call
// when the branch is created, the values come from the caller
// which is the node which writes itself
tree->Branch(path.c_str(), (*data)->ClassName(),
data, buffersize, splitlevel);
}
else
{
thisBranch->SetAddress(data);
}
return true;
}
return false;
}
bool PHNodeIOManager::read(size_t requestedEvent)
{
if (readEventFromFile(requestedEvent))
{
return true;
}
else
{
return false;
}
}
PHCompositeNode*
PHNodeIOManager::read(PHCompositeNode* topNode, size_t requestedEvent)
{
// No tree means we have not yet looked at the file,
// so we'll reconstruct the node tree now.
if (!tree)
{
topNode = reconstructNodeTree(topNode);
}
// If everything worked, there should be a tree now.
if (tree && readEventFromFile(requestedEvent))
{
return topNode;
}
else
{
return 0;
}
}
void PHNodeIOManager::print() const
{
if (file)
{
if (accessMode == PHReadOnly)
{
cout << "PHNodeIOManager reading " << filename << endl;
}
else
{
cout << "PHNodeIOManager writing " << filename << endl;
}
}
if (file && tree)
{
tree->Print();
}
cout << "\n\nList of selected objects to read:" << endl;
map<string, bool>::const_iterator classiter;
for (classiter = objectToRead.begin(); classiter != objectToRead.end(); ++classiter)
{
cout << classiter->first << " is set to " << classiter->second << endl;
}
}
string
PHNodeIOManager::getBranchClassName(TBranch* branch)
{
// OK. Here all the game is to find out the name of the type
// contained in this branch. In ROOT pre-3.01/05 versions, all
// branches we used were of the same type = TBranchObject, so that
// was easy. Since version 3.01/05 ROOT introduced new branch style
// with some TBranchElement objects. So far so good. The problem is
// that I did not find a common way to grab the typename of the
// object contained in those branches, so I hereby use some durty if
// { } else if { } ...
#if ROOT_VERSION_CODE >= ROOT_VERSION(3, 01, 5)
TBranchElement* be = dynamic_cast<TBranchElement*>(branch);
if (be)
{
// TBranchElement has a nice GetClassName() method for us :
return be->GetClassName();
}
#endif
TBranchObject* bo = dynamic_cast<TBranchObject*>(branch);
if (bo)
{
// For this one we need to go down a little before getting the
// name...
TLeafObject* leaf = static_cast<TLeafObject*>(branch->GetLeaf(branch->GetName()));
assert(leaf != 0);
return leaf->GetTypeName();
}
cout << PHWHERE << "Fatal error, dynamic cast of TBranchObject failed" << endl;
gSystem->Exit(1);
exit(1); // the compiler does not know gSystem->Exit() quits, needs exit to avoid warning
}
bool PHNodeIOManager::readEventFromFile(size_t requestedEvent)
{
// Se non c'e niente, non possiamo fare niente. Logisch, n'est ce
// pas?
if (!tree)
{
PHMessage("PHNodeIOManager::readEventFromFile", PHError,
"Tree not initialized.");
return false;
}
int bytesRead;
// Due to the current implementation of TBuffer>>(Long_t) we need
// to cd() in the current file before trying to fetch any event,
// otherwise mixing of reading 2.25/03 DST with writing some
// 3.01/05 trees will fail.
string currdir = gDirectory->GetPath();
TFile* file_ptr = gFile; // save current gFile
file->cd();
if (requestedEvent)
{
if ((bytesRead = tree->GetEvent(requestedEvent)))
{
eventNumber = requestedEvent + 1;
}
}
else
{
bytesRead = tree->GetEvent(eventNumber++);
}
gFile = file_ptr; // recover gFile
gROOT->cd(currdir.c_str());
if (!bytesRead)
{
return false;
}
if (bytesRead == -1)
{
cout << PHWHERE << "Error: Input TTree corrupt, exiting now" << endl;
exit(1);
}
return true;
}
int PHNodeIOManager::readSpecific(size_t requestedEvent, const std::string& objectName)
{
// objectName should be one of the valid branch name of the "T" TTree, and
// should be one of the branches selected by selectObjectToRead() method.
// No wildcard allowed for the moment.
map<string, TBranch*>::const_iterator p = fBranches.find(objectName);
if (p != fBranches.end())
{
TBranch* branch = p->second;
if (branch)
{
return branch->GetEvent(requestedEvent);
}
}
else
{
PHMessage("PHNodeIOManager::readSpecific", PHError,
"Unknown object name");
}
return 0;
}
PHCompositeNode*
PHNodeIOManager::reconstructNodeTree(PHCompositeNode* topNode)
{
if (!file)
{
if (filename.empty())
{
cout << PHWHERE << "filename was never set" << endl;
}
else
{
cout << PHWHERE << "TFile " << filename << " NULL pointer" << endl;
}
return nullptr;
}
tree = static_cast<TTree*>(file->Get(TreeName.c_str()));
if (!tree)
{
cout << PHWHERE << "PHNodeIOManager::reconstructNodeTree : Root Tree "
<< TreeName << " not found in file " << file->GetName() << endl;
return nullptr;
}
// ROOT sucks, we need a unique name for the tree so we can open multiple
// files. So we take the memory location of the file pointer which
// should be unique within this process to create it
ostringstream nname;
nname << TreeName << file;
tree->SetName(nname.str().c_str());
// Select the branches according to objectToRead
map<string, bool>::const_iterator it;
if (tree->GetNbranches() > 0)
{
for (it = objectToRead.begin(); it != objectToRead.end(); ++it)
{
tree->SetBranchStatus((it->first).c_str(),
static_cast<bool>(it->second));
}
}
// The file contains a TTree with a list of the TBranchObjects
// attached to it.
TObjArray* branchArray = tree->GetListOfBranches();
// We need these in the loops down below...
size_t i, j;
// If a topNode was provided, we can feed the iterator with it.
if (!topNode)
{
topNode = new PHCompositeNode("TOP"); // create topNode if we got a null pointer
}
PHNodeIterator nodeIter(topNode);
// Loop over all branches in the tree. Each branch-name contains the
// full 'path' of composite-nodes in the original node tree. We
// split the name and reconstruct the tree.
string delimeters = phooldefs::branchpathdelim + phooldefs::legacypathdelims; // add old backslash for backward compat
for (i = 0; i < (size_t)(branchArray->GetEntriesFast()); i++)
{
string branchname = (*branchArray)[i]->GetName();
vector<string> splitvec;
boost::split(splitvec, branchname, boost::is_any_of(delimeters));
for (size_t ia = 1; ia < splitvec.size() - 1; ia++) // -1 so we skip the node name
{
if (!nodeIter.cd(splitvec[ia]))
{
nodeIter.addNode(new PHCompositeNode(splitvec[ia]));
nodeIter.cd(splitvec[ia]);
}
}
TBranch* thisBranch = (TBranch*) ((*branchArray)[i]);
// Skip non-selected branches
if (thisBranch->TestBit(kDoNotProcess))
{
continue;
}
string branchClassName = getBranchClassName(thisBranch);
string branchName = thisBranch->GetName();
fBranches[branchName] = thisBranch;
assert(gROOT != 0);
TClass* thisClass = gROOT->GetClass(branchClassName.c_str());
if (!thisClass)
{
cout << PHWHERE << endl;
cout << "Missing Class: " << branchClassName << endl;
cout << "Did you forget to load the shared library which contains "
<< branchClassName << "?" << endl;
}
// it does not make sense to continue - the code coredumps
// later if a class is not loaded
assert(thisClass != 0);
PHIODataNode<TObject>* newIODataNode =
static_cast<PHIODataNode<TObject>*>(nodeIter.findFirst("PHIODataNode", (*splitvec.rbegin()).c_str()));
if (!newIODataNode)
{
TObject* newTObject = static_cast<TObject*>(thisClass->New());
newIODataNode = new PHIODataNode<TObject>(newTObject, (*splitvec.rbegin()).c_str());
nodeIter.addNode(newIODataNode);
}
else
{
TObject* oldobject = newIODataNode->getData();
string oldclass = oldobject->ClassName();
if (oldclass != branchClassName)
{
cout << "You only have to worry if you get this message when reading parallel files"
<< endl
<< "if you get this when opening the 2nd, 3rd,... file" << endl
<< "It looks like your objects are not of the same version in these files" << endl;
cout << PHWHERE << "Found object " << oldobject->ClassName()
<< " in node tree but the file "
<< filename << " contains a " << branchClassName
<< " object. The object will be replaced without harming you" << endl;
cout << "CAVEAT: If you use local copies of pointers to data nodes" << endl
<< "instead of searching the node tree you are in trouble now" << endl;
delete newIODataNode;
TObject* newTObject = static_cast<TObject*>(thisClass->New());
newIODataNode = new PHIODataNode<TObject>(newTObject, (*splitvec.rbegin()).c_str());
nodeIter.addNode(newIODataNode);
}
}
if (thisClass->InheritsFrom("PHObject"))
{
newIODataNode->setObjectType("PHObject");
}
else
{
cout << PHWHERE << branchClassName.c_str()
<< " inherits neither from PHTable nor from PHObject"
<< " setting type to PHObject" << endl;
newIODataNode->setObjectType("PHObject");
}
thisBranch->SetAddress(&(newIODataNode->data));
for (j = 1; j < splitvec.size() - 1; j++)
{
nodeIter.cd("..");
}
}
return topNode;
}
void PHNodeIOManager::selectObjectToRead(const std::string& objectName, bool readit)
{
objectToRead[objectName] = readit;
// If tree is already open, loop over map and set branch status
if (tree)
{
map<string, bool>::const_iterator it;
for (it = objectToRead.begin(); it != objectToRead.end(); ++it)
{
tree->SetBranchStatus((it->first).c_str(),
static_cast<bool>(it->second));
}
}
return;
}
bool PHNodeIOManager::isSelected(const std::string& objectName)
{
map<string, TBranch*>::const_iterator p = fBranches.find(objectName);
if (p != fBranches.end())
{
return true;
}
return false;
}
bool PHNodeIOManager::SetCompressionLevel(const int level)
{
if (level < 0)
{
return false;
}
CompressionLevel = level;
if (file)
{
file->SetCompressionLevel(CompressionLevel);
}
return true;
}
double
PHNodeIOManager::GetBytesWritten()
{
if (file) return file->GetBytesWritten();
return 0.;
}
map<string, TBranch*>*
PHNodeIOManager::GetBranchMap()
{
FillBranchMap();
return &fBranches;
}
int PHNodeIOManager::FillBranchMap()
{
if (fBranches.empty())
{
TTree* treetmp = static_cast<TTree*>(file->Get(TreeName.c_str()));
if (treetmp)
{
TObjArray* branchArray = treetmp->GetListOfBranches();
for (size_t i = 0; i < (size_t)(branchArray->GetEntriesFast()); i++)
{
TBranch* thisBranch = (TBranch*) ((*branchArray)[i]);
string branchName = (*branchArray)[i]->GetName();
fBranches[branchName] = thisBranch;
}
}
else
{
cout << PHWHERE << " No Root Tree " << TreeName
<< " on file " << filename << endl;
return -1;
}
}
return 0;
}
bool PHNodeIOManager::NodeExist(const std::string& nodename)
{
if (fBranches.empty())
{
FillBranchMap();
}
string delimeters = phooldefs::branchpathdelim + phooldefs::legacypathdelims; // add old backslash for backward compat
for (auto iter = fBranches.begin(); iter != fBranches.end(); ++iter)
{
vector<string> splitvec;
boost::split(splitvec, iter->first, boost::is_any_of(delimeters));
if (splitvec.back() == nodename)
{
return true;
}
}
return false;
}