BmnRoot
Loading...
Searching...
No Matches
BmnDrawHist.cxx
Go to the documentation of this file.
1
6#include "BmnDrawHist.h"
7
8#include "TH1.h"
9#include "TH2.h"
10#include "TH1D.h"
11#include "TPad.h"
12#include "TLegend.h"
13#include "TStyle.h"
14#include "TGraph.h"
15#include "TGraph2D.h"
16#include "TMath.h"
17#include "TGaxis.h"
18
19#include <string>
20#include <limits>
21#include <iostream>
22#include <sstream>
23#include <cassert>
24
25using std::stringstream;
26
27/* Set default styles for histograms. */
29 gStyle->SetOptStat("erm");
30 gStyle->SetOptFit(1);
31 gStyle->SetOptTitle(0);
32
33 gStyle->SetCanvasColor(kWhite);
34 gStyle->SetFrameFillColor(kWhite);
35 gStyle->SetFrameBorderMode(0);
36 gStyle->SetPadColor(kWhite);
37 gStyle->SetStatColor(kWhite);
38 gStyle->SetTitleFillColor(kWhite);
39 gStyle->SetPalette(77);
40}
41
42/* Draw TH1 histogram.*/
43void DrawH1(
44 TH1* hist,
45 HistScale logx,
46 HistScale logy,
47 const string& drawOpt,
48 Int_t lineColor,
49 Int_t lineWidth,
50 Int_t lineStyle,
51 Float_t markerSize,
52 Int_t markerStyle,
53 Int_t fillColor) {
54 Double_t textSize = BmnDrawingOptions::TextSize();
55 hist->SetLineColor(lineColor);
56 hist->SetLineWidth(lineWidth);
57 hist->SetLineStyle(lineStyle);
58 hist->SetMarkerColor(lineColor);
59 hist->SetMarkerSize(markerSize);
60 hist->SetMarkerStyle(markerStyle);
61 if (logx == kLog) {
62 gPad->SetLogx();
63 }
64 if (logy == kLog) {
65 gPad->SetLogy();
66 }
67 hist->GetXaxis()->SetLabelSize(textSize);
68 hist->GetXaxis()->SetNdivisions(505, kTRUE);
69 hist->GetYaxis()->SetLabelSize(textSize);
70 hist->GetXaxis()->SetTitleSize(textSize);
71 hist->GetYaxis()->SetTitleSize(textSize);
72 hist->GetXaxis()->SetTitleOffset(1.0);
73 hist->GetYaxis()->SetTitleOffset(1.3);
74 gPad->SetLeftMargin(0.17);
75 gPad->SetBottomMargin(0.15);
76 gPad->SetTicks(1, 1);
77 if (fillColor > 0) hist->SetFillColor(fillColor);
78 hist->Draw(drawOpt.c_str());
79 gPad->SetGrid(true, true);
80 hist->SetStats(true);
81}
82
83/* Draw TH2 histogram.*/
84void DrawH2(
85 TH2* hist,
86 HistScale logx,
87 HistScale logy,
88 HistScale logz,
89 const string& drawOpt) {
90 Double_t textSize = BmnDrawingOptions::TextSize();
91 if (logx == kLog) {
92 gPad->SetLogx();
93 }
94 if (logy == kLog) {
95 gPad->SetLogy();
96 }
97 if (logz == kLog) {
98 gPad->SetLogz();
99 }
100 hist->GetXaxis()->SetLabelSize(textSize);
101 hist->GetXaxis()->SetNdivisions(505, kTRUE);
102 hist->GetYaxis()->SetLabelSize(textSize);
103 hist->GetYaxis()->SetNdivisions(505, kTRUE);
104 hist->GetZaxis()->SetLabelSize(textSize);
105 // hist->GetZaxis()->SetNdivisions(505, kTRUE);
106 hist->GetXaxis()->SetTitleSize(textSize);
107 hist->GetYaxis()->SetTitleSize(textSize);
108 hist->GetZaxis()->SetTitleSize(textSize);
109 hist->GetXaxis()->SetTitleOffset(1.0);
110 hist->GetYaxis()->SetTitleOffset(1.3);
111 hist->GetZaxis()->SetTitleOffset(1.5);
112 gPad->SetLeftMargin(0.17);
113 gPad->SetRightMargin(0.30);
114 gPad->SetBottomMargin(0.15);
115 gPad->SetTicks(1, 1);
116 hist->Draw(drawOpt.c_str());
117 gPad->SetGrid(true, true);
118 hist->SetStats(true);
119}
120
121/* Draw several TH1 histograms. */
123 const vector<TH1*>& histos,
124 const vector<string>& histLabels,
125 HistScale logx,
126 HistScale logy,
127 Bool_t drawLegend,
128 Double_t x1,
129 Double_t y1,
130 Double_t x2,
131 Double_t y2,
132 const string& drawOpt,
133 Bool_t outputMeanValue01) {
134 assert(histos.size() != 0 && histLabels.size() == histos.size());
135 Double_t max = std::numeric_limits<Double_t>::min();
136 Int_t nofHistos = histos.size();
137 TLegend* legend = new TLegend(x1, y1, x2, y2);
138 legend->SetFillColor(kWhite);
139 for (Int_t iHist = 0; iHist < nofHistos; iHist++) {
140 TH1* hist = histos[iHist];
141 string opt = (iHist == 0) ? drawOpt : (iHist == nofHistos - 1) ? "SAME" + drawOpt : "SAME" + drawOpt;
142 //string opt = drawOpt;
143 DrawH1(hist, logx, logy, opt, BmnDrawingOptions::Color(iHist), BmnDrawingOptions::LineWidth(),
145 max = std::max(max, hist->GetMaximum());
146 Int_t nonZeroBins = 0;
147 for (Int_t i = 0; i < hist->GetNbinsX(); ++i) {
148 if (hist->GetBinContent(i) != 0.0) nonZeroBins++;
149 }
150
151 Double_t xMax = hist->GetBinCenter(hist->GetNbinsX());
152 Double_t xGev = 1.0; //1 GeV
153 Int_t nGev = Int_t(hist->GetNbinsX() * xGev / xMax);
154 Double_t effAll = hist->Integral() / nonZeroBins;
155 Double_t eff01 = hist->Integral(0.0, nGev) / nGev; // efficiency from 0 to 1 GeV
156 TString legText;
157 if (outputMeanValue01) legText = TString::Format("%s | mean = %3.1f | mean01 = %3.1f", histLabels[iHist].c_str(), effAll, eff01).Data();
158 else legText = TString::Format("%s | mean = %3.1f", histLabels[iHist].c_str(), effAll).Data();
159 legend->AddEntry(hist, legText, "lp");
160 //To choose the best combination of parameters
161 FILE* f = fopen("test.dat", "a+");
162 if (((TString) hist->GetName()).Contains("vs_P")) fprintf(f, "%s %s %f %f\n", hist->GetName(), histLabels[iHist].c_str(), effAll, eff01);
163 fclose(f);
164 }
165 histos[0]->SetMaximum(max * 1.10);
166
167 if (drawLegend) {
168 legend->Draw();
169 }
170}
171
173//void DrawH1(
174// const vector<TH1*>& histos,
175// const vector<string>& histLabels,
176// HistScale logx,
177// HistScale logy,
178// Bool_t drawLegend,
179// Double_t x1,
180// Double_t y1,
181// Double_t x2,
182// Double_t y2,
183// const string& drawOpt,
184// Bool_t drawMeanLine,
185// Bool_t printMeanValue) {
186// assert(histos.size() != 0 && histLabels.size() == histos.size());
187// Double_t max = std::numeric_limits<Double_t>::min();
188// Int_t nofHistos = histos.size();
189// TLegend* legend = new TLegend(x1, y1, x2, y2);
190// legend->SetFillColor(kWhite);
191// for (UInt_t iHist = 0; iHist < nofHistos; iHist++) {
192// TH1* hist = histos[iHist];
193// string opt = (iHist == 0) ? drawOpt : (iHist == nofHistos - 1) ? "SAME" + drawOpt : "SAME" + drawOpt;
194// //string opt = drawOpt;
195// DrawH1(hist, logx, logy, opt, BmnDrawingOptions::Color(iHist), BmnDrawingOptions::LineWidth(),
196// BmnDrawingOptions::LineStyle(0), BmnDrawingOptions::MarkerSize(), BmnDrawingOptions::MarkerStyle(iHist));
197// max = std::max(max, hist->GetMaximum());
198// Int_t nonZeroBins = 0;
199// for (Int_t i = 0; i < hist->GetNbinsX(); ++i) {
200// if (hist->GetBinContent(i) != 0.0) nonZeroBins++;
201// }
202// if (printMeanValue == kTRUE) {
203// legend->AddEntry(hist, TString::Format("%s | mean = %3.1f", histLabels[iHist].c_str(), hist->Integral() / nonZeroBins).Data(), "lp");
204//
205// } else {
206// Double_t xMax = hist->GetBinCenter(hist->GetNbinsX());
207// Double_t xGev = 1.0; //1 GeV
208// Int_t nGev = Int_t(hist->GetNbinsX() * xGev / xMax);
209// Double_t effAll = hist->Integral() / nonZeroBins;
210// Double_t eff01 = hist->Integral(0.0, nGev) / nGev; // efficiency from 0 to 1 GeV
211// legend->AddEntry(hist, TString::Format("%s | mean = %3.1f | mean01 = %3.1f", histLabels[iHist].c_str(), effAll, eff01).Data(), "lp");
212// FILE* f = fopen("test.dat", "a+");
213// if (((TString) hist->GetName()).Contains("Eff_vs_P_glob") ||
214// ((TString) hist->GetName()).Contains("Fake_vs_P_glob") ||
215// ((TString) hist->GetName()).Contains("SplitEff_vs_P_glob"))
216// fprintf(f, "%s %s %f\n", hist->GetName(), histLabels[iHist].c_str(), hist->Integral() / nonZeroBins);
217// fclose(f);
218// }
219// if (drawMeanLine == kTRUE) DrawMeanLine(hist);
220// }
221// histos[0]->SetMaximum(max * 1.10);
222//
223// if (drawLegend) {
224// legend->Draw();
225// }
226//}
227
228void DrawMeanLine(TH1* hist) {
229
230 Float_t minX = hist->GetXaxis()->GetXmin();
231 Float_t maxX = hist->GetXaxis()->GetXmax();
232 Int_t nonZeroBins = 0;
233 for (Int_t i = 0; i < hist->GetNbinsX(); ++i) {
234 if (hist->GetBinContent(i) != 0.0) nonZeroBins++;
235 }
236 TLine* line = new TLine(minX, hist->Integral() / nonZeroBins, maxX, hist->Integral() / nonZeroBins);
237 line->SetLineWidth(2);
238 line->SetLineColor(hist->GetLineColor());
239 line->Draw();
240}
241
243 TGraph* graph,
244 HistScale logx,
245 HistScale logy,
246 const string& drawOpt,
247 Int_t color,
248 Int_t lineWidth,
249 Int_t lineStyle,
250 Int_t markerSize,
251 Int_t markerStyle) {
252 Double_t textSize = BmnDrawingOptions::TextSize();
253 graph->SetLineColor(color);
254 graph->SetLineWidth(lineWidth);
255 graph->SetLineStyle(lineStyle);
256 graph->SetMarkerColor(color);
257 graph->SetMarkerSize(markerSize);
258 graph->SetMarkerStyle(markerStyle);
259 if (drawOpt.find("A") != string::npos) {
260 if (logx == kLog) {
261 gPad->SetLogx();
262 }
263 if (logy == kLog) {
264 gPad->SetLogy();
265 }
266 graph->GetXaxis()->SetLabelSize(textSize);
267 graph->GetXaxis()->SetNdivisions(505, kTRUE);
268 graph->GetYaxis()->SetLabelSize(textSize);
269 graph->GetXaxis()->SetTitleSize(textSize);
270 graph->GetYaxis()->SetTitleSize(textSize);
271 graph->GetXaxis()->SetTitleOffset(1.0);
272 graph->GetYaxis()->SetTitleOffset(1.3);
273 }
274 gPad->SetLeftMargin(0.17);
275 gPad->SetBottomMargin(0.15);
276 graph->Draw(drawOpt.c_str());
277 gPad->SetGrid(true, true);
278}
279
280/* Draw several TGraphs. */
282 const vector<TGraph*>& graphs,
283 const vector<string>& graphLabels,
284 HistScale logx,
285 HistScale logy,
286 Bool_t drawLegend,
287 Double_t x1,
288 Double_t y1,
289 Double_t x2,
290 Double_t y2) {
291 assert(graphs.size() != 0 && graphs.size() == graphLabels.size());
292
293 Double_t max = std::numeric_limits<Double_t>::min();
294 Double_t min = std::numeric_limits<Double_t>::max();
295 TLegend* legend = new TLegend(x1, y1, x2, y2);
296 legend->SetFillColor(kWhite);
297 Int_t nofGraphs = graphs.size();
298 for (Int_t iGraph = 0; iGraph < nofGraphs; iGraph++) {
299 TGraph* graph = graphs[iGraph];
300 string opt = (iGraph == 0) ? "ACP" : "CP";
301 DrawGraph(graph, logx, logy, opt, BmnDrawingOptions::Color(iGraph), BmnDrawingOptions::LineWidth(),
303 max = std::max(graph->GetYaxis()->GetXmax(), max);
304 min = std::min(graph->GetYaxis()->GetXmin(), min);
305 legend->AddEntry(graph, graphLabels[iGraph].c_str(), "lp");
306 }
307 graphs[0]->SetMaximum(max);
308 graphs[0]->SetMinimum(min);
309 if (drawLegend) {
310 legend->Draw();
311 }
312}
313
314/* Draws 2D graph.*/
316 TGraph2D* graph,
317 HistScale logx,
318 HistScale logy,
319 HistScale logz,
320 const string& drawOpt) {
321 Double_t textSize = BmnDrawingOptions::TextSize();
322 if (logx == kLog) {
323 gPad->SetLogx();
324 }
325 if (logy == kLog) {
326 gPad->SetLogy();
327 }
328 if (logz == kLog) {
329 gPad->SetLogz();
330 }
331 graph->GetXaxis()->SetLabelSize(textSize);
332 graph->GetXaxis()->SetNdivisions(505, kTRUE);
333 graph->GetYaxis()->SetLabelSize(textSize);
334 graph->GetYaxis()->SetNdivisions(505, kTRUE);
335 graph->GetZaxis()->SetLabelSize(textSize);
336 // graph->GetZaxis()->SetNdivisions(505, kTRUE);
337 graph->GetXaxis()->SetTitleSize(textSize);
338 graph->GetYaxis()->SetTitleSize(textSize);
339 graph->GetZaxis()->SetTitleSize(textSize);
340 graph->GetXaxis()->SetTitleOffset(1.0);
341 graph->GetYaxis()->SetTitleOffset(1.3);
342 graph->GetZaxis()->SetTitleOffset(1.5);
343 gPad->SetLeftMargin(0.17);
344 gPad->SetRightMargin(0.30);
345 gPad->SetBottomMargin(0.15);
346 gPad->SetTicks(1, 1);
347 graph->Draw(drawOpt.c_str());
348 gPad->SetGrid(true, true);
349}
void DrawH1(TH1 *hist, HistScale logx, HistScale logy, const string &drawOpt, Int_t lineColor, Int_t lineWidth, Int_t lineStyle, Float_t markerSize, Int_t markerStyle, Int_t fillColor)
void SetDefaultDrawStyle()
void DrawH2(TH2 *hist, HistScale logx, HistScale logy, HistScale logz, const string &drawOpt)
void DrawMeanLine(TH1 *hist)
void DrawGraph2D(TGraph2D *graph, HistScale logx, HistScale logy, HistScale logz, const string &drawOpt)
void DrawGraph(TGraph *graph, HistScale logx, HistScale logy, const string &drawOpt, Int_t color, Int_t lineWidth, Int_t lineStyle, Int_t markerSize, Int_t markerStyle)
int i
Definition P4_F32vec4.h:22
friend F32vec4 min(const F32vec4 &a, const F32vec4 &b)
Definition P4_F32vec4.h:30
float f
Definition P4_F32vec4.h:21
friend F32vec4 max(const F32vec4 &a, const F32vec4 &b)
Definition P4_F32vec4.h:31
Helper functions for drawing 1D and 2D histograms and graphs.
static Int_t LineStyle(Int_t lineStyleIndex)
Definition BmnDrawHist.h:44
static Double_t TextSize()
Definition BmnDrawHist.h:60
static Int_t Color(Int_t colorIndex)
Definition BmnDrawHist.h:34
static Int_t LineWidth()
Definition BmnDrawHist.h:40
static Int_t MarkerStyle(Int_t markerIndex)
Definition BmnDrawHist.h:52
static Float_t MarkerSize()
Definition BmnDrawHist.h:48
HistScale
Define linear or logarithmic scale for drawing.
Definition BmnDrawHist.h:69
@ kLog
Definition BmnDrawHist.h:70