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