pxar
 All Classes Namespaces Functions Variables Typedefs Friends
ConfigParameters.cc
1 #include <iostream>
2 #include <fstream>
3 #include <sstream>
4 #include <string>
5 #include <vector>
6 #include <algorithm>
7 #include <bitset>
8 
9 #include <cstdlib>
10 #include <stdio.h>
11 
12 #include "dictionaries.h"
13 #include "log.h"
14 
15 #include "ConfigParameters.hh"
16 
17 using namespace std;
18 using namespace pxar;
19 
20 
21 ConfigParameters * ConfigParameters::fInstance = 0;
22 
23 // ----------------------------------------------------------------------
24 ConfigParameters::ConfigParameters() {
25  initialize();
26 }
27 
28 
29 // ----------------------------------------------------------------------
30 ConfigParameters::ConfigParameters(string filename) {
31  initialize();
32  readConfigParameterFile(filename);
33 }
34 
35 
36 // ----------------------------------------------------------------------
37 void ConfigParameters::initialize() {
38  fReadTbParameters = fReadTbmParameters = fReadDacParameters = fReadRocPixelConfig = fReadReadbackCal = false;
39  fnCol = 52;
40  fnRow = 80;
41  fnRocs = 16;
42  fnTbms = 1;
43  fnModules = 1;
44  fHubId = 31;
45  fI2cAddresses.clear();
46 
47  fHvOn = true;
48  fTbmEnable = true;
49  fTbmEmulator = false;
50  fKeithleyRemote = false;
51  fGuiMode = false;
52  fTbmChannel = 0;
53  fHalfModule = 0;
54 
55  fEmptyReadoutLength = 54;
56  fEmptyReadoutLengthADC = 64;
57  fEmptyReadoutLengthADCDual = 40;
58 
59  fTrimVcalSuffix = "";
60  fDACParametersFileName = "defaultDACParameters";
61  fTbmParametersFileName = "defaultTBMParameters";
62  fTBParametersFileName = "defaultTBParameters.dat";
63  fTrimParametersFileName = "defaultTrimParameters";
64  fTestParametersFileName = "defaultTestParameters.dat";
65  fMaskFileName = "defaultMaskFile.dat";
66  fLogFileName = "log.txt";
67  fDebugFileName = "debug.log";
68  fRootFileName = "expert.root";
69  fGainPedestalParameterFileName = "phCalibrationFitTanH";
70  fGainPedestalFileName = "phCalibration";
71  fReadbackCalFileName = "readbackCal";
72 
73  ia = -1.;
74  id = -1.;
75  va = -1.;
76  vd = -1.;
77 
78  fProbeA1 = "sdata1";
79  fProbeA2 = "sdata2";
80  fProbeD1 = "clk";
81  fProbeD2 = "ctr";
82 
83  rocZeroAnalogCurrent = 0.0;
84  fRocType = "psi46digv21respin";
85  fTbmType = "";
86  fHdiType = "bpix";
87  fTBName = "*";
88 }
89 
90 
91 // ----------------------------------------------------------------------
92 ConfigParameters* ConfigParameters::Singleton() {
93  if (fInstance == 0) {fInstance = new ConfigParameters();}
94  return fInstance;
95 }
96 
97 
98 // ----------------------------------------------------------------------
99 void ConfigParameters::readAllConfigParameterFiles() {
100  readTbParameters();
101  readRocPixelConfig();
102  readRocDacs();
103  readTbmDacs();
104  readReadbackCal();
105 }
106 
107 
108 // ----------------------------------------------------------------------
109 void ConfigParameters::writeAllFiles() {
110  writeConfigParameterFile();
111  writeTbParameterFile();
112 
113 // for (unsigned int i = 0; i < fnTbms; ++i) writeIdx.push_back(i);
114 // writeTbmParameterFiles(writeIdx);
115 
116 // writeIdx.clear();
117 // for (unsigned int i = 0; i < fnRocs; ++i) writeIdx.push_back(i);
118 // writeDacParameterFiles(writeIdx);
119 // writeTrimFiles(writeIdx);
120 
121 }
122 
123 
124 
125 // ----------------------------------------------------------------------
126 bool ConfigParameters::readConfigParameterFile(string file) {
127  ifstream _input(file.c_str());
128  if (!_input.is_open())
129  {
130  LOG(logINFO) << "Can not open file '" << file << "' to read Config Parameters.";
131 
132  return false;
133  }
134 
135  // Read file by lines
136  for (string _line; _input.good();)
137  {
138  getline(_input, _line);
139 
140  // Skip Empty Lines and Comments (starting from # or - )
141  if (!_line.length()
142  || '#' == _line[0]
143  || '-' == _line[0]) continue;
144 
145  istringstream _istring(_line);
146  string _name;
147  string _value;
148 
149  _istring >> _name >> _value;
150 
151  // Skip line in case any errors occured while reading parameters
152  if (_istring.fail() || !_name.length()) continue;
153 
154  int _ivalue = atoi(_value.c_str());
155  double dvalue = atof(_value.c_str());
156 
157  if (0 == _name.compare("testboardName")) { fTBName = _value; }
158  else if (0 == _name.compare("directory")) { fDirectory = _value; }
159 
160  else if (0 == _name.compare("tbParameters")) { setTBParameterFileName(_value); }
161  else if (0 == _name.compare("tbmParameters")) { setTbmParameterFileName(_value); }
162  else if (0 == _name.compare("testParameters")) { setTestParameterFileName(_value); }
163  else if (0 == _name.compare("dacParameters")) { setDACParameterFileName(_value); }
164  else if (0 == _name.compare("rootFileName")) { setRootFileName(_value); }
165  else if (0 == _name.compare("trimParameters")) { setTrimParameterFileName(_value); }
166  else if (0 == _name.compare("maskFile")) { setMaskFileName(_value); }
167 
168  else if (0 == _name.compare("nModules")) { fnModules = _ivalue; }
169  else if (0 == _name.compare("nRocs")) { readNrocs(_istring.str()); }
170  else if (0 == _name.compare("nTbms")) { fnTbms = _ivalue; }
171  else if (0 == _name.compare("hubId")) { fHubId = _ivalue; }
172  else if (0 == _name.compare("halfModule")) { fHalfModule = _ivalue; }
173  else if (0 == _name.compare("emptyReadoutLength")) { fEmptyReadoutLength = _ivalue; }
174  else if (0 == _name.compare("emptyReadoutLengthADC")) { fEmptyReadoutLengthADC = _ivalue; }
175  else if (0 == _name.compare("emptyReadoutLengthADCDual")) { fEmptyReadoutLengthADCDual = _ivalue; }
176  else if (0 == _name.compare("hvOn")) { fHvOn = (_ivalue>0); }
177  else if (0 == _name.compare("keithleyRemote")) { fKeithleyRemote = (_ivalue>0); }
178  else if (0 == _name.compare("tbmEnable")) { fTbmEnable = (_ivalue>0); }
179  else if (0 == _name.compare("tbmEmulator")) { fTbmEmulator = (_ivalue>0); }
180  else if (0 == _name.compare("tbmChannel")) { fTbmChannel = _ivalue; }
181 
182  else if (0 == _name.compare("ia")) { ia = static_cast<float>((dvalue > 1000.?.001:1.) * dvalue); }
183  else if (0 == _name.compare("id")) { id = static_cast<float>((dvalue > 1000.?.001:1.) * dvalue); }
184  else if (0 == _name.compare("va")) { va = static_cast<float>((dvalue > 1000.?.001:1.) * dvalue); }
185  else if (0 == _name.compare("vd")) { vd = static_cast<float>((dvalue > 1000.?.001:1.) * dvalue); }
186 
187  else if (0 == _name.compare("rocZeroAnalogCurrent")) { rocZeroAnalogCurrent = static_cast<float>(.001 * static_cast<float>(_ivalue)); }
188 
189  else if (0 == _name.compare("rocType")) { fRocType = _value; }
190  else if (0 == _name.compare("tbmType")) { fTbmType = _value; }
191  else if (0 == _name.compare("hdiType")) { fHdiType = _value; }
192 
193  else if (0 == _name.compare("probeA1")) { fProbeA1 = _value; }
194  else if (0 == _name.compare("probeA2")) { fProbeA2 = _value; }
195  else if (0 == _name.compare("probeD1")) { fProbeD1 = _value; }
196  else if (0 == _name.compare("probeD2")) { fProbeD2 = _value; }
197 
198 
199  else { LOG(logINFO) << "Did not understand '" << _name << "'."; }
200  }
201 
202  _input.close();
203 
204  fTbPowerSettings.push_back(make_pair("ia", ia));
205  fTbPowerSettings.push_back(make_pair("id", id));
206  fTbPowerSettings.push_back(make_pair("va", va));
207  fTbPowerSettings.push_back(make_pair("vd", vd));
208 
209  return true;
210 }
211 
212 
213 // ----------------------------------------------------------------------
214 vector<pair<string, uint8_t> > ConfigParameters::readDacFile(string fname) {
215  vector<pair<string, uint8_t> > rocDacs;
216 
217  // -- read in file
218  vector<string> lines;
219  char buffer[5000];
220  LOG(logINFO) << " reading " << fname;
221  ifstream is(fname.c_str());
222  while (is.getline(buffer, 200, '\n')) {
223  lines.push_back(string(buffer));
224  }
225  is.close();
226 
227  // -- parse lines
228  unsigned int ival(0);
229  uint8_t uval(0);
230  // unsigned char uval(0);
231 
232  string::size_type s1, s2;
233  string str1, str2, str3;
234  for (unsigned int i = 0; i < lines.size(); ++i) {
235  // cout << lines[i] << endl;
236  // -- remove tabs, adjacent spaces, leading and trailing spaces
237  cleanupString(lines[i]);
238  if (lines[i].length() < 2) continue;
239  s1 = lines[i].find(" ");
240  s2 = lines[i].rfind(" ");
241  if (s1 != s2) {
242  // -- with register number
243  str1 = lines[i].substr(0, s1);
244  str2 = lines[i].substr(s1+1, s2-s1-1);
245  str3 = lines[i].substr(s2+1);
246  } else {
247  // -- without register number
248  str2 = lines[i].substr(0, s1);
249  str3 = lines[i].substr(s1+1);
250  }
251 
252  if (string::npos != str3.find("0x")) {
253  sscanf(str3.c_str(), "%x", &ival);
254  } else {
255  ival = atoi(str3.c_str());
256  }
257  uval = ival;
258  rocDacs.push_back(make_pair(str2, uval));
259 
260  }
261 
262  return rocDacs;
263 }
264 
265 // ----------------------------------------------------------------------
266 vector<pair<string, uint8_t> > ConfigParameters::getTbParameters() {
267  if (!fReadTbParameters) {
268  readTbParameters();
269  }
270  return fTbParameters;
271 }
272 
273 
274 // ----------------------------------------------------------------------
275 void ConfigParameters::readTbParameters() {
276  if (!fReadTbParameters) {
277  string filename = fDirectory + "/" + fTBParametersFileName;
278  fTbParameters = readDacFile(filename);
279  // Cannot use LOG(...) for this printout, as pxarCore is instantiated only afterwards ...
280  for (unsigned int i = 0; i < fTbParameters.size(); ++i) {
281  // LOG(logDEBUG) << fTbParameters[i].first << ": " << (int)fTbParameters[i].second;
282  LOG(logINFO) << " " << fTbParameters[i].first << ": " << (int)fTbParameters[i].second;
283  }
284  fReadTbParameters = true;
285  }
286 }
287 
288 // ----------------------------------------------------------------------
289 vector<pair<string, double> > ConfigParameters::getTbPowerSettings() {
290  vector<pair<string, double> > v;
291  if (ia < 0.) {
292  LOG(logINFO) << "Read config files first!" << endl;
293  return v;
294  }
295  return fTbPowerSettings;
296 }
297 
298 // ----------------------------------------------------------------------
299 vector<pair<string, uint8_t> > ConfigParameters::getTbSigDelays() {
300  vector<pair<string, uint8_t> > a;
301 
302  RegisterDictionary *dict = RegisterDictionary::getInstance();
303  vector<string> sigdelays = dict->getAllDTBNames();
304 
305  if (!fReadTbParameters) readTbParameters();
306  for (unsigned int i = 0; i < fTbParameters.size(); ++i) {
307  for (unsigned int j = 0; j < sigdelays.size(); ++j) {
308  if (0 == fTbParameters[i].first.compare(sigdelays[j])) a.push_back(make_pair(sigdelays[j], fTbParameters[i].second));
309  }
310  }
311 
312  return a;
313 }
314 
315 // ----------------------------------------------------------------------
316 vector<pair<std::string, uint8_t> > ConfigParameters::getTbPgSettings() {
317 
318  vector<pair<std::string, uint8_t> > a;
319 
320  uint8_t wbc = 100;
321  vector<vector<pair<string, uint8_t> > > dacs = getRocDacs();
322  for (unsigned int idac = 0; idac < dacs[0].size(); ++idac) {
323  if (!dacs[0][idac].first.compare("wbc")) {
324  wbc = dacs[0][idac].second;
325  }
326  }
327 
328  if (fnTbms < 1) {
329  a.push_back(make_pair("resetroc",25)); // PG_RESR b001000
330  a.push_back(make_pair("calibrate",wbc+6)); // PG_CAL b000100
331  a.push_back(make_pair("trigger",16)); // PG_TRG b000010
332  a.push_back(make_pair("token",0)); // PG_TOK b000001
333  } else {
334  a.push_back(std::make_pair("resetroc",15)); // PG_REST
335  a.push_back(std::make_pair("calibrate",wbc+6)); // PG_CAL
336  a.push_back(std::make_pair("trigger;sync",0)); // PG_TRG PG_SYNC
337  }
338 
339  return a;
340 }
341 
342 
343 // ----------------------------------------------------------------------
344 vector<vector<pxar::pixelConfig> > ConfigParameters::getRocPixelConfig() {
345  if (!fReadRocPixelConfig) {
346  readRocPixelConfig();
347  }
348  return fRocPixelConfigs;
349 }
350 
351 // ----------------------------------------------------------------------
352 vector<pxar::pixelConfig> ConfigParameters::getRocPixelConfig(int i) {
353  if (!fReadRocPixelConfig) {
354  readRocPixelConfig();
355  }
356  return fRocPixelConfigs[i];
357 
358 
359 }
360 
361 // ----------------------------------------------------------------------
362 void ConfigParameters::readRocPixelConfig() {
363  string filename;
364  // -- read one mask file containing entire DUT mask
365  filename = fDirectory + "/" + fMaskFileName;
366  vector<bool> rocmasked;
367  for (unsigned int i = 0; i < fnRocs; ++i) rocmasked.push_back(false);
368 
369  vector<vector<pair<int, int> > > vmask = readMaskFile(filename);
370  for (unsigned int i = 0; i < vmask.size(); ++i) {
371  vector<pair<int, int> > v = vmask[i];
372  if (v.size() > 0) {
373  rocmasked[i] = true;
374  for (unsigned int j = 0; j < v.size(); ++j) {
375  LOG(logINFO) << "MASKED Roc " << i << " col/row: " << v[j].first << " " << v[j].second;
376  }
377  }
378  }
379 
380  // -- read all trim files and create pixelconfig vector
381  for (unsigned int i = 0; i < fnRocs; ++i) {
382  vector<pxar::pixelConfig> v;
383  for (uint8_t ic = 0; ic < fnCol; ++ic) {
384  for (uint8_t ir = 0; ir < fnRow; ++ir) {
385  pxar::pixelConfig a(ic,ir,0,false,true);
386  if (rocmasked[i]) {
387  vector<pair<int, int> > v = vmask[i];
388  for (unsigned int j = 0; j < v.size(); ++j) {
389  if (v[j].first == ic && v[j].second == ir) {
390  LOG(logINFO) << " masking Roc " << i << " col/row: " << v[j].first << " " << v[j].second;
391  a.setMask(true);
392  }
393  }
394  }
395  v.push_back(a);
396  }
397  }
398  std::stringstream fname;
399  fname << fDirectory << "/" << fTrimParametersFileName << fTrimVcalSuffix << "_C" << i << ".dat";
400  readTrimFile(fname.str(), v);
401  fRocPixelConfigs.push_back(v);
402  }
403 
404  fReadRocPixelConfig = true;
405 }
406 
407 
408 // ----------------------------------------------------------------------
409 void ConfigParameters::readTrimFile(string fname, vector<pxar::pixelConfig> &v) {
410 
411  // -- read in file
412  vector<string> lines;
413  char buffer[5000];
414  LOG(logINFO) << " reading " << fname;
415  ifstream is(fname.c_str());
416  while (is.getline(buffer, 200, '\n')) {
417  lines.push_back(string(buffer));
418  }
419  is.close();
420 
421  // -- parse lines
422  unsigned int ival(0), irow(0), icol(0);
423  uint8_t uval(0);
424 
425  string::size_type s1, s2;
426  string str1, str2, str3;
427  for (unsigned int i = 0; i < lines.size(); ++i) {
428  // -- remove tabs, adjacent spaces, leading and trailing spaces
429  replaceAll(lines[i], "\t", " ");
430  replaceAll(lines[i], "Pix", " ");
431  string::iterator new_end = unique(lines[i].begin(), lines[i].end(), bothAreSpaces);
432  lines[i].erase(new_end, lines[i].end());
433  if (0 == lines[i].length()) continue;
434  if (lines[i].substr(0, 1) == string(" ")) lines[i].erase(0, 1);
435  if (lines[i].substr(lines[i].length()-1, 1) == string(" ")) lines[i].erase(lines[i].length()-1, 1);
436  s1 = lines[i].find(" ");
437  s2 = lines[i].rfind(" ");
438  if (s1 != s2) {
439  str1 = lines[i].substr(0, s1);
440  str2 = lines[i].substr(s1+1, s2-s1);
441  str3 = lines[i].substr(s2+1);
442  } else {
443  LOG(logINFO) << "could not read line -->" << lines[i] << "<--";
444  }
445 
446  ival = atoi(str1.c_str());
447  icol = atoi(str2.c_str());
448  irow = atoi(str3.c_str());
449  uval = ival;
450  unsigned int index = icol*80+irow;
451  if (index <= v.size()) {
452  v[index].setTrim(uval);
453  } else {
454  LOG(logINFO) << " not matching entry in trim vector found for row/col = " << irow << "/" << icol;
455  }
456  }
457 
458 
459 }
460 
461 
462 // ----------------------------------------------------------------------
463 vector<vector<pair<int, int> > > ConfigParameters::readMaskFile(string fname) {
464 
465  vector<vector<pair<int, int> > > v;
466  vector<pair<int, int> > a;
467  for (unsigned int i = 0; i < fnRocs; ++i) {
468  v.push_back(a);
469  }
470 
471  // -- read in file
472  vector<string> lines;
473  char buffer[5000];
474  LOG(logINFO) << " reading " << fname;
475  ifstream is(fname.c_str());
476  while (is.getline(buffer, 200, '\n')) {
477  lines.push_back(string(buffer));
478  }
479  is.close();
480 
481  // -- parse lines
482  unsigned int iroc(0), irow(0), icol(0);
483 
484  string::size_type s1, s2, s3;
485  string str1, str2, str3;
486  for (unsigned int i = 0; i < lines.size(); ++i) {
487  // cout << lines[i] << endl;
488  if (lines[i].substr(0, 1) == string("#")) continue;
489  // -- remove tabs, adjacent spaces, leading and trailing spaces
490  cleanupString(lines[i]);
491  if (0 == lines[i].length()) continue;
492 
493  s1 = lines[i].find("roc");
494  if (string::npos != s1) {
495  str3 = lines[i].substr(s1+4);
496  iroc = atoi(str3.c_str());
497  // cout << "masking all pixels for ROC " << iroc << endl;
498  if (iroc < fnRocs) {
499  for (uint8_t ic = 0; ic < fnCol; ++ic) {
500  for (uint8_t ir = 0; ir < fnRow; ++ir) {
501  v[iroc].push_back(make_pair(ic, ir));
502  }
503  }
504  } else {
505  LOG(logINFO) << "illegal ROC coordinates in line " << i << ": " << lines[i];
506  }
507  continue;
508  }
509 
510  s1 = lines[i].find("row");
511  if (string::npos != s1) {
512  s1 += 4;
513  s2 = lines[i].find(" ", s1) + 1;
514  iroc = atoi(lines[i].substr(s1, s2-s1-1).c_str());
515  irow = atoi(lines[i].substr(s2).c_str());
516  // cout << "-> MASKING ROC " << iroc << " row " << irow << endl;
517  if (iroc < fnRocs && irow < fnRow) {
518  for (unsigned int ic = 0; ic < fnCol; ++ic) {
519  v[iroc].push_back(make_pair(ic, irow));
520  }
521  } else {
522  LOG(logINFO) << "illegal ROC/row coordinates in line " << i << ": " << lines[i];
523  }
524  continue;
525  }
526 
527  s1 = lines[i].find("col");
528  if (string::npos != s1) {
529  s1 += 4;
530  s2 = lines[i].find(" ", s1) + 1;
531  iroc = atoi(lines[i].substr(s1, s2-s1-1).c_str());
532  icol = atoi(lines[i].substr(s2).c_str());
533  // cout << "-> MASKING ROC " << iroc << " col " << icol << endl;
534  if (iroc < fnRocs && icol < fnCol) {
535  for (unsigned int ir = 0; ir < fnRow; ++ir) {
536  v[iroc].push_back(make_pair(icol, ir));
537  }
538  } else {
539  LOG(logINFO) << "illegal ROC/col coordinates in line " << i << ": " << lines[i];
540  }
541  continue;
542  }
543 
544  s1 = lines[i].find("pix");
545  if (string::npos != s1) {
546  s1 += 4;
547  s2 = lines[i].find(" ", s1) + 1;
548  s3 = lines[i].find(" ", s2) + 1;
549  iroc = atoi(lines[i].substr(s1, s2-s1-1).c_str());
550  icol = atoi(lines[i].substr(s2, s3-s2-1).c_str());
551  irow = atoi(lines[i].substr(s3).c_str());
552  // cout << "-> MASKING ROC " << iroc << " col " << icol << " row " << irow << endl;
553  if (iroc < fnRocs && icol < fnCol && irow < fnRow) {
554  v[iroc].push_back(make_pair(icol, irow));
555  } else {
556  LOG(logINFO) << "illegal ROC/row/col coordinates in line " << i << ": " << lines[i];
557  }
558  continue;
559  }
560  }
561 
562  return v;
563 }
564 
565 
566 // ----------------------------------------------------------------------
567 bool ConfigParameters::writeMaskFile(vector<vector<pair<int, int> > > v, string name) {
568  std::stringstream fname;
569  if (!name.compare("")) {
570  fname << fDirectory << "/" << fMaskFileName;
571  } else {
572  if (string::npos == name.find("/")) {
573  fname << fDirectory << "/" << name;
574  } else {
575  fname << name;
576  }
577  }
578 
579  ofstream OutputFile;
580  OutputFile.open((fname.str()).c_str());
581  if (!OutputFile.is_open()) {
582  LOG(logERROR) << "failed to open " << fname.str().c_str() << " as mask file";
583  return false;
584  } else {
585  LOG(logDEBUG) << "write masked pixels into " << fname.str();
586  }
587 
588  OutputFile << "# mask file generated by maskHotPixels" << endl;
589 
590  for (unsigned int iroc = 0; iroc < v.size(); ++iroc) {
591  vector<pair<int, int> > a = v[iroc];
592  for (unsigned int ipix = 0; ipix < a.size(); ++ipix) {
593  OutputFile << "pix " << iroc << " "
594  << a[ipix].first << " " << a[ipix].second
595  << endl;
596  }
597  }
598  OutputFile.close();
599  return true;
600 
601 }
602 
603 
604 
605 
606 // ----------------------------------------------------------------------
607 vector<vector<pair<string, uint8_t> > > ConfigParameters::getRocDacs() {
608  if (!fReadDacParameters) {
609  readRocDacs();
610  }
611  return fDacParameters;
612 }
613 
614 
615 // ----------------------------------------------------------------------
616 vector<string> ConfigParameters::getDacs() {
617  if (!fReadDacParameters) {
618  readRocDacs();
619  }
620  vector<string> names;
621  vector<std::pair<std::string, uint8_t> > dacs = fDacParameters[0];
622  for (unsigned int i = 0; i < dacs.size(); ++i) {
623  names.push_back(dacs[i].first);
624  }
625  return names;
626 }
627 
628 
629 // ----------------------------------------------------------------------
630 void ConfigParameters::readRocDacs() {
631  if (!fReadDacParameters) {
632  for (unsigned int i = 0; i < fnRocs; ++i) {
633  stringstream filename;
634  filename << fDirectory << "/" << fDACParametersFileName << fTrimVcalSuffix << "_C" << i << ".dat";
635  vector<pair<string, uint8_t> > rocDacs = readDacFile(filename.str());
636  fDacParameters.push_back(rocDacs);
637  }
638  fReadDacParameters = true;
639  }
640 }
641 
642 
643 // ----------------------------------------------------------------------
644 vector<vector<pair<string, uint8_t> > > ConfigParameters::getTbmDacs() {
645  if (!fReadTbmParameters) {
646  readTbmDacs();
647  }
648  return fTbmParameters;
649 }
650 
651 // ----------------------------------------------------------------------
652 void ConfigParameters::readTbmDacs() {
653  if (!fReadTbmParameters) {
654  string filename;
655  for (unsigned int i = 0; i < fnTbms; ++i) {
656  for (unsigned int ic = 0; ic < 2; ++ic) {
657  stringstream filename;
658  filename << fDirectory << "/" << fTbmParametersFileName << "_C" << i << (ic==0?"a":"b") << ".dat";
659  vector<pair<string, uint8_t> > rocDacs = readDacFile(filename.str());
660  fTbmParameters.push_back(rocDacs);
661  }
662  }
663  fReadTbmParameters = true;
664  }
665 }
666 
667 
668 // ----------------------------------------------------------------------
669 bool ConfigParameters::setTbParameter(std::string var, uint8_t val, bool appendIfNotFound) {
670  for (unsigned int i = 0; i < fTbParameters.size(); ++i) {
671  if (!fTbParameters[i].first.compare(var)) {
672  fTbParameters[i].second = val;
673  return true;
674  }
675  }
676  if (appendIfNotFound) {
677  fTbParameters.push_back(make_pair(var, val));
678  return true;
679  }
680  return false;
681 }
682 
683 
684 // ----------------------------------------------------------------------
685 bool ConfigParameters::setTbmDac(std::string var, uint8_t val, int itbm) {
686  bool changed(false);
687  for (unsigned int i = 0; i < fTbmParameters.size(); ++i) {
688  if (itbm < 0 || itbm == static_cast<int>(i)) {
689  for (unsigned int idac = 0; idac < fTbmParameters[i].size(); ++idac) {
690  if (!fTbmParameters[i][idac].first.compare(var)) {
691  fTbmParameters[i][idac].second = val;
692  changed = true;
693  break;
694  }
695  }
696  }
697  }
698  if (changed) {
699  return true;
700  }
701  return false;
702 }
703 
704 
705 // ----------------------------------------------------------------------
706 bool ConfigParameters::setRocDac(std::string var, uint8_t val, int iroc) {
707  bool changed(false);
708  for (unsigned int i = 0; i < fDacParameters.size(); ++i) {
709  if (iroc < 0 || iroc == static_cast<int>(i)) {
710  for (unsigned int idac = 0; idac < fDacParameters[i].size(); ++idac) {
711  if (!fDacParameters[i][idac].first.compare(var)) {
712  fDacParameters[i][idac].second = val;
713  changed = true;
714  break;
715  }
716  }
717  }
718  }
719  if (changed) {
720  return true;
721  }
722  return false;
723 }
724 
725 
726 // ----------------------------------------------------------------------
727 bool ConfigParameters::setTbPowerSettings(std::string var, double val) {
728 
729  for (unsigned int i = 0; i < fTbPowerSettings.size(); ++i) {
730  if (!fTbPowerSettings[i].first.compare(var)) {
731  fTbPowerSettings[i].second = val;
732  return true;
733  }
734  }
735  return false;
736 
737 }
738 
739 
740 // ----------------------------------------------------------------------
741 bool ConfigParameters::setTrimBits(int trim) {
742  for (unsigned int iroc = 0; iroc < fRocPixelConfigs.size(); ++iroc) {
743  for (unsigned int ipix = 0; ipix < fRocPixelConfigs[iroc].size(); ++ipix) {
744  fRocPixelConfigs[iroc][ipix].setTrim(trim);
745  }
746  }
747  return true;
748 }
749 
750 
751 // ----------------------------------------------------------------------
752 bool ConfigParameters::writeConfigParameterFile() {
753  char filename[1000];
754  sprintf(filename, "%s/configParameters.dat", fDirectory.c_str());
755  FILE * file = fopen(filename, "w");
756  if (!file)
757  {
758  LOG(logINFO) << "Can not open file '" << filename << "' to write configParameters.";
759  return false;
760  }
761 
762  LOG(logINFO) << "Writing Config-Parameters to '" << filename << "'.";
763 
764  fprintf(file, "testboardName %s\n\n", fTBName.c_str());
765 
766  fprintf(file, "-- parameter files\n\n");
767 
768  fprintf(file, "tbParameters %s\n", fTBParametersFileName.c_str());
769  fprintf(file, "dacParameters %s\n", fDACParametersFileName.c_str());
770  fprintf(file, "tbmParameters %s\n", fTbmParametersFileName.c_str());
771  fprintf(file, "trimParameters %s\n", fTrimParametersFileName.c_str());
772  fprintf(file, "maskFile %s\n", fMaskFileName.c_str());
773  fprintf(file, "testParameters %s\n", fTestParametersFileName.c_str());
774  fprintf(file, "rootFileName %s\n\n", fRootFileName.c_str());
775 
776  fprintf(file, "-- configuration\n\n");
777 
778  fprintf(file, "nModules %i\n", fnModules);
779  fprintf(file, "nRocs %i\n", fnRocs);
780  fprintf(file, "nTbms %i\n", fnTbms);
781  fprintf(file, "hubId %i\n", fHubId);
782  fprintf(file, "tbmEnable %i\n", fTbmEnable);
783  fprintf(file, "tbmEmulator %i\n", fTbmEmulator);
784  fprintf(file, "hvOn %i\n", fHvOn);
785  fprintf(file, "tbmChannel %i\n", fTbmChannel);
786  fprintf(file, "rocType %s\n", fRocType.c_str());
787  if (fnTbms > 0) fprintf(file, "tbmType %s\n", fTbmType.c_str());
788  fprintf(file, "hdiType %s\n", fHdiType.c_str());
789 
790  fprintf(file, "\n");
791  fprintf(file, "-- voltages and current limits\n\n");
792 
793  fprintf(file, "ia %i\n" , static_cast<int>(ia * 1000));
794  fprintf(file, "id %i\n" , static_cast<int>(id * 1000));
795  fprintf(file, "va %i\n" , static_cast<int>(va * 1000));
796  fprintf(file, "vd %i\n\n", static_cast<int>(vd * 1000));
797 
798  fprintf(file, "probeA1 %s\n", fProbeA1.c_str());
799  fprintf(file, "probeA2 %s\n", fProbeA2.c_str());
800  fprintf(file, "probeD1 %s\n", fProbeD1.c_str());
801  fprintf(file, "probeD2 %s\n", fProbeD2.c_str());
802 
803  fclose(file);
804  return true;
805 }
806 
807 
808 // ----------------------------------------------------------------------
809 bool ConfigParameters::writeTrimFile(int iroc, vector<pixelConfig> v) {
810  std::stringstream fname;
811  fname << fDirectory << "/" << fTrimParametersFileName << fTrimVcalSuffix << "_C" << iroc << ".dat";
812 
813  ofstream OutputFile;
814  OutputFile.open((fname.str()).c_str());
815  if (!OutputFile.is_open()) {
816  return false;
817  } else {
818  LOG(logDEBUG) << "write trim parameters into " << fname.str();
819  }
820 
821  for (std::vector<pixelConfig>::iterator ipix = v.begin(); ipix != v.end(); ++ipix) {
822  OutputFile << setw(2) << static_cast<int>(ipix->trim())
823  << " Pix " << setw(2)
824  << static_cast<int>(ipix->column()) << " " << setw(2) << static_cast<int>(ipix->row())
825  << endl;
826  }
827 
828  OutputFile.close();
829  return true;
830 }
831 
832 
833 // ----------------------------------------------------------------------
834 bool ConfigParameters::writeDacParameterFile(int iroc, vector<pair<string, uint8_t> > v) {
835 
836  std::stringstream fname;
837  fname << fDirectory << "/" << getDACParameterFileName() << fTrimVcalSuffix << "_C" << iroc << ".dat";
838 
839  ofstream OutputFile;
840  OutputFile.open((fname.str()).c_str());
841  if (!OutputFile.is_open()) {
842  return false;
843  } else {
844  LOG(logDEBUG) << "write dac parameters into " << fname.str();
845  }
846 
847  RegisterDictionary *a = RegisterDictionary::getInstance();
848  for (std::vector<std::pair<std::string,uint8_t> >::iterator idac = v.begin(); idac != v.end(); ++idac) {
849  // OutputFile << left << std::setw(10) << idac->first << " " << std::setw(3) << static_cast<int>(idac->second) << std::endl;
850  OutputFile << right << setw(3) << static_cast<int>(a->getRegister(idac->first, ROC_REG)) << " " << left
851  << setw(10) << idac->first << " " << setw(3) << static_cast<int>(idac->second)
852  << endl;
853  }
854 
855  OutputFile.close();
856  return true;
857 }
858 
859 
860 // ----------------------------------------------------------------------
861 bool ConfigParameters::writeTbmParameterFile(int itbm, vector<pair<string, uint8_t> > vA, vector<pair<string, uint8_t> > vB) {
862 
863  vector<pair<string, uint8_t> > v;
864  for (unsigned int ic = 0; ic < 2; ++ic) {
865  stringstream fname;
866  fname << fDirectory << "/" << fTbmParametersFileName << "_C" << itbm << (ic==0?"a":"b") << ".dat";
867 
868  ofstream OutputFile;
869  OutputFile.open((fname.str()).c_str());
870  if (!OutputFile.is_open()) {
871  return false;
872  } else {
873  LOG(logDEBUG) << "write tbm parameters into " << (fname.str()).c_str();
874  }
875 
876  if (0 == ic) {
877  v = vA;
878  } else {
879  v = vB;
880  }
881 
882  RegisterDictionary *a = RegisterDictionary::getInstance();
883  for (std::vector<std::pair<std::string, uint8_t> >::iterator idac = v.begin(); idac != v.end(); ++idac) {
884  OutputFile << right << setw(3) << setfill('0') << static_cast<int>(a->getRegister(idac->first, TBM_REG)) << " "
885  << idac->first
886  << " 0x" << setw(2) << setfill('0') << hex << static_cast<int>(idac->second)
887  << endl;
888  }
889 
890  OutputFile.close();
891  }
892  return true;
893 }
894 
895 
896 // ----------------------------------------------------------------------
897 bool ConfigParameters::writeTbParameterFile() {
898  string fname = fDirectory + "/" + getTBParameterFileName();
899  ofstream OutputFile;
900  string data;
901 
902  OutputFile.open(fname.c_str());
903  if (!OutputFile.is_open()) {
904  return false;
905  } else {
906  LOG(logDEBUG) << "write dtb parameters into " << fname.c_str();
907  }
908 
909  RegisterDictionary *a = RegisterDictionary::getInstance();
910  for (unsigned int idac = 0; idac < fTbParameters.size(); ++idac) {
911  data = fTbParameters[idac].first;
912  std::transform(data.begin(), data.end(), data.begin(), ::tolower);
913  OutputFile << right << setw(3) << static_cast<int>(a->getRegister(fTbParameters[idac].first, DTB_REG)) << " "
914  << setw(15) << fTbParameters[idac].first << " " << std::setw(3) << static_cast<int>(fTbParameters[idac].second)
915  << endl;
916  }
917 
918  OutputFile.close();
919 
920  return true;
921 }
922 
923 // ----------------------------------------------------------------------
924 bool ConfigParameters::writeTestParameterFile(string whichTest) {
925  string bla = "no implementation for dumping test parameters " + whichTest;
926  LOG(logINFO) << bla;
927  return true;
928 }
929 
930 
931 // ----------------------------------------------------------------------
932 void ConfigParameters::readGainPedestalParameters() {
933 
934  string bname = getGainPedestalParameterFileName();
935 
936  fGainPedestalParameters.clear();
937 
938  // -- read in file
939  vector<string> lines;
940  char buffer[5000];
941  ifstream is;
942  vector<gainPedestalParameters> rocPar;
943  for (unsigned int iroc = 0; iroc < fnRocs; ++iroc) {
944  lines.clear();
945  rocPar.clear();
946  std::stringstream fname;
947  fname.str(std::string());
948  fname << fDirectory << "/" << bname << fTrimVcalSuffix << "_C" << iroc << ".dat";
949  LOG(logINFO) << " reading " << (fname.str());
950  is.open((fname.str()).c_str());
951  if (!is.is_open()) {
952  LOG(logERROR) << "cannot open " << (fname.str()) << " for reading PH calibration constants";
953  return;
954  }
955 
956  while (is.getline(buffer, 200, '\n')) {
957  lines.push_back(string(buffer));
958  }
959  is.close();
960 
961  // -- parse lines
962  double p0, p1, p2, p3;
963  int icol, irow;
964  string pix;
965 
966  for (unsigned int i = 3; i < lines.size(); ++i) {
967  istringstream istring(lines[i]);
968  istring >> p0 >> p1 >> p2 >> p3 >> pix >> icol >> irow;
969  gainPedestalParameters a = {p0, p1, p2, p3};
970  rocPar.push_back(a);
971  }
972  fGainPedestalParameters.push_back(rocPar);
973  }
974 }
975 
976 // ----------------------------------------------------------------------
977 void ConfigParameters::writeGainPedestalParameters() {
978 
979  stringstream fname;
980 
981  for (unsigned int iroc = 0; iroc < fGainPedestalParameters.size(); ++iroc) {
982  fname.str(std::string());
983  fname << fDirectory << "/" << getGainPedestalParameterFileName() << fTrimVcalSuffix << "_C" << iroc << ".dat";
984  ofstream OutputFile;
985  OutputFile.open((fname.str()).c_str());
986  if (!OutputFile.is_open()) {
987  LOG(logERROR) << "Could not open " << fname.str();
988  return;
989  } else {
990  LOG(logDEBUG) << "write gain/ped parameters into " << fname.str();
991  }
992 
993  OutputFile << "Parameters of the vcal vs. pulse height fits" << endl;
994  OutputFile << "par[3] + par[2] * TMath::TanH(par[0]*x[0] - par[1])" << endl << endl;
995 
996  vector<gainPedestalParameters> pars = fGainPedestalParameters[iroc];
997  for (unsigned ipix = 0; ipix < pars.size(); ++ipix) {
998  OutputFile << scientific
999  << pars[ipix].p0 << " "
1000  << pars[ipix].p1 << " "
1001  << pars[ipix].p2 << " "
1002  << pars[ipix].p3;
1003  OutputFile.unsetf(ios::fixed | ios::scientific);
1004  OutputFile << " Pix "
1005  << setw(2) << ipix/80 << " " << setw(2) << ipix%80
1006  << endl;
1007  }
1008  OutputFile.close();
1009  }
1010 
1011 }
1012 
1013 // ----------------------------------------------------------------------
1014 void ConfigParameters::setGainPedestalParameters(vector<vector<gainPedestalParameters> >v) {
1015  fGainPedestalParameters.clear();
1016  for (unsigned int i = 0; i < v.size(); ++i) {
1017  fGainPedestalParameters.push_back(v[i]);
1018  }
1019 }
1020 
1021 // ----------------------------------------------------------------------
1022 std::vector<std::vector<gainPedestalParameters> > ConfigParameters::getGainPedestalParameters() {
1023  if (fGainPedestalParameters.size() == 0) {
1024  readGainPedestalParameters();
1025  }
1026  return fGainPedestalParameters;
1027 }
1028 
1029 
1030 void ConfigParameters::setProbe(std::string probe, std::string value) {
1031 
1032  std::transform(probe.begin(), probe.end(), probe.begin(), ::tolower);
1033 
1034  if (probe == "a1") fProbeA1 = value;
1035  else if (probe == "a2") fProbeA2 = value;
1036  else if (probe == "d1") fProbeD1 = value;
1037  else if (probe == "d2") fProbeD2 = value;
1038 }
1039 
1040 // ----------------------------------------------------------------------
1041 std::string ConfigParameters::getProbe(std::string probe) {
1042  std::transform(probe.begin(), probe.end(), probe.begin(), ::tolower);
1043  if (probe == "a1") return fProbeA1;
1044  else if (probe == "a2") return fProbeA2;
1045  else if (probe == "d1") return fProbeD1;
1046  else if (probe == "d2") return fProbeD2;
1047  else return "";
1048 }
1049 
1050 
1051 // ----------------------------------------------------------------------
1052 void ConfigParameters::cleanupString(string &s) {
1053  replaceAll(s, "\t", " ");
1054  string::size_type s1 = s.find("#");
1055  if (string::npos != s1) s.erase(s1);
1056  if (0 == s.length()) return;
1057  string::iterator new_end = unique(s.begin(), s.end(), bothAreSpaces);
1058  s.erase(new_end, s.end());
1059  if (s.substr(0, 1) == string(" ")) s.erase(0, 1);
1060  if (s.substr(s.length()-1, 1) == string(" ")) s.erase(s.length()-1, 1);
1061 }
1062 
1063 // ----------------------------------------------------------------------
1064 bool ConfigParameters::bothAreSpaces(char lhs, char rhs) {
1065  return (lhs == rhs) && (lhs == ' ');
1066 }
1067 
1068 // ----------------------------------------------------------------------
1069 void ConfigParameters::replaceAll(string& str, const string& from, const string& to) {
1070  if (from.empty()) return;
1071  size_t start_pos = 0;
1072  while((start_pos = str.find(from, start_pos)) != string::npos) {
1073  str.replace(start_pos, from.length(), to);
1074  start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
1075  }
1076 }
1077 
1078 // ----------------------------------------------------------------------
1079 void ConfigParameters::readNrocs(string line) {
1080  cleanupString(line);
1081  string::size_type s0 = line.find(" ");
1082  string nrocs = line.substr(s0);
1083  fnRocs = atoi(nrocs.c_str());
1084  string::size_type s1 = line.find("i2c:");
1085  if (string::npos == s1) {
1086  return;
1087  } else {
1088  string i2cstring = line.substr(s1+5);
1089  s0 = i2cstring.find(",");
1090  string i2c(""), leftover("");
1091  while (string::npos != s0) {
1092  i2c = i2cstring.substr(0, s0);
1093  fI2cAddresses.push_back(atoi(i2c.c_str()));
1094  i2cstring = i2cstring.substr(s0+1);
1095  s0 = i2cstring.find(",");
1096  }
1097  // -- get the last one as well
1098  fI2cAddresses.push_back(atoi(i2cstring.c_str()));
1099  if (fnRocs != fI2cAddresses.size()) {
1100  LOG(logWARNING) << "mismatch between number of i2c addresses and nRocs! Resetting nRocs to "
1101  << fI2cAddresses.size();
1102  fnRocs = fI2cAddresses.size();
1103  }
1104  }
1105 }
1106 
1107 // ----------------------------------------------------------------------
1108 void ConfigParameters::readReadbackCal() {
1109  if (!fReadReadbackCal) {
1110  for (unsigned int i = 0; i < fnRocs; ++i) {
1111  stringstream filename;
1112  filename << fDirectory << "/" << fReadbackCalFileName << "_C" << i << ".dat";
1113  vector<pair<string, double> > rbCal = readReadbackFile(filename.str());
1114  fReadbackCal.push_back(rbCal);
1115  }
1116  fReadReadbackCal = true;
1117  }
1118 }
1119 
1120 // ----------------------------------------------------------------------
1121 vector<pair<string, double> > ConfigParameters::readReadbackFile(string fname) {
1122  vector<pair<string, double> > rocRb;
1123 
1124  // -- read in file
1125  vector<string> lines;
1126  char buffer[5000];
1127  LOG(logINFO) << " reading " << fname;
1128  ifstream is(fname.c_str());
1129  while (is.getline(buffer, 200, '\n')) {
1130  lines.push_back(string(buffer));
1131  }
1132  is.close();
1133 
1134  // -- parse lines
1135  double ival(0);
1136  double uval(0);
1137  // unsigned char uval(0);
1138 
1139  string::size_type s1;
1140  string str1, str2;
1141  for (unsigned int i = 0; i < lines.size(); ++i) {
1142  //cout << lines[i] << endl;
1143  // -- remove tabs, adjacent spaces, leading and trailing spaces
1144  cleanupString(lines[i]);
1145  if (lines[i].length() < 2) continue;
1146  s1 = lines[i].find(" ");
1147  str1 = lines[i].substr(0, s1);
1148  str2 = lines[i].substr(s1+1);
1149 
1150  std::transform(str1.begin(), str1.end(), str1.begin(), ::tolower);
1151  ival = atof(str2.c_str());
1152 
1153  uval = ival;
1154  rocRb.push_back(make_pair(str1, uval));
1155 
1156  }
1157 
1158  return rocRb;
1159 }
1160 
1161 // ----------------------------------------------------------------------
1162 vector<vector<pair<string, double> > > ConfigParameters::getReadbackCal() {
1163  if (!fReadReadbackCal) {
1164  readReadbackCal();
1165  }
1166  return fReadbackCal;
1167 }
1168 
1169 // ----------------------------------------------------------------------
1170 bool ConfigParameters::writeReadbackFile(int iroc, vector<pair<string, double> > v) {
1171 
1172  std::stringstream fname;
1173  fname << fDirectory << "/" << fReadbackCalFileName << "_C" << iroc << ".dat";
1174 
1175  ofstream OutputFile;
1176  OutputFile.open((fname.str()).c_str());
1177  if (!OutputFile.is_open()) {
1178  return false;
1179  } else {
1180  LOG(logDEBUG) << "write readback calibration parameters into " << fname.str();
1181  }
1182 
1183  // RegisterDictionary *a = RegisterDictionary::getInstance();
1184  for (std::vector<std::pair<std::string,double> >::iterator idac = v.begin(); idac != v.end(); ++idac) {
1185  OutputFile << left << setw(3) << idac->first << " " << setw(3) << static_cast<double>(idac->second)
1186  << endl;
1187  }
1188 
1189  OutputFile.close();
1190  return true;
1191 }
1192 
1193 
1194 // ----------------------------------------------------------------------
1195 void ConfigParameters::setTrimVcalSuffix(string name, bool nocheck) {
1196 
1197  if (nocheck) {
1198  fTrimVcalSuffix = name;
1199  return;
1200  }
1201 
1202  std::stringstream fname;
1203 
1204  fname << fDirectory << "/" << fTrimParametersFileName << name << "_C0.dat";
1205  ifstream InputFile;
1206  InputFile.open((fname.str()).c_str());
1207  // cout << "check for " << fname.str() << endl;
1208 
1209  if (!InputFile.is_open()) {
1210  LOG(logERROR) << "Did not find file " << fname.str() << ", no trim VCAL value used";
1211  fTrimVcalSuffix = "";
1212  } else {
1213  fTrimVcalSuffix = name;
1214  }
1215  InputFile.close();
1216 }
1217