[ROOT] Propose to have TH1GetRandom() const call

From: Constantin Loizides (loizides@ikf.uni-frankfurt.de)
Date: Thu Feb 26 2004 - 19:21:15 MET


Hi Rene,

it happens quite often that I am passing a pointer of
an histogram to another class or to a member function
of another class. Most of the time I only
want to Monte Carlo sample values from that histogram.
So I would prefer to pass const TH1F pointers.
Looking into the ROOT code it seems that it would be no
problem to have something like the following.

virtual Axis_t TH1::GetRandom(Bool_t UseIfOutdated) const

with the following implementation

Axis_t TH1::GetRandom(Bool_t UseIfOutdated) const
{
// return a random number distributed according the histogram bin contents.
// This function checks if the bins integral exists. If not, zero is
// returned
// if UseIfOutdated is true and If the number of entries
// is not the same as when the integral was computed
// the random value is still computed, otherwise (if UseIfOutdated 
// is false, again zero is returned 
// NB Only valid for 1-d histograms. Use GetRandom2 or 3 otherwise.

   if (fDimension > 1) {
      Error("GetRandom","Function only valid for 1-d histograms");
      return 0;
   }

   if(!fIntegral) return 0;
   Int_t nbinsx = GetNbinsX();

   if ((!UseIfOutdated) && (fIntegral[nbinsx+1] != fEntries))  
   {
      Error("GetRandom","Integral values must be computed, but GetRandom() 
const was called!");
      return 0;
     }

   Double_t r1 = gRandom->Rndm();
   Int_t ibin = TMath::BinarySearch(nbinsx,fIntegral,r1);
   return GetBinLowEdge(ibin+1)
      +GetBinWidth(ibin+1)*(r1-fIntegral[ibin])/(fIntegral[ibin+1] - 
fIntegral[ibin]);
}

Of course it would be nice to have the same for 2 and 3d,

what do you think?

Regards,
Constantin



This archive was generated by hypermail 2b29 : Sun Jan 02 2005 - 05:50:06 MET