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