Converted to AOD schema
[u/mrichter/AliRoot.git] / HBTAN / AliHBTReaderKineTree.cxx
1 #include "AliHBTReaderKineTree.h"
2 //_______________________________________________________________________
3 /////////////////////////////////////////////////////////////////////////
4 //
5 // class AliHBTReaderKineTree
6 //
7 // Reader for Kinematics
8 //
9 // Piotr.Skowronski@cern.ch
10 //
11 /////////////////////////////////////////////////////////////////////////
12
13 #include <TString.h>
14 #include <TParticle.h>
15
16 #include <AliRunLoader.h>
17 #include <AliStack.h>
18
19 #include "AliHBTEvent.h"
20 #include "AliHBTParticle.h"
21
22 ClassImp(AliHBTReaderKineTree)
23 /**********************************************************/
24 const TString AliHBTReaderKineTree::fgkEventFolderName("HBTReaderKineTree");
25
26 AliHBTReaderKineTree::AliHBTReaderKineTree():
27  fFileName("galice.root"),
28  fRunLoader(0x0)
29 {
30   //ctor
31 }
32 /**********************************************************/
33
34 AliHBTReaderKineTree::AliHBTReaderKineTree(TString& fname):
35  fFileName(fname),
36  fRunLoader(0x0)
37 {
38   //ctor
39 }
40 /**********************************************************/
41
42 AliHBTReaderKineTree::AliHBTReaderKineTree(TObjArray* dirs,const Char_t *filename):
43  AliHBTReader(dirs),
44  fFileName(filename),
45  fRunLoader(0x0)
46 {
47   //ctor
48 }
49
50 /**********************************************************/
51 AliHBTReaderKineTree::AliHBTReaderKineTree(const AliHBTReaderKineTree& in):
52  AliHBTReader(in),
53  fFileName(in.fFileName),
54  fRunLoader(0x0)
55 {
56   //cpy ctor
57 }
58
59 /**********************************************************/
60
61 AliHBTReaderKineTree::~AliHBTReaderKineTree()
62 {
63   //dtor
64   delete fRunLoader;
65 }
66 /**********************************************************/
67 AliHBTReaderKineTree& AliHBTReaderKineTree::operator=(const AliHBTReaderKineTree& in)
68 {
69 //Assiment operator
70   if (this == &in) return *this;
71   AliHBTReader::operator=(in);
72   delete fRunLoader;
73   fRunLoader = 0x0;
74   return * this;
75 }
76 /**********************************************************/
77
78 void AliHBTReaderKineTree::Rewind()
79 {
80 //Rewinds to the beginning
81   delete fRunLoader;
82   fRunLoader = 0x0;
83   fCurrentDir = 0;
84   fNEventsRead= 0;  
85 }
86 /**********************************************************/
87
88 Int_t AliHBTReaderKineTree::ReadNext()
89 {
90  //Reads Kinematics Tree
91   
92  Info("Read","");
93  if (fParticlesEvent == 0x0)  fParticlesEvent = new AliHBTEvent();
94  fParticlesEvent->Reset();
95
96  do  //do{}while; is OK even if 0 dirs specified. In that case we try to read from "./"
97   { 
98     if (fRunLoader == 0x0) 
99       if (OpenNextFile()) 
100         { 
101           fCurrentDir++;
102           continue;
103         }
104     
105     if (fCurrentEvent == fRunLoader->GetNumberOfEvents())
106      {
107        //read next directory
108        delete fRunLoader;//close current session
109        fRunLoader = 0x0;//assure pointer is null
110        fCurrentDir++;//go to next dir
111        continue;//directory counter is increased inside in case of error
112      }
113      
114     Info("ReadNext","Reading Event %d",fCurrentEvent);
115     
116     fRunLoader->GetEvent(fCurrentEvent);
117     
118     AliStack* stack = fRunLoader->Stack();
119     if (!stack)
120      {
121        Error("ReadNext","Can not get stack for event %d",fCurrentEvent);
122        continue;
123      }
124     Int_t npart = stack->GetNtrack();
125     for (Int_t i = 0;i<npart; i++)
126       {
127          TParticle * p = stack->Particle(i);
128 //         if (p->GetFirstMother() >= 0) continue; do not apply with pythia etc
129          
130          if(Pass(p->GetPdgCode())) continue; //check if we are intersted with particles of this type 
131                                              //if not take next partilce
132          
133          AliHBTParticle* part = new AliHBTParticle(*p,i);
134          if(Pass(part)) { delete part; continue;}//check if meets all criteria of any of our cuts
135                                                   //if it does not delete it and take next good track
136          fParticlesEvent->AddParticle(part);//put particle in event
137       }
138     Info("ReadNext","Read %d particles from event %d (event %d in dir %d).",
139                      fParticlesEvent->GetNumberOfParticles(),
140                      fNEventsRead,fCurrentEvent,fCurrentDir);
141       
142     fCurrentEvent++;
143     fNEventsRead++;
144     return 0;
145   }while(fCurrentDir < GetNumberOfDirs());//end of loop over directories specified in fDirs Obj Array
146   
147  return 1;
148 }
149 /**********************************************************/
150
151 Int_t AliHBTReaderKineTree::OpenNextFile()
152 {
153 //opens file with kine tree
154  Info("OpenNextFile","________________________________________________________");
155  
156  const TString& dirname = GetDirName(fCurrentDir);
157  if (dirname == "")
158   {
159    Error("OpenNextFile","Can not get directory name");
160    return 1;
161   }
162  TString filename = dirname +"/"+ fFileName;
163
164  fRunLoader = AliRunLoader::Open(filename.Data(),fgkEventFolderName,"READ"); 
165
166  if ( fRunLoader == 0x0)
167   {
168     Error("OpenNextFile","Can't open session from file %s",filename.Data());
169     return 1;
170   }
171   
172  if (fRunLoader->GetNumberOfEvents() <= 0)
173   {
174     Error("OpenNextFile","There is no events in this directory.");
175     delete fRunLoader;
176     fRunLoader = 0x0;
177     return 2;
178   }
179   
180  if (fRunLoader->LoadKinematics())
181   {
182     Error("OpenNextFile","Error occured while loading kinematics.");
183     return 3;
184   }
185   
186  fCurrentEvent = 0;
187  return 0;
188 }