Fix problems with includes (Diego)
[u/mrichter/AliRoot.git] / PWG1 / TRD / AliTRDrecoTask.cxx
CommitLineData
94b94be0 1///////////////////////////////////////////////////////\r
2//\r
3// Basic class for Performance/Calibration TRD tasks\r
4// \r
5// It performs generic tasks like :\r
6// - data file manegment\r
7// - reference container management\r
8// - debug container management\r
9// - interaction with AliAnalysisManager\r
10// - Plot functor loop\r
11//\r
12// Author: Alexandru Bercuci <A.Bercuci@gsi.de>, 10/09/2008\r
13//\r
14/////////////////////////////////////////////////////////\r
15\r
16#include "TClass.h"\r
17#include "TMethod.h"\r
18#include "TMethodCall.h"\r
19#include "TMethodArg.h"\r
20#include "TFile.h"\r
21#include "TChain.h"\r
22#include "TList.h"\r
23#include "TMap.h"\r
24#include "TH1.h"\r
25#include "TF1.h"\r
26#include "TObjArray.h"\r
27#include "TDirectory.h"\r
28#include "TTreeStream.h"\r
82e6e5dc 29#include "TBox.h"\r
30#include "TVectorT.h"\r
94b94be0 31\r
32#include "AliLog.h"\r
33#include "AliAnalysisTask.h"\r
100f4577 34#include "AliAnalysisManager.h"\r
3ceb45ae 35#include "AliExternalTrackParam.h"\r
94b94be0 36\r
37#include "info/AliTRDeventInfo.h"\r
38#include "AliTRDrecoTask.h"\r
3ceb45ae 39#include "AliTRDtrackV1.h"\r
40#include "AliTRDpidUtil.h"\r
94b94be0 41\r
42ClassImp(AliTRDrecoTask)\r
43\r
44TList* AliTRDrecoTask::fgTrendPoint(NULL);\r
45TTreeSRedirector* AliTRDrecoTask::fgDebugStream(NULL);\r
46//_______________________________________________________\r
47AliTRDrecoTask::AliTRDrecoTask()\r
48 : AliAnalysisTaskSE()\r
49 ,fNRefFigures(0)\r
82e6e5dc 50 ,fDets(NULL)\r
94b94be0 51 ,fContainer(NULL)\r
52 ,fEvent(NULL)\r
53 ,fTracks(NULL)\r
f073d500 54 ,fClusters(NULL)\r
55 ,fkClusters(NULL)\r
94b94be0 56 ,fkTrack(NULL)\r
57 ,fkMC(NULL)\r
58 ,fkESD(NULL)\r
3ceb45ae 59 ,fSpecies(-6)\r
60 ,fPt(-1.)\r
61 ,fPhi(0.)\r
62 ,fEta(0.)\r
94b94be0 63 ,fPlotFuncList(NULL)\r
f073d500 64 ,fDetFuncList(NULL)\r
94b94be0 65 ,fRunTerminate(kFALSE)\r
66{\r
67// Default constructor\r
68 snprintf(fNameId, 10, "no name");\r
69}\r
70\r
71//_______________________________________________________\r
72AliTRDrecoTask::AliTRDrecoTask(const char *name, const char *title)\r
73 : AliAnalysisTaskSE(name)\r
74 ,fNRefFigures(0)\r
82e6e5dc 75 ,fDets(NULL)\r
94b94be0 76 ,fContainer(NULL)\r
77 ,fEvent(NULL)\r
78 ,fTracks(NULL)\r
f073d500 79 ,fClusters(NULL)\r
80 ,fkClusters(NULL)\r
94b94be0 81 ,fkTrack(NULL)\r
82 ,fkMC(NULL)\r
83 ,fkESD(NULL)\r
3ceb45ae 84 ,fSpecies(-6)\r
85 ,fPt(-1.)\r
86 ,fPhi(0.)\r
87 ,fEta(0.)\r
94b94be0 88 ,fPlotFuncList(NULL)\r
f073d500 89 ,fDetFuncList(NULL)\r
94b94be0 90 ,fRunTerminate(kFALSE)\r
91{\r
92// Constructor for all derived performance tasks\r
93\r
94 SetTitle(title);\r
95 snprintf(fNameId, 10, "no name");\r
96 DefineInput (1, TObjArray::Class()); // track list\r
97 DefineInput (2, AliTRDeventInfo::Class()); // event info object\r
f073d500 98 DefineInput (3, TObjArray::Class()); // cluster list object\r
94b94be0 99 DefineOutput(1, TObjArray::Class()); // histogram list\r
100}\r
101\r
102//_______________________________________________________\r
103AliTRDrecoTask::~AliTRDrecoTask() \r
104{\r
105\r
106 // Generic task destructor\r
107\r
108 AliDebug(2, Form(" Ending task %s[%s]", GetName(), GetTitle()));\r
109 if(fgDebugStream){ \r
110 delete fgDebugStream;\r
111 fgDebugStream = NULL;\r
112 }\r
113\r
114 if(fPlotFuncList){\r
115 fPlotFuncList->Delete();\r
116 delete fPlotFuncList;\r
117 fPlotFuncList = NULL;\r
118 }\r
f073d500 119 if(fDetFuncList){\r
120 fDetFuncList->Delete();\r
121 delete fDetFuncList;\r
122 fDetFuncList = NULL;\r
123 }\r
94b94be0 124 \r
82e6e5dc 125 if(fDets){\r
126 if(fDets->IsOwner()) fDets->Delete();\r
127 delete fDets;\r
128 fDets = NULL;\r
129 }\r
130\r
f0857a6a 131 if(fContainer && !(AliAnalysisManager::GetAnalysisManager() && AliAnalysisManager::GetAnalysisManager()->IsProofMode())){\r
94b94be0 132 if(fContainer->IsOwner()) fContainer->Delete();\r
133 delete fContainer;\r
134 fContainer = NULL;\r
135 }\r
136\r
137 if(fgTrendPoint){\r
138 TFile::Open("TRD.PerformanceTrend.root", "UPDATE");\r
139 fgTrendPoint->Write();\r
140 delete fgTrendPoint;\r
141 fgTrendPoint=NULL;\r
142 gFile->Close();\r
143 }\r
144}\r
145\r
146//_______________________________________________________\r
147Int_t AliTRDrecoTask::GetNRefFigures() const \r
148{ \r
149 if(!fNRefFigures) AliWarning("No reference plots available.");\r
150 return fNRefFigures; \r
151} \r
152\r
153//_______________________________________________________\r
154void AliTRDrecoTask::UserCreateOutputObjects()\r
155{\r
156 if(!HasFunctorList()) InitFunctorList();\r
157 fContainer = Histos();\r
158 PostData(1, fContainer);\r
159}\r
160\r
161//_______________________________________________________\r
162void AliTRDrecoTask::UserExec(Option_t *)\r
163{\r
164// Loop over Plot functors published by particular tasks\r
165\r
f073d500 166 fTracks = dynamic_cast<TObjArray *>(GetInputData(1));\r
167 fEvent = dynamic_cast<AliTRDeventInfo *>(GetInputData(2));\r
168 fClusters = dynamic_cast<TObjArray*>(GetInputData(3));\r
94b94be0 169\r
170 if(!fPlotFuncList){\r
f073d500 171 AliWarning("No track functor list defined for the task");\r
94b94be0 172 return;\r
173 }\r
174 if(!fTracks) return;\r
175 if(!fTracks->GetEntriesFast()) return;\r
176 else AliDebug(2, Form("Tracks[%d] for %s", fTracks->GetEntriesFast(), GetName()));\r
177\r
178d284a 178 Int_t itrk(-1);\r
179 AliTRDtrackInfo *trackInfo(NULL);\r
94b94be0 180 TIter plotIter(fPlotFuncList);\r
181 TObjArrayIter trackIter(fTracks);\r
182 while((trackInfo = dynamic_cast<AliTRDtrackInfo*>(trackIter()))){\r
178d284a 183 itrk++; fPt=-1; fEta=0.; fPhi=0.; fSpecies=-6;\r
94b94be0 184 fkMC = trackInfo->GetMCinfo();\r
185 fkESD = trackInfo->GetESDinfo();\r
178d284a 186 if((fkTrack = trackInfo->GetTrack())){\r
187 // cache properties of the track at TRD entrance\r
188 // check input track status\r
189 AliExternalTrackParam *tin(NULL);\r
190 if(!(tin = fkTrack->GetTrackIn())) AliDebug(2, Form("Missing TRD track[%d] :: entry point.", itrk));\r
191 else {\r
192 fPt = tin->Pt();\r
193 fEta = tin->Eta();\r
194 Double_t xyz[3];\r
195 if(!tin->GetXYZ(xyz)) AliDebug(2, Form("Failed TRD track[%d] :: global track postion", itrk));\r
196 else fPhi = TMath::ATan2(xyz[1], xyz[0]);\r
197 fSpecies= fkTrack->Charge()*(AliTRDpidUtil::Mass2Pid(fkTrack->GetMass())+1);\r
198 }\r
199 } else AliDebug(2, Form("Missing TRD track[%d].", itrk));\r
200\r
201 TMethodCall *plot(NULL);\r
94b94be0 202 plotIter.Reset();\r
178d284a 203 while((plot=dynamic_cast<TMethodCall*>(plotIter()))) plot->Execute(this);\r
94b94be0 204 }\r
f073d500 205 if(!fClusters) return;\r
206 if(!fDetFuncList){\r
178d284a 207 AliDebug(1, "No detector functor list defined for task");\r
f073d500 208 return;\r
209 }\r
210 TIter detIter(fDetFuncList);\r
211 for(Int_t idet(0); idet<AliTRDgeometry::kNdet; idet++){\r
212 if(!(fkClusters = (TObjArray*)fClusters->At(idet))) continue;\r
213 TMethodCall *det(NULL);\r
214 detIter.Reset();\r
178d284a 215 while((det=dynamic_cast<TMethodCall*>(detIter()))) det->Execute(this);\r
f073d500 216 }\r
94b94be0 217}\r
218\r
219//_______________________________________________________\r
220Bool_t AliTRDrecoTask::GetRefFigure(Int_t /*ifig*/)\r
221{\r
222 AliWarning("Retrieving reference figures not implemented.");\r
223 return kFALSE;\r
224}\r
225\r
226//_______________________________________________________\r
227Bool_t AliTRDrecoTask::PutTrendValue(const Char_t *name, Double_t val)\r
228{\r
229// Generic publisher for trend values\r
230\r
231 if(!fgTrendPoint){\r
232 fgTrendPoint = new TList();\r
233 fgTrendPoint->SetOwner();\r
234 }\r
235 fgTrendPoint->AddLast(new TNamed(Form("%s_%s", GetName(), name), Form("%f", val)));\r
236 return kTRUE;\r
237}\r
238\r
239//_______________________________________________________\r
240void AliTRDrecoTask::InitFunctorList()\r
241{\r
242// Initialize list of functors\r
243\r
244 TClass *c = this->IsA();\r
3ed01fbe 245 if(fPlotFuncList) fPlotFuncList->Clear();\r
f073d500 246 if(fDetFuncList) fDetFuncList->Clear();\r
94b94be0 247\r
f073d500 248 TMethod *m(NULL);\r
94b94be0 249 TIter methIter(c->GetListOfMethods());\r
250 while((m=dynamic_cast<TMethod*>(methIter()))){\r
251 TString name(m->GetName());\r
f073d500 252 if(name.BeginsWith("Plot")){\r
253 if(!fPlotFuncList) fPlotFuncList = new TList();\r
254 fPlotFuncList->AddLast(new TMethodCall(c, (const char*)name, ""));\r
255 } else if(name.BeginsWith("Det")){\r
256 if(!fDetFuncList) fDetFuncList = new TList();\r
257 fDetFuncList->AddLast(new TMethodCall(c, (const char*)name, ""));\r
258 }\r
94b94be0 259 }\r
260}\r
261\r
262//_______________________________________________________\r
263Bool_t AliTRDrecoTask::Load(const Char_t *file, const Char_t *dir)\r
264{\r
265// Generic container loader\r
266\r
267 if(!TFile::Open(file)){\r
268 AliWarning(Form("Couldn't open file %s.", file));\r
269 return kFALSE;\r
270 }\r
271 if(!gFile->cd(dir)){\r
272 AliWarning(Form("Couldn't cd to %s in %s.", dir, file));\r
273 return kFALSE;\r
274 }\r
275 TObjArray *o = NULL;\r
276 if(!(o = (TObjArray*)gDirectory->Get(GetName()))){\r
277 AliWarning("Missing histogram container.");\r
278 return kFALSE;\r
279 }\r
280 fContainer = (TObjArray*)o->Clone(GetName());\r
281 gFile->Close();\r
282 return kTRUE;\r
283}\r
284\r
285//________________________________________________________\r
82e6e5dc 286Bool_t AliTRDrecoTask::LoadDetectorMap(const Char_t *file, const Char_t *dir)\r
287{\r
288// Load detector map.\r
289\r
290 if(!TFile::Open(file)){\r
291 AliWarning(Form("Couldn't open file %s.", file));\r
292 return kFALSE;\r
293 }\r
294 if(!gFile->cd(dir)){\r
295 AliWarning(Form("Couldn't cd to %s in %s.", dir, file));\r
296 return kFALSE;\r
297 }\r
298 TObjArray *info = NULL;\r
299 if(!(info = (TObjArray*)gDirectory->Get("TRDinfoGen"))){\r
300 AliWarning("Missing TRDinfoGen container.");\r
301 return kFALSE;\r
302 }\r
303 TObjArray *dets = (TObjArray*)info->FindObject("Chambers");\r
304 if(!dets){\r
305 AliWarning("Missing detector map from TRDinfoGen results.");\r
306 info->ls();\r
307 return kFALSE;\r
308 }\r
309 fDets = (TObjArray*)dets->Clone("Chambers");\r
310 gFile->Close();\r
311 return kTRUE;\r
312}\r
313\r
314\r
315//________________________________________________________\r
94b94be0 316Bool_t AliTRDrecoTask::Save(TObjArray * const results){\r
317 //\r
318 // Store the output graphs in a ROOT file\r
319 // Input TObject array will not be written as Key to the file,\r
320 // only content itself\r
321 //\r
322\r
323 TDirectory *cwd = gDirectory;\r
324 if(!TFile::Open(Form("TRD.Result%s.root", GetName()), "RECREATE")) return kFALSE;\r
325\r
326 TIterator *iter = results->MakeIterator();\r
327 TObject *inObject = NULL, *outObject = NULL;\r
328 while((inObject = iter->Next())){\r
329 outObject = inObject->Clone();\r
330 outObject->Write(NULL, TObject::kSingleKey);\r
331 }\r
332 delete iter;\r
333 gFile->Close(); delete gFile;\r
334 cwd->cd(); \r
335 return kTRUE;\r
336}\r
337\r
338//_______________________________________________________\r
339Bool_t AliTRDrecoTask::PostProcess()\r
340{\r
341// To be implemented by particular tasks\r
342\r
343 AliWarning("Post processing of reference histograms not implemented.");\r
344 return kTRUE;\r
345}\r
346\r
347//_______________________________________________________\r
82e6e5dc 348void AliTRDrecoTask::MakeDetectorPlot(Int_t ly)\r
349{\r
350// Draw chamber boundaries in eta/phi plots with misalignments\r
351// based on info collected by AliTRDinfoGen\r
352\r
353 if(!fDets){\r
354 AliWarning("Detector map and status not available.");\r
355 return;\r
356 }\r
357\r
358 TBox *gdet = new TBox();\r
359 gdet->SetLineWidth(kBlack);gdet->SetFillColor(kBlack);\r
360 Int_t style[] = {0, 3003};\r
361 for(Int_t idet(0); idet<540; idet++){\r
362 if(idet%6 != ly) continue;\r
363 TVectorF *det((TVectorF*)fDets->At(idet));\r
364 if(!det) continue;\r
82e6e5dc 365 Int_t iopt = Int_t((*det)[4]);\r
f073d500 366 AliDebug(2, Form("det[%03d] 0[%+4.1f %+4.1f] 1[%+4.1f %+4.1f] opt[%d]", idet, (*det)[0], (*det)[1], (*det)[2], (*det)[3], iopt));\r
6687261d 367 if(iopt==1){\r
07b3a6a6 368 gdet->SetFillStyle(style[1]);gdet->SetFillColor(kBlack);\r
369 gdet->DrawBox((*det)[0], (*det)[1], (*det)[2], (*det)[3]);\r
370 } else {\r
371 gdet->SetFillStyle(style[0]);\r
372 gdet->DrawBox((*det)[0], (*det)[1], (*det)[2], (*det)[3]);\r
6687261d 373 if(iopt==2){\r
07b3a6a6 374 gdet->SetFillStyle(style[1]);gdet->SetFillColor(kBlue);\r
375 gdet->DrawBox((*det)[0], (*det)[1], (*det)[2], 0.5*((*det)[3]+(*det)[1]));\r
6687261d 376 } else if(iopt==3){\r
07b3a6a6 377 gdet->SetFillStyle(style[1]);gdet->SetFillColor(kRed);\r
378 gdet->DrawBox((*det)[0], 0.5*((*det)[3]+(*det)[1]), (*det)[2], (*det)[3]);\r
379 } else if(iopt!=0) AliError(Form("Wrong chmb. status[%d] for det[%03d]", iopt, idet));\r
380 }\r
82e6e5dc 381 }\r
382}\r
383\r
384\r
385//_______________________________________________________\r
94b94be0 386void AliTRDrecoTask::MakeSummary()\r
387{\r
388// To be implemented by particular tasks\r
389 AliWarning("Summary not available");\r
390}\r
391\r
392//_______________________________________________________\r
393void AliTRDrecoTask::SetDebugLevel(Int_t level)\r
394{\r
395// Generic debug handler\r
396\r
397 AliAnalysisTaskSE::SetDebugLevel(level);\r
398 if(DebugLevel()>=1){\r
399 AliInfo(Form("Debug Level for Task %s set to %d", GetName(), level));\r
400 TDirectory *savedir = gDirectory;\r
401 fgDebugStream = new TTreeSRedirector("TRD.DebugPerformance.root");\r
402 savedir->cd();\r
403 }\r
404}\r
405\r
406//____________________________________________________________________\r
407void AliTRDrecoTask::Terminate(Option_t *)\r
408{\r
409 //\r
410 // Terminate\r
411 //\r
412\r
413 if(fgDebugStream){ \r
414 delete fgDebugStream;\r
415 fgDebugStream = NULL;\r
416 }\r
417 fContainer = dynamic_cast<TObjArray *>(GetOutputData(1));\r
418 if(fContainer && fRunTerminate){\r
419 PostProcess();\r
420 MakeSummary();\r
421 }\r
422}\r
423\r
424//________________________________________________________\r
425void AliTRDrecoTask::Adjust(TF1 *f, TH1 * const h)\r
426{\r
427// Helper function to avoid duplication of code\r
428// Make first guesses on the fit parameters\r
429\r
430 // find the intial parameters of the fit !! (thanks George)\r
431 Int_t nbinsy = Int_t(.5*h->GetNbinsX());\r
432 Double_t sum = 0.;\r
433 for(Int_t jbin=nbinsy-4; jbin<=nbinsy+4; jbin++) sum+=h->GetBinContent(jbin); sum/=9.;\r
434 f->SetParLimits(0, 0., 3.*sum);\r
435 f->SetParameter(0, .9*sum);\r
436\r
437 f->SetParLimits(1, -.2, .2);\r
438 f->SetParameter(1, -0.1);\r
439\r
440 f->SetParLimits(2, 0., 4.e-1);\r
441 f->SetParameter(2, 2.e-2);\r
442 if(f->GetNpar() <= 4) return;\r
443\r
444 f->SetParLimits(3, 0., sum);\r
445 f->SetParameter(3, .1*sum);\r
446\r
447 f->SetParLimits(4, -.3, .3);\r
448 f->SetParameter(4, 0.);\r
449\r
450 f->SetParLimits(5, 0., 1.e2);\r
451 f->SetParameter(5, 2.e-1);\r
452}\r