Logo ROOT  
Reference Guide
draw_legend.cxx File Reference

Detailed Description

This macro generates two TH1D objects and build RLegend In addition use of auto colors are shown.

/*************************************************************************
* Copyright (C) 1995-2019, 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/RCanvas.hxx"
#include "ROOT/RLegend.hxx"
#include "TRandom.h"
// macro must be here while cling is not capable to load
// library automatically for outlined function see ROOT-10336
R__LOAD_LIBRARY(libROOTHistDraw)
R__LOAD_LIBRARY(libROOTGraphicsPrimitives)
using namespace ROOT::Experimental;
void draw_legend()
{
// Create the histograms.
RAxisConfig xaxis(25, 0., 10.);
auto pHist = std::make_shared<RH1D>(xaxis);
auto pHist2 = std::make_shared<RH1D>(xaxis);
for (int n=0;n<1000;n++) {
pHist->Fill(gRandom->Gaus(3,0.8));
pHist2->Fill(gRandom->Gaus(7,1.2));
}
// Create a canvas to be displayed.
auto canvas = RCanvas::Create("Canvas Title");
// add palette to canvas, it will not be seen on the canvas but used for colors
canvas->Draw<RPaletteDrawable>(RPalette({{0., RColor::kWhite}, {.3, RColor::kRed}, {.7, RColor::kBlue}, {1., RColor::kBlack}}), false);
// draw first histogram
auto draw1 = canvas->Draw(pHist);
draw1->AttrLine().SetWidth(2).SetColor(0.3); // should be red color
// draw second histogram
auto draw2 = canvas->Draw(pHist2);
draw2->AttrLine().SetWidth(4).SetColor(0.7); // should be blue color
auto legend = canvas->Draw<RLegend>("Legend title");
legend->AttrFill().SetStyle(5).SetColor(RColor::kWhite);
legend->AttrBorder().SetWidth(2).SetColor(RColor::kRed);
legend->AddEntry(draw1, "histo1");
legend->AddEntry(draw2, "histo2");
legend->AddEntry("test").SetAttrLine(RAttrLine().SetColor(RColor::kGreen).SetWidth(5))
.SetAttrFill(RAttrFill().SetColor(RColor::kBlue).SetStyle(3004))
.SetAttrMarker(RAttrMarker().SetColor(RColor::kRed).SetSize(3).SetStyle(28));
canvas->SetSize(1000, 700);
canvas->Show();
}
Date
2019-10-09
Warning
This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback is welcome!
Author
Sergey Linev s.lin.nosp@m.ev@g.nosp@m.si.de

Definition in file draw_legend.cxx.

n
const Int_t n
Definition: legend1.C:16
TRandom::Gaus
virtual Double_t Gaus(Double_t mean=0, Double_t sigma=1)
Samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigm...
Definition: TRandom.cxx:274
ROOT::Experimental::RAxisConfig
Objects used to configure the different axis types.
Definition: RAxisConfig.hxx:35
RHistDrawable.hxx
ROOT::Experimental::RColor::kBlack
static constexpr R__DLLEXPORT RGB_t kBlack
Definition: RColor.hxx:178
ROOT::Experimental::RAttrMarker::SetColor
RAttrMarker & SetColor(const RColor &color)
Definition: RAttrMarker.hxx:34
ROOT::Experimental::RAttrMarker
A marker attributes.
Definition: RAttrMarker.hxx:26
ROOT::Experimental::RColor::kWhite
static constexpr R__DLLEXPORT RGB_t kWhite
Definition: RColor.hxx:193
ROOT::Experimental::RAttrLine
Drawing line attributes for different objects.
Definition: RAttrLine.hxx:26
TRandom.h
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::RAttrFill::SetColor
RAttrFill & SetColor(const RColor &color)
The fill color.
Definition: RAttrFill.hxx:38
ROOT::Experimental::RColor::kBlue
static constexpr R__DLLEXPORT RGB_t kBlue
Definition: RColor.hxx:186
ROOT::Experimental::RAttrMarker::SetStyle
RAttrMarker & SetStyle(int style)
The style of the marker.
Definition: RAttrMarker.hxx:42
ROOT::Experimental::RAttrLine::SetWidth
RAttrLine & SetWidth(double width)
The width of the line.
Definition: RAttrLine.hxx:35
RPaletteDrawable.hxx
ROOT::Experimental::RAttrMarker::SetSize
RAttrMarker & SetSize(double size)
The size of the marker.
Definition: RAttrMarker.hxx:38
TLegend::AddEntry
TLegendEntry * AddEntry(const TObject *obj, const char *label="", Option_t *option="lpf")
Add a new entry to this legend.
Definition: TLegend.cxx:330
TCanvas::Show
void Show()
Show canvas.
Definition: TCanvas.cxx:2238
gRandom
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
ROOT::Experimental::RColor::kGreen
static constexpr R__DLLEXPORT RGB_t kGreen
Definition: RColor.hxx:179
ROOT::Experimental::RAttrFill
Drawing fill attributes for different objects.
Definition: RAttrFill.hxx:26
ROOT::Experimental::RAttrLine::SetColor
RAttrLine & SetColor(const RColor &color)
The color of the line.
Definition: RAttrLine.hxx:43
ROOT::Experimental::RAttrFill::SetStyle
RAttrFill & SetStyle(int style)
The fill style.
Definition: RAttrFill.hxx:34
TCanvas::Draw
void Draw(Option_t *option="") override
Draw a canvas.
Definition: TCanvas.cxx:843
ROOT::Experimental
Definition: RDirectory.hxx:30
ROOT::Experimental::RPalette
A set of colors.
Definition: RPalette.hxx:38
ROOT::Experimental::RPaletteDrawable
A color palette draw near the frame.
Definition: RPaletteDrawable.hxx:32
R__LOAD_LIBRARY
#define R__LOAD_LIBRARY(LIBRARY)
Definition: Rtypes.h:472
ROOT::Experimental::RColor::kRed
static constexpr R__DLLEXPORT RGB_t kRed
Definition: RColor.hxx:191