GSI Object Oriented Online Offline (Go4)  GO4-5.3.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TGo4CommandLine.cpp
Go to the documentation of this file.
1 // $Id: TGo4CommandLine.cpp 2132 2018-09-14 09:52:45Z adamczew $
2 //-----------------------------------------------------------------------
3 // The GSI Online Offline Object Oriented (Go4) Project
4 // Experiment Data Processing at EE department, GSI
5 //-----------------------------------------------------------------------
6 // Copyright (C) 2000- GSI Helmholtzzentrum für Schwerionenforschung GmbH
7 // Planckstr. 1, 64291 Darmstadt, Germany
8 // Contact: http://go4.gsi.de
9 //-----------------------------------------------------------------------
10 // This software can be used under the license agreements as stated
11 // in Go4License.txt file which is part of the distribution.
12 //-----------------------------------------------------------------------
13 
14 #include "TGo4CommandLine.h"
15 
16 #include "Riostream.h"
17 #include "TROOT.h"
18 
19 #include <QFileDialog>
20 
21 #include "TGo4Log.h"
22 #include "TGo4QSettings.h"
23 #include "TGo4MacroDialog.h"
24 #include <exception>
25 
26 TGo4CommandLine::TGo4CommandLine(QWidget *parent, const char* name) :
27  QGo4Widget(parent, name),fbPythonBound(false)
28 {
29  setupUi(this);
30  LoadHistory();
31 }
32 
33 
35 {
36  QFileDialog fd( this, "Select ROOT macro to execute in GUI", QString(),
37  "ROOT macro (*.C *.c);;Go4 hotstart script (*.hotstart);;Python script(*.py)");
38  fd.setFileMode( QFileDialog::ExistingFile);
39 
40  if (fd.exec() != QDialog::Accepted) return;
41 
42  QStringList flst = fd.selectedFiles();
43  if (flst.isEmpty()) return;
44  bool iscint = fd.selectedNameFilter().contains(".C") || fd.selectedNameFilter().contains(".c");
45  bool ishot = fd.selectedNameFilter().contains(".hotstart");
46  bool ispyth = fd.selectedNameFilter().contains(".py");
47 
48  QString cmd;
49  if (iscint)
50  {
51  cmd = QString(".x ") + flst[0];
52  }
53  else if (ispyth)
54  {
55  cmd = QString("$") + flst[0];
56  }
57  else if (ishot)
58  {
59  cmd = flst[0];
60  }
61  else
62  {
63  std::cout << "TGo4CommandLine NEVER COME HERE - unknown file type!\n-" << std::endl;
64  return;
65  }
66 
67  int index=InputLine->findText(cmd);
68  if(index<0)
69  {
70  InputLine->insertItem(-1,cmd);
71  index=InputLine->findText(cmd);
72  }
73  //std::cout <<"inserted item "<< cmd.toLatin1().constData() << std::endl;
74  InputLine->setCurrentIndex(index);
75 
76 }
77 
79 {
81 }
82 
83 
85 {
86  const char pyprompt = '$';
87  QString pyinitfile="python/go4init.py"; // JAM put this to settings later?
88  QString str = InputLine->currentText();
89  if (str.length() == 0)
90  return;
91 
92  if (str.contains("help") || str.contains(".go4h"))
93  {
94  PrintHelp();
95  }
96  else if (str.contains(".hotstart") && !str.contains(".x"))
97  {
98  StatusMessage(QString("Executing hotstart script: ") + str);
99  StartHotstart(str.toLatin1().constData());
100  }
101  else
102  {
103  // process cint line, check if python or not:
104  if (str.contains(pyprompt))
105  {
106  // Python support as initiated by Sven Augustin, MPI Heidelberg
107  str.remove(0, str.indexOf(pyprompt) + 1);
108  StatusMessage(QString("Executing Python script: ") + str);
109  const QString PY_EXT = ".py";
110  int imin = str.indexOf(PY_EXT + " ");
111  int imax = str.length();
112 
113  if (imin == -1) {
114  imin = imax;
115  } else {
116  imin += PY_EXT.length();
117  }
118 
119  QString scrstring = str.left(imin);
120  QString argstring = str.mid(imin);
121 
122  str = "TPython::Exec(\"import sys; sys.argv = [\'" + scrstring + "\'] + \'" + argstring + "\'.split()\");";
123  str += "TPython::LoadMacro(\"" + scrstring + "\")";
124  if (!fbPythonBound)
125  {
126  QString go4sys=TGo4Log::GO4SYS();
127  str.prepend("TPython::LoadMacro(\"" + go4sys + pyinitfile +"\");");
128  str.prepend("TPython::Bind(TGo4AbstractInterface::Instance(), \"go4\");");
129  fbPythonBound = kTRUE;
130  }
131 
132  }
133  else
134  {
135  StatusMessage(QString("Executing command: ") + str);
136  }
137  try
138  {
139  gROOT->ProcessLineSync(str.toLatin1().constData());
140  go4sett->setCommandsHistoryGUI(InputLine->getHistory(50));
141  }
142  catch(std::exception& ex)
143  {
144  StatusMessage(QString("Error invoking command - Exception: ") + QString(ex.what()));
145  }
146  catch(...)
147  {
148  StatusMessage(QString("Error invoking command - unknown Exception!"));
149  }
150 
151  }
152 
153 }
154 
156 {
157 // read command history from settings:
158  QStringList histlist = go4sett->getCommandsHistoryGUI();
159  InputLine->addItems(histlist);
160 // prepared pre-loading of system macros:
161  gROOT->ProcessLineSync(Form(".L %s", TGo4Log::subGO4SYS("macros/corrhistos.C").Data()));
162  gROOT->ProcessLineSync(Form(".L %s", TGo4Log::subGO4SYS("macros/hishisto.C").Data()));
163  gROOT->ProcessLineSync(Form(".L %s", TGo4Log::subGO4SYS("macros/addhistos.C").Data()));
164  gROOT->ProcessLineSync(Form(".L %s", TGo4Log::subGO4SYS("macros/divhistos.C").Data()));
165  gROOT->ProcessLineSync(Form(".L %s", TGo4Log::subGO4SYS("macros/profileX.C").Data()));
166  gROOT->ProcessLineSync(Form(".L %s", TGo4Log::subGO4SYS("macros/profileY.C").Data()));
167  gROOT->ProcessLineSync(Form(".L %s", TGo4Log::subGO4SYS("macros/projectionX.C").Data()));
168  gROOT->ProcessLineSync(Form(".L %s", TGo4Log::subGO4SYS("macros/projectionY.C").Data()));
169  gROOT->ProcessLineSync(Form(".L %s", TGo4Log::subGO4SYS("macros/rebin.C").Data()));
170  gROOT->ProcessLineSync(Form(".L %s", TGo4Log::subGO4SYS("macros/scalex.C").Data()));
171  gROOT->ProcessLineSync(Form(".L %s", TGo4Log::subGO4SYS("macros/fft.C").Data()));
172 
173 }
174 
175 
177 {
178  TGo4MacroDialog md;
179  if (md.exec() != QDialog::Accepted) return;
180  InputLine->insertItem(0, md.getCommand());
181  InputLine->setCurrentIndex(0);
182 }
183 
184 
186 {
187  std::cout <<"\n--- Go4 GUI command line short help --- " << std::endl;
188  std::cout <<"- execute any command by pressing RETURN (Enter key)" << std::endl;
189  std::cout <<"--" << std::endl;
190  std::cout <<"- use '.h' for help concerning ROOT commands" << std::endl;
191  std::cout <<"--" << std::endl;
192  std::cout <<"- use 'go4->...' to access TGo4AbstractInterface for gui commands" << std::endl;
193  std::cout <<"--" << std::endl;
194  std::cout <<"- Some useful Go4 GUI command functions:" << std::endl;
195 
196  std::cout <<"\t- TObject* go4->GetObject(const char* itemname);"<< std::endl;
197  std::cout <<"\t\t: get browser object by full pathname. " << std::endl;
198  std::cout <<"\t- TString go4->FindItem(const char* name); " << std::endl;
199  std::cout <<"\t\t: get full pathname of browser object by name " << std::endl;
200  std::cout <<"\t- TString go4->SaveToMemory(const char* path, TObject* obj, Bool_t ownership = kFALSE);"<< std::endl;
201  std::cout <<"\t\t: Put object obj into workspace memory under subfolder path. Returns full item pathname. " << std::endl;
202  std::cout <<"\t- ViewPanelHandle go4->StartViewPanel()"<< std::endl;
203  std::cout <<"\t\t: Open new Go4 viewpanel with default size. Returns handle of new panel." << std::endl;
204  std::cout <<"\t- Bool_t go4->DrawItem(const char* itemname, ViewPanelHandle panel = 0, const char* drawopt = 0);"<< std::endl;
205  std::cout <<"\t\t: Draw object of full name itemname onto viewpanel panel. Will open new viewpanel if panel not specified. \n\t ROOT drawoption may be set. " << std::endl;
206  std::cout <<"\t- go4->OpenFile(const char* fname)" << std::endl;
207  std::cout <<"\t\t: open ROOT file of fname in Go4 browser " << std::endl;
208  std::cout <<"\t- go4->LaunchAnalysis()" << std::endl;
209  std::cout <<"\t\t: Start Go4 analysis process with previous set up " << std::endl;
210  std::cout <<"--" << std::endl;
211  std::cout <<"---- Please: note the Go4 helpwindow with TGo4AbstractInterface full method documentation!" << std::endl;
212  std::cout <<"---- see example scripts at $GO4SYS/macros !" << std::endl;
213  std::cout <<"---- visit our website at: \t http://go4.gsi.de !" << std::endl;
214  std::cout <<"------ " << std::endl;
215  HelpWindow("docs/Go4Reference.pdf", "Show Go4 Reference manual...");
216 }
void StartHotstart(const char *filename)
Definition: QGo4Widget.cpp:339
TGo4CommandLine(QWidget *parent=0, const char *name=0)
virtual void ExecuteSlot()
virtual void LoadHistory()
virtual void FileSearchDialog()
const QString & getCommand()
void StatusMessage(const QString &message)
Definition: QGo4Widget.cpp:244
virtual void PrintHelp()
TGo4QSettings * go4sett
virtual void PredefinedDialog()
void HelpWindow(const char *filename, const char *msg=0)
Definition: QGo4Widget.cpp:334
virtual void enterPressedSlot()
QStringList getCommandsHistoryGUI()
static TString subGO4SYS(const char *subdir)
Definition: TGo4Log.cxx:192
static const char * GO4SYS()
Definition: TGo4Log.cxx:158
void setCommandsHistoryGUI(const QStringList &commands)