Heavy ion Analysis Libriares
Loading...
Searching...
No Matches
HtmlCore.cxx
1
2#include "HtmlCore.h"
3
4
5#include <TCanvas.h>
6#include <TGraph.h>
7#include <TH1.h>
8#include <TH2.h>
9#include <TH3.h>
10#include <TList.h>
11#include <TObjArray.h>
12#include <TObjString.h>
13#include <TROOT.h>
14#include <TSystem.h>
15#include <TVector2.h>
16#include <iostream>
17#include <vector>
18
19#include "Cout.h"
20#include "HtmlFile.h"
21#include "HtmlTable.h"
22#include "Object.h"
23#include "Package.h"
24#include "Package2HTML.h"
25#include "Std.h"
26#include "StdString.h"
27
28namespace Hal {
29 HtmlCore* HtmlCore::fgInstance = NULL;
30 TString HtmlCore::fMainDir = "";
31 Bool_t HtmlCore::fgOnline = kFALSE;
32
33 void HtmlCore::CopyCss(TString path, TString css_name) {
34 TString workdir = Hal::Std::GetHalrootPlus();
35 TString filetocopy = Form("%s/html/%s", workdir.Data(), css_name.Data());
36 TString outPath = Form("%s/table.css", path.Data());
37 if (path.Length() == 0) outPath = "table.css"; // to copy into this dir
38 gSystem->CopyFile(filetocopy, outPath, kTRUE);
39 }
40
41 void HtmlCore::CopyJs(TString path) {
42 TString workdir = Hal::Std::GetHalrootPlus();
43 TString filetocopy = Form("%s/html/hal_js.js", workdir.Data());
44 if (path.Length() != 0) {
45 gSystem->CopyFile(filetocopy, Form("%s/hal_js.js", path.Data()), kTRUE);
46 } else {
47 gSystem->CopyFile(filetocopy, "hal_js.js", kTRUE);
48 }
49
50 if (fgOnline) return;
51 filetocopy = Hal::Std::GetJsRoot();
52 if (path.Length() != 0) {
53 gSystem->mkdir(Form("%s/hal_js", path.Data()));
54 gSystem->mkdir(Form("%s/hal_js/img", path.Data()));
55 gSystem->mkdir(Form("%s/hal_js/scripts", path.Data()));
56 gSystem->mkdir(Form("%s/hal_js/style", path.Data()));
57 Hal::Std::CopyFiles(Form("%s/img", filetocopy.Data()), Form("%s/hal_js/img", path.Data()), kFALSE);
58 Hal::Std::CopyFiles(Form("%s/scripts", filetocopy.Data()), Form("%s/hal_js/scripts", path.Data()), kFALSE);
59 Hal::Std::CopyFiles(Form("%s/style", filetocopy.Data()), Form("%s/hal_js/style", path.Data()), kFALSE);
60 } else {
61 gSystem->mkdir("hal_js");
62 gSystem->mkdir("hal_js/img");
63 gSystem->mkdir("hal_js/scripts");
64 gSystem->mkdir("hal_js/style");
65 Hal::Std::CopyFiles(Form("%s/img", filetocopy.Data()), "hal_js/img", kFALSE);
66 Hal::Std::CopyFiles(Form("%s/scripts", filetocopy.Data()), "hal_js/scripts", kFALSE);
67 Hal::Std::CopyFiles(Form("%s/style", filetocopy.Data()), "hal_js/style", kFALSE);
68 }
69 }
70
72 if (fgInstance != NULL) {
73 Cout::PrintInfo("Overwriting HalHTML !", Hal::EInfo::kError);
74 return;
75 } else {
76 fMainDir = "";
77 fgInstance = this;
78 }
79 }
80
82 if (fgInstance != NULL) {
83 } else {
84 fgInstance = new HtmlCore();
85 }
86 return fgInstance;
87 }
88
89 void HtmlCore::SetMainDir(TString name) {
90 if (name.Length() == 0) { Cout::PrintInfo("SetMainDir as null", Hal::EInfo::kLowWarning); }
91 fMainDir = name;
92 FixAddress(fMainDir);
93 }
94
95 void HtmlCore::SetMainDirAndExtract(TString name) {
96 SetMainDir(name);
97 CopyJs(name);
98 CopyCss(name);
99 }
100
101 void HtmlCore::FixAddress(TString& address) {
102 address.ReplaceAll("//", "/");
103 if (address.EndsWith(".js") || address.EndsWith(".html") || address.EndsWith(".htm")) return;
104 if (address.Length() > 0) {
105 if (!address.EndsWith("/")) { address = address + "/"; }
106 }
107 }
108
109 void
110 HtmlCore::HTMLExtractIntoTable(TObject* /*obj*/, Int_t /*no*/, HtmlTable& /*table*/, TString /*dir*/, TString /*rel_dir*/) {
159 return;
160 }
161
162 Bool_t HtmlCore::CanBeTableElement(TObject* /*obj*/) { return kFALSE; }
163
164 TString HtmlCore::GetUrl(TString adress, TString text) { return Form("<a href=\"%s\">%s</a>", adress.Data(), text.Data()); }
165
166 TString HtmlCore::ClickablePic(TString id_name, TObjArray* strings, Int_t width, Int_t height) {
167 TString url = "<img border=\"0\" ";
168 if (width > 0) { url = url + Form("width=\"%i\" ", width); }
169 if (height > 0) { url = url + Form("height=\"%i\" ", height); }
170 for (int i = 0; i < strings->GetEntries(); i++) {
171 TObjString* current_name = (TObjString*) strings->UncheckedAt(i);
172 TString capt = current_name->String();
173 if (i == 0) {
174 url = url + Form("src=\"%s\" ", capt.Data());
175 url = url + Form("id=\"%s\" onclick=\"changeImage('%s',['%s'", id_name.Data(), id_name.Data(), capt.Data());
176 } else {
177 url = url + Form(",'%s'", capt.Data());
178 }
179 }
180 url = url + "])\" />";
181 return url;
182 }
183
184 TString HtmlCore::GetRelativePath(TString path) {
185 FixAddress(path);
186 if (path.EqualTo(fMainDir)) { return ""; }
187 TString relative_path = "";
188 TString temp = path(fMainDir.Length(), path.Length() - fMainDir.Length());
189
190 if (temp.EndsWith("/")) { temp.Remove(temp.Length() - 1); }
191
192 std::vector<TString> vec = Hal::Std::ExplodeString(temp, '/');
193
194 if (vec.size() == 0) return "";
195 for (unsigned int i = 0; i < vec.size() - 1; i++) {
196 relative_path = relative_path + "../";
197 }
198 if (temp.Length() != 0) { relative_path = relative_path + "../"; }
199 return relative_path;
200 }
201
202 TString HtmlCore::CheckBr(TString text) {
203 if (text.Contains("\n")) {
204 // text = text.ReplaceAll()
205 return Form("<pre>%s</pre>", text.Data());
206 }
207 return text;
208 }
209
210 TString HtmlCore::GetLastDir(TString name, Int_t i) {
211 if (name.EndsWith("/")) name = name(0, name.Length() - 1);
212 Int_t cut_pos = -1;
213 Int_t count = 0;
214 for (int j = name.Length() - 1; j >= 0; j--) {
215 if (name[j] == '/') {
216 count++;
217 cut_pos = j;
218 }
219 if (count == i) break;
220 }
221 name = name(cut_pos + 1, name.Length());
222 if (name.Length() > 0) name = name + "/";
223 return name;
224 }
225
226 TString HtmlCore::GetJsDiv(TString root_file, TString object_name, TString draw_opt, TString draw_div_name) {
227 TString res;
228 if (Hal::Std::GetJsRootVer() == 5) {
229 res = "<script type='text/javascript'>\n";
230 res = res + Form("var filename = \"%s\";\n", root_file.Data());
231 res = res + " JSROOT.OpenFile(filename, function(file) {\n";
232 res = res + " JSROOT.gStyle.Palette= 50;\n";
233 res = res + Form("file.ReadObject(\"%s\", function(obj) {\n", object_name.Data());
234 res = res + Form("JSROOT.draw(\"%s\", obj, \"%s\");\n;", draw_div_name.Data(), draw_opt.Data());
235 res = res + "});\n;});\n";
236 res = res + "</script>\n<div id=\"" + draw_div_name + "\" style=\"width:90%;height:900px\"></div>\n";
237 } else { // JSROOT ver 6
238 res = "<script type='module'>\n";
239 res = res + Form("var filename = \"%s\";\n", root_file.Data());
240 res = res + "JSROOT.settings.Palette= 50;\n";
241 res = res + "JSROOT.openFile(filename)\n";
242 res = res + "\t.then(file=> file.readObject(\"" + object_name + "\"))\n";
243 res = res + "\t.then(obj=> JSROOT.draw(\"" + draw_div_name.Data() + "\",obj,\"" + draw_opt.Data() + "\"));\n";
244 res = res + "</script>\n<div id=\"" + draw_div_name + "\" style=\"width:90%;height:900px\"></div>\n";
245 }
246 return res;
247 }
248
249 TString HtmlCore::GetHideButtonTable(TString tableName, TString text) {
250 return Form("<button onclick=\"setTable('%s')\">%s</button>", tableName.Data(), text.Data());
251 }
252
253 TString HtmlCore::GetHideButtonRow(TString listName, TString text) {
254 return Form("<button onclick=\"setRow('%s')\">%s</button>", listName.Data(), text.Data());
255 }
256
257 TString HtmlCore::GetLinkToHistogram(TH1* h, Int_t no, TString path) {
258 TString drawOpt = "";
259 if (h->InheritsFrom("TH3")) {
260 drawOpt = "box1";
261 } else if (h->InheritsFrom("TH2")) {
262 drawOpt = "colz";
263 }
264 Bool_t batch = gROOT->IsBatch();
265 gROOT->SetBatch(kTRUE);
266 path = Form("%s/th_%i", path.Data(), no);
267 gSystem->mkdir(path);
268 TCanvas* c1 = new TCanvas("canvas", "canvas", 0, 0, 800, 600);
269 TH1* histo = (TH1*) h;
270 histo->Draw(drawOpt);
271 c1->SaveAs(Form("%s/histo.root", path.Data()));
272 delete c1;
273 gROOT->SetBatch(batch);
274 HtmlFile file(Form("%s/histo.html", path.Data()), kFALSE);
275 file.AddStringContent(HtmlCore::GetJsDiv("histo.root", "canvas;1"));
276 file.Save();
277 TString filename = Form("%s/histo.html", path.Data());
278 gROOT->SetBatch(batch);
279 return HtmlCore::GetUrl(Form("th_%i/histo.html", no), h->ClassName());
280 }
281
282 TString HtmlCore::HTMLExtract(TObject* obj, Int_t no, TString dir) {
283 TString className = obj->ClassName();
284 if (className.EqualTo("TObjString")) { return static_cast<TObjString*>(obj)->GetString(); }
285 if (className.EqualTo("TVector2")) { return GetLinkVector(static_cast<TVector2*>(obj)); }
286 if (className.EqualTo("Hal::Package")) {
287 Package2HTML* pack = new Package2HTML(static_cast<Package*>(obj), Form("%s/pack_%i", dir.Data(), no), kFALSE);
288 delete pack;
289 return HtmlCore::GetUrl(Form("pack_%i/index.html", no), "Hal::Package");
290 }
291 if (obj->InheritsFrom("Hal::Object")) { return static_cast<Object*>(obj)->HTMLExtract(no, dir); }
292 if (obj->InheritsFrom("TList")) { return GetLinkList(static_cast<TList*>(obj), no, dir); };
293 if (obj->InheritsFrom("TH1")) { return GetLinkTH1(static_cast<TH1*>(obj), no, dir); }
294 if (obj->InheritsFrom("TGraph")) { return GetLinkGraph(static_cast<TGraph*>(obj), no, dir); };
295 return "";
296 }
297
298 TString HtmlCore::GetLinkVector(TVector2* vect) {
299 TString stringA = Hal::Std::RoundToString(vect->Px(), 1);
300 TString stringB = Hal::Std::RoundToString(vect->Py(), 1);
301 stringA = stringA + " ";
302 return stringA + stringB;
303 }
304
305 TString HtmlCore::GetLinkTH1(TH1* h1, Int_t no, TString path) {
306 while (path.Contains("//")) {
307 path.ReplaceAll("//", "/");
308 }
309 TString rootFileName = "histo1d.root";
310 TString opt = "";
311 TString pattern = "th1";
312 if (h1->InheritsFrom("TH2")) {
313 rootFileName = "histo2d.root";
314 opt = "colz";
315 pattern = "th2";
316 }
317 if (h1->InheritsFrom("TH3")) {
318 rootFileName = "histo3d.root";
319 opt = "box2";
320 pattern = "th3";
321 }
322 Bool_t batch = gROOT->IsBatch();
323 gROOT->SetBatch(kTRUE);
324 path = Form("%s/%s_%i", path.Data(), pattern.Data(), no);
325 gSystem->mkdir(path);
326 TCanvas* c1 = new TCanvas("canvas", "canvas", 0, 0, 800, 600);
327 TH1* histo = (TH1*) h1;
328 histo->Draw(opt);
329 c1->SaveAs(Form("%s/%s", path.Data(), rootFileName.Data()));
330 delete c1;
331 gROOT->SetBatch(batch);
332 HtmlFile file(Form("%s/histo.html", path.Data()), kFALSE);
333 file.AddStringContent(HtmlCore::GetJsDiv(rootFileName, "canvas;1"));
334 file.Save();
335 TString filename = Form("%s/histo.html", path.Data());
336 gROOT->SetBatch(batch);
337 return HtmlCore::GetUrl(Form("%s_%i/histo.html", pattern.Data(), no), h1->ClassName());
338 }
339
340 TString HtmlCore::GetLinkGraph(TGraph* h, Int_t no, TString path) {
341 TString classname = h->ClassName();
342 Bool_t batch = gROOT->IsBatch();
343 gROOT->SetBatch(kTRUE);
344 path = Form("%s/graph_%i", path.Data(), no);
345 gSystem->mkdir(path);
346 TCanvas* c1 = new TCanvas("canvas", "canvas", 0, 0, 800, 600);
347 h->Draw("EP");
348 c1->SaveAs(Form("%s/graph.root", path.Data()));
349 delete c1;
350 HtmlFile F(Form("%s/graph.html", path.Data()), kFALSE);
351 F.AddStringContent(HtmlCore::GetJsDiv("graph.root", "canvas;1", "EP"));
352 F.Save();
353 gROOT->SetBatch(batch);
354 return HtmlCore::GetUrl(Form("graph_%i/graph.html", no), h->ClassName());
355 }
356
357 TString HtmlCore::GetLinkList(TList* list, Int_t no, TString path) {
358 TString filename = Form("%s/list_%i/list.html", path.Data(), no);
359 TString linkname = Form("list_%i.html", no);
360 HtmlFile file(filename, kFALSE);
361 file.AddStringContent(Form("<h1>Name %s </h1>", list->GetName()));
362 file.AddStringContent("<div id=\"main_table\">");
363 HtmlTable table("", "haltable", "");
364 HtmlRow row("", Hal::HtmlTableRowClass::TitleStyle(), "");
365 row.AddContent(HtmlCell("No"));
366 row.AddContent(HtmlCell("ClassName"));
367 row.AddContent(HtmlCell("Value"));
368 row.AddContent(HtmlCell("Name"));
369 table.AddContent(row);
370 TString list_dir = Form("list_%i/", no);
371 path = Form("%s/list_%i/", path.Data(), no);
372 auto addToUrl = [](TString add_dir, TString url) {
373 if (!url.Contains("href")) { return url; }
374 Int_t start_url = url.First("=") + 2;
375 TString first = url(0, start_url);
376 TString end = url(start_url, url.Length());
377 TString output = Form("%s%s%s", first.Data(), add_dir.Data(), end.Data());
378 output.ReplaceAll("//", "/");
379 return output;
380 };
381 for (int i = 0; i < list->GetEntries(); i++) {
382 TObject* object = list->At(i);
383 TString name = object->ClassName();
384 TString oryginal_class = object->GetName();
385 HtmlRow row2("", Hal::HtmlTableRowClass::DefStyle(), "");
386 table.AddContent(HtmlCell(Hal::Std::RoundToString(i)));
387 table.AddContent(HtmlCell(object->ClassName()));
388
389 table.AddContent(HtmlCell(addToUrl(list_dir, HTMLExtract(object, i, path))));
390 if (name == "Hal::Package") {
391 oryginal_class = ((Package*) object)->GetName();
392 oryginal_class = Form("%s </br> [%s]", object->ClassName(), oryginal_class.Data());
393 table.AddContent(HtmlCell(oryginal_class));
394 } else {
395 table.AddContent(HtmlCell(object->GetName()));
396 }
397 table.AddContent(row2);
398 }
399 file.AddContent(table);
400 file.Save();
401 return HtmlCore::GetUrl(linkname, list->ClassName());
402 }
403} // namespace Hal
static void PrintInfo(TString text, Hal::EInfo status)
Definition Cout.cxx:370
static TString GetUrl(TString adress, TString text)
Definition HtmlCore.cxx:164
static void HTMLExtractIntoTable(TObject *obj, Int_t no, HtmlTable &table, TString dir="", TString rel_dir="")
Definition HtmlCore.cxx:110
static TString GetRelativePath(TString path)
Definition HtmlCore.cxx:184
static void CopyCss(TString dir, TString css_name="table.css")
Definition HtmlCore.cxx:33
static void SetMainDirAndExtract(TString name)
Definition HtmlCore.cxx:95
static TString GetHideButtonTable(TString tableName, TString text)
Definition HtmlCore.cxx:249
static void SetMainDir(TString name)
Definition HtmlCore.cxx:89
static TString ClickablePic(TString id_name, TObjArray *strings, Int_t width=796, Int_t height=572)
Definition HtmlCore.cxx:166
static TString GetJsDiv(TString root_file, TString object_name, TString draw_opt="colz", TString draw_div_name="drawing")
Definition HtmlCore.cxx:226
static TString HTMLExtract(TObject *obj, Int_t no, TString dir="")
Definition HtmlCore.cxx:282
static void FixAddress(TString &address)
Definition HtmlCore.cxx:101
static void CopyJs(TString dir)
Definition HtmlCore.cxx:41
static TString GetHideButtonRow(TString listName, TString text)
Definition HtmlCore.cxx:253
virtual Bool_t CanBeTableElement(TObject *obj)
Definition HtmlCore.cxx:162
static TString GetLinkToHistogram(TH1 *h, Int_t no, TString path)
Definition HtmlCore.cxx:257
static HtmlCore * Instance()
Definition HtmlCore.cxx:81
static TString CheckBr(TString text)
Definition HtmlCore.cxx:202