00001
00002
00003
00004 #include "Stiostream.h"
00005 #include <string>
00006 #include <ctime>
00007 #include <stdlib.h>
00008
00009
00010 #include "TSystem.h"
00011 #include "TROOT.h"
00012 #include "TChain.h"
00013 #include "TTree.h"
00014 #include "TFile.h"
00015
00016
00017 #include "StJetMuEvent.h"
00018 #include "ChainMerger.h"
00019
00020 ClassImp(ChainMerger)
00021
00022 ChainMerger::ChainMerger(const char* dir, const char* outfile)
00023 : mChain(new TChain("mTree")), mEvent(new StJetMuEvent())
00024 {
00025 cout<<"ChainMerger::ChainMerger()"<<endl;
00026 buildChain(string(dir), string(outfile));
00027 }
00028
00029 ChainMerger::~ChainMerger()
00030 {
00031 cout<<"ChainMerger::~ChainMerger()"<<endl;
00032 }
00033
00034 void ChainMerger::buildChain(string dir, string outfile)
00035 {
00036 cout <<"ChainMerger::buildChain(string, string)"<<endl;
00037
00038 cout <<"SetBranchAddress"<<endl;
00039 mChain->SetBranchAddress("StJetMuEvent",&mEvent);
00040
00041 clock_t begin = clock();
00042
00043 cout <<"MakeChain()"<<endl;
00044 void *pDir = gSystem->OpenDirectory(dir.c_str());
00045
00046
00047 const char* fileName(0);
00048 int fileCount=0;
00049 int total=0;
00050
00051 while((fileName = gSystem->GetDirEntry(pDir))) {
00052
00053 string file(fileName);
00054 if ( (file.find(".MuDst.root")!=file.npos) && (file.find("_has_")!=file.npos) ) {
00055 int n=findEntries(file);
00056 cout <<"file number "<<fileCount<<":\t"<<fileName<<"\twith nEntries:\t"<<n<<endl;
00057 ++fileCount;
00058 mChain->AddFile(file.c_str(), n);
00059 total+=n;
00060 }
00061 }
00062 cout <<"\n TChain::GetEntries():\t"<<mChain->GetEntries()<<"\t total:\t"<<total<<endl;
00063
00064 cout <<"Merge chain"<<endl;
00065 mChain->Merge(outfile.c_str());
00066
00067 clock_t end = clock();
00068 cout <<"\n Elapsed Time:\t"<<static_cast<double>(end-begin)/static_cast<double>(CLOCKS_PER_SEC)<<endl;
00069 }
00070
00071 int ChainMerger::findEntries(string infile)
00072 {
00073
00074 string begin = "_has_";
00075 string end = "_events";
00076 int where1 = infile.find(begin);
00077 int where2 = infile.find(end);
00078
00079
00080
00081
00082
00083 int start=where1+begin.size();
00084 int stop=where2;
00085
00086
00087
00088
00089
00090 string number;
00091 for (int i=start; i<stop; ++i) {
00092 number += infile[i];
00093 }
00094
00095 int nevents = atoi(number.c_str());
00096
00097
00098
00099
00100
00101
00102 return nevents;
00103 }