ROOT logo
// @(#)root/base:$Id$
// Author: Rene Brun   29/12/99

/*************************************************************************
 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#include "Riostream.h"
#include "TROOT.h"
#include "TExec.h"

ClassImp(TExec)

//______________________________________________________________________________
//
//   TExec is a utility class that can be used to execute a CINT command
//   when some event happens in a pad.
//   The command in turn can invoke a CINT macro to paint graphics objects
//   at positions depending on the histogram or graph contents.
//
// Case 1:
//
// The TExec object is in the list of pad primitives (after exec.Draw()).
// When the pad is drawn, the TExec::Paint function is called. This function
// will execute the specified command.
// The following example uses the services of the class Aclock created
// in $ROOTSYS/test/Aclock.cxx.
// This examples uses a TTimer to redraw a pad at regular intervals (clock).
// When the clock is updated, a string with the current date&time is drawn.
//{
//   gSystem->Load("$ROOTSYS/test/Aclock");
//   Aclock ck(400);
//   gPad->SetFillColor(5);
//   TDatime dt;
//   TText t(.5,.3,"t");
//   t.SetTextAlign(22);
//   t.SetTextSize(.07);
//   t.SetTextColor(4);
//   t.Draw();
//   TExec ex("ex","dt.Set();t.SetTitle(dt.AsString())");
//   ex.Draw();
//}
//
// Case 2:
//
// The TExec object may be added to the list of functions of a TH1 or TGraph
// object via hist->GetListOfFunctions()->Add(exec).
// When the histogram (or graph) is drawn, the TExec will be executed.
// If the histogram is made persistent on a file, the TExec object
// is also saved with the histogram. When redrawing the histogram in a
// new session, the TExec will be executed.
// Example:
//     Assume an histogram TH1F *h already filled.
//     TExec *ex1 = new TExec("ex1","DoSomething()");
//     TExec *ex2 = new TExec("ex2",".x macro.C");
//     h->GetListOfFunctions()->Add(ex1);
//     h->GetListOfFunctions()->Add(ex2);
//     h->Draw();
//  When the Paint function for the histogram will be called, the "DoSomething"
//  function will be called (interpreted or compiled) and also the macro.C.
//
// Case 3:
//
// A TExec object is automatically generated when invoking TPad::AddExec.
// Each pad contains a TList of TExecs (0, 1 or more). When a mouse event
// (motion, click, etc) happens, the pad object executes sequentially
// this list of TExecs. In the code (interpreted or compiled) executed
// by the TExec referenced command, one can call the pad service functions
// such as TPad::GetEvent, TPad::GetEventX, TPad::GetEventY to find
// which type of event and the X,Y position of the mouse.
// By default, the list of TExecs is executed. This can be disabled
// via the canvas menu "Option".
// See $ROOTSYS/tutorials/hist/exec2.C for an example.
//    Root > TFile f("hsimple.root");
//    Root > hpxpy.Draw();
//    Root > c1.AddExec("ex2",".x exec2.C");
//    When moving the mouse in the canvas, a second canvas shows the
//    projection along X of the bin corresponding to the Y position
//    of the mouse. The resulting histogram is fitted with a gaussian.
//    A "dynamic" line shows the current bin position in Y.
//    This more elaborated example can be used as a starting point
//    to develop more powerful interactive applications exploiting CINT
//    as a development engine.
//
// The 3 options above can be combined.


//______________________________________________________________________________
TExec::TExec(): TNamed()
{
   // Exec default constructor.
}


//______________________________________________________________________________
TExec::TExec(const char *name, const char *command) : TNamed(name,command)
{
   // Exec normal constructor.
}


//______________________________________________________________________________
TExec::~TExec()
{
   // Exec default destructor.
}


//______________________________________________________________________________
TExec::TExec(const TExec &e) : TNamed(e)
{
   // Copy constructor.

   TNamed::Copy(*this);
}


//______________________________________________________________________________
void TExec::Exec(const char *command)
{
   // Execute the command referenced by this object.
   //
   //  if command is given, this command is executed
   // otherwise the default command of the object is executed
   //
   // if the default command (in the exec title) is empty, an attemp is made
   // to execute the exec name if it contains a "." or a "(", otherwise
   // the command ".x execname.C" is executed.
   // The function returns the result of the user function/script.

   if (command && (strlen(command) > 1))  gROOT->ProcessLine(command);
   else  {
      if (strlen(GetTitle()) > 0)         gROOT->ProcessLine(GetTitle());
      else  {
         if (strchr(GetName(),'('))      {gROOT->ProcessLine(GetName()); return;}
         if (strchr(GetName(),'.'))      {gROOT->ProcessLine(GetName()); return;}
         char action[512];
         snprintf(action, sizeof(action), ".x %s.C", GetName());
         gROOT->ProcessLine(action);
      }
   }
}


//______________________________________________________________________________
void TExec::Paint(Option_t *)
{
   // Execute the command referenced by this object.

   Exec();
}


//______________________________________________________________________________
void TExec::SavePrimitive(ostream &out, Option_t * /*= ""*/)
{
   // Save primitive as a C++ statement(s) on output stream out.

   char quote = '"';
   if (gROOT->ClassSaved(TExec::Class())) {
      out<<"   ";
   } else {
      out<<"   TExec *";
   }
   out<<"exec = new TExec("<<quote<<GetName()<<quote<<","<<quote<<GetTitle()<<quote<<");"<<endl;

   out<<"   exec->Draw();"<<endl;
}
 TExec.cxx:1
 TExec.cxx:2
 TExec.cxx:3
 TExec.cxx:4
 TExec.cxx:5
 TExec.cxx:6
 TExec.cxx:7
 TExec.cxx:8
 TExec.cxx:9
 TExec.cxx:10
 TExec.cxx:11
 TExec.cxx:12
 TExec.cxx:13
 TExec.cxx:14
 TExec.cxx:15
 TExec.cxx:16
 TExec.cxx:17
 TExec.cxx:18
 TExec.cxx:19
 TExec.cxx:20
 TExec.cxx:21
 TExec.cxx:22
 TExec.cxx:23
 TExec.cxx:24
 TExec.cxx:25
 TExec.cxx:26
 TExec.cxx:27
 TExec.cxx:28
 TExec.cxx:29
 TExec.cxx:30
 TExec.cxx:31
 TExec.cxx:32
 TExec.cxx:33
 TExec.cxx:34
 TExec.cxx:35
 TExec.cxx:36
 TExec.cxx:37
 TExec.cxx:38
 TExec.cxx:39
 TExec.cxx:40
 TExec.cxx:41
 TExec.cxx:42
 TExec.cxx:43
 TExec.cxx:44
 TExec.cxx:45
 TExec.cxx:46
 TExec.cxx:47
 TExec.cxx:48
 TExec.cxx:49
 TExec.cxx:50
 TExec.cxx:51
 TExec.cxx:52
 TExec.cxx:53
 TExec.cxx:54
 TExec.cxx:55
 TExec.cxx:56
 TExec.cxx:57
 TExec.cxx:58
 TExec.cxx:59
 TExec.cxx:60
 TExec.cxx:61
 TExec.cxx:62
 TExec.cxx:63
 TExec.cxx:64
 TExec.cxx:65
 TExec.cxx:66
 TExec.cxx:67
 TExec.cxx:68
 TExec.cxx:69
 TExec.cxx:70
 TExec.cxx:71
 TExec.cxx:72
 TExec.cxx:73
 TExec.cxx:74
 TExec.cxx:75
 TExec.cxx:76
 TExec.cxx:77
 TExec.cxx:78
 TExec.cxx:79
 TExec.cxx:80
 TExec.cxx:81
 TExec.cxx:82
 TExec.cxx:83
 TExec.cxx:84
 TExec.cxx:85
 TExec.cxx:86
 TExec.cxx:87
 TExec.cxx:88
 TExec.cxx:89
 TExec.cxx:90
 TExec.cxx:91
 TExec.cxx:92
 TExec.cxx:93
 TExec.cxx:94
 TExec.cxx:95
 TExec.cxx:96
 TExec.cxx:97
 TExec.cxx:98
 TExec.cxx:99
 TExec.cxx:100
 TExec.cxx:101
 TExec.cxx:102
 TExec.cxx:103
 TExec.cxx:104
 TExec.cxx:105
 TExec.cxx:106
 TExec.cxx:107
 TExec.cxx:108
 TExec.cxx:109
 TExec.cxx:110
 TExec.cxx:111
 TExec.cxx:112
 TExec.cxx:113
 TExec.cxx:114
 TExec.cxx:115
 TExec.cxx:116
 TExec.cxx:117
 TExec.cxx:118
 TExec.cxx:119
 TExec.cxx:120
 TExec.cxx:121
 TExec.cxx:122
 TExec.cxx:123
 TExec.cxx:124
 TExec.cxx:125
 TExec.cxx:126
 TExec.cxx:127
 TExec.cxx:128
 TExec.cxx:129
 TExec.cxx:130
 TExec.cxx:131
 TExec.cxx:132
 TExec.cxx:133
 TExec.cxx:134
 TExec.cxx:135
 TExec.cxx:136
 TExec.cxx:137
 TExec.cxx:138
 TExec.cxx:139
 TExec.cxx:140
 TExec.cxx:141
 TExec.cxx:142
 TExec.cxx:143
 TExec.cxx:144
 TExec.cxx:145
 TExec.cxx:146
 TExec.cxx:147
 TExec.cxx:148
 TExec.cxx:149
 TExec.cxx:150
 TExec.cxx:151
 TExec.cxx:152
 TExec.cxx:153
 TExec.cxx:154
 TExec.cxx:155
 TExec.cxx:156
 TExec.cxx:157
 TExec.cxx:158
 TExec.cxx:159
 TExec.cxx:160
 TExec.cxx:161
 TExec.cxx:162
 TExec.cxx:163
 TExec.cxx:164
 TExec.cxx:165
 TExec.cxx:166
 TExec.cxx:167
 TExec.cxx:168
 TExec.cxx:169
 TExec.cxx:170
 TExec.cxx:171
 TExec.cxx:172