11 #include <TBranchElement.h>
12 #include <TBranchObject.h>
14 #include <TDirectory.h>
16 #include <TLeafObject.h>
17 #include <TObjArray.h>
23 #include <boost/algorithm/string.hpp>
38 isFunctionalFlag = setFile(f,
"titled by PHOOL", a) ? 1 : 0;
44 isFunctionalFlag = setFile(f, title, a) ? 1 : 0;
53 temp << TreeName << treeindex;
54 TreeName = temp.str();
56 isFunctionalFlag = setFile(f,
"titled by PHOOL", a) ? 1 : 0;
91 string currdir = gDirectory->GetPath();
96 file = TFile::Open(
filename.c_str(),
"RECREATE", title.c_str());
101 file->SetCompressionLevel(CompressionLevel);
102 tree =
new TTree(TreeName.c_str(), title.c_str());
103 tree->SetMaxTreeSize(900000000000
LL);
104 gROOT->cd(currdir.c_str());
114 selectObjectToRead(
"*",
true);
115 gROOT->cd(currdir.c_str());
119 file = TFile::Open(
filename.c_str(),
"UPDATE", title.c_str());
124 file->SetCompressionLevel(CompressionLevel);
125 tree =
new TTree(TreeName.c_str(), title.c_str());
126 gROOT->cd(currdir.c_str());
140 topNode->
write(
this);
159 TBranch* thisBranch =
tree->GetBranch(path.c_str());
165 tree->Branch(path.c_str(), (*data)->ClassName(),
166 data, buffersize, splitlevel);
170 thisBranch->SetAddress(data);
180 if (readEventFromFile(requestedEvent))
197 topNode = reconstructNodeTree(topNode);
201 if (
tree && readEventFromFile(requestedEvent))
217 cout <<
"PHNodeIOManager reading " <<
filename << endl;
221 cout <<
"PHNodeIOManager writing " <<
filename << endl;
228 cout <<
"\n\nList of selected objects to read:" << endl;
229 map<string, bool>::const_iterator classiter;
230 for (classiter = objectToRead.begin(); classiter != objectToRead.end(); ++classiter)
232 cout << classiter->first <<
" is set to " << classiter->second << endl;
248 #if ROOT_VERSION_CODE >= ROOT_VERSION(3, 01, 5)
249 TBranchElement* be =
dynamic_cast<TBranchElement*
>(branch);
254 return be->GetClassName();
258 TBranchObject* bo =
dynamic_cast<TBranchObject*
>(branch);
263 TLeafObject* leaf =
static_cast<TLeafObject*
>(branch->GetLeaf(branch->GetName()));
265 return leaf->GetTypeName();
267 cout <<
PHWHERE <<
"Fatal error, dynamic cast of TBranchObject failed" << endl;
279 "Tree not initialized.");
289 string currdir = gDirectory->GetPath();
290 TFile* file_ptr = gFile;
295 if ((bytesRead =
tree->GetEvent(requestedEvent)))
297 eventNumber = requestedEvent + 1;
302 bytesRead =
tree->GetEvent(eventNumber++);
306 gROOT->cd(currdir.c_str());
314 cout <<
PHWHERE <<
"Error: Input TTree corrupt, exiting now" << endl;
325 map<string, TBranch*>::const_iterator
p = fBranches.find(objectName);
327 if (p != fBranches.end())
329 TBranch* branch = p->second;
332 return branch->GetEvent(requestedEvent);
338 "Unknown object name");
350 cout <<
PHWHERE <<
"filename was never set" << endl;
359 tree =
static_cast<TTree*
>(
file->Get(TreeName.c_str()));
363 cout <<
PHWHERE <<
"PHNodeIOManager::reconstructNodeTree : Root Tree "
364 << TreeName <<
" not found in file " <<
file->GetName() << endl;
372 nname << TreeName <<
file;
374 tree->SetName(nname.str().c_str());
377 map<string, bool>::const_iterator
it;
379 if (
tree->GetNbranches() > 0)
381 for (it = objectToRead.begin(); it != objectToRead.end(); ++
it)
383 tree->SetBranchStatus((it->first).c_str(),
384 static_cast<bool>(it->second));
389 TObjArray* branchArray =
tree->GetListOfBranches();
405 for (i = 0; i < (size_t)(branchArray->GetEntriesFast()); i++)
407 string branchname = (*branchArray)[i]->GetName();
408 vector<string> splitvec;
409 boost::split(splitvec, branchname, boost::is_any_of(delimeters));
410 for (
size_t ia = 1; ia < splitvec.size() - 1; ia++)
412 if (!nodeIter.
cd(splitvec[ia]))
415 nodeIter.
cd(splitvec[ia]);
418 TBranch* thisBranch = (TBranch*) ((*branchArray)[i]);
421 if (thisBranch->TestBit(kDoNotProcess))
426 string branchClassName = getBranchClassName(thisBranch);
427 string branchName = thisBranch->GetName();
428 fBranches[branchName] = thisBranch;
431 TClass* thisClass = gROOT->GetClass(branchClassName.c_str());
436 cout <<
"Missing Class: " << branchClassName << endl;
437 cout <<
"Did you forget to load the shared library which contains "
438 << branchClassName <<
"?" << endl;
442 assert(thisClass != 0);
450 nodeIter.
addNode(newIODataNode);
455 string oldclass = oldobject->ClassName();
456 if (oldclass != branchClassName)
458 cout <<
"You only have to worry if you get this message when reading parallel files"
460 <<
"if you get this when opening the 2nd, 3rd,... file" << endl
461 <<
"It looks like your objects are not of the same version in these files" << endl;
462 cout <<
PHWHERE <<
"Found object " << oldobject->ClassName()
463 <<
" in node tree but the file "
464 <<
filename <<
" contains a " << branchClassName
465 <<
" object. The object will be replaced without harming you" << endl;
466 cout <<
"CAVEAT: If you use local copies of pointers to data nodes" << endl
467 <<
"instead of searching the node tree you are in trouble now" << endl;
468 delete newIODataNode;
471 nodeIter.
addNode(newIODataNode);
475 if (thisClass->InheritsFrom(
"PHObject"))
481 cout <<
PHWHERE << branchClassName.c_str()
482 <<
" inherits neither from PHTable nor from PHObject"
483 <<
" setting type to PHObject" << endl;
486 thisBranch->SetAddress(&(newIODataNode->
data));
487 for (j = 1; j < splitvec.size() - 1; j++)
497 objectToRead[objectName] = readit;
502 map<string, bool>::const_iterator
it;
504 for (it = objectToRead.begin(); it != objectToRead.end(); ++
it)
506 tree->SetBranchStatus((it->first).c_str(),
507 static_cast<bool>(it->second));
515 map<string, TBranch*>::const_iterator
p = fBranches.find(objectName);
517 if (p != fBranches.end())
531 CompressionLevel = level;
534 file->SetCompressionLevel(CompressionLevel);
543 if (
file)
return file->GetBytesWritten();
547 map<string, TBranch*>*
556 if (fBranches.empty())
558 TTree* treetmp =
static_cast<TTree*
>(
file->Get(TreeName.c_str()));
561 TObjArray* branchArray = treetmp->GetListOfBranches();
562 for (
size_t i = 0; i < (size_t)(branchArray->GetEntriesFast()); i++)
564 TBranch* thisBranch = (TBranch*) ((*branchArray)[i]);
565 string branchName = (*branchArray)[i]->GetName();
566 fBranches[branchName] = thisBranch;
571 cout <<
PHWHERE <<
" No Root Tree " << TreeName
581 if (fBranches.empty())
586 for (
auto iter = fBranches.begin(); iter != fBranches.end(); ++iter)
588 vector<string> splitvec;
589 boost::split(splitvec, iter->first, boost::is_any_of(delimeters));
590 if (splitvec.back() == nodename)