Compilation on Windows/Cygwin
[u/mrichter/AliRoot.git] / PWG0 / AliCorrection.cxx
1 /* $Id$ */
2
3 // ------------------------------------------------------
4 //
5 // Class to handle corrections.
6 //
7 // ------------------------------------------------------
8 //
9
10 #include <TFile.h>
11 #include <TCanvas.h>
12 #include <TH3F.h>
13 #include <TH2F.h>
14
15 #include <AliLog.h>
16 #include "AliCorrectionMatrix2D.h"
17 #include "AliCorrectionMatrix3D.h"
18
19 #include "AliCorrection.h"
20
21 //____________________________________________________________________
22 ClassImp(AliCorrection)
23
24 //____________________________________________________________________
25 AliCorrection::AliCorrection() : TNamed(),
26   fEventCorr(0),
27   fTrackCorr(0)
28 {
29   // default constructor
30 }
31
32 //____________________________________________________________________
33 AliCorrection::AliCorrection(const Char_t* name, const Char_t* title, AliPWG0Helper::AnalysisMode analysisMode) : TNamed(name, title),
34   fEventCorr(0),
35   fTrackCorr(0)
36 {
37   // constructor initializing tnamed
38
39   Float_t* binLimitsPt = 0;
40   Int_t nBinsPt = 0;
41
42   // different binnings, better solution could be anticipated...
43   if (analysisMode == AliPWG0Helper::kTPC || analysisMode == AliPWG0Helper::kTPCITS)
44   {
45     static Float_t binLimitsPtTmp[] = {0.0, 0.05, 0.1, 0.125, 0.15, 0.175, 0.2, 0.225, 0.25, 0.275, 0.3, 0.325, 0.35, 0.375, 0.4, 0.425, 0.45, 0.475, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.5, 2.0, 5.0, 10.0, 100.0};
46     binLimitsPt = (Float_t*) binLimitsPtTmp;
47     nBinsPt = 28;
48   } 
49   else if (analysisMode == AliPWG0Helper::kSPD)
50   {
51     static Float_t binLimitsPtTmp[] = {-0.5, 100.0};
52     binLimitsPt = (Float_t*) binLimitsPtTmp;
53     nBinsPt = 1;
54   }
55
56   if (!binLimitsPt)
57   {
58     Printf("FATAL: Invalid binning");
59     return;
60   }
61
62   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, 10.5, 12.5, 14.5, 16.5, 18.5, 20.5, 25.5, 30.5, 40.5, 50.5, 100.5, 300.5};
63   //Float_t binLimitsVtx[] = {-20,-15,-10,-6,-3,0,3,6,10,15,20};
64   //Float_t binLimitsVtx[] = {-20,-19,-18,-17,-16,-15,-14,-13,-12,-11,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
65   Float_t binLimitsVtx[] = {-20,-15,-10,-8,-6,-4,-2,0,2,4,6,8,10,15,20};
66   Float_t binLimitsEta[] = {-2,-1.9,-1.8,-1.7,-1.6,-1.5,-1.4,-1.3,-1.2,-1.1,-1.0,-0.9,-0.8,-0.7,-0.6,-0.5,-0.4,-0.3,-0.2,-0.1,0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2.0};
67 //  Float_t binLimitsEta[] = { -7.0, -6.9, -6.8, -6.7, -6.6, -6.5, -6.4, -6.3, -6.2, -6.1, -6.0, -5.9, -5.8, -5.7, -5.6, -5.5, -5.4, -5.3, -5.2, -5.1, -5.0, -4.9, -4.8, -4.7, -4.6, -4.5, -4.4, -4.3, -4.2, -4.1, -4.0, -3.9, -3.8, -3.7, -3.6, -3.5, -3.4, -3.3, -3.2, -3.1, -3.0, -2.9, -2.8, -2.7, -2.6, -2.5, -2.4, -2.3, -2.2, -2.1, -2.0, -1.9, -1.8, -1.7, -1.6, -1.5, -1.4, -1.3, -1.2, -1.1, -1.0, -0.9, -0.8, -0.7, -0.6, -0.5, -0.4, -0.3, -0.2, -0.1, -0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3.0, 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4.0, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5.0, 5.1, 5.2, 5.3, 5.4, 5.5, 5.6, 5.7, 5.8, 5.9, 6.0, 6.1, 6.2, 6.3, 6.4, 6.5, 6.6, 6.7, 6.8, 6.9, 7.0 };
68
69   TH3F* dummyBinning = new TH3F("dummyBinning","dummyBinning",14, binLimitsVtx, 40, binLimitsEta , nBinsPt, binLimitsPt);
70
71   fEventCorr = new AliCorrectionMatrix2D("EventCorrection", Form("%s EventCorrection", fTitle.Data()), 14, binLimitsVtx, 22, binLimitsN);
72   fTrackCorr = new AliCorrectionMatrix3D("TrackCorrection", Form("%s TrackCorrection", fTitle.Data()), dummyBinning);
73
74   delete dummyBinning;
75
76   fEventCorr->SetAxisTitles("vtx z [cm]", "Ntracks");
77   fTrackCorr->SetAxisTitles("vtx z [cm]", "#eta", "p_{T} [GeV/c]");
78 }
79
80 //____________________________________________________________________
81 AliCorrection::AliCorrection(const AliCorrection& c) : TNamed(c),
82   fEventCorr(0),
83   fTrackCorr(0)
84 {
85   // copy constructor
86   ((AliCorrection &)c).Copy(*this);
87 }
88
89 //____________________________________________________________________
90 AliCorrection::~AliCorrection()
91 {
92   //
93   // destructor
94   //
95
96   if (fEventCorr)
97   {
98     delete fEventCorr;
99     fEventCorr = 0;
100   }
101
102   if (fTrackCorr)
103   {
104     delete fTrackCorr;
105     fTrackCorr = 0;
106   }
107 }
108
109 //____________________________________________________________________
110 AliCorrection &AliCorrection::operator=(const AliCorrection &c)
111 {
112   // assigment operator
113
114   if (this != &c)
115     ((AliCorrection &) c).Copy(*this);
116
117   return *this;
118 }
119
120 //____________________________________________________________________
121 void AliCorrection::Copy(TObject& c) const
122 {
123   // copy function
124
125   AliCorrection& target = (AliCorrection &) c;
126
127   if (fEventCorr)
128     target.fEventCorr = dynamic_cast<AliCorrectionMatrix2D*> (fEventCorr->Clone());
129
130   if (fTrackCorr)
131     target.fTrackCorr = dynamic_cast<AliCorrectionMatrix3D*> (fTrackCorr->Clone());
132 }
133
134 //____________________________________________________________________
135 Long64_t AliCorrection::Merge(TCollection* list)
136 {
137   // Merge a list of AliCorrection objects with this (needed for
138   // PROOF). 
139   // Returns the number of merged objects (including this).
140
141   if (!list)
142     return 0;
143   
144   if (list->IsEmpty())
145     return 1;
146
147   TIterator* iter = list->MakeIterator();
148   TObject* obj;
149
150   // collections of measured and generated histograms
151   TList* collectionEvent = new TList;
152   TList* collectionTrack = new TList;
153   
154   Int_t count = 0;
155   while ((obj = iter->Next())) {
156     
157     AliCorrection* entry = dynamic_cast<AliCorrection*> (obj);
158     if (entry == 0) 
159       continue;
160
161     collectionEvent->Add(entry->fEventCorr);
162     collectionTrack->Add(entry->fTrackCorr);
163
164     count++;
165   }
166   fEventCorr->Merge(collectionEvent);
167   fTrackCorr->Merge(collectionTrack);
168
169   delete collectionEvent;
170   delete collectionTrack;
171
172   return count+1;
173 }
174
175 //____________________________________________________________________
176 void AliCorrection::Divide()
177 {
178   //
179   // divide the histograms to get the correction
180   //
181   
182   if (!fEventCorr || !fTrackCorr)
183     return;
184     
185   fEventCorr->Divide();
186   fTrackCorr->Divide();
187
188   Int_t emptyBins = fTrackCorr->CheckEmptyBins(-9.99, 9.99, -0.79, 0.79, 0.3, 4.9);
189   printf("INFO: In the central region the track correction of %s has %d empty bins\n", GetName(), emptyBins);
190 }
191
192 //____________________________________________________________________
193 void AliCorrection::Add(AliCorrection* aCorrectionToAdd, Float_t c)
194 {
195   //
196   // add to measured and generated the measured and generated of aCorrectionToAdd
197   // with the weight c
198
199   fEventCorr->Add(aCorrectionToAdd->GetEventCorrection(),c);
200   fTrackCorr->Add(aCorrectionToAdd->GetTrackCorrection(),c);
201 }
202
203
204 //____________________________________________________________________
205 Bool_t AliCorrection::LoadHistograms(const Char_t* dir)
206 {
207   //
208   // loads the histograms from a file
209   // if dir is empty a directory with the name of this object is taken (like in SaveHistogram)
210   //
211
212   if (!fEventCorr || !fTrackCorr)
213     return kFALSE;
214
215   if (!dir)
216     dir = GetName();
217
218   if (!gDirectory->cd(dir))
219     return kFALSE;
220
221   Bool_t success = fEventCorr->LoadHistograms();
222   success &= fTrackCorr->LoadHistograms();
223
224   gDirectory->cd("..");
225
226   return success;
227 }
228
229 //____________________________________________________________________
230 void AliCorrection::SaveHistograms()
231 {
232   //
233   // saves the histograms in a directory with the name of this object (GetName)
234   //
235   
236   gDirectory->mkdir(GetName());
237   gDirectory->cd(GetName());
238
239   if (fEventCorr)
240     fEventCorr->SaveHistograms();
241
242   if (fTrackCorr)
243     fTrackCorr->SaveHistograms();
244     
245   gDirectory->cd("..");
246 }
247
248 //____________________________________________________________________
249 void AliCorrection::ReduceInformation()
250 {
251   // this function deletes the measured and generated histograms to reduce the amount of data
252   // in memory
253
254   if (!fEventCorr || !fTrackCorr)
255     return;
256
257   fEventCorr->ReduceInformation();
258   fTrackCorr->ReduceInformation();
259 }
260
261 //____________________________________________________________________
262 void AliCorrection::Reset(Option_t* option)
263 {
264   // resets the histograms
265
266   if (fEventCorr)
267     fEventCorr->Reset(option);
268
269   if (fTrackCorr)
270     fTrackCorr->Reset(option);
271 }
272
273 //____________________________________________________________________
274 void AliCorrection::DrawHistograms(const Char_t* name)
275 {
276   // draws the corrections
277
278   if (!name)
279     name = GetName();
280
281   if (fEventCorr)
282     fEventCorr->DrawHistograms(Form("%s event", name));
283
284   if (fTrackCorr)
285     fTrackCorr->DrawHistograms(Form("%s track", name));
286 }
287
288 void AliCorrection::DrawOverview(const char* canvasName)
289 {
290   // draw projection of the corrections
291   //   to the 3 axis of fTrackCorr and 2 axis of fEventCorr
292
293   TString canvasNameTmp(GetName());
294   if (canvasName)
295     canvasNameTmp = canvasName;
296
297   TCanvas* canvas = new TCanvas(canvasNameTmp, canvasNameTmp, 1200, 800);
298   canvas->Divide(3, 2);
299
300   if (fTrackCorr) {
301     canvas->cd(1);
302     fTrackCorr->Get1DCorrectionHistogram("x", 0.3, 5, -1, 1)->DrawCopy()->GetYaxis()->SetRangeUser(0, 10);
303
304     canvas->cd(2);
305     fTrackCorr->Get1DCorrectionHistogram("y", 0.3, 5, 0, 0)->DrawCopy()->GetYaxis()->SetRangeUser(0, 10);
306
307     canvas->cd(3);
308     fTrackCorr->Get1DCorrectionHistogram("z", 0, -1, -1, 1)->DrawCopy()->GetYaxis()->SetRangeUser(0, 10);
309   }
310
311   if (fEventCorr)
312   {
313     canvas->cd(4);
314     fEventCorr->Get1DCorrectionHistogram("x")->DrawCopy();
315
316     canvas->cd(5);
317     fEventCorr->Get1DCorrectionHistogram("y")->DrawCopy()->GetXaxis()->SetRangeUser(0, 30);
318   }
319 }
320
321 //____________________________________________________________________
322 void AliCorrection::SetCorrectionToUnity()
323 {
324   // set the corrections to unity
325
326   if (fEventCorr)
327     fEventCorr->SetCorrectionToUnity();
328
329   if (fTrackCorr)
330     fTrackCorr->SetCorrectionToUnity();
331 }
332
333 //____________________________________________________________________
334 void AliCorrection::Multiply()
335 {
336   // call Multiply
337
338   if (fEventCorr)
339   {
340     fEventCorr->Multiply();
341     // now we manually copy the overflow bin of the y axis (multiplicity) over. This is important to get the event count correct
342     TH2F* hist = fEventCorr->GetMeasuredHistogram();
343     for (Int_t x = 1; x <= hist->GetNbinsX(); ++x)
344       fEventCorr->GetGeneratedHistogram()->SetBinContent(x, hist->GetNbinsY() + 1, hist->GetBinContent(x, hist->GetNbinsY() + 1));
345   }
346
347   if (fTrackCorr)
348     fTrackCorr->Multiply();
349 }
350
351 //____________________________________________________________________
352 void AliCorrection::Scale(Double_t factor)
353 {
354   // scales the two contained corrections
355
356   fEventCorr->Scale(factor);
357   fTrackCorr->Scale(factor);
358 }
359
360 //____________________________________________________________________
361 void AliCorrection::PrintInfo(Float_t ptCut)
362 {
363   // prints some stats
364
365   TH3* measured = GetTrackCorrection()->GetMeasuredHistogram();
366   TH3* generated = GetTrackCorrection()->GetGeneratedHistogram();
367
368   TH2* measuredEvents = GetEventCorrection()->GetMeasuredHistogram();
369   TH2* generatedEvents = GetEventCorrection()->GetGeneratedHistogram();
370
371   Printf("AliCorrection::PrintInfo: Whole phasespace:");
372
373   Printf("tracks measured: %.1f tracks generated: %.1f, events measured: %1.f, events generated %1.f", measured->Integral(), generated->Integral(), measuredEvents->Integral(), generatedEvents->Integral());
374
375   Printf("AliCorrection::PrintInfo: Values in |eta| < 0.8, |vtx-z| < 10 and pt > %.2f:", ptCut);
376
377   Float_t tracksM = measured->Integral(measured->GetXaxis()->FindBin(-9.9), measured->GetXaxis()->FindBin(9.9), measured->GetYaxis()->FindBin(-0.79), measured->GetYaxis()->FindBin(0.79), measured->GetZaxis()->FindBin(ptCut), measured->GetZaxis()->GetNbins());
378   Float_t tracksG = generated->Integral(generated->GetXaxis()->FindBin(-9.9), generated->GetXaxis()->FindBin(9.9), generated->GetYaxis()->FindBin(-0.79), generated->GetYaxis()->FindBin(0.79), generated->GetZaxis()->FindBin(ptCut), generated->GetZaxis()->GetNbins());
379
380   Float_t eventsM = measuredEvents->Integral(measuredEvents->GetXaxis()->FindBin(-9.9), measuredEvents->GetXaxis()->FindBin(9.9), 1, measuredEvents->GetNbinsY());
381   Float_t eventsG = generatedEvents->Integral(generatedEvents->GetXaxis()->FindBin(-9.9), generatedEvents->GetXaxis()->FindBin(9.9), 1, generatedEvents->GetNbinsY());
382
383   Printf("tracks measured: %.1f tracks generated: %.1f, events measured: %1.f, events generated %1.f", tracksM, tracksG, eventsM, eventsG);
384
385   if (tracksM > 0)
386     Printf("Effective average correction factor for TRACKS: %.3f", tracksG / tracksM);
387   if (eventsM > 0)
388   Printf("Effective average correction factor for EVENTS: %.3f", eventsG / eventsM);
389
390   if (eventsM > 0 && eventsG > 0)
391   {
392     // normalize to number of events;
393     tracksM /= eventsM;
394     tracksG /= eventsG;
395
396     Printf("%.2f tracks/event measured, %.2f tracks/event after correction --> effective average correction factor is %.3f (pt cut %.2f GeV/c)", tracksM, tracksG, tracksG / tracksM, ptCut);
397   }
398 }