Logo ROOT   6.07/09
Reference Guide
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 //______________________________________________________________________________
23 Int_t exec_macro(const char *macro, Bool_t comp = kFALSE, Bool_t save = kTRUE)
24 {
25 
26  enum EErrorCodes {
27  kSuccess,
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->DirName(macro)))
37  return kScriptDirNotFound;
38  Int_t err = 0;
39  TString cmd(".x ");
40  cmd += gSystem->BaseName(macro);
41  if (comp) cmd += "+";
42  gVirtualX->Sync(1);
43  gROOT->ProcessLine(cmd, &err);
44  if (err != TInterpreter::kNoError)
45  return kCannotRunScript;
46  gSystem->cd(pwd);
47 
48  UInt_t nMainFrames = 0;
49  TClass* clGMainFrame = TClass::GetClass("TGMainFrame");
50  TGWindow* win = 0;
51  TIter iWin(gClient->GetListOfWindows());
52  while ((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",
62  ++nMainFrames));
63  TImage *img = TImage::Create();
64  win->RaiseWindow();
65  img->FromWindow(win->GetId());
66  img->WriteImage(outfile.Data());
67  delete img;
68  }
69  }
70  }
71  if (!gEnv->GetValue("X11.Sync", 0))
72  gVirtualX->Sync(0);
73  return kSuccess;
74 }
virtual const char * BaseName(const char *pathname)
Base name of a file name. Base name of /user/root is root.
Definition: TSystem.cxx:929
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition: TObject.cxx:488
const TGWindow * GetParent() const
Definition: TGWindow.h:87
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:635
virtual void WriteImage(const char *, EImageFileTypes=TImage::kUnknown)
Definition: TImage.h:123
#define gROOT
Definition: TROOT.h:364
Bool_t cd(const char *path)
Definition: TSystem.h:414
Basic string class.
Definition: TString.h:137
#define gClient
Definition: TGClient.h:174
int Int_t
Definition: RtypesCore.h:41
virtual const char * DirName(const char *pathname)
Return the directory name in pathname.
Definition: TSystem.cxx:997
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
An abstract interface to image processing library.
Definition: TImage.h:37
const char * Data() const
Definition: TString.h:349
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:2335
virtual void FromWindow(Drawable_t, Int_t=0, Int_t=0, UInt_t=0, UInt_t=0)
Definition: TImage.h:252
const char * pwd()
Definition: TSystem.h:415
R__EXTERN TSystem * gSystem
Definition: TSystem.h:549
virtual Int_t GetValue(const char *name, Int_t dflt)
Returns the integer value for a resource.
Definition: TEnv.cxx:496
unsigned int UInt_t
Definition: RtypesCore.h:42
The ROOT global object gROOT contains a list of all defined classes.
Definition: TClass.h:81
#define gVirtualX
Definition: TVirtualX.h:362
virtual void MapRaised()
Definition: TGWindow.h:92
virtual void RaiseWindow()
Definition: TGWindow.h:96
R__EXTERN TEnv * gEnv
Definition: TEnv.h:174
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:2882
Mother of all ROOT objects.
Definition: TObject.h:44
static TImage * Create()
Create an image.
Definition: TImage.cxx:36
const Bool_t kTRUE
Definition: Rtypes.h:91
Handle_t GetId() const
Definition: TGObject.h:52