ROOT logo

From $ROOTSYS/tutorials/hist/cumulative.C

// illustrate use of the TH1::GetCumulative method
//
// M. Schiller, 2014-11-04
//
#include <cassert>
#include <cmath>

#include "TH1.h"
#include "TH1D.h"
#include "TCanvas.h"
#include "TRandom.h"

TCanvas* cumulative()
{
   TH1* h = new TH1D("h", "h", 100, -5., 5.);
   gRandom->SetSeed();
   h->FillRandom("gaus", 1u << 16);
   // get the cumulative of h
   TH1* hc = h->GetCumulative();
   // check that c has the "right" contents
   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
   TCanvas* c = new TCanvas;
   c->Divide(1,2);
   c->cd(1);
   h->Draw();
   c->cd(2);
   hc->Draw();
   c->Update();

   return c;
}
 cumulative.C:1
 cumulative.C:2
 cumulative.C:3
 cumulative.C:4
 cumulative.C:5
 cumulative.C:6
 cumulative.C:7
 cumulative.C:8
 cumulative.C:9
 cumulative.C:10
 cumulative.C:11
 cumulative.C:12
 cumulative.C:13
 cumulative.C:14
 cumulative.C:15
 cumulative.C:16
 cumulative.C:17
 cumulative.C:18
 cumulative.C:19
 cumulative.C:20
 cumulative.C:21
 cumulative.C:22
 cumulative.C:23
 cumulative.C:24
 cumulative.C:25
 cumulative.C:26
 cumulative.C:27
 cumulative.C:28
 cumulative.C:29
 cumulative.C:30
 cumulative.C:31
 cumulative.C:32
 cumulative.C:33
 cumulative.C:34
 cumulative.C:35
 cumulative.C:36