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