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