Heavy ion Analysis Libriares
Loading...
Searching...
No Matches
QAPlotReport.cxx
1/*
2 * QAPlotReport.cxx
3 *
4 * Created on: 12 paź 2020
5 * Author: Daniel Wielanek
6 * E-mail: daniel.wielanek@gmail.com
7 * Warsaw University of Technology, Faculty of Physics
8 */
9
10#include "QAPlotReport.h"
11
12#include <TAxis.h>
13#include <TBrowser.h>
14#include <TCanvas.h>
15#include <TMathBase.h>
16#include <TNamed.h>
17#include <TROOT.h>
18#include <TRegexp.h>
19#include <TSystem.h>
20#include <iostream>
21#include <vector>
22
23#include "Cout.h"
24#include "HtmlCore.h"
25#include "HtmlDiv.h"
26#include "HtmlFile.h"
27#include "HtmlTable.h"
28#include "StdString.h"
29namespace Hal {
30 QAPlotReport::QAPlotReport() :
31 fExtraInfo(nullptr), fOriginName("OriginName", ""), f1dHistos(nullptr), f2dHistos(nullptr), f3dHistos(nullptr) {}
32
33 QAPlotReport::~QAPlotReport() {
34 if (f1dHistos) delete f1dHistos;
35 if (f2dHistos) delete f2dHistos;
36 if (f3dHistos) delete f3dHistos;
37 if (fExtraInfo) delete fExtraInfo;
38 }
39
40 QAPlotReport::QAPlotReport(const QAPlotReport& other) :
41 Object(other),
42 f1dFlags(other.f1dFlags),
43 f2dFlags(other.f2dFlags),
44 f3dFlags(other.f3dFlags),
45 fExtraInfo(nullptr),
46 f1dHistos(nullptr),
47 f2dHistos(nullptr),
48 f3dHistos(nullptr) {
49 fOriginName = other.fOriginName;
50 if (other.f1dHistos) f1dHistos = new HistogramManager_1_1D<TH1D>(*other.f1dHistos);
51 if (other.f2dHistos) f2dHistos = new HistogramManager_1_2D<TH2D>(*other.f2dHistos);
52 if (other.f3dHistos) f3dHistos = new HistogramManager_1_3D<TH3D>(*other.f3dHistos);
53 if (other.fExtraInfo) { fExtraInfo = (Object*) other.fExtraInfo->Clone(); }
54 }
55
56 QAPlotReport& QAPlotReport::operator=(const QAPlotReport& other) {
57 if (this == &other) return *this;
58 if (f1dHistos) {
59 delete f1dHistos;
60 f1dHistos = nullptr;
61 }
62 if (f2dHistos) {
63 delete f2dHistos;
64 f2dHistos = nullptr;
65 }
66 if (f3dHistos) {
67 delete f3dHistos;
68 f3dHistos = nullptr;
69 }
70 if (fExtraInfo) {
71 delete fExtraInfo;
72 fExtraInfo = nullptr;
73 }
74
75 Object::operator=(other);
76 fOriginName = other.fOriginName;
77 if (other.f1dHistos) f1dHistos = new HistogramManager_1_1D<TH1D>(*other.f1dHistos);
78 if (other.f2dHistos) f2dHistos = new HistogramManager_1_2D<TH2D>(*other.f2dHistos);
79 if (other.f3dHistos) f3dHistos = new HistogramManager_1_3D<TH3D>(*other.f3dHistos);
80 f1dFlags = other.f1dFlags;
81 f2dFlags = other.f2dFlags;
82 f3dFlags = other.f3dFlags;
83 if (other.fExtraInfo) { fExtraInfo = (Object*) other.fExtraInfo->Clone(); }
84 return *this;
85 }
86
87 void QAPlotReport::TranslateFlag(TString flag, Int_t& hist1, Int_t& hist2, Char_t& op) const {
88 if (flag.Contains("/")) { op = '/'; }
89 if (flag.Contains("+")) { op = '+'; }
90 if (flag.Contains("-")) { op = '-'; }
91 std::vector<TString> arr = Hal::Std::ExplodeString(flag, op);
92 hist1 = arr[0].Atoi();
93 hist2 = arr[1].Atoi();
94 }
95
96 void QAPlotReport::Operate(TH1* h0, TH1* h1, TH1* h2, Char_t op) const {
97 TString name = h0->GetName();
98 TString title = h0->GetTitle();
99 TString xAxis = h0->GetXaxis()->GetTitle();
100 TString yAxis = h0->GetYaxis()->GetTitle();
101 TString zAxis = h0->GetXaxis()->GetTitle();
102 // h0->Reset();
103 h1->Copy(*h0);
105 if (xAxis.Length() > 0) h0->GetXaxis()->SetTitle(xAxis);
106 if (yAxis.Length() > 0) h0->GetYaxis()->SetTitle(yAxis);
107 if (zAxis.Length() > 0) h0->GetZaxis()->SetTitle(zAxis);
108 h0->SetTitle(title);
109 h0->SetName(name);
110 switch (op) {
111 case '-': {
112 h0->Add(h2, -1);
113 } break;
114 case '+': {
115 h0->Add(h2);
116 } break;
117 case '/': {
118 h0->Divide(h2);
119 } break;
120 default: break;
121 }
122 }
123
124 void QAPlotReport::Add(const Object* pack) {
125 QAPlotReport* qa = (QAPlotReport*) pack;
126 if (fExtraInfo) fExtraInfo->Add(qa->fExtraInfo);
127 for (int i = 0; i < GetSize1D(); i++) {
128 f1dHistos->At(i)->Add(qa->f1dHistos->At(i));
129 }
130 for (int i = 0; i < GetSize2D(); i++) {
131 f2dHistos->At(i)->Add(qa->f2dHistos->At(i));
132 }
133 for (int i = 0; i < GetSize3D(); i++) {
134 f3dHistos->At(i)->Add(qa->f3dHistos->At(i));
135 }
136 Recalculate();
137 }
138
139 Long64_t QAPlotReport::Merge(TCollection* collection) {
140 Object::Merge(collection);
141 return 1;
142 }
143
144 void QAPlotReport::Recalculate() {
145 Int_t h1, h2;
146 Char_t oper;
147 for (int i = 0; i < GetSize1D(); i++) {
148 if (f1dFlags[i].Length() < 3) continue;
149 TranslateFlag(f1dFlags[i], h1, h2, oper);
150 TH1* H0 = f1dHistos->At(i);
151 TH1* H1 = f1dHistos->At(h1);
152 TH1* H2 = f1dHistos->At(h2);
153 Operate(H0, H1, H2, oper);
154 }
155 for (int i = 0; i < GetSize2D(); i++) {
156 if (f2dFlags[i].Length() < 3) continue;
157 TranslateFlag(f2dFlags[i], h1, h2, oper);
158 TH1* H0 = f2dHistos->At(i);
159 TH1* H1 = f2dHistos->At(h1);
160 TH1* H2 = f2dHistos->At(h2);
161 Operate(H0, H1, H2, oper);
162 }
163 for (int i = 0; i < GetSize3D(); i++) {
164 if (f3dFlags[i].Length() < 3) continue;
165 TranslateFlag(f3dFlags[i], h1, h2, oper);
166 TH1* H0 = f3dHistos->At(i);
167 TH1* H1 = f3dHistos->At(h1);
168 TH1* H2 = f3dHistos->At(h2);
169 Operate(H0, H1, H2, oper);
170 }
171 }
172
173 QAPlotReport::QAPlotReport(TString name, Int_t oneDim, Int_t twoDim, Int_t threeDim) :
174 Object(), fExtraInfo(nullptr), fOriginName("OriginName", name), f1dHistos(nullptr), f2dHistos(nullptr), f3dHistos(nullptr) {
175 SetName(name);
176 f1dFlags.resize(oneDim);
177 f2dFlags.resize(twoDim);
178 f3dFlags.resize(threeDim);
179
180 if (f1dFlags.size() > 0) { f1dHistos = new HistogramManager_1_1D<TH1D>(); }
181 if (f2dFlags.size() > 0) { f2dHistos = new HistogramManager_1_2D<TH2D>(); }
182 if (f3dFlags.size() > 0) { f3dHistos = new HistogramManager_1_3D<TH3D>(); }
183 }
184
186 if (fExtraInfo) delete fExtraInfo;
187 fExtraInfo = p;
188 }
189
190 void QAPlotReport::SetFlag1D(Int_t no, Char_t c, Int_t h1, Int_t h2) {
191 if (CheckFlag(no, c, h1, h2, 1)) f1dFlags[no] = Form("%i%c%i", h1, c, h2);
192 }
193
194 void QAPlotReport::SetFlag2D(Int_t no, Char_t c, Int_t h1, Int_t h2) {
195 if (CheckFlag(no, c, h1, h2, 2)) f2dFlags[no] = Form("%i%c%i", h1, c, h2);
196 }
197
198 void QAPlotReport::SetFlag3D(Int_t no, Char_t c, Int_t h1, Int_t h2) {
199 if (CheckFlag(no, c, h1, h2, 3)) f3dFlags[no] = Form("%i%c%i", h1, c, h2);
200 }
201
202 Bool_t QAPlotReport::CheckFlag(Int_t no, Char_t c, Int_t h1, Int_t h2, Int_t dim) const {
203 Int_t size = 0;
204 switch (dim) {
205 case 1: size = GetSize1D(); break;
206 case 2: size = GetSize2D(); break;
207 case 3: size = GetSize3D(); break;
208 }
209 if (no >= size) return kFALSE;
210 if (no < 0) return kFALSE;
211 if (h1 >= size) return kFALSE;
212 if (h1 < 0) return kFALSE;
213 if (h2 >= size) return kFALSE;
214 if (h2 < 0) return kFALSE;
215 if (h1 == h2) return kFALSE;
216 if (no == h1) return kFALSE;
217 if (no == h2) return kFALSE;
218 if (c != '/' && c != '+' && c != '-') return kFALSE;
219 return kTRUE;
220 }
221
222 TString QAPlotReport::ExportHistogramToFile(TString path, Int_t count, TH1* h) const {
223 return ExportHistogramToFile(path, "", count, h);
224 }
225
226 TString QAPlotReport::ExportHistogramToFile(TString path, TString dir, Int_t count, TH1* h) const {
227 Int_t dim = 1;
228 TString draw_opt = "";
229 if (h->InheritsFrom("TH2")) {
230 dim = 2;
231 draw_opt = "colz";
232 } else if (h->InheritsFrom("TH3")) {
233 dim = 3;
234 }
237 TString rootPathFull = Form("%shist_%id_%i.root", path.Data(), dim, count);
238 TString rootPathShort = Form("hist_%id_%i.root", dim, count);
239 TString htmlPathFull = Form("%shist_%id_%i.html", path.Data(), dim, count);
240 TString htmlPathShort = dir + Form("hist_%id_%i.html", dim, count);
241
242 TCanvas* c1 = new TCanvas("canvas", "canvas", 0, 0, 1000, 1500);
243 h->DrawClone(draw_opt);
244 c1->SaveAs(rootPathFull);
245 delete c1;
246 HtmlFile file(htmlPathFull, kFALSE);
247 file.AddStringContent(HtmlCore::GetJsDiv(rootPathShort, "canvas;1", draw_opt));
248 file.Save();
249 return HtmlCore::GetUrl(htmlPathShort, h->GetTitle());
250 }
251
252 TString QAPlotReport::HTMLExtract(Int_t no, TString dir) const {
253 Bool_t batch = gROOT->IsBatch();
254 gROOT->SetBatch(kTRUE);
255 TString path = Form("%s/qa_plot_%i/", dir.Data(), no);
257 gSystem->mkdir(path);
258 TString filename = Form("%sqa_plot.html", path.Data());
259 TString filePath = Form("qa_plot_%i", no);
260 HtmlFile file(filename);
261
262 HtmlDiv div;
263
264 Int_t max = TMath::Max(GetSize1D(), TMath::Max(GetSize2D(), GetSize3D()));
265 HtmlTable table1("table_1d", "haltable", "");
266
267 HtmlRow rowParent("", "green_", "");
268 rowParent.AddContent(HtmlCell("Origin"));
269 rowParent.AddContent(HtmlCellCol(fOriginName.GetValue(), 3));
270 HtmlRow extra("", "green_", "");
271 extra.AddContent(HtmlCellCol("Extra pack", 2));
272 if (fExtraInfo == nullptr) {
273 extra.AddContent(HtmlCellCol("Null", 2));
274 } else {
275 TString url = fExtraInfo->HTMLExtract(0, Form("%s/qa_plot_%i/", path.Data(), no));
276 extra.AddContent(HtmlCellCol(url, 2));
277 }
278
279 table1.AddContent(rowParent);
280 table1.AddContent(extra);
281 HtmlRow rowLeg1("", "dark_blue", "");
282 rowLeg1.AddContent(HtmlCell("No"));
283 rowLeg1.AddContent(HtmlCell("1D histograms"));
284 rowLeg1.AddContent(HtmlCell("2D histograms"));
285 rowLeg1.AddContent(HtmlCell("3D histograms"));
286 table1.AddContent(rowLeg1);
287 for (int i = 0; i < max; i++) {
288 HtmlRow rowEnt("", "light_blue", "");
289 rowEnt.AddContent(HtmlCell(Form("%i", i)));
290 TString cell1 = "", cell2 = "", cell3;
291 if (i < GetSize1D()) { cell1 = ExportHistogramToFile(path, i, f1dHistos->At(i)); }
292 if (i < GetSize2D()) { cell2 = ExportHistogramToFile(path, i, f2dHistos->At(i)); }
293 if (i < GetSize3D()) { cell3 = ExportHistogramToFile(path, i, f3dHistos->At(i)); }
294 rowEnt.AddContent(HtmlCell(cell1));
295 rowEnt.AddContent(HtmlCell(cell2));
296 rowEnt.AddContent(HtmlCell(cell3));
297 table1.AddContent(rowEnt);
298 }
299
300 div.AddContent(table1);
301 file.AddContent(div);
302 file.Save();
303 gROOT->SetBatch(batch);
304 TString relPath = Form("qa_plot_%i/qa_plot.html", no);
305 return HtmlCore::GetUrl(relPath, Form("QAPlot [%s]", GetName()));
306 }
307
308 void QAPlotReport::HTMLExtractIntoTable(Int_t no, HtmlTable& table, TString dir, TString rel_dir) const {
309 Bool_t batch = gROOT->IsBatch();
310 gROOT->SetBatch(kTRUE);
311 TString path = Form("%s/qa_plot_%i/", dir.Data(), no);
312 TString subdir = Form("qa_plot_%i", no);
313 if (rel_dir.Length() > 0) subdir = Form("%s/qa_plot_%i", rel_dir.Data(), no);
315 HtmlCore::FixAddress(subdir);
316 gSystem->mkdir(path);
317
318 // Int_t max = TMath::Max(GetSize1D(), TMath::Max(GetSize2D(), GetSize3D()));
319
320 TString className = Form("med_blue qa_%i", no);
321
322 HtmlRow row;
323 row.SetClass("light_blue");
324 HtmlCell cell1(Form("%i", no));
325 cell1.SetColSpan(2);
326 row.AddContent(cell1);
327 row.AddContent(HtmlCell("QAPlotReport"));
328 row.AddContent(HtmlCell(this->GetName()));
329 TString rowButton = HtmlCore::GetHideButtonRow(Form("qa_%i", no), "Show/Hide");
330 row.AddContent(HtmlCell(rowButton));
331 table.AddContent(row);
332 HtmlRow rowPaint("", className, "display:none");
333 TString buttonDraw = Form("<button onclick=\"qaPopup('%i')\">Plot Many</button>", no);
334 rowPaint.AddContent(HtmlCellCol(buttonDraw, 5));
335 table.AddContent(rowPaint);
336 gSystem->mkdir(path);
337 // if (path_url != path_data) list_dir2 = Form("%s/list_%i/", path_url.Data(), no);
338 struct constRowData {
339 TString className;
340 TString subDir;
341 TString path;
342 HtmlTable* table;
343 QAPlotReport* rep;
344 Int_t no;
345 Int_t dim;
346 };
347 constRowData data;
348 data.className = className;
349 data.subDir = subdir;
350 data.path = path;
351 data.no = no;
352 data.table = &table;
353
354 auto makeRow = [](Int_t nu, TH1* h, constRowData dat) {
355 HtmlRow rowElement2("", dat.className, "display:none");
356 TString url = dat.rep->ExportHistogramToFile(dat.path, dat.subDir, nu, h);
357 TString boxname = Form("cb_%i_%i_%i", dat.no, dat.dim, nu);
358 TString urlBox = Form("<input type=\"checkbox\" name = \"%s\" >", boxname.Data());
359 HtmlCellCol cell = HtmlCellCol(urlBox, 1);
360 cell.AddAtrib("onclick", Form("chageCheckBoxCell('%s')", boxname.Data()));
361 rowElement2.AddContent(cell);
362 rowElement2.AddContent(HtmlCellCol(Form("%i", nu), 1));
363 rowElement2.AddContent(HtmlCellCol(url, 3));
364 dat.table->AddContent(rowElement2);
365 };
366 HtmlRow rowElement("", className, "display:none");
367 rowElement.AddContent(HtmlCellCol(" ", 1));
368 if (GetSize1D() > 0) {
369 data.dim = 1;
370 HtmlRow rE = rowElement;
371 rE.AddContent(HtmlCellCol("Histograms 1D", 4));
372 table.AddContent(rE);
373 for (int i = 0; i < GetSize1D(); i++) {
374 makeRow(i, f1dHistos->At(i), data);
375 }
376 }
377 if (GetSize2D() > 0) {
378 data.dim = 2;
379 HtmlRow rE = rowElement;
380 rE.AddContent(HtmlCellCol("Histograms 2D", 4));
381 table.AddContent(rE);
382 for (int i = 0; i < GetSize2D(); i++) {
383 makeRow(i, f2dHistos->At(i), data);
384 }
385 }
386 if (GetSize3D() > 0) {
387 data.dim = 3;
388 HtmlRow rE = rowElement;
389 rE.AddContent(HtmlCellCol("Histograms 3D", 4));
390 table.AddContent(rE);
391 for (int i = 0; i < GetSize3D(); i++) {
392 makeRow(i, f3dHistos->At(i), data);
393 }
394 }
395 /*
396 HtmlRow rowParent("", className, "display:none");
397 rowParent.AddContent(HtmlCell("Origin"));
398 rowParent.AddContent(HtmlCellCol(fOriginName.GetValue(), 4));
399 table.AddContent(rowParent);
400 */
401
402 if (fExtraInfo == nullptr) {
403 /*
404 HtmlRow extra("", className, "display:none");
405 extra.AddContent(HtmlCellCol("Extra pack", 3));
406 extra.AddContent(HtmlCellCol("Null", 2));
407 table.AddContent(extra);
408
409 */
410 } else {
411 HtmlRow extra("", className, "display:none");
412 extra.AddContent(HtmlCellCol("Extra pack", 3));
413 TString url = fExtraInfo->HTMLExtract(0, Form("%s/qa_plot_%i/", path.Data(), no));
414 extra.AddContent(HtmlCellCol(url, 4));
415 table.AddContent(extra);
416 }
417
418
419 gROOT->SetBatch(batch);
420 }
421
422 void QAPlotReport::Browse(TBrowser* b) {
423 b->Add(&fOriginName);
424 for (int i = 0; i < GetSize1D(); i++) {
425 b->Add(f1dHistos->At(i));
426 }
427 for (int i = 0; i < GetSize2D(); i++) {
428 b->Add(f2dHistos->At(i));
429 }
430 for (int i = 0; i < GetSize3D(); i++) {
431 b->Add(f3dHistos->At(i));
432 }
433 }
434
435 void QAPlotReport::Print(Option_t* /*option*/) const {
436 Cout::Database({"ID", "Name", "Flags"});
437 Cout::InStars("1D", kWhite);
438 for (int i = 0; i < GetSize1D(); i++) {
439 TString name = f1dHistos->At(i)->GetName();
440 TString flag = f1dFlags[i];
441 Cout::Database({Form("%i", i), name, flag});
442 }
443 Cout::InStars("2D", kWhite);
444 for (int i = 0; i < GetSize2D(); i++) {
445 TString name = f2dHistos->At(i)->GetName();
446 TString flag = f2dFlags[i];
447 Cout::Database({Form("%i", i), name, flag});
448 }
449 Cout::InStars("3D", kWhite);
450 for (int i = 0; i < GetSize3D(); i++) {
451 TString name = f3dHistos->At(i)->GetName();
452 TString flag = f3dFlags[i];
453 Cout::Database({Form("%i", i), name, flag});
454 }
455 }
456
457 void QAPlotReport::SetFlag(Int_t no, TString flag, Int_t dim) {
458 switch (dim) {
459 case 1: f1dFlags[no] = flag; break;
460 case 2: f2dFlags[no] = flag; break;
461 case 3: f3dFlags[no] = flag; break;
462 }
463 }
464} // namespace Hal
static void Database(Int_t no...)
T * At(const Int_t i) const
static TString GetUrl(TString adress, TString text)
Definition HtmlCore.cxx:164
static TString GetJsDiv(TString root_file, TString object_name, TString draw_opt="colz", TString draw_div_name="drawing")
Definition HtmlCore.cxx:226
static void FixAddress(TString &address)
Definition HtmlCore.cxx:101
static TString GetHideButtonRow(TString listName, TString text)
Definition HtmlCore.cxx:253
void SetClass(TString className)
Definition HtmlObject.h:40
virtual void AddContent(const HtmlObject &obj)
virtual void AddContent(const HtmlObject &obj)
Definition HtmlTable.cxx:29
virtual void AddContent(const HtmlObject &obj)
Definition HtmlTable.cxx:17
virtual void Add(const Object *pack)
Definition Object.cxx:24
virtual TString HTMLExtract(Int_t, TString="") const
Definition Object.cxx:20
TString GetValue() const
Definition Parameter.h:202
virtual TString HTMLExtract(Int_t no, TString dir="") const
virtual void HTMLExtractIntoTable(Int_t no, HtmlTable &obj, TString dir="", TString rel_dir="") const
Int_t GetSize2D() const
TString ExportHistogramToFile(TString path, Int_t count, TH1 *h) const
Int_t GetSize1D() const
void SetFlag(Int_t no, TString flag, Int_t dim)
void SetFlag3D(Int_t no, Char_t c=' ', Int_t h1=0, Int_t h2=0)
void SetFlag2D(Int_t no, Char_t c=' ', Int_t h1=0, Int_t h2=0)
void SetFlag1D(Int_t no, Char_t c=' ', Int_t h1=0, Int_t h2=0)
Int_t GetSize3D() const
Bool_t CheckFlag(Int_t no, Char_t c, Int_t h1, Int_t h2, Int_t dim) const
virtual void SetExtraInfo(Object *p)