replaced THXF to THX in many function prototypes
[u/mrichter/AliRoot.git] / PWG0 / dNdEta / AlidNdEtaCorrection.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18 #include "AlidNdEtaCorrection.h"
19
20 #include <AliLog.h>
21 #include <TCanvas.h>
22 #include <TH3F.h>
23 #include <TH2F.h>
24 #include <TH1D.h>
25 #include <TDirectory.h>
26 #include <AliCorrection.h>
27 #include <AliCorrectionMatrix2D.h>
28 #include <AliCorrectionMatrix3D.h>
29
30 //____________________________________________________________________
31 ClassImp(AlidNdEtaCorrection)
32
33 //____________________________________________________________________
34 AlidNdEtaCorrection::AlidNdEtaCorrection()
35   : TNamed(),
36   fTrack2ParticleCorrection(0),
37   fVertexRecoCorrection(0),
38   fTriggerBiasCorrectionMBToINEL(0),
39   fTriggerBiasCorrectionMBToNSD(0),
40   fTriggerBiasCorrectionMBToND(0)
41 {
42   // default constructor
43 }
44
45 //____________________________________________________________________
46 AlidNdEtaCorrection::AlidNdEtaCorrection(const Char_t* name, const Char_t* title, AliPWG0Helper::AnalysisMode analysis)
47   : TNamed(name, title),
48   fTrack2ParticleCorrection(0),
49   fVertexRecoCorrection(0),
50   fTriggerBiasCorrectionMBToINEL(0),
51   fTriggerBiasCorrectionMBToNSD(0),
52   fTriggerBiasCorrectionMBToND(0)
53 {
54   //
55   // constructor
56   //
57
58   fTrack2ParticleCorrection = new AliCorrection("Track2Particle", "Track2Particle", analysis);
59   fVertexRecoCorrection     = new AliCorrection("VertexReconstruction", "VertexReconstruction", analysis);
60
61   fTriggerBiasCorrectionMBToINEL = new AliCorrection("TriggerBias_MBToINEL", "TriggerBias_MBToINEL", analysis);
62   fTriggerBiasCorrectionMBToNSD  = new AliCorrection("TriggerBias_MBToNSD", "TriggerBias_MBToNSD", analysis);
63   fTriggerBiasCorrectionMBToND   = new AliCorrection("TriggerBias_MBToND", "TriggerBias_MBToND", analysis);
64 }
65
66 //____________________________________________________________________
67 AlidNdEtaCorrection::~AlidNdEtaCorrection()
68 {
69   // destructor
70
71   if (fTrack2ParticleCorrection) {
72     delete fTrack2ParticleCorrection;
73     fTrack2ParticleCorrection = 0;
74   }
75
76   if (fVertexRecoCorrection) {
77     delete fVertexRecoCorrection;
78     fVertexRecoCorrection = 0;
79   }
80
81   if (fTriggerBiasCorrectionMBToINEL) {
82     delete fTriggerBiasCorrectionMBToINEL;
83     fTriggerBiasCorrectionMBToINEL = 0;
84   }
85
86   if (fTriggerBiasCorrectionMBToNSD) {
87     delete fTriggerBiasCorrectionMBToNSD;
88     fTriggerBiasCorrectionMBToNSD = 0;
89   }
90
91   if (fTriggerBiasCorrectionMBToND) {
92     delete fTriggerBiasCorrectionMBToND;
93     fTriggerBiasCorrectionMBToND = 0;
94   }
95 }
96
97 //____________________________________________________________________
98 void
99 AlidNdEtaCorrection::Finish() {
100   //
101   // finish method
102   //
103   // divide the histograms in the AliCorrectionMatrix2D objects to get the corrections
104
105   fTrack2ParticleCorrection->Divide();
106   fVertexRecoCorrection->Divide();
107   fTriggerBiasCorrectionMBToINEL->Divide();
108   fTriggerBiasCorrectionMBToNSD->Divide();
109   fTriggerBiasCorrectionMBToND->Divide();
110 }
111
112 //____________________________________________________________________
113 Long64_t AlidNdEtaCorrection::Merge(TCollection* list)
114 {
115   // Merge a list of dNdEtaCorrection objects with this (needed for
116   // PROOF).
117   // Returns the number of merged objects (including this).
118
119   if (!list)
120     return 0;
121
122   if (list->IsEmpty())
123     return 1;
124
125   TIterator* iter = list->MakeIterator();
126   TObject* obj;
127
128   // collections of measured and generated histograms
129   TList* collectionNtrackToNparticle    = new TList;
130   TList* collectionVertexReco           = new TList;
131   TList* collectionTriggerBiasMBToINEL  = new TList;
132   TList* collectionTriggerBiasMBToNSD   = new TList;
133   TList* collectionTriggerBiasMBToND    = new TList;
134
135   Int_t count = 0;
136   while ((obj = iter->Next())) {
137
138     AlidNdEtaCorrection* entry = dynamic_cast<AlidNdEtaCorrection*> (obj);
139     if (entry == 0)
140       continue;
141
142     collectionNtrackToNparticle  ->Add(entry->fTrack2ParticleCorrection);
143     collectionVertexReco         ->Add(entry->fVertexRecoCorrection);
144     collectionTriggerBiasMBToINEL->Add(entry->fTriggerBiasCorrectionMBToINEL);
145     collectionTriggerBiasMBToNSD ->Add(entry->fTriggerBiasCorrectionMBToNSD);
146     collectionTriggerBiasMBToND  ->Add(entry->fTriggerBiasCorrectionMBToND);
147
148     count++;
149   }
150   fTrack2ParticleCorrection      ->Merge(collectionNtrackToNparticle);
151   fVertexRecoCorrection          ->Merge(collectionVertexReco);
152   fTriggerBiasCorrectionMBToINEL ->Merge(collectionTriggerBiasMBToINEL);
153   fTriggerBiasCorrectionMBToNSD  ->Merge(collectionTriggerBiasMBToNSD);
154   fTriggerBiasCorrectionMBToND   ->Merge(collectionTriggerBiasMBToND);
155
156   delete collectionNtrackToNparticle;
157   delete collectionVertexReco;
158   delete collectionTriggerBiasMBToINEL;
159   delete collectionTriggerBiasMBToNSD;
160   delete collectionTriggerBiasMBToND;
161
162   return count+1;
163 }
164
165 //____________________________________________________________________
166 void AlidNdEtaCorrection::Add(AlidNdEtaCorrection* aCorrectionsToAdd, Float_t c) {
167   //
168   // adds the measured and generated of aCorrectionsToAdd to measured and generated
169   // of all corrections in this
170
171   fTrack2ParticleCorrection      ->Add(aCorrectionsToAdd->GetTrack2ParticleCorrection() ,c);
172   fVertexRecoCorrection          ->Add(aCorrectionsToAdd->GetVertexRecoCorrection()     ,c);
173   fTriggerBiasCorrectionMBToINEL ->Add(aCorrectionsToAdd->GetTriggerBiasCorrectionINEL(),c);
174   fTriggerBiasCorrectionMBToNSD  ->Add(aCorrectionsToAdd->GetTriggerBiasCorrectionNSD() ,c);
175   fTriggerBiasCorrectionMBToND   ->Add(aCorrectionsToAdd->GetTriggerBiasCorrectionND()  ,c);
176     
177 }
178
179 //____________________________________________________________________
180 void AlidNdEtaCorrection::Reset(void) {
181   //
182   // reset all corrections
183   // 
184
185   fTrack2ParticleCorrection      ->Reset();
186   fVertexRecoCorrection          ->Reset();
187   fTriggerBiasCorrectionMBToINEL ->Reset();
188   fTriggerBiasCorrectionMBToNSD  ->Reset();
189   fTriggerBiasCorrectionMBToND   ->Reset();
190     
191 }
192
193
194
195 //____________________________________________________________________
196 Bool_t AlidNdEtaCorrection::LoadHistograms(const Char_t* dir)
197 {
198   //
199   // loads the histograms
200   // if dir is empty a directory with the name of this object is taken (like in SaveHistogram)
201   //
202
203   if (!dir)
204     dir = GetName();
205
206   if (!gDirectory->cd(dir))
207     return kFALSE;
208
209   fTrack2ParticleCorrection      ->LoadHistograms();
210   fVertexRecoCorrection          ->LoadHistograms();
211   fTriggerBiasCorrectionMBToINEL ->LoadHistograms();
212   fTriggerBiasCorrectionMBToNSD  ->LoadHistograms();
213   fTriggerBiasCorrectionMBToND   ->LoadHistograms();
214
215   gDirectory->cd("..");
216
217   return kTRUE;
218 }
219
220 //____________________________________________________________________
221 void AlidNdEtaCorrection::SaveHistograms()
222 {
223   //
224   // save the histograms
225   //
226
227   gDirectory->mkdir(fName.Data());
228   gDirectory->cd(fName.Data());
229
230   fTrack2ParticleCorrection     ->SaveHistograms();
231   fVertexRecoCorrection         ->SaveHistograms();
232   fTriggerBiasCorrectionMBToINEL->SaveHistograms();
233   fTriggerBiasCorrectionMBToNSD ->SaveHistograms();
234   fTriggerBiasCorrectionMBToND  ->SaveHistograms();
235
236   gDirectory->cd("..");
237 }
238
239 //____________________________________________________________________
240 void AlidNdEtaCorrection::DrawHistograms()
241 {
242   //
243   // call the draw histogram method of the corrections
244   //
245
246   fTrack2ParticleCorrection     ->DrawHistograms();
247   fVertexRecoCorrection         ->DrawHistograms();
248   fTriggerBiasCorrectionMBToINEL->DrawHistograms();
249   fTriggerBiasCorrectionMBToNSD ->DrawHistograms();
250   fTriggerBiasCorrectionMBToND  ->DrawHistograms();
251 }
252
253 //____________________________________________________________________
254 void AlidNdEtaCorrection::DrawOverview(const char* canvasName)
255 {
256   //
257   // call the DrawOverview histogram method of the corrections
258   //
259
260   fTrack2ParticleCorrection     ->DrawOverview(canvasName);
261   fVertexRecoCorrection         ->DrawOverview(canvasName);
262   fTriggerBiasCorrectionMBToINEL->DrawOverview(canvasName);
263   fTriggerBiasCorrectionMBToNSD ->DrawOverview(canvasName);
264   fTriggerBiasCorrectionMBToND  ->DrawOverview(canvasName);
265 }
266
267 //____________________________________________________________________
268 void AlidNdEtaCorrection::FillMCParticle(Float_t vtx, Float_t eta, Float_t pt, Bool_t trigger, Bool_t vertex, Int_t processType)
269 {
270   // fills a particle in the corrections
271   // it is filled in generated or measured depending of the flags
272
273   fTriggerBiasCorrectionMBToINEL->GetTrackCorrection()->FillGene(vtx, eta, pt);
274
275   if (processType != AliPWG0Helper::kSD )
276     fTriggerBiasCorrectionMBToNSD->GetTrackCorrection()->FillGene(vtx, eta, pt);
277
278   if (processType == AliPWG0Helper::kND )
279     fTriggerBiasCorrectionMBToND->GetTrackCorrection()->FillGene(vtx, eta, pt);
280
281   if (!trigger)
282     return;
283
284   fTriggerBiasCorrectionMBToINEL->GetTrackCorrection()->FillMeas(vtx, eta, pt);
285   fTriggerBiasCorrectionMBToNSD->GetTrackCorrection()->FillMeas(vtx, eta, pt);
286   fTriggerBiasCorrectionMBToND->GetTrackCorrection()->FillMeas(vtx, eta, pt);
287   fVertexRecoCorrection->GetTrackCorrection()->FillGene(vtx, eta, pt);
288
289   if (!vertex)
290     return;
291
292   fVertexRecoCorrection->GetTrackCorrection()->FillMeas(vtx, eta, pt);
293   fTrack2ParticleCorrection->GetTrackCorrection()->FillGene(vtx, eta, pt);
294 }
295
296 //____________________________________________________________________
297 void AlidNdEtaCorrection::FillTrackedParticle(Float_t vtx, Float_t eta, Float_t pt)
298 {
299   // fills a tracked particle in the corrections
300
301   fTrack2ParticleCorrection->GetTrackCorrection()->FillMeas(vtx, eta, pt);
302 }
303
304 //____________________________________________________________________
305 void AlidNdEtaCorrection::FillEvent(Float_t vtx, Float_t n, Bool_t trigger, Bool_t vertex, Int_t processType)
306 {
307   // fills an event int he correction
308   // it is filled in generated or measured depending of the flags
309
310   fTriggerBiasCorrectionMBToINEL->GetEventCorrection()->FillGene(vtx, n);
311
312   if ( processType != AliPWG0Helper::kSD )
313     fTriggerBiasCorrectionMBToNSD->GetEventCorrection()->FillGene(vtx, n);
314
315   if (processType == AliPWG0Helper::kND )
316     fTriggerBiasCorrectionMBToND->GetEventCorrection()->FillGene(vtx, n);
317
318   if (!trigger)
319     return;
320
321   fTriggerBiasCorrectionMBToINEL->GetEventCorrection()->FillMeas(vtx, n);
322   fTriggerBiasCorrectionMBToNSD->GetEventCorrection()->FillMeas(vtx, n);
323   fTriggerBiasCorrectionMBToND->GetEventCorrection()->FillMeas(vtx, n);
324   fVertexRecoCorrection->GetEventCorrection()->FillGene(vtx, n);
325
326   if (!vertex)
327     return;
328
329   fVertexRecoCorrection->GetEventCorrection()->FillMeas(vtx, n);
330 }
331
332 //____________________________________________________________________
333 Float_t AlidNdEtaCorrection::GetMeasuredFraction(CorrectionType correctionType, Float_t ptCutOff, Float_t eta, Int_t vertexBegin, Int_t vertexEnd, Bool_t debug)
334 {
335   // calculates the fraction of particles measured (some are missed due to the pt cut off)
336   //
337   // uses the generated particle histogram from the correction passed, e.g. pass GetTrack2ParticleCorrection()
338
339   if (!GetCorrection(correctionType))
340     return -1;
341
342   const TH3* generated = GetCorrection(correctionType)->GetTrackCorrection()->GetGeneratedHistogram();
343
344   // find eta borders, if eta is negative assume -0.8 ... 0.8
345   Int_t etaBegin = 0;
346   Int_t etaEnd = 0;
347   if (eta < -99)
348   {
349     etaBegin = generated->GetYaxis()->FindBin(-0.8);
350     etaEnd = generated->GetYaxis()->FindBin(0.8);
351   }
352   else
353   {
354     etaBegin = generated->GetYaxis()->FindBin(eta);
355     etaEnd = etaBegin;
356   }
357
358   if (vertexBegin == -1)
359     vertexBegin = generated->GetXaxis()->FindBin(-9.99);
360
361   if (vertexEnd == -1)
362     vertexEnd = generated->GetXaxis()->FindBin(9.99);
363
364   TH1D* ptProj = dynamic_cast<TH1D*> (generated->ProjectionZ(Form("%s_pt", generated->GetName()), vertexBegin, vertexEnd, etaBegin, etaEnd));
365   //printf("GetMeasuredFraction: bin range %d %d %d %d\n", vertexBegin, vertexEnd, etaBegin, etaEnd);
366   ptProj->GetXaxis()->SetTitle(generated->GetZaxis()->GetTitle());
367
368   Int_t ptBin = ptProj->FindBin(ptCutOff);
369   //printf("GetMeasuredFraction: bin range %d %d\n", ptBin, ptProj->GetNbinsX());
370   Float_t abovePtCut = ptProj->Integral(ptBin, ptProj->GetNbinsX()+1);
371   Float_t all = ptProj->Integral(1, ptProj->GetNbinsX()+1);
372
373   if (all == 0)
374     return -1;
375
376   Float_t fraction = abovePtCut / all;
377
378   //printf("GetMeasuredFraction: all %f above %f fraction %f\n", all, abovePtCut, fraction);
379
380   if (debug)
381   {
382     new TCanvas;
383     ptProj->Draw();
384   }
385   else
386     delete ptProj;
387
388   if (debug)
389     printf("AlidNdEtaCorrection::GetMeasuredFraction: pt cut off = %f, eta = %f, => fraction = %f\n", ptCutOff, eta, fraction);
390
391   return fraction;
392 }
393
394 //____________________________________________________________________
395 TH1* AlidNdEtaCorrection::GetMeasuredEventFraction(CorrectionType correctionType, Int_t multCut)
396 {
397   // calculates the fraction of events above multCut (but including it)
398   //
399   // uses the generated event histogram from the correction passed, e.g. pass GetTrack2ParticleCorrection()
400
401   if (!GetCorrection(correctionType))
402     return 0;
403
404   const TH2* generated = GetCorrection(correctionType)->GetEventCorrection()->GetGeneratedHistogram();
405
406   TH1* allEvents = generated->ProjectionX(Form("%s_all", generated->GetName()), 1, generated->GetNbinsY());
407   TH1* aboveEvents = generated->ProjectionX(Form("%s_above", generated->GetName()), generated->GetYaxis()->FindBin(multCut), generated->GetNbinsY());
408   
409   aboveEvents->Divide(aboveEvents, allEvents, 1, 1, "B");
410
411   return aboveEvents;  
412 }
413
414 //____________________________________________________________________
415 void AlidNdEtaCorrection::ReduceInformation()
416 {
417   // this function deletes the measured and generated histograms from the corrections to reduce the amount of data
418   // in memory
419
420   // these are needed for GetMeasuredFraction(): fTrack2ParticleCorrection->ReduceInformation();
421   fVertexRecoCorrection          ->ReduceInformation();
422   fTriggerBiasCorrectionMBToINEL ->ReduceInformation();
423   fTriggerBiasCorrectionMBToNSD  ->ReduceInformation();
424   fTriggerBiasCorrectionMBToND   ->ReduceInformation();
425 }
426
427 //____________________________________________________________________
428 AliCorrection* AlidNdEtaCorrection::GetCorrection(CorrectionType correctionType)
429 {
430   // returns the given correction
431
432   switch (correctionType)
433   {
434     case kNone : return 0;
435     case kTrack2Particle : return fTrack2ParticleCorrection;
436     case kVertexReco :     return fVertexRecoCorrection;
437     case kINEL :           return fTriggerBiasCorrectionMBToINEL;
438     case kNSD :            return fTriggerBiasCorrectionMBToNSD;
439     case kND :             return fTriggerBiasCorrectionMBToND;
440   }
441
442   return 0;
443 }