few changes by Thomas Kuhr
[u/mrichter/AliRoot.git] / ANALYSIS / AliRunAnalysis.cxx
CommitLineData
b26900d0 1#include "AliRunAnalysis.h"
2//________________________________
3///////////////////////////////////////////////////////////
4//
5// class AliRunAnalysis
6//
7//
8//
9// Piotr.Skowronski@cern.ch
10//
11///////////////////////////////////////////////////////////
12
13#include <stdlib.h>
14
15#include <TString.h>
16#include <TObjString.h>
17#include <TClass.h>
18#include <TFile.h>
19#include <TKey.h>
20#include <TObjArray.h>
21
22#include <AliRun.h>
23#include <AliRunLoader.h>
24#include <AliStack.h>
25#include <AliESDtrack.h>
26#include <AliESD.h>
27
28#include "AliEventCut.h"
29
b26900d0 30
31ClassImp(AliRunAnalysis)
32AliRunAnalysis::AliRunAnalysis():
91757255 33 TTask("RunAnalysis","Alice Analysis Manager") ,
b26900d0 34 fDirs(),
35 fEventCut(0x0),
36 fFileName("AliESDs.root"),
37 fReadKinematics(kFALSE)
38{
39 //ctor
40}
41/*********************************************************/
42
43AliRunAnalysis::~AliRunAnalysis()
44{
45 //dtor
46 delete fDirs;
47 delete fAnalysies;
48 delete fEventCut;
49}
50/*********************************************************/
51
52Int_t AliRunAnalysis::Run()
53{
54 //makes analysis
55
56 Int_t currentdir = 0;
57 Int_t ndirs;
58 if (fDirs) //if array with directories is supplied by user
59 {
60 ndirs = fDirs->GetEntries(); //get the number if directories
61 }
62 else
63 {
64 ndirs = 0; //if the array is not supplied read only from current directory
65 }
66
67 /******************************/
68 /* Init Event */
69 /******************************/
70 if (fAnalysies == 0x0)
71 {
72 Info("Run","No analysis present");
73 return 0;
74 }
75
76 for (Int_t an = 0; an < fAnalysies->GetEntries(); an++)
77 {
78 AliAnalysis* analysis = (AliAnalysis*)fAnalysies->At(an);
79 analysis->Init();
80 }
81
82 do
83 {
84 TFile* file = OpenFile(currentdir);
85 if (file == 0x0)
86 {
87 Error("Run","Cannot get File for dir no. %d",currentdir);
88 currentdir++;
89 continue;
90 }
91 AliStack* stack = 0x0;
92 AliRunLoader* rl = 0x0;
93 if (fReadKinematics)
94 {
95 const TString& dirname = GetDirName(currentdir);
96 if (dirname == "")
97 {
98 Error("Run","Can not get directory name");
99 return 0x0;
100 }
101 TString filename = dirname +"/galice.root";
102
103 rl = AliRunLoader::Open(filename);
104 if ( rl == 0x0 )
105 {
106 Error("Run","Can't get Run Loader from dir %s",filename.Data());
107 delete file;
108 currentdir++;
109 continue;
110 }
111 if( rl->LoadHeader() )
112 {
113 Error("Run","Error while loading Header from dir %s",filename.Data());
114 delete file;
115 delete rl;
116 currentdir++;
117 continue;
118 }
119 if( rl->LoadKinematics() )
120 {
121 Error("Run","Error while loading Kinematics from dir %s",filename.Data());
122 delete file;
123 delete rl;
124 currentdir++;
125 continue;
126 }
127 }
128
129 file->cd();
130 TIter keyiter(file->GetListOfKeys());
131 TKey* key;
132 while (( key = (TKey*)keyiter.Next() ))
133 {
134 if (key == 0x0)
135 {
136 if (GetDebug() > 2 )
137 {
138 Info("Run","No more keys.");
139 }
140 break;
141 }
142
143 TObject* esdobj = key->ReadObj();
144 if (esdobj == 0x0)
145 {
146 if (GetDebug() > 2 )
147 {
148 Info("ReadNext","Key read NULL. Key Name is %s",key->GetName());
149 key->Dump();
150 }
151 continue;
152 }
153 if (GetDebug() > 9 ) esdobj->Dump();
154 AliESD* esd = dynamic_cast<AliESD*>(esdobj);
155
156 if (esd == 0x0)
157 {
158 if (GetDebug() > 7 )
159 {
160 Info("ReadNext","It is not an ESD object");
161 }
162 delete esdobj;
163 continue;
164 }
165
166 if (fReadKinematics)
167 {
168 TString esdname(esd->GetName());
169 esdname.ReplaceAll("ESD","");
170 Int_t nev = atoi(esdname);
171 Info("Run","ESD name is %s, Event Number is %d",esd->GetName(),nev);
172 if (rl->GetEvent(nev))
173 {
174 Error("Run","Error occured while RunLoader GetEvent %d",nev);
175 delete esd;
176 continue;
177 }
178 stack = rl->Stack();
179 if (stack == 0x0)
180 {
181 Error("Run","Can not get stack");
182 delete esd;
183 continue;
184 }
185 }
186 /******************************/
187 /* Event Cut */
188 /******************************/
189 if (fEventCut)
190 {
191 if (fEventCut->Pass(esd))
192 {
193 if (GetDebug()) Info("Run","Event rejected by Event Cut");
194 delete esd;
195 continue; //Did not pass the
196 }
197 }
198 /******************************/
199 /* Process Event */
200 /******************************/
201 for (Int_t an = 0; an < fAnalysies->GetEntries(); an++)
202 {
203 AliAnalysis* analysis = (AliAnalysis*)fAnalysies->At(an);
204 analysis->ProcessEvent(esd,stack);
205 }
206 delete esd;
207 }//end of loop over keys in file
208
209 delete file;
210 delete rl;
211 currentdir++;
212
213 }while (currentdir < ndirs);//end of loop over directories
214
215 /******************************/
216 /* Finish Event */
217 /******************************/
218 for (Int_t an = 0; an < fAnalysies->GetEntries(); an++)
219 {
220 AliAnalysis* analysis = (AliAnalysis*)fAnalysies->At(an);
221 analysis->Init();
222 }
223
224 return 0;
225}
226/*********************************************************/
227
228TFile* AliRunAnalysis::OpenFile(Int_t n)
229{
230//opens file with kine tree
231
232 const TString& dirname = GetDirName(n);
233 if (dirname == "")
234 {
235 Error("OpenFiles","Can not get directory name");
236 return 0x0;
237 }
238 TString filename = dirname +"/"+ fFileName;
239 TFile *ret = TFile::Open(filename.Data());
240
241 if ( ret == 0x0)
242 {
243 Error("OpenFiles","Can't open file %s",filename.Data());
244 return 0x0;
245 }
246 if (!ret->IsOpen())
247 {
248 Error("OpenFiles","Can't open file %s",filename.Data());
249 return 0x0;
250 }
251
252 return ret;
253}
254/*********************************************************/
255
256TString& AliRunAnalysis::GetDirName(Int_t entry)
257{
258//returns directory name of next one to read
259 TString* retval;//return value
260 if (fDirs == 0x0)
261 {
262 retval = new TString(".");
263 return *retval;
264 }
265
266 if ( (entry>fDirs->GetEntries()) || (entry<0))//if out of bounds return empty string
267 { //note that entry==0 is accepted even if array is empty (size=0)
268 Error("GetDirName","Name out of bounds");
269 retval = new TString();
270 return *retval;
271 }
272
273 if (fDirs->GetEntries() == 0)
274 {
275 retval = new TString(".");
276 return *retval;
277 }
278
279 TClass *objclass = fDirs->At(entry)->IsA();
280 TClass *stringclass = TObjString::Class();
281
282 TObjString *dir = (TObjString*)objclass->DynamicCast(stringclass,fDirs->At(entry));
283
284 if(dir == 0x0)
285 {
286 Error("GetDirName","Object in TObjArray is not a TObjString or its descendant");
287 retval = new TString();
288 return *retval;
289 }
290 if (gDebug > 0) Info("GetDirName","Returned ok %s",dir->String().Data());
291 return dir->String();
292}
293/*********************************************************/
294
295void AliRunAnalysis::Add(AliAnalysis* a)
296{
297 //adds a to the list of analysis
298 if (fAnalysies == 0x0) fAnalysies = new TObjArray();
299 fAnalysies->Add(a);
300}