StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
AliStHbtEventReader.cxx
1 // any problems, send an email to chajecki@mps.ohio-state.edu
2 #include "AliStHbtEventReader.h"
3 #include <stdlib.h>
4 #include "StChain.h"
5 #include "TChain.h"
6 #include "TFile.h"
7 #include "TTree.h"
8 
9 #include "StPhysicalHelixD.hh"
10 
11 #include "SystemOfUnits.h"
12 
13 #include "StIOMaker/StIOMaker.h"
14 
15 #include "TVector3.h"
16 #include "TString.h"
17 
18 #include <math.h>
19 #include <string>
20 #include <typeinfo>
21 
22 #include "StHbtMaker/Infrastructure/StExceptions.hh"
23 #include "StHbtMaker/Infrastructure/StHbtTrackCollection.hh"
24 #include "StHbtMaker/Infrastructure/StHbtEvent.hh"
25 
26 #include "StHbtMaker/Infrastructure/StHbtVector.hh"
27 
28 #include "StHbtMaker/Reader/AliStHbtEvent.h"
29 #include "StHbtMaker/Reader/AliStHbtTrack.h"
30 
31 #include "StarClassLibrary/StMemoryInfo.hh"
32 
33 ClassImp(AliStHbtEventReader)
34 
35 #if !(ST_NO_NAMESPACES)
36  using namespace units;
37 #endif
38 
39 
40 //__________________
41 AliStHbtEventReader::AliStHbtEventReader(StHbtIOMode mode, StIOMaker* io,
42  const char* dirName, const char* fileName,
43  const char* filter, int maxFiles)
44  : mIOMaker(io), mIOMode(mode), mMaxFiles(maxFiles), mDebug(0), mCurrentFile(0),
45  mTTree(0) {
46  if (mDebug) cout << "AliStHbtEventReader::AliStHbtEventReader(...)"<< endl;
47 
48  mDir = string(dirName);
49  mFile = string(fileName);
50  mFilter = string(filter);
51  mReaderStatus = 0; // "good"
52  mEvent = new AliStHbtEvent;
53  if (mDebug) cout << "AliStHbtEventReader::AliStHbtEventReader(...) - leaving"<< endl;
54 }
55 
56 //__________________
57 AliStHbtEventReader::~AliStHbtEventReader(){
58  if (mCurrentFile) { mCurrentFile->Close(); delete mCurrentFile; mCurrentFile = 0;}
59 }
60 
61 //__________________
62 StHbtString AliStHbtEventReader::Report(){
63  StHbtString temp = "\n This is the AliStHbtEventReader\n";
64  return temp;
65 }
66 
67 //__________________
68 StHbtEvent* AliStHbtEventReader::ReturnHbtEvent(){
69  if (mDebug) cout << "AliStHbtEventReader::ReturnHbtEvent()" << endl;
70 
71  StHbtEvent* hbtEvent = 0;
72 
73  try {
74  hbtEvent = read();
75  }
76  catch(StExceptionEOF e) {
77  e.print();
78  mReaderStatus = 2;
79  return 0;
80  }
81  catch(StException e) {
82  e.print();
83  mReaderStatus = 1;
84  return 0;
85  }
86 
87  if (!hbtEvent) cout << "AliStHbtEventReader::ReturnHbtEvent() - no hbtEvent" << endl;
88 
89  return hbtEvent;
90 }
91 
92 //__________________
93 StHbtEvent* AliStHbtEventReader::read(){
94  if (!mTChain) {
95  try {
96  cout << initRead(mDir,mFile,mFilter,mMaxFiles) << " files to analyse " << endl;
97  }
98  catch(StException e) {
99  e.print();
100  return 0;
101  }
102  }
103  float sumofpid;
104 
105  unsigned int nEvents = (unsigned int)mTChain->GetEntries();
106  if (!nEvents) throw StException("AliStHbtEventReader::read() - no events to read ");
107 
108  mEvent->Clear("");
109  int iBytes= mTChain->GetEntry(mEventIndex++);
110 
111  if (nEvents<mEventIndex) throw StExceptionEOF("AliStHbtEventReader::read()");
112  if (!iBytes) throw StException("AliStHbtEventReader::read() - no event ");
113 
114  StHbtEvent *hbtEvent = 0;
115 
116  if(mEvent)
117  {
118  hbtEvent = new StHbtEvent;
119 
120  hbtEvent->SetEventNumber(mEvent->GetEventNumber());
121  hbtEvent->SetRunNumber(mEvent->GetRunNumber());
122  hbtEvent->SetCtbMult(0);
123  hbtEvent->SetZdcAdcEast(0);
124  hbtEvent->SetZdcAdcWest(0);
125  hbtEvent->SetNumberOfTpcHits(0);
126  hbtEvent->SetNumberOfTracks(mEvent->GetMultiplicity());
127  hbtEvent->SetNumberOfGoodTracks(0);
128  hbtEvent->SetUncorrectedNumberOfPositivePrimaries(0);
129  hbtEvent->SetUncorrectedNumberOfNegativePrimaries(0);
130  hbtEvent->SetUncorrectedNumberOfPrimaries(0);
131  hbtEvent->SetReactionPlane(0,0);
132  hbtEvent->SetReactionPlaneError(0, 0);
133  hbtEvent->SetReactionPlaneSubEventDifference(0, 0);
134  hbtEvent->SetTriggerWord(mEvent->GetTrigger());
135  hbtEvent->SetTriggerActionWord(0);
136  hbtEvent->SetL3TriggerAlgorithm(0, 0);
137  hbtEvent->SetUncorrectedNumberOfPrimaries(mEvent->GetMultiplicity());
138  StThreeVectorF vertex(mEvent->GetVertexX(),mEvent->GetVertexY(),mEvent->GetVertexZ());
139  hbtEvent->SetPrimVertPos(vertex);
140 
141  StHbtTrackCollection *mTrackCollection = hbtEvent->TrackCollection();
142 
143  TClonesArray *tracks = 0;
144 
145  tracks = mEvent->Tracks();
146 
147  if (tracks)
148  {
149  int nTracks = tracks->GetEntries();
150  for ( int i=0; i<nTracks; i++)
151  {
152  sumofpid=0;
153  AliStHbtTrack *track = (AliStHbtTrack*) tracks->UncheckedAt(i);
154  StHbtTrack* trackCopy = new StHbtTrack;
155  trackCopy->SetHiddenInfo(0);
156 
157  trackCopy->SetCharge(track->GetCharge());
158  trackCopy->SetNHits(track->GetNTpcHits());
159 
160  //---- Set dummy values ----//
161 
162  trackCopy->SetNHitsDedx(0);
163  trackCopy->SetNSigmaElectron(0.);
164  trackCopy->SetNSigmaPion(0.);
165  trackCopy->SetNSigmaKaon(0.);
166  trackCopy->SetNSigmaProton(0.);
167 
168  sumofpid = track->GetPidProbElectron() + track->GetPidProbPion() + track->GetPidProbKaon() + track->GetPidProbProton();
169 
170  trackCopy->SetPidProbElectron(float(track->GetPidProbElectron()/sumofpid));
171  trackCopy->SetPidProbPion(float(track->GetPidProbPion()/sumofpid));
172  trackCopy->SetPidProbKaon(float(track->GetPidProbKaon()/sumofpid));
173  trackCopy->SetPidProbProton(float(track->GetPidProbProton()/sumofpid));
174  trackCopy->SetdEdx(track->GetdEdx());
175  trackCopy->SetDCAxy(track->GetImpactParameterXY());
176  trackCopy->SetDCAz(track->GetImpactParameterZ());
177  trackCopy->SetDCAxyGlobal(0.);
178  trackCopy->SetDCAzGlobal(0.);
179  trackCopy->SetChiSquaredXY(0.);
180  trackCopy->SetChiSquaredZ(0.);
181 
182  //---- Set momentum ----//
183 
184  float px = track->GetPx();
185  float py = track->GetPy();
186  float pz = track->GetPz();
187 
188  StHbtThreeVector v(px,py,pz);
189 
190  trackCopy->SetP(v);
191  trackCopy->SetPt(sqrt(px*px+py*py));
192 
193  // NEED TO SET HELIX!!
194 
195  const StThreeVectorD p((double)px,(double)py,(double)pz);
196  const StThreeVectorD origin((double)mEvent->GetVertexX(),(double)mEvent->GetVertexY(),(double)mEvent->GetVertexZ());
197 
198  StPhysicalHelixD helix(p,origin,(double)(mEvent->GetMagField())*kilogauss,(double)(track->GetCharge()));
199 
200  trackCopy->SetHelix(helix);
201 
202  trackCopy->SetTopologyMap(0,track->GetTopologyMap(0));
203  trackCopy->SetTopologyMap(1,track->GetTopologyMap(1));
204  trackCopy->SetTopologyMap(2,track->GetTopologyMap(2));
205  trackCopy->SetTopologyMap(3,track->GetTopologyMap(3));
206  trackCopy->SetTopologyMap(4,track->GetTopologyMap(4));
207  trackCopy->SetTopologyMap(5,track->GetTopologyMap(4));
208 
209  mTrackCollection->push_back(trackCopy);
210  }
211  }
212 
213  }
214 
215  return hbtEvent;
216 }
217 
218 int AliStHbtEventReader::initRead(string dir, string file, string filter, int mMaxFiles){
219  mEventIndex =0;
220  mTChain = new TChain("AliStHbtTree","AliStHbtTree");
221 
222  int nFiles =0;
223  if (file!="") { // if a filename was given
224  if( strstr(file.c_str(),".lis") || strstr(file.c_str(),".list") ) { // if a file list is specified
225  try {
226  nFiles = fillChain(mTChain, (dir+file).c_str(), mMaxFiles);
227  }
228  catch(StException e) {
229  throw e;
230  }
231  }
232  else { // a single file was specified
233  mTChain->Add((dir+file).c_str());
234  nFiles++;
235  }
236  }
237  else {
238  try {
239  nFiles = fillChain(mTChain,dir.c_str(), filter.c_str(), mMaxFiles);
240  }
241  catch(StException e) {
242  throw e;
243  }
244  }
245 
246  mTChain->SetBranchAddress("AliStHbtEvent",&mEvent);
247  return nFiles;
248 }
249 
250 
251 int AliStHbtEventReader::uninitRead(){
252  if (mEvent) delete mEvent;
253  if (mTChain) delete mTChain;
254  mEvent = 0;
255  mTChain = 0;
256  return 0;
257 }
258 
259 int AliStHbtEventReader::fillChain(TChain* chain, const char* fileList, const int maxFiles) {
260  ifstream* inputStream = new ifstream;
261  inputStream->open(fileList);
262  if (!(inputStream)) throw StException("AliStHbtEventReader::fillChain(string dir) - can not open directory");
263  char* temp;
264  int count=0;
265  if (mDebug>1) cout << " AliStHbtEventReader::fillChain(...)- inputStream->good() : " << inputStream->good() << endl;
266  for (;inputStream->good();) {
267  temp = new char[200];
268  inputStream->getline(temp,200);
269 
270  TString fileName(temp);
271  if(fileName.Contains("root")) {
272  chain->Add(temp);
273  ++count;
274  }
275  delete temp;
276  if (count>maxFiles) break;
277  }
278  delete inputStream;
279  if (mDebug) cout << "AliStHbtEventReader::(string dir)(string dir) - Added " << count << " files to the chain" << endl;
280  return count;
281 }
282 
283 int AliStHbtEventReader::fillChain(TChain* chain, const char* dir, const char* filter, const int maxFiles) {
284  // read directory
285  void *pDir = gSystem->OpenDirectory(dir);
286  if(!pDir) throw StException("AliStHbtEventReader::fillChain(string dir) - can not open directory");
287  // now find the files that end in the specified searchString
288  const char* fileName(0);
289  int count(0);
290  while((fileName = gSystem->GetDirEntry(pDir))){
291  if(strcmp(fileName,".")==0 || strcmp(fileName,"..")==0) continue;
292  if(strstr(fileName,filter) ) { // found a match
293  char* fullFile = gSystem->ConcatFileName(dir,fileName);
294  // add it to the chain
295  cout << "AliStHbtEventReader::fillChain(string dir) - Adding " << fullFile << " to the chain" << endl;
296  chain->Add(fullFile);
297  delete fullFile;
298  ++count;
299  if (count>maxFiles) break;
300  }
301  }
302  cout << "AliStHbtEventReader::(string dir)(string dir) - Added " << count << " files to the chain" << endl;
303  return count;
304 }
305 
306 
307 
308 
309 
310 
311 
312