Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
tobject.cxx
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_rcanvas
3///
4/// This macro shows how ROOT objects like TH1, TH2, TGraph can be drawn in RCanvas.
5///
6/// \macro_image (rcanvas_js)
7/// \macro_code
8///
9/// \date 2017-06-02
10/// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback
11/// is welcome!
12/// \authors Axel Naumann <axel@cern.ch>, Sergey Linev <s.linev@gsi.de>
13
14/*************************************************************************
15 * Copyright (C) 1995-2021, Rene Brun and Fons Rademakers. *
16 * All rights reserved. *
17 * *
18 * For the licensing terms see $ROOTSYS/LICENSE. *
19 * For the list of contributors see $ROOTSYS/README/CREDITS. *
20 *************************************************************************/
21
23#include <ROOT/RPad.hxx>
24#include <ROOT/RCanvas.hxx>
25#include "TH1.h"
26#include "TH2.h"
27#include "TGraph.h"
28#include "TMath.h"
29#include "TStyle.h"
30
31using namespace ROOT::Experimental;
32
33// create RStyle here to keep reference alive after leaving tobject() function scope
34auto tobject_style = RStyle::Parse("tgraph { line_width: 3; line_color: red; }");
35
36void tobject()
37{
38 static constexpr int npoints = 10;
39 static constexpr int nth1points = 100;
40 static constexpr int nth2points = 40;
41
42 double x[npoints] = { 1., 2., 3., 4., 5., 6., 7., 8., 9., 10. };
43 double y[npoints] = { .1, .2, .3, .4, .3, .2, .1, .2, .3, .4 };
44 auto gr = new TGraph(npoints, x, y);
45
46 // create normal object to be able draw it once
47 auto th1 = new TH1I("gaus", "Example of TH1", nth1points, -5, 5);
48 // it is recommended to set directory to nullptr, but it is also automatically done in TObjectDrawable
49 // th1->SetDirectory(nullptr);
50 th1->FillRandom("gaus", 5000);
51
52 // use std::shared_ptr<TH2I> to let draw same histogram twice with different draw options
53 auto th2 = std::make_shared<TH2I>("gaus2", "Example of TH2", nth2points, -5, 5, nth2points, -5, 5);
54 // is is highly recommended to set directory to nullptr to avoid ownership conflicts
55 th2->SetDirectory(nullptr);
56 for (int n=0;n<nth2points;++n) {
57 for (int k=0;k<nth2points;++k) {
58 double x = 10.*n/nth2points-5.;
59 double y = 10.*k/nth2points-5.;
60 th2->SetBinContent(th2->GetBin(n+1, k+1), (int) (1000*TMath::Gaus(x)*TMath::Gaus(y)));
61 }
62 }
63
65
66 auto canvas = RCanvas::Create("RCanvas showing TObject-derived classes");
67
68 // add gStyle object, will be applied on JSROOT side
69 // set on the canvas before any other object is drawn
70 canvas->Draw<TObjectDrawable>(TObjectDrawable::kStyle);
71
72 // add ROOT colors, required when they are changed from default values
73 canvas->Draw<TObjectDrawable>(TObjectDrawable::kColors);
74
75 // copy custom palette to canvas, will be used for col drawings
76 // style object does not include color settings
77 canvas->Draw<TObjectDrawable>(TObjectDrawable::kPalette);
78
79 // Divide canvas on 2x2 sub-pads to show different draw options
80 auto subpads = canvas->Divide(2,2);
81
82 // draw graph with "AL" option, drawable take over object ownership
83 subpads[0][0]->Draw<TObjectDrawable>(gr, "AL");
84
85 // one can change basic attributes via v7 classes, value will be replaced on client side
86 auto drawth1 = subpads[0][1]->Draw<TObjectDrawable>(th1);
87 drawth1->line = RAttrLine(RColor::kBlue, 3., RAttrLine::kDashed);
88
89 subpads[1][0]->Draw<TObjectDrawable>(th2, "colz");
90
91 // show same object again, but with other draw options
92 subpads[1][1]->Draw<TObjectDrawable>(th2, "lego2");
93
94 // add style, here used to configure TGraph attributes, evaluated only on client side
95 canvas->UseStyle(tobject_style);
96
97 // new window in web browser should popup and async update will be triggered
98 canvas->Show();
99
100 // create SVG file
101 // canvas->SaveAs("tobject.svg");
102
103 // create PNG file
104 // canvas->SaveAs("tobject.png");
105}
@ kRainBow
Definition TColor.h:109
R__EXTERN TStyle * gStyle
Definition TStyle.h:413
Drawing line attributes for different objects.
Definition RAttrLine.hxx:26
virtual void UseStyle(const std::shared_ptr< RStyle > &style)
Provides v7 drawing facilities for TObject types (TGraph, TH1, TH2, etc).
RAttrLine line
! object line attributes
A TGraph is an object made of two arrays X and Y with npoints each.
Definition TGraph.h:41
1-D histogram with an int per channel (see TH1 documentation)}
Definition TH1.h:534
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