Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
hist101_TH1_autobinning.C File Reference

Detailed Description

Fill multiple histograms with different functions and automatic binning.

Illustrates merging with the power-of-two autobin algorithm

OBJ: TStatistic min Mean = -0.19085 +- 0.1072 RMS = 0.33908 Count = 10 Min = -0.66323 Max = 0.29233
OBJ: TStatistic max Mean = 6.2333 +- 0.1942 RMS = 0.61424 Count = 10 Min = 5.5614 Max = 7.6223
OBJ: TStatistic dif Mean = 6.4241 +- 0.1857 RMS = 0.58714 Count = 10 Min = 5.3576 Max = 7.5832
OBJ: TStatistic mean Mean = 3.0119 +- 0.006168 RMS = 0.019504 Count = 10 Min = 2.9827 Max = 3.0426
OBJ: TStatistic rms Mean = 0.99315 +- 0.006448 RMS = 0.020391 Count = 10 Min = 0.95757 Max = 1.0212
ent: 10010
TH1.Print Name = myh0, Entries= 10010, Total sum= 9999
TH1.Print Name = myhref, Entries= 10010, Total sum= 10009
#include "TF1.h"
#include "TH1D.h"
#include "TMath.h"
#include "TF1.h"
#include "TCanvas.h"
#include "TRandom3.h"
#include "TStatistic.h"
#include "TFile.h"
#include "TStyle.h"
TF1 *gam = new TF1("gam", "1/(1+0.1*x*0.1*x)", -100., 100.);
TF1 *gam1 = new TF1("gam", "1/(1+0.1*x*0.1*x)", -1., .25);
TF1 *iga = new TF1("inv gam", "1.-1/(1+0.1*x*0.1*x)", -100., 100.);
TF1 *iga1 = new TF1("inv gam", "1.-1/(1+0.1*x*0.1*x)", -.5, 1.);
enum class EHist101_Func {
kGaus = 1,
kLinear = 2,
kGamma = 3,
kGamma1 = 4,
kInvGamma = 5,
};
void hist101_TH1_autobinning(EHist101_Func function = EHist101_Func::kGaus, unsigned nEntriesPerHisto = 1001)
{
const Int_t nbins = 50;
TRandom3 rndm((Long64_t)time(0));
// Create a histogram with `nbins` bins and range [0, -1].
// When a histogram is created with an upper limit lower or equal to its lower limit, it will automatically
// compute the axis limits.
// The binning is decided as soon as the histogram's internal entry buffer is filled, i.e. when you fill in a number
// of entries equal to the buffer size.
// The default buffer size is determined by TH1::SetDefaultBufferSize, or you can customize each individual histogram's
// buffer by calling TH1D::SetBuffer(int bufferSize).
auto href = std::make_unique<TH1D>("myhref", "current", nbins, 0., -1.);
auto href2 = std::make_unique<TH1D>("myhref", "Auto P2, sequential", nbins, 0., -1.);
// If you want to enable power-of-2 auto binning, call this:
// list to hold all histograms we're going to create
// tell the list it should delete its elements upon destruction.
histoList.SetOwner(true);
TStatistic x("min"), y("max"), d("dif"), a("mean"), r("rms");
// Fill a bunch of histograms with the selected function
for (int j = 0; j < 10; ++j) {
double xmi = 1e15, xma = -1e15;
TStatistic xw("work");
const std::string hname = "myh" + std::to_string(j);
// Create more auto-binning histograms and add them to the list
auto hw = new TH1D(hname.c_str(), "Auto P2, merged", nbins, 0., -1.);
bool buffering = true;
for (UInt_t i = 0; i < nEntriesPerHisto; ++i) {
double xx = 0;
// clang-format off
switch (function) {
case EHist101_Func::kGaus: xx = rndm.Gaus(3, 1); break;
case EHist101_Func::kLinear: xx = rndm.Rndm() * 100. - 50.; break;
case EHist101_Func::kGamma: xx = gam->GetRandom(); break;
case EHist101_Func::kGamma1: xx = gam1->GetRandom(); break;
case EHist101_Func::kInvGamma: xx = iga->GetRandom(); break;
case EHist101_Func::kInvGamma1: xx = iga1->GetRandom(); break;
default: xx = rndm.Gaus(0, 1);
}
// clang-format on
if (buffering) {
if (xx > xma)
xma = xx;
if (xx < xmi)
xmi = xx;
xw.Fill(xx);
}
hw->Fill(xx);
href->Fill(xx);
href2->Fill(xx);
if (!hw->GetBuffer()) {
// We exhausted the histogram's buffer
buffering = false;
}
}
x.Fill(xmi);
y.Fill(xma);
d.Fill(xma - xmi);
a.Fill(xw.GetMean());
r.Fill(xw.GetRMS());
histoList.Add(hw);
}
x.Print();
y.Print();
d.Print();
a.Print();
r.Print();
// Merge all histograms into one
auto h0 = std::unique_ptr<TH1D>(static_cast<TH1D *>(histoList.First()));
histoList.Remove(h0.get());
if (!h0->Merge(&histoList))
return;
// Set what we want to display in the histogram stat box
gStyle->SetOptStat(111110);
if (gROOT->GetListOfCanvases()->FindObject("c3"))
delete gROOT->GetListOfCanvases()->FindObject("c3");
TCanvas *c3 = new TCanvas("c3", "c3", 800, 800);
c3->Divide(1, 3);
c3->cd(1);
h0->StatOverflows();
h0->DrawClone("HIST");
c3->cd(2);
href2->StatOverflows();
href2->DrawClone();
c3->cd(3);
href->StatOverflows();
href->DrawClone();
c3->Update();
std::cout << " ent: " << h0->GetEntries() << "\n";
h0->Print();
href->Print();
}
#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
Date
November 2017
Author
Gerardo Ganis

Definition in file hist101_TH1_autobinning.C.