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