Cumulative

Illustrate use of the TH1::GetCumulative method.

Author: M. Schiller
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, May 24, 2022 at 03:54 PM.

In [1]:
%%cpp -d
#include <cassert>
#include <cmath>

#include "TH1.h"
#include "TH1D.h"
#include "TCanvas.h"
#include "TRandom.h"
In [2]:
TH1* h = new TH1D("h", "h", 100, -5., 5.);
gRandom->SetSeed();
h->FillRandom("gaus", 1u << 16);

Get the cumulative of h

In [3]:
TH1* hc = h->GetCumulative();

Check that c has the "right" contents

In [4]:
Double_t* integral = h->GetIntegral();
for (Int_t i = 1; i <= hc->GetNbinsX(); ++i) {
   assert(std::abs(integral[i] * h->GetEntries() - hc->GetBinContent(i)) < 1e-7);
}

Draw histogram together with its cumulative distribution

In [5]:
TCanvas* c = new TCanvas;
c->Divide(1,2);
c->cd(1);
h->Draw();
c->cd(2);
hc->Draw();
c->Update();

return c;

Draw all canvases

In [6]:
%jsroot on
gROOT->GetListOfCanvases()->Draw()