Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
exec_macro.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_gui
3/// This utility macro executes the macro "macro" given as first argument and save a capture in a png file.
4/// This macro is used by stressGUI to execute and compare the output of the GUI tutorials.
5///
6/// \macro_code
7///
8/// \author Bertrand Bellenot
9
10#include "TSystem.h"
11#include "TString.h"
12#include "TGClient.h"
13#include "TGWindow.h"
14#include "TClass.h"
15#include "THashList.h"
16#include "TROOT.h"
17#include "TInterpreter.h"
18#include "TEnv.h"
19#include "TVirtualX.h"
20#include "TImage.h"
21
22//______________________________________________________________________________
23Int_t exec_macro(const char *macro, Bool_t comp = kFALSE, Bool_t save = kTRUE)
24{
25
26 enum EErrorCodes {
28 kScriptDirNotFound,
29 kCannotRunScript,
30 kNumErrorCodes
31 };
32
33 if (gROOT->IsBatch() || !(gClient))
34 return kCannotRunScript;
35 TString pwd(gSystem->pwd());
36 if (!gSystem->cd(gSystem->GetDirName(macro)))
37 return kScriptDirNotFound;
38 Int_t err = 0;
39 TString cmd(".x ");
40 cmd += gSystem->BaseName(macro);
41 if (comp)
42 cmd += "+";
43 gVirtualX->Sync(1);
44 gROOT->ProcessLine(cmd, &err);
45 if (err != TInterpreter::kNoError)
46 return kCannotRunScript;
47 gSystem->cd(pwd);
48
49 UInt_t nMainFrames = 0;
50 TClass *clGMainFrame = TClass::GetClass("TGMainFrame");
51 TIter iWin(gClient->GetListOfWindows());
52 while (auto win = (TGWindow *)iWin()) {
53 const TObject *winGetParent = win->GetParent();
54 Bool_t winIsMapped = kFALSE;
55 if (winGetParent == gClient->GetDefaultRoot())
56 winIsMapped = kTRUE; // win->IsMapped();
57 if (winIsMapped && win->InheritsFrom(clGMainFrame)) {
58 win->MapRaised();
59 if (save) {
60 TString outfile = gSystem->BaseName(macro);
61 outfile.ReplaceAll(".C", TString::Format("_%d.png", ++nMainFrames));
62 win->RaiseWindow();
63
64 auto img = TGWindow::CreateImageFromWindow(win->GetId());
65 if (img)
66 img->WriteImage(outfile.Data());
67 }
68 }
69 }
70 if (!gEnv->GetValue("X11.Sync", 0))
71 gVirtualX->Sync(0);
72 return kSuccess;
73}
bool Bool_t
Boolean (0=false, 1=true) (bool)
Definition RtypesCore.h:78
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:60
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int)
Definition RtypesCore.h:61
constexpr Bool_t kFALSE
Definition RtypesCore.h:109
constexpr Bool_t kTRUE
Definition RtypesCore.h:108
R__EXTERN TEnv * gEnv
Definition TEnv.h:126
#define gClient
Definition TGClient.h:157
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t win
#define gROOT
Definition TROOT.h:417
R__EXTERN TSystem * gSystem
Definition TSystem.h:582
#define gVirtualX
Definition TVirtualX.h:379
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:84
static TClass * GetClass(const char *name, Bool_t load=kTRUE, Bool_t silent=kFALSE)
Static method returning pointer to TClass of the specified class name.
Definition TClass.cxx:2999
virtual Int_t GetValue(const char *name, Int_t dflt) const
Returns the integer value for a resource.
Definition TEnv.cxx:511
ROOT GUI Window base class.
Definition TGWindow.h:24
static std::unique_ptr< TImage > CreateImageFromWindow(Drawable_t wid)
Create TImage object with content of displayed window.
Definition TGWindow.cxx:364
Mother of all ROOT objects.
Definition TObject.h:42
Basic string class.
Definition TString.h:137
const char * Data() const
Definition TString.h:385
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition TString.h:714
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:2460
Bool_t cd(const char *path)
Definition TSystem.h:443
const char * pwd()
Definition TSystem.h:444
virtual const char * BaseName(const char *pathname)
Base name of a file name. Base name of /user/root is root.
Definition TSystem.cxx:949
virtual TString GetDirName(const char *pathname)
Return the directory name in pathname.
Definition TSystem.cxx:1047