5#include "TClonesArray.h"
7#include "RootCnvSvc/RootInterface.h"
8#include "RootEventData/TJobInfo.h"
9#include "DataInfoSvc/DataInfoSvc.h"
10#include "DataInfoSvc/IDataInfoSvc.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;
174 m_currentFileName = m_fileNames[treenr].c_str();
182 for(
int i=0; i<input.size(); i++)
184 log << MSG::DEBUG <<
"input tag file: "<<i<<
" "<<input[i]<<endreq;
185 m_tagInputFile.push_back(input[i]);
192 log << MSG::DEBUG <<
"addOutput for Tree "<<treename<<endreq;
193 StatusCode sc=StatusCode::SUCCESS;
195 sc=getTreeNr(treename,treenr,
true);
196 m_outputFilenames[treenr]=
file;
197 m_outputFiles[treenr]=NULL;
198 m_outputTrees[treenr]=NULL;
199 m_splitModes[treenr]=split;
200 m_bufSizes[treenr]=bufsize;
201 m_compressionLevels[treenr]=compression;
206StatusCode
RootInterface::createBranch(
const std::string& treename,
const std::string& branchname ,
const char *classname,
void *addr,
int & branchnr){
208 log << MSG::DEBUG <<
"CreateBranch, Tree "<<treename<<
" branch "<<branchname<<endreq;
212 StatusCode sc=getTreeNr(treename,treenr);
213 if (!sc.isSuccess())
return sc;
215 if ( m_outputFilenames[treenr].empty() ) {
216 log << MSG::DEBUG <<
"No corresponding output file specified, ignore createBranch: "<<branchname<<endreq;
217 return StatusCode::SUCCESS;
220 if(!m_outputTrees[treenr]) sc=this->createTree(treenr,treename);
221 if (!sc.isSuccess())
return sc;
222 TTree * tree=m_outputTrees[treenr];
223 tree->SetUniqueID(treenr);
225 branch = tree->Branch(branchname.c_str(),classname,addr,m_bufSizes[treenr],m_splitModes[treenr]);
226 branch->SetUniqueID(treenr);
227 branchnr=m_branches->GetEntriesFast()+1;
228 m_branches->Expand(branchnr);
229 TClonesArray &a = *m_branches;
230 a[branchnr-1]=branch;
231 tree->SetBasketSize(branchname.c_str(),m_bufSizes[treenr]);
232 return StatusCode::SUCCESS;
236StatusCode RootInterface::createTree(
const unsigned int treenr,
const std::string treename)
240 TDirectory *saveDir = gDirectory;
243 m_outputFiles[treenr] =
new TFile(m_outputFilenames[treenr].c_str(),
"RECREATE");
244 if(m_outputFiles[treenr]->IsZombie()){
245 std::cout<<
"RootInterface ERROR::Can't not open file"<<m_outputFilenames[treenr].c_str()<<std::endl;
248 if (!m_outputFiles[treenr]->IsOpen()) {
249 log << MSG::FATAL <<
"ROOT file " << m_outputFilenames[treenr]
250 <<
" could not be opened for writing." << endreq;
252 return StatusCode::FAILURE;
254 log << MSG::INFO <<
"RootInterface::opened file for output:" << m_outputFilenames[treenr].c_str() <<endreq;
256 m_outputFiles[treenr]->cd();
257 m_outputFiles[treenr]->SetCompressionLevel(m_compressionLevels[treenr]);
258 std::string
title=treename+
" from conversion";
259 m_outputTrees[treenr]=
new TTree(treename.c_str(),
title.c_str());
260 TTree::SetMaxTreeSize(20000000000LL);
264 return StatusCode::SUCCESS;
267TTree * RootInterface::getTree(
const std::string treename)
271 log<<MSG::INFO<<
"RootInterface:;getTree"<<endreq;
273 getTreeNr(treename,treenr);
275 if (m_inputTrees[treenr])
return m_inputTrees[treenr];
276 if (!m_inputFiles[treenr] ) {
277 m_inputFiles[treenr] = TFile::Open(m_fileNames[treenr].
c_str(),
"READ");
278 if (!m_inputFiles[treenr]->IsOpen()) {
279 log << MSG::ERROR <<
"ROOT file " << m_inputFiles[treenr]->GetName()
280 <<
" could not be opened for reading." << endreq;
281 delete m_inputFiles[treenr];
282 m_inputFiles[treenr]=NULL;
287 log << MSG::INFO <<
"RootInterface::opened file for input:" << m_fileNames[treenr].c_str() <<endreq;
288 m_currentFileName = m_fileNames[treenr].c_str();
289 TTree *tree= (TTree *)m_inputFiles[treenr]->Get(treename.c_str());
291 log << MSG::ERROR <<
"ROOT file " << m_inputFiles[treenr]->GetName()
292 <<
" does not contain requested TTree: " << treename <<endreq;
295 if (tree->GetEntries()<=0)
297 log << MSG::ERROR <<
"ROOT file "<< m_inputFiles[treenr]->GetName()<<
" entries <= 0" << endreq;
301 m_inputTrees[treenr]=tree;
303 m_entries=(Int_t)tree->GetEntries();
313 TTree* tree2 = (TTree *)
file->Get(
"JobInfoTree");
316 std::cout<<
"no JobInfoTree for file "<<
file->GetName()<<std::endl;
321 log << MSG::INFO <<
"get JobInfoTree" << endreq;
322 TBranch* branch = tree2->GetBranch(
"JobInfo");
325 std::cout<<
"ERROR! No branch in JobInfoTree"<<std::endl;
331 branch->SetAddress(&jobInfo);
335 <<
"**************************************************" << std::endl
336 <<
"Print JobInfo for data file: "<<
file->GetName()<<std::endl
337 <<
" BOSS version: "<< m_bossVer << std::endl
338 <<
"**************************************************" << std::endl
342 if(m_decayOptions.size()>0)
344 std::cout<< std::endl
345 <<
"**************************************************" << std::endl
346 <<
" Decay Options: "<<std::endl
347 <<m_decayOptions << std::endl
348 <<
"**************************************************" << std::endl
352 ISvcLocator* svcLocator = Gaudi::svcLocator();
355 StatusCode status = svcLocator->service(
"DataInfoSvc",tmpInfoSvc);
356 if (status.isSuccess()) {
357 log << MSG::INFO <<
"get the DataInfoSvc" << endreq;
358 jobInfoSvc=
dynamic_cast<DataInfoSvc *
>(tmpInfoSvc);
360 log << MSG::WARNING <<
"could not get the DataInfoSvc." << endreq;
369 <<
"**************************************************" << std::endl
370 <<
" JobOptions for this data file: " << std::endl
375 vector<std::string> vs = m_jobOptions;
379 for(
int i=0;i<nv;i++)
381 std::cout<<vs[i]<<std::endl;
382 std::cout<<
" end of the jobOptions file " << std::endl;
383 std::cout<<
"**************************************************" << std::endl
392TTree * RootInterface::getOtherTree(
const std::string treename){
394 log<<MSG::INFO<<
"RootInterface:;getOtherTree"<<endreq;
396 if(m_otherTrees[m_fileNum])
return m_otherTrees[m_fileNum];
398 inputFiles[m_fileNum] = TFile::Open(m_fileNames[m_fileNum].c_str(),
"READ");
400 if(!inputFiles[m_fileNum]->IsOpen()){
401 log<<MSG::ERROR<<
"ROOT File" <<inputFiles[m_fileNum]->GetName()<<
"Coult not be opened for reading."<<endreq;
402 delete inputFiles[m_fileNum];
403 inputFiles[m_fileNum] = NULL;
407 log<<MSG::INFO<<
"RootIntrFace:;Opened File for input:"<<m_fileNames[m_fileNum].c_str()<<endreq;
408 m_currentFileName = m_fileNames[m_fileNum].c_str();
410 TTree* tree =(TTree*)inputFiles[m_fileNum]->Get(treename.c_str());
412 log << MSG::ERROR <<
"ROOT file " << inputFiles[m_fileNum]->GetName()
413 <<
" does not contain requested TTree: " << treename <<endreq;
417 if(tree->GetEntries()<=0)
419 log << MSG::ERROR <<
"ROOT file "<< m_fileNames[m_fileNum].c_str()<<
" entries <= 0" << endreq;
423 m_otherTrees[m_fileNum] = tree;
425 m_entries=(Int_t)tree->GetEntries();
426 log<<MSG::INFO<<
"m_entries = "<<m_entries<<endreq;
438 if ( m_fileNum >=
int(m_fileNames.size())-1 ){
440 delete m_inputFiles[0];
441 m_inputFiles[0] = NULL;
446 (*m_branchesRead).Clear();
448 getTreeNr(
"Event",treenr);
449 if(m_inputFiles[treenr]){
450 delete m_inputFiles[treenr];
451 m_inputFiles[treenr] = NULL;
453 if(m_inputTrees[treenr]){
455 m_inputTrees[treenr] = NULL;
457 if(m_otherTrees[m_fileNum])
delete m_otherTrees[m_fileNum];
458 if(inputFiles[m_fileNum])
delete inputFiles[m_fileNum];
462 m_currentFileName = m_fileNames[m_fileNum].c_str();
498 log << MSG::DEBUG <<
"RootInterface::setbranch address, treename: "<<treename <<
", branch "<<branchname <<endreq;
506 tree = getOtherTree(treename);
508 tree = getTree(treename);
513 log << MSG::ERROR <<
"Could not find tree " << treename <<endreq;
514 log << MSG::ERROR <<
"terminate the process handly"<<endreq;
516 return StatusCode::FAILURE;
518 tree->SetMakeClass(1);
521 TBranch *b = tree->GetBranch(branchname.c_str());
524 log << MSG::DEBUG <<
"Could not find branch xx" << branchname <<
"xx"<<endreq;
525 return StatusCode::FAILURE;
531 branchnr=m_branchesRead->GetEntries();
533 TClonesArray &a = *m_branchesRead;
534 m_branchesRead->Expand(branchnr+1);
536 return StatusCode::SUCCESS;
541 log << MSG::DEBUG <<
"RootInterface::getBranchEntry: "<<
", branch nr "<<nr <<
", entry "<<entry <<endreq;
543 if (nr <0)
return StatusCode::FAILURE;
544 TBranch *branch=(TBranch *)m_branchesRead->At(nr);
546 log << MSG::ERROR <<
"Could not find branch " << nr <<endreq;
547 return StatusCode::FAILURE;
550 branch->SetAddress(addr);
551 nb=branch->GetEntry(entry);
557 return StatusCode::SUCCESS;
563 log << MSG::DEBUG <<
"RootInterface::getBranchEntry: "<<
", branch nr "<<nr <<
", entry "<<entry <<endreq;
565 if (nr <0)
return StatusCode::FAILURE;
566 TBranch *branch=(TBranch *)m_branchesRead->At(nr);
568 log << MSG::ERROR <<
"Could not find branch " << nr <<endreq;
569 return StatusCode::FAILURE;
571 nb=branch->GetEntry(entry);
578 return StatusCode::SUCCESS;
581StatusCode RootInterface::getTreeNr(
const std::string treename,
unsigned int& treenr,
bool doAdd) {
584 std::vector<std::string>::iterator where=std::find(m_treenames.begin(),m_treenames.end(),treename);
585 if (where == m_treenames.end()) {
587 treenr=m_treenames.size();
588 m_treenames.push_back(treename);
589 m_inputFilenames.push_back(
"");
590 m_inputFiles.push_back(NULL);
591 m_inputTrees.push_back(NULL);
592 m_outputFilenames.push_back(
"");
593 m_outputFiles.push_back(NULL);
594 m_outputTrees.push_back(NULL);
595 m_splitModes.push_back(0);
596 m_bufSizes.push_back(0);
597 m_compressionLevels.push_back(0);
598 return StatusCode::SUCCESS;
600 log << MSG::ERROR <<
"Invalid tree name: " <<treename<< endreq;
601 return StatusCode::FAILURE;
604 treenr=where-m_treenames.begin();
605 return StatusCode::SUCCESS;
610 StatusCode sc=StatusCode::FAILURE;
612 std::vector<TTree *>::const_iterator trees;
613 for (trees=m_outputTrees.begin();trees<m_outputTrees.end();trees++) {
614 if ((*trees)==NULL)
continue;
615 int treenr=(*trees)->GetUniqueID();
616 if(m_outputFiles[treenr]->IsZombie()||(!m_outputFiles[treenr]->IsOpen())){
617 std::cout<<
"RootInterface ERROR::The ROOT File:"<<m_outputFilenames[treenr].c_str()<<
"status is false"<<std::endl;
621 m_outputFiles[treenr] = (*trees)->GetCurrentFile();
622 log << MSG::DEBUG <<
"filled tree "<<(* trees)->GetName() <<
" with "<<nb<<
" bytes"<< endreq;
624 log << MSG::FATAL <<
"Error in filling tree "<<(* trees)->GetName() <<
" with "<<nb<<
" bytes"<< endreq;
627 sc=StatusCode::SUCCESS;
637 const std::string&
file,
638 int splitx,
int bufsize,
int compression){
639 log << MSG::INFO <<
"addOutput to single event" << endreq;
640 StatusCode status = StatusCode::FAILURE;
644 m_single_compressionLevels[treenr] = compression;
645 m_single_outputFileNames[treenr] =
file;
646 m_single_outputFiles[treenr] = NULL;
647 m_single_outputTrees[treenr] = NULL;
648 m_single_splitModes[treenr] = splitx;
649 m_single_bufSizes[treenr] = bufsize;
651 std::cout <<
"finish f_addOutput to single event" << std::endl;
656 const std::string treename){
657 log << MSG::INFO <<
"f_createTree()" << endreq;
659 TDirectory *saveDir = gDirectory;
661 m_single_outputFiles[treenr] =
662 new TFile(m_single_outputFileNames[treenr].c_str(),
"RECREATE");
663 if ( !m_single_outputFiles[treenr]->IsOpen()){
664 log << MSG::ERROR <<
"ROOT share file: "
665 << m_single_outputFileNames[treenr]
666 <<
" could not be opened for writing"
668 return StatusCode::FAILURE;
670 log << MSG::INFO <<
"f_createTree()::open share file for writing: "
671 << m_single_outputFileNames[treenr] << endreq;
673 m_single_outputFiles[treenr]->cd();
674 m_single_outputFiles[treenr]->SetCompressionLevel(m_single_compressionLevels[treenr]);
676 std::string
title = treename +
" for share";
677 m_single_outputTrees[treenr] =
new TTree(treename.c_str(),
title.c_str());
680 return StatusCode::SUCCESS;
684 const std::string& branchname,
685 const char *classname,
686 void *addr,
int & branchnr){
687 log << MSG::INFO <<
"f_craeteBranch() create branch, tree name:"
688 << treename <<
", branch name:" << branchname << endreq;
693 if ( !status.isSuccess())
return status;
695 if ( !m_single_outputTrees[treenr])
697 if ( !status.isSuccess())
return status;
699 TTree* tree = m_single_outputTrees[treenr];
700 tree->SetUniqueID(treenr);
702 branch = tree->Branch(branchname.c_str(),
705 m_single_bufSizes[treenr],
706 m_single_splitModes[treenr]);
711 unsigned int& treenr,
bool doAdd){
713 std::vector<std::string>::iterator where =
714 std::find(m_single_treenames.begin(), m_single_treenames.end(), treename);
716 if ( where == m_single_treenames.end()){
718 treenr = m_single_treenames.size();
719 m_single_treenames.push_back(treename);
721 m_single_outputFileNames.push_back(
"");
722 m_single_outputFiles.push_back(NULL);
723 m_single_outputTrees.push_back(NULL);
724 m_single_splitModes.push_back(0);
725 m_single_bufSizes.push_back(0);
726 m_single_compressionLevels.push_back(0);
728 return StatusCode::SUCCESS;
731 log << MSG::ERROR <<
"Invalid share tree name: "
732 << treename << endreq;
733 return StatusCode::FAILURE;
736 treenr = where - m_single_treenames.begin();
737 return StatusCode::SUCCESS;
741 StatusCode status = StatusCode::FAILURE;
744 std::vector<TTree *>::const_iterator tree;
745 for ( tree = m_single_outputTrees.begin(); tree < m_single_outputTrees.end(); tree++){
746 if ( (*tree) == NULL)
continue;
747 byte = (*tree)->Fill();
749 log << MSG::INFO <<
"f_fillTrees() filled tree " << (*tree)->GetName()
750 <<
" with " <<
byte <<
" bytes!" << endreq;
751 status = StatusCode::SUCCESS;
758 log << MSG::INFO <<
"f_finalize() in RootInterface" << endreq;
760 std::vector<TTree *>::const_iterator tree;
761 for ( tree = m_single_outputTrees.begin(); tree < m_single_outputTrees.end(); tree++){
763 unsigned int treenr = (*tree)->GetUniqueID();
764 log << MSG::INFO <<
"tree id: " << treenr << endreq;
765 if ( m_single_outputFiles[treenr] ){
766 if ( !m_single_outputFiles[treenr]->IsOpen()){
767 log << MSG::ERROR <<
"f_finalize could not open share file for writing"
769 return StatusCode::FAILURE;
772 log << MSG::INFO <<
"Closing file:" << treenr
773 <<
", tree:" << (*tree)->GetName() << endreq;
775 TDirectory *saveDir = gDirectory;
776 m_single_outputFiles[treenr]->cd();
777 log <<MSG::INFO <<
"WREITE TO FILE BYTES: "
778 << m_single_outputFiles[treenr]->Write()
780 m_single_outputFiles[treenr]->Close();
786 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 void setTagInputFile(std::vector< std::string > input)
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)