@@ -2505,12 +2505,15 @@ 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 option (optional) Set it to "width" if your non-uniform bin contents
2509+ // / represent a density rather than counts. In that case, we internally multiply
2510+ // / by the bin width/area so that fIntegral is in the "counts" and not on the original "density" domain.
25082511// / \return 1 if success, 0 if integral is zero, NAN if onlyPositive-test fails
25092512
2510- Double_t TH1::ComputeIntegral (Bool_t onlyPositive)
2513+ Double_t TH1::ComputeIntegral (Bool_t onlyPositive, Option_t *option )
25112514{
25122515 if (fBuffer ) BufferEmpty ();
2513-
2516+ bool useArea = TString (option). ToLower (). Contains ( " width " );
25142517 // delete previously computed integral (if any)
25152518 if (fIntegral ) delete [] fIntegral ;
25162519
@@ -2523,11 +2526,20 @@ Double_t TH1::ComputeIntegral(Bool_t onlyPositive)
25232526 fIntegral = new Double_t[nbins + 2 ];
25242527 Int_t ibin = 0 ; fIntegral [ibin] = 0 ;
25252528
2529+
2530+
2531+ y=y*xWidth*yWidth*zWidth;
25262532 for (Int_t binz=1 ; binz <= nbinsz; ++binz) {
2533+ Double_t zWidth = (fDimension > 2 ) ? fZaxis .GetBinWidth (binz) : 1 ;
25272534 for (Int_t biny=1 ; biny <= nbinsy; ++biny) {
2535+ Double_t yWidth= (fDimension > 1 ) ? fYaxis .GetBinWidth (biny) : 1 ;
25282536 for (Int_t binx=1 ; binx <= nbinsx; ++binx) {
2537+ Double_t xWidth = fXaxis .GetBinWidth (binx);
25292538 ++ibin;
25302539 Double_t y = RetrieveBinContent (GetBin (binx, biny, binz));
2540+ if (useArea)
2541+ y *= xWidth * yWidth * zWidth;
2542+
25312543 if (onlyPositive && y < 0 ) {
25322544 Error (" ComputeIntegral" ," Bin content is negative - return a NaN value" );
25332545 fIntegral [nbins] = TMath::QuietNaN ();
@@ -5005,13 +5017,14 @@ void TH1::GetBinXYZ(Int_t binglobal, Int_t &binx, Int_t &biny, Int_t &binz) cons
50055017// / is evaluated, normalized to one.
50065018// /
50075019// / @param rng (optional) Random number generator pointer used (default is gRandom)
5020+ // / @param option (optional) Set it to "width" if your non-uniform bin contents represent a density rather than counts
50085021// /
50095022// / The integral is automatically recomputed if the number of entries
50105023// / is not the same then when the integral was computed.
5011- // / NB Only valid for 1-d histograms. Use GetRandom2 or 3 otherwise.
5024+ // / @note Only valid for 1-d histograms. Use GetRandom2 or GetRandom3 otherwise.
50125025// / If the histogram has a bin with negative content a NaN is returned
50135026
5014- Double_t TH1::GetRandom (TRandom * rng) const
5027+ Double_t TH1::GetRandom (TRandom * rng, Option_t *option ) const
50155028{
50165029 if (fDimension > 1 ) {
50175030 Error (" GetRandom" ," Function only valid for 1-d histograms" );
@@ -5021,10 +5034,10 @@ Double_t TH1::GetRandom(TRandom * rng) const
50215034 Double_t integral = 0 ;
50225035 // compute integral checking that all bins have positive content (see ROOT-5894)
50235036 if (fIntegral ) {
5024- if (fIntegral [nbinsx+1 ] != fEntries ) integral = ((TH1*)this )->ComputeIntegral (true );
5037+ if (fIntegral [nbinsx+1 ] != fEntries ) integral = ((TH1*)this )->ComputeIntegral (true , option );
50255038 else integral = fIntegral [nbinsx];
50265039 } else {
5027- integral = ((TH1*)this )->ComputeIntegral (true );
5040+ integral = ((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