Logo ROOT  
Reference Guide
TExec.cxx
Go to the documentation of this file.
1 // @(#)root/base:$Id$
2 // Author: Rene Brun 29/12/99
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #include <iostream>
13 #include "TROOT.h"
14 #include "TExec.h"
15 #include "snprintf.h"
16 
18 
19 /** \class TExec
20 \ingroup Base
21 
22 TExec is a utility class that can be used to execute a C++ command
23 when some event happens in a pad.
24 The command in turn can invoke a C++ macro to paint graphics objects
25 at positions depending on the histogram or graph contents.
26 
27 ### Case 1:
28 
29 The TExec object is in the list of pad primitives (after exec.Draw()).
30 When the pad is drawn, the TExec::Paint function is called. This function
31 will execute the specified command.
32 The following example uses the services of the class Aclock created
33 in `$ROOTSYS/test/Aclock.cxx`.
34 This examples uses a TTimer to redraw a pad at regular intervals (clock).
35 When the clock is updated, a string with the current date&time is drawn.
36 ~~~ {.cpp}
37 {
38  gSystem->Load("$ROOTSYS/test/Aclock");
39  Aclock ck(400);
40  gPad->SetFillColor(5);
41  TDatime dt;
42  TText t(.5,.3,"t");
43  t.SetTextAlign(22);
44  t.SetTextSize(.07);
45  t.SetTextColor(4);
46  t.Draw();
47  TExec ex("ex","dt.Set();t.SetTitle(dt.AsString())");
48  ex.Draw();
49 }
50 ~~~
51 
52 ### Case 2:
53 
54 The TExec object may be added to the list of functions of a TH1 or TGraph
55 object via hist->GetListOfFunctions()->Add(exec).
56 When the histogram (or graph) is drawn, the TExec will be executed.
57 If the histogram is made persistent on a file, the TExec object
58 is also saved with the histogram. When redrawing the histogram in a
59 new session, the TExec will be executed.
60 
61 Example:
62 
63 Assume an histogram TH1F *h already filled.
64 ~~~ {.cpp}
65  TExec *ex1 = new TExec("ex1","DoSomething()");
66  TExec *ex2 = new TExec("ex2",".x macro.C");
67  h->GetListOfFunctions()->Add(ex1);
68  h->GetListOfFunctions()->Add(ex2);
69  h->Draw();
70 ~~~
71 
72 When the Paint function for the histogram will be called, the "DoSomething"
73 function will be called (interpreted or compiled) and also the macro.C.
74 
75 ### Case 3:
76 
77 A TExec object is automatically generated when invoking TPad::AddExec.
78 Each pad contains a TList of TExecs (0, 1 or more). When a mouse event
79 (motion, click, etc) happens, the pad object executes sequentially
80 this list of TExecs. In the code (interpreted or compiled) executed
81 by the TExec referenced command, one can call the pad service functions
82 such as TPad::GetEvent, TPad::GetEventX, TPad::GetEventY to find
83 which type of event and the X,Y position of the mouse.
84 By default, the list of TExecs is executed. This can be disabled
85 via the canvas menu "Option".
86 See $ROOTSYS/tutorials/hist/exec2.C for an example.
87 ~~~ {.cpp}
88  Root > TFile f("hsimple.root");
89  Root > hpxpy.Draw();
90  Root > c1.AddExec("ex2",".x exec2.C");
91 ~~~
92 When moving the mouse in the canvas, a second canvas shows the
93 projection along X of the bin corresponding to the Y position
94 of the mouse. The resulting histogram is fitted with a gaussian.
95 A "dynamic" line shows the current bin position in Y.
96 This more elaborated example can be used as a starting point
97 to develop more powerful interactive applications exploiting CINT
98 as a development engine.
99 
100 The 3 options above can be combined.
101 */
102 
103 ////////////////////////////////////////////////////////////////////////////////
104 /// Exec default constructor.
105 
107 {
108 }
109 
110 
111 ////////////////////////////////////////////////////////////////////////////////
112 /// Exec normal constructor.
113 
114 TExec::TExec(const char *name, const char *command) : TNamed(name,command)
115 {
116 }
117 
118 ////////////////////////////////////////////////////////////////////////////////
119 /// Exec default destructor.
120 
122 {
123 }
124 
125 ////////////////////////////////////////////////////////////////////////////////
126 /// Copy constructor.
127 
129 {
130  TNamed::Copy(*this);
131 }
132 
133 ////////////////////////////////////////////////////////////////////////////////
134 /// Execute the command referenced by this object.
135 ///
136 /// if command is given, this command is executed
137 /// otherwise the default command of the object is executed
138 ///
139 /// if the default command (in the exec title) is empty, an attempt is made
140 /// to execute the exec name if it contains a "." or a "(", otherwise
141 /// the command ".x execname.C" is executed.
142 /// The function returns the result of the user function/script.
143 
144 void TExec::Exec(const char *command)
145 {
146  if (command && (strlen(command) > 1)) gROOT->ProcessLine(command);
147  else {
148  if (strlen(GetTitle()) > 0) gROOT->ProcessLine(GetTitle());
149  else {
150  if (strchr(GetName(),'(')) {gROOT->ProcessLine(GetName()); return;}
151  if (strchr(GetName(),'.')) {gROOT->ProcessLine(GetName()); return;}
152  char action[512];
153  snprintf(action, sizeof(action), ".x %s.C", GetName());
154  gROOT->ProcessLine(action);
155  }
156  }
157 }
158 
159 ////////////////////////////////////////////////////////////////////////////////
160 /// Execute the command referenced by this object.
161 
163 {
164  Exec();
165 }
166 
167 ////////////////////////////////////////////////////////////////////////////////
168 /// Save primitive as a C++ statement(s) on output stream out.
169 
170 void TExec::SavePrimitive(std::ostream &out, Option_t * /*= ""*/)
171 {
172  char quote = '"';
173  if (gROOT->ClassSaved(TExec::Class())) {
174  out<<" ";
175  } else {
176  out<<" TExec *";
177  }
178  out<<"exec = new TExec("<<quote<<GetName()<<quote<<","<<quote<<GetTitle()<<quote<<");"<<std::endl;
179 
180  out<<" exec->Draw();"<<std::endl;
181 }
TExec::Exec
virtual void Exec(const char *command="")
Execute the command referenced by this object.
Definition: TExec.cxx:144
e
#define e(i)
Definition: RSha256.hxx:103
TExec::~TExec
virtual ~TExec()
Exec default destructor.
Definition: TExec.cxx:121
snprintf
#define snprintf
Definition: civetweb.c:1540
Option_t
const char Option_t
Definition: RtypesCore.h:66
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
TNamed::GetTitle
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:48
TExec
TExec is a utility class that can be used to execute a C++ command when some event happens in a pad.
Definition: TExec.h:28
TROOT.h
TNamed::Copy
virtual void Copy(TObject &named) const
Copy this to obj.
Definition: TNamed.cxx:94
TExec::Paint
virtual void Paint(Option_t *option="")
Execute the command referenced by this object.
Definition: TExec.cxx:162
TNamed
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
TExec::SavePrimitive
virtual void SavePrimitive(std::ostream &out, Option_t *option="")
Save primitive as a C++ statement(s) on output stream out.
Definition: TExec.cxx:170
TExec.h
TExec::TExec
TExec()
Exec default constructor.
Definition: TExec.cxx:106
name
char name[80]
Definition: TGX11.cxx:110
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
Class
void Class()
Definition: Class.C:29
gROOT
#define gROOT
Definition: TROOT.h:406