00001 // @(#)root/base:$Id: TExec.cxx 35953 2010-09-30 16:28:42Z brun $ 00002 // Author: Rene Brun 29/12/99 00003 00004 /************************************************************************* 00005 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. * 00006 * All rights reserved. * 00007 * * 00008 * For the licensing terms see $ROOTSYS/LICENSE. * 00009 * For the list of contributors see $ROOTSYS/README/CREDITS. * 00010 *************************************************************************/ 00011 00012 #include "Riostream.h" 00013 #include "TROOT.h" 00014 #include "TExec.h" 00015 00016 ClassImp(TExec) 00017 00018 //______________________________________________________________________________ 00019 // 00020 // TExec is a utility class that can be used to execute a CINT command 00021 // when some event happens in a pad. 00022 // The command in turn can invoke a CINT macro to paint graphics objects 00023 // at positions depending on the histogram or graph contents. 00024 // 00025 // Case 1: 00026 // 00027 // The TExec object is in the list of pad primitives (after exec.Draw()). 00028 // When the pad is drawn, the TExec::Paint function is called. This function 00029 // will execute the specified command. 00030 // The following example uses the services of the class Aclock created 00031 // in $ROOTSYS/test/Aclock.cxx. 00032 // This examples uses a TTimer to redraw a pad at regular intervals (clock). 00033 // When the clock is updated, a string with the current date&time is drawn. 00034 //{ 00035 // gSystem->Load("$ROOTSYS/test/Aclock"); 00036 // Aclock ck(400); 00037 // gPad->SetFillColor(5); 00038 // TDatime dt; 00039 // TText t(.5,.3,"t"); 00040 // t.SetTextAlign(22); 00041 // t.SetTextSize(.07); 00042 // t.SetTextColor(4); 00043 // t.Draw(); 00044 // TExec ex("ex","dt.Set();t.SetTitle(dt.AsString())"); 00045 // ex.Draw(); 00046 //} 00047 // 00048 // Case 2: 00049 // 00050 // The TExec object may be added to the list of functions of a TH1 or TGraph 00051 // object via hist->GetListOfFunctions()->Add(exec). 00052 // When the histogram (or graph) is drawn, the TExec will be executed. 00053 // If the histogram is made persistent on a file, the TExec object 00054 // is also saved with the histogram. When redrawing the histogram in a 00055 // new session, the TExec will be executed. 00056 // Example: 00057 // Assume an histogram TH1F *h already filled. 00058 // TExec *ex1 = new TExec("ex1","DoSomething()"); 00059 // TExec *ex2 = new TExec("ex2",".x macro.C"); 00060 // h->GetListOfFunctions()->Add(ex1); 00061 // h->GetListOfFunctions()->Add(ex2); 00062 // h->Draw(); 00063 // When the Paint function for the histogram will be called, the "DoSomething" 00064 // function will be called (interpreted or compiled) and also the macro.C. 00065 // 00066 // Case 3: 00067 // 00068 // A TExec object is automatically generated when invoking TPad::AddExec. 00069 // Each pad contains a TList of TExecs (0, 1 or more). When a mouse event 00070 // (motion, click, etc) happens, the pad object executes sequentially 00071 // this list of TExecs. In the code (interpreted or compiled) executed 00072 // by the TExec referenced command, one can call the pad service functions 00073 // such as TPad::GetEvent, TPad::GetEventX, TPad::GetEventY to find 00074 // which type of event and the X,Y position of the mouse. 00075 // By default, the list of TExecs is executed. This can be disabled 00076 // via the canvas menu "Option". 00077 // See $ROOTSYS/tutorials/hist/exec2.C for an example. 00078 // Root > TFile f("hsimple.root"); 00079 // Root > hpxpy.Draw(); 00080 // Root > c1.AddExec("ex2",".x exec2.C"); 00081 // When moving the mouse in the canvas, a second canvas shows the 00082 // projection along X of the bin corresponding to the Y position 00083 // of the mouse. The resulting histogram is fitted with a gaussian. 00084 // A "dynamic" line shows the current bin position in Y. 00085 // This more elaborated example can be used as a starting point 00086 // to develop more powerful interactive applications exploiting CINT 00087 // as a development engine. 00088 // 00089 // The 3 options above can be combined. 00090 00091 00092 //______________________________________________________________________________ 00093 TExec::TExec(): TNamed() 00094 { 00095 // Exec default constructor. 00096 } 00097 00098 00099 //______________________________________________________________________________ 00100 TExec::TExec(const char *name, const char *command) : TNamed(name,command) 00101 { 00102 // Exec normal constructor. 00103 } 00104 00105 00106 //______________________________________________________________________________ 00107 TExec::~TExec() 00108 { 00109 // Exec default destructor. 00110 } 00111 00112 00113 //______________________________________________________________________________ 00114 TExec::TExec(const TExec &e) : TNamed(e) 00115 { 00116 // Copy constructor. 00117 00118 TNamed::Copy(*this); 00119 } 00120 00121 00122 //______________________________________________________________________________ 00123 void TExec::Exec(const char *command) 00124 { 00125 // Execute the command referenced by this object. 00126 // 00127 // if command is given, this command is executed 00128 // otherwise the default command of the object is executed 00129 // 00130 // if the default command (in the exec title) is empty, an attemp is made 00131 // to execute the exec name if it contains a "." or a "(", otherwise 00132 // the command ".x execname.C" is executed. 00133 // The function returns the result of the user function/script. 00134 00135 if (command && (strlen(command) > 1)) gROOT->ProcessLine(command); 00136 else { 00137 if (strlen(GetTitle()) > 0) gROOT->ProcessLine(GetTitle()); 00138 else { 00139 if (strchr(GetName(),'(')) {gROOT->ProcessLine(GetName()); return;} 00140 if (strchr(GetName(),'.')) {gROOT->ProcessLine(GetName()); return;} 00141 char action[512]; 00142 snprintf(action, sizeof(action), ".x %s.C", GetName()); 00143 gROOT->ProcessLine(action); 00144 } 00145 } 00146 } 00147 00148 00149 //______________________________________________________________________________ 00150 void TExec::Paint(Option_t *) 00151 { 00152 // Execute the command referenced by this object. 00153 00154 Exec(); 00155 } 00156 00157 00158 //______________________________________________________________________________ 00159 void TExec::SavePrimitive(ostream &out, Option_t * /*= ""*/) 00160 { 00161 // Save primitive as a C++ statement(s) on output stream out. 00162 00163 char quote = '"'; 00164 if (gROOT->ClassSaved(TExec::Class())) { 00165 out<<" "; 00166 } else { 00167 out<<" TExec *"; 00168 } 00169 out<<"exec = new TExec("<<quote<<GetName()<<quote<<","<<quote<<GetTitle()<<quote<<");"<<endl; 00170 00171 out<<" exec->Draw();"<<endl; 00172 }