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 //___________________________________________________________________
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 for(Int_t ien = 0; ien < fList->GetEntries(); ien++)
103 target.fList->Add(fList->At(ien)->Clone());
105 //___________________________________________________________________
106 AliHFEcollection::~AliHFEcollection(){
112 AliDebug(1, "DESTRUCTOR");
114 //___________________________________________________________________
115 Bool_t AliHFEcollection::CreateTH1F(const char* name, const char* title, Int_t nBin, Float_t nMin, Float_t nMax, Int_t logAxis){
118 // Creates a TH1F histogram for the collection
122 AliError("No TList pointer ! ");
126 fList->Add(new TH1F(name, title, nBin, nMin, nMax));
128 BinLogAxis(name, logAxis);
130 return CheckObject(name);
133 //___________________________________________________________________
134 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){
137 // Creates a TH2F histogram for the collection
141 AliError("No TList pointer ! ");
144 fList->Add(new TH2F(name, title, nBinX, nMinX, nMaxX, nBinY, nMinY, nMaxY));
146 BinLogAxis(name, logAxis);
148 return CheckObject(name);
150 //___________________________________________________________________
151 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){
154 // create a 1 dimensional array of size [X]
158 AliError("No TList pointer ! ");
162 AliError("can not create array with negative or zero size ");
166 for(Int_t i=0; i<X; ++i){
168 hname.Append(Form("%s_[%d]", name, i));
169 //cout<<" -D: name: "<<name.str().c_str()<<endl;
170 //cout<<" -D: nBin: "<<_nBin<<" ,Min: "<<_nMin<<" , Max: "<<_nMax<<endl;
171 CreateTH1F(hname.Data(), title, nBin, nMin, nMax, logAxis);
172 if(!CheckObject(hname.Data())){
173 AliError(Form("Not possible to create object: %s", hname.Data()));
179 //___________________________________________________________________
180 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){
183 // create a 1 dimensinal array of TH2F histograms with size [X]
187 AliError("No TList pointer !");
191 AliError("can not create array with negative or zero size ");
195 for(Int_t i=0; i<X; ++i){
197 hname.Append(Form("%s_[%d]", name, i));
198 //cout<<" -D: name: "<<name<<endl;
199 //cout<<" -D: nBin: "<<_nBin<<" ,Min: "<<_nMin<<" , Max: "<<_nMax<<endl;
200 CreateTH2F(hname.Data(), title, nBinX, nMinX, nMaxX, nBinY, nMinY, nMaxY, logAxis);
201 if(!CheckObject(hname.Data())){
202 AliError(Form("Not possible to create object: %s", hname.Data()));
208 //___________________________________________________________________
209 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){
212 // create a 2 dimensional array of histograms of size [X, Y]
216 AliError("No TList pointer ! ");
220 AliError("can not create array with negative or zero size ");
224 for(Int_t i=0; i<X; ++i){
225 for(Int_t j=0; j<Y; ++j){
227 hname.Append(Form("%s_[%d][%d]", name, i, j));
228 //cout<<" -D: name: "<<name.str().c_str()<<endl;
229 //cout<<" -D: nBin: "<<_nBin<<" ,Min: "<<_nMin<<" , Max: "<<_nMax<<endl;
230 CreateTH1F(hname.Data(), title, nBin, nMin, nMax, logAxis);
231 if(!CheckObject(hname.Data())){
232 AliError(Form("Not possible to create object: %s", hname.Data()));
239 //___________________________________________________________________
240 Bool_t AliHFEcollection::CreateProfile(const char* name, const char* title, Int_t nbins, Double_t xmin, Double_t xmax){
243 // create a simple TProfile
247 AliError("No TList pointer ! ");
250 fList->Add(new TProfile(name, title, nbins, xmin, xmax));
251 return CheckObject(name);
254 //___________________________________________________________________
255 Bool_t AliHFEcollection::CreateTHnSparse(const char* name, const char* title, Int_t dim, Int_t* nbins, Double_t* xmin, Double_t* xmax){
258 // create 'dim' dimensional THnSparse
262 AliError("No TList pointer ! ");
265 fList->Add(new THnSparseF(name, title, dim, nbins, xmin, xmax));
266 return CheckObject(name);
269 //___________________________________________________________________
270 TObject* AliHFEcollection::Get(const char* name){
273 // Get histogram with the required name
277 if(!CheckObject(name)){
278 AliWarning(Form("Not possible to return pointer to the object '%s'\n", name));
282 return fList->FindObject(name);
285 //___________________________________________________________________
286 Bool_t AliHFEcollection::Fill(const char* name, Double_t v){
289 // fill function for one TH1 histograms
292 if(!CheckObject(name)){
293 AliError(Form("Not possible to fill the object '%s', the object does not exist\n", name));
297 TH1 *htmp = dynamic_cast<TH1F*>(fList->FindObject(name));
298 // chack the possible object types
307 //___________________________________________________________________
308 Bool_t AliHFEcollection::Fill(const char* name, Int_t v){
311 // fill function for one TH1 histograms for integer numbers
314 return Fill(name, v*1.0);
316 //___________________________________________________________________
317 Bool_t AliHFEcollection::Fill(const char* name, Int_t X, Double_t v){
320 // fill function for one dimension arrays of TH1
323 const char* n = Form("%s_[%d]", name, X);
328 Fill(o->GetName(), v);
331 //___________________________________________________________________
332 Bool_t AliHFEcollection::Fill(const char* name, Int_t X, Int_t Y, Double_t v){
335 // Fill function fir 2 dimensional TH1 arrays
338 const char* n = Form("%s_[%d][%d]", name, X, Y);
343 Fill(o->GetName(), v);
346 //___________________________________________________________________
347 Bool_t AliHFEcollection::Fill(const char* name, Int_t X, Double_t v1, Double_t v2){
350 // fill function for one dimension array of TH2
353 const char* n = Form("%s_[%d]", name, X);
358 Fill(o->GetName(), v1, v2);
362 //___________________________________________________________________
363 Bool_t AliHFEcollection::Fill(const char* name, Double_t v1, Double_t v2){
366 // fill function for TH2 objects
369 if(!CheckObject(name)){
370 AliError(Form("Not possible to fill the object '%s', the object does not exist\n", name));
374 // chack the possible object types
375 if(fList->FindObject(name)->InheritsFrom("TH2")){
376 TH2 *h2 = dynamic_cast<TH2F*>(fList->FindObject(name));
377 if(h2) h2->Fill(v1, v2);
380 if(fList->FindObject(name)->InheritsFrom("TProfile")){
381 TProfile *pr = dynamic_cast<TProfile*>(fList->FindObject(name));
382 if(pr) pr->Fill(v1, v2);
389 //___________________________________________________________________
390 Bool_t AliHFEcollection::Fill(const char* name, Double_t* entry, Double_t weight){
392 // Fill a THnSparse object
395 if(!CheckObject(name)){
396 AliError(Form("Not possible to fill the object '%s', the object does not exist\n", name));
400 THnSparseF *htmp = dynamic_cast<THnSparseF*>(fList->FindObject(name));
402 htmp->Fill(entry, weight);
408 //___________________________________________________________________
409 Bool_t AliHFEcollection::CheckObject(const char* name){
412 // check wheter the creation of the histogram was succesfull
416 AliError("No TList pointer ! ");
420 if(!fList->FindObject(name)){
421 AliWarning(Form("Creating or Finding the object '%s' failed\n", name));
426 //___________________________________________________________________
427 Bool_t AliHFEcollection::Sumw2(const char* name){
429 // Set Sumw2 for the given object
431 if(!CheckObject(name)){
435 TObject *o = Get(name);
436 THnSparse *htmp = dynamic_cast<THnSparse*>(o);
442 //___________________________________________________________________
443 Bool_t AliHFEcollection::BinLogAxis(const char* name, Int_t dim){
446 // converts the axis (defined by the dimension) of THx or THnSparse
447 // object to Log scale. Number of bins and bin min and bin max are preserved
451 if(!CheckObject(name)){
455 TObject *o = Get(name);
457 if(o->InheritsFrom("TH1")){
458 TH1 *h1 = dynamic_cast<TH1F*>(o);
459 if(h1) axis = h1->GetXaxis();
461 if(o->InheritsFrom("TH2")){
462 TH2 *h2 = dynamic_cast<TH2F*>(o);
465 axis = h2->GetXaxis();
468 axis = h2->GetYaxis();
471 AliError("Only dim = 0 or 1 possible for TH2F");
475 if(o->InheritsFrom("THnSparse")){
476 THnSparse *hs = dynamic_cast<THnSparse*>(o);
477 if(hs) axis = hs->GetAxis(dim);
481 AliError(Form("Axis '%d' could not be identified in the object '%s'\n", dim, name));
485 Int_t bins = axis->GetNbins();
487 Double_t from = axis->GetXmin();
489 AliError(Form(" Log binning not possible for object '%s'because the '%d' axis starts from '%f\n'", name, dim, from));
492 Double_t to = axis->GetXmax();
493 Double_t *newBins = new Double_t[bins+1];
495 Double_t factor = TMath::Power(to/from, 1./bins);
496 for(Int_t i=1; i<=bins; ++i){
497 newBins[i] = factor * newBins[i-1];
499 axis->Set(bins, newBins);
506 //___________________________________________________________________
507 Long64_t AliHFEcollection::Merge(TCollection *list){
510 // Merge the collections
520 TList templist; // Create temporary list containing all the lists to merge
522 AliHFEcollection *coll = dynamic_cast<AliHFEcollection *>(o);
524 templist.Add(coll->fList);
527 fList->Merge(&templist);
530 //____________________________________________________________________
531 void AliHFEcollection::Browse(TBrowser *b)
535 // Browse the content of the directory.
542 //Add objects that are only in memory
543 while ((obj = nextin())) {
544 b->Add(obj, obj->GetName());