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