]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG1/TRD/AliTRDrecoTask.cxx
b192bf0c4ecf14b55f2f9e3f6108c73f082c6593
[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(1, Form(" Ending %s (%s)\n", 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::ConnectInputData(Option_t *)
106 {
107   //
108   // Connect input data
109   //
110   AliAnalysisTaskSE::ConnectInputData();
111   fTracks = dynamic_cast<TObjArray *>(GetInputData(1));
112 }
113
114 //_______________________________________________________
115 void AliTRDrecoTask::UserExec(Option_t *)
116 {
117 // Loop over Plot functors published by particular tasks
118
119   if(!fPlotFuncList){
120     AliWarning("No functor list defined for the reference plots");
121     return;
122   }
123   if(!fTracks) return;
124   if(!fTracks->GetEntriesFast()) return;
125   
126   AliTRDtrackInfo *trackInfo = NULL;
127   TIter plotIter(fPlotFuncList);
128   TObjArrayIter trackIter(fTracks);
129   while((trackInfo = dynamic_cast<AliTRDtrackInfo*>(trackIter()))){
130     fkTrack = trackInfo->GetTrack();
131     fkMC    = trackInfo->GetMCinfo();
132     fkESD   = trackInfo->GetESDinfo();
133
134     TMethodCall *plot = NULL;
135     plotIter.Reset();
136     while((plot=dynamic_cast<TMethodCall*>(plotIter()))){
137       plot->Execute(this);
138     }
139   }
140   PostData(1, fContainer);
141 }
142
143 //_______________________________________________________
144 Bool_t AliTRDrecoTask::GetRefFigure(Int_t /*ifig*/)
145 {
146   AliWarning("Retrieving reference figures not implemented.");
147   return kFALSE;
148 }
149
150 //_______________________________________________________
151 Bool_t AliTRDrecoTask::PutTrendValue(const Char_t *name, Double_t val)
152 {
153 // Generic publisher for trend values
154
155   if(!fgTrendPoint){
156     fgTrendPoint = new TList();
157     fgTrendPoint->SetOwner();
158   }
159   fgTrendPoint->AddLast(new TNamed(Form("%s_%s", GetName(), name), Form("%f", val)));
160   return kTRUE;
161 }
162
163 //_______________________________________________________
164 void AliTRDrecoTask::InitFunctorList()
165 {
166 // Initialize list of functors
167
168   TClass *c = this->IsA();
169
170   TMethod *m = NULL;
171   TIter methIter(c->GetListOfMethods());
172   while((m=dynamic_cast<TMethod*>(methIter()))){
173     TString name(m->GetName());
174     if(!name.BeginsWith("Plot")) continue;
175     if(!fPlotFuncList) fPlotFuncList = new TList();
176     fPlotFuncList->AddLast(new TMethodCall(c, (const char*)name, ""));
177   }
178 }
179
180 //_______________________________________________________
181 Bool_t AliTRDrecoTask::Load(const Char_t *filename)
182 {
183 // Generic container loader
184
185   if(!TFile::Open(filename)){
186     AliWarning(Form("Couldn't open file %s.", filename));
187     return kFALSE;
188   }
189   TObjArray *o = NULL;
190   if(!(o = (TObjArray*)gFile->Get(GetName()))){
191     AliWarning("Missing histogram container.");
192     return kFALSE;
193   }
194   fContainer = (TObjArray*)o->Clone(GetName());
195   gFile->Close();
196   return kTRUE;
197 }
198
199 //________________________________________________________
200 Bool_t AliTRDrecoTask::Save(TObjArray * const results){
201   //
202   // Store the output graphs in a ROOT file
203   // Input TObject array will not be written as Key to the file,
204   // only content itself
205   //
206
207   TDirectory *cwd = gDirectory;
208   if(!TFile::Open(Form("TRD.Result%s.root", GetName()), "RECREATE")) return kFALSE;
209
210   TIterator *iter = results->MakeIterator();
211   TObject *inObject = NULL, *outObject = NULL;
212   while((inObject = iter->Next())){
213     outObject = inObject->Clone();
214     outObject->Write(NULL, TObject::kSingleKey);
215   }
216   delete iter;
217   gFile->Close(); delete gFile;
218   cwd->cd(); 
219   return kTRUE;
220 }
221
222 //_______________________________________________________
223 Bool_t AliTRDrecoTask::PostProcess()
224 {
225 // To be implemented by particular tasks
226
227   AliWarning("Post processing of reference histograms not implemented.");
228   return kFALSE;
229 }
230
231 //_______________________________________________________
232 void AliTRDrecoTask::SetDebugLevel(Int_t level)
233 {
234 // Generic debug handler
235
236   AliAnalysisTaskSE::SetDebugLevel(level);
237   if(DebugLevel()>=1){
238     TDirectory *savedir = gDirectory;
239     fgDebugStream = new TTreeSRedirector("TRD.DebugPerformance.root");
240     savedir->cd();
241   }
242 }
243
244 //____________________________________________________________________
245 void AliTRDrecoTask::Terminate(Option_t *)
246 {
247   //
248   // Terminate
249   //
250
251   if(fgDebugStream){ 
252     delete fgDebugStream;
253     fgDebugStream = NULL;
254   }
255   if(HasPostProcess()) PostProcess();
256 }
257
258 //________________________________________________________
259 void AliTRDrecoTask::Adjust(TF1 *f, TH1 * const h)
260 {
261 // Helper function to avoid duplication of code
262 // Make first guesses on the fit parameters
263
264   // find the intial parameters of the fit !! (thanks George)
265   Int_t nbinsy = Int_t(.5*h->GetNbinsX());
266   Double_t sum = 0.;
267   for(Int_t jbin=nbinsy-4; jbin<=nbinsy+4; jbin++) sum+=h->GetBinContent(jbin); sum/=9.;
268   f->SetParLimits(0, 0., 3.*sum);
269   f->SetParameter(0, .9*sum);
270
271   f->SetParLimits(1, -.2, .2);
272   f->SetParameter(1, -0.1);
273
274   f->SetParLimits(2, 0., 4.e-1);
275   f->SetParameter(2, 2.e-2);
276   if(f->GetNpar() <= 4) return;
277
278   f->SetParLimits(3, 0., sum);
279   f->SetParameter(3, .1*sum);
280
281   f->SetParLimits(4, -.3, .3);
282   f->SetParameter(4, 0.);
283
284   f->SetParLimits(5, 0., 1.e2);
285   f->SetParameter(5, 2.e-1);
286 }