BmnRoot 26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
BmnDrawOnline.cxx
Go to the documentation of this file.
1#include "BmnDrawOnline.h"
2
3#include "BmnDrawHist.h"
4#include "TGaxis.h"
5#include "TH1.h"
6#include "TH1D.h"
7#include "TH2.h"
8#include "TLegend.h"
9#include "TMath.h"
10#include "TPad.h"
11#include "TPaveLabel.h"
12#include "TStyle.h"
13
14#include <cassert>
15#include <iostream>
16#include <limits>
17#include <sstream>
18#include <string>
19
20using std::stringstream;
21
23{
24 // For param use:
25 // RECREATE Create a new file, if the file already exists it will be overwritten.
26 // UPDATE Open an existing file for writing. If no file exists, it is created.
27 // canvStorage = TFile::Open("canvStorage.root",param);
28 canvStorage = new TFile(storageName + ".root", param);
29}
30
32 : fServer(nullptr)
33{
34 InitServer(port);
35 RegisterCanvases(file);
36}
37
39{
40 if (fServer)
41 delete fServer;
42}
43
45 TH1* hist,
46 HistScale logx,
47 HistScale logy,
48 const string& drawOpt,
49 Int_t lineColor,
50 Int_t lineWidth,
51 Int_t lineStyle,
52 Float_t markerSize,
53 Int_t markerStyle,
54 Int_t fillColor)
55{
57 hist->SetLineColor(lineColor);
58 hist->SetLineWidth(lineWidth);
59 hist->SetLineStyle(lineStyle);
60 hist->SetMarkerColor(lineColor);
61 hist->SetMarkerSize(markerSize);
62 hist->SetMarkerStyle(markerStyle);
63 if (logx == kLog) {
64 gPad->SetLogx();
65 }
66 if (logy == kLog) {
67 gPad->SetLogy();
68 }
69 hist->GetXaxis()->SetLabelSize(textSize);
70 hist->GetXaxis()->SetNdivisions(505, kTRUE);
71 hist->GetYaxis()->SetLabelSize(textSize);
72 hist->GetXaxis()->SetTitleSize(textSize);
73 hist->GetYaxis()->SetTitleSize(textSize);
74 hist->GetXaxis()->SetTitleOffset(1.0);
75 hist->GetYaxis()->SetTitleOffset(1.3);
76
77 gPad->SetTopMargin(0.20);
78 gPad->SetLeftMargin(0.17);
79 gPad->SetBottomMargin(0.15);
80 gPad->SetTicks(1, 1);
81 if (fillColor > 0)
82 hist->SetFillColor(fillColor);
83
84 if (canvName != canvas->GetName()) {
85 canvName = canvas->GetName();
86 canvVect.push_back(canvas);
87 maxHeight += canvas->GetWh();
88 }
89
90 hist->Draw(drawOpt.c_str());
91 gPad->SetGrid(true, true);
92 hist->SetStats(false);
93}
94
95/* Draw TH2 histogram.*/
97 TH2* hist,
98 HistScale logx,
99 HistScale logy,
100 HistScale logz,
101 const string& drawOpt)
102{
104 if (logx == kLog) {
105 gPad->SetLogx();
106 }
107 if (logy == kLog) {
108 gPad->SetLogy();
109 }
110 if (logz == kLog) {
111 gPad->SetLogz();
112 }
113 hist->GetXaxis()->SetLabelSize(textSize);
114 hist->GetXaxis()->SetNdivisions(505, kTRUE);
115 hist->GetYaxis()->SetLabelSize(textSize);
116 hist->GetYaxis()->SetNdivisions(505, kTRUE);
117 hist->GetZaxis()->SetLabelSize(textSize);
118 // hist->GetZaxis()->SetNdivisions(505, kTRUE);
119 hist->GetXaxis()->SetTitleSize(textSize);
120 hist->GetYaxis()->SetTitleSize(textSize);
121 hist->GetZaxis()->SetTitleSize(textSize);
122 hist->GetXaxis()->SetTitleOffset(1.0);
123 hist->GetYaxis()->SetTitleOffset(1.3);
124 hist->GetZaxis()->SetTitleOffset(1.5);
125
126 gPad->SetTopMargin(0.20);
127 gPad->SetLeftMargin(0.17);
128 gPad->SetRightMargin(0.30);
129 gPad->SetBottomMargin(0.15);
130 gPad->SetTicks(1, 1);
131
132 if (canvName != canvas->GetName()) {
133 canvName = canvas->GetName();
134 canvVect.push_back(canvas);
135 maxHeight += canvas->GetWh();
136 }
137
138 hist->Draw(drawOpt.c_str());
139 gPad->SetGrid(true, true);
140 hist->SetStats(false);
141}
142
143/* Draw several TH1 histograms. */
145 const vector<TH1*>& histos,
146 const vector<string>& histLabels,
147 HistScale logx,
148 HistScale logy,
149 Bool_t drawLegend,
150 Double_t x1,
151 Double_t y1,
152 Double_t x2,
153 Double_t y2,
154 const string& drawOpt,
155 Bool_t outputMeanValue01)
156{
157 assert(histos.size() != 0 && histLabels.size() == histos.size());
158 Double_t max = std::numeric_limits<Double_t>::min();
159 Int_t nofHistos = histos.size();
160 TLegend* legend = new TLegend(x1, y1, x2, y2);
161 legend->SetFillColor(kWhite);
162 for (Int_t iHist = 0; iHist < nofHistos; iHist++) {
163 TH1* hist = histos[iHist];
164 string opt = (iHist == 0) ? drawOpt : (iHist == nofHistos - 1) ? "SAME" + drawOpt : "SAME" + drawOpt;
165 // string opt = drawOpt;
166 DrawH1(canvas, hist, logx, logy, opt, BmnDrawingOptions::Color(iHist), BmnDrawingOptions::LineWidth(),
168 max = std::max(max, hist->GetMaximum());
169 Int_t nonZeroBins = 0;
170 for (Int_t i = 0; i < hist->GetNbinsX(); ++i) {
171 if (hist->GetBinContent(i) != 0.0)
172 nonZeroBins++;
173 }
174
175 Double_t xMax = hist->GetBinCenter(hist->GetNbinsX());
176 Double_t xGev = 1.0; // 1 GeV
177 Int_t nGev = Int_t(hist->GetNbinsX() * xGev / xMax);
178 Double_t effAll = hist->Integral() / nonZeroBins;
179 Double_t eff01 = hist->Integral(0.0, nGev) / nGev; // efficiency from 0 to 1 GeV
180 TString legText;
181 if (outputMeanValue01)
182 legText = TString::Format("%s | mean = %3.1f | mean01 = %3.1f", histLabels[iHist].c_str(), effAll, eff01)
183 .Data();
184 else
185 legText = TString::Format("%s | mean = %3.1f", histLabels[iHist].c_str(), effAll).Data();
186 legend->AddEntry(hist, legText, "lp");
187 // To choose the best combination of parameters
188 FILE* f = fopen("test.dat", "a+");
189 if (((TString)hist->GetName()).Contains("vs_P"))
190 fprintf(f, "%s %s %f %f\n", hist->GetName(), histLabels[iHist].c_str(), effAll, eff01);
191 fclose(f);
192 }
193
194 histos[0]->SetMaximum(max * 1.10);
195
196 if (drawLegend) {
197 legend->Draw();
198 }
199}
200
202{
203
204 gPad->SetTopMargin(0.20);
205 gPad->SetLeftMargin(0.17);
206 gPad->SetBottomMargin(0.15);
207 gPad->SetTicks(1, 1);
208
209 if (canvName != canvas->GetName()) {
210 canvName = canvas->GetName();
211 canvVect.push_back(canvas);
212 maxHeight += canvas->GetWh();
213 }
214
215 hist->Draw();
216 gPad->SetGrid(true, true);
217}
218
220{
221
222 const Char_t* httpStr = Form("http:%d;cors;noglobal", port);
223
224 // Start http-server ...
225 fServer = new THttpServer(httpStr);
226}
227
228/* Принцип работы DrawMainCanvas():
2291.Создается холст с высотой, равной суммарной высоте всех холстов с гистограммами. 2.Размечаем его так, чтобы он состоял
230из одного столбца из числа строк, равному общему числу холстов. 3.Для каждого холста создаем TPad и TPaveLabel - будем
231использовать их, чтобы подписывать холсты. 4.Запускаем цикл, с первой строчки общего холста, до последней, в котором: С
232помощью метода DrawClonePad() на нужную строку холста добавляем набор гистограмм из вектора canvVect. Поверх
233нарисованных гистограмм добавляем панель TPad, на этой панели, с помощью TPaveLabel подписываем заголовок для нужного
234подхолста. 5.Полученный холст записывается в root-файл canvStorage.root 6.Зануляются значения общей высоты, вектора
235холстов и имени холста.
236*/
237
239{
240
241 Int_t height = maxHeight;
242 TCanvas* mainCanvas = new TCanvas(nameOfCanv, "title", 1280, height);
243 Int_t numOfCanvases = (canvVect.size());
244 mainCanvas->Divide(1, numOfCanvases, 0.01, 0.002);
245 TPad* newpad[numOfCanvases];
246 TPaveLabel* title[numOfCanvases];
247
248 for (Int_t i = 0; i < numOfCanvases; ++i) {
249 mainCanvas->cd(i + 1);
250 canvVect[i]->DrawClonePad();
251
252 TString newpadName = "newpad" + std::to_string(i);
253 newpad[i] = new TPad(newpadName, "a transparent pad", 0.0, 0.93, 1.0, 1.0);
254 newpad[i]->SetFillStyle(4000);
255 newpad[i]->Draw();
256 newpad[i]->cd();
257 title[i] = new TPaveLabel(0.0, 0.0, 1.0, 1.0, canvVect[i]->GetName());
258 title[i]->SetFillColor(16);
259 title[i]->SetTextFont(52);
260 title[i]->Draw();
261 }
262 mainCanvas->Write();
263 maxHeight = 0;
264 canvName = "null";
265 vector<TCanvas*>().swap(canvVect);
266}
267
269{
270 TString namesOfCanv[] = {"inner", "TOF", "CSC", "DCH", "pid", "He3", "pi+", "e-", "proton", "K+", "D", "T", "He4"};
271 Int_t size = sizeof(namesOfCanv) / sizeof(namesOfCanv[0]);
272 for (Int_t i = 0; i < size; ++i) {
273 TCanvas* canv = (TCanvas*)file->Get(namesOfCanv[i]);
274 if (!canv)
275 continue;
276 fServer->Register("/Objects", canv);
277 }
278}
int i
Definition P4_F32vec4.h:22
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
char Char_t
float Float_t
double Double_t
constexpr Bool_t kTRUE
kWhite
char * Form(const char *fmt,...)
#define gPad
void InitServer(Int_t)
void DrawH2(TCanvas *canvas, TH2 *hist, HistScale logx=kLinear, HistScale logy=kLinear, HistScale logz=kLinear, const string &drawOpt="COLZ")
void DrawMainCanvas(TString)
BmnDrawOnline(TString param, TString storageName="canvStorage")
Constructor.
vector< TCanvas * > canvVect
TFile * canvStorage
void DrawH1(TCanvas *canvas, TH1 *hist, HistScale logx=kLinear, HistScale logy=kLinear, const string &drawOpt="", Int_t color=BmnDrawingOptions::Color(0), Int_t lineWidth=BmnDrawingOptions::LineWidth(), Int_t lineStyle=BmnDrawingOptions::LineStyle(0), Float_t markerSize=BmnDrawingOptions::MarkerSize(), Int_t markerStyle=BmnDrawingOptions::MarkerStyle(0), Int_t fillColor=-1)
void RegisterCanvases(TFile *)
virtual ~BmnDrawOnline()
Destructor.
THttpServer * fServer
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 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)
virtual void SetTextFont(Font_t tfont=62)
TVirtualPad * cd(Int_t subpadnumber=0) override
UInt_t GetWh() const override
T * Get(const char *namecycle)
void Draw(Option_t *opt="") override
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 Int_t Write(const char *name=nullptr, Int_t option=0, Int_t bufsize=0)
void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0) override
void SetFillStyle(Style_t fstyle) override
TVirtualPad * cd(Int_t subpadnumber=0) override
void Draw(Option_t *option="") override
const char * GetName() const override
void Draw(Option_t *option="") override
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