// with number of bins equal to the product of the numbers of bins in all
// dimensions. For easy inspection the histogram range was set to -0.5,n-0.5.
+ for (int i=0; i<fNdim; i++) ax[i]->SetName(Form("axis%d",i));
for (int i=0; i<fNdim; i++) ax[i]->Copy(fAxis[i]);
// for speed reasons, number of bins of each axis is stored on fNbins
fMbins[fNdim-1] = 1;
for (int i=0; i<fNdim; i++) fNbins[i] = fAxis[i].GetNbins();
for (int i=fNdim-1; i>0; i--) fMbins[i-1] = fMbins[i]*fNbins[i];
- printf("%d-dimensional histogram %s with %d bins created\n",fNdim,nam,GetNbinsX());
+ printf(" %d-dimensional histogram %s with %d bins created\n",fNdim,nam,GetNbinsX());
}
//=============================================================================
-AliUnicorHN::AliUnicorHN(Char_t *filnam, Char_t *nam)
- : TH1D(*((TH1D*) TFile::Open(filnam,"read")->GetDirectory(nam)->Get("histo"))),
- fNdim(0)
+AliUnicorHN::AliUnicorHN(Char_t *filnam, Char_t *nam) :
+ TH1D(*(TFile::Open(filnam,"read")?
+ TFile::Open(filnam,"read")->GetDirectory(nam)?
+ (TH1D*) TFile::Open(filnam,"read")->GetDirectory(nam)->Get("histo"):new TH1D():new TH1D()
+ )),
+ fNdim(0)
{
// Constructor for reading from file.
TFile *f = TFile::Open(filnam,"read");
- f->cd(nam);
- TAxis *ax[fgkMaxNdim];
- char axnam[1000];
- for (fNdim=0; fNdim<fgkMaxNdim; fNdim++) {
- sprintf(axnam,"axis%d",fNdim);
- ax[fNdim] = (TAxis *) gDirectory->Get(axnam);
- if (ax[fNdim]) ax[fNdim]->Copy(fAxis[fNdim]);
- else break;
- }
- f->Close();
-
- fMbins[fNdim-1] = 1;
- for (int i=0; i<fNdim; i++) fNbins[i] = fAxis[i].GetNbins();
- for (int i=fNdim-1; i>0; i--) fMbins[i-1] = fMbins[i]*fNbins[i];
+ if (f) if (f->cd(nam)) {
+ TAxis *ax[fgkMaxNdim];
+ for (fNdim=0; fNdim<fgkMaxNdim; fNdim++) {
+ ax[fNdim] = (TAxis *) gDirectory->Get(Form("axis%d",fNdim));
+ if (ax[fNdim]) ax[fNdim]->Copy(fAxis[fNdim]);
+ else break;
+ }
+ f->Close();
+
+ fMbins[fNdim-1] = 1;
+ for (int i=0; i<fNdim; i++) fNbins[i] = fAxis[i].GetNbins();
+ for (int i=fNdim-1; i>0; i--) fMbins[i-1] = fMbins[i]*fNbins[i];
+
+ if (GetNbinsX()!=Albins(fNdim,ax)) {
+ printf("number of bins of histo %d differs from product of nbins of axes %d\n",
+ GetNbinsX(),Albins(fNdim,ax));
+ printf("bombing\n");
+ exit(-1);
+ }
- if (GetNbinsX()!=Albins(fNdim,ax)) {
- printf("number of bins of histo %d differs from product of nbins of axes %d\n",
- GetNbinsX(),Albins(fNdim,ax));
- printf("bombing\n");
- exit(-1);
+ printf("%d-dimensional histogram %s with %d bins read from file %s\n",
+ fNdim,nam,GetNbinsX(),filnam);
}
- printf("%d-dimensional histogram %s with %d bins read from file %s\n",
- fNdim,nam,GetNbinsX(),filnam);
}
//=============================================================================
Int_t AliUnicorHN::Albins(Int_t n, TAxis **ax)
return nbins;
}
//=============================================================================
-Int_t AliUnicorHN::MulToOne(Int_t *k) const
+Int_t AliUnicorHN::MulToOne(const Int_t * const k) const
{
// Calculate the 1-dim index n from n-dim indices k[fNdim].
// Valid k[i] should be between 0 and fNbins[i]-1.
}
}
//=============================================================================
-Int_t AliUnicorHN::Fill(Double_t *xx, Double_t w)
+Int_t AliUnicorHN::Fill(Double_t *xx, Double_t w)
{
// Fill the histogram. The array xx holds the abscissa information, w is the
// weigth. The 1-dim histogram is filled using the standard Fill method
for (int i=2; i<fNdim; i++) xx[i] = va_arg(ap,Double_t);
Double_t weigth = va_arg(ap,Double_t);
va_end(ap);
+ //for (int i=0; i<fgkMaxNdim; i++) printf("%8.2f ",xx[i]); printf("%6.2f \n",weigth);
return Fill(xx,weigth);
}
//=============================================================================
if (dest) {
dest->cd();
nbytes += histo.Write("histo");
- char axnam[1000];
- for (int i=0; i<fNdim; i++) {
- sprintf(axnam,"axis%d",i);
- nbytes += fAxis[i].Write(axnam);
- }
+ for (int i=0; i<fNdim; i++) nbytes += fAxis[i].Write();
printf(" %s stored in %s\n",GetName(),dest->GetPath());
dest->cd("..");
}
{
// Reduce dimension dim by summing up its bins between first and last.
// Use root convention: bin=1 is the first bin, bin=nbins is the last.
+ // last=0 means till the last bin
// Return the resulting fNdim-1 dimensional histogram.
if (dim<0 || dim>fNdim-1) return 0;
- if (first<0) first = 1;
- if (last<0) last = fNbins[dim];
+ if (last<=0) last = fNbins[dim];
// create new (reduced) histogram
return his;
}
//=============================================================================
-TH1D *AliUnicorHN::ProjectOn(char *nam, Int_t dim, Int_t *first, Int_t *last) const
+TH1D *AliUnicorHN::ProjectOn(char *nam, Int_t dim, const Int_t * const first, const Int_t * const last) const
{
// Project on dimension dim. Use only bins between first[i] and last[i].
// Use root convention: bin=1 is the first bin, bin=nbins is the last.
TH1D *his;
char *name = strlen(nam)? nam : Form("%s_proj%d",GetName(),dim);
+ // if (gDirectory->Get(name)) gDirectory->Get(name)->Delete(); // for some reason leads to troubles
if (fAxis[dim].IsVariableBinSize())
his = new TH1D(name,name,fNbins[dim],fAxis[dim].GetXbins()->GetArray());
else
return his;
}
//=============================================================================
-TH1D *AliUnicorHN::ProjectOn(char *nam, Int_t dim, Double_t *first, Double_t *last)
+TH1D *AliUnicorHN::ProjectOn(char *nam, Int_t dim, const Double_t * const first, const Double_t * const last)
{
// Project on dimension dim. Use only bins between first[i] and last[i].
return ProjectOn(nam,dim,kfirst,klast);
}
//=============================================================================
-TH2D *AliUnicorHN::ProjectOn(char *nam, Int_t dim0, Int_t dim1, Int_t *first, Int_t *last) const
+TH2D *AliUnicorHN::ProjectOn(char *nam, Int_t dim0, Int_t dim1, const Int_t * const first, const Int_t * const last) const
{
// Project on dim1 vs dim0. Use only bins between first[i] and last[i].
// Use root convention: bin=1 is the first bin, bin=nbins is the last.