2f52464ab64b437e774b826eca0fefcecac25b08
[u/mrichter/AliRoot.git] / PWGUD / base / 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   fTriggerBiasCorrectionMBToOnePart(0)
42 {
43   // default constructor
44 }
45
46 //____________________________________________________________________
47 AlidNdEtaCorrection::AlidNdEtaCorrection(const Char_t* name, const Char_t* title, AliPWG0Helper::AnalysisMode analysis)
48   : TNamed(name, title),
49   fTrack2ParticleCorrection(0),
50   fVertexRecoCorrection(0),
51   fTriggerBiasCorrectionMBToINEL(0),
52   fTriggerBiasCorrectionMBToNSD(0),
53   fTriggerBiasCorrectionMBToND(0),
54   fTriggerBiasCorrectionMBToOnePart(0)
55 {
56   //
57   // constructor
58   //
59
60   fTrack2ParticleCorrection = new AliCorrection("Track2Particle", "Track2Particle", analysis);
61   fVertexRecoCorrection     = new AliCorrection("VertexReconstruction", "VertexReconstruction", analysis);
62
63   fTriggerBiasCorrectionMBToINEL = new AliCorrection("TriggerBias_MBToINEL", "TriggerBias_MBToINEL", analysis);
64   fTriggerBiasCorrectionMBToNSD  = new AliCorrection("TriggerBias_MBToNSD", "TriggerBias_MBToNSD", analysis);
65   fTriggerBiasCorrectionMBToND   = new AliCorrection("TriggerBias_MBToND", "TriggerBias_MBToND", analysis);
66   fTriggerBiasCorrectionMBToOnePart   = new AliCorrection("TriggerBias_MBToOnePart", "TriggerBias_MBToOnePart", analysis);
67 }
68
69 //____________________________________________________________________
70 AlidNdEtaCorrection::~AlidNdEtaCorrection()
71 {
72   // destructor
73
74   if (fTrack2ParticleCorrection) {
75     delete fTrack2ParticleCorrection;
76     fTrack2ParticleCorrection = 0;
77   }
78
79   if (fVertexRecoCorrection) {
80     delete fVertexRecoCorrection;
81     fVertexRecoCorrection = 0;
82   }
83
84   if (fTriggerBiasCorrectionMBToINEL) {
85     delete fTriggerBiasCorrectionMBToINEL;
86     fTriggerBiasCorrectionMBToINEL = 0;
87   }
88
89   if (fTriggerBiasCorrectionMBToNSD) {
90     delete fTriggerBiasCorrectionMBToNSD;
91     fTriggerBiasCorrectionMBToNSD = 0;
92   }
93
94   if (fTriggerBiasCorrectionMBToND) {
95     delete fTriggerBiasCorrectionMBToND;
96     fTriggerBiasCorrectionMBToND = 0;
97   }
98
99   if (fTriggerBiasCorrectionMBToOnePart) {
100     delete fTriggerBiasCorrectionMBToOnePart;
101     fTriggerBiasCorrectionMBToOnePart = 0;
102   }
103 }
104
105 //____________________________________________________________________
106 void
107 AlidNdEtaCorrection::Finish() {
108   //
109   // finish method
110   //
111   // divide the histograms in the AliCorrectionMatrix2D objects to get the corrections
112
113   fTrack2ParticleCorrection->Divide();
114   fVertexRecoCorrection->Divide();
115   fTriggerBiasCorrectionMBToINEL->Divide();
116   fTriggerBiasCorrectionMBToNSD->Divide();
117   fTriggerBiasCorrectionMBToND->Divide();
118   fTriggerBiasCorrectionMBToOnePart->Divide();
119 }
120
121 //____________________________________________________________________
122 Long64_t AlidNdEtaCorrection::Merge(TCollection* list)
123 {
124   // Merge a list of dNdEtaCorrection objects with this (needed for
125   // PROOF).
126   // Returns the number of merged objects (including this).
127
128   if (!list)
129     return 0;
130
131   if (list->IsEmpty())
132     return 1;
133
134   TIterator* iter = list->MakeIterator();
135   TObject* obj;
136
137   // collections of measured and generated histograms
138   TList* collectionNtrackToNparticle    = new TList;
139   TList* collectionVertexReco           = new TList;
140   TList* collectionTriggerBiasMBToINEL  = new TList;
141   TList* collectionTriggerBiasMBToNSD   = new TList;
142   TList* collectionTriggerBiasMBToND    = new TList;
143   TList* collectionTriggerBiasMBToOnePart    = new TList;
144
145   Int_t count = 0;
146   while ((obj = iter->Next())) {
147
148     AlidNdEtaCorrection* entry = dynamic_cast<AlidNdEtaCorrection*> (obj);
149     if (entry == 0)
150       continue;
151
152     collectionNtrackToNparticle  ->Add(entry->fTrack2ParticleCorrection);
153     collectionVertexReco         ->Add(entry->fVertexRecoCorrection);
154     collectionTriggerBiasMBToINEL->Add(entry->fTriggerBiasCorrectionMBToINEL);
155     collectionTriggerBiasMBToNSD ->Add(entry->fTriggerBiasCorrectionMBToNSD);
156     collectionTriggerBiasMBToND  ->Add(entry->fTriggerBiasCorrectionMBToND);
157     collectionTriggerBiasMBToOnePart  ->Add(entry->fTriggerBiasCorrectionMBToOnePart);
158
159     count++;
160   }
161   fTrack2ParticleCorrection      ->Merge(collectionNtrackToNparticle);
162   fVertexRecoCorrection          ->Merge(collectionVertexReco);
163   fTriggerBiasCorrectionMBToINEL ->Merge(collectionTriggerBiasMBToINEL);
164   fTriggerBiasCorrectionMBToNSD  ->Merge(collectionTriggerBiasMBToNSD);
165   fTriggerBiasCorrectionMBToND   ->Merge(collectionTriggerBiasMBToND);
166   fTriggerBiasCorrectionMBToOnePart->Merge(collectionTriggerBiasMBToOnePart);
167
168   delete collectionNtrackToNparticle;
169   delete collectionVertexReco;
170   delete collectionTriggerBiasMBToINEL;
171   delete collectionTriggerBiasMBToNSD;
172   delete collectionTriggerBiasMBToND;
173   delete collectionTriggerBiasMBToOnePart;
174
175   return count+1;
176 }
177
178 //____________________________________________________________________
179 void AlidNdEtaCorrection::Add(AlidNdEtaCorrection* aCorrectionsToAdd, Float_t c) {
180   //
181   // adds the measured and generated of aCorrectionsToAdd to measured and generated
182   // of all corrections in this
183
184   fTrack2ParticleCorrection      ->Add(aCorrectionsToAdd->GetTrack2ParticleCorrection() ,c);
185   fVertexRecoCorrection          ->Add(aCorrectionsToAdd->GetVertexRecoCorrection()     ,c);
186   fTriggerBiasCorrectionMBToINEL ->Add(aCorrectionsToAdd->GetTriggerBiasCorrectionINEL(),c);
187   fTriggerBiasCorrectionMBToNSD  ->Add(aCorrectionsToAdd->GetTriggerBiasCorrectionNSD() ,c);
188   fTriggerBiasCorrectionMBToND   ->Add(aCorrectionsToAdd->GetTriggerBiasCorrectionND()  ,c);
189   fTriggerBiasCorrectionMBToOnePart   ->Add(aCorrectionsToAdd->GetTriggerBiasCorrectionOnePart()  ,c);
190 }
191
192 //____________________________________________________________________
193 void AlidNdEtaCorrection::Scale(Float_t c) 
194 {
195   //
196   // scales all contained corrections
197   // 
198
199   fTrack2ParticleCorrection      ->Scale(c);
200   fVertexRecoCorrection          ->Scale(c);
201   fTriggerBiasCorrectionMBToINEL ->Scale(c);
202   fTriggerBiasCorrectionMBToNSD  ->Scale(c);
203   fTriggerBiasCorrectionMBToND   ->Scale(c);
204   fTriggerBiasCorrectionMBToOnePart   ->Scale(c);
205 }
206
207 //____________________________________________________________________
208 void AlidNdEtaCorrection::Reset(void) {
209   //
210   // reset all corrections
211   // 
212
213   fTrack2ParticleCorrection      ->Reset();
214   fVertexRecoCorrection          ->Reset();
215   fTriggerBiasCorrectionMBToINEL ->Reset();
216   fTriggerBiasCorrectionMBToNSD  ->Reset();
217   fTriggerBiasCorrectionMBToND   ->Reset();
218   fTriggerBiasCorrectionMBToOnePart   ->Reset();
219 }
220
221
222
223 //____________________________________________________________________
224 Bool_t AlidNdEtaCorrection::LoadHistograms(const Char_t* dir)
225 {
226   //
227   // loads the histograms
228   // if dir is empty a directory with the name of this object is taken (like in SaveHistogram)
229   //
230
231   if (!dir)
232     dir = GetName();
233
234   if (!gDirectory->cd(dir))
235     return kFALSE;
236
237   fTrack2ParticleCorrection      ->LoadHistograms();
238   fVertexRecoCorrection          ->LoadHistograms();
239   fTriggerBiasCorrectionMBToINEL ->LoadHistograms();
240   fTriggerBiasCorrectionMBToNSD  ->LoadHistograms();
241   fTriggerBiasCorrectionMBToND   ->LoadHistograms();
242   fTriggerBiasCorrectionMBToOnePart   ->LoadHistograms();
243
244   gDirectory->cd("..");
245
246   return kTRUE;
247 }
248
249 //____________________________________________________________________
250 void AlidNdEtaCorrection::SaveHistograms()
251 {
252   //
253   // save the histograms
254   //
255
256   gDirectory->mkdir(fName.Data());
257   gDirectory->cd(fName.Data());
258
259   fTrack2ParticleCorrection     ->SaveHistograms();
260   fVertexRecoCorrection         ->SaveHistograms();
261   fTriggerBiasCorrectionMBToINEL->SaveHistograms();
262   fTriggerBiasCorrectionMBToNSD ->SaveHistograms();
263   fTriggerBiasCorrectionMBToND  ->SaveHistograms();
264   fTriggerBiasCorrectionMBToOnePart->SaveHistograms();
265
266   gDirectory->cd("..");
267 }
268
269 //____________________________________________________________________
270 void AlidNdEtaCorrection::DrawHistograms()
271 {
272   //
273   // call the draw histogram method of the corrections
274   //
275
276   fTrack2ParticleCorrection     ->DrawHistograms();
277   fVertexRecoCorrection         ->DrawHistograms();
278   fTriggerBiasCorrectionMBToINEL->DrawHistograms();
279   fTriggerBiasCorrectionMBToNSD ->DrawHistograms();
280   fTriggerBiasCorrectionMBToND  ->DrawHistograms();
281   fTriggerBiasCorrectionMBToOnePart  ->DrawHistograms();
282 }
283
284 //____________________________________________________________________
285 void AlidNdEtaCorrection::DrawOverview(const char* canvasName)
286 {
287   //
288   // call the DrawOverview histogram method of the corrections
289   //
290
291   fTrack2ParticleCorrection     ->DrawOverview(canvasName);
292   fVertexRecoCorrection         ->DrawOverview(canvasName);
293   fTriggerBiasCorrectionMBToINEL->DrawOverview(canvasName);
294   fTriggerBiasCorrectionMBToNSD ->DrawOverview(canvasName);
295   fTriggerBiasCorrectionMBToND  ->DrawOverview(canvasName);
296   fTriggerBiasCorrectionMBToOnePart  ->DrawOverview(canvasName);
297 }
298
299 //____________________________________________________________________
300 void AlidNdEtaCorrection::FillMCParticle(Float_t vtx, Float_t eta, Float_t pt, Bool_t trigger, Bool_t vertex, Int_t processType)
301 {
302   // fills a particle in the corrections
303   // it is filled in generated or measured depending of the flags
304
305   fTriggerBiasCorrectionMBToINEL->GetTrackCorrection()->FillGene(vtx, eta, pt);
306
307   if ((processType & AliPWG0Helper::kSD) == 0)
308     fTriggerBiasCorrectionMBToNSD->GetTrackCorrection()->FillGene(vtx, eta, pt);
309
310   if (processType & AliPWG0Helper::kND )
311     fTriggerBiasCorrectionMBToND->GetTrackCorrection()->FillGene(vtx, eta, pt);
312
313   if (processType & AliPWG0Helper::kOnePart)
314     fTriggerBiasCorrectionMBToOnePart->GetTrackCorrection()->FillGene(vtx, eta, pt);
315
316   if (!trigger)
317     return;
318
319   fTriggerBiasCorrectionMBToINEL->GetTrackCorrection()->FillMeas(vtx, eta, pt);
320   fTriggerBiasCorrectionMBToNSD->GetTrackCorrection()->FillMeas(vtx, eta, pt);
321   fTriggerBiasCorrectionMBToND->GetTrackCorrection()->FillMeas(vtx, eta, pt);
322   fTriggerBiasCorrectionMBToOnePart->GetTrackCorrection()->FillMeas(vtx, eta, pt);
323   fVertexRecoCorrection->GetTrackCorrection()->FillGene(vtx, eta, pt);
324
325   if (!vertex)
326     return;
327
328   fVertexRecoCorrection->GetTrackCorrection()->FillMeas(vtx, eta, pt);
329   fTrack2ParticleCorrection->GetTrackCorrection()->FillGene(vtx, eta, pt);
330 }
331
332 //____________________________________________________________________
333 void AlidNdEtaCorrection::FillTrackedParticle(Float_t vtx, Float_t eta, Float_t pt, Double_t weight)
334 {
335   // fills a tracked particle in the corrections
336
337   fTrack2ParticleCorrection->GetTrackCorrection()->FillMeas(vtx, eta, pt, weight);
338 }
339
340 //____________________________________________________________________
341 void AlidNdEtaCorrection::FillEvent(Float_t vtx, Float_t n, Bool_t trigger, Bool_t vertex, Int_t processType)
342 {
343   // fills an event int he correction
344   // it is filled in generated or measured depending of the flags
345
346   fTriggerBiasCorrectionMBToINEL->GetEventCorrection()->FillGene(vtx, n);
347
348   if ((processType & AliPWG0Helper::kSD) == 0)
349     fTriggerBiasCorrectionMBToNSD->GetEventCorrection()->FillGene(vtx, n);
350
351   if (processType & AliPWG0Helper::kND )
352     fTriggerBiasCorrectionMBToND->GetEventCorrection()->FillGene(vtx, n);
353
354   if (processType & AliPWG0Helper::kOnePart)
355     fTriggerBiasCorrectionMBToOnePart->GetEventCorrection()->FillGene(vtx, n);
356
357   if (!trigger)
358     return;
359
360   fTriggerBiasCorrectionMBToINEL->GetEventCorrection()->FillMeas(vtx, n);
361   fTriggerBiasCorrectionMBToNSD->GetEventCorrection()->FillMeas(vtx, n);
362   fTriggerBiasCorrectionMBToND->GetEventCorrection()->FillMeas(vtx, n);
363   fTriggerBiasCorrectionMBToOnePart->GetEventCorrection()->FillMeas(vtx, n);
364   fVertexRecoCorrection->GetEventCorrection()->FillGene(vtx, n);
365
366   if (!vertex)
367     return;
368
369   fVertexRecoCorrection->GetEventCorrection()->FillMeas(vtx, n);
370 }
371
372 //____________________________________________________________________
373 Float_t AlidNdEtaCorrection::GetMeasuredFraction(CorrectionType correctionType, Float_t ptCutOff, Float_t eta, Int_t vertexBegin, Int_t vertexEnd, Bool_t debug)
374 {
375   // calculates the fraction of particles measured (some are missed due to the pt cut off)
376   //
377   // uses the generated particle histogram from the correction passed, e.g. pass GetTrack2ParticleCorrection()
378
379   if (!GetCorrection(correctionType))
380     return -1;
381
382   const TH3* generated = GetCorrection(correctionType)->GetTrackCorrection()->GetGeneratedHistogram();
383
384   // find eta borders, if eta is negative assume -0.8 ... 0.8
385   Int_t etaBegin = 0;
386   Int_t etaEnd = 0;
387   if (eta < -99)
388   {
389     etaBegin = generated->GetYaxis()->FindBin(-0.8);
390     etaEnd = generated->GetYaxis()->FindBin(0.8);
391   }
392   else
393   {
394     etaBegin = generated->GetYaxis()->FindBin(eta);
395     etaEnd = etaBegin;
396   }
397
398   if (vertexBegin == -1)
399     vertexBegin = generated->GetXaxis()->FindBin(-9.99);
400
401   if (vertexEnd == -1)
402     vertexEnd = generated->GetXaxis()->FindBin(9.99);
403
404   TH1D* ptProj = dynamic_cast<TH1D*> (generated->ProjectionZ(Form("%s_pt", generated->GetName()), vertexBegin, vertexEnd, etaBegin, etaEnd));
405   //printf("GetMeasuredFraction: bin range %d %d %d %d\n", vertexBegin, vertexEnd, etaBegin, etaEnd);
406   ptProj->GetXaxis()->SetTitle(generated->GetZaxis()->GetTitle());
407
408   Int_t ptBin = ptProj->FindBin(ptCutOff);
409   //printf("GetMeasuredFraction: bin range %d %d\n", ptBin, ptProj->GetNbinsX());
410   Float_t abovePtCut = ptProj->Integral(ptBin, ptProj->GetNbinsX()+1);
411   Float_t all = ptProj->Integral(1, ptProj->GetNbinsX()+1);
412
413   if (all == 0)
414     return -1;
415
416   Float_t fraction = abovePtCut / all;
417
418   //printf("GetMeasuredFraction: all %f above %f fraction %f\n", all, abovePtCut, fraction);
419
420   if (debug)
421   {
422     new TCanvas;
423     ptProj->Draw();
424   }
425   else
426     delete ptProj;
427
428   if (debug)
429     printf("AlidNdEtaCorrection::GetMeasuredFraction: pt cut off = %f, eta = %f, => fraction = %f\n", ptCutOff, eta, fraction);
430
431   return fraction;
432 }
433
434 //____________________________________________________________________
435 TH1* AlidNdEtaCorrection::GetMeasuredEventFraction(CorrectionType correctionType, Int_t multCut)
436 {
437   // calculates the fraction of events above multCut (but including it)
438   //
439   // uses the generated event histogram from the correction passed, e.g. pass GetTrack2ParticleCorrection()
440
441   if (!GetCorrection(correctionType))
442     return 0;
443
444   const TH2* generated = GetCorrection(correctionType)->GetEventCorrection()->GetGeneratedHistogram();
445
446   TH1* allEvents = generated->ProjectionX(Form("%s_all", generated->GetName()), 1, generated->GetNbinsY());
447   TH1* aboveEvents = generated->ProjectionX(Form("%s_above", generated->GetName()), generated->GetYaxis()->FindBin(multCut), generated->GetNbinsY());
448   
449   aboveEvents->Divide(aboveEvents, allEvents, 1, 1, "B");
450
451   return aboveEvents;  
452 }
453
454 //____________________________________________________________________
455 void AlidNdEtaCorrection::ReduceInformation()
456 {
457   // this function deletes the measured and generated histograms from the corrections to reduce the amount of data
458   // in memory
459
460   // these are needed for GetMeasuredFraction(): fTrack2ParticleCorrection->ReduceInformation();
461   fVertexRecoCorrection          ->ReduceInformation();
462   fTriggerBiasCorrectionMBToINEL ->ReduceInformation();
463   fTriggerBiasCorrectionMBToNSD  ->ReduceInformation();
464   fTriggerBiasCorrectionMBToND   ->ReduceInformation();
465   fTriggerBiasCorrectionMBToOnePart   ->ReduceInformation();
466 }
467
468 //____________________________________________________________________
469 AliCorrection* AlidNdEtaCorrection::GetCorrection(CorrectionType correctionType)
470 {
471   // returns the given correction
472
473   switch (correctionType)
474   {
475     case kNone : return 0;
476     case kTrack2Particle : return fTrack2ParticleCorrection;
477     case kVertexReco :     return fVertexRecoCorrection;
478     case kINEL :           return fTriggerBiasCorrectionMBToINEL;
479     case kNSD :            return fTriggerBiasCorrectionMBToNSD;
480     case kND :             return fTriggerBiasCorrectionMBToND;
481     case kOnePart :             return fTriggerBiasCorrectionMBToOnePart;
482   }
483
484   return 0;
485 }