implemented destructors
[u/mrichter/AliRoot.git] / PWG0 / dNdEta / AlidNdEtaCorrection.cxx
1 /* $Id$ */
2
3 #include "AlidNdEtaCorrection.h"
4
5 #include <TCanvas.h>
6 #include <TH3F.h>
7 #include <TH1D.h>
8
9 //____________________________________________________________________
10 ClassImp(AlidNdEtaCorrection)
11
12 //____________________________________________________________________
13 AlidNdEtaCorrection::AlidNdEtaCorrection()
14   : TNamed(),
15   fTrack2ParticleCorrection(0),
16   fVertexRecoCorrection(0),
17   fTriggerCorrection(0),
18   fTriggerBiasCorrection(0),
19   fNEvents(0),
20   fNTriggeredEvents(0)
21 {
22   // default constructor
23 }
24
25 //____________________________________________________________________
26 AlidNdEtaCorrection::AlidNdEtaCorrection(const Char_t* name, const Char_t* title)
27   : TNamed(name, title),
28   fTrack2ParticleCorrection(0),
29   fVertexRecoCorrection(0),
30   fTriggerCorrection(0),
31   fTriggerBiasCorrection(0),
32   fNEvents(0),
33   fNTriggeredEvents(0)
34 {
35   // constructor
36   //
37
38   Float_t binLimitsPt[] = {0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.5, 2.0, 10.0, 100.0};
39
40   fTrack2ParticleCorrection = new AliCorrectionMatrix3D("nTrackToNPart", "nTrackToNPart", 40, -20, 20, 60, -6, 6, 14, binLimitsPt);
41
42   Float_t binLimitsN[]   = {-0.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5,
43                             10.5, 12.5, 14.5, 16.5, 18.5, 20.5, 25.5, 30.5, 40.5, 50.5, 100.5, 300.5};
44   Float_t binLimitsVtx[] = {-20,-15,-10,-6,-3,0,3,6,10,15,20};
45
46   fVertexRecoCorrection        = new AliCorrectionMatrix2D("vtxReco",       "vtxReco",10,binLimitsVtx ,22,binLimitsN);
47   fTriggerCorrection           = new AliCorrectionMatrix2D("trigger",       "trigger",10,binLimitsVtx ,22,binLimitsN);
48
49   fTriggerBiasCorrection       = new AliCorrectionMatrix2D("triggerBias",   "triggerBias",120,-6,6,100, 0, 10);
50
51   fTrack2ParticleCorrection ->SetAxisTitles("vtx z [cm]", "#eta", "p_{T} [GeV/c]");
52   fVertexRecoCorrection        ->SetAxisTitles("vtx z [cm]", "Ntracks");
53   fTriggerCorrection        ->SetAxisTitles("vtx z [cm]", "Ntracks");
54
55   fTriggerBiasCorrection       ->SetAxisTitles("#eta", "p_{T} [GeV/c]");
56 }
57
58 //____________________________________________________________________
59 AlidNdEtaCorrection::~AlidNdEtaCorrection()
60 {
61   // destructor
62
63   if (fTrack2ParticleCorrection)
64   {
65     delete fTrack2ParticleCorrection;
66     fTrack2ParticleCorrection = 0;
67   }
68
69   if (fVertexRecoCorrection)
70   {
71     delete fVertexRecoCorrection;
72     fVertexRecoCorrection = 0;
73   }
74
75   if (fTriggerCorrection)
76   {
77     delete fTriggerCorrection;
78     fTriggerCorrection = 0;
79   }
80
81   if (fTriggerBiasCorrection)
82   {
83     delete fTriggerBiasCorrection;
84     fTriggerBiasCorrection = 0;
85   }
86 }
87
88 //____________________________________________________________________
89 void
90 AlidNdEtaCorrection::Finish() {
91   //
92   // finish method
93   //
94   // divide the histograms in the AliCorrectionMatrix2D objects to get the corrections
95
96   fTrack2ParticleCorrection->Divide();
97
98   TH3F* hist = fTrack2ParticleCorrection->GetCorrectionHistogram();
99   Int_t emptyBins = 0;
100   for (Int_t x=hist->GetXaxis()->FindBin(-10); x<=hist->GetXaxis()->FindBin(10); ++x)
101     for (Int_t y=hist->GetYaxis()->FindBin(-0.8); y<=hist->GetYaxis()->FindBin(0.8); ++y)
102       for (Int_t z=hist->GetZaxis()->FindBin(0.3); z<=hist->GetZaxis()->FindBin(9.9); ++z)
103         if (hist->GetBinContent(x, y, z) == 0)
104         {
105           printf("Empty bin in fTrack2ParticleCorrection at vtx = %f, eta = %f, pt = %f\n", hist->GetXaxis()->GetBinCenter(x), hist->GetYaxis()->GetBinCenter(y), hist->GetZaxis()->GetBinCenter(z));
106           ++emptyBins;
107         }
108
109   printf("INFO: In the central region fTrack2ParticleCorrection has %d empty bins\n", emptyBins);
110
111   fVertexRecoCorrection->Divide();
112   fTriggerCorrection->Divide();
113
114   if (fNEvents == 0)
115   {
116     printf("ERROR: fNEvents is empty. Cannot scale histogram. Skipping processing of fTriggerBiasCorrection\n");
117     return;
118   }
119   fTriggerBiasCorrection->GetMeasuredHistogram()->Scale(Double_t(fNTriggeredEvents)/Double_t(fNEvents));
120   fTriggerBiasCorrection->Divide();
121 }
122
123 //____________________________________________________________________
124 Long64_t 
125 AlidNdEtaCorrection::Merge(TCollection* list) {
126   // Merge a list of dNdEtaCorrection objects with this (needed for
127   // PROOF). 
128   // Returns the number of merged objects (including this).
129
130   if (!list)
131     return 0;
132   
133   if (list->IsEmpty())
134     return 1;
135
136   TIterator* iter = list->MakeIterator();
137   TObject* obj;
138
139   // collections of measured and generated histograms
140   TList* collectionNtrackToNparticle = new TList;
141   TList* collectionVertexReco        = new TList;
142   TList* collectionTriggerBias       = new TList;
143
144   Int_t count = 0;
145   while ((obj = iter->Next())) {
146     
147     AlidNdEtaCorrection* entry = dynamic_cast<AlidNdEtaCorrection*> (obj);
148     if (entry == 0) 
149       continue;
150
151     collectionNtrackToNparticle ->Add(entry->GetTrack2ParticleCorrection());
152     collectionVertexReco        ->Add(entry->GetVertexRecoCorrection());
153     collectionTriggerBias        ->Add(entry->GetTriggerBiasCorrection());
154
155     count++;
156   }
157   fTrack2ParticleCorrection ->Merge(collectionNtrackToNparticle);
158   fVertexRecoCorrection        ->Merge(collectionVertexReco);
159   fTriggerBiasCorrection        ->Merge(collectionTriggerBias);
160   
161   delete collectionNtrackToNparticle;
162   delete collectionVertexReco;
163   delete collectionTriggerBias;
164
165   return count+1;
166 }
167
168
169 //____________________________________________________________________
170 Bool_t
171 AlidNdEtaCorrection::LoadHistograms(const Char_t* fileName, const Char_t* dir) {
172   //
173   // loads the histograms
174   //
175
176   fTrack2ParticleCorrection ->LoadHistograms(fileName, dir);
177   fVertexRecoCorrection        ->LoadHistograms(fileName, dir);
178   fTriggerCorrection        ->LoadHistograms(fileName, dir);
179   fTriggerBiasCorrection       ->LoadHistograms(fileName, dir);
180
181   return kTRUE;
182 }
183
184
185 //____________________________________________________________________
186 void
187 AlidNdEtaCorrection::SaveHistograms() {
188   //
189   // save the histograms
190   //
191
192   gDirectory->mkdir(fName.Data());
193   gDirectory->cd(fName.Data());
194
195   fTrack2ParticleCorrection->SaveHistograms();
196   fVertexRecoCorrection->SaveHistograms();
197   fTriggerCorrection->SaveHistograms();
198   fTriggerBiasCorrection->SaveHistograms();
199
200   gDirectory->cd("../");
201 }
202
203 //____________________________________________________________________
204 void AlidNdEtaCorrection::DrawHistograms()
205 {
206   //
207   // call the draw histogram method of the two AliCorrectionMatrix2D objects
208
209   fTrack2ParticleCorrection ->DrawHistograms();
210   fVertexRecoCorrection        ->DrawHistograms();
211   fTriggerCorrection        ->DrawHistograms();
212   fTriggerBiasCorrection       ->DrawHistograms();
213
214 }
215
216 //____________________________________________________________________
217 Float_t AlidNdEtaCorrection::GetMeasuredFraction(Float_t ptCutOff, Float_t eta, Bool_t debug)
218 {
219   // calculates the fraction of particles measured (some are missed due to the pt cut off)
220   // uses the generated particle histogram from fTrack2ParticleCorrection
221
222   const TH3F* generated = fTrack2ParticleCorrection->GetGeneratedHistogram();
223
224   // find eta borders, if eta is negative assume -0.8 ... 0.8
225   Int_t etaBegin = 0;
226   Int_t etaEnd = 0;
227   if (eta < 0)
228   {
229     etaBegin = generated->GetYaxis()->FindBin(-0.8);
230     etaEnd = generated->GetYaxis()->FindBin(0.8);
231   }
232   else
233   {
234     etaBegin = generated->GetYaxis()->FindBin(eta);
235     etaEnd = etaBegin;
236   }
237
238   Int_t vertexBegin = generated->GetXaxis()->FindBin(-10);
239   Int_t vertexEnd = generated->GetXaxis()->FindBin(10);
240
241   TH1D* ptProj = dynamic_cast<TH1D*> (generated->ProjectionZ(Form("%s_pt", generated->GetName()), vertexBegin, vertexEnd, etaBegin, etaEnd));
242   ptProj->GetXaxis()->SetTitle(generated->GetZaxis()->GetTitle());
243
244   Int_t ptBin = ptProj->FindBin(ptCutOff);
245   Float_t abovePtCut = ptProj->Integral(ptBin, ptProj->GetNbinsX());
246   Float_t all = ptProj->Integral();
247
248   if (all == 0)
249     return -1;
250
251   Float_t fraction = abovePtCut / all;
252
253   if (debug)
254   {
255     new TCanvas;
256     ptProj->Draw();
257   }
258   else
259     delete ptProj;
260
261   return fraction;
262 }
263
264 void AlidNdEtaCorrection::ReduceInformation()
265 {
266   // this function deletes the measured and generated histograms from the corrections to reduce the amount of data
267   // in memory
268
269   // these are needed for GetMeasuredFraction(): fTrack2ParticleCorrection->ReduceInformation();
270   fVertexRecoCorrection->ReduceInformation();
271   fTriggerCorrection->ReduceInformation();
272   fTriggerBiasCorrection->ReduceInformation();
273 }