major TRD reconstruction update
[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
29\r
30#include "AliLog.h"\r
31#include "AliAnalysisTask.h"\r
32\r
33#include "info/AliTRDeventInfo.h"\r
34#include "AliTRDrecoTask.h"\r
35\r
36ClassImp(AliTRDrecoTask)\r
37\r
38TList* AliTRDrecoTask::fgTrendPoint(NULL);\r
39TTreeSRedirector* AliTRDrecoTask::fgDebugStream(NULL);\r
40//_______________________________________________________\r
41AliTRDrecoTask::AliTRDrecoTask()\r
42 : AliAnalysisTaskSE()\r
43 ,fNRefFigures(0)\r
44 ,fContainer(NULL)\r
45 ,fEvent(NULL)\r
46 ,fTracks(NULL)\r
47 ,fkTrack(NULL)\r
48 ,fkMC(NULL)\r
49 ,fkESD(NULL)\r
50 ,fPlotFuncList(NULL)\r
51 ,fRunTerminate(kFALSE)\r
52{\r
53// Default constructor\r
54 snprintf(fNameId, 10, "no name");\r
55}\r
56\r
57//_______________________________________________________\r
58AliTRDrecoTask::AliTRDrecoTask(const char *name, const char *title)\r
59 : AliAnalysisTaskSE(name)\r
60 ,fNRefFigures(0)\r
61 ,fContainer(NULL)\r
62 ,fEvent(NULL)\r
63 ,fTracks(NULL)\r
64 ,fkTrack(NULL)\r
65 ,fkMC(NULL)\r
66 ,fkESD(NULL)\r
67 ,fPlotFuncList(NULL)\r
68 ,fRunTerminate(kFALSE)\r
69{\r
70// Constructor for all derived performance tasks\r
71\r
72 SetTitle(title);\r
73 snprintf(fNameId, 10, "no name");\r
74 DefineInput (1, TObjArray::Class()); // track list\r
75 DefineInput (2, AliTRDeventInfo::Class()); // event info object\r
76 DefineOutput(1, TObjArray::Class()); // histogram list\r
77}\r
78\r
79//_______________________________________________________\r
80AliTRDrecoTask::~AliTRDrecoTask() \r
81{\r
82\r
83 // Generic task destructor\r
84\r
85 AliDebug(2, Form(" Ending task %s[%s]", GetName(), GetTitle()));\r
86 if(fgDebugStream){ \r
87 delete fgDebugStream;\r
88 fgDebugStream = NULL;\r
89 }\r
90\r
91 if(fPlotFuncList){\r
92 fPlotFuncList->Delete();\r
93 delete fPlotFuncList;\r
94 fPlotFuncList = NULL;\r
95 }\r
96 \r
97 if(fContainer){\r
98 if(fContainer->IsOwner()) fContainer->Delete();\r
99 delete fContainer;\r
100 fContainer = NULL;\r
101 }\r
102\r
103 if(fgTrendPoint){\r
104 TFile::Open("TRD.PerformanceTrend.root", "UPDATE");\r
105 fgTrendPoint->Write();\r
106 delete fgTrendPoint;\r
107 fgTrendPoint=NULL;\r
108 gFile->Close();\r
109 }\r
110}\r
111\r
112//_______________________________________________________\r
113Int_t AliTRDrecoTask::GetNRefFigures() const \r
114{ \r
115 if(!fNRefFigures) AliWarning("No reference plots available.");\r
116 return fNRefFigures; \r
117} \r
118\r
119//_______________________________________________________\r
120void AliTRDrecoTask::UserCreateOutputObjects()\r
121{\r
122 if(!HasFunctorList()) InitFunctorList();\r
123 fContainer = Histos();\r
124 PostData(1, fContainer);\r
125}\r
126\r
127//_______________________________________________________\r
128void AliTRDrecoTask::UserExec(Option_t *)\r
129{\r
130// Loop over Plot functors published by particular tasks\r
131\r
132 fTracks = dynamic_cast<TObjArray *>(GetInputData(1));\r
133 fEvent = dynamic_cast<AliTRDeventInfo *>(GetInputData(2));\r
134\r
135 if(!fPlotFuncList){\r
136 AliWarning("No functor list defined for the reference plots");\r
137 return;\r
138 }\r
139 if(!fTracks) return;\r
140 if(!fTracks->GetEntriesFast()) return;\r
141 else AliDebug(2, Form("Tracks[%d] for %s", fTracks->GetEntriesFast(), GetName()));\r
142\r
143 AliTRDtrackInfo *trackInfo = NULL;\r
144 TIter plotIter(fPlotFuncList);\r
145 TObjArrayIter trackIter(fTracks);\r
146 while((trackInfo = dynamic_cast<AliTRDtrackInfo*>(trackIter()))){\r
147 fkTrack = trackInfo->GetTrack();\r
148 fkMC = trackInfo->GetMCinfo();\r
149 fkESD = trackInfo->GetESDinfo();\r
150\r
151 TMethodCall *plot = NULL;\r
152 plotIter.Reset();\r
153 while((plot=dynamic_cast<TMethodCall*>(plotIter()))){\r
154 plot->Execute(this);\r
155 }\r
156 }\r
157}\r
158\r
159//_______________________________________________________\r
160Bool_t AliTRDrecoTask::GetRefFigure(Int_t /*ifig*/)\r
161{\r
162 AliWarning("Retrieving reference figures not implemented.");\r
163 return kFALSE;\r
164}\r
165\r
166//_______________________________________________________\r
167Bool_t AliTRDrecoTask::PutTrendValue(const Char_t *name, Double_t val)\r
168{\r
169// Generic publisher for trend values\r
170\r
171 if(!fgTrendPoint){\r
172 fgTrendPoint = new TList();\r
173 fgTrendPoint->SetOwner();\r
174 }\r
175 fgTrendPoint->AddLast(new TNamed(Form("%s_%s", GetName(), name), Form("%f", val)));\r
176 return kTRUE;\r
177}\r
178\r
179//_______________________________________________________\r
180void AliTRDrecoTask::InitFunctorList()\r
181{\r
182// Initialize list of functors\r
183\r
184 TClass *c = this->IsA();\r
185\r
186 TMethod *m = NULL;\r
187 TIter methIter(c->GetListOfMethods());\r
188 while((m=dynamic_cast<TMethod*>(methIter()))){\r
189 TString name(m->GetName());\r
190 if(!name.BeginsWith("Plot")) continue;\r
191 if(!fPlotFuncList) fPlotFuncList = new TList();\r
192 fPlotFuncList->AddLast(new TMethodCall(c, (const char*)name, ""));\r
193 }\r
194}\r
195\r
196//_______________________________________________________\r
197Bool_t AliTRDrecoTask::Load(const Char_t *file, const Char_t *dir)\r
198{\r
199// Generic container loader\r
200\r
201 if(!TFile::Open(file)){\r
202 AliWarning(Form("Couldn't open file %s.", file));\r
203 return kFALSE;\r
204 }\r
205 if(!gFile->cd(dir)){\r
206 AliWarning(Form("Couldn't cd to %s in %s.", dir, file));\r
207 return kFALSE;\r
208 }\r
209 TObjArray *o = NULL;\r
210 if(!(o = (TObjArray*)gDirectory->Get(GetName()))){\r
211 AliWarning("Missing histogram container.");\r
212 return kFALSE;\r
213 }\r
214 fContainer = (TObjArray*)o->Clone(GetName());\r
215 gFile->Close();\r
216 return kTRUE;\r
217}\r
218\r
219//________________________________________________________\r
220Bool_t AliTRDrecoTask::Save(TObjArray * const results){\r
221 //\r
222 // Store the output graphs in a ROOT file\r
223 // Input TObject array will not be written as Key to the file,\r
224 // only content itself\r
225 //\r
226\r
227 TDirectory *cwd = gDirectory;\r
228 if(!TFile::Open(Form("TRD.Result%s.root", GetName()), "RECREATE")) return kFALSE;\r
229\r
230 TIterator *iter = results->MakeIterator();\r
231 TObject *inObject = NULL, *outObject = NULL;\r
232 while((inObject = iter->Next())){\r
233 outObject = inObject->Clone();\r
234 outObject->Write(NULL, TObject::kSingleKey);\r
235 }\r
236 delete iter;\r
237 gFile->Close(); delete gFile;\r
238 cwd->cd(); \r
239 return kTRUE;\r
240}\r
241\r
242//_______________________________________________________\r
243Bool_t AliTRDrecoTask::PostProcess()\r
244{\r
245// To be implemented by particular tasks\r
246\r
247 AliWarning("Post processing of reference histograms not implemented.");\r
248 return kTRUE;\r
249}\r
250\r
251//_______________________________________________________\r
252void AliTRDrecoTask::MakeSummary()\r
253{\r
254// To be implemented by particular tasks\r
255 AliWarning("Summary not available");\r
256}\r
257\r
258//_______________________________________________________\r
259void AliTRDrecoTask::SetDebugLevel(Int_t level)\r
260{\r
261// Generic debug handler\r
262\r
263 AliAnalysisTaskSE::SetDebugLevel(level);\r
264 if(DebugLevel()>=1){\r
265 AliInfo(Form("Debug Level for Task %s set to %d", GetName(), level));\r
266 TDirectory *savedir = gDirectory;\r
267 fgDebugStream = new TTreeSRedirector("TRD.DebugPerformance.root");\r
268 savedir->cd();\r
269 }\r
270}\r
271\r
272//____________________________________________________________________\r
273void AliTRDrecoTask::Terminate(Option_t *)\r
274{\r
275 //\r
276 // Terminate\r
277 //\r
278\r
279 if(fgDebugStream){ \r
280 delete fgDebugStream;\r
281 fgDebugStream = NULL;\r
282 }\r
283 fContainer = dynamic_cast<TObjArray *>(GetOutputData(1));\r
284 if(fContainer && fRunTerminate){\r
285 PostProcess();\r
286 MakeSummary();\r
287 }\r
288}\r
289\r
290//________________________________________________________\r
291void AliTRDrecoTask::Adjust(TF1 *f, TH1 * const h)\r
292{\r
293// Helper function to avoid duplication of code\r
294// Make first guesses on the fit parameters\r
295\r
296 // find the intial parameters of the fit !! (thanks George)\r
297 Int_t nbinsy = Int_t(.5*h->GetNbinsX());\r
298 Double_t sum = 0.;\r
299 for(Int_t jbin=nbinsy-4; jbin<=nbinsy+4; jbin++) sum+=h->GetBinContent(jbin); sum/=9.;\r
300 f->SetParLimits(0, 0., 3.*sum);\r
301 f->SetParameter(0, .9*sum);\r
302\r
303 f->SetParLimits(1, -.2, .2);\r
304 f->SetParameter(1, -0.1);\r
305\r
306 f->SetParLimits(2, 0., 4.e-1);\r
307 f->SetParameter(2, 2.e-2);\r
308 if(f->GetNpar() <= 4) return;\r
309\r
310 f->SetParLimits(3, 0., sum);\r
311 f->SetParameter(3, .1*sum);\r
312\r
313 f->SetParLimits(4, -.3, .3);\r
314 f->SetParameter(4, 0.);\r
315\r
316 f->SetParLimits(5, 0., 1.e2);\r
317 f->SetParameter(5, 2.e-1);\r
318}\r