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 is welcome!
13/// \author Sergey Linev <s.linev@gsi.de>
14
15/*************************************************************************
16 * Copyright (C) 1995-2021, Rene Brun and Fons Rademakers. *
17 * All rights reserved. *
18 * *
19 * For the licensing terms see $ROOTSYS/LICENSE. *
20 * For the list of contributors see $ROOTSYS/README/CREDITS. *
21 *************************************************************************/
22
23#include "ROOT/RCanvas.hxx"
25#include "TH1.h"
26#include "TRandom.h"
27
28using namespace ROOT::Experimental;
29
30void th1_twoscales()
31{
32 //create/fill draw h1
33 auto h1 = std::make_shared<TH1F>("h1","Example histogram",100,-3,3);
34 h1->SetDirectory(nullptr);
36 for (int i=0;i<10000;i++)
37 h1->Fill(gRandom->Gaus(0,1));
38
39 //create hint1 filled with the bins integral of h1
40 auto hint1 = std::make_shared<TH1F>("hint1","h1 bins integral",100,-3,3);
41 hint1->SetDirectory(nullptr);
42 hint1->SetStats(kFALSE);
43 Float_t sum = 0;
44 for (int i=1;i<=100;i++) {
45 sum += h1->GetBinContent(i);
46 hint1->SetBinContent(i,sum);
47 }
48 hint1->SetLineColor(kRed);
49 hint1->GetYaxis()->SetAxisColor(kRed);
50 hint1->GetYaxis()->SetLabelColor(kRed);
51
52 // Create a canvas to be displayed.
53 auto canvas = RCanvas::Create("Two TH1 with two independent Y scales");
54
55 // just draw histogram on RCanvas
56 canvas->Draw<TObjectDrawable>(h1, "");
57
58 // add second histogram and specify Y+ draw option
59 canvas->Draw<TObjectDrawable>(hint1, "same,Y+");
60
61 // new window in web browser should popup
62 canvas->Show();
63
64 // create PNG file
65 // canvas->SaveAs("th1_twoscales.png");
66}
float Float_t
Definition RtypesCore.h:57
constexpr Bool_t kFALSE
Definition RtypesCore.h:94
@ kRed
Definition Rtypes.h:66
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:8970
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition TH1.cxx:3346
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
Definition TH1.cxx:5090
virtual void SetStats(Bool_t stats=kTRUE)
Set statistics option on/off.
Definition TH1.cxx:9023
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