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, frame_top = 0.8, frame_bottom = 0.05;
28 auto nbins = negative.size();
30 auto hmain =
new TH1D(
"hmain",
"title", nbins, 0, nbins);
31 hmain->SetMarkerSize(3);
32 hmain->SetMarkerStyle(33);
33 hmain->SetMarkerColor(
kBlue);
35 auto hpos =
new TH1D(
"hpos",
"title", nbins, 0, nbins);
36 hpos->SetFillColor(
kRed);
37 hpos->SetBarOffset(0.2);
38 hpos->SetBarWidth(0.6);
40 auto hneg =
new TH1D(
"hneg",
"title", nbins, 0, nbins);
41 hneg->SetFillColor(
kGreen);
42 hneg->SetBarOffset(0.2);
43 hneg->SetBarWidth(0.6);
45 double vmin = 0, vmax = 0;
47 for (
unsigned n = 0;
n < nbins;
n++) {
48 hmain->SetBinContent(
n + 1, negative[
n] + positive[
n]);
49 hpos->SetBinContent(
n + 1, positive[
n]);
50 hneg->SetBinContent(
n + 1, negative[
n]);
51 if (negative[
n] < vmin)
53 if (positive[
n] > vmax)
57 double scale_min = (vmin - (vmax - vmin) * 0.1), 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");
90 title->SetTextAlign(22);
91 title->SetTextSize(0.08);
95 auto add_arrow = [&](
bool left_side,
const char *txt) {
96 double x1 = left_side ? frame_0 - 0.02 : frame_0 + 0.02, x2 = left_side ? frame_left + 0.05 : frame_right - 0.05,
97 x3 = left_side ? frame_left + 0.03 : frame_right - 0.03, y0 = frame_top + 0.08, wy = 0.02;
99 std::vector<double> xpos = {x1, x2, x2, x3, x2, x2, x1, x1};
100 std::vector<double> ypos = {y0 + wy, y0 + wy, y0 + wy * 1.5, y0, y0 - wy * 1.5, y0 - wy, y0 - wy, y0 + wy};
106 TLatex *
l =
new TLatex(left_side ? x1 - 0.03 : x1 + 0.03, y0, txt);
108 l->SetTextAlign(left_side ? 32 : 12);
109 l->SetTextSize(0.03);
114 add_arrow(
true,
"Reduction");
116 add_arrow(
false,
"Increase");
118 auto box =
new TBox(0.2, 6, 0.8, 14);
121 box->SetLineWidth(3);
123 c1->Add(
box,
"l,frame");
125 auto l1 =
new TLatex(0.5, 10,
"Text inside frame");
126 l1->SetTextAlign(22);
127 l1->SetTextSize(0.04);
130 c1->Add(l1,
"frame");
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
1-D histogram with a double per channel (see TH1 documentation)
virtual void SetNDC(Bool_t isNDC=kTRUE)
void box(Int_t pat, Double_t x1, Double_t y1, Double_t x2, Double_t y2)