]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG1/TRD/AliTRDrecoTask.cxx
- Remove the holes in the lists of output histograms to make the merging working...
[u/mrichter/AliRoot.git] / PWG1 / TRD / AliTRDrecoTask.cxx
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"
21 #include "TChain.h"
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
33 #include "info/AliTRDeventInfo.h"
34 #include "AliTRDrecoTask.h"
35
36 ClassImp(AliTRDrecoTask)
37
38 TList* AliTRDrecoTask::fgTrendPoint(NULL);
39 TTreeSRedirector* AliTRDrecoTask::fgDebugStream(NULL);
40 //_______________________________________________________
41 AliTRDrecoTask::AliTRDrecoTask()
42   : AliAnalysisTaskSE()
43   ,fNRefFigures(0)
44   ,fContainer(NULL)
45   ,fEvent(NULL)
46   ,fTracks(NULL)
47   ,fkTrack(NULL)
48   ,fkMC(NULL)
49   ,fkESD(NULL)
50   ,fPlotFuncList(NULL)
51   ,fRunTerminate(kFALSE)
52 {
53 // Default constructor
54   snprintf(fNameId, 10, "no name");
55 }
56
57 //_______________________________________________________
58 AliTRDrecoTask::AliTRDrecoTask(const char *name, const char *title)
59   : AliAnalysisTaskSE(name)
60   ,fNRefFigures(0)
61   ,fContainer(NULL)
62   ,fEvent(NULL)
63   ,fTracks(NULL)
64   ,fkTrack(NULL)
65   ,fkMC(NULL)
66   ,fkESD(NULL)
67   ,fPlotFuncList(NULL)
68   ,fRunTerminate(kFALSE)
69 {
70 // Constructor for all derived performance tasks
71
72   SetTitle(title);
73   snprintf(fNameId, 10, "no name");
74   DefineInput (1, TObjArray::Class()); // track list
75   DefineOutput(1, TObjArray::Class()); // histogram list
76 }
77
78 //_______________________________________________________
79 AliTRDrecoTask::~AliTRDrecoTask() 
80 {
81
82   // Generic task destructor
83
84   AliDebug(2, Form(" Ending task %s[%s]", GetName(), GetTitle()));
85   if(fgDebugStream){ 
86     delete fgDebugStream;
87     fgDebugStream = NULL;
88   }
89
90   if(fPlotFuncList){
91     fPlotFuncList->Delete();
92     delete fPlotFuncList;
93     fPlotFuncList = NULL;
94   }
95   
96   if(fContainer){
97     if(fContainer->IsOwner()) fContainer->Delete();
98     delete fContainer;
99     fContainer = NULL;
100   }
101
102   if(fgTrendPoint){
103     TFile::Open("TRD.PerformanceTrend.root", "UPDATE");
104     fgTrendPoint->Write();
105     delete fgTrendPoint;
106     fgTrendPoint=NULL;
107     gFile->Close();
108   }
109 }
110
111 //_______________________________________________________
112 Int_t AliTRDrecoTask::GetNRefFigures() const  
113
114   if(!fNRefFigures) AliWarning("No reference plots available.");
115   return fNRefFigures; 
116
117
118 //_______________________________________________________
119 void AliTRDrecoTask::UserCreateOutputObjects()
120 {
121   if(!HasFunctorList()) InitFunctorList();
122   fContainer = Histos();
123   PostData(1, fContainer);
124 }
125
126 //_______________________________________________________
127 void AliTRDrecoTask::UserExec(Option_t *)
128 {
129 // Loop over Plot functors published by particular tasks
130
131   fTracks = dynamic_cast<TObjArray *>(GetInputData(1));
132   fEvent = dynamic_cast<AliTRDeventInfo *>(GetInputData(2));
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;
139   else AliDebug(2, Form("Tracks[%d] for %s", fTracks->GetEntriesFast(), GetName()));
140
141   AliTRDtrackInfo *trackInfo = NULL;
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
149     TMethodCall *plot = NULL;
150     plotIter.Reset();
151     while((plot=dynamic_cast<TMethodCall*>(plotIter()))){
152       plot->Execute(this);
153     }
154   }
155 }
156
157 //_______________________________________________________
158 Bool_t AliTRDrecoTask::GetRefFigure(Int_t /*ifig*/)
159 {
160   AliWarning("Retrieving reference figures not implemented.");
161   return kFALSE;
162 }
163
164 //_______________________________________________________
165 Bool_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 //_______________________________________________________
178 void AliTRDrecoTask::InitFunctorList()
179 {
180 // Initialize list of functors
181
182   TClass *c = this->IsA();
183
184   TMethod *m = NULL;
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 //_______________________________________________________
195 Bool_t AliTRDrecoTask::Load(const Char_t *file, const Char_t *dir)
196 {
197 // Generic container loader
198
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));
205     return kFALSE;
206   }
207   TObjArray *o = NULL;
208   if(!(o = (TObjArray*)gDirectory->Get(GetName()))){
209     AliWarning("Missing histogram container.");
210     return kFALSE;
211   }
212   fContainer = (TObjArray*)o->Clone(GetName());
213   gFile->Close();
214   return kTRUE;
215 }
216
217 //________________________________________________________
218 Bool_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();
229   TObject *inObject = NULL, *outObject = NULL;
230   while((inObject = iter->Next())){
231     outObject = inObject->Clone();
232     outObject->Write(NULL, TObject::kSingleKey);
233   }
234   delete iter;
235   gFile->Close(); delete gFile;
236   cwd->cd(); 
237   return kTRUE;
238 }
239
240 //_______________________________________________________
241 Bool_t AliTRDrecoTask::PostProcess()
242 {
243 // To be implemented by particular tasks
244
245   AliWarning("Post processing of reference histograms not implemented.");
246   return kTRUE;
247 }
248
249 //_______________________________________________________
250 void AliTRDrecoTask::MakeSummary()
251 {
252 // To be implemented by particular tasks
253   AliWarning("Summary not available");
254 }
255
256 //_______________________________________________________
257 void AliTRDrecoTask::SetDebugLevel(Int_t level)
258 {
259 // Generic debug handler
260
261   AliAnalysisTaskSE::SetDebugLevel(level);
262   if(DebugLevel()>=1){
263     AliInfo(Form("Debug Level for Task %s set to %d", GetName(), level));
264     TDirectory *savedir = gDirectory;
265     fgDebugStream = new TTreeSRedirector("TRD.DebugPerformance.root");
266     savedir->cd();
267   }
268 }
269
270 //____________________________________________________________________
271 void AliTRDrecoTask::Terminate(Option_t *)
272 {
273   //
274   // Terminate
275   //
276
277   if(fgDebugStream){ 
278     delete fgDebugStream;
279     fgDebugStream = NULL;
280   }
281   fContainer = dynamic_cast<TObjArray *>(GetOutputData(1));
282   if(fContainer && fRunTerminate){
283     PostProcess();
284     MakeSummary();
285   }
286 }
287
288 //________________________________________________________
289 void 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 }