pxar
 All Classes Namespaces Functions Variables Typedefs Friends
pXar.cc
1 #ifndef PIX_H
2 #define PIX_H
3 
4 #include <iostream>
5 #include <algorithm>
6 #include <fstream>
7 #include <sys/stat.h>
8 
9 #if (defined WIN32)
10 #include <Windows4Root.h>
11 #endif
12 
13 #include <TApplication.h>
14 #include <TFile.h>
15 #include <TROOT.h>
16 #include <TRint.h>
17 #include <TSystem.h>
18 #include <TDatime.h>
19 
20 #include "ConfigParameters.hh"
21 #include "PixTestParameters.hh"
22 
23 #include "PixTest.hh"
24 #include "PixTestFactory.hh"
25 #include "PixUserTestFactory.hh"
26 #include "PixGui.hh"
27 #include "PixSetup.hh"
28 #include "PixUtil.hh"
29 
30 #include "api.h"
31 #include "log.h"
32 
33 
34 using namespace std;
35 using namespace pxar;
36 
37 void runGui(PixSetup &a, int argc = 0, char *argv[] = 0);
38 void createBackup(string a, string b);
39 
40 // ----------------------------------------------------------------------
41 int main(int argc, char *argv[]){
42 
43  LOG(logINFO) << "*** Welcome to pxar ***";
44  gSystem->Exec("git status");
45 
46  // -- command line arguments
47  string dir("."), cmdFile("nada"), rootfile("nada.root"), logfile("nada.log"),
48  verbosity("INFO"), flashFile("nada"), runtest("fulltest"), trimVcal(""), testParameters("nada");
49  bool doRunGui(false),
50  doRunScript(false),
51  doRunSingleTest(false),
52  doUpdateFlash(false),
53  doUpdateRootFile(false),
54  doUseRootLogon(false)
55  ;
56  for (int i = 0; i < argc; i++){
57  if (!strcmp(argv[i],"-h")) {
58  cout << "List of arguments:" << endl;
59  cout << "-a do not do tests, do not recreate rootfile, but read in existing rootfile" << endl;
60  cout << "-c filename read in commands from filename" << endl;
61  cout << "-d [--dir] path directory with config files" << endl;
62  cout << "-g start with GUI" << endl;
63  cout << "-p \"p1=v1[;p2=v2]\" set parameters for test" << endl;
64  cout << "-r rootfilename set rootfile (and logfile) name" << endl;
65  cout << "-t test run test" << endl;
66  cout << "-T [--vcal] XX read in DAC and Trim parameter files corresponding to trim VCAL = XX" << endl;
67  cout << "-v verbositylevel set verbosity level: QUIET CRITICAL ERROR WARNING DEBUG DEBUGAPI DEBUGHAL ..." << endl;
68  return 0;
69  }
70  if (!strcmp(argv[i],"-c")) {cmdFile = string(argv[++i]); doRunScript = true;}
71  if (!strcmp(argv[i],"-d") || !strcmp(argv[i], "--dir")) {dir = string(argv[++i]); }
72  if (!strcmp(argv[i],"-f")) {doUpdateFlash = true; flashFile = string(argv[++i]);}
73  if (!strcmp(argv[i],"-g")) {doRunGui = true; }
74  if (!strcmp(argv[i],"-p")) {testParameters = string(argv[++i]); }
75  if (!strcmp(argv[i],"-r")) {rootfile = string(argv[++i]); }
76  if (!strcmp(argv[i],"-t")) {doRunSingleTest = true; runtest = string(argv[++i]); }
77  if (!strcmp(argv[i],"-T") || !strcmp(argv[i], "--vcal")) {trimVcal = string(argv[++i]); }
78  if (!strcmp(argv[i],"-u")) {doUpdateRootFile = true;}
79  if (!strcmp(argv[i],"-v")) {verbosity = string(argv[++i]); }
80  }
81 
82  struct stat buffer;
83  if (stat("rootlogon.C", &buffer) == 0) {
84  LOG(logINFO) << "reading rootlogon.C, will use gStyle settings from there";
85  gROOT->Macro("rootlogon.C");
86  doUseRootLogon = true;
87  } else {
88  LOG(logINFO) << "no rootlogon.C found, live with the defaults provided";
89  }
90 
91  if (doRunScript) {
92  TRint *interpreter = new TRint("pXar", 0, 0, 0, true);
93  interpreter->ExecuteFile(cmdFile.c_str());
94  interpreter->Terminate(0);
95  LOG(logINFO) << "terminate and shut down";
96  }
97 
98 
99  pxar::pxarCore *api(0);
100  if (doUpdateFlash) {
101  api = new pxar::pxarCore("*", verbosity);
102  struct stat buffer;
103  if (stat(flashFile.c_str(), &buffer) == 0) {
104 
105  api->flashTB(flashFile);
106  } else {
107  LOG(logINFO) << "error: File " << flashFile << " not found" << endl;
108  }
109  delete api;
110  return 0;
111  }
112 
113 
114  ConfigParameters *configParameters = ConfigParameters::Singleton();
115  configParameters->setDirectory(dir);
116  string cfgFile = configParameters->getDirectory() + string("/configParameters.dat");
117  LOG(logINFO) << "pxar: reading config parameters from " << cfgFile;
118  if (!configParameters->readConfigParameterFile(cfgFile)) return 1;
119 
120  if (!rootfile.compare("nada.root")) {
121  rootfile = configParameters->getDirectory() + "/" + configParameters->getRootFileName();
122  } else {
123  configParameters->setRootFileName(rootfile);
124  rootfile = configParameters->getDirectory() + "/" + rootfile;
125  }
126 
127  if (trimVcal.compare("")) {
128  configParameters->setTrimVcalSuffix(trimVcal);
129  }
130 
131  logfile = rootfile;
132  PixUtil::replaceAll(logfile, ".root", ".log");
133 
134  LOG(logINFO)<< "pxar: dumping results into " << rootfile << " logfile = " << logfile;
135  TFile *rfile(0);
136  FILE* lfile;
137  if (doUpdateRootFile) {
138  rfile = TFile::Open(rootfile.c_str(), "UPDATE");
139  lfile = fopen(logfile.c_str(), "a");
140  SetLogOutput::Stream() = lfile;
141  SetLogOutput::Duplicate() = true;
142  } else {
143  createBackup(rootfile, logfile);
144  rfile = TFile::Open(rootfile.c_str(), "RECREATE");
145  lfile = fopen(logfile.c_str(), "a");
146  SetLogOutput::Stream() = lfile;
147  SetLogOutput::Duplicate() = true;
148  }
149 
150  TDatime today;
151  string tstamp = Form("%d/%02d/%02d", today.GetYear(), today.GetMonth(), today.GetDay());
152 
153  LOG(logINFO) << "*** Welcome to pxar ***";
154  LOG(logINFO) << Form("*** Today: %s", tstamp.c_str());
155 
156  vector<vector<pair<string,uint8_t> > > rocDACs = configParameters->getRocDacs();
157  vector<vector<pair<string,uint8_t> > > tbmDACs = configParameters->getTbmDacs();
158  vector<vector<pixelConfig> > rocPixels = configParameters->getRocPixelConfig();
159  vector<pair<string,uint8_t> > sig_delays = configParameters->getTbSigDelays();
160  vector<pair<string, double> > power_settings = configParameters->getTbPowerSettings();
161  vector<pair<std::string, uint8_t> > pg_setup = configParameters->getTbPgSettings();
162  string tbname = "*";
163  if (configParameters->getTbName() != "")
164  tbname = configParameters->getTbName();
165 
166  try {
167  api = new pxar::pxarCore(tbname, verbosity);
168 
169  api->initTestboard(sig_delays, power_settings, pg_setup);
170  if (configParameters->customI2cAddresses()) {
171  string i2cstring("");
172  vector<uint8_t> i2cAddr = configParameters->getI2cAddresses();
173  for (unsigned int i = 0; i < i2cAddr.size(); ++i) i2cstring += Form(" %d", (int)i2cAddr[i]);
174  LOG(logINFO) << "custom i2c addresses: " << i2cstring;
175  api->initDUT(configParameters->getHubId(),
176  configParameters->getTbmType(), tbmDACs,
177  configParameters->getRocType(), rocDACs,
178  rocPixels,
179  i2cAddr);
180  } else {
181  api->initDUT(configParameters->getHubId(),
182  configParameters->getTbmType(), tbmDACs,
183  configParameters->getRocType(), rocDACs,
184  rocPixels);
185  }
186 
187  // -- Set up the four signal probe outputs:
188  api->SignalProbe("a1", configParameters->getProbe("a1"));
189  api->SignalProbe("a2", configParameters->getProbe("a2"));
190  api->SignalProbe("d1", configParameters->getProbe("d1"));
191  api->SignalProbe("d2", configParameters->getProbe("d2"));
192 
193  LOG(logINFO) << "DUT info: ";
194  api->_dut->info();
195  }
196  catch (pxar::InvalidConfig &e){
197  std::cout << "pxar caught an exception due to invalid configuration settings: " << e.what() << std::endl;
198  delete api;
199  return -1;
200  }
201  catch (pxar::pxarException &e){
202  std::cout << "pxar caught an internal exception: " << e.what() << std::endl;
203  delete api;
204  return -1;
205  }
206  catch (...) {
207  std::cout << "pxar caught an unknown exception. Exiting." << std::endl;
208  delete api;
209  return -1;
210  }
211 
212  PixTestParameters *ptp = new PixTestParameters(configParameters->getDirectory() + "/"
213  + configParameters->getTestParameterFileName()
214  , true);
215  PixSetup a(api, ptp, configParameters);
216  a.setUseRootLogon(doUseRootLogon);
217  a.setRootFileUpdate(doUpdateRootFile);
218 
219  if (doRunGui) {
220  runGui(a, argc, argv);
221  } else if (doRunSingleTest) {
222  PixTestFactory *factory = PixTestFactory::instance();
223  PixUserTestFactory *userfactory = PixUserTestFactory::instance();
224  if (configParameters->getHvOn()) api->HVon();
225 
226  // -- search for subtest
227  string::size_type m0 = runtest.find(":");
228  string subtest("nada");
229  if (m0 != string::npos) {
230  subtest = runtest.substr(m0+1);
231  runtest = runtest.substr(0, m0);
232  }
233 
234  if (testParameters.compare("nada")) {
235  ptp->setTestParameters(runtest, testParameters);
236  }
237  PixTest *t = factory->createTest(runtest, &a);
238  if (0 == t) t = userfactory->createTest(runtest, &a);
239  if (t) {
240  if (subtest.compare("nada")) {
241  t->runCommand(subtest);
242  } else {
243  t->doTest();
244  }
245  delete t;
246  }
247  } else {
248  string input;
249  bool stop(false);
250  PixTestFactory *factory = PixTestFactory::instance();
251  PixUserTestFactory *userfactory = PixUserTestFactory::instance();
252  if (configParameters->getHvOn()) api->HVon();
253  LOG(logINFO) << "enter 'restricted' command line mode";
254  do {
255  LOG(logINFO) << "enter test to run";
256  cout << "pxar> ";
257  string input;
258  std::getline(cin, input);
259  if (input.size() == 0) stop = true;
260  string parameters("nada"), subtest("nada");
261  // -- split input with space into testname(s) and parameters
262  string::size_type m1 = input.find(" ");
263  if (m1 != string::npos) {
264  parameters = input.substr(m1+1);
265  input = input.substr(0, m1);
266  cout << "parameters: ->" << parameters << "<- input: ->" << input << "<-" << endl;
267  }
268  // -- find subtest
269  string::size_type m0 = input.find(":");
270  if (m0 != string::npos) {
271  subtest = input.substr(m0+1);
272  input = input.substr(0, m0);
273  cout << "subtest: ->" << subtest << "<- input: ->" << input << "<-" << endl;
274  }
275 
276 
277  if (!parameters.compare("nada")) {
278  LOG(logINFO) << " test: " << input << " no parameter change";
279  } else {
280  LOG(logINFO) << " test: " << input << " setting parameters: ->" << parameters << "<-";
281  ptp->setTestParameters(input, parameters);
282  }
283 
284  std::transform(subtest.begin(), subtest.end(), subtest.begin(), ::tolower);
285  std::transform(input.begin(), input.end(), input.begin(), ::tolower);
286 
287  if (!input.compare("savedacs")) {
288  a.writeDacParameterFiles();
289  continue;
290  }
291  if (!input.compare("savetrims")) {
292  a.writeTrimFiles();
293  continue;
294  }
295  if (!input.compare("savetbm")) {
296  a.writeTbmParameterFiles();
297  continue;
298  }
299 
300  if (!input.compare("gui")) runGui(a, argc, argv);
301  if (!input.compare("exit")) stop = true;
302  if (!input.compare("quit")) stop = true;
303  if (!input.compare("q")) stop = true;
304 
305  if (stop) break;
306  LOG(logINFO) << " running: " << input;
307  PixTest *t = factory->createTest(input, &a);
308  if (0 == t) t = userfactory->createTest(input, &a);
309  if (t) {
310  if (subtest.compare("nada")) {
311  t->runCommand(subtest);
312  } else {
313  t->doTest();
314  }
315  delete t;
316  } else {
317  LOG(logINFO) << "command ->" << input << "<- not known, ignored";
318  }
319  } while (!stop);
320 
321 
322  }
323 
324  // -- clean exit (however, you should not get here when running with the GUI)
325  a.getPixMonitor()->dumpSummaries();
326  rfile->Close();
327  if (api) delete api;
328 
329  LOG(logINFO) << "pXar: this is the end, my friend";
330 
331  return 0;
332 }
333 
334 
335 // ----------------------------------------------------------------------
336 void runGui(PixSetup &a, int /*argc*/, char ** /*argv[]*/) {
337 
338  TApplication theApp("App", 0, 0);
339  theApp.SetReturnFromRun(true);
340  PixGui gui(gClient->GetRoot(), 1300, 800, &a);
341  theApp.Run();
342  LOG(logINFO) << "closing down 0 ";
343 }
344 
345 
346 // ----------------------------------------------------------------------
347 void createBackup(string rootfile, string logfile) {
348 
349  Long_t id, flags, modtime;
350  Long64_t size;
351 
352  string nrootfile(rootfile), nlogfile(logfile);
353  const char *path = rootfile.c_str();
354  int result = gSystem->GetPathInfo(path, &id, &size, &flags, &modtime);
355  if (1 == result) return;
356 
357  TDatime d(modtime);
358  string tstamp = Form("_%d%02d%02d_%02d%02d%02d", d.GetYear(), d.GetMonth(), d.GetDay(), d.GetHour(), d.GetMinute(), d.GetSecond());
359  PixUtil::replaceAll(nrootfile, ".root", tstamp+".root");
360  PixUtil::replaceAll(nlogfile, ".log", tstamp+".log");
361 
362  LOG(logINFO) << "creating backup files for previous run: " << nrootfile << " and " << nlogfile;
363  if (!gSystem->AccessPathName(rootfile.c_str())) gSystem->Rename(rootfile.c_str(), nrootfile.c_str());
364  if (!gSystem->AccessPathName(logfile.c_str())) gSystem->Rename(logfile.c_str(), nlogfile.c_str());
365 
366 }
367 
368 #endif
static void replaceAll(std::string &str, const std::string &from, const std::string &to)
in str, replace all occurences of from to to
Definition: PixUtil.cc:24
virtual void runCommand(std::string command)
allow execution of any button in the test
Definition: PixTest.cc:129
virtual void doTest()
function connected to "DoTest" button of PixTab
Definition: PixTest.cc:602
Definition: PixGui.hh:43