Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
hist014_TH1_cumulative.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_hist
3/// \notebook -js
4/// \preview Illustrate use of the TH1::GetCumulative method.
5///
6/// \macro_image
7/// \macro_code
8///
9/// \date July 2016
10/// \author M. Schiller
11
12#include <cassert>
13#include <cmath>
14
15#include "TH1.h"
16#include "TH1D.h"
17#include "TCanvas.h"
18#include "TRandom.h"
19
20void hist014_TH1_cumulative()
21{
22 TH1 *h = new TH1D("h", "h", 100, -5., 5.);
23 gRandom->SetSeed();
24 h->FillRandom("gaus", 1u << 16);
25 // get the cumulative of h
26 TH1 *hc = h->GetCumulative();
27 // check that c has the "right" contents
28 Double_t *integral = h->GetIntegral();
29 for (Int_t i = 1; i <= hc->GetNbinsX(); ++i) {
30 assert(std::abs(integral[i] * h->GetEntries() - hc->GetBinContent(i)) < 1e-7);
31 }
32 // draw histogram together with its cumulative distribution
33 TCanvas *c = new TCanvas;
34 c->Divide(1, 2);
35 c->cd(1);
36 h->Draw();
37 c->cd(2);
38 hc->Draw();
39 c->Update();
40}
#define c(i)
Definition RSha256.hxx:101
#define h(i)
Definition RSha256.hxx:106
#define e(i)
Definition RSha256.hxx:103
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
externTRandom * gRandom
Definition TRandom.h:62
The Canvas class.
Definition TCanvas.h:23
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:926
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:109
virtual Int_t GetNbinsX() const
Definition TH1.h:541
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3097
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
Definition TH1.cxx:5143