added correction for events with vertex but 0 tracks
[u/mrichter/AliRoot.git] / PWG0 / AliCorrectionMatrix.cxx
CommitLineData
bf21645b 1/* $Id$ */
2
3// ------------------------------------------------------
4//
5// Class to handle corrections.
6//
7// ------------------------------------------------------
8//
9
10#include <TFile.h>
11#include <TCanvas.h>
12#include <TH2F.h>
13
14#include <AliLog.h>
15
16#include "AliCorrectionMatrix.h"
17
18//____________________________________________________________________
19ClassImp(AliCorrectionMatrix)
20
21//____________________________________________________________________
22AliCorrectionMatrix::AliCorrectionMatrix() : TNamed(),
23 fhMeas(0),
24 fhGene(0),
25 fhCorr(0)
26{
27 // default constructor
28}
29
61385583 30//____________________________________________________________________
bf21645b 31AliCorrectionMatrix::AliCorrectionMatrix(const Char_t* name, const Char_t* title) : TNamed(name, title),
32 fhMeas(0),
33 fhGene(0),
34 fhCorr(0)
35{
36 // constructor initializing tnamed
37}
38
39//____________________________________________________________________
61385583 40AliCorrectionMatrix::AliCorrectionMatrix(const AliCorrectionMatrix& c) : TNamed(c),
41 fhMeas(0),
42 fhGene(0),
43 fhCorr(0)
bf21645b 44{
45 // copy constructor
46 ((AliCorrectionMatrix &)c).Copy(*this);
47}
48
49//____________________________________________________________________
50AliCorrectionMatrix::~AliCorrectionMatrix()
51{
52 //
53 // destructor
54 //
55
56 if (fhMeas)
57 {
58 delete fhMeas;
59 fhMeas = 0;
60 }
61
62 if (fhGene)
63 {
64 delete fhGene;
65 fhGene = 0;
66 }
67
68 if (fhCorr)
69 {
70 delete fhCorr;
71 fhCorr = 0;
72 }
73}
74
75//____________________________________________________________________
76AliCorrectionMatrix &AliCorrectionMatrix::operator=(const AliCorrectionMatrix &c)
77{
78 // assigment operator
79
61385583 80 if (this != &c)
bf21645b 81 ((AliCorrectionMatrix &) c).Copy(*this);
82
83 return *this;
84}
85
86//____________________________________________________________________
87void AliCorrectionMatrix::Copy(TObject& c) const
88{
89 // copy function
90
91 AliCorrectionMatrix& target = (AliCorrectionMatrix &) c;
92
93 if (fhMeas)
94 target.fhMeas = dynamic_cast<TH1*> (fhMeas->Clone());
95
96 if (fhGene)
97 target.fhGene = dynamic_cast<TH1*> (fhGene->Clone());
98
99 if (fhCorr)
100 target.fhCorr = dynamic_cast<TH1*> (fhCorr->Clone());
101}
102
103//________________________________________________________________________
104void AliCorrectionMatrix::SetAxisTitles(const Char_t* titleX, const Char_t* titleY, const Char_t* titleZ)
0ab29cfa 105{
bf21645b 106 //
107 // method for setting the axis titles of the histograms
108 //
109
110 fhMeas ->SetXTitle(titleX); fhMeas ->SetYTitle(titleY); fhMeas ->SetZTitle(titleZ);
111 fhGene ->SetXTitle(titleX); fhGene ->SetYTitle(titleY); fhGene ->SetZTitle(titleZ);
112 fhCorr ->SetXTitle(titleX); fhCorr ->SetYTitle(titleY); fhCorr ->SetZTitle(titleZ);
113}
114
115//____________________________________________________________________
116Long64_t AliCorrectionMatrix::Merge(TCollection* list)
117{
118 // Merge a list of AliCorrectionMatrix objects with this (needed for
119 // PROOF).
120 // Returns the number of merged objects (including this).
121
122 if (!list)
123 return 0;
124
125 if (list->IsEmpty())
126 return 1;
127
128 TIterator* iter = list->MakeIterator();
129 TObject* obj;
130
131 // collections of measured and generated histograms
132 TList* collectionMeas = new TList;
133 TList* collectionGene = new TList;
134
135 Int_t count = 0;
136 while ((obj = iter->Next())) {
137
138 AliCorrectionMatrix* entry = dynamic_cast<AliCorrectionMatrix*> (obj);
139 if (entry == 0)
140 continue;
141
142 collectionMeas->Add(entry->GetMeasuredHistogram());
143 collectionGene->Add(entry->GetGeneratedHistogram());
144
145 count++;
146 }
147 fhMeas->Merge(collectionMeas);
148 fhGene->Merge(collectionGene);
149
150 delete collectionMeas;
151 delete collectionGene;
152
153 return count+1;
154}
155
156//____________________________________________________________________
157void AliCorrectionMatrix::Divide()
158{
159 //
29771dc8 160 // divides generated by measured to get the correction
161 //
bf21645b 162
06e4b91b 163// if (!fhCorr) {
164// fhCorr = (TH1*)fhGene->Clone("correction");
165// fhCorr->SetTitle(Form("%s correction",GetTitle()));
166// fhCorr->Reset();
167// }
168
169 if (!fhMeas || !fhGene || !fhCorr) {
170 AliDebug(AliLog::kError, "measured or generated histograms not available");
bf21645b 171 return;
06e4b91b 172 }
bf21645b 173
174 fhCorr->Divide(fhGene, fhMeas, 1, 1, "B");
175
1afae8ff 176 Int_t emptyBins = 0;
177 for (Int_t x=1; x<=fhCorr->GetNbinsX(); ++x)
178 for (Int_t y=1; y<=fhCorr->GetNbinsY(); ++y)
179 for (Int_t z=1; z<=fhCorr->GetNbinsZ(); ++z)
180 if (fhCorr->GetBinContent(x, y, z) == 0)
181 ++emptyBins;
182
183 if (emptyBins > 0)
29771dc8 184 printf("INFO: In %s we have %d empty bins (of %d) in the correction map\n", GetTitle(), emptyBins, fhCorr->GetNbinsX() * fhCorr->GetNbinsY() * fhCorr->GetNbinsZ());
bf21645b 185}
186
187//____________________________________________________________________
29771dc8 188void AliCorrectionMatrix::Multiply()
189{
190 //
191 // multiplies measured with correction to get the generated
192 //
193
194 if (!fhMeas || !fhGene || !fhCorr)
195 return;
196
197 fhGene->Multiply(fhMeas, fhCorr, 1, 1, "B");
198}
199
200//____________________________________________________________________
2e88424e 201void AliCorrectionMatrix::Add(AliCorrectionMatrix* aMatrixToAdd, Float_t c) {
202 //
203 // adds the measured and generated of aMatrixToAdd to measured and generated of this
204 //
205 // NB: the correction will naturally stay the same
206
207 fhMeas->Add(aMatrixToAdd->GetMeasuredHistogram(), c);
208 fhGene->Add(aMatrixToAdd->GetGeneratedHistogram(), c);
209}
210
211
212//____________________________________________________________________
29771dc8 213Bool_t AliCorrectionMatrix::LoadHistograms(const Char_t* dir)
bf21645b 214{
215 //
216 // loads the histograms from a file
29771dc8 217 // if dir is empty a directory with the name of this object is taken (like in SaveHistogram)
bf21645b 218 //
1afae8ff 219
29771dc8 220 if (!dir)
221 dir = GetName();
1afae8ff 222
29771dc8 223 if (!gDirectory->cd(dir))
bf21645b 224 return kFALSE;
29771dc8 225
226 if (fhGene)
227 {
228 delete fhGene;
229 fhGene=0;
bf21645b 230 }
0ab29cfa 231
29771dc8 232 if (fhCorr)
233 {
234 delete fhCorr;
235 fhCorr=0;
236 }
0ab29cfa 237
29771dc8 238 if (fhMeas)
239 {
240 delete fhMeas;
241 fhMeas=0;
bf21645b 242 }
29771dc8 243
244 fhMeas = dynamic_cast<TH1*> (gDirectory->Get("measured"));
245 if (!fhMeas)
246 Info("LoadHistograms", "No measured hist available");
247
248 fhGene = dynamic_cast<TH1*> (gDirectory->Get("generated"));
249 if (!fhGene)
250 Info("LoadHistograms", "No generated hist available");
251
252 fhCorr = dynamic_cast<TH1*> (gDirectory->Get("correction"));
253
254 Bool_t success = kTRUE;
255 if (!fhCorr)
256 {
257 Info("LoadHistograms", "No correction hist available");
258 success = kFALSE;
259 }
260
261 gDirectory->cd("..");
262
263 return success;
bf21645b 264}
265
266//____________________________________________________________________
267void AliCorrectionMatrix::SaveHistograms()
268{
269 //
270 // saves the histograms
271 //
272
29771dc8 273 gDirectory->mkdir(GetName());
274 gDirectory->cd(GetName());
275
0bd1f8a0 276 if (fhMeas)
277 fhMeas ->Write();
278
279 if (fhGene)
280 fhGene ->Write();
bf21645b 281
282 if (fhCorr)
283 fhCorr->Write();
29771dc8 284
285 gDirectory->cd("..");
bf21645b 286}
287
288//____________________________________________________________________
29771dc8 289void AliCorrectionMatrix::DrawHistograms(const Char_t* canvasName)
bf21645b 290{
291 //
29771dc8 292 // draws all histograms on one TCanvas
293 // if canvasName is 0 the name of this object is taken
bf21645b 294 //
295
29771dc8 296 if (!canvasName)
297 canvasName = Form("%s_canvas", GetName());
298
299 TCanvas* canvas = new TCanvas(canvasName, GetTitle(), 1200, 400);
300 canvas->Divide(3, 1);
bf21645b 301
302 canvas->cd(1);
303 if (fhMeas)
304 fhMeas->Draw("COLZ");
29771dc8 305
bf21645b 306 canvas->cd(2);
307 if (fhGene)
29771dc8 308 {
309 // work around ROOT bug #22011
310 if (fhGene->GetEntries() == 0)
311 fhGene->SetEntries(1);
bf21645b 312 fhGene->Draw("COLZ");
29771dc8 313 }
bf21645b 314
315 canvas->cd(3);
316 if (fhCorr)
317 fhCorr->Draw("COLZ");
bf21645b 318}
0ab29cfa 319
320//____________________________________________________________________
321void AliCorrectionMatrix::ReduceInformation()
322{
323 // this function deletes the measured and generated histograms to reduce the amount of data
324 // in memory
325
326 if (fhMeas)
327 {
328 delete fhMeas;
329 fhMeas = 0;
330 }
331
332 if (fhGene)
333 {
334 delete fhGene;
335 fhGene = 0;
336 }
337}
ef2713c5 338
339//____________________________________________________________________
340void AliCorrectionMatrix::Reset(Option_t* option)
341{
342 // resets the histograms
343
344 if (fhGene)
345 fhGene->Reset(option);
346
347 if (fhMeas)
348 fhMeas->Reset(option);
349
350 if (fhCorr)
351 fhCorr->Reset(option);
352}
29771dc8 353
354//____________________________________________________________________
355void AliCorrectionMatrix::SetCorrectionToUnity()
356{
357 // sets the correction matrix to unity
358
359 if (!fhCorr)
360 return;
361
362 for (Int_t x=1; x<=fhCorr->GetNbinsX(); ++x)
363 for (Int_t y=1; y<=fhCorr->GetNbinsY(); ++y)
364 for (Int_t z=1; z<=fhCorr->GetNbinsZ(); ++z)
3dfa46a4 365 {
29771dc8 366 fhCorr->SetBinContent(x, y, z, 1);
3dfa46a4 367 fhCorr->SetBinError(x, y, z, 0);
368 }
29771dc8 369}
dd367a14 370
371//____________________________________________________________________
372void AliCorrectionMatrix::Scale(Double_t factor)
373{
374 // scales the generated and measured histogram with the given factor
375
376 Printf("Scaling histograms with %f", factor);
377
378 fhMeas->Scale(factor);
379 fhGene->Scale(factor);
380}