GRSISort "v4.0.0.5"
An extension of the ROOT analysis Framework
Loading...
Searching...
No Matches
saveScriptOutput.C
Go to the documentation of this file.
1#include "TString.h"
2#include "TSystem.h"
3#include "TGWindow.h"
4#include "TGClient.h"
5#include "TClass.h"
6#include "THashList.h"
7#include "TROOT.h"
8#include "TInterpreter.h"
9#include "TRootCanvas.h"
10#include "TCanvas.h"
11#include "TVirtualViewer3D.h"
12#include "TEnv.h"
13#include "TVirtualX.h"
14
15int saveScriptOutput(const char* script, const char* outdir, Bool_t compiled)
16{
17 // Run script and save all windows to
18 // outdir/script_0.png, outdir/script_1.png, ...
19
20 enum EErrorCodes {
21 kSuccess,
22 kScriptDirNotFound,
23 kCannotRunScript,
24 kOutDirNotFound,
25 kNumErrorCodes
26 };
27
28 TString pwd(gSystem->pwd());
29 if(!gSystem->cd(gSystem->DirName(script))) {
30 return kScriptDirNotFound;
31 }
32 Int_t err = 0;
33 TString cmd(".x ");
34 cmd += gSystem->BaseName(script);
35 if(compiled) {
36 cmd += "+";
37 }
38 if(!gROOT->IsBatch()) {
39 gVirtualX->Sync(1);
40 }
41
42 // save current interpreter context to avoid gROOT->Reset()
43 // in the script to cause havoc by wiping everything away
44 gInterpreter->SaveContext();
45 gInterpreter->SaveGlobalsContext();
46
47 gROOT->ProcessLine(cmd, &err);
48 if(err != TInterpreter::kNoError) {
49 return kCannotRunScript;
50 }
51
52 gSystem->cd(pwd);
53 if(!gSystem->cd(outdir)) {
54 return kOutDirNotFound;
55 }
56
57 UInt_t nCanvases = 0;
58 if(gClient) {
59 auto clRootCanvas = TClass::GetClass("TRootCanvas");
60 auto clGMainFrame = TClass::GetClass("TGMainFrame");
61 TGWindow* win = nullptr;
62 TIter iWin(gClient->GetListOfWindows());
63 while((win = (TGWindow*)iWin())) {
64 const TObject* winGetParent = win->GetParent();
65 Bool_t winIsMapped = kFALSE;
66 if(winGetParent == gClient->GetDefaultRoot()) {
67 winIsMapped = win->IsMapped();
68 }
69 if(winIsMapped && win->InheritsFrom(clGMainFrame)) {
70 win->MapRaised();
71 Bool_t isRootCanvas = win->InheritsFrom(clRootCanvas);
72 Bool_t hasEditor = false;
73 if(isRootCanvas) {
74 hasEditor = ((TRootCanvas*)win)->HasEditor();
75 }
76 if(isRootCanvas && !hasEditor) {
77 TVirtualPad* pad = ((TRootCanvas*)win)->Canvas();
78 if(!pad->HasViewer3D() || pad->GetViewer3D()->InheritsFrom("TViewer3DPad")) {
79 pad->SaveAs(TString::Format("%s_%d.png", gSystem->BaseName(script), nCanvases++));
80 }
81 } else
82 win->SaveAs(TString::Format("%s_%d.png", gSystem->BaseName(script), nCanvases++));
83 }
84 }
85 } else {
86 // no gClient
87 TVirtualPad* pad = 0;
88 TIter iCanvas(gROOT->GetListOfCanvases());
89 while((pad = (TVirtualPad*)iCanvas())) {
90 pad->SaveAs(TString::Format("%s_%d.png", gSystem->BaseName(script), nCanvases++));
91 }
92 }
93 if(!gROOT->IsBatch() && !gEnv->GetValue("X11.Sync", 0)) {
94 gVirtualX->Sync(0);
95 }
96 return kSuccess;
97}
int saveScriptOutput(const char *script, const char *outdir, Bool_t compiled)