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::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){
156 // Creates a TH2F histogram for the collection
160 AliError("No TList pointer ! ");
163 fList->Add(new TH2F(name, title, nBinX, nMinX, nMaxX, nBinY, nMinY, nMaxY));
165 BinLogAxis(name, logAxis);
167 return CheckObject(name);
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){
173 // create a 1 dimensional array of size [X]
177 AliError("No TList pointer ! ");
181 AliError("can not create array with negative or zero size ");
185 for(Int_t i=0; i<X; ++i){
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()));
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){
202 // create a 1 dimensinal array of TH2F histograms with size [X]
206 AliError("No TList pointer !");
210 AliError("can not create array with negative or zero size ");
214 for(Int_t i=0; i<X; ++i){
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()));
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){
231 // create a 2 dimensional array of histograms of size [X, Y]
235 AliError("No TList pointer ! ");
239 AliError("can not create array with negative or zero size ");
243 for(Int_t i=0; i<X; ++i){
244 for(Int_t j=0; j<Y; ++j){
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()));
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){
262 // Creates a TH2F histogram for the collection
266 AliError("No TList pointer ! ");
269 fList->Add(new TH3F(name, title, nBinX, nMinX, nMaxX, nBinY, nMinY, nMaxY, nBinZ, nMinZ, nMaxZ));
271 BinLogAxis(name, logAxis);
273 return CheckObject(name);
275 //___________________________________________________________________
276 Bool_t AliHFEcollection::CreateProfile(const char* name, const char* title, Int_t nbins, Double_t xmin, Double_t xmax){
279 // create a simple TProfile
283 AliError("No TList pointer ! ");
286 fList->Add(new TProfile(name, title, nbins, xmin, xmax));
287 return CheckObject(name);
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){
294 // create 'dim' dimensional THnSparse
298 AliError("No TList pointer ! ");
301 fList->Add(new THnSparseF(name, title, dim, nbins, xmin, xmax));
302 return CheckObject(name);
305 //___________________________________________________________________
306 TObject* AliHFEcollection::Get(const char* name){
309 // Get histogram with the required name
313 if(!CheckObject(name)){
314 AliWarning(Form("Not possible to return pointer to the object '%s'\n", name));
318 return fList->FindObject(name);
321 //___________________________________________________________________
322 Bool_t AliHFEcollection::Fill(const char* name, Double_t v){
325 // fill function for one TH1 histograms
328 if(!CheckObject(name)){
329 AliError(Form("Not possible to fill the object '%s', the object does not exist\n", name));
333 TH1 *htmp = dynamic_cast<TH1F*>(fList->FindObject(name));
334 // chack the possible object types
343 //___________________________________________________________________
344 Bool_t AliHFEcollection::Fill(const char* name, Int_t v){
347 // fill function for one TH1 histograms for integer numbers
350 return Fill(name, v*1.0);
352 //___________________________________________________________________
353 Bool_t AliHFEcollection::Fill(const char* name, Int_t X, Double_t v){
356 // fill function for one dimension arrays of TH1
359 const char* n = Form("%s_[%d]", name, X);
364 Fill(o->GetName(), v);
367 //___________________________________________________________________
368 Bool_t AliHFEcollection::Fill(const char* name, Int_t X, Int_t Y, Double_t v){
371 // Fill function fir 2 dimensional TH1 arrays
374 const char* n = Form("%s_[%d][%d]", name, X, Y);
379 Fill(o->GetName(), v);
382 //___________________________________________________________________
383 Bool_t AliHFEcollection::Fill(const char* name, Int_t X, Double_t v1, Double_t v2){
386 // fill function for one dimension array of TH2
389 const char* n = Form("%s_[%d]", name, X);
394 Fill(o->GetName(), v1, v2);
398 //___________________________________________________________________
399 Bool_t AliHFEcollection::Fill(const char* name, Double_t v1, Double_t v2){
402 // fill function for TH2 objects
405 if(!CheckObject(name)){
406 AliError(Form("Not possible to fill the object '%s', the object does not exist\n", name));
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);
416 if(fList->FindObject(name)->InheritsFrom("TProfile")){
417 TProfile *pr = dynamic_cast<TProfile*>(fList->FindObject(name));
418 if(pr) pr->Fill(v1, v2);
425 //___________________________________________________________________
426 Bool_t AliHFEcollection::Fill(const char* name, Double_t v1, Double_t v2, Double_t v3){
429 // fill function for TH3 objects
432 if(!CheckObject(name)){
433 AliError(Form("Not possible to fill the object '%s', the object does not exist\n", name));
437 // chack the possible object types
438 TH3 *h3 = dynamic_cast<TH3F*>(fList->FindObject(name));
440 h3->Fill(v1, v2, v3);
447 //___________________________________________________________________
448 Bool_t AliHFEcollection::Fill(const char* name, Double_t* entry, Double_t weight){
450 // Fill a THnSparse object
453 if(!CheckObject(name)){
454 AliError(Form("Not possible to fill the object '%s', the object does not exist\n", name));
458 THnSparseF *htmp = dynamic_cast<THnSparseF*>(fList->FindObject(name));
460 htmp->Fill(entry, weight);
466 //___________________________________________________________________
467 Bool_t AliHFEcollection::CheckObject(const char* name){
470 // check wheter the creation of the histogram was succesfull
474 AliError("No TList pointer ! ");
478 if(!fList->FindObject(name)){
479 AliWarning(Form("Creating or Finding the object '%s' failed\n", name));
484 //___________________________________________________________________
485 Bool_t AliHFEcollection::Sumw2(const char* name){
487 // Set Sumw2 for the given object
489 if(!CheckObject(name)){
493 TObject *o = Get(name);
494 THnSparse *htmp = dynamic_cast<THnSparse*>(o);
500 //___________________________________________________________________
501 Bool_t AliHFEcollection::BinLogAxis(const char* name, Int_t dim){
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
509 if(!CheckObject(name)){
513 TObject *o = Get(name);
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);
523 axis = h2->GetXaxis();
526 axis = h2->GetYaxis();
529 AliError("Only dim = 0 or 1 possible for TH2F");
532 } else if(type.Contains("TH3")){
533 TH3 *h3 = dynamic_cast<TH3F*>(o);
536 axis = h3->GetXaxis();
539 axis = h3->GetYaxis();
542 axis = h3->GetZaxis();
545 AliError("Only dim = 0, 1 or 2 possible for TH3F");
548 } else if(type.Contains("THnSparse")){
549 THnSparse *hs = dynamic_cast<THnSparse*>(o);
550 if(hs) axis = hs->GetAxis(dim);
554 AliError(Form("Axis '%d' could not be identified in the object '%s'\n", dim, name));
558 Int_t bins = axis->GetNbins();
560 Double_t from = axis->GetXmin();
562 AliError(Form(" Log binning not possible for object '%s'because the '%d' axis starts from '%f\n'", name, dim, from));
565 Double_t to = axis->GetXmax();
566 Double_t *newBins = new Double_t[bins+1];
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];
572 axis->Set(bins, newBins);
579 //___________________________________________________________________
580 Long64_t AliHFEcollection::Merge(const TCollection *list){
583 // Merge the collections
593 TList templist; // Create temporary list containing all the lists to merge
595 AliHFEcollection *coll = dynamic_cast<AliHFEcollection *>(o);
597 templist.Add(coll->fList);
600 fList->Merge(&templist);
603 //____________________________________________________________________
604 void AliHFEcollection::Browse(TBrowser *b)
608 // Browse the content of the directory.
615 //Add objects that are only in memory
616 while ((obj = nextin())) {
617 b->Add(obj, obj->GetName());
621 //____________________________________________________________________
622 void AliHFEcollection::Print(Option_t *) const{
624 // Print content of the collection
626 TIter histIter(fList);
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());
635 printf("Number of histos in the collection: %d\n", nHistos);