Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files 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 
21 #include "ROOT/RHistDrawable.hxx"
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
28 R__LOAD_LIBRARY(libROOTHistDraw)
29 
30 using namespace ROOT::Experimental;
31 
32 void 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).Color().SetAuto();
50 
51  // draw histogram
52  auto draw2 = canvas->Draw(pHist2);
53  draw2->AttrLine().SetWidth(4).Color().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 }