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
22#include <iostream>
23
24TF1 *gam = new TF1("gam", "1/(1+0.1*x*0.1*x)", -100., 100.);
25TF1 *gam1 = new TF1("gam", "1/(1+0.1*x*0.1*x)", -1., .25);
26TF1 *iga = new TF1("inv gam", "1.-1/(1+0.1*x*0.1*x)", -100., 100.);
27TF1 *iga1 = new TF1("inv gam", "1.-1/(1+0.1*x*0.1*x)", -.5, 1.);
28
29enum class EHist101_Func {
30 kGaus = 1,
31 kLinear = 2,
32 kGamma = 3,
33 kGamma1 = 4,
34 kInvGamma = 5,
35 kInvGamma1 = 6
36};
37
38void hist101_TH1_autobinning(EHist101_Func function = EHist101_Func::kGaus, unsigned nEntriesPerHisto = 1001)
39{
40 const Int_t nbins = 50;
41
42 TRandom3 rndm((Long64_t)time(0));
43
45
46 // Create a histogram with `nbins` bins and range [0, -1].
47 // When a histogram is created with an upper limit lower or equal to its lower limit, it will automatically
48 // compute the axis limits.
49 // The binning is decided as soon as the histogram's internal entry buffer is filled, i.e. when you fill in a number
50 // of entries equal to the buffer size.
51 // The default buffer size is determined by TH1::SetDefaultBufferSize, or you can customize each individual histogram's
52 // buffer by calling TH1D::SetBuffer(int bufferSize).
53 auto href = std::make_unique<TH1D>("myhref", "current", nbins, 0., -1.);
54
55 auto href2 = std::make_unique<TH1D>("myhref", "Auto P2, sequential", nbins, 0., -1.);
56 // If you want to enable power-of-2 auto binning, call this:
57 href2->SetBit(TH1::kAutoBinPTwo);
58
59 // list to hold all histograms we're going to create
61 // tell the list it should delete its elements upon destruction.
62 histoList.SetOwner(true);
63
64 TStatistic x("min"), y("max"), d("dif"), a("mean"), r("rms");
65
66 // Fill a bunch of histograms with the selected function
67 for (int j = 0; j < 10; ++j) {
68 double xmi = 1e15, xma = -1e15;
69 TStatistic xw("work");
70 const std::string hname = "myh" + std::to_string(j);
71
72 // Create more auto-binning histograms and add them to the list
73 auto hw = new TH1D(hname.c_str(), "Auto P2, merged", nbins, 0., -1.);
74 hw->SetBit(TH1::kAutoBinPTwo);
75
76 bool buffering = true;
77 for (UInt_t i = 0; i < nEntriesPerHisto; ++i) {
78 double xx = 0;
79 // clang-format off
80 switch (function) {
81 case EHist101_Func::kGaus: xx = rndm.Gaus(3, 1); break;
82 case EHist101_Func::kLinear: xx = rndm.Rndm() * 100. - 50.; break;
83 case EHist101_Func::kGamma: xx = gam->GetRandom(); break;
84 case EHist101_Func::kGamma1: xx = gam1->GetRandom(); break;
85 case EHist101_Func::kInvGamma: xx = iga->GetRandom(); break;
86 case EHist101_Func::kInvGamma1: xx = iga1->GetRandom(); break;
87 default: xx = rndm.Gaus(0, 1);
88 }
89 // clang-format on
90
91 if (buffering) {
92 if (xx > xma)
93 xma = xx;
94 if (xx < xmi)
95 xmi = xx;
96 xw.Fill(xx);
97 }
98 hw->Fill(xx);
99 href->Fill(xx);
100 href2->Fill(xx);
101 if (!hw->GetBuffer()) {
102 // We exhausted the histogram's buffer
103 buffering = false;
104 }
105 }
106 x.Fill(xmi);
107 y.Fill(xma);
108 d.Fill(xma - xmi);
109 a.Fill(xw.GetMean());
110 r.Fill(xw.GetRMS());
111
112 histoList.Add(hw);
113 }
114
115 x.Print();
116 y.Print();
117 d.Print();
118 a.Print();
119 r.Print();
120
121 // Merge all histograms into one
122 auto h0 = std::unique_ptr<TH1D>(static_cast<TH1D *>(histoList.First()));
123 histoList.Remove(h0.get());
124 if (!h0->Merge(&histoList))
125 return;
126
127 // Set what we want to display in the histogram stat box
128 gStyle->SetOptStat(111110);
129
130 if (gROOT->GetListOfCanvases()->FindObject("c3"))
131 delete gROOT->GetListOfCanvases()->FindObject("c3");
132
133 TCanvas *c3 = new TCanvas("c3", "c3", 800, 800);
134 c3->Divide(1, 3);
135 c3->cd(1);
136 h0->StatOverflows();
137 h0->DrawClone("HIST");
138
139 c3->cd(2);
140 href2->StatOverflows();
141 href2->DrawClone();
142
143 c3->cd(3);
144 href->StatOverflows();
145 href->DrawClone();
146 c3->Update();
147 std::cout << " ent: " << h0->GetEntries() << "\n";
148 h0->Print();
149 href->Print();
150}
#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:60
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int)
Definition RtypesCore.h:61
long long Long64_t
Portable signed long integer 8 bytes.
Definition RtypesCore.h:84
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:417
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:6904
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