Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
hist007_TH1_liveupdate.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_hist
3/// \notebook -js
4/// Histograms filled and drawn in a loop.
5/// Simple example illustrating how to use the C++ interpreter
6/// to fill histograms in a loop and show the graphics results
7///
8/// \macro_image
9/// \macro_code
10///
11/// \date November 2024
12/// \author Rene Brun
13
15{
16 TCanvas *c1 = new TCanvas("c1", "Live update of histograms", 200, 10, 600, 400);
17 c1->SetGrid();
18
19 // Create some histograms.
20 auto *total = new TH1D("total", "This is the total distribution", 100, -4, 4);
21 auto *main = new TH1D("main", "Main contributor", 100, -4, 4);
22 auto *s1 = new TH1D("s1", "This is the first signal", 100, -4, 4);
23 auto *s2 = new TH1D("s2", "This is the second signal", 100, -4, 4);
24 total->Sumw2(); // store the sum of squares of weights
25 // set some style properties
26 total->SetMarkerStyle(21); // shape of the markers (\see EMarkerStyle)
27 total->SetMarkerSize(0.7);
28 main->SetFillColor(16);
29 s1->SetFillColor(42);
30 s2->SetFillColor(46);
31 TSlider *slider = nullptr;
32
33 // Fill histograms randomly
35 const int kUPDATE = 500;
36 for (int i = 0; i < 10000; i++) {
37 float xmain = rng.Gaus(-1, 1.5);
38 float xs1 = rng.Gaus(-0.5, 0.5);
39 float xs2 = rng.Landau(1, 0.15);
40 main->Fill(xmain);
41 s1->Fill(xs1, 0.3);
42 s2->Fill(xs2, 0.2);
43 total->Fill(xmain);
44 total->Fill(xs1, 0.3);
45 total->Fill(xs2, 0.2);
46 if (i && (i % kUPDATE) == 0) {
47 if (i == kUPDATE) {
48 total->Draw("e1p");
49 main->Draw("same");
50 s1->Draw("same");
51 s2->Draw("same");
52 c1->Update();
53 slider = new TSlider("slider", "test", 4.2, 0, 4.6, total->GetMaximum(), 38);
54 slider->SetFillColor(46);
55 }
56 if (slider)
57 slider->SetRange(0., 1. * i / 10000.);
58 c1->Modified();
59 c1->Update();
60 }
61 }
62 slider->SetRange(0., 1.);
63 c1->Modified();
64}
int main()
Definition Prototype.cxx:12
#define s1(x)
Definition RSha256.hxx:91
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
static unsigned int total
The Canvas class.
Definition TCanvas.h:23
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:693
Random number generator class based on M.
Definition TRandom3.h:27
A specialized TPad including a TSliderBox object.
Definition TSlider.h:17
return c1
Definition legend1.C:41