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-2022, 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
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))
146 gROOT->ProcessLine(command);
147 else if (strlen(GetTitle()) > 0)
148 gROOT->ProcessLine(GetTitle());
149 else if (strchr(GetName(),'(') || strchr(GetName(),'.'))
150 gROOT->ProcessLine(GetName());
151 else {
152 auto action = TString::Format(".x %s.C", GetName());
153 gROOT->ProcessLine(action.Data());
154 }
155}
156
157////////////////////////////////////////////////////////////////////////////////
158/// Execute the command referenced by this object.
159
161{
162 Exec();
163}
164
165////////////////////////////////////////////////////////////////////////////////
166/// Save primitive as a C++ statement(s) on output stream out.
167
168void TExec::SavePrimitive(std::ostream &out, Option_t * /*= ""*/)
169{
170 char quote = '"';
171 if (gROOT->ClassSaved(TExec::Class()))
172 out<<" ";
173 else
174 out<<" TExec *";
175
176 out<<"exec = new TExec("<<quote<<GetName()<<quote<<", "
177 <<quote<<TString(GetTitle()).ReplaceSpecialCppChars()<<quote<<");"<<std::endl;
178
179 out<<" exec->Draw();"<<std::endl;
180}
#define e(i)
Definition RSha256.hxx:103
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:382
char name[80]
Definition TGX11.cxx:110
#define gROOT
Definition TROOT.h:406
TExec is a utility class that can be used to execute a C++ command when some event happens in a pad.
Definition TExec.h:26
virtual ~TExec()
Exec default destructor.
Definition TExec.cxx:120
void SavePrimitive(std::ostream &out, Option_t *option="") override
Save primitive as a C++ statement(s) on output stream out.
Definition TExec.cxx:168
void Paint(Option_t *option="") override
Execute the command referenced by this object.
Definition TExec.cxx:160
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
static TClass * Class()
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
void Copy(TObject &named) const override
Copy this to obj.
Definition TNamed.cxx:94
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:48
Basic string class.
Definition TString.h:139
TString & ReplaceSpecialCppChars()
Find special characters which are typically used in printf() calls and replace them by appropriate es...
Definition TString.cxx:1114
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2378