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