]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/qaRec/AliTRDrecoTask.cxx
restore functionality of "mc" and "friends" flags
[u/mrichter/AliRoot.git] / TRD / qaRec / AliTRDrecoTask.cxx
1 #include "TClass.h"
2 #include "TMethod.h"
3 #include "TMethodCall.h"
4 #include "TMethodArg.h"
5 #include "TFile.h"
6 #include "TList.h"
7 #include "TH1.h"
8 #include "TF1.h"
9 #include "TObjArray.h"
10 #include "TDirectory.h"
11 #include "TTreeStream.h"
12
13 #include "AliLog.h"
14 #include "AliAnalysisTask.h"
15
16 #include "AliTRDrecoTask.h"
17
18 ClassImp(AliTRDrecoTask)
19 FILE* AliTRDrecoTask::fgFile = 0x0;
20 //_______________________________________________________
21 AliTRDrecoTask::AliTRDrecoTask(const char *name, const char *title)
22   : AliAnalysisTask(name, title)
23   ,fNRefFigures(0)
24   ,fDebugLevel(0)
25   ,fPlotFuncList(0x0)
26   ,fContainer(0x0)
27   ,fTracks(0x0)
28   ,fTrack(0x0)
29   ,fMC(0x0)
30   ,fESD(0x0)
31   ,fDebugStream(0x0)
32 {
33   DefineInput(0, TObjArray::Class());
34   DefineOutput(0, TObjArray::Class());
35 }
36
37 //_______________________________________________________
38 AliTRDrecoTask::~AliTRDrecoTask() 
39 {
40   printf(" %s (%s)\n", GetName(), GetTitle());
41   if(fDebugStream){ 
42     delete fDebugStream;
43     fDebugStream = 0x0;
44   }
45
46   if(fPlotFuncList){
47     fPlotFuncList->Delete();
48     delete fPlotFuncList;
49     fPlotFuncList = 0x0;
50   }
51   
52   if(fContainer){
53     if(fContainer->IsOwner()) fContainer->Delete();
54     delete fContainer;
55     fContainer = 0x0;
56   }
57
58   if(fgFile){
59     fflush(fgFile);
60     fclose(fgFile);
61     fgFile = 0x0;
62   }
63 }
64
65 //_______________________________________________________
66 void AliTRDrecoTask::ConnectInputData(Option_t *)
67 {
68   //
69   // Connect input data
70   //
71
72   fTracks = dynamic_cast<TObjArray *>(GetInputData(0));
73 }
74
75 //_______________________________________________________
76 void AliTRDrecoTask::Exec(Option_t *)
77 {
78   if(!fPlotFuncList){
79     AliWarning("No functor list defined for the reference plots");
80     return;
81   }
82   if(!fTracks) return;
83   if(!fTracks->GetEntriesFast()) return;
84   
85   AliTRDtrackInfo *trackInfo = 0x0;
86   TIter plotIter(fPlotFuncList);
87   TObjArrayIter trackIter(fTracks);
88   while((trackInfo = dynamic_cast<AliTRDtrackInfo*>(trackIter()))){
89     fTrack = trackInfo->GetTrack();
90     fMC    = trackInfo->GetMCinfo();
91     fESD   = trackInfo->GetESDinfo();
92
93     TMethodCall *plot = 0x0;
94     plotIter.Reset();
95     while((plot=dynamic_cast<TMethodCall*>(plotIter()))){
96       plot->Execute(this);
97     }
98   }
99   PostData(0, fContainer);
100 }
101
102 //_______________________________________________________
103 Bool_t AliTRDrecoTask::GetRefFigure(Int_t /*ifig*/)
104 {
105   AliWarning("Retrieving reference figures not implemented.");
106   return kFALSE;
107 }
108
109 //_______________________________________________________
110 Bool_t AliTRDrecoTask::PutTrendValue(Char_t *name, Double_t val, Double_t err)
111 {
112   if(!fgFile){
113     fgFile = fopen("TRD.Performance.txt", "at");
114   }
115   fprintf(fgFile, "%s_%s %f %f\n", GetName(), name, val, err);
116   return kTRUE;
117 }
118
119 //_______________________________________________________
120 void AliTRDrecoTask::InitFunctorList()
121 {
122   TClass *c = this->IsA();
123
124   TMethod *m = 0x0;
125   TIter methIter(c->GetListOfMethods());
126   while((m=dynamic_cast<TMethod*>(methIter()))){
127     TString name(m->GetName());
128     if(!name.BeginsWith("Plot")) continue;
129     if(!fPlotFuncList) fPlotFuncList = new TList();
130     fPlotFuncList->AddLast(new TMethodCall(c, (const char*)name, ""));
131   }
132 }
133
134 //_______________________________________________________
135 Bool_t AliTRDrecoTask::Load(const Char_t *filename)
136 {
137   if(!TFile::Open(filename)){
138     AliWarning(Form("Couldn't open file %s.", filename));
139     return kFALSE;
140   }
141   TObjArray *o = 0x0;
142   if(!(o = (TObjArray*)gFile->Get(GetName()))){
143     AliWarning("Missing histogram container.");
144     return kFALSE;
145   }
146   fContainer = (TObjArray*)o->Clone(GetName());
147   gFile->Close();
148   return kTRUE;
149 }
150
151 //________________________________________________________
152 Bool_t AliTRDrecoTask::Save(TObjArray *results){
153   //
154   // Store the output graphs in a ROOT file
155   // Input TObject array will not be written as Key to the file,
156   // only content itself
157   //
158
159   TDirectory *cwd = gDirectory;
160   if(!TFile::Open(Form("TRD.Result%s.root", GetName()), "RECREATE")) return kFALSE;
161
162   TIterator *iter = results->MakeIterator();
163   TObject *inObject = 0x0, *outObject = 0x0;
164   while((inObject = iter->Next())){
165     outObject = inObject->Clone();
166     outObject->Write(0x0, TObject::kSingleKey);
167   }
168   delete iter;
169   gFile->Close(); delete gFile;
170   cwd->cd(); 
171   return kTRUE;
172 }
173
174 //_______________________________________________________
175 Bool_t AliTRDrecoTask::PostProcess()
176 {
177   AliWarning("Post processing of reference histograms not implemented.");
178   return kFALSE;
179 }
180
181 //_______________________________________________________
182 void AliTRDrecoTask::SetDebugLevel(Int_t level)
183 {
184   fDebugLevel = level;
185   if(fDebugLevel>=1){
186     TDirectory *savedir = gDirectory;
187     fDebugStream = new TTreeSRedirector(Form("TRD.DBG%s.root", GetName()));
188     savedir->cd();
189   }
190 }
191
192 //________________________________________________________
193 void AliTRDrecoTask::Adjust(TF1 *f, TH1 *h)
194 {
195 // Helper function to avoid duplication of code
196 // Make first guesses on the fit parameters
197
198   // find the intial parameters of the fit !! (thanks George)
199   Int_t nbinsy = Int_t(.5*h->GetNbinsX());
200   Double_t sum = 0.;
201   for(Int_t jbin=nbinsy-4; jbin<=nbinsy+4; jbin++) sum+=h->GetBinContent(jbin); sum/=9.;
202   f->SetParLimits(0, 0., 3.*sum);
203   f->SetParameter(0, .9*sum);
204
205   f->SetParLimits(1, -.2, .2);
206   f->SetParameter(1, -0.1);
207
208   f->SetParLimits(2, 0., 4.e-1);
209   f->SetParameter(2, 2.e-2);
210   if(f->GetNpar() <= 4) return;
211
212   f->SetParLimits(3, 0., sum);
213   f->SetParameter(3, .1*sum);
214
215   f->SetParLimits(4, -.3, .3);
216   f->SetParameter(4, 0.);
217
218   f->SetParLimits(5, 0., 1.e2);
219   f->SetParameter(5, 2.e-1);
220 }