Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
hist101_TH1_autobinning.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_hist
3/// \preview Fill multiple histograms with different functions and automatic binning.
4/// Illustrates merging with the power-of-two autobin algorithm
5///
6/// \macro_output
7/// \macro_code
8///
9/// \date November 2017
10/// \author Gerardo Ganis
11
12#include "TF1.h"
13#include "TH1D.h"
14#include "TMath.h"
15#include "TF1.h"
16#include "TCanvas.h"
17#include "TRandom3.h"
18#include "TStatistic.h"
19#include "TFile.h"
20#include "TStyle.h"
21
22TF1 *gam = new TF1("gam", "1/(1+0.1*x*0.1*x)", -100., 100.);
23TF1 *gam1 = new TF1("gam", "1/(1+0.1*x*0.1*x)", -1., .25);
24TF1 *iga = new TF1("inv gam", "1.-1/(1+0.1*x*0.1*x)", -100., 100.);
25TF1 *iga1 = new TF1("inv gam", "1.-1/(1+0.1*x*0.1*x)", -.5, 1.);
26
27enum class EHist101_Func {
28 kGaus = 1,
29 kLinear = 2,
30 kGamma = 3,
31 kGamma1 = 4,
32 kInvGamma = 5,
33 kInvGamma1 = 6
34};
35
36void hist101_TH1_autobinning(EHist101_Func function = EHist101_Func::kGaus, unsigned nEntriesPerHisto = 1001)
37{
38 const Int_t nbins = 50;
39
40 TRandom3 rndm((Long64_t)time(0));
41
43
44 // Create a histogram with `nbins` bins and range [0, -1].
45 // When a histogram is created with an upper limit lower or equal to its lower limit, it will automatically
46 // compute the axis limits.
47 // The binning is decided as soon as the histogram's internal entry buffer is filled, i.e. when you fill in a number
48 // of entries equal to the buffer size.
49 // The default buffer size is determined by TH1::SetDefaultBufferSize, or you can customize each individual histogram's
50 // buffer by calling TH1D::SetBuffer(int bufferSize).
51 auto href = std::make_unique<TH1D>("myhref", "current", nbins, 0., -1.);
52
53 auto href2 = std::make_unique<TH1D>("myhref", "Auto P2, sequential", nbins, 0., -1.);
54 // If you want to enable power-of-2 auto binning, call this:
55 href2->SetBit(TH1::kAutoBinPTwo);
56
57 // list to hold all histograms we're going to create
59 // tell the list it should delete its elements upon destruction.
60 histoList.SetOwner(true);
61
62 TStatistic x("min"), y("max"), d("dif"), a("mean"), r("rms");
63
64 // Fill a bunch of histograms with the selected function
65 for (int j = 0; j < 10; ++j) {
66 double xmi = 1e15, xma = -1e15;
67 TStatistic xw("work");
68 const std::string hname = "myh" + std::to_string(j);
69
70 // Create more auto-binning histograms and add them to the list
71 auto hw = new TH1D(hname.c_str(), "Auto P2, merged", nbins, 0., -1.);
72 hw->SetBit(TH1::kAutoBinPTwo);
73
74 bool buffering = true;
75 for (UInt_t i = 0; i < nEntriesPerHisto; ++i) {
76 double xx = 0;
77 // clang-format off
78 switch (function) {
79 case EHist101_Func::kGaus: xx = rndm.Gaus(3, 1); break;
80 case EHist101_Func::kLinear: xx = rndm.Rndm() * 100. - 50.; break;
81 case EHist101_Func::kGamma: xx = gam->GetRandom(); break;
82 case EHist101_Func::kGamma1: xx = gam1->GetRandom(); break;
83 case EHist101_Func::kInvGamma: xx = iga->GetRandom(); break;
84 case EHist101_Func::kInvGamma1: xx = iga1->GetRandom(); break;
85 default: xx = rndm.Gaus(0, 1);
86 }
87 // clang-format on
88
89 if (buffering) {
90 if (xx > xma)
91 xma = xx;
92 if (xx < xmi)
93 xmi = xx;
94 xw.Fill(xx);
95 }
96 hw->Fill(xx);
97 href->Fill(xx);
98 href2->Fill(xx);
99 if (!hw->GetBuffer()) {
100 // We exhausted the histogram's buffer
101 buffering = false;
102 }
103 }
104 x.Fill(xmi);
105 y.Fill(xma);
106 d.Fill(xma - xmi);
107 a.Fill(xw.GetMean());
108 r.Fill(xw.GetRMS());
109
110 histoList.Add(hw);
111 }
112
113 x.Print();
114 y.Print();
115 d.Print();
116 a.Print();
117 r.Print();
118
119 // Merge all histograms into one
120 auto h0 = std::unique_ptr<TH1D>(static_cast<TH1D *>(histoList.First()));
121 histoList.Remove(h0.get());
122 if (!h0->Merge(&histoList))
123 return;
124
125 // Set what we want to display in the histogram stat box
126 gStyle->SetOptStat(111110);
127
128 if (gROOT->GetListOfCanvases()->FindObject("c3"))
129 delete gROOT->GetListOfCanvases()->FindObject("c3");
130
131 TCanvas *c3 = new TCanvas("c3", "c3", 800, 800);
132 c3->Divide(1, 3);
133 c3->cd(1);
134 h0->StatOverflows();
135 h0->DrawClone("HIST");
136
137 c3->cd(2);
138 href2->StatOverflows();
139 href2->DrawClone();
140
141 c3->cd(3);
142 href->StatOverflows();
143 href->DrawClone();
144 c3->Update();
145 std::cout << " ent: " << h0->GetEntries() << "\n";
146 h0->Print();
147 href->Print();
148}
#define d(i)
Definition RSha256.hxx:102
#define a(i)
Definition RSha256.hxx:99
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:59
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int)
Definition RtypesCore.h:60
long long Long64_t
Portable signed long integer 8 bytes.
Definition RtypesCore.h:83
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
@ kGamma
Definition TPDGCode.h:27
#define gROOT
Definition TROOT.h:426
R__EXTERN TStyle * gStyle
Definition TStyle.h:442
The Canvas class.
Definition TCanvas.h:23
1-Dim function class
Definition TF1.h:182
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:926
@ kAutoBinPTwo
different than 1.
Definition TH1.h:412
static void SetDefaultBufferSize(Int_t bufsize=1000)
Static function to set the default buffer size for automatic histograms.
Definition TH1.cxx:6806
A doubly linked list.
Definition TList.h:38
Random number generator class based on M.
Definition TRandom3.h:27
Statistical variable, defined by its mean and variance (RMS).
Definition TStatistic.h:33
void SetOptStat(Int_t stat=1)
The type of information printed in the histogram statistics box can be selected via the parameter mod...
Definition TStyle.cxx:1641
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
return c3
Definition legend3.C:15