Logo ROOT  
Reference Guide
fillhistosauto2p.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_hist
3/// 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
27void fillhistosauto2p(unsigned opt = 1, unsigned n = 1001)
28{
29
30 UInt_t nh = 10;
31 UInt_t bsize = 1000;
32
33 TRandom3 rndm((Long64_t)time(0));
34
35 // Standard autobinning reference
36 auto href = new TH1D("myhref", "current", 50, 0., -1.);
37 href->SetBuffer(bsize);
38
39 // New autobinning 1-histo reference
40 auto href2 = new TH1D("myhref", "Auto P2, sequential", 50, 0., -1.);
41 href2->SetBit(TH1::kAutoBinPTwo);
42 href2->SetBuffer(bsize);
43
44 TList *hlist = new TList;
45
46 Int_t nbins = 50;
47
48 TStatistic x("min"), y("max"), d("dif"), a("mean"), r("rms");
49 for (UInt_t j = 0; j < nh; ++j) {
50 Double_t xmi = 1e15, xma = -1e15;
51 TStatistic xw("work");
52 TString hname = TString::Format("myh%d", j);
53 auto hw = new TH1D(hname.Data(), "Auto P2, merged", nbins, 0., -1.);
54 hw->SetBit(TH1::kAutoBinPTwo);
55 hw->SetBuffer(bsize);
56
57 Double_t xhma, xhmi, ovf, unf;
58 Bool_t emptied = kFALSE, tofill = kTRUE;
59 Bool_t buffering = kTRUE;
60 for (UInt_t i = 0; i < n; ++i) {
61
62 Double_t xx;
63 switch (opt) {
64 case 1: xx = rndm.Gaus(3, 1); break;
65 case 2: xx = rndm.Rndm() * 100. - 50.; break;
66 case 3: xx = gam->GetRandom(); break;
67 case 4: xx = gam1->GetRandom(); break;
68 case 5: xx = iga->GetRandom(); break;
69 case 6: xx = iga1->GetRandom(); break;
70 default: xx = rndm.Gaus(0, 1);
71 }
72
73 if (buffering) {
74 if (xx > xma)
75 xma = xx;
76 if (xx < xmi)
77 xmi = xx;
78 xw.Fill(xx);
79 }
80 hw->Fill(xx);
81 href->Fill(xx);
82 href2->Fill(xx);
83 if (!hw->GetBuffer()) {
84 // Not buffering anymore
85 buffering = kFALSE;
86 }
87 }
88 x.Fill(xmi);
89 y.Fill(xma);
90 d.Fill(xma - xmi);
91 a.Fill(xw.GetMean());
92 r.Fill(xw.GetRMS());
93
94 hlist->Add(hw);
95 }
96
97 x.Print();
98 y.Print();
99 d.Print();
100 a.Print();
101 r.Print();
102
103 TH1D *h0 = (TH1D *)hlist->First();
104 hlist->Remove(h0);
105 if (!h0->Merge(hlist))
106 return;
107
108 gStyle->SetOptStat(111110);
109
110 if (gROOT->GetListOfCanvases()->FindObject("c3"))
111 delete gROOT->GetListOfCanvases()->FindObject("c3");
112 TCanvas *c3 = new TCanvas("c3", "c3", 800, 800);
113 c3->Divide(1, 3);
114 c3->cd(1);
115 h0->StatOverflows();
116 h0->DrawClone("HIST");
117
118 c3->cd(2);
119 href2->StatOverflows();
120 href2->DrawClone();
121
122 c3->cd(3);
123 href->StatOverflows();
124 href->DrawClone();
125 c3->Update();
126 std::cout << " ent: " << h0->GetEntries() << "\n";
127 h0->Print();
128 href->Print();
129
130 hlist->SetOwner(kTRUE);
131 delete hlist;
132 delete href;
133 delete href2;
134 delete h0;
135}
#define d(i)
Definition: RSha256.hxx:102
bool Bool_t
Definition: RtypesCore.h:63
int Int_t
Definition: RtypesCore.h:45
const Bool_t kFALSE
Definition: RtypesCore.h:101
unsigned int UInt_t
Definition: RtypesCore.h:46
double Double_t
Definition: RtypesCore.h:59
long long Long64_t
Definition: RtypesCore.h:80
const Bool_t kTRUE
Definition: RtypesCore.h:100
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
#define gROOT
Definition: TROOT.h:404
R__EXTERN TStyle * gStyle
Definition: TStyle.h:414
The Canvas class.
Definition: TCanvas.h:23
virtual void SetOwner(Bool_t enable=kTRUE)
Set whether this collection is the owner (enable==true) of its content.
void Print(Option_t *option="") const override
Dump this ellipse with its attributes.
Definition: TEllipse.cxx:578
1-Dim function class
Definition: TF1.h:213
virtual Double_t GetRandom(TRandom *rng=nullptr, Option_t *opt=nullptr)
Return a random number following this function shape.
Definition: TF1.cxx:2206
1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:620
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:6913
@ kAutoBinPTwo
different than 1.
Definition: TH1.h:172
void Print(Option_t *option="") const override
Print some global quantities for this histogram.
Definition: TH1.cxx:6997
virtual Double_t GetEntries() const
Return the current number of entries.
Definition: TH1.cxx:4419
virtual Long64_t Merge(TCollection *list)
Definition: TH1.h:343
A doubly linked list.
Definition: TList.h:38
void Add(TObject *obj) override
Definition: TList.h:81
TObject * Remove(TObject *obj) override
Remove object from the list.
Definition: TList.cxx:822
TObject * First() const override
Return the first object in the list. Returns 0 when list is empty.
Definition: TList.cxx:659
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:298
Random number generator class based on M.
Definition: TRandom3.h:27
Statistical variable, defined by its mean and variance (RMS).
Definition: TStatistic.h:33
Basic string class.
Definition: TString.h:136
const char * Data() const
Definition: TString.h:369
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:2345
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:1589
Double_t y[n]
Definition: legend1.C:17
Double_t x[n]
Definition: legend1.C:17
const Int_t n
Definition: legend1.C:16
return c3
Definition: legend3.C:15
TArc a
Definition: textangle.C:12