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