1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
16 // Collection class for histograms
17 // Stores either histograms or vectors of histograms
20 // Matus Kalisky <matus.kalisky@cern.ch>
26 #include <THnSparse.h>
34 #include "AliHFEcollection.h"
39 ClassImp(AliHFEcollection)
41 //___________________________________________________________________
42 AliHFEcollection::AliHFEcollection():
48 // default constructor
51 //___________________________________________________________________
52 AliHFEcollection::AliHFEcollection(const char* name, const char* title):
61 fList = new THashList();
64 fList->SetName(Form("list_%s", name));
67 //___________________________________________________________________
68 AliHFEcollection::AliHFEcollection(const AliHFEcollection &c) :
79 //___________________________________________________________________
80 AliHFEcollection &AliHFEcollection::operator=(const AliHFEcollection &ref)
83 // Assignment operator
91 //___________________________________________________________________
92 void AliHFEcollection::Copy(TObject &ref) const {
95 // Performs the copying of the object
98 AliHFEcollection &target = dynamic_cast<AliHFEcollection &>(ref);
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());
106 //___________________________________________________________________
107 AliHFEcollection::~AliHFEcollection(){
113 AliDebug(1, "DESTRUCTOR");
115 //___________________________________________________________________
116 Bool_t AliHFEcollection::CreateTH1F(const char* name, const char* title, Int_t nBin, Float_t nMin, Float_t nMax, Int_t logAxis){
119 // Creates a TH1F histogram for the collection
123 AliError("No TList pointer ! ");
127 fList->Add(new TH1F(name, title, nBin, nMin, nMax));
129 BinLogAxis(name, logAxis);
131 return CheckObject(name);
135 //___________________________________________________________________
136 Bool_t AliHFEcollection::CreateTH1Farray(const char* name, const char* title, Int_t nBin, const Double_t* xbins){
139 // Creates a TH1F histogram for the collection 2nd version
143 AliError("No TList pointer ! ");
147 fList->Add(new TH1F(name, title, nBin, xbins));
148 return CheckObject(name);
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){
156 // Creates a TH1F histogram for the collection 2nd version
160 AliError("No TList pointer ! ");
164 fList->Add(new TH2F(name, title, nBin, xbins, nBinY, nMinY, nMaxY));
165 return CheckObject(name);
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){
173 // Creates a TH2F histogram for the collection
177 AliError("No TList pointer ! ");
180 fList->Add(new TH2F(name, title, nBinX, nMinX, nMaxX, nBinY, nMinY, nMaxY));
182 BinLogAxis(name, logAxis);
184 return CheckObject(name);
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){
190 // create a 1 dimensional array of size [X]
194 AliError("No TList pointer ! ");
198 AliError("can not create array with negative or zero size ");
202 for(Int_t i=0; i<X; ++i){
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()));
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){
219 // create a 1 dimensinal array of TH2F histograms with size [X]
223 AliError("No TList pointer !");
227 AliError("can not create array with negative or zero size ");
231 for(Int_t i=0; i<X; ++i){
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()));
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){
248 // create a 2 dimensional array of histograms of size [X, Y]
252 AliError("No TList pointer ! ");
256 AliError("can not create array with negative or zero size ");
260 for(Int_t i=0; i<X; ++i){
261 for(Int_t j=0; j<Y; ++j){
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()));
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){
279 // Creates a TH2F histogram for the collection
283 AliError("No TList pointer ! ");
286 fList->Add(new TH3F(name, title, nBinX, nMinX, nMaxX, nBinY, nMinY, nMaxY, nBinZ, nMinZ, nMaxZ));
288 BinLogAxis(name, logAxis);
290 return CheckObject(name);
292 //___________________________________________________________________
293 Bool_t AliHFEcollection::CreateProfile(const char* name, const char* title, Int_t nbins, Double_t xmin, Double_t xmax){
296 // create a simple TProfile
300 AliError("No TList pointer ! ");
303 fList->Add(new TProfile(name, title, nbins, xmin, xmax));
304 return CheckObject(name);
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){
311 // create 'dim' dimensional THnSparse
315 AliError("No TList pointer ! ");
318 fList->Add(new THnSparseF(name, title, dim, nbins, xmin, xmax));
319 return CheckObject(name);
322 //___________________________________________________________________
323 Bool_t AliHFEcollection::CreateTHnSparseNoLimits(const char* name, const char* title, Int_t dim, const Int_t* nbins){
326 // create 'dim' dimensional THnSparse without limits
330 AliError("No TList pointer ! ");
333 fList->Add(new THnSparseF(name, title, dim, nbins));
334 return CheckObject(name);
337 //___________________________________________________________________
338 TObject* AliHFEcollection::Get(const char* name){
341 // Get histogram with the required name
345 if(!CheckObject(name)){
346 AliWarning(Form("Not possible to return pointer to the object '%s'\n", name));
350 return fList->FindObject(name);
353 //___________________________________________________________________
354 Bool_t AliHFEcollection::Fill(const char* name, Double_t v){
357 // fill function for one TH1 histograms
360 if(!CheckObject(name)){
361 AliError(Form("Not possible to fill the object '%s', the object does not exist\n", name));
365 TH1 *htmp = dynamic_cast<TH1F*>(fList->FindObject(name));
366 // chack the possible object types
375 //___________________________________________________________________
376 Bool_t AliHFEcollection::Fill(const char* name, Int_t v){
379 // fill function for one TH1 histograms for integer numbers
382 return Fill(name, v*1.0);
384 //___________________________________________________________________
385 Bool_t AliHFEcollection::Fill(const char* name, Int_t X, Double_t v){
388 // fill function for one dimension arrays of TH1
391 const char* n = Form("%s_[%d]", name, X);
396 Fill(o->GetName(), v);
399 //___________________________________________________________________
400 Bool_t AliHFEcollection::Fill(const char* name, Int_t X, Int_t Y, Double_t v){
403 // Fill function fir 2 dimensional TH1 arrays
406 const char* n = Form("%s_[%d][%d]", name, X, Y);
411 Fill(o->GetName(), v);
414 //___________________________________________________________________
415 Bool_t AliHFEcollection::Fill(const char* name, Int_t X, Double_t v1, Double_t v2){
418 // fill function for one dimension array of TH2
421 const char* n = Form("%s_[%d]", name, X);
426 Fill(o->GetName(), v1, v2);
430 //___________________________________________________________________
431 Bool_t AliHFEcollection::Fill(const char* name, Double_t v1, Double_t v2){
434 // fill function for TH2 objects
437 if(!CheckObject(name)){
438 AliError(Form("Not possible to fill the object '%s', the object does not exist\n", name));
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);
448 if(fList->FindObject(name)->InheritsFrom("TProfile")){
449 TProfile *pr = dynamic_cast<TProfile*>(fList->FindObject(name));
450 if(pr) pr->Fill(v1, v2);
457 //___________________________________________________________________
458 Bool_t AliHFEcollection::Fill(const char* name, Double_t v1, Double_t v2, Double_t v3){
461 // fill function for TH3 objects
464 if(!CheckObject(name)){
465 AliError(Form("Not possible to fill the object '%s', the object does not exist\n", name));
469 // chack the possible object types
470 TH3 *h3 = dynamic_cast<TH3F*>(fList->FindObject(name));
472 h3->Fill(v1, v2, v3);
479 //___________________________________________________________________
480 Bool_t AliHFEcollection::Fill(const char* name, Double_t* entry, Double_t weight){
482 // Fill a THnSparse object
485 if(!CheckObject(name)){
486 AliError(Form("Not possible to fill the object '%s', the object does not exist\n", name));
490 THnSparseF *htmp = dynamic_cast<THnSparseF*>(fList->FindObject(name));
492 htmp->Fill(entry, weight);
498 //___________________________________________________________________
499 Bool_t AliHFEcollection::CheckObject(const char* name){
502 // check wheter the creation of the histogram was succesfull
506 AliError("No TList pointer ! ");
510 if(!fList->FindObject(name)){
511 AliWarning(Form("Creating or Finding the object '%s' failed\n", name));
516 //___________________________________________________________________
517 Bool_t AliHFEcollection::Sumw2(const char* name){
519 // Set Sumw2 for the given object
521 if(!CheckObject(name)){
525 TObject *o = Get(name);
526 THnSparse *htmp = dynamic_cast<THnSparse*>(o);
532 //___________________________________________________________________
533 Bool_t AliHFEcollection::BinLogAxis(const char* name, Int_t dim){
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
541 if(!CheckObject(name)){
545 TObject *o = Get(name);
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);
555 axis = h2->GetXaxis();
558 axis = h2->GetYaxis();
561 AliError("Only dim = 0 or 1 possible for TH2F");
564 } else if(type.Contains("TH3")){
565 TH3 *h3 = dynamic_cast<TH3F*>(o);
568 axis = h3->GetXaxis();
571 axis = h3->GetYaxis();
574 axis = h3->GetZaxis();
577 AliError("Only dim = 0, 1 or 2 possible for TH3F");
580 } else if(type.Contains("THnSparse")){
581 THnSparse *hs = dynamic_cast<THnSparse*>(o);
582 if(hs) axis = hs->GetAxis(dim);
586 AliError(Form("Axis '%d' could not be identified in the object '%s'\n", dim, name));
590 Int_t bins = axis->GetNbins();
592 Double_t from = axis->GetXmin();
594 AliError(Form(" Log binning not possible for object '%s'because the '%d' axis starts from '%f\n'", name, dim, from));
597 Double_t to = axis->GetXmax();
598 Double_t *newBins = new Double_t[bins+1];
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];
604 axis->Set(bins, newBins);
611 //___________________________________________________________________
612 Long64_t AliHFEcollection::Merge(const TCollection *list){
615 // Merge the collections
625 TList templist; // Create temporary list containing all the lists to merge
627 AliHFEcollection *coll = dynamic_cast<AliHFEcollection *>(o);
629 templist.Add(coll->fList);
632 fList->Merge(&templist);
635 //____________________________________________________________________
636 void AliHFEcollection::Browse(TBrowser *b)
640 // Browse the content of the directory.
647 //Add objects that are only in memory
648 while ((obj = nextin())) {
649 b->Add(obj, obj->GetName());
653 //____________________________________________________________________
654 void AliHFEcollection::Print(Option_t *) const{
656 // Print content of the collection
658 TIter histIter(fList);
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());
667 printf("Number of histos in the collection: %d\n", nHistos);