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 **************************************************************************/
19 // Collection class for histograms
20 // Stores either histograms or vectors of histograms
23 // Matus Kalisky <matus.kalisky@cern.ch>
30 #include <THnSparse.h>
37 #include "AliHFEcollection.h"
42 ClassImp(AliHFEcollection)
44 //___________________________________________________________________
45 AliHFEcollection::AliHFEcollection():
51 // default constructor
54 //___________________________________________________________________
55 AliHFEcollection::AliHFEcollection(const char* name, const char* title):
64 fList = new THashList();
67 fList->SetName(Form("list_%s", name));
70 //___________________________________________________________________
71 AliHFEcollection::AliHFEcollection(const AliHFEcollection &c) :
82 //___________________________________________________________________
83 AliHFEcollection &AliHFEcollection::operator=(const AliHFEcollection &ref)
86 // Assignment operator
94 //___________________________________________________________________
95 void AliHFEcollection::Copy(TObject &ref) const {
98 // Performs the copying of the object
101 AliHFEcollection &target = dynamic_cast<AliHFEcollection &>(ref);
103 // Clone List Content
104 target.fList = new THashList();
105 for(Int_t ien = 0; ien < fList->GetEntries(); ien++)
106 target.fList->Add(fList->At(ien)->Clone());
108 //___________________________________________________________________
109 AliHFEcollection::~AliHFEcollection(){
115 AliDebug(1, "DESTRUCTOR");
117 //___________________________________________________________________
118 Bool_t AliHFEcollection::CreateTH1F(const char* name, const char* title, Int_t nBin, Float_t nMin, Float_t nMax, Int_t logAxis){
121 // Creates a TH1F histogram for the collection
125 AliError("No TList pointer ! ");
129 fList->Add(new TH1F(name, title, nBin, nMin, nMax));
131 BinLogAxis(name, logAxis);
133 return CheckObject(name);
136 //___________________________________________________________________
137 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){
140 // Creates a TH2F histogram for the collection
144 AliError("No TList pointer ! ");
147 fList->Add(new TH2F(name, title, nBinX, nMinX, nMaxX, nBinY, nMinY, nMaxY));
149 BinLogAxis(name, logAxis);
151 return CheckObject(name);
153 //___________________________________________________________________
154 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){
157 // create a 1 dimensional array of size [X]
161 AliError("No TList pointer ! ");
165 AliError("can not create array with negative or zero size ");
169 for(Int_t i=0; i<X; ++i){
171 hname.Append(Form("%s_[%d]", name, i));
172 //cout<<" -D: name: "<<name.str().c_str()<<endl;
173 //cout<<" -D: nBin: "<<_nBin<<" ,Min: "<<_nMin<<" , Max: "<<_nMax<<endl;
174 CreateTH1F(hname.Data(), title, nBin, nMin, nMax, logAxis);
175 if(!CheckObject(hname.Data())){
176 AliError(Form("Not possible to create object: %s", hname.Data()));
182 //___________________________________________________________________
183 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){
186 // create a 1 dimensinal array of TH2F histograms with size [X]
190 AliError("No TList pointer !");
194 AliError("can not create array with negative or zero size ");
198 for(Int_t i=0; i<X; ++i){
200 hname.Append(Form("%s_[%d]", name, i));
201 //cout<<" -D: name: "<<name<<endl;
202 //cout<<" -D: nBin: "<<_nBin<<" ,Min: "<<_nMin<<" , Max: "<<_nMax<<endl;
203 CreateTH2F(hname.Data(), title, nBinX, nMinX, nMaxX, nBinY, nMinY, nMaxY, logAxis);
204 if(!CheckObject(hname.Data())){
205 AliError(Form("Not possible to create object: %s", hname.Data()));
211 //___________________________________________________________________
212 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){
215 // create a 2 dimensional array of histograms of size [X, Y]
219 AliError("No TList pointer ! ");
223 AliError("can not create array with negative or zero size ");
227 for(Int_t i=0; i<X; ++i){
228 for(Int_t j=0; j<Y; ++j){
230 hname.Append(Form("%s_[%d][%d]", name, i, j));
231 //cout<<" -D: name: "<<name.str().c_str()<<endl;
232 //cout<<" -D: nBin: "<<_nBin<<" ,Min: "<<_nMin<<" , Max: "<<_nMax<<endl;
233 CreateTH1F(hname.Data(), title, nBin, nMin, nMax, logAxis);
234 if(!CheckObject(hname.Data())){
235 AliError(Form("Not possible to create object: %s", hname.Data()));
242 //___________________________________________________________________
243 Bool_t AliHFEcollection::CreateProfile(const char* name, const char* title, Int_t nbins, Double_t xmin, Double_t xmax){
246 // create a simple TProfile
250 AliError("No TList pointer ! ");
253 fList->Add(new TProfile(name, title, nbins, xmin, xmax));
254 return CheckObject(name);
257 //___________________________________________________________________
258 Bool_t AliHFEcollection::CreateTHnSparse(const char* name, const char* title, Int_t dim, Int_t* nbins, Double_t* xmin, Double_t* xmax){
261 // create 'dim' dimensional THnSparse
265 AliError("No TList pointer ! ");
268 fList->Add(new THnSparseF(name, title, dim, nbins, xmin, xmax));
269 return CheckObject(name);
272 //___________________________________________________________________
273 TObject* AliHFEcollection::Get(const char* name){
276 // Get histogram with the required name
280 if(!CheckObject(name)){
281 AliWarning(Form("Not possible to return pointer to the object '%s'\n", name));
285 return fList->FindObject(name);
288 //___________________________________________________________________
289 Bool_t AliHFEcollection::Fill(const char* name, Double_t v){
292 // fill function for one TH1 histograms
295 if(!CheckObject(name)){
296 AliError(Form("Not possible to fill the object '%s', the object does not exist\n", name));
300 TH1 *htmp = dynamic_cast<TH1F*>(fList->FindObject(name));
301 // chack the possible object types
310 //___________________________________________________________________
311 Bool_t AliHFEcollection::Fill(const char* name, Int_t v){
314 // fill function for one TH1 histograms for integer numbers
317 return Fill(name, v*1.0);
319 //___________________________________________________________________
320 Bool_t AliHFEcollection::Fill(const char* name, Int_t X, Double_t v){
323 // fill function for one dimension arrays of TH1
326 const char* n = Form("%s_[%d]", name, X);
331 Fill(o->GetName(), v);
334 //___________________________________________________________________
335 Bool_t AliHFEcollection::Fill(const char* name, Int_t X, Int_t Y, Double_t v){
338 // Fill function fir 2 dimensional TH1 arrays
341 const char* n = Form("%s_[%d][%d]", name, X, Y);
346 Fill(o->GetName(), v);
349 //___________________________________________________________________
350 Bool_t AliHFEcollection::Fill(const char* name, Int_t X, Double_t v1, Double_t v2){
353 // fill function for one dimension array of TH2
356 const char* n = Form("%s_[%d]", name, X);
361 Fill(o->GetName(), v1, v2);
365 //___________________________________________________________________
366 Bool_t AliHFEcollection::Fill(const char* name, Double_t v1, Double_t v2){
369 // fill function for TH2 objects
372 if(!CheckObject(name)){
373 AliError(Form("Not possible to fill the object '%s', the object does not exist\n", name));
377 // chack the possible object types
378 if(fList->FindObject(name)->InheritsFrom("TH2")){
379 TH2 *h2 = dynamic_cast<TH2F*>(fList->FindObject(name));
380 if(h2) h2->Fill(v1, v2);
383 if(fList->FindObject(name)->InheritsFrom("TProfile")){
384 TProfile *pr = dynamic_cast<TProfile*>(fList->FindObject(name));
385 if(pr) pr->Fill(v1, v2);
392 //___________________________________________________________________
393 Bool_t AliHFEcollection::Fill(const char* name, Double_t* entry, Double_t weight){
395 // Fill a THnSparse object
398 if(!CheckObject(name)){
399 AliError(Form("Not possible to fill the object '%s', the object does not exist\n", name));
403 THnSparseF *htmp = dynamic_cast<THnSparseF*>(fList->FindObject(name));
405 htmp->Fill(entry, weight);
411 //___________________________________________________________________
412 Bool_t AliHFEcollection::CheckObject(const char* name){
415 // check wheter the creation of the histogram was succesfull
419 AliError("No TList pointer ! ");
423 if(!fList->FindObject(name)){
424 AliWarning(Form("Creating or Finding the object '%s' failed\n", name));
429 //___________________________________________________________________
430 Bool_t AliHFEcollection::Sumw2(const char* name){
432 // Set Sumw2 for the given object
434 if(!CheckObject(name)){
438 TObject *o = Get(name);
439 THnSparse *htmp = dynamic_cast<THnSparse*>(o);
445 //___________________________________________________________________
446 Bool_t AliHFEcollection::BinLogAxis(const char* name, Int_t dim){
449 // converts the axis (defined by the dimension) of THx or THnSparse
450 // object to Log scale. Number of bins and bin min and bin max are preserved
454 if(!CheckObject(name)){
458 TObject *o = Get(name);
460 if(o->InheritsFrom("TH1")){
461 TH1 *h1 = dynamic_cast<TH1F*>(o);
462 if(h1) axis = h1->GetXaxis();
464 if(o->InheritsFrom("TH2")){
465 TH2 *h2 = dynamic_cast<TH2F*>(o);
468 axis = h2->GetXaxis();
471 axis = h2->GetYaxis();
474 AliError("Only dim = 0 or 1 possible for TH2F");
478 if(o->InheritsFrom("THnSparse")){
479 THnSparse *hs = dynamic_cast<THnSparse*>(o);
480 if(hs) axis = hs->GetAxis(dim);
484 AliError(Form("Axis '%d' could not be identified in the object '%s'\n", dim, name));
488 Int_t bins = axis->GetNbins();
490 Double_t from = axis->GetXmin();
492 AliError(Form(" Log binning not possible for object '%s'because the '%d' axis starts from '%f\n'", name, dim, from));
495 Double_t to = axis->GetXmax();
496 Double_t *newBins = new Double_t[bins+1];
498 Double_t factor = TMath::Power(to/from, 1./bins);
499 for(Int_t i=1; i<=bins; ++i){
500 newBins[i] = factor * newBins[i-1];
502 axis->Set(bins, newBins);
509 //___________________________________________________________________
510 Long64_t AliHFEcollection::Merge(TCollection *list){
513 // Merge the collections
523 TList templist; // Create temporary list containing all the lists to merge
525 AliHFEcollection *coll = dynamic_cast<AliHFEcollection *>(o);
527 templist.Add(coll->fList);
530 fList->Merge(&templist);
533 //____________________________________________________________________
534 void AliHFEcollection::Browse(TBrowser *b)
538 // Browse the content of the directory.
545 //Add objects that are only in memory
546 while ((obj = nextin())) {
547 b->Add(obj, obj->GetName());