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>
27 #include <THnSparse.h>
34 #include "AliHFEcollection.h"
39 ClassImp(AliHFEcollection)
41 //___________________________________________________________________
42 AliHFEcollection::AliHFEcollection():
48 // default constructor
51 fList = new THashList();
53 AliError("Initialization of the list failed");
56 // list is owner of the objects. Once list is deleted, the objects
57 // it contains will be deleted too
58 //fList->SetOwner(kTRUE);
60 //Printf("%s:%d,%p",(char*)__FILE__,__LINE__,fInstance);
63 //___________________________________________________________________
64 AliHFEcollection::AliHFEcollection(const char* name, const char* title):
73 fList = new THashList();
74 fList->SetName(Form("list_%s", name));
76 AliError("Initialization of the list failed");
79 // list is owner of the objects. Once list is deleted, the objects
80 // it contains will be deleted too
81 // fList->SetOwner(kTRUE);
84 //___________________________________________________________________
85 AliHFEcollection::AliHFEcollection(const AliHFEcollection &c) :
96 //___________________________________________________________________
97 AliHFEcollection &AliHFEcollection::operator=(const AliHFEcollection &ref)
100 // Assignment operator
108 //___________________________________________________________________
109 void AliHFEcollection::Copy(TObject &ref) const {
112 // Performs the copying of the object
115 AliHFEcollection &target = dynamic_cast<AliHFEcollection &>(ref);
117 // Clone List Content
118 target.fList = new THashList();
119 for(Int_t ien = 0; ien < fList->GetEntries(); ien++)
120 target.fList->Add(fList->At(ien)->Clone());
122 //___________________________________________________________________
123 AliHFEcollection::~AliHFEcollection(){
131 AliInfo("DESTRUCTOR");
133 //___________________________________________________________________
134 Bool_t AliHFEcollection::CreateTH1F(const char* name, const char* title, Int_t nBin, Float_t nMin, Float_t nMax, Int_t logAxis){
137 // Creates a TH1F histogram for the collection
141 AliError("No TList pointer ! ");
145 fList->Add(new TH1F(name, title, nBin, nMin, nMax));
147 BinLogAxis(name, logAxis);
149 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::CreateProfile(const char* name, const char* title, Int_t nbins, Double_t xmin, Double_t xmax){
262 // create a simple TProfile
266 AliError("No TList pointer ! ");
269 fList->Add(new TProfile(name, title, nbins, xmin, xmax));
270 return CheckObject(name);
273 //___________________________________________________________________
274 Bool_t AliHFEcollection::CreateTHnSparse(const char* name, const char* title, Int_t dim, Int_t* nbins, Double_t* xmin, Double_t* xmax){
277 // create 'dim' dimensional THnSparse
281 AliError("No TList pointer ! ");
284 fList->Add(new THnSparseF(name, title, dim, nbins, xmin, xmax));
285 return CheckObject(name);
288 //___________________________________________________________________
289 TObject* AliHFEcollection::Get(const char* name){
292 // Get histogram with the required name
296 if(!CheckObject(name)){
297 AliWarning(Form("Not possible to return pointer to the object '%s'\n", name));
301 return fList->FindObject(name);
304 //___________________________________________________________________
305 Bool_t AliHFEcollection::Fill(const char* name, Double_t v){
308 // fill function for one TH1 histograms
311 if(!CheckObject(name)){
312 AliError(Form("Not possible to fill the object '%s', the object does not exist\n", name));
316 TH1 *htmp = dynamic_cast<TH1F*>(fList->FindObject(name));
317 // chack the possible object types
326 //___________________________________________________________________
327 Bool_t AliHFEcollection::Fill(const char* name, Int_t v){
330 // fill function for one TH1 histograms for integer numbers
333 return Fill(name, v*1.0);
335 //___________________________________________________________________
336 Bool_t AliHFEcollection::Fill(const char* name, Int_t X, Double_t v){
339 // fill function for one dimension arrays of TH1
342 const char* n = Form("%s_[%d]", name, X);
347 Fill(o->GetName(), v);
350 //___________________________________________________________________
351 Bool_t AliHFEcollection::Fill(const char* name, Int_t X, Int_t Y, Double_t v){
354 // Fill function fir 2 dimensional TH1 arrays
357 const char* n = Form("%s_[%d][%d]", name, X, Y);
362 Fill(o->GetName(), v);
365 //___________________________________________________________________
366 Bool_t AliHFEcollection::Fill(const char* name, Int_t X, Double_t v1, Double_t v2){
369 // fill function for one dimension array of TH2
372 const char* n = Form("%s_[%d]", name, X);
377 Fill(o->GetName(), v1, v2);
381 //___________________________________________________________________
382 Bool_t AliHFEcollection::Fill(const char* name, Double_t v1, Double_t v2){
385 // fill function for TH2 objects
388 if(!CheckObject(name)){
389 AliError(Form("Not possible to fill the object '%s', the object does not exist\n", name));
393 // chack the possible object types
394 if(fList->FindObject(name)->InheritsFrom("TH2")){
395 TH2 *h2 = dynamic_cast<TH2F*>(fList->FindObject(name));
396 if(h2) h2->Fill(v1, v2);
399 if(fList->FindObject(name)->InheritsFrom("TProfile")){
400 (dynamic_cast<TProfile*>(fList->FindObject(name)))->Fill(v1, v2);
407 //___________________________________________________________________
408 Bool_t AliHFEcollection::Fill(const char* name, Double_t* entry, Double_t weight){
410 // Fill a THnSparse object
413 if(!CheckObject(name)){
414 AliError(Form("Not possible to fill the object '%s', the object does not exist\n", name));
418 THnSparseF *htmp = dynamic_cast<THnSparseF*>(fList->FindObject(name));
420 htmp->Fill(entry, weight);
426 //___________________________________________________________________
427 Bool_t AliHFEcollection::CheckObject(const char* name){
430 // check wheter the creation of the histogram was succesfull
434 AliError("No TList pointer ! ");
438 if(!fList->FindObject(name)){
439 AliWarning(Form("Creating or Finding the object '%s' failed\n", name));
444 //___________________________________________________________________
445 Bool_t AliHFEcollection::Sumw2(const char* name){
447 // Set Sumw2 for the given object
449 if(!CheckObject(name)){
453 TObject *o = Get(name);
454 THnSparse *htmp = dynamic_cast<THnSparse*>(o);
460 //___________________________________________________________________
461 Bool_t AliHFEcollection::BinLogAxis(const char* name, Int_t dim){
464 // converts the axis (defined by the dimension) of THx or THnSparse
465 // object to Log scale. Number of bins and bin min and bin max are preserved
469 if(!CheckObject(name)){
473 TObject *o = Get(name);
475 if(o->InheritsFrom("TH1")){
476 TH1 *h1 = dynamic_cast<TH1F*>(o);
477 if(h1) axis = h1->GetXaxis();
479 if(o->InheritsFrom("TH2")){
480 TH2 *h2 = dynamic_cast<TH2F*>(o);
483 axis = h2->GetXaxis();
486 axis = h2->GetYaxis();
489 AliError("Only dim = 0 or 1 possible for TH2F");
493 if(o->InheritsFrom("THnSparse")){
494 THnSparse *hs = dynamic_cast<THnSparse*>(o);
495 if(hs) axis = hs->GetAxis(dim);
499 AliError(Form("Axis '%d' could not be identified in the object '%s'\n", dim, name));
503 Int_t bins = axis->GetNbins();
505 Double_t from = axis->GetXmin();
507 AliError(Form(" Log binning not possible for object '%s'because the '%d' axis starts from '%f\n'", name, dim, from));
510 Double_t to = axis->GetXmax();
511 Double_t *newBins = new Double_t[bins+1];
513 Double_t factor = TMath::Power(to/from, 1./bins);
514 for(Int_t i=1; i<=bins; ++i){
515 newBins[i] = factor * newBins[i-1];
517 axis->Set(bins, newBins);
524 //___________________________________________________________________
525 Long64_t AliHFEcollection::Merge(TCollection *list){
528 // Merge the collections
535 TIterator *iter = list->MakeIterator();
538 while((o = iter->Next())){
539 AliHFEcollection *coll = dynamic_cast<AliHFEcollection *>(o);
541 TList templist; // Create temporary list containing all the lists to merge
542 templist.Add(coll->fList);
543 fList->Merge(&templist);
549 //____________________________________________________________________
550 void AliHFEcollection::Browse(TBrowser *b)
554 // Browse the content of the directory.
561 //Add objects that are only in memory
562 while ((obj = nextin())) {
563 b->Add(obj, obj->GetName());