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