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