00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "StSpinTreeReader.h"
00010
00011 #include "TDirectory.h"
00012 #include "TFile.h"
00013 #include "TStopwatch.h"
00014
00015 ClassImp(StSpinTreeReader)
00016
00017 #include <fstream>
00018
00019 StSpinTreeReader::StSpinTreeReader(const char *treeName) : connectJets(true),
00020 connectNeutralJets(true), connectChargedPions(true), connectBemcPions(true),
00021 connectEemcPions(true), connectBemcElectrons(true), requireDidFire(false), requireShouldFire(false),
00022 mEemcPions(NULL), mEventList(NULL), mIsConnected(false)
00023 {
00024 mChain = new TChain(treeName);
00025 mChainConeJets = new TChain("ConeJets");
00026 mChainConeJetsEMC = new TChain("ConeJetsEMC");
00027 mChainChargedPions = new TChain("chargedPions");
00028 mChainBemcPions = new TChain("bemcPions");
00029 mChainBemcElectrons = new TChain("bemcElectrons");
00030
00031 mEvent = new StJetSkimEvent();
00032 mConeJets = new TClonesArray("StJet",100);
00033 mConeJetsEMC = new TClonesArray("StJet",100);
00034 mChargedPions = new TClonesArray("StChargedPionTrack",100);
00035 mBemcPions = new TClonesArray("TPi0Candidate",100);
00036 mBemcElectrons = new TClonesArray("StPrimaryElectron",100);
00037 mBemcGlobalElectrons = new TClonesArray("StGlobalElectron",500);
00038 }
00039
00040 StSpinTreeReader::~StSpinTreeReader() {
00041 std::cout << "StSpinTreeReader::~StSpinTreeReader()" << std::endl;
00042 delete mChainConeJets;
00043 delete mChainConeJetsEMC;
00044 delete mChainChargedPions;
00045 delete mChainBemcPions;
00046 delete mChainBemcElectrons;
00047 delete mChain;
00048
00049 delete mConeJets;
00050 delete mConeJetsEMC;
00051 delete mChargedPions;
00052 delete mBemcPions;
00053 delete mBemcElectrons;
00054 delete mBemcGlobalElectrons;
00055
00056 if(mEventList){
00057 delete mEventList;
00058 mEventList = NULL;
00059 }
00060 }
00061
00062 StSpinTreeReader::StSpinTreeReader(const StSpinTreeReader & t) {
00063
00064 }
00065
00066 StSpinTreeReader& StSpinTreeReader::operator=(const StSpinTreeReader &rhs) {
00067 return *this;
00068 }
00069
00070 void StSpinTreeReader::selectDataset(const char *path) {
00071 TString fullPath = path;
00072 fullPath.ReplaceAll("$STAR",getenv("STAR"));
00073 std::ifstream filelist(fullPath.Data());
00074 std::string currentFile;
00075 while(filelist.good()) {
00076 getline(filelist,currentFile);
00077 if(currentFile.size() == 0) continue;
00078
00079 selectFile(currentFile);
00080 }
00081 }
00082
00083 void StSpinTreeReader::selectFile(const char *path) {
00084 std::string theFile(path);
00085 selectFile(theFile);
00086 }
00087
00088 void StSpinTreeReader::selectFile(std::string & path) {
00089 int run = atoi(path.substr(path.length()-17,7).c_str());
00090 mFileList[run] = path;
00091 }
00092
00093 long StSpinTreeReader::GetEntries() {
00094 connect();
00095 if(mEventList) return mEventList->GetN();
00096 return mChain->GetEntries();
00097 }
00098
00099 void StSpinTreeReader::GetEntry(long i) {
00100 connect();
00101 if(mEventList) {
00102 long n = mEventList->GetEntry(i);
00103 mChain->GetEntry(n);
00104 }
00105 else mChain->GetEntry(i);
00106
00107 if(mCurrentFileName != mChain->GetFile()->GetName()) {
00108 mCurrentFileName = mChain->GetFile()->GetName();
00109 std::cout << "now analyzing " << mCurrentFileName << std::endl;
00110 }
00111 }
00112
00113 void StSpinTreeReader::connect() {
00114 if(!mIsConnected) {
00115
00116 if(mFileList.empty()) std::cout << "no files to analyze! check your macro" << std::endl;
00117 for(map<int,std::string>::iterator it=mFileList.begin(); it!=mFileList.end(); it++) {
00118 if(mRunList.empty() || mRunList.count(it->first)) {
00119 std::cout << "adding " << it->second << std::endl;
00120 mChain->AddFile(it->second.c_str());
00121 if(connectJets) mChainConeJets->AddFile(it->second.c_str());
00122 if(connectNeutralJets) mChainConeJetsEMC->AddFile(it->second.c_str());
00123 if(connectChargedPions) mChainChargedPions->AddFile(it->second.c_str());
00124 if(connectBemcPions) mChainBemcPions->AddFile(it->second.c_str());
00125 if(connectBemcElectrons)mChainBemcElectrons->AddFile(it->second.c_str());
00126 }
00127 }
00128
00129 mChain->SetBranchAddress("skimEventBranch",&mEvent);
00130
00131 if(connectJets) {
00132 mChain->AddFriend("ConeJets");
00133 mChain->SetBranchAddress("ConeJets", &mConeJets);
00134 }
00135
00136 if(connectNeutralJets) {
00137 mChain->AddFriend("ConeJetsEMC");
00138 mChain->SetBranchAddress("ConeJetsEMC",&mConeJetsEMC);
00139 }
00140
00141 if(connectChargedPions) {
00142 mChain->AddFriend("chargedPions");
00143 mChain->SetBranchAddress("chargedPions",&mChargedPions);
00144 }
00145
00146 if(connectBemcPions) {
00147 mChain->AddFriend("bemcPions");
00148 mChain->SetBranchAddress("bemcPions",&mBemcPions);
00149 }
00150 if(connectBemcElectrons) {
00151 mChain->AddFriend("bemcElectrons");
00152 mChain->SetBranchAddress("PrimaryElectrons",&mBemcElectrons);
00153 mChain->SetBranchAddress("GlobalElectrons",&mBemcGlobalElectrons);
00154 }
00155 if(connectEemcPions) {
00156
00157
00158 }
00159
00160
00161 if(mEventList == NULL) {
00162 TString s = "";
00163 bool atStart = true;
00164 for(std::set<int>::const_iterator it = mTriggerList.begin(); it != mTriggerList.end(); it++) {
00165 if(atStart) {
00166 s += "( mTriggers.mTrigId==";
00167 atStart = false;
00168 }
00169 else { s += " || mTriggers.mTrigId=="; }
00170 s += *it;
00171 }
00172 if(mTriggerList.size()) s += " )";
00173 if(requireDidFire || requireShouldFire) {
00174 s += " && ( ";
00175 if(requireDidFire && requireShouldFire) s += "mTriggers.mDidFire==1 && mTriggers.mShouldFire==1 )";
00176 else if(requireDidFire) s += "mTriggers.mDidFire==1 )";
00177 else s+= "mTriggers.mShouldFire==1 )";
00178 }
00179 if(s.Length()) {
00180 std::cout << "begin generation of TEventList with contents \n" << s << std::endl;
00181 TStopwatch timer;
00182 mChain->Draw(">>elist_spinTreeReader",s.Data(),"entrylist");
00183 mEventList = (TEventList*)gDirectory->Get("elist_spinTreeReader");
00184 mChain->SetEventList(mEventList);
00185 std::cout << "TEventList generated and stored in " << timer.CpuTime()
00186 << " CPU / " << timer.RealTime() << " real seconds" << std::endl;
00187 }
00188 }
00189
00190 mIsConnected = true;
00191 }
00192 }
00193
00194 void StSpinTreeReader::selectRunlist(const char *path) {
00195 TString fullPath = path;
00196 fullPath.ReplaceAll("$STAR",getenv("STAR"));
00197 std::ifstream list(fullPath.Data());
00198 int currentRun;
00199 while(!list.eof()) {
00200 list >> currentRun;
00201 if(currentRun == 0) continue;
00202
00203 mRunList.insert(currentRun);
00204 }
00205 }
00206
00207 void StSpinTreeReader::selectRun(int runnumber) {
00208 mRunList.insert(runnumber);
00209 }
00210
00211 void StSpinTreeReader::removeRun(int runnumber) {
00212 mRunList.erase(runnumber);
00213 }
00214
00215 void StSpinTreeReader::selectTrigger(int trigger) {
00216 mTriggerList.insert(trigger);
00217 }