]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGHF/hfe/AliHFEcollection.cxx
Bug fix
[u/mrichter/AliRoot.git] / PWGHF / 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 <TH1F.h>
24 #include <TH2F.h>
25 #include <TH3F.h>
26 #include <THnSparse.h>
27 #include <TProfile.h>
28 #include <TString.h>
29 #include <TBrowser.h>
30 #include <TMath.h>
31
32 #include "AliLog.h"
33
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   target.fList->SetOwner();
103   for(Int_t ien = 0; ien < fList->GetEntries(); ien++)
104     target.fList->Add(fList->At(ien)->Clone());
105 }
106 //___________________________________________________________________
107 AliHFEcollection::~AliHFEcollection(){
108
109   //
110   // Destructor
111   //
112   delete fList;
113   AliDebug(1, "DESTRUCTOR");
114 }
115 //___________________________________________________________________
116 Bool_t AliHFEcollection::CreateTH1F(const char* name, const char* title, Int_t nBin, Float_t nMin, Float_t nMax, Int_t logAxis){
117
118   //
119   // Creates a TH1F histogram for the collection
120   //
121
122   if(!fList){
123     AliError("No TList pointer ! ");
124     return kFALSE;
125   }
126   else{
127     fList->Add(new TH1F(name, title, nBin, nMin, nMax));
128     if(logAxis >= 0){
129       BinLogAxis(name, logAxis);
130     }
131     return CheckObject(name);
132   }
133 }
134
135 //___________________________________________________________________
136 Bool_t AliHFEcollection::CreateTH1Farray(const char* name, const char* title, Int_t nBin, const Double_t* xbins){
137
138   //
139   // Creates a TH1F histogram for the collection 2nd version 
140   //
141
142   if(!fList){
143     AliError("No TList pointer ! ");
144     return kFALSE;
145   }
146   else{
147     fList->Add(new TH1F(name, title, nBin, xbins));
148     return CheckObject(name);
149   }
150 }
151
152 //___________________________________________________________________
153 Bool_t AliHFEcollection::CreateTH2Farray(const char* name, const char* title, Int_t nBin, const Double_t* xbins, Int_t nBinY, Float_t nMinY, Float_t nMaxY){
154
155   //
156   // Creates a TH1F histogram for the collection 2nd version 
157   //
158
159   if(!fList){
160     AliError("No TList pointer ! ");
161     return kFALSE;
162   }
163   else{
164     fList->Add(new TH2F(name, title, nBin, xbins, nBinY, nMinY, nMaxY));
165     return CheckObject(name);
166   }
167 }
168
169 //___________________________________________________________________
170 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){
171
172   //
173   // Creates a TH2F histogram for the collection
174   //
175
176   if(!fList){
177     AliError("No TList pointer ! ");
178     return kFALSE;
179   }
180   fList->Add(new TH2F(name, title, nBinX, nMinX, nMaxX, nBinY, nMinY, nMaxY));
181   if(logAxis >= 0){
182     BinLogAxis(name, logAxis);
183   }
184   return CheckObject(name); 
185 }
186 //___________________________________________________________________
187 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){
188
189   //
190   // create a 1 dimensional array of size [X]
191   //
192
193   if(!fList){
194     AliError("No TList pointer ! ");
195     return kFALSE;
196   }
197   if(X <=0){
198     AliError("can not create array with negative or zero size ");
199     return kFALSE;
200   }
201   TString hname;
202   for(Int_t i=0; i<X; ++i){
203     hname = "";
204     hname.Append(Form("%s_[%d]", name, i));
205     //cout<<" -D: name: "<<name.str().c_str()<<endl;
206     //cout<<" -D: nBin: "<<_nBin<<" ,Min: "<<_nMin<<" , Max: "<<_nMax<<endl;
207     CreateTH1F(hname.Data(), title, nBin, nMin, nMax, logAxis);
208     if(!CheckObject(hname.Data())){
209       AliError(Form("Not possible to create object: %s", hname.Data()));
210       return kFALSE;
211     }    
212   }
213   return kTRUE;  
214 }
215 //___________________________________________________________________
216 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){
217
218   //
219   // create a 1 dimensinal array of TH2F histograms with size [X]
220   //
221
222   if(!fList){
223     AliError("No TList pointer !");
224     return kFALSE;
225   }
226   if(X <=0){
227     AliError("can not create array with negative or zero size ");
228     return kFALSE;
229   }
230   TString hname;
231   for(Int_t i=0; i<X; ++i){
232     hname = "";
233     hname.Append(Form("%s_[%d]", name, i));
234     //cout<<" -D: name: "<<name<<endl;
235     //cout<<" -D: nBin: "<<_nBin<<" ,Min: "<<_nMin<<" , Max: "<<_nMax<<endl;
236     CreateTH2F(hname.Data(), title, nBinX, nMinX, nMaxX, nBinY, nMinY, nMaxY, logAxis);
237     if(!CheckObject(hname.Data())){
238       AliError(Form("Not possible to create object: %s", hname.Data()));
239       return kFALSE;
240     }    
241   }
242   return kTRUE;  
243 }
244 //___________________________________________________________________
245 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){
246
247   //
248   // create a 2 dimensional array of histograms of size [X, Y]
249   //
250
251   if(!fList){
252     AliError("No TList pointer ! ");
253     return kFALSE;
254   }
255   if(X <=0 || Y <=0){
256     AliError("can not create array with negative or zero size ");
257     return kFALSE;
258   }
259   TString hname;
260   for(Int_t i=0; i<X; ++i){
261     for(Int_t j=0; j<Y; ++j){
262       hname = "";
263       hname.Append(Form("%s_[%d][%d]", name, i, j));
264       //cout<<" -D: name: "<<name.str().c_str()<<endl;
265       //cout<<" -D: nBin: "<<_nBin<<" ,Min: "<<_nMin<<" , Max: "<<_nMax<<endl;
266       CreateTH1F(hname.Data(), title, nBin, nMin, nMax, logAxis);
267       if(!CheckObject(hname.Data())){
268               AliError(Form("Not possible to create object: %s", hname.Data()));
269               return kFALSE;
270       }
271     }
272   }
273   return kTRUE;  
274 }
275 //___________________________________________________________________
276 Bool_t AliHFEcollection::CreateTH3F(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 nBinZ, Float_t nMinZ, Float_t nMaxZ, Int_t logAxis){
277
278   //
279   // Creates a TH2F histogram for the collection
280   //
281
282   if(!fList){
283     AliError("No TList pointer ! ");
284     return kFALSE;
285   }
286   fList->Add(new TH3F(name, title, nBinX, nMinX, nMaxX, nBinY, nMinY, nMaxY, nBinZ, nMinZ, nMaxZ));
287   if(logAxis >= 0){
288     BinLogAxis(name, logAxis);
289   }
290   return CheckObject(name); 
291 }
292 //___________________________________________________________________
293 Bool_t AliHFEcollection::CreateProfile(const char* name, const char* title, Int_t nbins, Double_t xmin, Double_t xmax){
294   
295   //
296   // create a simple TProfile
297   //
298
299   if(!fList){
300     AliError("No TList pointer ! ");
301     return kFALSE;
302   }
303   fList->Add(new TProfile(name, title, nbins, xmin, xmax));
304   return CheckObject(name);
305
306 }
307 //___________________________________________________________________
308 Bool_t AliHFEcollection::CreateTHnSparse(const char* name, const char* title, Int_t dim, const Int_t* nbins, const Double_t* xmin, const Double_t* xmax){
309
310   //
311   // create 'dim' dimensional THnSparse
312   //
313
314   if(!fList){
315     AliError("No TList pointer ! ");
316     return kFALSE;
317   }
318   fList->Add(new THnSparseF(name, title, dim, nbins, xmin, xmax));
319   return CheckObject(name);
320
321 }
322 //___________________________________________________________________
323 Bool_t AliHFEcollection::CreateTHnSparseNoLimits(const char* name, const char* title, Int_t dim, const Int_t* nbins){
324
325   //
326   // create 'dim' dimensional THnSparse without limits
327   //
328
329   if(!fList){
330     AliError("No TList pointer ! ");
331     return kFALSE;
332   }
333   fList->Add(new THnSparseF(name, title, dim, nbins));
334   return CheckObject(name);
335
336 }
337 //___________________________________________________________________
338 TObject* AliHFEcollection::Get(const char* name){ 
339
340   //
341   // Get histogram with the required name
342   // 
343   
344
345   if(!CheckObject(name)){
346     AliWarning(Form("Not possible to return pointer to the object '%s'\n", name));
347     return 0;
348   }
349
350   return fList->FindObject(name);
351   
352 }
353 //___________________________________________________________________
354 Bool_t AliHFEcollection::Fill(const char* name, Double_t v){
355
356   //
357   // fill function for one TH1 histograms
358   //
359
360   if(!CheckObject(name)){
361     AliError(Form("Not possible to fill the object '%s', the object does not exist\n", name));
362     return kFALSE;
363   }
364
365   TH1 *htmp = dynamic_cast<TH1F*>(fList->FindObject(name));
366   // chack the possible object types
367   if(htmp){
368     htmp->Fill(v);
369     return kTRUE;
370   }
371   
372   return kFALSE;
373
374 }
375 //___________________________________________________________________
376 Bool_t AliHFEcollection::Fill(const char* name, Int_t v){
377
378   //
379   // fill function for one TH1 histograms for integer numbers
380   //
381
382   return Fill(name, v*1.0);
383 }
384 //___________________________________________________________________
385 Bool_t AliHFEcollection::Fill(const char* name, Int_t X, Double_t v){
386
387   //
388   // fill function for one dimension arrays of TH1
389   //
390
391   const char* n = Form("%s_[%d]", name, X);
392   TObject *o = Get(n);
393   if(!o){
394     return kFALSE;
395   }
396   Fill(o->GetName(), v);
397   return kTRUE;
398 }
399 //___________________________________________________________________
400 Bool_t AliHFEcollection::Fill(const char* name, Int_t X, Int_t Y, Double_t v){
401
402   //
403   // Fill function fir 2 dimensional TH1 arrays
404   //
405   
406   const char* n = Form("%s_[%d][%d]", name, X, Y);
407   TObject *o = Get(n);
408   if(!o){
409     return kFALSE;
410   }
411   Fill(o->GetName(), v);
412   return kTRUE;
413 }
414 //___________________________________________________________________
415 Bool_t AliHFEcollection::Fill(const char* name, Int_t X, Double_t v1, Double_t v2){
416
417   //
418   // fill function for one dimension array of TH2
419   //
420
421   const char* n = Form("%s_[%d]", name, X);
422   TObject *o = Get(n);
423   if(!o){
424     return kFALSE;
425   }
426   Fill(o->GetName(), v1, v2);
427   
428   return kTRUE;
429 }
430 //___________________________________________________________________
431 Bool_t AliHFEcollection::Fill(const char* name, Double_t v1, Double_t v2){
432
433   //
434   // fill function for TH2 objects
435   //
436
437   if(!CheckObject(name)){
438     AliError(Form("Not possible to fill the object '%s', the object does not exist\n", name));
439     return kFALSE;
440   }
441
442   // chack the possible object types
443   if(fList->FindObject(name)->InheritsFrom("TH2")){
444     TH2 *h2 = dynamic_cast<TH2F*>(fList->FindObject(name));
445     if(h2) h2->Fill(v1, v2);
446     return kTRUE;
447   }  
448   if(fList->FindObject(name)->InheritsFrom("TProfile")){
449     TProfile *pr = dynamic_cast<TProfile*>(fList->FindObject(name));
450     if(pr) pr->Fill(v1, v2);
451     return kTRUE;
452   }  
453   
454   return kFALSE;
455   
456 }
457 //___________________________________________________________________
458 Bool_t AliHFEcollection::Fill(const char* name, Double_t v1, Double_t v2, Double_t v3){
459
460   //
461   // fill function for TH3 objects
462   //
463
464   if(!CheckObject(name)){
465     AliError(Form("Not possible to fill the object '%s', the object does not exist\n", name));
466     return kFALSE;
467   }
468
469   // chack the possible object types
470   TH3 *h3 = dynamic_cast<TH3F*>(fList->FindObject(name));
471   if(h3){ 
472     h3->Fill(v1, v2, v3);
473     return kTRUE;
474   }
475
476   return kFALSE;
477   
478 }
479 //___________________________________________________________________
480 Bool_t AliHFEcollection::Fill(const char* name, Double_t* entry, Double_t weight){
481   //
482   // Fill a THnSparse object
483   //
484
485   if(!CheckObject(name)){
486     AliError(Form("Not possible to fill the object '%s', the object does not exist\n", name));
487     return kFALSE;
488   }
489   
490   THnSparseF *htmp = dynamic_cast<THnSparseF*>(fList->FindObject(name));
491    if(htmp){
492      htmp->Fill(entry, weight);
493      return kTRUE;
494    }
495    return kFALSE;
496
497 }
498 //___________________________________________________________________
499 Bool_t AliHFEcollection::CheckObject(const char* name){
500
501   //
502   // check wheter the creation of the histogram was succesfull
503   //
504   
505   if(!fList){
506     AliError("No TList pointer ! ");
507     return kFALSE;
508   }
509   
510   if(!fList->FindObject(name)){
511     AliWarning(Form("Creating or Finding the object '%s' failed\n", name));
512     return kFALSE;
513   }
514   return kTRUE;
515 }
516 //___________________________________________________________________
517 Bool_t AliHFEcollection::Sumw2(const char* name){
518   //
519   // Set Sumw2 for the given object
520   //
521   if(!CheckObject(name)){
522     return kFALSE;
523   }
524
525   TObject *o = Get(name);
526   THnSparse *htmp = dynamic_cast<THnSparse*>(o);
527   if(htmp){
528     htmp->Sumw2();
529   }
530   return kTRUE;
531 }
532 //___________________________________________________________________
533 Bool_t AliHFEcollection::BinLogAxis(const char* name, Int_t dim){
534
535   // 
536   // converts the axis (defined by the dimension) of THx or THnSparse
537   // object to Log scale. Number of bins and bin min and bin max are preserved
538   //
539
540
541   if(!CheckObject(name)){
542     return kFALSE;
543   }
544
545   TObject *o = Get(name);
546   TAxis *axis = NULL;
547   TString type(o->IsA()->GetName()); 
548   if(type.Contains("TH1")){ // 1D histogram
549     TH1 *h1 = dynamic_cast<TH1F*>(o);
550     if(h1) axis = h1->GetXaxis();
551   } else if(type.Contains("TH2")){
552     TH2 *h2 = dynamic_cast<TH2F*>(o);
553     if(h2){
554       if(0 == dim){
555         axis = h2->GetXaxis();
556       }
557       else if(1 == dim){
558         axis = h2->GetYaxis();
559       }
560       else{
561          AliError("Only dim = 0 or 1 possible for TH2F");
562       }
563     }
564   } else if(type.Contains("TH3")){
565     TH3 *h3 = dynamic_cast<TH3F*>(o);
566     if(h3){
567       if(0 == dim){
568         axis = h3->GetXaxis();
569       }
570       else if(1 == dim){
571         axis = h3->GetYaxis();
572       }
573       else if(2 == dim){
574         axis = h3->GetZaxis();
575       }
576       else{
577          AliError("Only dim = 0, 1 or 2 possible for TH3F");
578       }
579     }
580   } else if(type.Contains("THnSparse")){
581     THnSparse *hs = dynamic_cast<THnSparse*>(o);
582     if(hs) axis = hs->GetAxis(dim);
583   }
584
585   if(!axis){
586     AliError(Form("Axis '%d' could not be identified in the object '%s'\n", dim, name));
587     return kFALSE;
588   }
589   
590   Int_t bins = axis->GetNbins();
591
592   Double_t from = axis->GetXmin();
593   if(from <= 0){
594     AliError(Form(" Log binning not possible for object '%s'because the '%d' axis starts from '%f\n'", name, dim, from));
595     return kFALSE;
596   }
597   Double_t to = axis->GetXmax();
598   Double_t *newBins = new Double_t[bins+1];
599   newBins[0] = from;
600   Double_t factor = TMath::Power(to/from, 1./bins);
601   for(Int_t i=1; i<=bins; ++i){
602     newBins[i] = factor * newBins[i-1];
603   }
604   axis->Set(bins, newBins);
605   delete[] newBins;
606
607   return kTRUE;
608
609
610 }
611 //___________________________________________________________________
612 Long64_t AliHFEcollection::Merge(const TCollection *list){
613
614   //
615   // Merge the collections
616   //
617   if(!list)
618     return 0;
619   if(list->IsEmpty())
620     return 1;
621   
622   TIter it(list);
623   TObject *o = NULL;
624   Int_t index = 0;
625   TList templist;       // Create temporary list containing all the lists to merge
626   while((o = it())){
627     AliHFEcollection *coll = dynamic_cast<AliHFEcollection *>(o);
628     if(!coll) continue; 
629     templist.Add(coll->fList);
630     index++;
631   }
632   fList->Merge(&templist);
633   return index + 1;
634 }
635 //____________________________________________________________________
636 void AliHFEcollection::Browse(TBrowser *b)
637 {
638
639   //
640   // Browse the content of the directory.
641   //
642
643    if (b) {
644       TObject *obj = 0;
645       TIter nextin(fList);
646
647       //Add objects that are only in memory
648       while ((obj = nextin())) {
649          b->Add(obj, obj->GetName());
650       }
651    }
652 }
653 //____________________________________________________________________
654 void AliHFEcollection::Print(Option_t *) const{
655   //
656   // Print content of the collection
657   //
658   TIter histIter(fList);
659   TObject *o = NULL;
660   Int_t nHistos = 0;
661   printf("Collection %s\n", GetName());
662   printf("Content of the collection:\n=========================================\n");
663   while((o = histIter())){
664     printf("Histo %s, Type %s\n", o->GetName(), o->IsA()->GetName());
665     nHistos++;
666   }
667   printf("Number of histos in the collection: %d\n", nHistos);
668   printf("\n");
669 }
670