Logo ROOT  
Reference Guide
draw_frame.cxx
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_v7
3///
4/// \macro_code
5///
6/// \date 2020-02-20
7/// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback
8/// is welcome!
9/// \author Sergey Linev <s.linev@gsi.de>
10
11/*************************************************************************
12 * Copyright (C) 1995-2020, Rene Brun and Fons Rademakers. *
13 * All rights reserved. *
14 * *
15 * For the licensing terms see $ROOTSYS/LICENSE. *
16 * For the list of contributors see $ROOTSYS/README/CREDITS. *
17 *************************************************************************/
18
19#include "ROOT/RCanvas.hxx"
20#include "ROOT/RColor.hxx"
21#include "ROOT/RFrameTitle.hxx"
22#include "ROOT/RFrame.hxx"
24#include "ROOT/RStyle.hxx"
25#include "ROOT/RPad.hxx"
26
27// macro must be here while cling is not capable to load
28// library automatically for outlined function see ROOT-10336
29R__LOAD_LIBRARY(libROOTHistDraw)
30
31using namespace ROOT::Experimental;
32
33auto style = std::make_shared<RStyle>(); // keep here to avoid destroy when leaving function scope
34
35void draw_frame()
36{
37 // Create the histogram.
38 RAxisConfig xaxis("x", 10, 0.1, 110.);
39 RAxisConfig yaxis("y", {0., 1., 2., 3., 10.});
40 auto pHist = std::make_shared<RH2D>(xaxis, yaxis);
41
42 // Fill a few points.
43 pHist->Fill({0.11, 1.02});
44 pHist->Fill({5.54, 3.02});
45 pHist->Fill({20.98, 1.02});
46 pHist->Fill({20.90, 1.02});
47 pHist->Fill({1.75, -0.02});
48
49 // Create a canvas to be displayed.
50 auto canvas = RCanvas::Create("Canvas Title");
51
52 // Make divisions
53 auto subpads = canvas->Divide(2,1);
54
55 // configure RFrame with direct API calls
56 auto frame1 = subpads[0][0]->GetOrCreateFrame();
57 frame1->AttrFill().SetColor(RColor::kBlue);
58 frame1->AttrBorder().SetColor(RColor::kRed);
59 frame1->AttrBorder().SetWidth(3);
60 frame1->Margins().SetTop(0.3_normal);
61 frame1->Margins().SetBottom(0.1_normal);
62 frame1->Margins().SetLeft(0.2_normal);
63 frame1->Margins().SetRight(0.2_normal);
64
65 frame1->AttrX().AttrLine().SetColor(RColor::kGreen);
66 frame1->AttrY().AttrLine().SetColor(RColor::kBlue);
67
68 frame1->AttrX().SetLog(true);
69 frame1->AttrX().SetZoomMinMax(2.,80.);
70 frame1->AttrY().SetMinMax(2,8);
71
72 subpads[0][0]->Draw<RFrameTitle>("Frame1 title")->SetMargin(0.01_normal).SetHeight(0.05_normal);
73
74 auto draw1 = subpads[0][0]->Draw(pHist);
75
76 // create frame before drawing histograms
77 auto frame2 = subpads[1][0]->GetOrCreateFrame();
78
79 auto draw2 = subpads[1][0]->Draw(pHist);
80
81 subpads[1][0]->Draw<RFrameTitle>("Frame2 title");
82
83 style->ParseString("frame { margin_left: 0.3; margin_right: 0.3; x_line_color_name: blue; y_line_color_name: green; } \n title { margin: 0.02; height: 0.1; text_size: 20; }");
84
85 subpads[1][0]->UseStyle(style);
86
87 canvas->Show("");
88}
#define R__LOAD_LIBRARY(LIBRARY)
Definition: Rtypes.h:469
@ kRed
Definition: Rtypes.h:64
@ kGreen
Definition: Rtypes.h:64
@ kBlue
Definition: Rtypes.h:64
Objects used to configure the different axis types.
Definition: RAxisConfig.hxx:35
void Show()
Definition: TCanvas.h:218
TCanvas * style()
Definition: style.C:1