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);
134 //___________________________________________________________________
135 Bool_t AliHFEcollection::CreateTH1Farray(const char* name, const char* title, Int_t nBin, const Double_t* xbins){
138 // Creates a TH1F histogram for the collection 2nd version
142 AliError("No TList pointer ! ");
146 fList->Add(new TH1F(name, title, nBin, xbins));
147 return CheckObject(name);
151 //___________________________________________________________________
152 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){
155 // Creates a TH2F histogram for the collection
159 AliError("No TList pointer ! ");
162 fList->Add(new TH2F(name, title, nBinX, nMinX, nMaxX, nBinY, nMinY, nMaxY));
164 BinLogAxis(name, logAxis);
166 return CheckObject(name);
168 //___________________________________________________________________
169 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){
172 // create a 1 dimensional array of size [X]
176 AliError("No TList pointer ! ");
180 AliError("can not create array with negative or zero size ");
184 for(Int_t i=0; i<X; ++i){
186 hname.Append(Form("%s_[%d]", name, i));
187 //cout<<" -D: name: "<<name.str().c_str()<<endl;
188 //cout<<" -D: nBin: "<<_nBin<<" ,Min: "<<_nMin<<" , Max: "<<_nMax<<endl;
189 CreateTH1F(hname.Data(), title, nBin, nMin, nMax, logAxis);
190 if(!CheckObject(hname.Data())){
191 AliError(Form("Not possible to create object: %s", hname.Data()));
197 //___________________________________________________________________
198 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){
201 // create a 1 dimensinal array of TH2F histograms with size [X]
205 AliError("No TList pointer !");
209 AliError("can not create array with negative or zero size ");
213 for(Int_t i=0; i<X; ++i){
215 hname.Append(Form("%s_[%d]", name, i));
216 //cout<<" -D: name: "<<name<<endl;
217 //cout<<" -D: nBin: "<<_nBin<<" ,Min: "<<_nMin<<" , Max: "<<_nMax<<endl;
218 CreateTH2F(hname.Data(), title, nBinX, nMinX, nMaxX, nBinY, nMinY, nMaxY, logAxis);
219 if(!CheckObject(hname.Data())){
220 AliError(Form("Not possible to create object: %s", hname.Data()));
226 //___________________________________________________________________
227 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){
230 // create a 2 dimensional array of histograms of size [X, Y]
234 AliError("No TList pointer ! ");
238 AliError("can not create array with negative or zero size ");
242 for(Int_t i=0; i<X; ++i){
243 for(Int_t j=0; j<Y; ++j){
245 hname.Append(Form("%s_[%d][%d]", name, i, j));
246 //cout<<" -D: name: "<<name.str().c_str()<<endl;
247 //cout<<" -D: nBin: "<<_nBin<<" ,Min: "<<_nMin<<" , Max: "<<_nMax<<endl;
248 CreateTH1F(hname.Data(), title, nBin, nMin, nMax, logAxis);
249 if(!CheckObject(hname.Data())){
250 AliError(Form("Not possible to create object: %s", hname.Data()));
257 //___________________________________________________________________
258 Bool_t AliHFEcollection::CreateProfile(const char* name, const char* title, Int_t nbins, Double_t xmin, Double_t xmax){
261 // create a simple TProfile
265 AliError("No TList pointer ! ");
268 fList->Add(new TProfile(name, title, nbins, xmin, xmax));
269 return CheckObject(name);
272 //___________________________________________________________________
273 Bool_t AliHFEcollection::CreateTHnSparse(const char* name, const char* title, Int_t dim, Int_t* nbins, Double_t* xmin, Double_t* xmax){
276 // create 'dim' dimensional THnSparse
280 AliError("No TList pointer ! ");
283 fList->Add(new THnSparseF(name, title, dim, nbins, xmin, xmax));
284 return CheckObject(name);
287 //___________________________________________________________________
288 TObject* AliHFEcollection::Get(const char* name){
291 // Get histogram with the required name
295 if(!CheckObject(name)){
296 AliWarning(Form("Not possible to return pointer to the object '%s'\n", name));
300 return fList->FindObject(name);
303 //___________________________________________________________________
304 Bool_t AliHFEcollection::Fill(const char* name, Double_t v){
307 // fill function for one TH1 histograms
310 if(!CheckObject(name)){
311 AliError(Form("Not possible to fill the object '%s', the object does not exist\n", name));
315 TH1 *htmp = dynamic_cast<TH1F*>(fList->FindObject(name));
316 // chack the possible object types
325 //___________________________________________________________________
326 Bool_t AliHFEcollection::Fill(const char* name, Int_t v){
329 // fill function for one TH1 histograms for integer numbers
332 return Fill(name, v*1.0);
334 //___________________________________________________________________
335 Bool_t AliHFEcollection::Fill(const char* name, Int_t X, Double_t v){
338 // fill function for one dimension arrays of TH1
341 const char* n = Form("%s_[%d]", name, X);
346 Fill(o->GetName(), v);
349 //___________________________________________________________________
350 Bool_t AliHFEcollection::Fill(const char* name, Int_t X, Int_t Y, Double_t v){
353 // Fill function fir 2 dimensional TH1 arrays
356 const char* n = Form("%s_[%d][%d]", name, X, Y);
361 Fill(o->GetName(), v);
364 //___________________________________________________________________
365 Bool_t AliHFEcollection::Fill(const char* name, Int_t X, Double_t v1, Double_t v2){
368 // fill function for one dimension array of TH2
371 const char* n = Form("%s_[%d]", name, X);
376 Fill(o->GetName(), v1, v2);
380 //___________________________________________________________________
381 Bool_t AliHFEcollection::Fill(const char* name, Double_t v1, Double_t v2){
384 // fill function for TH2 objects
387 if(!CheckObject(name)){
388 AliError(Form("Not possible to fill the object '%s', the object does not exist\n", name));
392 // chack the possible object types
393 if(fList->FindObject(name)->InheritsFrom("TH2")){
394 TH2 *h2 = dynamic_cast<TH2F*>(fList->FindObject(name));
395 if(h2) h2->Fill(v1, v2);
398 if(fList->FindObject(name)->InheritsFrom("TProfile")){
399 TProfile *pr = dynamic_cast<TProfile*>(fList->FindObject(name));
400 if(pr) pr->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
538 TList templist; // Create temporary list containing all the lists to merge
540 AliHFEcollection *coll = dynamic_cast<AliHFEcollection *>(o);
542 templist.Add(coll->fList);
545 fList->Merge(&templist);
548 //____________________________________________________________________
549 void AliHFEcollection::Browse(TBrowser *b)
553 // Browse the content of the directory.
560 //Add objects that are only in memory
561 while ((obj = nextin())) {
562 b->Add(obj, obj->GetName());