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);
35 h1->SetStats(kFALSE);
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:101
@ kRed
Definition Rtypes.h:66
Int_t i
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
static std::shared_ptr< RCanvas > Create(const std::string &title)
Create new canvas instance.
Definition RCanvas.cxx:89
Provides v7 drawing facilities for TObject types (TGraph, TH1, TH2, etc).
TH1F * h1
Definition legend1.C:5
static uint64_t sum(uint64_t i)
Definition Factory.cxx:2345