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" };
26 double frame_left = 0.15, frame_right = 0.9,
27 frame_top = 0.8, frame_bottom = 0.05;
29 auto nbins = negative.size();
31 auto hmain =
new TH1D(
"hmain",
"title", nbins, 0, nbins);
32 hmain->SetMarkerSize(3);
33 hmain->SetMarkerStyle(33);
34 hmain->SetMarkerColor(
kBlue);
36 auto hpos =
new TH1D(
"hpos",
"title", nbins, 0, nbins);
37 hpos->SetFillColor(
kRed);
38 hpos->SetBarOffset(0.2);
39 hpos->SetBarWidth(0.6);
41 auto hneg =
new TH1D(
"hneg",
"title", nbins, 0, nbins);
42 hneg->SetFillColor(
kGreen);
43 hneg->SetBarOffset(0.2);
44 hneg->SetBarWidth(0.6);
46 double vmin = 0, vmax = 0;
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];
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;
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);
70 auto c1 =
new TCanvas(
"chaxis",
"title", 1500, 800);
72 if (!
gROOT->IsBatch() && !
c1->IsWeb())
73 ::Warning(
"haxis.cxx",
"macro may not work without enabling web-based canvas");
75 c1->SetLeftMargin(frame_left);
76 c1->SetRightMargin(1 - frame_right);
77 c1->SetTopMargin(1 - frame_top);
78 c1->SetBottomMargin(frame_bottom);
82 c1->Add(haxis,
"haxisg;y+");
84 c1->Add(hpos,
"bar,base0,same");
85 c1->Add(hneg,
"bar,base0,same");
86 c1->Add(hmain,
"P,same");
88 TLatex *title =
new TLatex((frame_left + frame_right)*0.5, 0.96,
"Example of haxis with overlayed histograms");
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;
102 std::vector<double>
ypos = { y0 + wy, y0 + wy, y0 + wy*1.5, y0, y0 - wy*1.5, y0 - wy, y0 - wy, y0 + wy };
110 l->SetTextAlign(left_side ? 32 : 12);
111 l->SetTextSize(0.03);
116 add_arrow(
true,
"Reduction");
118 add_arrow(
false,
"Increase");
120 auto box =
new TBox(0.2, 6, 0.8, 14);
123 box->SetLineWidth(3);
125 c1->Add(
box,
"l,frame");
127 auto l1 =
new TLatex(0.5, 10,
"Text inside frame");
128 l1->SetTextAlign(22);
129 l1->SetTextSize(0.04);
132 c1->Add(l1,
"frame");
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
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
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
virtual void SetTextAlign(Short_t align=11)
Set the text alignment.
virtual void SetTextColor(Color_t tcolor=1)
Set the text color.
virtual void SetTextSize(Float_t tsize=1)
Set the text size.
1-D histogram with a double per channel (see TH1 documentation)
To draw Mathematical Formula.
virtual void SetNDC(Bool_t isNDC=kTRUE)
Set NDC mode on if isNDC = kTRUE, off otherwise.
Defined by an array on N points in a 2-D space.
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.
void box(Int_t pat, Double_t x1, Double_t y1, Double_t x2, Double_t y2)