Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
th1_twoscales.cxx
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_rcanvas
3///
4/// Macro illustrating how to superimpose two histograms
5/// with different scales on the RCanvas. It shows exactly same data
6/// as in hist/twoscales.C macro, but with fully interactive graphics
7///
8/// \macro_image (rcanvas_js)
9/// \macro_code
10///
11/// \date 2021-07-05
12/// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback
13/// is welcome!
14/// \author Sergey Linev <s.linev@gsi.de>
15
16/*************************************************************************
17 * Copyright (C) 1995-2021, Rene Brun and Fons Rademakers. *
18 * All rights reserved. *
19 * *
20 * For the licensing terms see $ROOTSYS/LICENSE. *
21 * For the list of contributors see $ROOTSYS/README/CREDITS. *
22 *************************************************************************/
23
24#include "ROOT/RCanvas.hxx"
26#include "TH1.h"
27#include "TRandom.h"
28
29using namespace ROOT::Experimental;
30
31void th1_twoscales()
32{
33 // create/fill draw h1
34 auto h1 = std::make_shared<TH1F>("h1", "Example histogram", 100, -3, 3);
35 h1->SetDirectory(nullptr);
37 for (int i = 0; i < 10000; i++)
38 h1->Fill(gRandom->Gaus(0, 1));
39
40 // create hint1 filled with the bins integral of h1
41 auto hint1 = std::make_shared<TH1F>("hint1", "h1 bins integral", 100, -3, 3);
42 hint1->SetDirectory(nullptr);
43 hint1->SetStats(kFALSE);
44 Float_t sum = 0;
45 for (int i = 1; i <= 100; i++) {
46 sum += h1->GetBinContent(i);
47 hint1->SetBinContent(i, sum);
48 }
49 hint1->SetLineColor(kRed);
50 hint1->GetYaxis()->SetAxisColor(kRed);
51 hint1->GetYaxis()->SetLabelColor(kRed);
52
53 // Create a canvas to be displayed.
54 auto canvas = RCanvas::Create("Two TH1 with two independent Y scales");
55
56 // just draw histogram on RCanvas
57 canvas->Draw<TObjectDrawable>(h1, "");
58
59 // add second histogram and specify Y+ draw option
60 canvas->Draw<TObjectDrawable>(hint1, "same,Y+");
61
62 // new window in web browser should popup
63 canvas->Show();
64
65 // create PNG file
66 // canvas->SaveAs("th1_twoscales.png");
67}
float Float_t
Definition RtypesCore.h:57
constexpr Bool_t kFALSE
Definition RtypesCore.h:94
@ kRed
Definition Rtypes.h:66
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
Provides v7 drawing facilities for TObject types (TGraph, TH1, TH2, etc).
virtual void SetDirectory(TDirectory *dir)
By default, when a histogram is created, it is added to the list of histogram objects in the current ...
Definition TH1.cxx:8933
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition TH1.cxx:3316
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
Definition TH1.cxx:5064
virtual void SetStats(Bool_t stats=kTRUE)
Set statistics option on/off.
Definition TH1.cxx:8986
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:275
TH1F * h1
Definition legend1.C:5
static uint64_t sum(uint64_t i)
Definition Factory.cxx:2345