]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EVGEN/AliGenReaderTreeK.cxx
Added ToF as data member
[u/mrichter/AliRoot.git] / EVGEN / AliGenReaderTreeK.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 // Realisation of AliGenReader to be used with AliGenExtFile
19 // It reads events from a kinematics TreeK.
20 // NextEvent() is used to loop over events 
21 // and NextParticle() to loop over particles.  
22 // Author: andreas.morsch@cern.ch
23 //
24 #include <TFile.h>
25 #include <TTree.h>
26 #include <TParticle.h>
27 #include <TObjString.h>
28 #include <TObjArray.h>
29
30 #include "AliGenReaderTreeK.h"
31 #include "AliHeader.h"
32 #include "AliRun.h"
33 #include "AliRunLoader.h"
34
35 ClassImp(AliGenReaderTreeK);
36
37 const TString AliGenReaderTreeK::fgkEventFolderName("GenReaderTreeK");
38
39 AliGenReaderTreeK::AliGenReaderTreeK():
40     AliGenReader(),
41     fNcurrent(0),
42     fNparticle(0),
43     fNp(0),
44     fInRunLoader(0),
45     fBaseFile(0),
46     fStack(0),
47     fOnlyPrimaries(kFALSE),
48     fDirs(0x0),
49     fCurrentDir(0)
50 {
51 //  Default constructor
52 }
53
54 AliGenReaderTreeK::AliGenReaderTreeK(const AliGenReaderTreeK &reader):
55     AliGenReader(reader),
56     fNcurrent(0),
57     fNparticle(0),
58     fNp(0),
59     fInRunLoader(0),
60     fBaseFile(0),
61     fStack(0),
62     fOnlyPrimaries(kFALSE),
63     fDirs(0x0),
64     fCurrentDir(0)
65 {
66     reader.Copy(*this);
67 }
68
69
70 AliGenReaderTreeK::~AliGenReaderTreeK() 
71 {
72 // Destructor
73     delete fInRunLoader;//it cleans all the loaded data
74     delete fDirs;
75 }
76
77 void AliGenReaderTreeK::Init() 
78 {
79 // Initialization
80 // Connect base file and file to read from
81
82     TTree *ali = gAlice->TreeE();
83     if (ali) {
84       fBaseFile = ali->GetCurrentFile();
85     } else {
86       printf("\n Warning: Basefile cannot be found !\n");
87     }
88     //if (!fFile) fFile  = new TFile(fFileName);
89     if (fInRunLoader == 0x0) 
90      {
91        fInRunLoader = AliRunLoader::Open((GetDirName(fCurrentDir++)+"/")+fFileName,fgkEventFolderName);
92        fInRunLoader->LoadHeader();
93        fInRunLoader->LoadKinematics("READ");
94      }
95 }
96
97 Int_t AliGenReaderTreeK::NextEvent() 
98 {
99 //  Read the next event  
100 //  cd to file with old kine tree    
101     if (!fBaseFile) Init();
102 //  Get next event
103     
104     if (fNcurrent >= fInRunLoader->GetNumberOfEvents())
105      {
106       if (fCurrentDir >= fDirs->GetEntries())
107        {
108          Warning("NextEvent","No more events");
109          return 0;
110        }
111       delete fInRunLoader;
112       fInRunLoader = AliRunLoader::Open((GetDirName(fCurrentDir++)+"/")+fFileName,fgkEventFolderName);
113       fInRunLoader->LoadHeader();
114       fInRunLoader->LoadKinematics("READ");
115       fNcurrent = 0;
116      }
117     fInRunLoader->GetEvent(fNcurrent);
118     fStack = fInRunLoader->Stack();
119     
120 //  cd back to base file
121     fBaseFile->cd();
122 //
123     fNcurrent++;
124     fNparticle = 0;
125     fNp =  fStack->GetNtrack();
126     printf("\n Next event contains %d particles", fNp);
127 //    
128     return  fNp;
129 }
130
131 TParticle* AliGenReaderTreeK::NextParticle() 
132 {
133 //Return next particle
134   TParticle* part = GetParticle(fNparticle++);
135   if (part == 0x0) return 0x0;
136   //if only primaries are to be read, and this particle is not primary enter loop  
137   if (fOnlyPrimaries && ( part->GetFirstMother() > -1) ) 
138    for (;;)
139     { //look for a primary
140       part = GetParticle(fNparticle++);
141       if (part == 0x0) return 0x0;
142       if (part->GetFirstMother() == -1) return part;
143     }
144
145   return part;
146 }
147
148 void AliGenReaderTreeK::RewindEvent()
149 {
150   // Go back to the first particle of the event
151   fNparticle = 0;
152 }
153
154
155 AliGenReaderTreeK& AliGenReaderTreeK::operator=(const  AliGenReaderTreeK& rhs)
156 {
157 // Assignment operator
158     rhs.Copy(*this);
159     return *this;
160 }
161
162 void AliGenReaderTreeK::Copy(TObject&) const
163 {
164     //
165     // Copy 
166     //
167     Fatal("Copy","Not implemented!\n");
168 }
169
170
171
172 TString& AliGenReaderTreeK::GetDirName(Int_t entry)
173  {
174 // Get the current directory name
175
176    TString* retval;//return value
177    if (fDirs ==  0x0)
178     {
179       retval = new TString(".");
180       return *retval;
181     }
182    
183    if ( (entry>fDirs->GetEntries()) || (entry<0))//if out of bounds return empty string
184     {                                            //note that entry==0 is accepted even if array is empty (size=0)
185       Error("GetDirName","Name out of bounds");
186       retval = new TString();
187       return *retval;
188     }
189    
190    if (fDirs->GetEntries() == 0)
191     { 
192       retval = new TString(".");
193       return *retval;
194     }
195    
196    TObjString *dir = dynamic_cast<TObjString*>(fDirs->At(entry));
197    if(dir == 0x0)
198     {
199       Error("GetDirName","Object in TObjArray is not a TObjString or its descendant");
200       retval = new TString();
201       return *retval;
202     }
203    if (gDebug > 0) Info("GetDirName","Returned ok %s",dir->String().Data());
204    return dir->String();
205  }
206  
207 void AliGenReaderTreeK::AddDir(const char* dirname)
208 {
209   //adds a directory to the list of directories where data are looked for
210   if(fDirs == 0x0) 
211    {
212      fDirs = new TObjArray();
213      fDirs->SetOwner(kTRUE);
214    }
215   TObjString *odir= new TObjString(dirname);
216   fDirs->Add(odir);
217 }