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