Logo ROOT   6.18/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
21TExec is a utility class that can be used to execute a C++ command
22when some event happens in a pad.
23The command in turn can invoke a C++ macro to paint graphics objects
24at positions depending on the histogram or graph contents.
25
26### Case 1:
27
28The TExec object is in the list of pad primitives (after exec.Draw()).
29When the pad is drawn, the TExec::Paint function is called. This function
30will execute the specified command.
31The following example uses the services of the class Aclock created
32in `$ROOTSYS/test/Aclock.cxx`.
33This examples uses a TTimer to redraw a pad at regular intervals (clock).
34When 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
53The TExec object may be added to the list of functions of a TH1 or TGraph
54object via hist->GetListOfFunctions()->Add(exec).
55When the histogram (or graph) is drawn, the TExec will be executed.
56If the histogram is made persistent on a file, the TExec object
57is also saved with the histogram. When redrawing the histogram in a
58new session, the TExec will be executed.
59
60Example:
61
62Assume 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
71When the Paint function for the histogram will be called, the "DoSomething"
72function will be called (interpreted or compiled) and also the macro.C.
73
74### Case 3:
75
76A TExec object is automatically generated when invoking TPad::AddExec.
77Each pad contains a TList of TExecs (0, 1 or more). When a mouse event
78(motion, click, etc) happens, the pad object executes sequentially
79this list of TExecs. In the code (interpreted or compiled) executed
80by the TExec referenced command, one can call the pad service functions
81such as TPad::GetEvent, TPad::GetEventX, TPad::GetEventY to find
82which type of event and the X,Y position of the mouse.
83By default, the list of TExecs is executed. This can be disabled
84via the canvas menu "Option".
85See $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~~~
91When moving the mouse in the canvas, a second canvas shows the
92projection along X of the bin corresponding to the Y position
93of the mouse. The resulting histogram is fitted with a gaussian.
94A "dynamic" line shows the current bin position in Y.
95This more elaborated example can be used as a starting point
96to develop more powerful interactive applications exploiting CINT
97as a development engine.
98
99The 3 options above can be combined.
100*/
101
102////////////////////////////////////////////////////////////////////////////////
103/// Exec default constructor.
104
106{
107}
108
109
110////////////////////////////////////////////////////////////////////////////////
111/// Exec normal constructor.
112
113TExec::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
143void 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
169void 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}
void Class()
Definition: Class.C:29
#define e(i)
Definition: RSha256.hxx:103
const char Option_t
Definition: RtypesCore.h:62
#define ClassImp(name)
Definition: Rtypes.h:365
char name[80]
Definition: TGX11.cxx:109
#define gROOT
Definition: TROOT.h:414
#define snprintf
Definition: civetweb.c:1540
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
virtual ~TExec()
Exec default destructor.
Definition: TExec.cxx:120
virtual void Paint(Option_t *option="")
Execute the command referenced by this object.
Definition: TExec.cxx:161
virtual void Exec(const char *command="")
Execute the command referenced by this object.
Definition: TExec.cxx:143
TExec()
Exec default constructor.
Definition: TExec.cxx:105
virtual void SavePrimitive(std::ostream &out, Option_t *option="")
Save primitive as a C++ statement(s) on output stream out.
Definition: TExec.cxx:169
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
virtual void Copy(TObject &named) const
Copy this to obj.
Definition: TNamed.cxx:94
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:48
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47