Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
draw_legend.cxx
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_v7
3///
4/// This macro generates two TH1D objects and build RLegend
5/// In addition use of auto colors are shown
6///
7/// \macro_code
8///
9/// \date 2019-10-09
10/// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback is welcome!
11/// \author Sergey Linev <s.linev@gsi.de>
12
13/*************************************************************************
14 * Copyright (C) 1995-2019, 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/RCanvas.hxx"
23#include "ROOT/RLegend.hxx"
24#include "TRandom.h"
25
26// macro must be here while cling is not capable to load
27// library automatically for outlined function see ROOT-10336
28R__LOAD_LIBRARY(libROOTHistDraw)
29R__LOAD_LIBRARY(libROOTGraphicsPrimitives)
30
31using namespace ROOT::Experimental;
32
33void draw_legend()
34{
35 // Create the histograms.
36 RAxisConfig xaxis(25, 0., 10.);
37 auto pHist = std::make_shared<RH1D>(xaxis);
38 auto pHist2 = std::make_shared<RH1D>(xaxis);
39
40 for (int n=0;n<1000;n++) {
41 pHist->Fill(gRandom->Gaus(3,0.8));
42 pHist2->Fill(gRandom->Gaus(7,1.2));
43 }
44
45 // Create a canvas to be displayed.
46 auto canvas = RCanvas::Create("Canvas Title");
47
48 // draw histogram
49 auto draw1 = canvas->Draw(pHist);
50 draw1->AttrLine().SetWidth(2).AttrColor().SetAuto();
51
52 // draw histogram
53 auto draw2 = canvas->Draw(pHist2);
54 draw2->AttrLine().SetWidth(4).AttrColor().SetAuto();
55
56 canvas->AssignAutoColors();
57
58 auto legend = canvas->Draw<RLegend>("Legend title");
59 legend->AttrFill().SetStyle(5).SetColor(RColor::kWhite);
60 legend->AttrBorder().SetWidth(2).SetColor(RColor::kRed);
61 legend->AddEntry(draw1, "histo1");
62 legend->AddEntry(draw2, "histo2");
63
64 legend->AddEntry("test").SetAttrLine(RAttrLine().SetColor(RColor::kGreen).SetWidth(5))
65 .SetAttrFill(RAttrFill().SetColor(RColor::kBlue).SetStyle(3004))
66 .SetAttrMarker(RAttrMarker().SetColor(RColor::kRed).SetSize(3).SetStyle(28));
67
68 canvas->SetSize(1000, 700);
69 canvas->Show();
70}
#define R__LOAD_LIBRARY(LIBRARY)
Definition Rtypes.h:472
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
Drawing fill attributes for different objects.
Definition RAttrFill.hxx:27
RAttrFill & SetStyle(int style)
The fill style.
Definition RAttrFill.hxx:35
RAttrFill & SetColor(const RColor &color)
The fill color.
Definition RAttrFill.hxx:39
Drawing line attributes for different objects.
Definition RAttrLine.hxx:27
RAttrLine & SetColor(const RColor &color)
The color of the line.
Definition RAttrLine.hxx:44
RAttrLine & SetWidth(double width)
The width of the line.
Definition RAttrLine.hxx:36
RAttrMarker & SetColor(const RColor &color)
RAttrMarker & SetStyle(int style)
The style of the marker.
RAttrMarker & SetSize(double size)
The size of the marker.
Objects used to configure the different axis types.
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
const Int_t n
Definition legend1.C:16