Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
draw_v6.cxx
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_v7
3///
4/// This macro shows how ROOT objects like TH1, TH2, TGraph can be drawn in RCanvas.
5///
6/// \macro_code
7///
8/// \date 2017-06-02
9/// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback
10/// is welcome!
11/// \authors Axel Naumann <axel@cern.ch>, Sergey Linev <s.linev@gsi.de>
12
13/*************************************************************************
14 * Copyright (C) 1995-2017, Rene Brun and Fons Rademakers. *
15 * All rights reserved. *
16 * *
17 * For the licensing terms see $ROOTSYS/LICENSE. *
18 * For the list of contributors see $ROOTSYS/README/CREDITS. *
19 *************************************************************************/
20
22#include <ROOT/RPad.hxx>
23#include <ROOT/RCanvas.hxx>
24#include "TH1.h"
25#include "TH2.h"
26#include "TGraph.h"
27#include "TMath.h"
28#include "TStyle.h"
29
30#include <iostream>
31
32void draw_v6()
33{
34 using namespace ROOT::Experimental;
35
36 static constexpr int npoints = 10;
37 double x[npoints] = { 0., 1., 2., 3., 4., 5., 6., 7., 8., 9. };
38 double y[npoints] = { .1, .2, .3, .4, .3, .2, .1, .2, .3, .4 };
39 auto gr = std::make_shared<TGraph>(npoints, x, y);
40
41 static constexpr int nth1points = 100;
42 auto th1 = std::make_shared<TH1I>("gaus", "Example of TH1", nth1points, -5, 5);
43 th1->SetDirectory(nullptr);
44 for (int n=0;n<nth1points;++n) {
45 double x = 10.*n/nth1points-5.;
46 th1->SetBinContent(n+1, (int) (1000*TMath::Gaus(x)));
47 }
48
49 static constexpr int nth2points = 40;
50 auto th2 = std::make_shared<TH2I>("gaus2", "Example of TH1", nth2points, -5, 5, nth2points, -5, 5);
51 th2->SetDirectory(nullptr);
52 for (int n=0;n<nth2points;++n) {
53 for (int k=0;k<nth2points;++k) {
54 double x = 10.*n/nth2points-5.;
55 double y = 10.*k/nth2points-5.;
56 th2->SetBinContent(th2->GetBin(n+1, k+1), (int) (1000*TMath::Gaus(x)*TMath::Gaus(y)));
57 }
58 }
59
61
62 auto canvas = RCanvas::Create("RCanvas showing a v6 objects");
63
64 // place copy of gStyle object, will be applied on JSROOT side
65 // set on the canvas before any other object is drawn
66 canvas->Draw<TObjectDrawable>(TObjectDrawable::kStyle);
67
68 // copy all existing ROOT colors, required when colors was modified
69 // or when colors should be possible from client side
70 canvas->Draw<TObjectDrawable>(TObjectDrawable::kColors);
71
72 // copy custom palette to canvas, will be used for col drawings
73 // style object does not include color settings
74 canvas->Draw<TObjectDrawable>(TObjectDrawable::kPalette);
75
76 // Divide canvas on 2x2 sub-pads to show different draw options
77 auto subpads = canvas->Divide(2,2);
78
79 subpads[0][0]->Draw<TObjectDrawable>(gr, "AL");
80
81 subpads[0][1]->Draw<TObjectDrawable>(th1, "");
82
83 subpads[1][0]->Draw<TObjectDrawable>(th2, "colz");
84
85 // show same object again, but with other draw options
86 subpads[1][1]->Draw<TObjectDrawable>(th2, "lego2");
87
88 canvas->Show(); // new window in default browser should popup and async update will be triggered
89
90 // synchronous, wait until drawing is really finished
91 canvas->Update(false, [](bool res) { std::cout << "First sync update done = " << (res ? "true" : "false") << std::endl; });
92
93 // invalidate canvas and force repainting with next Update()
94 canvas->Modified();
95
96 // call Update again, should return immediately if canvas was not modified
97 canvas->Update(true, [](bool res) { std::cout << "Second async update done = " << (res ? "true" : "false") << std::endl; });
98
99 std::cout << "This message appear normally before second async update" << std::endl;
100
101 // create SVG file
102 // canvas->SaveAs("draw_v6.svg");
103
104 // create PNG file
105 // canvas->SaveAs("draw_v6.png");
106}
@ kRainBow
Definition TColor.h:108
R__EXTERN TStyle * gStyle
Definition TStyle.h:412
Provides v7 drawing facilities for TObject types (TGraph etc).
void SetPalette(Int_t ncolors=kBird, Int_t *colors=0, Float_t alpha=1.)
See TColor::SetPalette.
Definition TStyle.cxx:1782
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
TGraphErrors * gr
Definition legend1.C:25
Double_t Gaus(Double_t x, Double_t mean=0, Double_t sigma=1, Bool_t norm=kFALSE)
Calculate a gaussian function with mean and sigma.
Definition TMath.cxx:448
auto * th2
Definition textalign.C:17
auto * th1
Definition textalign.C:13