Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
MakeRCanvasJS.C
Go to the documentation of this file.
1/// Generates the json file output of the macro MacroName
2
3#include "ROOT/RCanvas.hxx"
4#include "TString.h"
5#include "TSystem.h"
6#include "TROOT.h"
7#include <fstream>
8
9void MakeRCanvasJS(const char *MacroName, const char *IN, const char *OutDir, bool cp, bool py)
10{
11 using namespace ROOT::Experimental;
12
13 // Execute the macro as a C++ one or a Python one.
14 if (!py) gROOT->ProcessLine(TString::Format(".x %s",MacroName));
15 else gROOT->ProcessLine(TString::Format("TPython::ExecScript(\"%s\");",MacroName));
16
17 // If needed, copy the macro in the documentation directory.
18 if (cp) {
19 TString MN = MacroName;
20 Int_t i = MN.Index("(");
21 Int_t l = MN.Length();
22 if (i>0) MN.Remove(i, l);
23 gSystem->Exec(TString::Format("cp %s %s/macros", MN.Data(), OutDir));
24 }
25
26 // Retrieve the RCanvas produced by the macro execution and produce the json file.
27 auto canv = RCanvas::GetCanvases()[0];
28 TString json_file = TString::Format("%s/html/%s.json",OutDir,IN);
29 canv->SaveAs(json_file.Data());
30
31 std::string json_str;
32 {
33 std::ifstream fjson(json_file.Data());
34 json_str = std::string((std::istreambuf_iterator<char>(fjson)), std::istreambuf_iterator<char>());
35 }
36 gSystem->Unlink(json_file.Data());
37
38 // Build the html file inlining the json picture
39 FILE *fh = fopen(TString::Format("%s/macros/%s.html",OutDir,IN), "w");
40 fprintf(fh,"<div id=\"draw_json_%s\" style=\"position: relative; width: 700px; height: 500px;\"></div>\n", IN);
41 fprintf(fh,"<script type=\"module\">\n");
42 fprintf(fh," import { settings, parse, draw } from './js/modules/main.mjs';\n");
43 fprintf(fh," settings.HandleKeys = false;\n");
44 fprintf(fh," let obj = parse(%s);\n", json_str.c_str());
45 fprintf(fh," draw('draw_json_%s', obj);\n", IN);
46 fprintf(fh,"</script>\n");
47 fclose(fh);
48}
void MakeRCanvasJS(const char *MacroName, const char *IN, const char *OutDir, bool cp, bool py)
Generates the json file output of the macro MacroName.
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
#define gROOT
Definition TROOT.h:417
externTSystem * gSystem
Definition TSystem.h:582
static const std::vector< std::shared_ptr< RCanvas > > GetCanvases()
Returns list of created canvases.
Definition RCanvas.cxx:45
Basic string class.
Definition TString.h:138
Ssiz_t Length() const
Definition TString.h:425
const char * Data() const
Definition TString.h:384
TString & Remove(Ssiz_t pos)
Definition TString.h:694
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2385
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition TString.h:660
Float_t py
Definition hprod.C:37
Namespace for ROOT features in testing.
Definition TROOT.h:100
TLine l
Definition textangle.C:4