@@ -2505,12 +2505,17 @@ void TH1::ClearUnderflowAndOverflow()
25052505// / The resulting integral is normalized to 1.
25062506// / If the routine is called with the onlyPositive flag set an error will
25072507// / be produced in case of negative bin content and a NaN value returned
2508+ // / \param onlyPositive If set to true, an error will be produced and NaN will be returned
2509+ // / when a bin with negative number of entries is encountered.
2510+ // / \param option
2511+ // / - `""` (default) Compute the cumulative density function assuming current bin contents represent counts.
2512+ // / - `"width"` Computes the cumulative density function assuming current bin contents represent densities.
25082513// / \return 1 if success, 0 if integral is zero, NAN if onlyPositive-test fails
25092514
2510- Double_t TH1::ComputeIntegral (Bool_t onlyPositive)
2515+ Double_t TH1::ComputeIntegral (Bool_t onlyPositive, Option_t *option )
25112516{
25122517 if (fBuffer ) BufferEmpty ();
2513-
2518+ bool useArea = TString (option). Contains ( " width " , TString:: kIgnoreCase );
25142519 // delete previously computed integral (if any)
25152520 if (fIntegral ) delete [] fIntegral ;
25162521
@@ -2524,10 +2529,16 @@ Double_t TH1::ComputeIntegral(Bool_t onlyPositive)
25242529 Int_t ibin = 0 ; fIntegral [ibin] = 0 ;
25252530
25262531 for (Int_t binz=1 ; binz <= nbinsz; ++binz) {
2532+ Double_t zWidth = (fDimension > 2 ) ? fZaxis .GetBinWidth (binz) : 1 ;
25272533 for (Int_t biny=1 ; biny <= nbinsy; ++biny) {
2534+ Double_t yWidth = (fDimension > 1 ) ? fYaxis .GetBinWidth (biny) : 1 ;
25282535 for (Int_t binx=1 ; binx <= nbinsx; ++binx) {
2536+ Double_t xWidth = fXaxis .GetBinWidth (binx);
25292537 ++ibin;
25302538 Double_t y = RetrieveBinContent (GetBin (binx, biny, binz));
2539+ if (useArea)
2540+ y *= xWidth * yWidth * zWidth;
2541+
25312542 if (onlyPositive && y < 0 ) {
25322543 Error (" ComputeIntegral" ," Bin content is negative - return a NaN value" );
25332544 fIntegral [nbins] = TMath::QuietNaN ();
@@ -5005,13 +5016,14 @@ void TH1::GetBinXYZ(Int_t binglobal, Int_t &binx, Int_t &biny, Int_t &binz) cons
50055016// / is evaluated, normalized to one.
50065017// /
50075018// / @param rng (optional) Random number generator pointer used (default is gRandom)
5019+ // / @param option (optional) Set it to "width" if your non-uniform bin contents represent a density rather than counts
50085020// /
50095021// / The integral is automatically recomputed if the number of entries
50105022// / is not the same then when the integral was computed.
5011- // / NB Only valid for 1-d histograms. Use GetRandom2 or 3 otherwise.
5012- // / If the histogram has a bin with negative content a NaN is returned
5023+ // / @note Only valid for 1-d histograms. Use GetRandom2 or GetRandom3 otherwise.
5024+ // / If the histogram has a bin with negative content, a NaN is returned.
50135025
5014- Double_t TH1::GetRandom (TRandom * rng) const
5026+ Double_t TH1::GetRandom (TRandom *rng, Option_t *option ) const
50155027{
50165028 if (fDimension > 1 ) {
50175029 Error (" GetRandom" ," Function only valid for 1-d histograms" );
@@ -5021,10 +5033,11 @@ Double_t TH1::GetRandom(TRandom * rng) const
50215033 Double_t integral = 0 ;
50225034 // compute integral checking that all bins have positive content (see ROOT-5894)
50235035 if (fIntegral ) {
5024- if (fIntegral [nbinsx+1 ] != fEntries ) integral = ((TH1*)this )->ComputeIntegral (true );
5036+ if (fIntegral [nbinsx + 1 ] != fEntries )
5037+ integral = const_cast <TH1 *>(this )->ComputeIntegral (true , option);
50255038 else integral = fIntegral [nbinsx];
50265039 } else {
5027- integral = (( TH1*) this )->ComputeIntegral (true );
5040+ integral = const_cast < TH1 *>( this )->ComputeIntegral (true , option );
50285041 }
50295042 if (integral == 0 ) return 0 ;
50305043 // return a NaN in case some bins have negative content
0 commit comments