BOSS 7.0.1
BESIII Offline Software System
Loading...
Searching...
No Matches
RootInterface.cxx
Go to the documentation of this file.
1#include "TFile.h"
2#include "TTree.h"
3#include "TBranch.h"
4#include "TChain.h"
5#include "TClonesArray.h"
6
7#include "RootCnvSvc/RootInterface.h"
8#include "RootEventData/TJobInfo.h"
9#include "DataInfoSvc/DataInfoSvc.h"
10#include "DataInfoSvc/IDataInfoSvc.h"
11
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"
17
18#include <algorithm> // for find
19#include <iostream>
20#include <fstream>
21#include <string>
22RootInterface* RootInterface::m_rootInterface = 0;
23
24
26 if (m_rootInterface) return m_rootInterface;
27 m_rootInterface=new RootInterface(log);
28 return m_rootInterface;
29}
30
31RootInterface::RootInterface(MsgStream str) : log(str)
32{
33
34 m_branches= new TClonesArray("TBranch",1);
35 m_branchesRead= new TClonesArray("TBranch",1);
36 m_entries=-1;
37 m_EOF=false;
38 m_ENDFILE = false ;
39 m_fileNum = 0; //-1
40 }
41
42
44{
45}
46
48{
49 IInterface* iface = Gaudi::createApplicationMgr();
50 SmartIF<IProperty> propMgr (iface );
51 std::string path;
52 propMgr->getProperty( "JobOptionsPath", path);
53 log << MSG::INFO << "JobOptions file for current job: " <<path << endreq;
54 ifstream fin(path.c_str());
55 string jobOptions;
56 string tempString;
57 while(getline(fin,tempString))
58 {
59 if( tempString.size()>0 && tempString.find("//")>tempString.size() )
60 {
61 jobOptions += tempString;
62 jobOptions += "\n";
63 }
64 }
65 log << MSG::INFO << "JobOptions: " << endreq
66 << jobOptions << endreq;
67 return jobOptions;
68}
69
71{
72 ISvcLocator* svcLocator = Gaudi::svcLocator();
73 IDataInfoSvc *tmpInfoSvc;
74 DataInfoSvc* jobInfoSvc;
75 string decayOptions;
76 StatusCode status = svcLocator->service("DataInfoSvc",tmpInfoSvc);
77 if (status.isSuccess()) {
78 log << MSG::INFO << "get the DataInfoSvc" << endreq;
79 jobInfoSvc=dynamic_cast<DataInfoSvc *>(tmpInfoSvc);
80 decayOptions = jobInfoSvc->getDecayOptions();
81 log << MSG::INFO << "get decay options" << endreq
82 << decayOptions << endreq;
83 }else {
84 log << MSG::WARNING << "could not get the DataInfoSvc. Ignore it." << endreq;
85 }
86 return decayOptions;
87}
88
90{
91 ISvcLocator* svcLocator = Gaudi::svcLocator();
92 IDataInfoSvc *tmpInfoSvc;
93 DataInfoSvc* jobInfoSvc;
94 std::vector<int> totEvtNo;
95 StatusCode status = svcLocator->service("DataInfoSvc",tmpInfoSvc);
96 if (status.isSuccess()) {
97 log << MSG::INFO << "get the DataInfoSvc" << endreq;
98 jobInfoSvc=dynamic_cast<DataInfoSvc *>(tmpInfoSvc);
99 totEvtNo = jobInfoSvc->getTotEvtNo();
100 log << MSG::INFO << "get total event number for each run" << endreq;
101 }else {
102 log << MSG::WARNING << "could not get the DataInfoSvc. Ignore it." << endreq;
103 }
104 return totEvtNo;
105}
106
108
109 // Get the messaging service, print where you are
110 log << MSG::INFO << "finalize() in RootInterface" << endreq;
111
112 // close file (FIXME for several output files)
113 std::vector<TTree *>::const_iterator trees;
114 for (trees=m_outputTrees.begin();trees<m_outputTrees.end();trees++)
115 if (*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;
121 } else {
122 log << MSG::DEBUG<<" Closing file "<<treenr<<", tree "<<(*trees)->GetName()<<endreq;
123 TDirectory *saveDir = gDirectory;
124 m_outputFiles[treenr]->cd();
125
126 TJobInfo* jobInfo = new TJobInfo;
127 TTree* m_jobInfoTree = new TTree("JobInfoTree","Job info");
128 m_jobInfoTree->Branch("JobInfo",&jobInfo);
129
130 m_bossVer = getenv("BES_RELEASE");
131 log << MSG::INFO << "fill boss version: "<<m_bossVer << endreq;
132
133 string tmpJobOptions = getJobOptions();
134 m_jobOptions.push_back( tmpJobOptions );
135
136 if(m_decayOptions.size()==0)
137 m_decayOptions = getDecayOptions();
138
139 m_totEvtNo = getTotEvtNo();
140 jobInfo->setBossVer(m_bossVer);
141 jobInfo->setJobOptions(m_jobOptions);
142 jobInfo->setDecayOptions(m_decayOptions);
143 jobInfo->setTotEvtNo(m_totEvtNo);
144 m_jobInfoTree->Fill();
145
146 //? m_mcFile->Write(0, TObject::kOverwrite);
147 int st =1;
148 st = m_outputFiles[treenr]->Write();
149 if(st==0){
150 log << MSG::FATAL<<" can not write the file "<< m_outputFilenames[treenr].c_str()<<endreq;
151 exit(1);
152 }
153
154 m_outputFiles[treenr]->Close();
155 saveDir->cd();
156 }
157 }
158 }
159 if(m_outputTrees.size()>0) m_outputTrees.clear();
160 return StatusCode::SUCCESS;
161}
162
163StatusCode RootInterface::addInput(const std::string& treename,const std::string& file) {
164 log << MSG::DEBUG << "addInput for Tree "<<treename<<endreq;
165 StatusCode sc=StatusCode::SUCCESS;
166 m_fileNames.push_back(file); //input files 2005-11-28
167 m_otherTrees.push_back(NULL);
168 inputFiles.push_back(NULL);
169 unsigned int treenr;
170 sc=getTreeNr(treename,treenr,true);
171 m_inputFilenames[treenr]=file; // the last one file is setted
172 m_inputFiles[treenr]=NULL;
173 m_inputTrees[treenr]=NULL;
174 m_currentFileName = m_fileNames[treenr].c_str();
175 return sc;
176}
177
178
179void RootInterface::setTagInputFile(std::vector<std::string> input)
180{
181
182 for(int i=0; i<input.size(); i++)
183 {
184 log << MSG::DEBUG <<"input tag file: "<<i<<" "<<input[i]<<endreq;
185 m_tagInputFile.push_back(input[i]);
186 }
187}
188
189StatusCode RootInterface::addOutput(const std::string& treename,const std::string& file,int split, int bufsize, int compression) {
190 static int i =0;
191 i ++;
192 log << MSG::DEBUG << "addOutput for Tree "<<treename<<endreq;
193 StatusCode sc=StatusCode::SUCCESS;
194 unsigned int treenr;
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;
202
203 return sc;
204}
205
206StatusCode RootInterface::createBranch(const std::string& treename,const std::string& branchname ,const char *classname,void *addr,int & branchnr){
207
208 log << MSG::DEBUG << "CreateBranch, Tree "<<treename<<" branch "<<branchname<<endreq;
209
210 TBranch *branch;
211 unsigned int treenr;
212 StatusCode sc=getTreeNr(treename,treenr);
213 if (!sc.isSuccess()) return sc;
214
215 if ( m_outputFilenames[treenr].empty() ) {
216 log << MSG::DEBUG << "No corresponding output file specified, ignore createBranch: "<<branchname<<endreq;
217 return StatusCode::SUCCESS;
218 }
219
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);
224
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]); //some problem with above method to set buffersize, so we set it here.
232 return StatusCode::SUCCESS;
233}
234
235
236StatusCode RootInterface::createTree(const unsigned int treenr,const std::string treename)
237{
238 // opens file and creates TTree on it
239
240 TDirectory *saveDir = gDirectory;
241
242 // Create the new ROOT file
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;
246 exit(1);
247 }
248 if (!m_outputFiles[treenr]->IsOpen()) {
249 log << MSG::FATAL << "ROOT file " << m_outputFilenames[treenr]
250 << " could not be opened for writing." << endreq;
251 exit(1);
252 return StatusCode::FAILURE;
253 }
254 log << MSG::INFO << "RootInterface::opened file for output:" << m_outputFilenames[treenr].c_str() <<endreq;
255
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);
261
262 saveDir->cd();
263
264 return StatusCode::SUCCESS;
265}
266
267TTree * RootInterface::getTree(const std::string treename)
268{
269
270 // get TTree for input
271 log<<MSG::INFO<<"RootInterface:;getTree"<<endreq;
272 unsigned int treenr;
273 getTreeNr(treename,treenr);
274
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;
283 m_EOF=true;
284 return NULL;
285 }
286 }
287 log << MSG::INFO << "RootInterface::opened file for input:" << m_fileNames[treenr].c_str() <<endreq;
288 m_currentFileName = m_fileNames[treenr].c_str(); //liangyt 2008-11-19
289 TTree *tree= (TTree *)m_inputFiles[treenr]->Get(treename.c_str());
290 if (!tree) {
291 log << MSG::ERROR << "ROOT file " << m_inputFiles[treenr]->GetName()
292 << " does not contain requested TTree: " << treename <<endreq;
293 return NULL;
294 }
295 if (tree->GetEntries()<=0)
296 {
297 log << MSG::ERROR << "ROOT file "<< m_inputFiles[treenr]->GetName()<< " entries <= 0" << endreq;
298 exit(1);
299 }
300
301 m_inputTrees[treenr]=tree;
302 if (m_entries<=0 ) {
303 m_entries=(Int_t)tree->GetEntries();
304 }
305
306 printJobInfo( m_inputFiles[treenr],1);
307
308 return tree;
309}
310
311void RootInterface::printJobInfo(TFile* file, int level)
312{
313 TTree* tree2 = (TTree *)file->Get("JobInfoTree");
314 if(!tree2)
315 {
316 std::cout<<"no JobInfoTree for file "<<file->GetName()<<std::endl;
317 exit(1);
318 }
319 else
320 {
321 log << MSG::INFO << "get JobInfoTree" << endreq;
322 TBranch* branch = tree2->GetBranch("JobInfo");
323 if(!branch)
324 {
325 std::cout<<"ERROR! No branch in JobInfoTree"<<std::endl;
326 exit(1);
327 }
328 else
329 {
330 TJobInfo* jobInfo= new TJobInfo;
331 branch->SetAddress(&jobInfo);
332 branch->GetEntry(0);
333 m_bossVer = jobInfo->getBossVer() ;
334 std::cout<<std::endl
335 << "**************************************************" << std::endl
336 <<"Print JobInfo for data file: "<< file->GetName()<<std::endl
337 << " BOSS version: "<< m_bossVer << std::endl
338 << "**************************************************" << std::endl
339 << std::endl;
340
341 m_decayOptions = jobInfo->getDecayOptions();
342 if(m_decayOptions.size()>0)
343 {
344 std::cout<< std::endl
345 <<"**************************************************" << std::endl
346 <<" Decay Options: "<<std::endl
347 <<m_decayOptions << std::endl
348 <<"**************************************************" << std::endl
349 << std::endl;
350 }
351
352 ISvcLocator* svcLocator = Gaudi::svcLocator();
353 IDataInfoSvc *tmpInfoSvc;
354 DataInfoSvc* jobInfoSvc;
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);
359 }else {
360 log << MSG::WARNING << "could not get the DataInfoSvc." << endreq;
361 }
362
363 m_totEvtNo = jobInfo->getTotEvtNo();
364 jobInfoSvc->setTotEvtNo(m_totEvtNo);
365
366 if(level>0)
367 {
368 std::cout<<std::endl
369 <<"**************************************************" << std::endl
370 <<" JobOptions for this data file: " << std::endl
371 << std::endl;
372
373
374 m_jobOptions = jobInfo->getJobOptions();
375 vector<std::string> vs = m_jobOptions;
376 int nv = vs.size();
377 if(nv>0)
378 {
379 for(int i=0;i<nv;i++)
380 {
381 std::cout<<vs[i]<<std::endl;
382 std::cout<<" end of the jobOptions file " << std::endl;
383 std::cout<<"**************************************************" << std::endl
384 <<std::endl;
385 }
386 }
387 }
388 }
389 }
390}
391
392TTree * RootInterface::getOtherTree(const std::string treename){
393 //get other TTree for input
394 log<<MSG::INFO<<"RootInterface:;getOtherTree"<<endreq;
395 m_ENDFILE = false ;
396 if(m_otherTrees[m_fileNum]) return m_otherTrees[m_fileNum];
397 // TFile* inputFile = new TFile(m_fileNames[m_fileNum].c_str(),"READ");
398 inputFiles[m_fileNum] = TFile::Open(m_fileNames[m_fileNum].c_str(),"READ");
399
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;
404 return NULL; // The Root can not be opened
405 }
406 m_EOF = false;
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(); //liangyt 2008-11-19
409
410 TTree* tree =(TTree*)inputFiles[m_fileNum]->Get(treename.c_str());//the same tree name;
411 if(!tree){
412 log << MSG::ERROR << "ROOT file " << inputFiles[m_fileNum]->GetName()
413 << " does not contain requested TTree: " << treename <<endreq;
414 return NULL;
415 }
416
417 if(tree->GetEntries()<=0)
418 {
419 log << MSG::ERROR << "ROOT file "<< m_fileNames[m_fileNum].c_str()<< " entries <= 0" << endreq;
420 exit(1);
421 }
422
423 m_otherTrees[m_fileNum] = tree;
424 if (m_entries<=0){
425 m_entries=(Int_t)tree->GetEntries();
426 log<<MSG::INFO<<"m_entries = "<<m_entries<<endreq;
427 }
428
429 printJobInfo(inputFiles[m_fileNum],0);
430
431 //delete inputFile;
432 // inputFile= NULL;
433 return tree;
434}
435
437
438 if ( m_fileNum >= int(m_fileNames.size())-1 ){
439 if(m_inputFiles[0]){
440 delete m_inputFiles[0];
441 m_inputFiles[0] = NULL;
442 }
443 return true;
444 }
445
446 (*m_branchesRead).Clear();
447 unsigned int treenr;
448 getTreeNr("Event",treenr);
449 if(m_inputFiles[treenr]){
450 delete m_inputFiles[treenr];
451 m_inputFiles[treenr] = NULL;
452 }
453 if(m_inputTrees[treenr]){
454 //delete m_inputTrees[treenr];
455 m_inputTrees[treenr] = NULL;
456 }
457 if(m_otherTrees[m_fileNum]) delete m_otherTrees[m_fileNum];
458 if(inputFiles[m_fileNum]) delete inputFiles[m_fileNum];
459
460 m_ENDFILE = true;
461 m_fileNum++;
462 m_currentFileName = m_fileNames[m_fileNum].c_str();
463
464 m_entries=-1;
465 m_EOF = false;
466 return false;
467}
468
469/*
470bool RootInterface::checkEndOfTree(){
471 static int fileNum = 1;
472
473 if (m_EOF){
474 if (fileNmu >m_fileNames.size())
475 return true; //End of all Files;
476 else{
477 Tfile inputFile = TFile::Open(m_fileNames[fileNum].c_str(),"READ");
478 TTree *tree;
479 if(!inputFile->IsOpen()){
480 log<<MSG::ERROR<<"ROOT File" <<inputFile->GetName()<<"Coult not be opened for reading."<<endreq;
481 delete inputFile;
482 tree = Null; // The Root can not be opened
483 }
484 log<<MSG::INFO<<"RootIntrFace:;Opened File for input:"<<m_fileNames[fileNum].c_str()<<endreq;
485 tree =(TTree*)m_fileNames[fileNum]->Get("Dst");//the same tree name;
486 if (m_entries<=0){
487 m_entries=(Int_t)tree->GetEntries();
488 }
489 return false;
490 }
491 }
492 return false;
493}
494*/
495
496StatusCode RootInterface::setBranchAddress(const std::string treename, const std::string branchname, void *addr,int &branchnr)
497{
498 log << MSG::DEBUG <<"RootInterface::setbranch address, treename: "<<treename <<", branch "<<branchname <<endreq;
499
500 branchnr=-1;
501
502 TTree * tree;
503
504 if(m_fileNum != 0){
505
506 tree = getOtherTree(treename);
507 }else{
508 tree = getTree(treename);
509 }
510
511 //TTree * tree = getTree("Dst");
512 if (!tree) {
513 log << MSG::ERROR << "Could not find tree " << treename <<endreq;
514 log << MSG::ERROR << "terminate the process handly"<<endreq;
515 exit(1);
516 return StatusCode::FAILURE;
517 }
518 tree->SetMakeClass(1); // necessary for separate branch reading (segv otherwise)!
519
520 // log << MSG::INFO <<"ok!!!!!!!!!!!!!11"<<endreq;
521 TBranch *b = tree->GetBranch(branchname.c_str());
522 if (!b) {
523 //tree->Print();
524 log << MSG::DEBUG << "Could not find branch xx" << branchname <<"xx"<<endreq;
525 return StatusCode::FAILURE;
526 }
527 // log << MSG::INFO <<"ok!!!!!!!!!!!!!22"<<endreq;
528 // log << MSG::INFO <<"ok!!!!!!!!!!!!!22"<<(*addr)<<endreq;
529 b->SetAddress(addr);
530 // log << MSG::INFO <<"ok!!!!!!!!!!!!!33"<<endreq;
531 branchnr=m_branchesRead->GetEntries();
532 // log << MSG::INFO <<"ok!!!!!!!!!!!!!44"<<endreq;
533 TClonesArray &a = *m_branchesRead;
534 m_branchesRead->Expand(branchnr+1);
535 a[branchnr]=b;
536 return StatusCode::SUCCESS;
537}
538
539StatusCode RootInterface::getBranchEntry(int nr, int entry, void * addr, int& nb)
540{
541 log << MSG::DEBUG <<"RootInterface::getBranchEntry: "<<", branch nr "<<nr <<", entry "<<entry <<endreq;
542
543 if (nr <0) return StatusCode::FAILURE;
544 TBranch *branch=(TBranch *)m_branchesRead->At(nr);
545 if (!branch) {
546 log << MSG::ERROR << "Could not find branch " << nr <<endreq;
547 return StatusCode::FAILURE;
548 }
549
550 branch->SetAddress(addr);
551 nb=branch->GetEntry(entry);
552
553 if (nb<=0){
554 m_EOF=true;
555
556 }
557 return StatusCode::SUCCESS;
558}
559
560
561StatusCode RootInterface::getBranchEntry(int nr, int entry, int& nb)
562{
563 log << MSG::DEBUG <<"RootInterface::getBranchEntry: "<<", branch nr "<<nr <<", entry "<<entry <<endreq;
564
565 if (nr <0) return StatusCode::FAILURE;
566 TBranch *branch=(TBranch *)m_branchesRead->At(nr);
567 if (!branch) {
568 log << MSG::ERROR << "Could not find branch " << nr <<endreq;
569 return StatusCode::FAILURE;
570 }
571 nb=branch->GetEntry(entry);
572
573 if (nb<=0){
574 m_EOF=true;
575
576 }
577
578 return StatusCode::SUCCESS;
579}
580
581StatusCode RootInterface::getTreeNr(const std::string treename,unsigned int& treenr,bool doAdd) {
582 // look whether this tree has already got a number
583 // if not, add it
584 std::vector<std::string>::iterator where=std::find(m_treenames.begin(),m_treenames.end(),treename);
585 if (where == m_treenames.end()) {
586 if (doAdd) {
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;
599 }else {
600 log << MSG::ERROR << "Invalid tree name: " <<treename<< endreq;
601 return StatusCode::FAILURE;
602 }
603 }
604 treenr=where-m_treenames.begin();
605 return StatusCode::SUCCESS;
606}
607
609 // loop over all trees and fill them
610 StatusCode sc=StatusCode::FAILURE;
611 int nb;
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;
618 exit(1);
619 }
620 nb=(*trees)->Fill();
621 m_outputFiles[treenr] = (*trees)->GetCurrentFile();
622 log << MSG::DEBUG << "filled tree "<<(* trees)->GetName() <<" with "<<nb<<" bytes"<< endreq;
623 if(nb==-1){
624 log << MSG::FATAL << "Error in filling tree "<<(* trees)->GetName() <<" with "<<nb<<" bytes"<< endreq;
625 exit(1);
626 }
627 sc=StatusCode::SUCCESS;
628 }
629 return sc;
630}
631
632
633
634
635
636StatusCode RootInterface::f_addOutput(const std::string& treename,
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;
641 unsigned int treenr;
642
643 status = f_getTreeNr(treename, treenr, true);
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;
650
651 std::cout << "finish f_addOutput to single event" << std::endl;
652 return status;
653}
654
655StatusCode RootInterface::f_createTree(unsigned int treenr,
656 const std::string treename){
657 log << MSG::INFO << "f_createTree()" << endreq;
658
659 TDirectory *saveDir = gDirectory;
660
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"
667 << endreq;
668 return StatusCode::FAILURE;
669 }
670 log << MSG::INFO << "f_createTree()::open share file for writing: "
671 << m_single_outputFileNames[treenr] << endreq;
672
673 m_single_outputFiles[treenr]->cd();
674 m_single_outputFiles[treenr]->SetCompressionLevel(m_single_compressionLevels[treenr]);
675
676 std::string title = treename + " for share";
677 m_single_outputTrees[treenr] = new TTree(treename.c_str(), title.c_str());
678 saveDir->cd();
679
680 return StatusCode::SUCCESS;
681}
682
683StatusCode RootInterface::f_createBranch(const std::string& treename,
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;
689
690 TBranch *branch;
691 unsigned int treenr;
692 StatusCode status = f_getTreeNr(treename, treenr);
693 if ( !status.isSuccess()) return status;
694
695 if ( !m_single_outputTrees[treenr])
696 status = this->f_createTree(treenr, treename);
697 if ( !status.isSuccess()) return status;
698
699 TTree* tree = m_single_outputTrees[treenr];
700 tree->SetUniqueID(treenr);
701
702 branch = tree->Branch(branchname.c_str(),
703 classname,
704 addr,
705 m_single_bufSizes[treenr],
706 m_single_splitModes[treenr]);
707
708}
709
710StatusCode RootInterface::f_getTreeNr(const std::string treename,
711 unsigned int& treenr,bool doAdd){
712
713 std::vector<std::string>::iterator where =
714 std::find(m_single_treenames.begin(), m_single_treenames.end(), treename);
715
716 if ( where == m_single_treenames.end()){
717 if ( doAdd){
718 treenr = m_single_treenames.size();
719 m_single_treenames.push_back(treename);
720
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);
727
728 return StatusCode::SUCCESS;
729 }
730 else {
731 log << MSG::ERROR << "Invalid share tree name: "
732 << treename << endreq;
733 return StatusCode::FAILURE;
734 }
735 }
736 treenr = where - m_single_treenames.begin();
737 return StatusCode::SUCCESS;
738}
739
741 StatusCode status = StatusCode::FAILURE;
742 int byte;
743
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();
748 (*tree)->Print();
749 log << MSG::INFO << "f_fillTrees() filled tree " << (*tree)->GetName()
750 << " with " << byte << " bytes!" << endreq;
751 status = StatusCode::SUCCESS;
752 }
753
754 return status;
755}
756
758 log << MSG::INFO << "f_finalize() in RootInterface" << endreq;
759
760 std::vector<TTree *>::const_iterator tree;
761 for ( tree = m_single_outputTrees.begin(); tree < m_single_outputTrees.end(); tree++){
762 if ( *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"
768 << endreq;
769 return StatusCode::FAILURE;
770 }
771 else {
772 log << MSG::INFO << "Closing file:" << treenr
773 << ", tree:" << (*tree)->GetName() << endreq;
774
775 TDirectory *saveDir = gDirectory;
776 m_single_outputFiles[treenr]->cd();
777 log <<MSG::INFO << "WREITE TO FILE BYTES: "
778 << m_single_outputFiles[treenr]->Write()
779 << endreq;
780 m_single_outputFiles[treenr]->Close();
781 saveDir->cd();
782 }
783 }
784 }
785 }
786 return StatusCode::SUCCESS;
787}
char * file
Definition: DQA_TO_DB.cxx:15
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)
virtual ~RootInterface()
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()
char * c_str(Index i)
Definition: EvtCyclic3.cc:252