Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
haxis.cxx
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_webcanv
3/// \notebook -js
4/// Swap X/Y axes drawing and use to draw TH1 as bar and as markers.
5///
6/// Option "haxisg;y+" draw histogram axis as for "hbar" plus allow to draw grids plus draw Y labels on other side
7/// Option "bar,base0,same" draws histogram as bars with 0 as reference value
8/// Option "P,same" draws histogram as markers
9/// Macro also shows how frame margins can be configured and poly-line drawing like filled arrow can be
10/// placed relative to frame.
11///
12/// Functionality available only in web-based graphics
13///
14/// \macro_image (tcanvas_js)
15/// \macro_code
16///
17/// \author Sergey Linev
18
19void haxis()
20{
21 std::vector<double> negative = { -5, -7, -3, -1 };
22 std::vector<double> positive = { 2, 15, 2, 10 };
23 std::vector<std::string> labels = { "Category 1", "Category 2", "Category 3", "Category 4" };
24
25 // position of frame
26 double frame_left = 0.15, frame_right = 0.9,
27 frame_top = 0.8, frame_bottom = 0.05;
28
29 auto nbins = negative.size();
30
31 auto hmain = new TH1D("hmain", "title", nbins, 0, nbins);
32 hmain->SetMarkerSize(3);
33 hmain->SetMarkerStyle(33);
34 hmain->SetMarkerColor(kBlue);
35
36 auto hpos = new TH1D("hpos", "title", nbins, 0, nbins);
37 hpos->SetFillColor(kRed);
38 hpos->SetBarOffset(0.2);
39 hpos->SetBarWidth(0.6);
40
41 auto hneg = new TH1D("hneg", "title", nbins, 0, nbins);
42 hneg->SetFillColor(kGreen);
43 hneg->SetBarOffset(0.2);
44 hneg->SetBarWidth(0.6);
45
46 double vmin = 0, vmax = 0;
47
48 for (unsigned n = 0; n < nbins; n++) {
49 hmain->SetBinContent(n + 1, negative[n] + positive[n]);
50 hpos->SetBinContent(n + 1, positive[n]);
51 hneg->SetBinContent(n + 1, negative[n]);
52 if (negative[n] < vmin) vmin = negative[n];
53 if (positive[n] > vmax) vmax = positive[n];
54 }
55
56 double scale_min = (vmin - (vmax-vmin) * 0.1),
57 scale_max = (vmax + (vmax-vmin) * 0.1),
58 frame_0 = (0 - scale_min) / (scale_max - scale_min) * (frame_right - frame_left) + frame_left;
59
60 auto haxis = new TH1D("haxis", "title", nbins, 0, nbins);
61 haxis->SetMinimum(scale_min);
62 haxis->SetMaximum(scale_max);
63 haxis->SetStats(false);
64 for (unsigned n = 0; n < nbins; n++)
65 haxis->GetXaxis()->SetBinLabel(n + 1, labels[n].c_str());
66 haxis->GetXaxis()->SetTickSize(0);
67 haxis->GetXaxis()->SetLabelSize(0.07);
68 haxis->GetXaxis()->SetLabelOffset(0.02);
69
70 auto c1 = new TCanvas("chaxis", "title", 1500, 800);
71
72 if (!gROOT->IsBatch() && !c1->IsWeb())
73 ::Warning("haxis.cxx", "macro may not work without enabling web-based canvas");
74
75 c1->SetLeftMargin(frame_left);
76 c1->SetRightMargin(1 - frame_right);
77 c1->SetTopMargin(1 - frame_top);
78 c1->SetBottomMargin(frame_bottom);
79 c1->SetGridy(1);
80 //c1->SetGridx(1);
81
82 c1->Add(haxis, "haxisg;y+"); // swap x/y axis, let draw grids, y on top
83
84 c1->Add(hpos, "bar,base0,same"); // draw as bar, 0 as base, same
85 c1->Add(hneg, "bar,base0,same"); // draw as bar, 0 as base, same
86 c1->Add(hmain, "P,same"); // draw as marker, also text and line supported, same
87
88 TLatex *title = new TLatex((frame_left + frame_right)*0.5, 0.96, "Example of haxis with overlayed histograms");
89 title->SetNDC(true);
90 title->SetTextAlign(22);
91 title->SetTextSize(0.08);
92 title->SetTextColor(kBlue);
93 c1->Add(title);
94
95 auto add_arrow = [&](bool left_side, const char *txt) {
96 double x1 = left_side ? frame_0 - 0.02 : frame_0 + 0.02,
97 x2 = left_side ? frame_left + 0.05 : frame_right - 0.05,
98 x3 = left_side ? frame_left + 0.03 : frame_right - 0.03,
99 y0 = frame_top + 0.08, wy = 0.02;
100
101 std::vector<double> xpos = { x1, x2, x2, x3, x2, x2, x1, x1 };
102 std::vector<double> ypos = { y0 + wy, y0 + wy, y0 + wy*1.5, y0, y0 - wy*1.5, y0 - wy, y0 - wy, y0 + wy };
103 TPolyLine *pleft = new TPolyLine(xpos.size(),xpos.data(),ypos.data());
104 pleft->SetFillColor(left_side ? kGreen : kRed);
105 pleft->SetNDC();
106 c1->Add(pleft, "f");
107
108 TLatex *l = new TLatex(left_side ? x1 - 0.03 : x1 + 0.03, y0, txt);
109 l->SetNDC(true);
110 l->SetTextAlign(left_side ? 32 : 12);
111 l->SetTextSize(0.03);
112 l->SetTextColor(kWhite);
113 c1->Add(l);
114 };
115
116 add_arrow(true, "Reduction");
117
118 add_arrow(false, "Increase");
119
120 auto box = new TBox(0.2, 6, 0.8, 14);
121 box->SetFillColor(kCyan);
122 box->SetLineColor(kRed);
123 box->SetLineWidth(3);
124 // use "frame" option to embed box into frame, "l" for line drawing
125 c1->Add(box, "l,frame");
126
127 auto l1 = new TLatex(0.5, 10, "Text inside frame");
128 l1->SetTextAlign(22);
129 l1->SetTextSize(0.04);
130 l1->SetTextColor(kMagenta);
131 // use "frame" option to embed text into frame
132 c1->Add(l1, "frame");
133}
@ kRed
Definition Rtypes.h:66
@ kGreen
Definition Rtypes.h:66
@ kMagenta
Definition Rtypes.h:66
@ kWhite
Definition Rtypes.h:65
@ kCyan
Definition Rtypes.h:66
@ kBlue
Definition Rtypes.h:66
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Definition TError.cxx:229
Option_t Option_t TPoint TPoint const char x2
Option_t Option_t TPoint TPoint const char x1
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void xpos
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void ypos
#define gROOT
Definition TROOT.h:406
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:37
virtual void SetTextAlign(Short_t align=11)
Set the text alignment.
Definition TAttText.h:42
virtual void SetTextColor(Color_t tcolor=1)
Set the text color.
Definition TAttText.h:44
virtual void SetTextSize(Float_t tsize=1)
Set the text size.
Definition TAttText.h:47
Create a Box.
Definition TBox.h:22
The Canvas class.
Definition TCanvas.h:23
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:671
To draw Mathematical Formula.
Definition TLatex.h:18
virtual void SetNDC(Bool_t isNDC=kTRUE)
Set NDC mode on if isNDC = kTRUE, off otherwise.
Definition TLine.cxx:467
Defined by an array on N points in a 2-D space.
Definition TPolyLine.h:23
virtual void SetNDC(Bool_t isNDC=kTRUE)
Set NDC mode on if isNDC = kTRUE, off otherwise.
virtual void SetNDC(Bool_t isNDC=kTRUE)
Set NDC mode on if isNDC = kTRUE, off otherwise.
Definition TText.cxx:823
void box(Int_t pat, Double_t x1, Double_t y1, Double_t x2, Double_t y2)
Definition fillpatterns.C:1
return c1
Definition legend1.C:41
const Int_t n
Definition legend1.C:16
TLine l
Definition textangle.C:4