Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
tobject.cxx File Reference

Detailed Description

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

/*************************************************************************
* Copyright (C) 1995-2021, 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"
using namespace ROOT::Experimental;
// create RStyle here to keep reference alive after leaving tobject() function scope
auto tobject_style = RStyle::Parse("tgraph { line_width: 3; line_color: red; }");
void tobject()
{
static constexpr int npoints = 10;
static constexpr int nth1points = 100;
static constexpr int nth2points = 40;
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 = new TGraph(npoints, x, y);
// create normal object to be able draw it once
auto th1 = new TH1I("gaus", "Example of TH1", nth1points, -5, 5);
// it is recommended to set directory to nullptr, but it is also automatically done in TObjectDrawable
// th1->SetDirectory(nullptr);
th1->FillRandom("gaus", 5000);
// use std::shared_ptr<TH2I> to let draw same histogram twice with different draw options
auto th2 = std::make_shared<TH2I>("gaus2", "Example of TH2", nth2points, -5, 5, nth2points, -5, 5);
// is is highly recommended to set directory to nullptr to avoid ownership conflicts
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 TObject-derived classes");
// add gStyle object, will be applied on JSROOT side
// set on the canvas before any other object is drawn
canvas->Draw<TObjectDrawable>(TObjectDrawable::kStyle);
// add ROOT colors, required when they are changed from default values
canvas->Draw<TObjectDrawable>(TObjectDrawable::kColors);
// copy custom palette to canvas, will be used for col drawings
// style object does not include color settings
canvas->Draw<TObjectDrawable>(TObjectDrawable::kPalette);
// Divide canvas on 2x2 sub-pads to show different draw options
auto subpads = canvas->Divide(2,2);
// draw graph with "AL" option, drawable take over object ownership
subpads[0][0]->Draw<TObjectDrawable>(gr, "AL");
// one can change basic attributes via v7 classes, value will be replaced on client side
auto drawth1 = subpads[0][1]->Draw<TObjectDrawable>(th1);
drawth1->line = RAttrLine(RColor::kBlue, 3., RAttrLine::kDashed);
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 attributes, evaluated only on client side
canvas->UseStyle(tobject_style);
// new window in web browser should popup and async update will be triggered
canvas->Show();
// create SVG file
// canvas->SaveAs("tobject.svg");
// create PNG file
// canvas->SaveAs("tobject.png");
}
@ kRainBow
Definition TColor.h:115
R__EXTERN TStyle * gStyle
Definition TStyle.h:433
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:539
void SetPalette(Int_t ncolors=kBird, Int_t *colors=nullptr, Float_t alpha=1.)
See TColor::SetPalette.
Definition TStyle.cxx:1884
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)
Calculates a gaussian function with mean and sigma.
Definition TMath.cxx:471
auto * th2
Definition textalign.C:18
auto * th1
Definition textalign.C:14
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 tobject.cxx.