]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ANALYSIS/AliReader.cxx
delete the AliSurvey objs after use
[u/mrichter/AliRoot.git] / ANALYSIS / AliReader.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18 //_________________________________________________________________________
19 ///////////////////////////////////////////////////////////////////////////
20 //
21 // class AliReader
22 //
23 // Reader Base class 
24 // Reads particles and tracks and
25 // puts them to the AliAOD objects and eventually, if needed, buffers AliAODs in AliAODRun(s)
26 //
27 // User loops over events calling method Next. In case of success this method returns 0.
28 // In case of error or if there is no more events to read, non-0 value is returned
29 //
30 // Reading can be rewound to the beginning using method Rewind.
31 //
32 // Tracks are read to the fEventRec (contains reconstructed tracks) 
33 // and fEventSim (corresponding MC simulated data) data members,
34 // that are of the type AliAOD. 
35 //
36 // If a given reader has ability of reading both, reconstructed and simulated data, 
37 // these are structured in AODs so a "n'th" simulated particle 
38 // (the one stored in the fEventSim at slot n) 
39 // corresponds to the n'th reconstructed track (the one stored in the fEventRec at slot n).
40 //
41 // The same reconstructed track can be present more than ones in the AOD,
42 // but with a different PID. In this case
43 // pointer to the corresponding MC simulated particles is also present more than ones.
44 // This situation happens if you want to read all particles 
45 // with PID probability of being , e.g.,  pion higher than 60%
46 // and being kaon higher than 40%. Than, if a given track has probability Ppid(pi)=52% and Ppid(K)=48% 
47 // than it is read twise.
48 //
49 // Provides functionality for both buffering and non-buffering reading
50 // This can be switched on/off via method SetEventBuffering(bool)
51 // The main method that inheriting classes need to implement is ReadNext()
52 // that read next event in queue.
53 //
54 // The others are:
55 // Bool_t  ReadsRec() const; specifies if reader is able to read simulated particles
56 // Bool_t  ReadsSim() const; specifies if reader is able to read reconstructed tracks
57 // void    Rewind(); rewind reading to the beginning
58 //
59 // This class provides full functionality for reading from many sources
60 // User can provide TObjArray of TObjStrings (SetDirs method or via parameter 
61 // in the constructor) which desribes paths of directories to search data in.
62 // If none specified current directory is searched.
63 // 
64 // Piotr.Skowronski@cern.ch
65 //
66 ///////////////////////////////////////////////////////////////////////////
67
68 #include <TClass.h>
69 #include <TGliteXmlEventlist.h>
70 #include <TH1.h>
71 #include <TObjArray.h>
72 #include <TObjString.h>
73 #include <TRandom.h>
74 #include <TString.h>
75
76 #include "AliAOD.h"
77 #include "AliAODParticleCut.h"
78 #include "AliAODRun.h"
79 #include "AliLog.h"
80 #include "AliReader.h"
81  
82 ClassImp(AliReader)
83 //pure virtual
84     
85 /*************************************************************************************/
86
87 AliReader::AliReader():
88  fEventList(0x0),
89  fCuts(new TObjArray()),
90  fDirs(0x0),
91  fCurrentEvent(0),
92  fCurrentDir(0),
93  fNEventsRead(0),
94  fEventRec(0x0),
95  fEventSim(0x0),
96  fRunSim(0x0),
97  fRunRec(0x0),
98  fIsRead(kFALSE),
99  fBufferEvents(kFALSE),
100  fBlend(kFALSE),
101  fFirst(0),
102  fLast(0),
103  fTrackCounter(0x0)
104 {
105 //constructor
106 }
107 /*************************************************************************************/
108
109 AliReader::AliReader(TObjArray* dirs):
110  fEventList(0x0),
111  fCuts(new TObjArray()),
112  fDirs(dirs),
113  fCurrentEvent(0),
114  fCurrentDir(0),
115  fNEventsRead(0),
116  fEventRec(0x0),
117  fEventSim(0x0),
118  fRunSim(0x0),
119  fRunRec(0x0),
120  fIsRead(kFALSE),
121  fBufferEvents(kFALSE),
122  fBlend(kFALSE),
123  fFirst(0),
124  fLast(0),
125  fTrackCounter(0x0)
126 {
127 //ctor with array of directories to read as parameter
128 }
129 /*************************************************************************************/
130 AliReader::AliReader(const AliReader& in):
131  TNamed(in),
132  fEventList((in.fEventList)?(TGliteXmlEventlist*)in.fEventList->Clone():0x0),
133  fCuts((in.fCuts)?(TObjArray*)in.fCuts->Clone():0x0),
134  fDirs((in.fDirs)?(TObjArray*)in.fDirs->Clone():0x0),
135  fCurrentEvent(0),
136  fCurrentDir(0),
137  fNEventsRead(0),
138  fEventRec(0x0),
139  fEventSim(0x0),
140  fRunSim(0x0),
141  fRunRec(0x0),
142  fIsRead(kFALSE),
143  fBufferEvents(in.fBufferEvents),
144  fBlend(in.fBlend),
145  fFirst(in.fFirst),
146  fLast(in.fLast),
147  fTrackCounter(0x0)
148 {
149  //cpy constructor
150 }
151
152 AliReader::~AliReader()
153 {
154 //destructor
155  if(fCuts)
156   {
157    fCuts->SetOwner();
158    delete fCuts;
159   }
160  delete fEventSim;
161  delete fEventRec;
162  delete fTrackCounter;
163  delete fEventList;
164 }
165 /*************************************************************************************/
166
167 AliReader& AliReader::operator=(const AliReader& in)
168 {
169   //Assigment operator
170  if (this == &in) return *this;  
171  TNamed::operator=( (const TNamed&)in );
172   
173  fCuts = (in.fCuts)?(TObjArray*)in.fCuts->Clone():0x0;
174  fDirs = (in.fDirs)?(TObjArray*)in.fDirs->Clone():0x0;
175  fCurrentEvent = 0;
176  fCurrentDir = 0;
177  fNEventsRead = 0;
178  fEventRec = 0x0;
179  fEventSim = 0x0;
180  fRunSim = 0x0;
181  fRunRec = 0x0;
182  fIsRead = kFALSE;
183  fBufferEvents = in.fBufferEvents;
184  fBlend = in.fBlend;
185  fFirst = in.fFirst;
186  fLast = in.fLast;
187  fTrackCounter = 0x0;
188  return *this;  
189 }
190 /*************************************************************************************/
191
192 Int_t AliReader::Next()
193 {
194 //moves to next event
195
196   //if asked to read up to event nb. fLast, and it is overcome, report no more events
197   if ((fNEventsRead >= fLast) && (fLast > 0) ) return kTRUE;
198   
199   if (fTrackCounter == 0x0)//create Track Counter
200    {
201      fTrackCounter = new TH1I("trackcounter","Track Counter",20000,0,20000);
202      fTrackCounter->SetDirectory(0x0);
203    }
204   
205   do //if asked to read from event fFirst, rewind to it
206    {
207     if ( ReadNext() == kTRUE) //if no more evets, return it
208       return kTRUE;
209    }while (fNEventsRead < fFirst);
210    
211   //here we have event
212   
213   if (fBlend) Blend();//Mix particles order 
214   
215   if (fBufferEvents)//store events if buffering is on
216    {
217      if ( ReadsRec() && fEventRec) 
218        fRunRec->SetEvent(fNEventsRead-1-fFirst,fEventRec);
219      if ( ReadsSim() && fEventSim)
220        fRunSim->SetEvent(fNEventsRead-1-fFirst,fEventSim);
221    }
222   return kFALSE;
223 }
224 /*************************************************************************************/
225
226 void AliReader::AddParticleCut(AliAODParticleCut* cut)
227 {
228  //sets the new cut. MAKES A COPY OF THE CUT !!!!
229  
230   if (!cut) //if cut is NULL return with error
231    {
232     Error("AddParticleType","NULL pointers are not accepted any more.\nIf You want to accept all particles of this type, set an empty cut ");
233     return;
234    }
235   AliAODParticleCut *c = (AliAODParticleCut*)cut->Clone();
236   fCuts->Add(c);
237 }
238 /********************************************************************/
239
240 AliAOD* AliReader::GetEventSim(Int_t n)
241  {
242  //returns Nth event with simulated particles
243   if (ReadsSim() == kFALSE)
244    {
245      Error("GetParticleEvent","This reader is not able to provide simulated particles.");
246      return 0;
247    } 
248    
249   if (!fIsRead) 
250    { 
251     if (ReadsSim() && (fRunSim == 0x0)) fRunSim = new AliAODRun();
252     if (ReadsRec() && (fRunRec == 0x0)) fRunRec = new AliAODRun();
253     
254     if (Read(fRunSim,fRunRec))
255      {
256        Error("GetParticleEvent","Error in reading");
257        return 0x0;
258      }
259     else fIsRead = kTRUE;
260    }
261   return fRunSim->GetEvent(n);
262  }
263 /********************************************************************/
264
265 AliAOD* AliReader::GetEventRec(Int_t n)
266  {
267  //returns Nth event with reconstructed tracks
268   if (ReadsRec() == kFALSE)
269    {
270      Error("GetTrackEvent","This reader is not able to provide recosntructed tracks.");
271      return 0;
272    } 
273   if (!fIsRead) 
274    {
275     if (ReadsSim() && (fRunSim == 0x0)) fRunSim = new AliAODRun();
276     if (ReadsRec() && (fRunRec == 0x0)) fRunRec = new AliAODRun();
277     
278     if(Read(fRunSim,fRunRec))
279      {
280        Error("GetTrackEvent","Error in reading");
281        return 0x0;
282      }
283     else fIsRead = kTRUE;
284    }
285   return fRunRec->GetEvent(n);
286  }
287 /********************************************************************/
288
289 Int_t AliReader::GetNumberOfSimEvents()
290  {
291  //returns number of events of particles
292   if (ReadsSim() == kFALSE)
293    {
294      Error("GetNumberOfPartEvents","This reader is not able to provide simulated particles.");
295      return 0;
296    } 
297    
298   if (!fIsRead) 
299    {
300     if (ReadsSim() && (fRunSim == 0x0)) fRunSim = new AliAODRun();
301     if (ReadsRec() && (fRunRec == 0x0)) fRunRec = new AliAODRun();
302     
303     if (Read(fRunSim,fRunRec))
304      {
305        Error("GetNumberOfPartEvents","Error in reading");
306        return 0;
307      }
308     else fIsRead = kTRUE;
309    }
310    return fRunSim->GetNumberOfEvents();
311  }
312 /********************************************************************/
313  
314 Int_t AliReader::GetNumberOfRecEvents()
315  {
316  //returns number of events of tracks
317   if (ReadsRec() == kFALSE)
318    {
319      Error("GetNumberOfTrackEvents","This reader is not able to provide recosntructed tracks.");
320      return 0;
321    } 
322   if (!fIsRead)
323    {
324      if (ReadsSim() && (fRunSim == 0x0)) fRunSim = new AliAODRun();
325      if (ReadsRec() && (fRunRec == 0x0)) fRunRec = new AliAODRun();
326      
327      if(Read(fRunSim,fRunRec))
328       {
329         Error("GetNumberOfTrackEvents","Error in reading");
330         return 0;
331       }
332      else fIsRead = kTRUE;
333    }
334   return fRunRec->GetNumberOfEvents();
335  }
336 /********************************************************************/
337
338 Int_t AliReader::Read(AliAODRun* particles, AliAODRun *tracks)
339 {
340  //reads data and puts put to the particles and tracks objects
341  //reurns 0 if everything is OK
342  //
343   Info("Read","");
344   
345   if ( ReadsSim() && (particles == 0x0) ) //check if an object is instatiated
346    {
347      Error("Read"," particles object must be instatiated before passing it to the reader");
348      return 1;
349    }
350   if ( ReadsRec() && (tracks == 0x0) )  //check if an object is instatiated
351    {
352      Error("Read"," tracks object must be instatiated before passing it to the reader");
353      return 1;
354    }
355    
356   if (ReadsSim()) particles->Reset();//clear runs == delete all old events
357   if (ReadsRec()) tracks->Reset();
358   
359   Rewind();
360   
361   Int_t i = 0;
362   while(Next() == kFALSE)
363    {
364      if (ReadsRec()) tracks->SetEvent(i,fEventRec);
365      if (ReadsSim()) particles->SetEvent(i,fEventSim);
366      i++;
367    }
368   return 0;
369 }      
370 /*************************************************************************************/
371
372 Bool_t AliReader::Rejected(AliVAODParticle* p)
373 {
374  //Method examines whether particle meets all cut and particle type criteria
375   
376    if(p==0x0)//of corse we not pass NULL pointers
377     {
378      Warning("Rejected()","No Pasaran! We never accept NULL pointers");
379      return kTRUE;
380     }
381    //if no particle is specified, we pass all particles
382    //excluding NULL pointers, of course
383   if ( fCuts->GetEntriesFast() == 0 ) return kFALSE; //if no cut specified accept all particles
384   for(Int_t i=0; i<fCuts->GetEntriesFast(); i++)   
385    {
386      AliAODParticleCut &cut = *((AliAODParticleCut*)fCuts->At(i));
387      if(!cut.Rejected(p)) return kFALSE;  //accepted
388    }
389    
390   return kTRUE;//not accepted
391 }
392 /*************************************************************************************/
393
394 Bool_t  AliReader::Rejected(Int_t pid)
395 {
396 //this method checks if any of existing cuts accepts this pid particles
397 //or any cuts accepts all particles
398
399  if(pid == 0)
400   return kTRUE;
401
402  if ( fCuts->GetEntriesFast() == 0 ) return kFALSE; //if no cut specified accept all particles
403   
404  for(Int_t i=0; i<fCuts->GetEntriesFast(); i++)   
405    {
406      AliAODParticleCut &cut = *((AliAODParticleCut*)fCuts->At(i));
407      //if some of cuts accepts all particles or some accepts particles of this type, accept
408      if ( (cut.GetPID() == 0) || (cut.GetPID() == pid) ) return kFALSE; 
409    }
410  return kTRUE;
411 }
412 /*************************************************************************************/
413
414 TString AliReader::GetDirName(Int_t entry)
415 {
416 //returns directory name of next one to read
417   TString  retval;//return value
418   if (fDirs ==  0x0)
419    { 
420      if (entry == 0)
421       {
422        retval = ".";
423        return retval;
424       }
425      else
426       {
427        return retval;
428       }  
429    }
430   
431   
432   if ( (entry >= fDirs->GetEntries()) || (entry < 0))//if out of bounds return empty string
433    {                                            //note that entry==0 is accepted even if array is empty (size=0)
434     if ( (fDirs->GetEntries() == 0) && (entry == 0) )
435       { 
436         retval = ".";
437         return retval;
438       }
439      AliDebug(1,Form("Index %d out of bounds",entry));
440
441      return retval;
442    }
443
444
445   TClass *objclass = fDirs->At(entry)->IsA();
446   TClass *stringclass = TObjString::Class();
447
448   TObjString *dir = (TObjString*)objclass->DynamicCast(stringclass,fDirs->At(entry));
449
450   if(dir == 0x0)
451    {
452      Error("GetDirName","Object in TObjArray is not a TObjString or its descendant");
453      return retval;
454    }
455   AliDebug(1,Form("Returned ok %s",dir->String().Data()));
456   retval = dir->String();
457   return retval;
458 }
459 /*************************************************************************************/
460
461 void AliReader::Blend()
462 {
463   //randomly change positions of the particles after reading
464   //is used to check if some distributions (of many particle properties) 
465   //depend on the order of particles
466   //(tracking gives particles Pt sorted)
467   Int_t npart = 0;
468
469   if (fEventSim ) 
470    {
471      npart = fEventSim->GetNumberOfParticles();
472     } 
473   else 
474     if (fEventRec ) 
475      {
476         npart = fEventRec->GetNumberOfParticles();
477      }
478     else
479      {
480        return;
481      }
482   for (Int_t i = 2;  i < npart; i++)
483    {
484      Int_t with = gRandom->Integer(i);
485 //     Info("Blend","%d %d",i, with);
486      if (fEventSim) fEventSim->SwapParticles(i,with);
487      if (fEventRec) fEventRec->SwapParticles(i,with);
488    }
489 }
490 /*************************************************************************************/
491
492 void AliReader::WriteTrackCounter() const
493 {
494  //writes the counter histogram
495  
496  if (fTrackCounter) fTrackCounter->Write(0,TObject::kOverwrite);
497  else 
498   {
499     Warning("WriteTrackCounter","Counter is NULL");
500   }
501 }