Logo ROOT  
Reference Guide
hsum.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/// \author Rene Brun
12
13void hsum() {
14 TCanvas *c1 = new TCanvas("c1","The HSUM example",200,10,600,400);
15 c1->SetGrid();
16
17 gBenchmark->Start("hsum");
18
19 // Create some histograms.
20 auto total = new TH1F("total","This is the total distribution",100,-4,4);
21 auto main = new TH1F("main","Main contributor",100,-4,4);
22 auto s1 = new TH1F("s1","This is the first signal",100,-4,4);
23 auto s2 = new TH1F("s2","This is the second signal",100,-4,4);
24 total->Sumw2(); // store the sum of squares of weights
25 total->SetMarkerStyle(21);
26 total->SetMarkerSize(0.7);
27 main->SetFillColor(16);
28 s1->SetFillColor(42);
29 s2->SetFillColor(46);
30 TSlider *slider = 0;
31
32 // Fill histograms randomly
34 const Int_t kUPDATE = 500;
35 Float_t xs1, xs2, xmain;
36 for ( Int_t i=0; i<10000; i++) {
37 xmain = gRandom->Gaus(-1,1.5);
38 xs1 = gRandom->Gaus(-0.5,0.5);
39 xs2 = gRandom->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",
54 "test",4.2,0,4.6,total->GetMaximum(),38);
55 slider->SetFillColor(46);
56 }
57 if (slider) slider->SetRange(0,Float_t(i)/10000.);
58 c1->Modified();
59 c1->Update();
60 }
61 }
62 slider->SetRange(0,1);
63 total->Draw("sameaxis"); // to redraw axis hidden by the fill area
64 c1->Modified();
65 gBenchmark->Show("hsum");
66}
#define s1(x)
Definition: RSha256.hxx:91
int Int_t
Definition: RtypesCore.h:43
float Float_t
Definition: RtypesCore.h:55
R__EXTERN TBenchmark * gBenchmark
Definition: TBenchmark.h:59
static unsigned int total
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:37
virtual void Start(const char *name)
Starts Benchmark with the specified name.
Definition: TBenchmark.cxx:174
virtual void Show(const char *name)
Stops Benchmark name and Prints results.
Definition: TBenchmark.cxx:157
The Canvas class.
Definition: TCanvas.h:27
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:571
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
virtual void SetSeed(ULong_t seed=0)
Set the random generator seed.
Definition: TRandom.cxx:597
virtual Double_t Landau(Double_t mean=0, Double_t sigma=1)
Generate a random number following a Landau distribution with location parameter mu and scale paramet...
Definition: TRandom.cxx:369
A specialized TPad including a TSliderBox object.
Definition: TSlider.h:18
virtual void SetRange(Double_t xmin=0, Double_t xmax=1)
Set Slider range in [0,1].
Definition: TSlider.cxx:207
int main(int argc, char **argv)
return c1
Definition: legend1.C:41
Definition: hsum.py:1