StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StSpinTreeReader.cxx
1 /*
2  * StSpinTreeReader.cpp
3  * StarSpinLibraries
4  *
5  * Created by Adam Kocoloski on 5/7/07.
6  *
7  */
8 
9 #include "StSpinTreeReader.h"
10 
11 #include "TDirectory.h"
12 #include "TFile.h"
13 #include "TStopwatch.h"
14 
15 ClassImp(StSpinTreeReader)
16 
17 #include <fstream>
18 
19 StSpinTreeReader::StSpinTreeReader(const char *treeName) : connectJets(true),
20  connectNeutralJets(true), connectChargedPions(true), connectBemcPions(true),
21  connectEemcPions(true), connectBemcElectrons(true), requireDidFire(false), requireShouldFire(false),
22  mEemcPions(NULL), mEventList(NULL), mIsConnected(false)
23 {
24  mChain = new TChain(treeName);
25  mChainConeJets = new TChain("ConeJets");
26  mChainConeJetsEMC = new TChain("ConeJetsEMC");
27  mChainChargedPions = new TChain("chargedPions");
28  mChainBemcPions = new TChain("bemcPions");
29  mChainBemcElectrons = new TChain("bemcElectrons");
30 
31  mEvent = new StJetSkimEvent();
32  mConeJets = new TClonesArray("StJet",100);
33  mConeJetsEMC = new TClonesArray("StJet",100);
34  mChargedPions = new TClonesArray("StChargedPionTrack",100);
35  mBemcPions = new TClonesArray("TPi0Candidate",100);
36  mBemcElectrons = new TClonesArray("StPrimaryElectron",100);
37  mBemcGlobalElectrons = new TClonesArray("StGlobalElectron",500);
38 }
39 
40 StSpinTreeReader::~StSpinTreeReader() {
41  std::cout << "StSpinTreeReader::~StSpinTreeReader()" << std::endl;
42  delete mChainConeJets;
43  delete mChainConeJetsEMC;
44  delete mChainChargedPions;
45  delete mChainBemcPions;
46  delete mChainBemcElectrons;
47  delete mChain;
48 
49  delete mConeJets;
50  delete mConeJetsEMC;
51  delete mChargedPions;
52  delete mBemcPions;
53  delete mBemcElectrons;
54  delete mBemcGlobalElectrons;
55 
56  if(mEventList){
57  delete mEventList;
58  mEventList = NULL;
59  }
60 }
61 
62 StSpinTreeReader::StSpinTreeReader(const StSpinTreeReader & t) {
63 
64 }
65 
66 StSpinTreeReader& StSpinTreeReader::operator=(const StSpinTreeReader &rhs) {
67  return *this;
68 }
69 
70 void StSpinTreeReader::selectDataset(const char *path) {
71  TString fullPath = path;
72  fullPath.ReplaceAll("$STAR",getenv("STAR"));
73  std::ifstream filelist(fullPath.Data());
74  std::string currentFile;
75  while(filelist.good()) {
76  getline(filelist,currentFile);
77  if(currentFile.size() == 0) continue;
78  //std::cout << "adding current file = " << currentFile << std::endl;
79  selectFile(currentFile);
80  }
81 }
82 
83 void StSpinTreeReader::selectFile(const char *path) {
84  std::string theFile(path);
85  selectFile(theFile);
86 }
87 
88 void StSpinTreeReader::selectFile(std::string & path) {
89  int run = atoi(path.substr(path.length()-17,7).c_str());
90  mFileList[run] = path;
91 }
92 
93 long StSpinTreeReader::GetEntries() {
94  connect();
95  if(mEventList) return mEventList->GetN();
96  return mChain->GetEntries();
97 }
98 
99 void StSpinTreeReader::GetEntry(long i) {
100  connect();
101  if(mEventList) {
102  long n = mEventList->GetEntry(i);
103  mChain->GetEntry(n);
104  }
105  else mChain->GetEntry(i);
106 
107  if(mCurrentFileName != mChain->GetFile()->GetName()) {
108  mCurrentFileName = mChain->GetFile()->GetName();
109  std::cout << "now analyzing " << mCurrentFileName << std::endl;
110  }
111 }
112 
113 void StSpinTreeReader::connect() {
114  if(!mIsConnected) {
115  //only use files in the filelist if the run is selected or runlist.empty()
116  if(mFileList.empty()) std::cout << "no files to analyze! check your macro" << std::endl;
117  for(map<int,std::string>::iterator it=mFileList.begin(); it!=mFileList.end(); it++) {
118  if(mRunList.empty() || mRunList.count(it->first)) {
119  std::cout << "adding " << it->second << std::endl;
120  mChain->AddFile(it->second.c_str());
121  if(connectJets) mChainConeJets->AddFile(it->second.c_str());
122  if(connectNeutralJets) mChainConeJetsEMC->AddFile(it->second.c_str());
123  if(connectChargedPions) mChainChargedPions->AddFile(it->second.c_str());
124  if(connectBemcPions) mChainBemcPions->AddFile(it->second.c_str());
125  if(connectBemcElectrons)mChainBemcElectrons->AddFile(it->second.c_str());
126  }
127  }
128 
129  mChain->SetBranchAddress("skimEventBranch",&mEvent);
130 
131  if(connectJets) {
132  mChain->AddFriend("ConeJets");
133  mChain->SetBranchAddress("ConeJets", &mConeJets);
134  }
135 
136  if(connectNeutralJets) {
137  mChain->AddFriend("ConeJetsEMC");
138  mChain->SetBranchAddress("ConeJetsEMC",&mConeJetsEMC);
139  }
140 
141  if(connectChargedPions) {
142  mChain->AddFriend("chargedPions");
143  mChain->SetBranchAddress("chargedPions",&mChargedPions);
144  }
145 
146  if(connectBemcPions) {
147  mChain->AddFriend("bemcPions");
148  mChain->SetBranchAddress("bemcPions",&mBemcPions);
149  }
150  if(connectBemcElectrons) {
151  mChain->AddFriend("bemcElectrons");
152  mChain->SetBranchAddress("PrimaryElectrons",&mBemcElectrons);
153  mChain->SetBranchAddress("GlobalElectrons",&mBemcGlobalElectrons);
154  }
155  if(connectEemcPions) {
156  //mEemcPions = new TClonesArray("StEEmcPair",100);
157  //mChain->SetBranchAddress("eemcNeutralPions",&mEemcPions);
158  }
159 
160  //now do the event list selection by building a TString from the sets of triggers
161  if(mEventList == NULL) {
162  TString s = "";
163  bool atStart = true;
164  for(std::set<int>::const_iterator it = mTriggerList.begin(); it != mTriggerList.end(); it++) {
165  if(atStart) {
166  s += "( mTriggers.mTrigId==";
167  atStart = false;
168  }
169  else { s += " || mTriggers.mTrigId=="; }
170  s += *it;
171  }
172  if(mTriggerList.size()) s += " )";
173  if(requireDidFire || requireShouldFire) {
174  s += " && ( ";
175  if(requireDidFire && requireShouldFire) s += "mTriggers.mDidFire==1 && mTriggers.mShouldFire==1 )";
176  else if(requireDidFire) s += "mTriggers.mDidFire==1 )";
177  else s+= "mTriggers.mShouldFire==1 )";
178  }
179  if(s.Length()) {
180  std::cout << "begin generation of TEventList with contents \n" << s << std::endl;
181  TStopwatch timer;
182  mChain->Draw(">>elist_spinTreeReader",s.Data(),"entrylist");
183  mEventList = (TEventList*)gDirectory->Get("elist_spinTreeReader");
184  mChain->SetEventList(mEventList);
185  std::cout << "TEventList generated and stored in " << timer.CpuTime()
186  << " CPU / " << timer.RealTime() << " real seconds" << std::endl;
187  }
188  }
189 
190  mIsConnected = true;
191  }
192 }
193 
194 void StSpinTreeReader::selectRunlist(const char *path) {
195  TString fullPath = path;
196  fullPath.ReplaceAll("$STAR",getenv("STAR"));
197  std::ifstream list(fullPath.Data());
198  int currentRun;
199  while(!list.eof()) {
200  list >> currentRun;
201  if(currentRun == 0) continue;
202  //std::cout << "adding current run = " << currentRun << std::endl;
203  mRunList.insert(currentRun);
204  }
205 }
206 
207 void StSpinTreeReader::selectRun(int runnumber) {
208  mRunList.insert(runnumber);
209 }
210 
211 void StSpinTreeReader::removeRun(int runnumber) {
212  mRunList.erase(runnumber);
213 }
214 
215 void StSpinTreeReader::selectTrigger(int trigger) {
216  mTriggerList.insert(trigger);
217 }