]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG3/hfe/AliHFEcollection.cxx
Changes for bug #70680: AliROOT Coverity DELETE_ARRAY checker fix
[u/mrichter/AliRoot.git] / PWG3 / hfe / AliHFEcollection.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 // Collection class for histograms
17 // Stores either histograms or vectors of histograms
18 // 
19 // Author:
20 // Matus Kalisky  <matus.kalisky@cern.ch>
21 //
22
23 //#include <iostream>
24
25 #include <TH1F.h>
26 #include <TH2F.h>
27 #include <THnSparse.h>
28 #include <TProfile.h>
29 #include <TString.h>
30 #include <TBrowser.h>
31 #include <TMath.h>
32
33 #include "AliLog.h"
34 #include "AliHFEcollection.h"
35
36 using namespace std;
37
38
39 ClassImp(AliHFEcollection)
40
41 //___________________________________________________________________
42 AliHFEcollection::AliHFEcollection():
43   TNamed()
44   , fList(0x0)
45 {
46
47   //
48   // default constructor
49   //
50
51   fList = new THashList();
52   if(!fList){
53     AliError("Initialization of the list failed");
54   }
55   else{
56     // list is owner of the objects. Once list is deleted, the objects
57     // it contains will be deleted too
58     //fList->SetOwner(kTRUE);
59   }
60   //Printf("%s:%d,%p",(char*)__FILE__,__LINE__,fInstance);
61   
62 }
63 //___________________________________________________________________
64 AliHFEcollection::AliHFEcollection(char* name, char* title):
65   TNamed(name, title)
66   , fList(0x0)
67 {
68  
69   //
70   // constructor
71   //
72  
73   fList = new THashList();
74   if(!fList){
75     AliError("Initialization of the list failed");
76   }
77   else{
78     // list is owner of the objects. Once list is deleted, the objects
79     // it contains will be deleted too
80     // fList->SetOwner(kTRUE);
81   }
82 }
83 //___________________________________________________________________
84 AliHFEcollection::AliHFEcollection(const AliHFEcollection &c) :
85   TNamed(c)
86   , fList(0x0)
87 {
88
89   //
90   // copy operator
91   // 
92   
93   c.Copy(*this);
94 }
95 //___________________________________________________________________
96 AliHFEcollection &AliHFEcollection::operator=(const AliHFEcollection &ref)
97 {
98   //
99   // Assignment operator
100   //
101   
102   if(this != &ref){
103     ref.Copy(*this);
104   }
105   return *this;
106 }
107 //___________________________________________________________________
108 void AliHFEcollection::Copy(TObject &ref) const {
109
110   //
111   // Performs the copying of the object
112   //
113
114   AliHFEcollection &target = dynamic_cast<AliHFEcollection &>(ref);
115
116   // Clone List Content
117   target.fList = new THashList();          
118   for(Int_t ien = 0; ien < fList->GetEntries(); ien++)
119     target.fList->Add(fList->At(ien)->Clone());
120 }
121 //___________________________________________________________________
122 AliHFEcollection::~AliHFEcollection(){
123
124   //
125   // Destructor
126   //
127   if(fList)
128     fList->Delete();
129   delete fList;
130   AliInfo("DESTRUCTOR");
131 }
132 //___________________________________________________________________
133 Bool_t AliHFEcollection::CreateTH1F(const char* name, const char* title, Int_t nBin, Float_t nMin, Float_t nMax){
134
135   //
136   // Creates a TH1F histogram for the collection
137   //
138
139   if(!fList){
140     AliError("No TList pointer ! ");
141     return kFALSE;
142   }
143   else{
144     fList->Add(new TH1F(name, title, nBin, nMin, nMax));
145     return CheckObject(name);
146   }
147 }
148 //___________________________________________________________________
149 Bool_t AliHFEcollection::CreateTH2F(const char* name, const char* title, Int_t nBinX, Float_t nMinX, Float_t nMaxX, Int_t nBinY, Float_t nMinY, Float_t nMaxY){
150
151   //
152   // Creates a TH2F histogram for the collection
153   //
154
155   if(!fList){
156     AliError("No TList pointer ! ");
157     return kFALSE;
158   }
159   fList->Add(new TH2F(name, title, nBinX, nMinX, nMaxX, nBinY, nMinY, nMaxY));
160   return CheckObject(name); 
161 }
162 //___________________________________________________________________
163 Bool_t AliHFEcollection::CreateTH1Fvector1(Int_t X, const char* name, const char* title, Int_t nBin, Float_t nMin, Float_t nMax){
164
165   //
166   // create a 1 dimensional array of size [X]
167   //
168
169   if(!fList){
170     AliError("No TList pointer ! ");
171     return kFALSE;
172   }
173   if(X <=0){
174     AliError("can not create array with negative or zero size ");
175     return kFALSE;
176   }
177   TString hname;
178   for(Int_t i=0; i<X; ++i){
179     hname = "";
180     hname.Append(Form("%s_[%d]", name, i));
181     //cout<<" -D: name: "<<name.str().c_str()<<endl;
182     //cout<<" -D: nBin: "<<_nBin<<" ,Min: "<<_nMin<<" , Max: "<<_nMax<<endl;
183     CreateTH1F(hname.Data(), title, nBin, nMin, nMax);
184     if(!CheckObject(hname.Data())){
185       AliError(Form("Not possible to create object: %s", hname.Data()));
186       return kFALSE;
187     }    
188   }
189   return kTRUE;  
190 }
191 //___________________________________________________________________
192 Bool_t AliHFEcollection::CreateTH2Fvector1(Int_t X, const char* name, const char* title, Int_t nBinX, Float_t nMinX, Float_t nMaxX, Int_t nBinY, Float_t nMinY, Float_t nMaxY){
193
194   //
195   // create a 1 dimensinal array of TH2F histograms with size [X]
196   //
197
198   if(!fList){
199     AliError("No TList pointer !");
200     return kFALSE;
201   }
202   if(X <=0){
203     AliError("can not create array with negative or zero size ");
204     return kFALSE;
205   }
206   TString hname;
207   for(Int_t i=0; i<X; ++i){
208     hname = "";
209     hname.Append(Form("%s_[%d]", name, i));
210     //cout<<" -D: name: "<<name<<endl;
211     //cout<<" -D: nBin: "<<_nBin<<" ,Min: "<<_nMin<<" , Max: "<<_nMax<<endl;
212     CreateTH2F(hname.Data(), title, nBinX, nMinX, nMaxX, nBinY, nMinY, nMaxY);
213     if(!CheckObject(hname.Data())){
214       AliError(Form("Not possible to create object: %s", hname.Data()));
215       return kFALSE;
216     }    
217   }
218   return kTRUE;  
219 }
220 //___________________________________________________________________
221 Bool_t AliHFEcollection::CreateTH1Fvector2(Int_t X, Int_t Y, const char* name, const char* title, Int_t nBin, Float_t nMin, Float_t nMax){
222
223   //
224   // create a 2 dimensional array of histograms of size [X, Y]
225   //
226
227   if(!fList){
228     AliError("No TList pointer ! ");
229     return kFALSE;
230   }
231   if(X <=0 || Y <=0){
232     AliError("can not create array with negative or zero size ");
233     return kFALSE;
234   }
235   TString hname;
236   for(Int_t i=0; i<X; ++i){
237     for(Int_t j=0; j<Y; ++j){
238       hname = "";
239       hname.Append(Form("%s_[%d][%d]", name, i, j));
240       //cout<<" -D: name: "<<name.str().c_str()<<endl;
241       //cout<<" -D: nBin: "<<_nBin<<" ,Min: "<<_nMin<<" , Max: "<<_nMax<<endl;
242       CreateTH1F(hname.Data(), title, nBin, nMin, nMax);
243       if(!CheckObject(hname.Data())){
244               AliError(Form("Not possible to create object: %s", hname.Data()));
245               return kFALSE;
246       }
247     }
248   }
249   return kTRUE;  
250 }
251 //___________________________________________________________________
252 Bool_t AliHFEcollection::CreateProfile(const char* name, const char* title, Int_t nbins, Double_t xmin, Double_t xmax){
253   
254   //
255   // create a simple TProfile
256   //
257
258   if(!fList){
259     AliError("No TList pointer ! ");
260     return kFALSE;
261   }
262   fList->Add(new TProfile(name, title, nbins, xmin, xmax));
263   return CheckObject(name);
264
265 }
266 //___________________________________________________________________
267 Bool_t AliHFEcollection::CreateTHnSparse(const char* name, const char* title, Int_t dim, Int_t* nbins, Double_t* xmin, Double_t* xmax){
268
269   //
270   // create 'dim' dimensional THnSparse
271   //
272
273   if(!fList){
274     AliError("No TList pointer ! ");
275     return kFALSE;
276   }
277   fList->Add(new THnSparseF(name, title, dim, nbins, xmin, xmax));
278   return CheckObject(name);
279
280 }
281 //___________________________________________________________________
282 TObject* AliHFEcollection::Get(const char* name){ 
283
284   //
285   // Get histogram with the required name
286   // 
287   
288
289   if(!CheckObject(name)){
290     AliWarning(Form("Not possible to return pointer to the object '%s'\n", name));
291     return 0;
292   }
293
294   return fList->FindObject(name);
295   
296 }
297 //___________________________________________________________________
298 Bool_t AliHFEcollection::Fill(const char* name, Double_t v){
299
300   //
301   // fill function for one TH1 histograms
302   //
303
304    if(!CheckObject(name)){
305     AliError(Form("Not possible to return pointer to the object '%s'\n", name));
306     return kFALSE;
307   }
308
309   // chack the possible object types
310   if(fList->FindObject(name)->InheritsFrom("TH1")){
311     (dynamic_cast<TH1F*>(fList->FindObject(name)))->Fill(v);
312     return kTRUE;
313   }
314   
315   return kFALSE;
316
317 }
318 //___________________________________________________________________
319 Bool_t AliHFEcollection::Fill(const char* name, Int_t X, Double_t v){
320
321   //
322   // fill function for one dimension arrays of TH1
323   //
324
325   const char* n = Form("%s_[%d]", name, X);
326   TObject *o = Get(n);
327   if(!o){
328     return kFALSE;
329   }
330   Fill(o->GetName(), v);
331   return kTRUE;
332 }
333 //___________________________________________________________________
334 Bool_t AliHFEcollection::Fill(const char* name, Int_t X, Int_t Y, Double_t v){
335
336   //
337   // Fill function fir 2 dimensional TH1 arrays
338   //
339   
340   const char* n = Form("%s_[%d][%d]", name, X, Y);
341   TObject *o = Get(n);
342   if(!o){
343     return kFALSE;
344   }
345   Fill(o->GetName(), v);
346   return kTRUE;
347 }
348 //___________________________________________________________________
349 Bool_t AliHFEcollection::Fill(const char* name, Int_t X, Double_t v1, Double_t v2){
350
351   //
352   // fill function for one dimension array of TH2
353   //
354
355   const char* n = Form("%s_[%d]", name, X);
356   TObject *o = Get(n);
357   if(!o){
358     return kFALSE;
359   }
360   Fill(o->GetName(), v1, v2);
361   
362   return kTRUE;
363 }
364 //___________________________________________________________________
365 Bool_t AliHFEcollection::Fill(const char* name, Double_t v1, Double_t v2){
366
367   //
368   // fill function for TH2 objects
369   //
370
371    if(!CheckObject(name)){
372     AliError(Form("Not possible to return pointer to the object '%s'\n", name));
373     return kFALSE;
374   }
375
376   // chack the possible object types
377   if(fList->FindObject(name)->InheritsFrom("TH2")){
378     (dynamic_cast<TH2F*>(fList->FindObject(name)))->Fill(v1, v2);
379     return kTRUE;
380   }  
381   if(fList->FindObject(name)->InheritsFrom("TProfile")){
382     (dynamic_cast<TProfile*>(fList->FindObject(name)))->Fill(v1, v2);
383     return kTRUE;
384   }  
385   
386   return kFALSE;
387   
388 }
389
390 //___________________________________________________________________
391 Bool_t AliHFEcollection::CheckObject(const char* name){
392
393   //
394   // check wheter the creation of the histogram was succesfull
395   //
396   
397   if(!fList){
398     AliError("No TList pointer ! ");
399     return kFALSE;
400   }
401   
402   if(!fList->FindObject(name)){
403     AliWarning(Form("Creating or Finding the object '%s' failed\n", name));
404     return kFALSE;
405   }
406   return kTRUE;
407 }
408 //___________________________________________________________________
409 Bool_t AliHFEcollection::Sumw2(const char* name){
410   //
411   // Set Sumw2 for the given object
412   //
413   if(!CheckObject(name)){
414     return kFALSE;
415   }
416
417   TObject *o = Get(name);
418   if(o->InheritsFrom("THnSparse")){
419     (dynamic_cast<THnSparse*>(o))->Sumw2();
420   }
421   return kTRUE;
422 }
423 //___________________________________________________________________
424 Bool_t AliHFEcollection::BinLogAxis(const char* name, Int_t dim){
425
426   // 
427   // converts the axis (defined by the dimension) of THx or THnSparse
428   // object to Log scale. Number of bins and bin min and bin max are preserved
429   //
430
431
432   if(!CheckObject(name)){
433     return kFALSE;
434   }
435
436   TObject *o = Get(name);
437   TAxis *axis = 0x0;
438   if(o->InheritsFrom("TH1")){
439     axis = (dynamic_cast<TH1F*>(o))->GetXaxis();
440   }
441   if(o->InheritsFrom("TH2")){
442     if(0 == dim){
443       axis = (dynamic_cast<TH2F*>(o))->GetXaxis();
444     }
445     else if(1 == dim){
446       axis = (dynamic_cast<TH2F*>(o))->GetYaxis();
447     }
448      else{
449        AliError("Only dim = 0 or 1 possible for TH2F");
450      }
451   }
452   if(o->InheritsFrom("THnSparse")){
453     axis = (dynamic_cast<THnSparse*>(o))->GetAxis(dim);
454   }
455   
456   if(!axis){
457     AliError(Form("Axis '%d' could not be identified in the object '%s'\n", dim, name));
458     return kFALSE;
459   }
460   
461   Int_t bins = axis->GetNbins();
462
463   Double_t from = axis->GetXmin();
464   Double_t to = axis->GetXmax();
465   Double_t *newBins = new Double_t[bins+1];
466   newBins[0] = from;
467   Double_t factor = TMath::Power(to/from, 1./bins);
468   for(Int_t i=1; i<=bins; ++i){
469     newBins[i] = factor * newBins[i-1];
470   }
471   axis->Set(bins, newBins);
472   delete [] newBins;
473
474   return kTRUE;
475
476
477 }
478 //___________________________________________________________________
479 Long64_t AliHFEcollection::Merge(TCollection *list){
480
481   //
482   // Merge the collections
483   //
484   if(!list)
485     return 0;
486   if(list->IsEmpty())
487     return 1;
488   
489   TIterator *iter = list->MakeIterator();
490   TObject *o = NULL;
491   Int_t index = 0;
492   while((o = iter->Next())){
493     AliHFEcollection *coll = dynamic_cast<AliHFEcollection *>(o);
494     if(!coll) continue; 
495     TList templist;       // Create temporary list containing all the lists to merge
496     templist.Add(coll->fList);
497     fList->Merge(&templist);
498     index++;
499   }
500   delete iter;
501   return index + 1;
502 }
503 //____________________________________________________________________
504 void AliHFEcollection::Browse(TBrowser *b)
505 {
506
507   //
508   // Browse the content of the directory.
509   //
510
511    if (b) {
512       TObject *obj = 0;
513       TIter nextin(fList);
514
515       //Add objects that are only in memory
516       while ((obj = nextin())) {
517          b->Add(obj, obj->GetName());
518       }
519    }
520 }