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);
36 h1->SetStats(kFALSE);
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}
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
float Float_t
Float 4 bytes (float).
Definition RtypesCore.h:71
@ kRed
Definition Rtypes.h:67
externTRandom * 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
Namespace for ROOT features in testing.
Definition TROOT.h:100
static uint64_t sum(uint64_t i)
Definition Factory.cxx:2338