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