Logo ROOT   6.12/07
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_code
7 ///
8 /// \author Gerardo Ganis
9 /// \date November 2017
10 #include "TF1.h"
11 #include "TH1D.h"
12 #include "TMath.h"
13 #include "TF1.h"
14 #include "TCanvas.h"
15 #include "TRandom3.h"
16 #include "TStatistic.h"
17 #include "TFile.h"
18 #include "TStyle.h"
19 
20 TF1 *gam = new TF1("gam", "1/(1+0.1*x*0.1*x)", -100., 100.);
21 TF1 *gam1 = new TF1("gam", "1/(1+0.1*x*0.1*x)", -1., .25);
22 TF1 *iga = new TF1("inv gam", "1.-1/(1+0.1*x*0.1*x)", -100., 100.);
23 TF1 *iga1 = new TF1("inv gam", "1.-1/(1+0.1*x*0.1*x)", -.5, 1.);
24 
25 void fillhistosauto2p(unsigned opt = 1, unsigned n = 1001)
26 {
27 
28  UInt_t nh = 10;
29  UInt_t bsize = 1000;
30 
31  TRandom3 rndm((Long64_t)time(0));
32 
33  // Standard autobinning reference
34  auto href = new TH1D("myhref", "current", 50, 0., -1.);
35  href->SetBuffer(bsize);
36 
37  // New autobinning 1-histo reference
38  auto href2 = new TH1D("myhref", "Auto P2, sequential", 50, 0., -1.);
39  href2->SetBit(TH1::kAutoBinPTwo);
40  href2->SetBuffer(bsize);
41 
42  TList *hlist = new TList;
43 
44  Int_t nbins = 50;
45 
46  TStatistic x("min"), y("max"), d("dif"), a("mean"), r("rms");
47  for (UInt_t j = 0; j < nh; ++j) {
48  Double_t xmi = 1e15, xma = -1e15;
49  TStatistic xw("work");
50  TString hname = TString::Format("myh%d", j);
51  auto hw = new TH1D(hname.Data(), "Auto P2, merged", nbins, 0., -1.);
52  hw->SetBit(TH1::kAutoBinPTwo);
53  hw->SetBuffer(bsize);
54 
55  Double_t xhma, xhmi, ovf, unf;
56  Bool_t emptied = kFALSE, tofill = kTRUE;
57  Bool_t buffering = kTRUE;
58  for (UInt_t i = 0; i < n; ++i) {
59 
60  Double_t xx;
61  switch (opt) {
62  case 1: xx = rndm.Gaus(3, 1); break;
63  case 2: xx = rndm.Rndm() * 100. - 50.; break;
64  case 3: xx = gam->GetRandom(); break;
65  case 4: xx = gam1->GetRandom(); break;
66  case 5: xx = iga->GetRandom(); break;
67  case 6: xx = iga1->GetRandom(); break;
68  default: xx = rndm.Gaus(0, 1);
69  }
70 
71  if (buffering) {
72  if (xx > xma)
73  xma = xx;
74  if (xx < xmi)
75  xmi = xx;
76  xw.Fill(xx);
77  }
78  hw->Fill(xx);
79  href->Fill(xx);
80  href2->Fill(xx);
81  if (!hw->GetBuffer()) {
82  // Not buffering anymore
83  buffering = kFALSE;
84  }
85  }
86  x.Fill(xmi);
87  y.Fill(xma);
88  d.Fill(xma - xmi);
89  a.Fill(xw.GetMean());
90  r.Fill(xw.GetRMS());
91 
92  hlist->Add(hw);
93  }
94 
95  x.Print();
96  y.Print();
97  d.Print();
98  a.Print();
99  r.Print();
100 
101  TH1D *h0 = (TH1D *)hlist->First();
102  hlist->Remove(h0);
103  if (!h0->Merge(hlist))
104  return;
105 
106  gStyle->SetOptStat(111110);
107 
108  if (gROOT->GetListOfCanvases()->FindObject("c3"))
109  delete gROOT->GetListOfCanvases()->FindObject("c3");
110  TCanvas *c3 = new TCanvas("c3", "c3", 800, 800);
111  c3->Divide(1, 3);
112  c3->cd(1);
113  h0->StatOverflows();
114  h0->DrawClone("HIST");
115 
116  c3->cd(2);
117  href2->StatOverflows();
118  href2->DrawClone();
119 
120  c3->cd(3);
121  href->StatOverflows();
122  href->DrawClone();
123  c3->Update();
124  std::cout << " ent: " << h0->GetEntries() << "\n";
125  h0->Print();
126  href->Print();
127 
128  hlist->SetOwner(kTRUE);
129  delete hlist;
130  delete href;
131  delete href2;
132  delete h0;
133 }
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:6367
virtual void Print(Option_t *option="") const
Print some global quantities for this histogram.
Definition: TH1.cxx:6451
virtual Double_t GetRandom()
Return a random number following this function shape.
Definition: TF1.cxx:1974
Random number generator class based on M.
Definition: TRandom3.h:27
long long Long64_t
Definition: RtypesCore.h:69
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:219
R__EXTERN TStyle * gStyle
Definition: TStyle.h:402
virtual void SetOwner(Bool_t enable=kTRUE)
Set whether this collection is the owner (enable==true) of its content.
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:688
#define gROOT
Definition: TROOT.h:402
Basic string class.
Definition: TString.h:125
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
virtual void Print(Option_t *option="") const
This method must be overridden when a class wants to print itself.
Definition: TObject.cxx:550
Double_t x[n]
Definition: legend1.C:17
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:2365
Use Power(2)-based algorithm for autobinning.
Definition: TH1.h:169
Statistical variable, defined by its mean and variance (RMS).
Definition: TStatistic.h:35
A doubly linked list.
Definition: TList.h:44
virtual TObject * First() const
Return the first object in the list. Returns 0 when list is empty.
Definition: TList.cxx:655
ROOT::R::TRInterface & r
Definition: Object.C:4
auto * a
Definition: textangle.C:12
virtual TObject * Remove(TObject *obj)
Remove object from the list.
Definition: TList.cxx:818
unsigned int UInt_t
Definition: RtypesCore.h:42
1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:610
const Bool_t kFALSE
Definition: RtypesCore.h:88
The Canvas class.
Definition: TCanvas.h:31
double Double_t
Definition: RtypesCore.h:55
Double_t y[n]
Definition: legend1.C:17
virtual Double_t GetEntries() const
Return the current number of entries.
Definition: TH1.cxx:4178
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
Definition: TPad.cxx:1153
virtual void Add(TObject *obj)
Definition: TList.h:87
1-Dim function class
Definition: TF1.h:211
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:1266
THist< 1, double, THistStatContent, THistStatUncertainty > TH1D
Definition: THist.hxx:284
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2248
const Bool_t kTRUE
Definition: RtypesCore.h:87
return c3
Definition: legend3.C:15
const Int_t n
Definition: legend1.C:16
virtual Long64_t Merge(TCollection *list)
Add all histograms in the collection to this histogram.
Definition: TH1.cxx:5464
const char * Data() const
Definition: TString.h:345