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