5#include "TClonesArray.h"
12#include "GaudiKernel/SmartIF.h"
13#include "GaudiKernel/Bootstrap.h"
14#include "GaudiKernel/IAppMgrUI.h"
15#include "GaudiKernel/IProperty.h"
16#include "GaudiKernel/ISvcLocator.h"
26 if (m_rootInterface)
return m_rootInterface;
28 return m_rootInterface;
34 m_branches=
new TClonesArray(
"TBranch",1);
35 m_branchesRead=
new TClonesArray(
"TBranch",1);
49 IInterface* iface = Gaudi::createApplicationMgr();
50 SmartIF<IProperty> propMgr (iface );
52 propMgr->getProperty(
"JobOptionsPath", path);
53 log << MSG::INFO <<
"JobOptions file for current job: " <<path << endreq;
54 ifstream fin(path.c_str());
57 while(getline(fin,tempString))
59 if( tempString.size()>0 && tempString.find(
"//")>tempString.size() )
61 jobOptions += tempString;
65 log << MSG::INFO <<
"JobOptions: " << endreq
66 << jobOptions << endreq;
72 ISvcLocator* svcLocator = Gaudi::svcLocator();
76 StatusCode status = svcLocator->service(
"DataInfoSvc",tmpInfoSvc);
77 if (status.isSuccess()) {
78 log << MSG::INFO <<
"get the DataInfoSvc" << endreq;
81 log << MSG::INFO <<
"get decay options" << endreq
82 << decayOptions << endreq;
84 log << MSG::WARNING <<
"could not get the DataInfoSvc. Ignore it." << endreq;
91 ISvcLocator* svcLocator = Gaudi::svcLocator();
94 std::vector<int> totEvtNo;
95 StatusCode status = svcLocator->service(
"DataInfoSvc",tmpInfoSvc);
96 if (status.isSuccess()) {
97 log << MSG::INFO <<
"get the DataInfoSvc" << endreq;
100 log << MSG::INFO <<
"get total event number for each run" << endreq;
102 log << MSG::WARNING <<
"could not get the DataInfoSvc. Ignore it." << endreq;
110 log << MSG::INFO <<
"finalize() in RootInterface" << endreq;
113 std::vector<TTree *>::const_iterator trees;
114 for (trees=m_outputTrees.begin();trees<m_outputTrees.end();trees++)
116 int treenr=(*trees)->GetUniqueID();
117 if (m_outputFiles[treenr]) {
118 if (!m_outputFiles[treenr]->IsOpen()) {
119 log << MSG::ERROR <<
"Could not open file for writing" << endreq;
120 return StatusCode::FAILURE;
122 log << MSG::DEBUG<<
" Closing file "<<treenr<<
", tree "<<(*trees)->GetName()<<endreq;
123 TDirectory *saveDir = gDirectory;
124 m_outputFiles[treenr]->cd();
127 TTree* m_jobInfoTree =
new TTree(
"JobInfoTree",
"Job info");
128 m_jobInfoTree->Branch(
"JobInfo",&jobInfo);
130 m_bossVer = getenv(
"BES_RELEASE");
131 log << MSG::INFO <<
"fill boss version: "<<m_bossVer << endreq;
134 m_jobOptions.push_back( tmpJobOptions );
136 if(m_decayOptions.size()==0)
144 m_jobInfoTree->Fill();
148 st = m_outputFiles[treenr]->Write();
150 log << MSG::FATAL<<
" can not write the file "<< m_outputFilenames[treenr].c_str()<<endreq;
154 m_outputFiles[treenr]->Close();
159 if(m_outputTrees.size()>0) m_outputTrees.clear();
160 return StatusCode::SUCCESS;
164 log << MSG::DEBUG <<
"addInput for Tree "<<treename<<endreq;
165 StatusCode sc=StatusCode::SUCCESS;
166 m_fileNames.push_back(file);
167 m_otherTrees.push_back(NULL);
168 inputFiles.push_back(NULL);
170 sc=getTreeNr(treename,treenr,
true);
171 m_inputFilenames[treenr]=file;
172 m_inputFiles[treenr]=NULL;
173 m_inputTrees[treenr]=NULL;
181 log << MSG::DEBUG <<
"addOutput for Tree "<<treename<<endreq;
182 StatusCode sc=StatusCode::SUCCESS;
184 sc=getTreeNr(treename,treenr,
true);
185 m_outputFilenames[treenr]=file;
186 m_outputFiles[treenr]=NULL;
187 m_outputTrees[treenr]=NULL;
188 m_splitModes[treenr]=split;
189 m_bufSizes[treenr]=bufsize;
190 m_compressionLevels[treenr]=compression;
195StatusCode
RootInterface::createBranch(
const std::string& treename,
const std::string& branchname ,
const char *classname,
void *addr,
int & branchnr){
197 log << MSG::DEBUG <<
"CreateBranch, Tree "<<treename<<
" branch "<<branchname<<endreq;
201 StatusCode sc=getTreeNr(treename,treenr);
202 if (!sc.isSuccess())
return sc;
204 if ( m_outputFilenames[treenr].empty() ) {
205 log << MSG::DEBUG <<
"No corresponding output file specified, ignore createBranch: "<<branchname<<endreq;
206 return StatusCode::SUCCESS;
209 if(!m_outputTrees[treenr]) sc=this->createTree(treenr,treename);
210 if (!sc.isSuccess())
return sc;
211 TTree * tree=m_outputTrees[treenr];
212 tree->SetUniqueID(treenr);
214 branch = tree->Branch(branchname.c_str(),classname,addr,m_bufSizes[treenr],m_splitModes[treenr]);
215 branch->SetUniqueID(treenr);
216 branchnr=m_branches->GetEntriesFast()+1;
217 m_branches->Expand(branchnr);
218 TClonesArray &a = *m_branches;
219 a[branchnr-1]=branch;
220 tree->SetBasketSize(branchname.c_str(),m_bufSizes[treenr]);
221 return StatusCode::SUCCESS;
225StatusCode RootInterface::createTree(
const unsigned int treenr,
const std::string treename)
229 TDirectory *saveDir = gDirectory;
232 m_outputFiles[treenr] =
new TFile(m_outputFilenames[treenr].c_str(),
"RECREATE");
233 if(m_outputFiles[treenr]->IsZombie()){
234 std::cout<<
"RootInterface ERROR::Can't not open file"<<m_outputFilenames[treenr].c_str()<<std::endl;
237 if (!m_outputFiles[treenr]->IsOpen()) {
238 log << MSG::FATAL <<
"ROOT file " << m_outputFilenames[treenr]
239 <<
" could not be opened for writing." << endreq;
241 return StatusCode::FAILURE;
243 log << MSG::INFO <<
"RootInterface::opened file for output:" << m_outputFilenames[treenr].c_str() <<endreq;
245 m_outputFiles[treenr]->cd();
246 m_outputFiles[treenr]->SetCompressionLevel(m_compressionLevels[treenr]);
247 std::string title=treename+
" from conversion";
248 m_outputTrees[treenr]=
new TTree(treename.c_str(), title.c_str());
249 TTree::SetMaxTreeSize(20000000000LL);
253 return StatusCode::SUCCESS;
256TTree * RootInterface::getTree(
const std::string treename)
260 log<<MSG::INFO<<
"RootInterface:;getTree"<<endreq;
262 getTreeNr(treename,treenr);
264 if (m_inputTrees[treenr])
return m_inputTrees[treenr];
265 if (!m_inputFiles[treenr] ) {
266 m_inputFiles[treenr] = TFile::Open(m_fileNames[treenr].
c_str(),
"READ");
267 if (!m_inputFiles[treenr]->IsOpen()) {
268 log << MSG::ERROR <<
"ROOT file " << m_inputFiles[treenr]->GetName()
269 <<
" could not be opened for reading." << endreq;
270 delete m_inputFiles[treenr];
271 m_inputFiles[treenr]=NULL;
276 log << MSG::INFO <<
"RootInterface::opened file for input:" << m_fileNames[treenr].c_str() <<endreq;
277 m_currentFileName = m_fileNames[treenr].c_str();
278 TTree *tree= (TTree *)m_inputFiles[treenr]->Get(treename.c_str());
280 log << MSG::ERROR <<
"ROOT file " << m_inputFiles[treenr]->GetName()
281 <<
" does not contain requested TTree: " << treename <<endreq;
284 if (tree->GetEntries()<=0)
286 log << MSG::ERROR <<
"ROOT file "<< m_inputFiles[treenr]->GetName()<<
" entries <= 0" << endreq;
290 m_inputTrees[treenr]=tree;
292 m_entries=(Int_t)tree->GetEntries();
302 TTree* tree2 = (TTree *)file->Get(
"JobInfoTree");
305 std::cout<<
"no JobInfoTree for file "<<file->GetName()<<std::endl;
310 log << MSG::INFO <<
"get JobInfoTree" << endreq;
311 TBranch* branch = tree2->GetBranch(
"JobInfo");
314 std::cout<<
"ERROR! No branch in JobInfoTree"<<std::endl;
320 branch->SetAddress(&jobInfo);
324 <<
"**************************************************" << std::endl
325 <<
"Print JobInfo for data file: "<< file->GetName()<<std::endl
326 <<
" BOSS version: "<< m_bossVer << std::endl
327 <<
"**************************************************" << std::endl
331 if(m_decayOptions.size()>0)
333 std::cout<< std::endl
334 <<
"**************************************************" << std::endl
335 <<
" Decay Options: "<<std::endl
336 <<m_decayOptions << std::endl
337 <<
"**************************************************" << std::endl
341 ISvcLocator* svcLocator = Gaudi::svcLocator();
344 StatusCode status = svcLocator->service(
"DataInfoSvc",tmpInfoSvc);
345 if (status.isSuccess()) {
346 log << MSG::INFO <<
"get the DataInfoSvc" << endreq;
347 jobInfoSvc=
dynamic_cast<DataInfoSvc *
>(tmpInfoSvc);
349 log << MSG::WARNING <<
"could not get the DataInfoSvc." << endreq;
358 <<
"**************************************************" << std::endl
359 <<
" JobOptions for this data file: " << std::endl
364 vector<std::string> vs = m_jobOptions;
368 for(
int i=0;i<nv;i++)
370 std::cout<<vs[i]<<std::endl;
371 std::cout<<
" end of the jobOptions file " << std::endl;
372 std::cout<<
"**************************************************" << std::endl
381TTree * RootInterface::getOtherTree(
const std::string treename){
383 log<<MSG::INFO<<
"RootInterface:;getOtherTree"<<endreq;
385 if(m_otherTrees[m_fileNum])
return m_otherTrees[m_fileNum];
387 inputFiles[m_fileNum] = TFile::Open(m_fileNames[m_fileNum].c_str(),
"READ");
389 if(!inputFiles[m_fileNum]->IsOpen()){
390 log<<MSG::ERROR<<
"ROOT File" <<inputFiles[m_fileNum]->GetName()<<
"Coult not be opened for reading."<<endreq;
391 delete inputFiles[m_fileNum];
392 inputFiles[m_fileNum] = NULL;
396 log<<MSG::INFO<<
"RootIntrFace:;Opened File for input:"<<m_fileNames[m_fileNum].c_str()<<endreq;
397 m_currentFileName = m_fileNames[m_fileNum].c_str();
399 TTree* tree =(TTree*)inputFiles[m_fileNum]->Get(treename.c_str());
401 log << MSG::ERROR <<
"ROOT file " << inputFiles[m_fileNum]->GetName()
402 <<
" does not contain requested TTree: " << treename <<endreq;
406 if(tree->GetEntries()<=0)
408 log << MSG::ERROR <<
"ROOT file "<< m_fileNames[m_fileNum].c_str()<<
" entries <= 0" << endreq;
412 m_otherTrees[m_fileNum] = tree;
414 m_entries=(Int_t)tree->GetEntries();
415 log<<MSG::INFO<<
"m_entries = "<<m_entries<<endreq;
427 if ( m_fileNum >=
int(m_fileNames.size())-1 ){
429 delete m_inputFiles[0];
430 m_inputFiles[0] = NULL;
435 (*m_branchesRead).Clear();
437 getTreeNr(
"Event",treenr);
438 if(m_inputFiles[treenr]){
439 delete m_inputFiles[treenr];
440 m_inputFiles[treenr] = NULL;
442 if(m_inputTrees[treenr]){
444 m_inputTrees[treenr] = NULL;
446 if(m_otherTrees[m_fileNum])
delete m_otherTrees[m_fileNum];
447 if(inputFiles[m_fileNum])
delete inputFiles[m_fileNum];
486 log << MSG::DEBUG <<
"RootInterface::setbranch address, treename: "<<treename <<
", branch "<<branchname <<endreq;
494 tree = getOtherTree(treename);
496 tree = getTree(treename);
501 log << MSG::ERROR <<
"Could not find tree " << treename <<endreq;
502 log << MSG::ERROR <<
"terminate the process handly"<<endreq;
504 return StatusCode::FAILURE;
506 tree->SetMakeClass(1);
509 TBranch *b = tree->GetBranch(branchname.c_str());
512 log << MSG::DEBUG <<
"Could not find branch xx" << branchname <<
"xx"<<endreq;
513 return StatusCode::FAILURE;
519 branchnr=m_branchesRead->GetEntries();
521 TClonesArray &a = *m_branchesRead;
522 m_branchesRead->Expand(branchnr+1);
524 return StatusCode::SUCCESS;
529 log << MSG::DEBUG <<
"RootInterface::getBranchEntry: "<<
", branch nr "<<nr <<
", entry "<<entry <<endreq;
531 if (nr <0)
return StatusCode::FAILURE;
532 TBranch *branch=(TBranch *)m_branchesRead->At(nr);
534 log << MSG::ERROR <<
"Could not find branch " << nr <<endreq;
535 return StatusCode::FAILURE;
538 branch->SetAddress(addr);
539 nb=branch->GetEntry(entry);
545 return StatusCode::SUCCESS;
551 log << MSG::DEBUG <<
"RootInterface::getBranchEntry: "<<
", branch nr "<<nr <<
", entry "<<entry <<endreq;
553 if (nr <0)
return StatusCode::FAILURE;
554 TBranch *branch=(TBranch *)m_branchesRead->At(nr);
556 log << MSG::ERROR <<
"Could not find branch " << nr <<endreq;
557 return StatusCode::FAILURE;
559 nb=branch->GetEntry(entry);
566 return StatusCode::SUCCESS;
569StatusCode RootInterface::getTreeNr(
const std::string treename,
unsigned int& treenr,
bool doAdd) {
572 std::vector<std::string>::iterator where=std::find(m_treenames.begin(),m_treenames.end(),treename);
573 if (where == m_treenames.end()) {
575 treenr=m_treenames.size();
576 m_treenames.push_back(treename);
577 m_inputFilenames.push_back(
"");
578 m_inputFiles.push_back(NULL);
579 m_inputTrees.push_back(NULL);
580 m_outputFilenames.push_back(
"");
581 m_outputFiles.push_back(NULL);
582 m_outputTrees.push_back(NULL);
583 m_splitModes.push_back(0);
584 m_bufSizes.push_back(0);
585 m_compressionLevels.push_back(0);
586 return StatusCode::SUCCESS;
588 log << MSG::ERROR <<
"Invalid tree name: " <<treename<< endreq;
589 return StatusCode::FAILURE;
592 treenr=where-m_treenames.begin();
593 return StatusCode::SUCCESS;
598 StatusCode sc=StatusCode::FAILURE;
600 std::vector<TTree *>::const_iterator trees;
601 for (trees=m_outputTrees.begin();trees<m_outputTrees.end();trees++) {
602 if ((*trees)==NULL)
continue;
603 int treenr=(*trees)->GetUniqueID();
604 if(m_outputFiles[treenr]->IsZombie()||(!m_outputFiles[treenr]->IsOpen())){
605 std::cout<<
"RootInterface ERROR::The ROOT File:"<<m_outputFilenames[treenr].c_str()<<
"status is false"<<std::endl;
609 m_outputFiles[treenr] = (*trees)->GetCurrentFile();
610 log << MSG::DEBUG <<
"filled tree "<<(* trees)->GetName() <<
" with "<<nb<<
" bytes"<< endreq;
612 log << MSG::FATAL <<
"Error in filling tree "<<(* trees)->GetName() <<
" with "<<nb<<
" bytes"<< endreq;
615 sc=StatusCode::SUCCESS;
625 const std::string& file,
626 int splitx,
int bufsize,
int compression){
627 log << MSG::INFO <<
"addOutput to single event" << endreq;
628 StatusCode status = StatusCode::FAILURE;
632 m_single_compressionLevels[treenr] = compression;
633 m_single_outputFileNames[treenr] = file;
634 m_single_outputFiles[treenr] = NULL;
635 m_single_outputTrees[treenr] = NULL;
636 m_single_splitModes[treenr] = splitx;
637 m_single_bufSizes[treenr] = bufsize;
639 std::cout <<
"finish f_addOutput to single event" << std::endl;
644 const std::string treename){
645 log << MSG::INFO <<
"f_createTree()" << endreq;
647 TDirectory *saveDir = gDirectory;
649 m_single_outputFiles[treenr] =
650 new TFile(m_single_outputFileNames[treenr].c_str(),
"RECREATE");
651 if ( !m_single_outputFiles[treenr]->IsOpen()){
652 log << MSG::ERROR <<
"ROOT share file: "
653 << m_single_outputFileNames[treenr]
654 <<
" could not be opened for writing"
656 return StatusCode::FAILURE;
658 log << MSG::INFO <<
"f_createTree()::open share file for writing: "
659 << m_single_outputFileNames[treenr] << endreq;
661 m_single_outputFiles[treenr]->cd();
662 m_single_outputFiles[treenr]->SetCompressionLevel(m_single_compressionLevels[treenr]);
664 std::string title = treename +
" for share";
665 m_single_outputTrees[treenr] =
new TTree(treename.c_str(), title.c_str());
668 return StatusCode::SUCCESS;
672 const std::string& branchname,
673 const char *classname,
674 void *addr,
int & branchnr){
675 log << MSG::INFO <<
"f_craeteBranch() create branch, tree name:"
676 << treename <<
", branch name:" << branchname << endreq;
681 if ( !status.isSuccess())
return status;
683 if ( !m_single_outputTrees[treenr])
685 if ( !status.isSuccess())
return status;
687 TTree* tree = m_single_outputTrees[treenr];
688 tree->SetUniqueID(treenr);
690 branch = tree->Branch(branchname.c_str(),
693 m_single_bufSizes[treenr],
694 m_single_splitModes[treenr]);
699 unsigned int& treenr,
bool doAdd){
701 std::vector<std::string>::iterator where =
702 std::find(m_single_treenames.begin(), m_single_treenames.end(), treename);
704 if ( where == m_single_treenames.end()){
706 treenr = m_single_treenames.size();
707 m_single_treenames.push_back(treename);
709 m_single_outputFileNames.push_back(
"");
710 m_single_outputFiles.push_back(NULL);
711 m_single_outputTrees.push_back(NULL);
712 m_single_splitModes.push_back(0);
713 m_single_bufSizes.push_back(0);
714 m_single_compressionLevels.push_back(0);
716 return StatusCode::SUCCESS;
719 log << MSG::ERROR <<
"Invalid share tree name: "
720 << treename << endreq;
721 return StatusCode::FAILURE;
724 treenr = where - m_single_treenames.begin();
725 return StatusCode::SUCCESS;
729 StatusCode status = StatusCode::FAILURE;
732 std::vector<TTree *>::const_iterator tree;
733 for ( tree = m_single_outputTrees.begin(); tree < m_single_outputTrees.end(); tree++){
734 if ( (*tree) == NULL)
continue;
735 byte = (*tree)->Fill();
737 log << MSG::INFO <<
"f_fillTrees() filled tree " << (*tree)->GetName()
738 <<
" with " <<
byte <<
" bytes!" << endreq;
739 status = StatusCode::SUCCESS;
746 log << MSG::INFO <<
"f_finalize() in RootInterface" << endreq;
748 std::vector<TTree *>::const_iterator tree;
749 for ( tree = m_single_outputTrees.begin(); tree < m_single_outputTrees.end(); tree++){
751 unsigned int treenr = (*tree)->GetUniqueID();
752 log << MSG::INFO <<
"tree id: " << treenr << endreq;
753 if ( m_single_outputFiles[treenr] ){
754 if ( !m_single_outputFiles[treenr]->IsOpen()){
755 log << MSG::ERROR <<
"f_finalize could not open share file for writing"
757 return StatusCode::FAILURE;
760 log << MSG::INFO <<
"Closing file:" << treenr
761 <<
", tree:" << (*tree)->GetName() << endreq;
763 TDirectory *saveDir = gDirectory;
764 m_single_outputFiles[treenr]->cd();
765 log <<MSG::INFO <<
"WREITE TO FILE BYTES: "
766 << m_single_outputFiles[treenr]->Write()
768 m_single_outputFiles[treenr]->Close();
774 return StatusCode::SUCCESS;
void setTotEvtNo(std::vector< int > i)
std::vector< int > getTotEvtNo()
virtual std::vector< int > getTotEvtNo()
RootInterface(MsgStream log)
virtual StatusCode f_createBranch(const std::string &treename, const std::string &branchname, const char *classname, void *addr, int &branchnr)
virtual StatusCode f_getTreeNr(const std::string treename, unsigned int &treenr, bool doAdd=false)
virtual StatusCode f_finalize()
virtual std::string getJobOptions()
virtual StatusCode getBranchEntry(int nr, int entry, int &nb)
get entry from this branch
virtual StatusCode f_fillTrees()
virtual StatusCode finalize()
virtual bool checkEndOfTree()
check if all the files is over 2005-11-28
virtual StatusCode createBranch(const std::string &tree, const std::string &branch, const char *classname, void *addr, int &branchnr)
create a branch in this tree
virtual void printJobInfo(TFile *file, int level)
virtual StatusCode setBranchAddress(const std::string treename, const std::string branchname, void *addr, int &nb)
set branch address
virtual StatusCode addInput(const std::string &treename, const std::string &file)
add input tree to the list
virtual StatusCode f_addOutput(const std::string &treename, const std::string &file, int splitx=1, int bufsize=64000, int compression=1)
static RootInterface * Instance(MsgStream log)
singleton behaviour
virtual StatusCode addOutput(const std::string &treename, const std::string &file, int splitx, int bufsize, int compression)
add output tree to the list
virtual StatusCode fillTrees()
fill in all trees
virtual StatusCode f_createTree(unsigned int treenr, const std::string treename)
virtual std::string getDecayOptions()
void setBossVer(string ver)
void setTotEvtNo(std::vector< int > i)
string getDecayOptions() const
void setDecayOptions(string opt)
vector< string > getJobOptions() const
std::vector< int > getTotEvtNo() const
string getBossVer() const
void setJobOptions(vector< string > opt)