Loading [MathJax]/extensions/tex2jax.js
Logo ROOT  
Reference Guide
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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)
29
30using namespace ROOT::Experimental;
31
32void draw_legend()
33{
34 // Create the histograms.
35 RAxisConfig xaxis(25, 0., 10.);
36 auto pHist = std::make_shared<RH1D>(xaxis);
37 auto pHist2 = std::make_shared<RH1D>(xaxis);
38
39 for (int n=0;n<1000;n++) {
40 pHist->Fill(gRandom->Gaus(3,0.8));
41 pHist2->Fill(gRandom->Gaus(7,1.2));
42 }
43
44 // Create a canvas to be displayed.
45 auto canvas = RCanvas::Create("Canvas Title");
46
47 // draw histogram
48 auto draw1 = canvas->Draw(pHist);
49 draw1->AttrLine().SetWidth(2).AttrColor().SetAuto();
50
51 // draw histogram
52 auto draw2 = canvas->Draw(pHist2);
53 draw2->AttrLine().SetWidth(4).AttrColor().SetAuto();
54
55 canvas->AssignAutoColors();
56
57 auto legend = canvas->Draw<RLegend>(RPadPos(0.5_normal, 0.6_normal), RPadPos(0.9_normal,0.9_normal), "Legend title");
58 legend->AttrBox().AttrFill().SetStyle(5).SetColor(RColor::kWhite);
59 legend->AttrBox().AttrBorder().SetWidth(2).SetColor(RColor::kRed);
60 legend->AddEntry(draw1, "histo1").SetLine("line_");
61 legend->AddEntry(draw2, "histo2").SetLine("line_");
62
63 canvas->Show();
64}
#define R__LOAD_LIBRARY(LIBRARY)
Definition: Rtypes.h:469
@ kRed
Definition: Rtypes.h:64
@ kWhite
Definition: Rtypes.h:63
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
Objects used to configure the different axis types.
Definition: RAxisConfig.hxx:35
A position (horizontal and vertical) in a RPad.
Definition: RPadPos.hxx:28
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:263
const Int_t n
Definition: legend1.C:16