Draw overflow bin

Hello all,

I found this thread as I was looking for a way to draw overflow bin.
I tried the macros you proposed and I found some problems regarding the reset of the number of entries of the new histogram and the way in which the errors are propagated by using the Fill method.
Concerning the number of entries, the previous macro posted here says htmp->SetEntries(h->GetEntries()). This is ok just in case the original histogram is filled with weight=1. In the case in which weights!=1, it is correct using GetEffectiveEntries() which returns the square of sum of the weights divided by the sum of the weights square (as Lorenzo Moneta explains in this thread [url]TH1D: integral + underflow + overflow != entries Regarding the Fill(), I substituted that part of the code with SetBinContent() and SetBinError() methods. This is the only way I found to restore the uncertainties properly.
I put here an updated version of the macro, in case it might be useful for anyone.

   TH1F *DrawOverflow(TH1F* h){
    //function to paint the histogram h with an extra bin for overflows
    UInt_t nx    = h->GetNbinsX()+1;
    Double_t *xbins= new Double_t[nx+1];
    for (UInt_t i=0;i<nx;i++)
        xbins[i]=h->GetBinLowEdge(i+1);
    xbins[nx]=xbins[nx-1]+h->GetBinWidth(nx);
    //book a temporary histogram having extra bins for overflows
    TH1F *htmp = new TH1F(h->GetName(), h->GetTitle(), nx, xbins);
    htmp->Sumw2();
    //fill the new histogram including the overflows
    for (UInt_t i=1; i<=nx; i++) {
        htmp->SetBinContent(htmp->FindBin(htmp->GetBinCenter(i)),h->GetBinContent(i));
        htmp->SetBinError(htmp->FindBin(htmp->GetBinCenter(i)),h->GetBinError(i));
    }
    htmp->SetBinContent(htmp->FindBin(h->GetBinLowEdge(1)-1), h->GetBinContent(0));
    htmp->SetBinError(htmp->FindBin(h->GetBinLowEdge(1)-1), h->GetBinError(0));
    // Restore the number of entries
    htmp->SetEntries(h->GetEffectiveEntries());
    return htmp;
}

Cheers,
Lucia

1 Like