Logo ROOT  
Reference Guide
draw_v6.cxx File Reference

Detailed Description

This macro shows how ROOT objects like TH1, TH2, TGraph can be drawn in RCanvas.

/*************************************************************************
* Copyright (C) 1995-2017, Rene Brun and Fons Rademakers. *
* All rights reserved. *
* *
* For the licensing terms see $ROOTSYS/LICENSE. *
* For the list of contributors see $ROOTSYS/README/CREDITS. *
*************************************************************************/
#include <ROOT/RPad.hxx>
#include <ROOT/RCanvas.hxx>
#include "TH1.h"
#include "TH2.h"
#include "TGraph.h"
#include "TMath.h"
#include "TStyle.h"
#include <iostream>
using namespace ROOT::Experimental;
auto v6_style = RStyle::Parse("tgraph { line_width: 3; line_color: red; }");
void draw_v6()
{
static constexpr int npoints = 10;
double x[npoints] = { 1., 2., 3., 4., 5., 6., 7., 8., 9., 10. };
double y[npoints] = { .1, .2, .3, .4, .3, .2, .1, .2, .3, .4 };
auto gr = std::make_shared<TGraph>(npoints, x, y);
static constexpr int nth1points = 100;
auto th1 = std::make_shared<TH1I>("gaus", "Example of TH1", nth1points, -5, 5);
th1->SetDirectory(nullptr);
for (int n=0;n<nth1points;++n) {
double x = 10.*n/nth1points-5.;
th1->SetBinContent(n+1, (int) (1000*TMath::Gaus(x)));
}
static constexpr int nth2points = 40;
auto th2 = std::make_shared<TH2I>("gaus2", "Example of TH2", nth2points, -5, 5, nth2points, -5, 5);
th2->SetDirectory(nullptr);
for (int n=0;n<nth2points;++n) {
for (int k=0;k<nth2points;++k) {
double x = 10.*n/nth2points-5.;
double y = 10.*k/nth2points-5.;
th2->SetBinContent(th2->GetBin(n+1, k+1), (int) (1000*TMath::Gaus(x)*TMath::Gaus(y)));
}
}
auto canvas = RCanvas::Create("RCanvas showing a v6 objects");
// add gStyle object, will be applied on JSROOT side
// set on the canvas before any other object is drawn
// add ROOT colors, required when they are changed from default values
// copy custom palette to canvas, will be used for col drawings
// style object does not include color settings
// Divide canvas on 2x2 sub-pads to show different draw options
auto subpads = canvas->Divide(2,2);
subpads[0][0]->Draw<TObjectDrawable>(gr, "AL");
// one can change basic attributes via v7 classes, value will be replaced on client side
subpads[0][1]->Draw<TObjectDrawable>(th1)->AttrLine().SetColor(RColor::kBlue).SetWidth(3).SetStyle(2);
subpads[1][0]->Draw<TObjectDrawable>(th2, "colz");
// show same object again, but with other draw options
subpads[1][1]->Draw<TObjectDrawable>(th2, "lego2");
// add style, here used to configure TGraph attrbutes, evaluated only on client side
canvas->UseStyle(v6_style);
// new window in web browser should popup and async update will be triggered
canvas->Show();
// synchronous, wait until drawing is really finished
canvas->Update(false, [](bool res) { std::cout << "First sync update done = " << (res ? "true" : "false") << std::endl; });
// invalidate canvas and force repainting with next Update()
canvas->Modified();
// call Update again, should return immediately if canvas was not modified
canvas->Update(true, [](bool res) { std::cout << "Second async update done = " << (res ? "true" : "false") << std::endl; });
std::cout << "This message appear normally before second async update" << std::endl;
// create SVG file
// canvas->SaveAs("draw_v6.svg");
// create PNG file
// canvas->SaveAs("draw_v6.png");
}
Date
2017-06-02
Warning
This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback is welcome!
Authors
Axel Naumann axel@.nosp@m.cern.nosp@m..ch, Sergey Linev s.lin.nosp@m.ev@g.nosp@m.si.de

Definition in file draw_v6.cxx.

n
const Int_t n
Definition: legend1.C:16
TPad::Modified
void Modified(Bool_t flag=1) override
Definition: TPad.h:414
TGraph.h
TMath::Gaus
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
TStyle.h
x
Double_t x[n]
Definition: legend1.C:17
ROOT::Experimental::RCanvas::Create
static std::shared_ptr< RCanvas > Create(const std::string &title)
Create new canvas instance.
Definition: RCanvas.cxx:103
RCanvas.hxx
ROOT::Experimental::RColor::kBlue
static constexpr R__DLLEXPORT RGB_t kBlue
Definition: RColor.hxx:186
RPad.hxx
TObjectDrawable.hxx
gStyle
R__EXTERN TStyle * gStyle
Definition: TStyle.h:412
kRainBow
@ kRainBow
Definition: TColor.h:108
gr
TGraphErrors * gr
Definition: legend1.C:25
ROOT::Experimental::TObjectDrawable::kPalette
@ kPalette
list of colors from palette
Definition: TObjectDrawable.hxx:76
ROOT::Experimental::RStyle::Parse
static std::shared_ptr< RStyle > Parse(const std::string &css_code)
Parse CSS code and returns std::shared_ptr<RStyle> when successful.
Definition: RStyle.cxx:260
TCanvas::Show
void Show()
Show canvas.
Definition: TCanvas.cxx:2238
y
Double_t y[n]
Definition: legend1.C:17
TH2.h
ROOT::Experimental::TObjectDrawable::kColors
@ kColors
list of ROOT colors
Definition: TObjectDrawable.hxx:74
TStyle::SetPalette
void SetPalette(Int_t ncolors=kBird, Int_t *colors=0, Float_t alpha=1.)
See TColor::SetPalette.
Definition: TStyle.cxx:1782
ROOT::Experimental::TObjectDrawable::kStyle
@ kStyle
instance of TStyle object
Definition: TObjectDrawable.hxx:75
ROOT::Experimental::TObjectDrawable
Provides v7 drawing facilities for TObject types (TGraph, TH1, TH2, etc).
Definition: TObjectDrawable.hxx:37
th2
auto * th2
Definition: textalign.C:17
th1
auto * th1
Definition: textalign.C:13
TCanvas::Draw
void Draw(Option_t *option="") override
Draw a canvas.
Definition: TCanvas.cxx:843
ROOT::Experimental
Definition: RDirectory.hxx:30
TPad::Divide
void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0) override
Automatic pad generation by division.
Definition: TPad.cxx:1178
TH1.h
TCanvas::Update
void Update() override
Update canvas pad buffers.
Definition: TCanvas.cxx:2502
TMath.h