]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HBTAN/AliHBTReader.cxx
Bug corrected
[u/mrichter/AliRoot.git] / HBTAN / AliHBTReader.cxx
1 #include "AliHBTReader.h"
2
3 #include <TString.h>
4 #include <TObjString.h>
5 #include <TObjArray.h>
6 #include <TClass.h>
7 #include <TRandom.h>
8
9 #include "AliHBTParticleCut.h"
10 #include "AliHBTEvent.h"
11 #include "AliHBTRun.h"
12  
13 ClassImp(AliHBTReader)
14 //pure virtual
15     
16 /*************************************************************************************/
17
18 AliHBTReader::AliHBTReader():
19  fCuts(new TObjArray()),
20  fDirs(0x0),
21  fCurrentEvent(0),
22  fCurrentDir(0),
23  fNEventsRead(0),
24  fTracksEvent(0x0),
25  fParticlesEvent(0x0),
26  fParticles(0x0),
27  fTracks(0x0),
28  fIsRead(kFALSE),
29  fBufferEvents(kFALSE),
30  fBlend(kFALSE),
31  fFirst(0),
32  fLast(0)
33 {
34 //constructor
35 }
36 /*************************************************************************************/
37
38 AliHBTReader::AliHBTReader(TObjArray* dirs):
39  fCuts(new TObjArray()),
40  fDirs(dirs),
41  fCurrentEvent(0),
42  fCurrentDir(0),
43  fNEventsRead(0),
44  fTracksEvent(0x0),
45  fParticlesEvent(0x0),
46  fParticles(0x0),
47  fTracks(0x0),
48  fIsRead(kFALSE),
49  fBufferEvents(kFALSE),
50  fBlend(kFALSE),
51  fFirst(0),
52  fLast(0)
53 {
54 //ctor with array of directories to read as parameter
55 }
56 /*************************************************************************************/
57
58 AliHBTReader::~AliHBTReader()
59 {
60 //destructor
61  if(fCuts)
62   {
63    fCuts->SetOwner();
64    delete fCuts;
65   }
66  delete fParticlesEvent;
67  delete fTracksEvent;
68 }
69 /*************************************************************************************/
70
71 Int_t AliHBTReader::Next()
72 {
73 //moves to next event
74   if ((fNEventsRead > fLast) && (fLast > 0) ) return kTRUE;
75    
76   do
77    {
78     if ( ReadNext() == kTRUE)
79       return kTRUE;
80    }while (fNEventsRead < fFirst);
81   
82   if (fBlend) Blend();
83   
84   if (fBufferEvents)
85    {
86      if ( ReadsTracks() && fTracksEvent) 
87        fTracks->SetEvent(fNEventsRead-1-fFirst,fTracksEvent);
88      if ( ReadsParticles() && fParticlesEvent)
89        fParticles->SetEvent(fNEventsRead-1-fFirst,fParticlesEvent);
90    }
91   return kFALSE;
92 }
93 /*************************************************************************************/
94
95 void AliHBTReader::AddParticleCut(AliHBTParticleCut* cut)
96 {
97  //sets the new cut 
98  
99   if (!cut) //if cut is NULL return with error
100    {
101     Error("AddParticleType","NULL pointers are not accepted any more.\nIf You want to accept all particles of this type, set an empty cut ");
102     return;
103    }
104   AliHBTParticleCut *c = (AliHBTParticleCut*)cut->Clone();
105   fCuts->Add(c);
106 }
107 /********************************************************************/
108
109 AliHBTEvent* AliHBTReader::GetParticleEvent(Int_t n)
110  {
111  //returns Nth event with simulated particles
112   if (ReadsParticles() == kFALSE)
113    {
114      Error("GetParticleEvent","This reader is not able to provide simulated particles.");
115      return 0;
116    } 
117    
118   if (!fIsRead) 
119    { 
120     if (ReadsParticles() && (fParticles == 0x0)) fParticles = new AliHBTRun();
121     if (ReadsTracks() && (fTracks == 0x0)) fTracks = new AliHBTRun();
122     
123     if (Read(fParticles,fTracks))
124      {
125        Error("GetParticleEvent","Error in reading");
126        return 0x0;
127      }
128     else fIsRead = kTRUE;
129    }
130   return fParticles->GetEvent(n);
131  }
132 /********************************************************************/
133
134 AliHBTEvent* AliHBTReader::GetTrackEvent(Int_t n)
135  {
136  //returns Nth event with reconstructed tracks
137   if (ReadsTracks() == kFALSE)
138    {
139      Error("GetTrackEvent","This reader is not able to provide recosntructed tracks.");
140      return 0;
141    } 
142   if (!fIsRead) 
143    {
144     if (ReadsParticles() && (fParticles == 0x0)) fParticles = new AliHBTRun();
145     if (ReadsTracks() && (fTracks == 0x0)) fTracks = new AliHBTRun();
146     
147     if(Read(fParticles,fTracks))
148      {
149        Error("GetTrackEvent","Error in reading");
150        return 0x0;
151      }
152     else fIsRead = kTRUE;
153    }
154   return fTracks->GetEvent(n);
155  }
156 /********************************************************************/
157
158 Int_t AliHBTReader::GetNumberOfPartEvents()
159  {
160  //returns number of events of particles
161   if (ReadsParticles() == kFALSE)
162    {
163      Error("GetNumberOfPartEvents","This reader is not able to provide simulated particles.");
164      return 0;
165    } 
166    
167   if (!fIsRead) 
168    {
169     if (ReadsParticles() && (fParticles == 0x0)) fParticles = new AliHBTRun();
170     if (ReadsTracks() && (fTracks == 0x0)) fTracks = new AliHBTRun();
171     
172     if (Read(fParticles,fTracks))
173      {
174        Error("GetNumberOfPartEvents","Error in reading");
175        return 0;
176      }
177     else fIsRead = kTRUE;
178    }
179    return fParticles->GetNumberOfEvents();
180  }
181 /********************************************************************/
182  
183 Int_t AliHBTReader::GetNumberOfTrackEvents()
184  {
185  //returns number of events of tracks
186   if (ReadsTracks() == kFALSE)
187    {
188      Error("GetNumberOfTrackEvents","This reader is not able to provide recosntructed tracks.");
189      return 0;
190    } 
191   if (!fIsRead)
192    {
193      if (ReadsParticles() && (fParticles == 0x0)) fParticles = new AliHBTRun();
194      if (ReadsTracks() && (fTracks == 0x0)) fTracks = new AliHBTRun();
195      
196      if(Read(fParticles,fTracks))
197       {
198         Error("GetNumberOfTrackEvents","Error in reading");
199         return 0;
200       }
201      else fIsRead = kTRUE;
202    }
203   return fTracks->GetNumberOfEvents();
204  }
205 /********************************************************************/
206
207 Int_t AliHBTReader::Read(AliHBTRun* particles, AliHBTRun *tracks)
208 {
209  //reads data and puts put to the particles and tracks objects
210  //reurns 0 if everything is OK
211  //
212   Info("Read","");
213   
214   if ( ReadsParticles() && (particles == 0x0) ) //check if an object is instatiated
215    {
216      Error("Read"," particles object must be instatiated before passing it to the reader");
217      return 1;
218    }
219   if ( ReadsTracks() && (tracks == 0x0) )  //check if an object is instatiated
220    {
221      Error("Read"," tracks object must be instatiated before passing it to the reader");
222      return 1;
223    }
224    
225   if (ReadsParticles()) particles->Reset();//clear runs == delete all old events
226   if (ReadsTracks()) tracks->Reset();
227   
228   Rewind();
229   
230   Int_t i = 0;
231   while(Next() == kFALSE)
232    {
233      if (ReadsTracks()) tracks->SetEvent(i,fTracksEvent);
234      if (ReadsParticles()) particles->SetEvent(i,fParticlesEvent);
235      i++;
236    }
237   return 0;
238 }      
239 /*************************************************************************************/
240
241 Bool_t AliHBTReader::Pass(AliHBTParticle* p)
242 {
243  //Method examines whether particle meets all cut and particle type criteria
244   
245    if(p==0x0)//of corse we not pass NULL pointers
246     {
247      Warning("Pass()","No Pasaran! We never accept NULL pointers");
248      return kTRUE;
249     }
250    //if no particle is specified, we pass all particles
251    //excluding NULL pointers, of course
252   if ( fCuts->GetEntriesFast() == 0 ) return kFALSE; //if no cut specified accept all particles
253   for(Int_t i=0; i<fCuts->GetEntriesFast(); i++)   
254    {
255      AliHBTParticleCut &cut = *((AliHBTParticleCut*)fCuts->At(i));
256      if(!cut.Pass(p)) return kFALSE;  //accepted
257    }
258    
259   return kTRUE;//not accepted
260 }
261 /*************************************************************************************/
262
263 Bool_t  AliHBTReader::Pass(Int_t pid)
264 {
265 //this method checks if any of existing cuts accepts this pid particles
266 //or any cuts accepts all particles
267
268  if(pid == 0)
269   return kTRUE;
270
271  if ( fCuts->GetEntriesFast() == 0 ) return kFALSE; //if no cut specified accept all particles
272   
273  for(Int_t i=0; i<fCuts->GetEntriesFast(); i++)   
274    {
275      AliHBTParticleCut &cut = *((AliHBTParticleCut*)fCuts->At(i));
276      //if some of cuts accepts all particles or some accepts particles of this type, accept
277      if ( (cut.GetPID() == 0) || (cut.GetPID() == pid) ) return kFALSE; 
278    }
279  return kTRUE;
280 }
281 /*************************************************************************************/
282
283 TString& AliHBTReader::GetDirName(Int_t entry)
284 {
285 //returns directory name of next one to read
286   TString* retval;//return value
287   if (fDirs ==  0x0)
288    {
289      retval = new TString(".");
290      return *retval;
291    }
292
293   if ( (entry>fDirs->GetEntries()) || (entry<0))//if out of bounds return empty string
294    {                                            //note that entry==0 is accepted even if array is empty (size=0)
295      Error("GetDirName","Name out of bounds");
296      retval = new TString();
297      return *retval;
298    }
299
300   if (fDirs->GetEntries() == 0)
301    { 
302      retval = new TString(".");
303      return *retval;
304    }
305
306   TClass *objclass = fDirs->At(entry)->IsA();
307   TClass *stringclass = TObjString::Class();
308
309   TObjString *dir = (TObjString*)objclass->DynamicCast(stringclass,fDirs->At(entry));
310
311   if(dir == 0x0)
312    {
313      Error("GetDirName","Object in TObjArray is not a TObjString or its descendant");
314      retval = new TString();
315      return *retval;
316    }
317   if (gDebug > 0) Info("GetDirName","Returned ok %s",dir->String().Data());
318   return dir->String();
319 }
320 /*************************************************************************************/
321
322 void AliHBTReader::Blend()
323 {
324   //randomly change positions of the particles after reading
325   //is used to check if some distr depends on order of particles
326   //(tracking gives particles Pt sorted)
327   
328   if (fParticlesEvent == 0x0) return;
329   
330   for (Int_t i = 2; i < fParticlesEvent->GetNumberOfParticles(); i++)
331    {
332      Int_t with = gRandom->Integer(i);
333      fParticlesEvent->SwapParticles(i,with);
334      if (fTracksEvent) fTracksEvent->SwapParticles(i,with);
335    }
336 }