Logo ROOT  
Reference Guide
fillhistosauto2p.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.29071 +- 0.08507 RMS = 0.26902 Count = 10 Min = -0.83577 Max = 2.2251e-308
OBJ: TStatistic max Mean = 6.441 +- 0.1477 RMS = 0.46703 Count = 10 Min = 5.7399 Max = 7.2569
OBJ: TStatistic dif Mean = 6.7318 +- 0.1928 RMS = 0.60974 Count = 10 Min = 6.0969 Max = 8.0927
OBJ: TStatistic mean Mean = 3.0052 +- 0.009827 RMS = 0.031076 Count = 10 Min = 2.9417 Max = 3.0481
OBJ: TStatistic rms Mean = 1.0164 +- 0.005343 RMS = 0.016895 Count = 10 Min = 0.99435 Max = 1.0463
ent: 10010
TH1.Print Name = myh0, Entries= 10010, Total sum= 10005
TH1.Print Name = myhref, Entries= 10010, Total sum= 10008
#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.);
void fillhistosauto2p(unsigned opt = 1, unsigned n = 1001)
{
UInt_t nh = 10;
UInt_t bsize = 1000;
TRandom3 rndm((Long64_t)time(0));
// Standard autobinning reference
auto href = new TH1D("myhref", "current", 50, 0., -1.);
href->SetBuffer(bsize);
// New autobinning 1-histo reference
auto href2 = new TH1D("myhref", "Auto P2, sequential", 50, 0., -1.);
href2->SetBit(TH1::kAutoBinPTwo);
href2->SetBuffer(bsize);
TList *hlist = new TList;
Int_t nbins = 50;
TStatistic x("min"), y("max"), d("dif"), a("mean"), r("rms");
for (UInt_t j = 0; j < nh; ++j) {
Double_t xmi = 1e15, xma = -1e15;
TStatistic xw("work");
TString hname = TString::Format("myh%d", j);
auto hw = new TH1D(hname.Data(), "Auto P2, merged", nbins, 0., -1.);
hw->SetBit(TH1::kAutoBinPTwo);
hw->SetBuffer(bsize);
Double_t xhma, xhmi, ovf, unf;
Bool_t emptied = kFALSE, tofill = kTRUE;
Bool_t buffering = kTRUE;
for (UInt_t i = 0; i < n; ++i) {
switch (opt) {
case 1: xx = rndm.Gaus(3, 1); break;
case 2: xx = rndm.Rndm() * 100. - 50.; break;
case 3: xx = gam->GetRandom(); break;
case 4: xx = gam1->GetRandom(); break;
case 5: xx = iga->GetRandom(); break;
case 6: xx = iga1->GetRandom(); break;
default: xx = rndm.Gaus(0, 1);
}
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()) {
// Not buffering anymore
buffering = kFALSE;
}
}
x.Fill(xmi);
y.Fill(xma);
d.Fill(xma - xmi);
a.Fill(xw.GetMean());
r.Fill(xw.GetRMS());
hlist->Add(hw);
}
x.Print();
y.Print();
d.Print();
a.Print();
r.Print();
TH1D *h0 = (TH1D *)hlist->First();
hlist->Remove(h0);
if (!h0->Merge(hlist))
return;
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->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();
hlist->SetOwner(kTRUE);
delete hlist;
delete href;
delete href2;
delete h0;
}
Date
November 2017
Author
Gerardo Ganis

Definition in file fillhistosauto2p.C.

n
const Int_t n
Definition: legend1.C:16
TRandom::Gaus
virtual Double_t Gaus(Double_t mean=0, Double_t sigma=1)
Samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigm...
Definition: TRandom.cxx:263
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
c3
return c3
Definition: legend3.C:15
TObject::Print
virtual void Print(Option_t *option="") const
This method must be overridden when a class wants to print itself.
Definition: TObject.cxx:552
TString::Data
const char * Data() const
Definition: TString.h:369
r
ROOT::R::TRInterface & r
Definition: Object.C:4
Long64_t
long long Long64_t
Definition: RtypesCore.h:73
TCollection::SetOwner
virtual void SetOwner(Bool_t enable=kTRUE)
Set whether this collection is the owner (enable==true) of its content.
Definition: TCollection.cxx:746
TH1D
1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:615
TObject::DrawClone
virtual TObject * DrawClone(Option_t *option="") const
Draw a clone of this object in the current selected pad for instance with: gROOT->SetSelectedPad(gPad...
Definition: TObject.cxx:221
TStyle.h
Int_t
int Int_t
Definition: RtypesCore.h:45
TH1::GetEntries
virtual Double_t GetEntries() const
Return the current number of entries.
Definition: TH1.cxx:4301
x
Double_t x[n]
Definition: legend1.C:17
TString::Format
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition: TString.cxx:2311
TCanvas.h
TStatistic.h
TString
Definition: TString.h:136
TH1::Print
virtual void Print(Option_t *option="") const
Print some global quantities for this histogram.
Definition: TH1.cxx:6641
Bool_t
bool Bool_t
Definition: RtypesCore.h:63
TFile.h
TList::First
virtual TObject * First() const
Return the first object in the list. Returns 0 when list is empty.
Definition: TList.cxx:658
gStyle
R__EXTERN TStyle * gStyle
Definition: TStyle.h:412
TRandom3
Definition: TRandom3.h:27
a
auto * a
Definition: textangle.C:12
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TRandom3.h
UInt_t
unsigned int UInt_t
Definition: RtypesCore.h:46
y
Double_t y[n]
Definition: legend1.C:17
TStyle::SetOptStat
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:1592
TH1::StatOverflows
static void StatOverflows(Bool_t flag=kTRUE)
if flag=kTRUE, underflows and overflows are used by the Fill functions in the computation of statisti...
Definition: TH1.cxx:6557
Double_t
double Double_t
Definition: RtypesCore.h:59
TF1.h
TCanvas
Definition: TCanvas.h:23
TList::Remove
virtual TObject * Remove(TObject *obj)
Remove object from the list.
Definition: TList.cxx:821
TRandom3::Rndm
virtual Double_t Rndm()
Machine independent random number generator.
Definition: TRandom3.cxx:99
TList::Add
virtual void Add(TObject *obj)
Definition: TList.h:87
d
#define d(i)
Definition: RSha256.hxx:120
TH1::Merge
virtual Long64_t Merge(TCollection *list)
Definition: TH1.h:338
TF1
1-Dim function class
Definition: TF1.h:212
TStatistic
Statistical variable, defined by its mean and variance (RMS). Named, streamable, storable and mergeab...
Definition: TStatistic.h:33
TH1D.h
TF1::GetRandom
virtual Double_t GetRandom()
Return a random number following this function shape.
Definition: TF1.cxx:2094
TList
Definition: TList.h:44
TMath.h
TH1::kAutoBinPTwo
@ kAutoBinPTwo
Use Power(2)-based algorithm for autobinning.
Definition: TH1.h:170
gROOT
#define gROOT
Definition: TROOT.h:406