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