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