#include <TH1F.h>
#include <TH2F.h>
#include <TH3F.h>
#include <THnSparse.h>
#include <TProfile.h>
#include <TString.h>
#include <TBrowser.h>
#include <TMath.h>
#include "AliLog.h"
#include "AliHFEcollection.h"
using namespace std;
ClassImp(AliHFEcollection)
AliHFEcollection::AliHFEcollection():
TNamed()
, fList(NULL)
{
}
AliHFEcollection::AliHFEcollection(const char* name, const char* title):
TNamed(name, title)
, fList(NULL)
{
fList = new THashList();
if(fList){
fList->SetOwner();
fList->SetName(Form("list_%s", name));
}
}
AliHFEcollection::AliHFEcollection(const AliHFEcollection &c) :
TNamed(c)
, fList(NULL)
{
c.Copy(*this);
}
AliHFEcollection &AliHFEcollection::operator=(const AliHFEcollection &ref)
{
if(this != &ref){
ref.Copy(*this);
}
return *this;
}
void AliHFEcollection::Copy(TObject &ref) const {
AliHFEcollection &target = dynamic_cast<AliHFEcollection &>(ref);
target.fList = new THashList();
target.fList->SetOwner();
for(Int_t ien = 0; ien < fList->GetEntries(); ien++)
target.fList->Add(fList->At(ien)->Clone());
}
AliHFEcollection::~AliHFEcollection(){
delete fList;
AliDebug(1, "DESTRUCTOR");
}
Bool_t AliHFEcollection::CreateTH1F(const char* name, const char* title, Int_t nBin, Float_t nMin, Float_t nMax, Int_t logAxis){
if(!fList){
AliError("No TList pointer ! ");
return kFALSE;
}
else{
fList->Add(new TH1F(name, title, nBin, nMin, nMax));
if(logAxis >= 0){
BinLogAxis(name, logAxis);
}
return CheckObject(name);
}
}
Bool_t AliHFEcollection::CreateTH1Farray(const char* name, const char* title, Int_t nBin, const Double_t* xbins){
if(!fList){
AliError("No TList pointer ! ");
return kFALSE;
}
else{
fList->Add(new TH1F(name, title, nBin, xbins));
return CheckObject(name);
}
}
Bool_t AliHFEcollection::CreateTH2Farray(const char* name, const char* title, Int_t nBin, const Double_t* xbins, Int_t nBinY, Float_t nMinY, Float_t nMaxY){
if(!fList){
AliError("No TList pointer ! ");
return kFALSE;
}
else{
fList->Add(new TH2F(name, title, nBin, xbins, nBinY, nMinY, nMaxY));
return CheckObject(name);
}
}
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){
if(!fList){
AliError("No TList pointer ! ");
return kFALSE;
}
fList->Add(new TH2F(name, title, nBinX, nMinX, nMaxX, nBinY, nMinY, nMaxY));
if(logAxis >= 0){
BinLogAxis(name, logAxis);
}
return CheckObject(name);
}
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){
if(!fList){
AliError("No TList pointer ! ");
return kFALSE;
}
if(X <=0){
AliError("can not create array with negative or zero size ");
return kFALSE;
}
TString hname;
for(Int_t i=0; i<X; ++i){
hname = "";
hname.Append(Form("%s_[%d]", name, i));
CreateTH1F(hname.Data(), title, nBin, nMin, nMax, logAxis);
if(!CheckObject(hname.Data())){
AliError(Form("Not possible to create object: %s", hname.Data()));
return kFALSE;
}
}
return kTRUE;
}
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){
if(!fList){
AliError("No TList pointer !");
return kFALSE;
}
if(X <=0){
AliError("can not create array with negative or zero size ");
return kFALSE;
}
TString hname;
for(Int_t i=0; i<X; ++i){
hname = "";
hname.Append(Form("%s_[%d]", name, i));
CreateTH2F(hname.Data(), title, nBinX, nMinX, nMaxX, nBinY, nMinY, nMaxY, logAxis);
if(!CheckObject(hname.Data())){
AliError(Form("Not possible to create object: %s", hname.Data()));
return kFALSE;
}
}
return kTRUE;
}
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){
if(!fList){
AliError("No TList pointer ! ");
return kFALSE;
}
if(X <=0 || Y <=0){
AliError("can not create array with negative or zero size ");
return kFALSE;
}
TString hname;
for(Int_t i=0; i<X; ++i){
for(Int_t j=0; j<Y; ++j){
hname = "";
hname.Append(Form("%s_[%d][%d]", name, i, j));
CreateTH1F(hname.Data(), title, nBin, nMin, nMax, logAxis);
if(!CheckObject(hname.Data())){
AliError(Form("Not possible to create object: %s", hname.Data()));
return kFALSE;
}
}
}
return kTRUE;
}
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){
if(!fList){
AliError("No TList pointer ! ");
return kFALSE;
}
fList->Add(new TH3F(name, title, nBinX, nMinX, nMaxX, nBinY, nMinY, nMaxY, nBinZ, nMinZ, nMaxZ));
if(logAxis >= 0){
BinLogAxis(name, logAxis);
}
return CheckObject(name);
}
Bool_t AliHFEcollection::CreateProfile(const char* name, const char* title, Int_t nbins, Double_t xmin, Double_t xmax){
if(!fList){
AliError("No TList pointer ! ");
return kFALSE;
}
fList->Add(new TProfile(name, title, nbins, xmin, xmax));
return CheckObject(name);
}
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){
if(!fList){
AliError("No TList pointer ! ");
return kFALSE;
}
fList->Add(new THnSparseF(name, title, dim, nbins, xmin, xmax));
return CheckObject(name);
}
Bool_t AliHFEcollection::CreateTHnSparseNoLimits(const char* name, const char* title, Int_t dim, const Int_t* nbins){
if(!fList){
AliError("No TList pointer ! ");
return kFALSE;
}
fList->Add(new THnSparseF(name, title, dim, nbins));
return CheckObject(name);
}
TObject* AliHFEcollection::Get(const char* name){
if(!CheckObject(name)){
AliWarning(Form("Not possible to return pointer to the object '%s'\n", name));
return 0;
}
return fList->FindObject(name);
}
Bool_t AliHFEcollection::Fill(const char* name, Double_t v){
if(!CheckObject(name)){
AliError(Form("Not possible to fill the object '%s', the object does not exist\n", name));
return kFALSE;
}
TH1 *htmp = dynamic_cast<TH1F*>(fList->FindObject(name));
if(htmp){
htmp->Fill(v);
return kTRUE;
}
return kFALSE;
}
Bool_t AliHFEcollection::Fill(const char* name, Int_t v){
return Fill(name, v*1.0);
}
Bool_t AliHFEcollection::Fill(const char* name, Int_t X, Double_t v){
const char* n = Form("%s_[%d]", name, X);
TObject *o = Get(n);
if(!o){
return kFALSE;
}
Fill(o->GetName(), v);
return kTRUE;
}
Bool_t AliHFEcollection::Fill(const char* name, Int_t X, Int_t Y, Double_t v){
const char* n = Form("%s_[%d][%d]", name, X, Y);
TObject *o = Get(n);
if(!o){
return kFALSE;
}
Fill(o->GetName(), v);
return kTRUE;
}
Bool_t AliHFEcollection::Fill(const char* name, Int_t X, Double_t v1, Double_t v2){
const char* n = Form("%s_[%d]", name, X);
TObject *o = Get(n);
if(!o){
return kFALSE;
}
Fill(o->GetName(), v1, v2);
return kTRUE;
}
Bool_t AliHFEcollection::Fill(const char* name, Double_t v1, Double_t v2){
if(!CheckObject(name)){
AliError(Form("Not possible to fill the object '%s', the object does not exist\n", name));
return kFALSE;
}
if(fList->FindObject(name)->InheritsFrom("TH2")){
TH2 *h2 = dynamic_cast<TH2F*>(fList->FindObject(name));
if(h2) h2->Fill(v1, v2);
return kTRUE;
}
if(fList->FindObject(name)->InheritsFrom("TProfile")){
TProfile *pr = dynamic_cast<TProfile*>(fList->FindObject(name));
if(pr) pr->Fill(v1, v2);
return kTRUE;
}
return kFALSE;
}
Bool_t AliHFEcollection::Fill(const char* name, Double_t v1, Double_t v2, Double_t v3){
if(!CheckObject(name)){
AliError(Form("Not possible to fill the object '%s', the object does not exist\n", name));
return kFALSE;
}
TH3 *h3 = dynamic_cast<TH3F*>(fList->FindObject(name));
if(h3){
h3->Fill(v1, v2, v3);
return kTRUE;
}
return kFALSE;
}
Bool_t AliHFEcollection::Fill(const char* name, Double_t* entry, Double_t weight){
if(!CheckObject(name)){
AliError(Form("Not possible to fill the object '%s', the object does not exist\n", name));
return kFALSE;
}
THnSparseF *htmp = dynamic_cast<THnSparseF*>(fList->FindObject(name));
if(htmp){
htmp->Fill(entry, weight);
return kTRUE;
}
return kFALSE;
}
Bool_t AliHFEcollection::CheckObject(const char* name){
if(!fList){
AliError("No TList pointer ! ");
return kFALSE;
}
if(!fList->FindObject(name)){
AliWarning(Form("Creating or Finding the object '%s' failed\n", name));
return kFALSE;
}
return kTRUE;
}
Bool_t AliHFEcollection::Sumw2(const char* name){
if(!CheckObject(name)){
return kFALSE;
}
TObject *o = Get(name);
THnSparse *htmp = dynamic_cast<THnSparse*>(o);
if(htmp){
htmp->Sumw2();
}
return kTRUE;
}
Bool_t AliHFEcollection::BinLogAxis(const char* name, Int_t dim){
if(!CheckObject(name)){
return kFALSE;
}
TObject *o = Get(name);
TAxis *axis = NULL;
TString type(o->IsA()->GetName());
if(type.Contains("TH1")){
TH1 *h1 = dynamic_cast<TH1F*>(o);
if(h1) axis = h1->GetXaxis();
} else if(type.Contains("TH2")){
TH2 *h2 = dynamic_cast<TH2F*>(o);
if(h2){
if(0 == dim){
axis = h2->GetXaxis();
}
else if(1 == dim){
axis = h2->GetYaxis();
}
else{
AliError("Only dim = 0 or 1 possible for TH2F");
}
}
} else if(type.Contains("TH3")){
TH3 *h3 = dynamic_cast<TH3F*>(o);
if(h3){
if(0 == dim){
axis = h3->GetXaxis();
}
else if(1 == dim){
axis = h3->GetYaxis();
}
else if(2 == dim){
axis = h3->GetZaxis();
}
else{
AliError("Only dim = 0, 1 or 2 possible for TH3F");
}
}
} else if(type.Contains("THnSparse")){
THnSparse *hs = dynamic_cast<THnSparse*>(o);
if(hs) axis = hs->GetAxis(dim);
}
if(!axis){
AliError(Form("Axis '%d' could not be identified in the object '%s'\n", dim, name));
return kFALSE;
}
Int_t bins = axis->GetNbins();
Double_t from = axis->GetXmin();
if(from <= 0){
AliError(Form(" Log binning not possible for object '%s'because the '%d' axis starts from '%f\n'", name, dim, from));
return kFALSE;
}
Double_t to = axis->GetXmax();
Double_t *newBins = new Double_t[bins+1];
newBins[0] = from;
Double_t factor = TMath::Power(to/from, 1./bins);
for(Int_t i=1; i<=bins; ++i){
newBins[i] = factor * newBins[i-1];
}
axis->Set(bins, newBins);
delete[] newBins;
return kTRUE;
}
Long64_t AliHFEcollection::Merge(const TCollection *list){
if(!list)
return 0;
if(list->IsEmpty())
return 1;
TIter it(list);
TObject *o = NULL;
Int_t index = 0;
TList templist;
while((o = it())){
AliHFEcollection *coll = dynamic_cast<AliHFEcollection *>(o);
if(!coll) continue;
templist.Add(coll->fList);
index++;
}
fList->Merge(&templist);
return index + 1;
}
void AliHFEcollection::Browse(TBrowser *b)
{
if (b) {
TObject *obj = 0;
TIter nextin(fList);
while ((obj = nextin())) {
b->Add(obj, obj->GetName());
}
}
}
void AliHFEcollection::Print(Option_t *) const{
TIter histIter(fList);
TObject *o = NULL;
Int_t nHistos = 0;
printf("Collection %s\n", GetName());
printf("Content of the collection:\n=========================================\n");
while((o = histIter())){
printf("Histo %s, Type %s\n", o->GetName(), o->IsA()->GetName());
nHistos++;
}
printf("Number of histos in the collection: %d\n", nHistos);
printf("\n");
}