]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG1/TRD/AliTRDrecoTask.cxx
2a0930c6c71c44e2b33d8f3c5f2f02571b42a220
[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   ,fDebugLevel(0)
49   ,fPlotFuncList(NULL)
50 {
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   ,fDebugLevel(0)
63   ,fPlotFuncList(NULL)
64 {
65   SetTitle(title);
66   DefineInput (1, TObjArray::Class());
67   DefineOutput(1, TObjArray::Class());
68 }
69
70 //_______________________________________________________
71 AliTRDrecoTask::~AliTRDrecoTask() 
72 {
73
74   // Generic task destructor
75
76   AliDebug(1, Form(" Ending %s (%s)\n", GetName(), GetTitle()));
77   if(fgDebugStream){ 
78     delete fgDebugStream;
79     fgDebugStream = NULL;
80   }
81
82   if(fPlotFuncList){
83     fPlotFuncList->Delete();
84     delete fPlotFuncList;
85     fPlotFuncList = NULL;
86   }
87   
88   if(fContainer){
89     if(fContainer->IsOwner()) fContainer->Delete();
90     delete fContainer;
91     fContainer = NULL;
92   }
93
94   if(fgTrendPoint){
95     TFile::Open("TRD.PerformanceTrend.root", "UPDATE");
96     fgTrendPoint->Write();
97     delete fgTrendPoint;
98     fgTrendPoint=NULL;
99     gFile->Close();
100   }
101 }
102
103 //_______________________________________________________
104 void AliTRDrecoTask::ConnectInputData(Option_t *)
105 {
106   //
107   // Connect input data
108   //
109     AliAnalysisTaskSE::ConnectInputData();
110     fTracks = dynamic_cast<TObjArray *>(GetInputData(1));
111 }
112
113 //_______________________________________________________
114 void AliTRDrecoTask::UserExec(Option_t *)
115 {
116 // Loop over Plot functors published by particular tasks
117
118   if(!fPlotFuncList){
119     AliWarning("No functor list defined for the reference plots");
120     return;
121   }
122   if(!fTracks) return;
123   if(!fTracks->GetEntriesFast()) return;
124   
125   AliTRDtrackInfo *trackInfo = NULL;
126   TIter plotIter(fPlotFuncList);
127   TObjArrayIter trackIter(fTracks);
128   while((trackInfo = dynamic_cast<AliTRDtrackInfo*>(trackIter()))){
129     fkTrack = trackInfo->GetTrack();
130     fkMC    = trackInfo->GetMCinfo();
131     fkESD   = trackInfo->GetESDinfo();
132
133     TMethodCall *plot = NULL;
134     plotIter.Reset();
135     while((plot=dynamic_cast<TMethodCall*>(plotIter()))){
136       plot->Execute(this);
137     }
138   }
139   PostData(1, fContainer);
140 }
141
142 //_______________________________________________________
143 Bool_t AliTRDrecoTask::GetRefFigure(Int_t /*ifig*/)
144 {
145   AliWarning("Retrieving reference figures not implemented.");
146   return kFALSE;
147 }
148
149 //_______________________________________________________
150 Bool_t AliTRDrecoTask::PutTrendValue(const Char_t *name, Double_t val)
151 {
152 // Generic publisher for trend values
153
154   if(!fgTrendPoint){
155     fgTrendPoint = new TList();
156     fgTrendPoint->SetOwner();
157   }
158   fgTrendPoint->AddLast(new TNamed(Form("%s_%s", GetName(), name), Form("%f", val)));
159   return kTRUE;
160 }
161
162 //_______________________________________________________
163 void AliTRDrecoTask::InitFunctorList()
164 {
165 // Initialize list of functors
166
167   TClass *c = this->IsA();
168
169   TMethod *m = NULL;
170   TIter methIter(c->GetListOfMethods());
171   while((m=dynamic_cast<TMethod*>(methIter()))){
172     TString name(m->GetName());
173     if(!name.BeginsWith("Plot")) continue;
174     if(!fPlotFuncList) fPlotFuncList = new TList();
175     fPlotFuncList->AddLast(new TMethodCall(c, (const char*)name, ""));
176   }
177 }
178
179 //_______________________________________________________
180 Bool_t AliTRDrecoTask::Load(const Char_t *filename)
181 {
182 // Generic container loader
183
184   if(!TFile::Open(filename)){
185     AliWarning(Form("Couldn't open file %s.", filename));
186     return kFALSE;
187   }
188   TObjArray *o = NULL;
189   if(!(o = (TObjArray*)gFile->Get(GetName()))){
190     AliWarning("Missing histogram container.");
191     return kFALSE;
192   }
193   fContainer = (TObjArray*)o->Clone(GetName());
194   gFile->Close();
195   return kTRUE;
196 }
197
198 //________________________________________________________
199 Bool_t AliTRDrecoTask::Save(TObjArray * const results){
200   //
201   // Store the output graphs in a ROOT file
202   // Input TObject array will not be written as Key to the file,
203   // only content itself
204   //
205
206   TDirectory *cwd = gDirectory;
207   if(!TFile::Open(Form("TRD.Result%s.root", GetName()), "RECREATE")) return kFALSE;
208
209   TIterator *iter = results->MakeIterator();
210   TObject *inObject = NULL, *outObject = NULL;
211   while((inObject = iter->Next())){
212     outObject = inObject->Clone();
213     outObject->Write(NULL, TObject::kSingleKey);
214   }
215   delete iter;
216   gFile->Close(); delete gFile;
217   cwd->cd(); 
218   return kTRUE;
219 }
220
221 //_______________________________________________________
222 Bool_t AliTRDrecoTask::PostProcess()
223 {
224 // To be implemented by particular tasks
225
226   AliWarning("Post processing of reference histograms not implemented.");
227   return kFALSE;
228 }
229
230 //_______________________________________________________
231 void AliTRDrecoTask::SetDebugLevel(Int_t level)
232 {
233 // Generic debug handler
234
235   fDebug = level;
236   if(fDebug>=1){
237     TDirectory *savedir = gDirectory;
238     fgDebugStream = new TTreeSRedirector("TRD.DebugPerformance.root");
239     savedir->cd();
240   }
241 }
242
243 //____________________________________________________________________
244 void AliTRDrecoTask::Terminate(Option_t *)
245 {
246   //
247   // Terminate
248   //
249
250   if(fgDebugStream){ 
251     delete fgDebugStream;
252     fgDebugStream = NULL;
253     fDebug = 0;
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 }