]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - PWG2/UNICOR/AliUnicorHN.cxx
removed duplications
[u/mrichter/AliRoot.git] / PWG2 / UNICOR / AliUnicorHN.cxx
index 5727def25ca01629e7f15851806f1302e08aea4d..afc35c3fe88075c05ccd23f71f9568ef7907a97c 100644 (file)
@@ -44,6 +44,7 @@ AliUnicorHN::AliUnicorHN(Char_t *nam, Int_t ndim, TAxis **ax)
   // 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
@@ -53,39 +54,42 @@ AliUnicorHN::AliUnicorHN(Char_t *nam, Int_t ndim, TAxis **ax)
   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) 
@@ -99,7 +103,7 @@ 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.
@@ -140,7 +144,7 @@ void AliUnicorHN::OneToMul(Int_t n, Int_t *k) const
   }
 }
 //=============================================================================
-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 
@@ -160,6 +164,7 @@ Int_t AliUnicorHN::Fill(Double_t x0, Double_t x1, ...)
   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);
 }
 //=============================================================================
@@ -177,11 +182,7 @@ Int_t AliUnicorHN::Write() const
   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("..");
   }
@@ -192,11 +193,11 @@ AliUnicorHN *AliUnicorHN::ProjectAlong(char *nam, Int_t dim, Int_t first, Int_t
 {
   // 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
 
@@ -240,7 +241,7 @@ AliUnicorHN *AliUnicorHN::ProjectAlong(char *nam, Int_t dim, Int_t first, Int_t
   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. 
@@ -274,6 +275,7 @@ TH1D *AliUnicorHN::ProjectOn(char *nam, Int_t dim, Int_t *first, Int_t *last) co
 
   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 
@@ -300,7 +302,7 @@ TH1D *AliUnicorHN::ProjectOn(char *nam, Int_t dim, Int_t *first, Int_t *last) co
   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]. 
 
@@ -314,7 +316,7 @@ TH1D *AliUnicorHN::ProjectOn(char *nam, Int_t dim, Double_t *first, Double_t *la
   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.