StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ChainMerger.cxx
1 //ChainMerger.cxx
2 
3 //std
4 #include "Stiostream.h"
5 #include <string>
6 #include <ctime>
7 #include <stdlib.h>
8 
9 //ROOT
10 #include "TSystem.h"
11 #include "TROOT.h"
12 #include "TChain.h"
13 #include "TTree.h"
14 #include "TFile.h"
15 
16 //UpsilonAna
17 #include "StJetMuEvent.h"
18 #include "ChainMerger.h"
19 
20 ClassImp(ChainMerger)
21 
22 ChainMerger::ChainMerger(const char* dir, const char* outfile)
23  : mChain(new TChain("mTree")), mEvent(new StJetMuEvent())
24 {
25  cout<<"ChainMerger::ChainMerger()"<<endl;
26  buildChain(string(dir), string(outfile));
27 }
28 
29 ChainMerger::~ChainMerger()
30 {
31  cout<<"ChainMerger::~ChainMerger()"<<endl;
32 }
33 
34 void ChainMerger::buildChain(string dir, string outfile)
35 {
36  cout <<"ChainMerger::buildChain(string, string)"<<endl;
37 
38  cout <<"SetBranchAddress"<<endl;
39  mChain->SetBranchAddress("StJetMuEvent",&mEvent);
40 
41  clock_t begin = clock();
42 
43  cout <<"MakeChain()"<<endl;
44  void *pDir = gSystem->OpenDirectory(dir.c_str());
45 
46  // now find the files that end in the specified extention
47  const char* fileName(0);
48  int fileCount=0;
49  int total=0;
50 
51  while((fileName = gSystem->GetDirEntry(pDir))) { //loop on files
52 
53  string file(fileName);
54  if ( (file.find(".MuDst.root")!=file.npos) && (file.find("_has_")!=file.npos) ) {
55  int n=findEntries(file);
56  cout <<"file number "<<fileCount<<":\t"<<fileName<<"\twith nEntries:\t"<<n<<endl;
57  ++fileCount;
58  mChain->AddFile(file.c_str(), n);
59  total+=n;
60  }
61  }
62  cout <<"\n TChain::GetEntries():\t"<<mChain->GetEntries()<<"\t total:\t"<<total<<endl;
63 
64  cout <<"Merge chain"<<endl;
65  mChain->Merge(outfile.c_str());
66 
67  clock_t end = clock();
68  cout <<"\n Elapsed Time:\t"<<static_cast<double>(end-begin)/static_cast<double>(CLOCKS_PER_SEC)<<endl;
69 }
70 
71 int ChainMerger::findEntries(string infile)
72 {
73  //cout <<"infile:\t"<<infile<<endl;
74  string begin = "_has_";
75  string end = "_events";
76  int where1 = infile.find(begin);
77  int where2 = infile.find(end);
78  //int npos = infile.npos; //for compiled code
79  //int npos=-1 //for interpreted code
80 
81  //cout <<"npos:\t"<<npos<<"\twhere1:\t"<<where1<<"\twhere2:\t"<<where2<<endl;
82 
83  int start=where1+begin.size();
84  int stop=where2;
85 
86  //cout <<"numbers of events is between indices: ["<<start<<","<<stop<<")"<<endl;
87 
88  //cout <<"Get number of events"<<endl;
89 
90  string number;
91  for (int i=start; i<stop; ++i) {
92  number += infile[i];
93  }
94 
95  int nevents = atoi(number.c_str());
96 
97  //cout <<"Number of events as string:\t"<<number<<endl;
98  //cout <<"Number of events as int: \t"<<nevents<<endl;
99 
100  //cout <<"Done"<<endl;
101 
102  return nevents;
103 }