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