CMS 3D CMS Logo

PixelCalibConfiguration.cc
Go to the documentation of this file.
1 //
2 // This class is a first attempt at writing a configuration
3 // object that will perform a calibration loop.
4 //
5 //
6 //
7 
11 //#include "PixelUtilities/PixelTestStandUtilities/include/PixelTimer.h"
12 #include <fstream>
13 #include <iostream>
14 #include <ios>
15 #include <cassert>
16 #include <cstdlib>
17 #include <algorithm>
18 
19 using namespace pos;
20 using namespace std;
21 
22 #define BPIX
23 
24 PixelCalibConfiguration::PixelCalibConfiguration(std::vector<std::vector<std::string> >& tableMat)
25  : PixelCalibBase(), PixelConfigBase("", "", "") {
26  std::string mthn = "[PixelCalibConfiguration::PixelCalibConfiguration()]\t ";
27  std::map<std::string, int> colM;
28  std::vector<std::string> colNames;
43  colNames.push_back("CONFIG_KEY");
44  colNames.push_back("KEY_TYPE");
45  colNames.push_back("KEY_ALIAS");
46  colNames.push_back("VERSION");
47  colNames.push_back("KIND_OF_COND");
48  colNames.push_back("CALIB_TYPE");
49  colNames.push_back("CALIB_OBJ_DATA_FILE");
50  colNames.push_back("CALIB_OBJ_DATA_CLOB");
51 
52  for (unsigned int c = 0; c < tableMat[0].size(); c++) {
53  for (unsigned int n = 0; n < colNames.size(); n++) {
54  if (tableMat[0][c] == colNames[n]) {
55  colM[colNames[n]] = c;
56  break;
57  }
58  }
59  } //end for
60  for (unsigned int n = 0; n < colNames.size(); n++) {
61  if (colM.find(colNames[n]) == colM.end()) {
62  std::cerr << mthn << "Couldn't find in the database the column with name " << colNames[n] << std::endl;
63  assert(0);
64  }
65  }
66 
67  _bufferData = true;
68 
69  std::istringstream in;
70  in.str(tableMat[1][colM["CALIB_OBJ_DATA_CLOB"]]);
71 
73 
74  in >> tmp;
75 
76  if (tmp == "Mode:") {
77  in >> mode_;
78  // std::cout << __LINE__ << "]\t" << mthn << "mode=" << mode_ << std::endl;
79  in >> tmp;
80  } else {
81  mode_ = "FEDChannelOffsetPixel";
82  std::cout << __LINE__ << "]\t" << mthn << "mode not set, is this an old file? " << std::endl;
83  assert(0);
84  }
85 
86  singleROC_ = false;
87 
88  if (tmp == "SingleROC") {
89  singleROC_ = true;
90  in >> tmp;
91  }
92 
93  // Read in parameters, if any.
94  if (tmp == "Parameters:") {
95  in >> tmp;
96  while (tmp != "Rows:") {
97  assert(!in.eof());
98  std::string paramName = tmp;
99  in >> tmp; // tmp contains the parameter value
100  parameters_[paramName] = tmp;
101  in >> tmp; // tmp contains the next parameter's name, or "Rows:"
102  }
103  }
104 
105  assert(tmp == "Rows:");
106 
107  in >> tmp;
108 
109  std::vector<unsigned int> rows;
110  while (tmp != "Cols:") {
111  if (tmp == "|") {
112  rows_.push_back(rows);
113  rows.clear();
114  } else {
115  if (tmp != "*") {
116  rows.push_back(atoi(tmp.c_str()));
117  }
118  }
119  in >> tmp;
120  }
121  rows_.push_back(rows);
122  rows.clear();
123 
124  in >> tmp;
125 
126  std::vector<unsigned int> cols;
127  while ((tmp != "VcalLow:") && (tmp != "VcalHigh:") && (tmp != "Vcal:") && (tmp != "VcalHigh") && (tmp != "VcalLow")) {
128  if (tmp == "|") {
129  cols_.push_back(cols);
130  cols.clear();
131  } else {
132  if (tmp != "*") {
133  cols.push_back(atoi(tmp.c_str()));
134  }
135  }
136  in >> tmp;
137  }
138  cols_.push_back(cols);
139  cols.clear();
140 
141  highVCalRange_ = true;
142 
143  if (tmp == "VcalLow") {
144  highVCalRange_ = false;
145  in >> tmp;
146  }
147 
148  if (tmp == "VcalHigh") {
149  highVCalRange_ = true;
150  in >> tmp;
151  }
152 
153  if (tmp == "VcalLow:") {
154  highVCalRange_ = false;
155  }
156 
157  if ((tmp == "VcalLow:") || (tmp == "VcalHigh:") || (tmp == "Vcal:")) {
158  unsigned int first, last, step;
159  in >> first >> last >> step;
160  unsigned int index = 1;
161  if (!dacs_.empty()) {
162  index = dacs_.back().index() * dacs_.back().getNPoints();
163  }
164  in >> tmp;
165  bool mix = false;
166  if (tmp == "mix") {
167  mix = true;
168  in >> tmp;
169  }
170  PixelDACScanRange dacrange(pos::k_DACName_Vcal, first, last, step, index, mix);
171  dacs_.push_back(dacrange);
172  } else {
173  //in >> tmp;
174  while (tmp == "Scan:" || tmp == "ScanValues:") {
175  if (tmp == "ScanValues:") {
176  std::string dacname;
177  in >> dacname;
178  vector<unsigned int> values;
179  int val;
180  in >> val;
181  while (val != -1) {
182  values.push_back(val);
183  in >> val;
184  }
185  unsigned int index = 1;
186  if (!dacs_.empty()) {
187  index = dacs_.back().index() * dacs_.back().getNPoints();
188  }
189  PixelDACScanRange dacrange(dacname, values, index, false);
190  dacs_.push_back(dacrange);
191  in >> tmp;
192  } else {
193  std::string dacname;
194  in >> dacname;
195  unsigned int first, last, step;
196  in >> first >> last >> step;
197  unsigned int index = 1;
198  if (!dacs_.empty()) {
199  index = dacs_.back().index() * dacs_.back().getNPoints();
200  }
201  in >> tmp;
202  bool mix = false;
203  if (tmp == "mix") {
204  mix = true;
205  in >> tmp;
206  }
207  PixelDACScanRange dacrange(dacname, first, last, step, index, mix);
208  dacs_.push_back(dacrange);
209  }
210  }
211 
212  while ((tmp == "Set:") || (tmp == "SetRelative:")) {
213  string name;
214  in >> name;
215  int val;
216  in >> val;
217  unsigned int index = 1;
218  if (!dacs_.empty())
219  index = dacs_.back().index() * dacs_.back().getNPoints();
220  PixelDACScanRange dacrange(name, val, val, 1, index, false);
221  if (tmp == "SetRelative:") {
222  dacrange.setRelative();
223  }
224  dacs_.push_back(dacrange);
225  in >> tmp;
226  }
227  }
228 
229  assert(tmp == "Repeat:");
230 
231  in >> ntrigger_;
232 
233  in >> tmp;
234 
235  usesROCList_ = false;
236  bool buildROCListNow = false;
237  if (tmp == "Rocs:") {
238  buildROCListNow = true;
239  usesROCList_ = true;
240  } else {
241  assert(tmp == "ToCalibrate:");
242  buildROCListNow = false;
243  }
244 
245  while (!in.eof()) {
246  tmp = "";
247  in >> tmp;
248 
249  // added by F.Blekman to be able to deal with POS245 style calib.dat files in CMSSW
250  // these files use the syntax:
251  // Rocs:
252  // all
253 
254  if (tmp == "all" || tmp == "+" || tmp == "-") {
255  buildROCListNow = false;
256  }
257  // end of addition by F.B.
258 
259  if (tmp.empty())
260  continue;
261  rocListInstructions_.push_back(tmp);
262  }
263 
264  rocAndModuleListsBuilt_ = false;
265  if (buildROCListNow) {
266  std::set<PixelROCName> rocSet;
267  for (std::vector<std::string>::iterator rocListInstructions_itr = rocListInstructions_.begin();
268  rocListInstructions_itr != rocListInstructions_.end();
269  ++rocListInstructions_itr) {
270  PixelROCName rocname(*rocListInstructions_itr);
271  rocSet.insert(rocname);
272  }
274  }
275 
277 
278  // Added by Dario as a temporary patch for Debbie (this will disappear in the future)
279  calibFileContent_ = in.str();
280  // End of temporary patch
281 
282  return;
283 }
284 
286  std::string mthn = "[PixelCalibConfiguration::PixelCalibConfiguration()]\t ";
287 
288  _bufferData = true;
289 
290  std::ifstream in(filename.c_str());
291 
292  if (!in.good()) {
293  std::cout << __LINE__ << "]\t" << mthn << "Could not open:" << filename << std::endl;
294  assert(0);
295  } else {
296  std::cout << __LINE__ << "]\t" << mthn << "Opened:" << filename << std::endl;
297  }
298 
300 
301  in >> tmp;
302 
303  if (tmp == "Mode:") {
304  in >> mode_;
305  std::cout << __LINE__ << "]\t" << mthn << "PixelCalibConfiguration mode=" << mode_ << std::endl;
306  in >> tmp;
307  } else {
308  mode_ = "FEDChannelOffsetPixel";
309  std::cout << __LINE__ << "]\t" << mthn << "PixelCalibConfiguration mode not set, is this an old file? " << __LINE__
310  << "]\t" << std::endl;
311  assert(0);
312  }
313 
314  singleROC_ = false;
315 
316  if (tmp == "SingleROC") {
317  singleROC_ = true;
318  in >> tmp;
319  }
320 
321  // Read in parameters, if any.
322  if (tmp == "Parameters:") {
323  in >> tmp;
324  while (tmp != "Rows:") {
325  assert(!in.eof());
326  std::string paramName = tmp;
327  in >> tmp; // tmp contains the parameter value
328  parameters_[paramName] = tmp;
329  in >> tmp; // tmp contains the next parameter's name, or "Rows:"
330  }
331  }
332 
333  assert(tmp == "Rows:");
334 
335  in >> tmp;
336 
337  std::vector<unsigned int> rows;
338  while (tmp != "Cols:") {
339  if (tmp == "|") {
340  rows_.push_back(rows);
341  rows.clear();
342  } else {
343  if (tmp != "*") {
344  rows.push_back(atoi(tmp.c_str()));
345  }
346  }
347  in >> tmp;
348  }
349  rows_.push_back(rows);
350  rows.clear();
351 
352  in >> tmp;
353 
354  std::vector<unsigned int> cols;
355  while ((tmp != "VcalLow:") && (tmp != "VcalHigh:") && (tmp != "Vcal:") && (tmp != "VcalHigh") && (tmp != "VcalLow")) {
356  if (tmp == "|") {
357  cols_.push_back(cols);
358  cols.clear();
359  } else {
360  if (tmp != "*") {
361  cols.push_back(atoi(tmp.c_str()));
362  }
363  }
364  in >> tmp;
365  }
366  cols_.push_back(cols);
367  cols.clear();
368 
369  highVCalRange_ = true;
370 
371  if (tmp == "VcalLow") {
372  highVCalRange_ = false;
373  in >> tmp;
374  }
375 
376  if (tmp == "VcalHigh") {
377  highVCalRange_ = true;
378  in >> tmp;
379  }
380 
381  if (tmp == "VcalLow:") {
382  highVCalRange_ = false;
383  }
384 
385  if ((tmp == "VcalLow:") || (tmp == "VcalHigh:") || (tmp == "Vcal:")) {
386  unsigned int first, last, step;
387  in >> first >> last >> step;
388  unsigned int index = 1;
389  if (!dacs_.empty()) {
390  index = dacs_.back().index() * dacs_.back().getNPoints();
391  }
392  in >> tmp;
393  bool mix = false;
394  if (tmp == "mix") {
395  mix = true;
396  in >> tmp;
397  }
398  PixelDACScanRange dacrange(pos::k_DACName_Vcal, first, last, step, index, mix);
399  dacs_.push_back(dacrange);
400  } else {
401  //in >> tmp;
402  while (tmp == "Scan:" || tmp == "ScanValues:") {
403  if (tmp == "ScanValues:") {
404  std::string dacname;
405  in >> dacname;
406  vector<unsigned int> values;
407  int val;
408  in >> val;
409  while (val != -1) {
410  values.push_back(val);
411  in >> val;
412  }
413  unsigned int index = 1;
414  if (!dacs_.empty()) {
415  index = dacs_.back().index() * dacs_.back().getNPoints();
416  }
417  PixelDACScanRange dacrange(dacname, values, index, false);
418  dacs_.push_back(dacrange);
419  in >> tmp;
420  } else {
421  std::string dacname;
422  in >> dacname;
423  unsigned int first, last, step;
424  in >> first >> last >> step;
425  unsigned int index = 1;
426  if (!dacs_.empty()) {
427  index = dacs_.back().index() * dacs_.back().getNPoints();
428  }
429  in >> tmp;
430  bool mix = false;
431  if (tmp == "mix") {
432  mix = true;
433  in >> tmp;
434  }
435  PixelDACScanRange dacrange(dacname, first, last, step, index, mix);
436  dacs_.push_back(dacrange);
437  }
438  }
439 
440  while ((tmp == "Set:") || (tmp == "SetRelative:")) {
441  string name;
442  in >> name;
443  int val;
444  in >> val;
445  unsigned int absval = std::abs(val);
446  unsigned int index = 1;
447  if (!dacs_.empty())
448  index = dacs_.back().index() * dacs_.back().getNPoints();
449  PixelDACScanRange dacrange(name, absval, absval, 1, index, false);
450  if (tmp == "SetRelative:") {
451  dacrange.setRelative();
452  if (val < 0) {
453  dacrange.setNegative();
454  }
455  }
456  dacs_.push_back(dacrange);
457  in >> tmp;
458  }
459  }
460 
461  assert(tmp == "Repeat:");
462 
463  in >> ntrigger_;
464 
465  in >> tmp;
466 
467  usesROCList_ = false;
468  bool buildROCListNow = false;
469  if (tmp == "Rocs:") {
470  buildROCListNow = true;
471  usesROCList_ = true;
472  } else {
473  assert(tmp == "ToCalibrate:");
474  buildROCListNow = false;
475  }
476 
477  while (!in.eof()) {
478  tmp = "";
479  in >> tmp;
480 
481  // added by F.Blekman to be able to deal with POS245 style calib.dat files in CMSSW
482  // these files use the syntax:
483  // Rocs:
484  // all
485 
486  if (tmp == "all" || tmp == "+" || tmp == "-") {
487  buildROCListNow = false;
488  }
489  // end of addition by F.B.
490 
491  if (tmp.empty())
492  continue;
493  rocListInstructions_.push_back(tmp);
494  }
495 
496  in.close();
497 
498  rocAndModuleListsBuilt_ = false;
499  if (buildROCListNow) {
500  std::set<PixelROCName> rocSet;
501  for (std::vector<std::string>::iterator rocListInstructions_itr = rocListInstructions_.begin();
502  rocListInstructions_itr != rocListInstructions_.end();
503  ++rocListInstructions_itr) {
504  PixelROCName rocname(*rocListInstructions_itr);
505  rocSet.insert(rocname);
506  }
508  }
509 
511 
512  // Added by Dario as a temporary patch for Debbie (this will disappear in the future)
513  std::ifstream inTmp(filename.c_str());
514  calibFileContent_ = "";
515  while (!inTmp.eof()) {
516  std::string tmpString;
517  getline(inTmp, tmpString);
518  calibFileContent_ += tmpString + "\n";
519  //cout << __LINE__ << "]\t" << "[PixelCalibConfiguration::~PixelCalibConfiguration()]\t\t" << calibFileContent_ << endl ;
520  }
521  inTmp.close();
522  // End of temporary patch
523 
524  return;
525 }
526 
528 
530  const PixelDetectorConfig* detconfig) {
531  assert(translation != nullptr);
532  assert(detconfig != nullptr);
533 
536  return;
537  }
538 
539  // Build the ROC set from the instructions.
540  std::set<PixelROCName> rocSet;
541  bool addNext = true;
542  const map<PixelROCName, PixelROCStatus>& iroclist = detconfig->getROCsList();
543  for (std::vector<std::string>::iterator rocListInstructions_itr = rocListInstructions_.begin();
544  rocListInstructions_itr != rocListInstructions_.end();
545  ++rocListInstructions_itr) {
546  std::string instruction = *rocListInstructions_itr;
547 
548  if (instruction == "+") {
549  addNext = true;
550  continue;
551  }
552  if (instruction == "-") {
553  addNext = false;
554  continue;
555  }
556 
557  if (instruction == "all") {
558  if (addNext) // add all ROCs in the configuration
559  {
560  const std::vector<PixelModuleName>& moduleList = detconfig->getModuleList();
561  for (std::vector<PixelModuleName>::const_iterator moduleList_itr = moduleList.begin();
562  moduleList_itr != moduleList.end();
563  ++moduleList_itr) {
564  std::vector<PixelROCName> ROCsOnThisModule = translation->getROCsFromModule(*moduleList_itr);
565  for (std::vector<PixelROCName>::const_iterator ROCsOnThisModule_itr = ROCsOnThisModule.begin();
566  ROCsOnThisModule_itr != ROCsOnThisModule.end();
567  ++ROCsOnThisModule_itr) {
568  map<PixelROCName, PixelROCStatus>::const_iterator it = iroclist.find(*ROCsOnThisModule_itr);
569  assert(it != iroclist.end());
570  PixelROCStatus istatus = it->second;
571  if (!istatus.get(PixelROCStatus::noAnalogSignal))
572  rocSet.insert(*ROCsOnThisModule_itr);
573  }
574  }
575  } else // remove all ROCs
576  {
577  rocSet.clear();
578  }
579  addNext = true;
580  continue;
581  }
582 
583  // Assume it's a ROC or module name.
584  PixelModuleName modulename(instruction);
585 
586  // Skip if this module (or the module this ROC is on) isn't in the detector config.
587  if (!(detconfig->containsModule(modulename))) {
588  addNext = true;
589  continue;
590  }
591 
592  if (modulename.modulename() == instruction) // it's a module
593  {
594  std::vector<PixelROCName> ROCsOnThisModule = translation->getROCsFromModule(modulename);
595  for (std::vector<PixelROCName>::iterator ROCsOnThisModule_itr = ROCsOnThisModule.begin();
596  ROCsOnThisModule_itr != ROCsOnThisModule.end();
597  ++ROCsOnThisModule_itr) {
598  if (addNext) {
599  map<PixelROCName, PixelROCStatus>::const_iterator it = iroclist.find(*ROCsOnThisModule_itr);
600  assert(it != iroclist.end());
601  PixelROCStatus istatus = it->second;
602  if (!istatus.get(PixelROCStatus::noAnalogSignal))
603  rocSet.insert(*ROCsOnThisModule_itr);
604  } else
605  rocSet.erase(*ROCsOnThisModule_itr);
606  }
607  addNext = true;
608  continue;
609  } else // it's a ROC
610  {
611  PixelROCName rocname(instruction);
612  if (addNext) {
613  // Only add this ROC if it's in the configuration.
614  bool foundIt = false;
615  std::list<const PixelROCName*> allROCs = translation->getROCs();
616  for (std::list<const PixelROCName*>::iterator allROCs_itr = allROCs.begin(); allROCs_itr != allROCs.end();
617  ++allROCs_itr) {
618  if ((*(*allROCs_itr)) == rocname) {
619  foundIt = true;
620  break;
621  }
622  }
623  if (foundIt) {
624  map<PixelROCName, PixelROCStatus>::const_iterator it = iroclist.find(rocname);
625  assert(it != iroclist.end());
626  PixelROCStatus istatus = it->second;
627  if (!istatus.get(PixelROCStatus::noAnalogSignal))
628  rocSet.insert(rocname);
629  }
630  } else {
631  rocSet.erase(rocname);
632  }
633  addNext = true;
634  continue;
635  }
636 
637  // should never get here
638  assert(0);
639  }
640  // done building ROC set
641 
643 
645 }
646 
647 void PixelCalibConfiguration::buildROCAndModuleListsFromROCSet(const std::set<PixelROCName>& rocSet) {
648  assert(!rocAndModuleListsBuilt_);
649 
650  std::string mthn = "[PixelCalibConfiguration::buildROCAndModuleListsFromROCSet()] ";
651  // Build the ROC list from the ROC set.
652  for (std::set<PixelROCName>::iterator rocSet_itr = rocSet.begin(); rocSet_itr != rocSet.end(); ++rocSet_itr) {
653  rocs_.push_back(*rocSet_itr);
654  }
655 
656  //t.stop();
657  //cout << "buildROCAndModuleListsFromROCSet 1 time="<<t.tottime()<<endl;
658  //t.start();
659 
660  // Build the module set from the ROC set.
661  std::map<PixelModuleName, unsigned int> countROC;
662  for (std::set<PixelROCName>::iterator rocSet_itr = rocSet.begin(); rocSet_itr != rocSet.end(); ++rocSet_itr) {
663  //t1.start();
664  PixelModuleName modulename(*rocSet_itr);
665  //t1.stop();
666  //t2.start();
667  modules_.insert(modulename);
668  countROC[modulename]++;
669  //t2.stop();
670  }
671 
672  //t.stop();
673  //cout << "buildROCAndModuleListsFromROCSet 2 time="<<t.tottime()<<endl;
674  //cout << "buildROCAndModuleListsFromROCSet t1="<<t1.tottime()<<endl;
675  //cout << "buildROCAndModuleListsFromROCSet t2="<<t2.tottime()<<endl;
676  //t.start();
677 
678  // Test printout.
679  /*cout << "\nROC list:\n";
680  for ( std::vector<PixelROCName>::iterator rocs_itr = rocs_.begin();
681  rocs_itr != rocs_.end();
682  rocs_itr++ ){
683  cout << rocs_itr->rocname() << "\n";
684  } cout << "\nModule list:\n";
685  for ( std::set<PixelModuleName>::iterator modules_itr = modules_.begin(); modules_itr != modules_.end(); modules_itr++ )
686  {
687  cout << modules_itr->modulename() << "\n";
688  }
689  cout << "\n";*/
690 
691  // Determine max ROCs on a module for singleROC mode.
692  nROC_ = 1;
693  if (singleROC_) {
694  unsigned maxROCs = 0;
695  for (std::map<PixelModuleName, unsigned int>::iterator imodule = countROC.begin(); imodule != countROC.end();
696  ++imodule) {
697  if (imodule->second > maxROCs)
698  maxROCs = imodule->second;
699  }
700  nROC_ = maxROCs;
701 
702  std::cout << __LINE__ << "]\t" << mthn << "Max ROCs on a module=" << nROC_ << std::endl;
703  }
704 
705  for (unsigned int irocs = 0; irocs < rocs_.size(); irocs++) {
706  old_irows.push_back(-1);
707  old_icols.push_back(-1);
708  }
709 
711 }
712 
715  assert(rocAndModuleListsBuilt_);
716  assert(aNameTranslation != nullptr);
717 
718  // Build the channel list.
719  assert(channels_.empty());
720  for (std::vector<PixelROCName>::const_iterator rocs_itr = rocs_.begin(); rocs_itr != rocs_.end(); ++rocs_itr) {
721  channels_.insert(aNameTranslation->getChannelForROC(*rocs_itr));
722  }
723 
724  // Build the maps from ROC to ROC number.
725 
727 
728  std::set<PixelROCName> tempROCs;
729 
730  for (std::vector<PixelROCName>::const_iterator it = rocs_.begin(); it != rocs_.end(); ++it) {
731  tempROCs.insert(*it);
732  }
733 
734  for (std::set<PixelChannel>::const_iterator channels_itr = channels_.begin(); channels_itr != channels_.end();
735  ++channels_itr) {
736  std::vector<PixelROCName> rocsOnChannel = aNameTranslation->getROCsFromChannel(*channels_itr);
737 
738  std::set<PixelROCName> foundROCs;
739 
740  for (std::vector<PixelROCName>::const_iterator rocsOnChannel_itr = rocsOnChannel.begin();
741  rocsOnChannel_itr != rocsOnChannel.end();
742  ++rocsOnChannel_itr) {
743  if (tempROCs.find(*rocsOnChannel_itr) != tempROCs.end()) {
744  ROCNumberOnChannelAmongThoseCalibrated_[*rocsOnChannel_itr] = foundROCs.size();
745  foundROCs.insert(*rocsOnChannel_itr);
746  }
747  }
748 
749  for (std::set<PixelROCName>::const_iterator foundROCs_itr = foundROCs.begin(); foundROCs_itr != foundROCs.end();
750  ++foundROCs_itr) {
751  numROCsCalibratedOnChannel_[*foundROCs_itr] = foundROCs.size();
752  }
753  }
754 
756 }
757 
759  for (unsigned int i = 0; i < dacs_.size(); i++) {
760  if (dac == dacs_[i].name())
761  return i;
762  }
763 
764  std::cout << __LINE__ << "]\t[PixelCalibConfiguration::iScan()]\t\t could not find dac=" << dac << std::endl;
765 
766  assert(0);
767 
768  return 0;
769 }
770 
771 unsigned int PixelCalibConfiguration::scanROC(unsigned int state) const {
772  assert(state < nConfigurations());
773 
774  unsigned int i_ROC = state / (cols_.size() * rows_.size() * nScanPoints());
775 
776  return i_ROC;
777 }
778 
779 unsigned int PixelCalibConfiguration::scanValue(unsigned int iscan,
780  unsigned int state,
781  unsigned int ROCNumber,
782  unsigned int ROCsOnChannel) const {
783  unsigned int i_threshold = scanCounter(iscan, state);
784 
785  // Spread the DAC values on the different ROCs uniformly across the scan range.
786  if (dacs_[iscan].mixValuesAcrossROCs())
787  i_threshold = (i_threshold + (nScanPoints(iscan) * ROCNumber) / ROCsOnChannel) % nScanPoints(iscan);
788 
789  unsigned int threshold = dacs_[iscan].value(i_threshold);
790 
791  //assert(threshold==dacs_[iscan].first()+i_threshold*dacs_[iscan].step());
792 
793  return threshold;
794 }
795 
797  if (!singleROC_)
798  return true;
799  return scanROC(state) == ROCNumberOnChannelAmongThoseCalibrated(roc);
800 }
801 
802 unsigned int PixelCalibConfiguration::scanValue(unsigned int iscan, unsigned int state, PixelROCName roc) const {
803  unsigned int ROCNumber = ROCNumberOnChannelAmongThoseCalibrated(roc);
804  unsigned int ROCsOnChannel = numROCsCalibratedOnChannel(roc);
805 
806  return scanValue(iscan, state, ROCNumber, ROCsOnChannel);
807 }
808 
811  std::map<PixelROCName, unsigned int>::const_iterator foundROC = ROCNumberOnChannelAmongThoseCalibrated_.find(roc);
812  assert(foundROC != ROCNumberOnChannelAmongThoseCalibrated_.end());
813  return foundROC->second;
814 }
815 
818  std::map<PixelROCName, unsigned int>::const_iterator foundROC = numROCsCalibratedOnChannel_.find(roc);
819  assert(foundROC != numROCsCalibratedOnChannel_.end());
820  return foundROC->second;
821 }
822 
823 unsigned int PixelCalibConfiguration::scanCounter(unsigned int iscan, unsigned int state) const {
824  assert(state < nConfigurations());
825 
826  unsigned int i_scan = state % nScanPoints();
827 
828  for (unsigned int i = 0; i < iscan; i++) {
829  i_scan /= nScanPoints(i);
830  }
831 
832  unsigned int i_threshold = i_scan % nScanPoints(iscan);
833 
834  return i_threshold;
835 }
836 
837 unsigned int PixelCalibConfiguration::rowCounter(unsigned int state) const {
838  unsigned int i_row =
839  (state - scanROC(state) * cols_.size() * rows_.size() * nScanPoints()) / (cols_.size() * nScanPoints());
840  assert(i_row < rows_.size());
841  return i_row;
842 }
843 
844 unsigned int PixelCalibConfiguration::colCounter(unsigned int state) const {
845  unsigned int i_col = (state - scanROC(state) * cols_.size() * rows_.size() * nScanPoints() -
846  rowCounter(state) * cols_.size() * nScanPoints()) /
847  (nScanPoints());
848  assert(i_col < cols_.size());
849  return i_col;
850 }
851 
852 void PixelCalibConfiguration::nextFECState(std::map<unsigned int, PixelFECConfigInterface*>& pixelFECs,
853  PixelDetectorConfig* detconfig,
854  PixelNameTranslation* trans,
855  std::map<pos::PixelModuleName, pos::PixelMaskBase*>* masks,
856  std::map<pos::PixelModuleName, pos::PixelTrimBase*>* trims,
857  std::map<pos::PixelModuleName, pos::PixelDACSettings*>* dacs,
858 
859  unsigned int state) const {
860  std::string mthn = "[PixelCalibConfiguration::nextFECState()]\t\t ";
861  std::string modeName = parameterValue("ScanMode");
862 
863  int mode = -1;
864 
865  if (modeName == "maskAllPixel")
866  mode = 0;
867  if (modeName == "useAllPixel" || modeName.empty())
868  mode = 1;
869  if (modeName == "default")
870  mode = 2;
871 
872  static bool first = true;
873 
874  if (first) {
875  cout << __LINE__ << "]\t" << mthn << "mode=" << mode << endl;
876  first = false;
877  }
878 
879  if (mode == -1) {
880  cout << __LINE__ << "]\t" << mthn << "ScanMode=" << modeName << " not understood." << endl;
881  ::abort();
882  }
883 
884  if (rocInfo_.empty()) {
885  //here we will do some initialization...
886  for (unsigned int i = 0; i < rocs_.size(); i++) {
887  const PixelHdwAddress* hdwadd = trans->getHdwAddress(rocs_[i]);
888  PixelROCInfo rocInfo;
889  rocInfo.use_ = true;
890  //FIXME This is very inefficient
891  PixelModuleName module(rocs_[i].rocname());
892 
893  std::map<pos::PixelModuleName, pos::PixelMaskBase*>::const_iterator foundMask = masks->find(module);
894  if (foundMask == masks->end()) {
895  rocInfo.use_ = false;
896  rocInfo_.push_back(rocInfo);
897  continue;
898  }
899 
900  rocInfo.hdwadd_ = hdwadd;
901  rocInfo.trims_ = (*trims)[module]->getTrimBits(rocs_[i]);
902  rocInfo.masks_ = (*masks)[module]->getMaskBits(rocs_[i]);
903 
904 #ifdef BPIX
905  const PixelChannel channel = trans->getChannelForROC(rocs_[i]);
906  string tbmChannel = channel.TBMChannelString();
907  //cout<<" tbm channel "<<tbmChannel<<endl;
908  rocInfo.tbmChannel_ = tbmChannel;
909 #endif
910 
911  std::map<std::string, unsigned int> defaultDACValues;
912  (*dacs)[PixelModuleName(rocs_[i].rocname())]->getDACSettings(rocs_[i])->getDACs(defaultDACValues);
913 
914  for (std::vector<PixelDACScanRange>::const_iterator dacs_itr = dacs_.begin(); dacs_itr != dacs_.end();
915  ++dacs_itr) {
916  std::map<std::string, unsigned int>::const_iterator foundThisDAC = defaultDACValues.find(dacs_itr->name());
917  assert(foundThisDAC != defaultDACValues.end());
918 
919  pair<unsigned int, unsigned int> dacchannelAndValue(dacs_itr->dacchannel(), foundThisDAC->second);
920 
921  rocInfo.defaultDACs_.push_back(dacchannelAndValue);
922  }
923  rocInfo_.push_back(rocInfo);
924  }
925  }
926 
927  assert(rocs_.size() == rocInfo_.size());
928 
929  bool changedWBC = false;
930 
931  //std::map<unsigned int, PixelFECConfigInterface*>::iterator iPixelFEC=pixelFECs.begin();
932  //for(;iPixelFEC!=pixelFECs.end();++iPixelFEC){
933  //iPixelFEC->second->fecDebug(1);
934  //}
935 
936  //unsigned long version=0;
937  //pixelFEC->getversion(&version);
938  //std::cout<<"mfec firmware version:"<<version<<std::endl;
939 
940  assert(rocAndModuleListsBuilt_);
941 
942  assert(state < nConfigurations());
943 
944  // Which set of rows we're on.
945  unsigned int i_row = rowCounter(state);
946 
947  // Which set of columns we're on.
948  unsigned int i_col = colCounter(state);
949 
950  // Whether we're beginning a new scan over the DACs after changing which ROC or which pixel pattern.
951  unsigned int first_scan = true;
952  for (unsigned int i = 0; i < dacs_.size(); i++) {
953  if (scanCounter(i, state) != 0)
954  first_scan = false;
955  }
956 
957  // Disable all pixels at the beginning.
958  if (state == 0 && (mode == 0 || mode == 1)) {
959  for (unsigned int i = 0; i < rocs_.size(); i++) {
960  if (!rocInfo_[i].use_)
961  continue;
962 
963  PixelHdwAddress theROC = *rocInfo_[i].hdwadd_;
964  PixelROCTrimBits* rocTrims = rocInfo_[i].trims_;
965 
966  //Turn off all pixels
967  disablePixels(pixelFECs[theROC.fecnumber()], rocTrims, theROC);
968  }
969 
970  // reset
971  //cout<<"finish init"<<endl;
972  //sleep(1);
973  //pixelFEC->injectrstroc(7,1);
974  // or do pixelFEC->rocreset(theROC.mfec(),
975  // theROC.mfecchannel(),
976  // 14, //FIXME hardcode for Channel A
977  // theROC.hubaddress());
978  }
979 
980  // When a scan is complete for a given ROC or pixel pattern, reset the DACs to default values and disable the previously-enabled pixels.
981  if (first_scan && state != 0 && mode != 2) {
982  unsigned int previousState = state - 1;
983 
984  unsigned int i_row_previous = rowCounter(previousState);
985 
986  unsigned int i_col_previous = colCounter(previousState);
987 
988  for (unsigned int i = 0; i < rocs_.size(); i++) {
989  if (!rocInfo_[i].use_)
990  continue;
991 
992  PixelHdwAddress theROC = *rocInfo_[i].hdwadd_;
993 
994  if (!scanningROCForState(rocs_[i], previousState))
995  continue;
996 
997  // Set the DACs back to their default values when we're done with a scan.
998  for (unsigned int j = 0; j < dacs_.size(); j++) {
999  //Try to not reprogram DACs as often..
1000  if (state != 0) {
1001  if (scanCounter(dacs_[j].name(), state) == scanCounter(dacs_[j].name(), state - 1)) {
1002  continue;
1003  }
1004  }
1005 
1006  pixelFECs[theROC.fecnumber()]->progdac(theROC.mfec(),
1007  theROC.mfecchannel(),
1008  theROC.hubaddress(),
1009  theROC.portaddress(),
1010  theROC.rocid(),
1011  rocInfo_[i].defaultDACs_[j].first,
1012  rocInfo_[i].defaultDACs_[j].second,
1013  _bufferData);
1014 
1015  if (dacs_[j].dacchannel() == k_DACAddress_WBC) {
1016  changedWBC = true;
1017  //cout << "Changed WBC 1"<<endl;
1018  }
1019  }
1020 
1021  PixelROCTrimBits* rocTrims = rocInfo_[i].trims_;
1022 
1023  disablePixels(pixelFECs[theROC.fecnumber()], i_row_previous, i_col_previous, rocTrims, theROC);
1024  }
1025  }
1026 
1027  // Set each ROC with the new settings for this state.
1028  for (unsigned int i = 0; i < rocs_.size(); i++) {
1029  if (!rocInfo_[i].use_)
1030  continue;
1031 
1032  PixelHdwAddress theROC = *rocInfo_[i].hdwadd_;
1033 
1034  // Skip this ROC if we're in SingleROC mode and we're not on this ROC number.
1035  if (!scanningROCForState(rocs_[i], state))
1036  continue;
1037 
1038  // std::cout << "Will call progdac for vcal:"<< vcal << std::endl;
1039 
1040  // Program all the DAC values.
1041  for (unsigned int ii = 0; ii < dacs_.size(); ii++) {
1042  //Try to not reprogram DACs as often..
1043  if (state != 0) {
1044  if (scanCounter(dacs_[ii].name(), state) == scanCounter(dacs_[ii].name(), state - 1)) {
1045  continue;
1046  }
1047  }
1048 
1049  int dacvalue = scanValue(ii, state, rocs_[i]);
1050 
1051  //cout << "dacname ii:"<<dacs_[ii].name()<<" "<<ii<<endl;
1052 
1053  if (dacs_[ii].relative()) {
1054  //We have to find the default DAC setting so that we can
1055  //add the offset as we are in relative mode.
1056 
1057  if (dacs_[ii].negative())
1058  dacvalue = -dacvalue;
1059 
1060  dacvalue += rocInfo_[i].defaultDACs_[ii].second;
1061  //cout << "[PixelCalibConfiguration::nextFECState] ROC="<<rocs_[i]
1062  // << " dac="<<dacs_[ii].name()<<" new value="<<dacvalue<<endl;
1063  }
1064 
1065  pixelFECs[theROC.fecnumber()]->progdac(theROC.mfec(),
1066  theROC.mfecchannel(),
1067  theROC.hubaddress(),
1068  theROC.portaddress(),
1069  theROC.rocid(),
1070  rocInfo_[i].defaultDACs_[ii].first,
1071  dacvalue,
1072  _bufferData);
1073 
1074  if (dacs_[ii].dacchannel() == k_DACAddress_WBC) {
1075  changedWBC = true;
1076  //cout << "Changed WBC 2"<<endl;
1077  }
1078  }
1079 
1080  // At the beginning of a scan, set the pixel pattern.
1081  if (first_scan) {
1082  // Set masks and trims.
1083  if (mode != 2) {
1084  PixelROCMaskBits* rocMasks = rocInfo_[i].masks_;
1085  PixelROCTrimBits* rocTrims = rocInfo_[i].trims_;
1086 
1087  if (mode == 1)
1088  rocMasks = nullptr;
1089 
1090  //std::cout << "Will enable pixels!" <<std::endl;
1091  enablePixels(pixelFECs[theROC.fecnumber()], i_row, i_col, rocMasks, rocTrims, theROC);
1092  }
1093 
1094  // Set high or low Vcal range.
1095 
1096  if (state == 0) {
1097  PixelModuleName module(rocs_[i].rocname());
1098 
1099  unsigned int roccontrolword = (*dacs)[module]->getDACSettings(rocs_[i])->getControlRegister();
1100 
1101  //range is controlled here by one bit, but rest must match config
1102  //bit 0 on/off= 20/40 MHz speed; bit 1 on/off=disabled/enable; bit 3=Vcal range
1103 
1104  if (highVCalRange_)
1105  roccontrolword |= 0x4; //turn range bit on
1106  else
1107  roccontrolword &= 0xfb; //turn range bit off
1108 
1109  pixelFECs[theROC.fecnumber()]->progdac(theROC.mfec(),
1110  theROC.mfecchannel(),
1111  theROC.hubaddress(),
1112  theROC.portaddress(),
1113  theROC.rocid(),
1114  0xfd,
1115  roccontrolword,
1116  _bufferData);
1117  }
1118 
1119  // Clear all pixels before setting the pixel pattern.
1120  pixelFECs[theROC.fecnumber()]->clrcal(
1121  theROC.mfec(), theROC.mfecchannel(), theROC.hubaddress(), theROC.portaddress(), theROC.rocid(), _bufferData);
1122 
1123  // Program the pixel pattern.
1124  unsigned int nrow = rows_[i_row].size();
1125  unsigned int ncol = cols_[i_col].size();
1126  unsigned int nmax = std::max(nrow, ncol);
1127  if (nrow == 0 || ncol == 0)
1128  nmax = 0;
1129  for (unsigned int n = 0; n < nmax; n++) {
1130  unsigned int irow = n;
1131  unsigned int icol = n;
1132  if (irow >= nrow)
1133  irow = nrow - 1;
1134  if (icol >= ncol)
1135  icol = ncol - 1;
1136  unsigned int row = rows_[i_row][irow];
1137  unsigned int col = cols_[i_col][icol];
1138 
1139  pixelFECs[theROC.fecnumber()]->calpix(theROC.mfec(),
1140  theROC.mfecchannel(),
1141  theROC.hubaddress(),
1142  theROC.portaddress(),
1143  theROC.rocid(),
1144  col,
1145  row,
1146  1,
1147  _bufferData);
1148  }
1149 
1150  } // end of instructions for the beginning of a scan
1151  } // end of loop over ROCs
1152 
1153  if (_bufferData) {
1154  std::map<unsigned int, PixelFECConfigInterface*>::iterator iPixelFEC = pixelFECs.begin();
1155  for (; iPixelFEC != pixelFECs.end(); ++iPixelFEC) {
1156  iPixelFEC->second->qbufsend();
1157  }
1158  }
1159 
1160  if (changedWBC) {
1161  for (unsigned int i = 0; i < rocs_.size(); i++) {
1162  if (!rocInfo_[i].use_)
1163  continue;
1164 
1165  PixelHdwAddress theROC = *rocInfo_[i].hdwadd_;
1166 
1167  int tbmRegister = 14; // define for TBM-A
1168 #ifdef BPIX
1169  string tbmChannel = rocInfo_[i].tbmChannel_; // get TBM channel
1170  if (tbmChannel == "B")
1171  tbmRegister = 15; // for TBM=B
1172 #endif
1173 
1174  pixelFECs[theROC.fecnumber()]->rocreset(theROC.mfec(), theROC.mfecchannel(), tbmRegister, theROC.hubaddress());
1175  } // for rocs
1176  }
1177 
1178  return;
1179 }
1180 
1181 // FIXME This code breaks if it is called more than once with different crate numbers!
1182 std::vector<std::pair<unsigned int, std::vector<unsigned int> > >& PixelCalibConfiguration::fedCardsAndChannels(
1183  unsigned int crate,
1184  PixelNameTranslation* translation,
1185  PixelFEDConfig* fedconfig,
1186  PixelDetectorConfig* detconfig) const {
1187  assert(rocAndModuleListsBuilt_);
1188 
1189  assert(!rocs_.empty());
1190 
1191  for (unsigned int i = 0; i < rocs_.size(); i++) {
1192  PixelModuleName module(rocs_[i].rocname());
1193  if (!detconfig->containsModule(module))
1194  continue;
1195  const PixelHdwAddress* hdw = translation->getHdwAddress(rocs_[i]);
1196  assert(hdw != nullptr);
1197  //std::cout << "ROC, fednumber:"<<rocs_[i]<<" "<<hdw->fednumber()
1198  // << std::endl;
1199  //first check if fed associated with the roc is in the right crate
1200  if (fedconfig->crateFromFEDNumber(hdw->fednumber()) != crate)
1201  continue;
1202  //next look if we have already found fed number
1203  unsigned int index = fedCardsAndChannels_.size();
1204  for (unsigned int j = 0; j < fedCardsAndChannels_.size(); j++) {
1205  if (fedCardsAndChannels_[j].first == hdw->fednumber()) {
1206  index = j;
1207  break;
1208  }
1209  }
1210  //If we didn't find the fedcard we will create it
1211  if (index == fedCardsAndChannels_.size()) {
1212  std::vector<unsigned int> tmp;
1213  tmp.push_back(hdw->fedchannel());
1214  std::pair<unsigned int, std::vector<unsigned int> > tmp2(hdw->fednumber(), tmp);
1215  fedCardsAndChannels_.push_back(tmp2);
1216  continue;
1217  }
1218  //Now look and see if the channel has been added
1219  std::vector<unsigned int>& channels = fedCardsAndChannels_[index].second;
1220  bool found = false;
1221  for (unsigned int k = 0; k < channels.size(); k++) {
1222  if (channels[k] == hdw->fedchannel()) {
1223  found = true;
1224  break;
1225  }
1226  }
1227  if (found)
1228  continue;
1229  channels.push_back(hdw->fedchannel());
1230  }
1231 
1232  return fedCardsAndChannels_;
1233 }
1234 
1235 std::map<unsigned int, std::set<unsigned int> > PixelCalibConfiguration::getFEDsAndChannels(
1236  PixelNameTranslation* translation) {
1237  assert(rocAndModuleListsBuilt_);
1238 
1239  std::map<unsigned int, std::set<unsigned int> > fedsChannels;
1240  assert(!rocs_.empty());
1241  std::vector<PixelROCName>::iterator iroc = rocs_.begin();
1242 
1243  for (; iroc != rocs_.end(); ++iroc) {
1244  const PixelHdwAddress* roc_hdwaddress = translation->getHdwAddress(*iroc);
1245  unsigned int fednumber = roc_hdwaddress->fednumber();
1246  unsigned int fedchannel = roc_hdwaddress->fedchannel();
1247  fedsChannels[fednumber].insert(fedchannel);
1248  }
1249 
1250  return fedsChannels;
1251 }
1252 
1253 std::set<unsigned int> PixelCalibConfiguration::getFEDCrates(const PixelNameTranslation* translation,
1254  const PixelFEDConfig* fedconfig) const {
1255  assert(rocAndModuleListsBuilt_);
1256 
1257  std::set<unsigned int> fedcrates;
1258  assert(!modules_.empty());
1259  std::set<PixelModuleName>::iterator imodule = modules_.begin();
1260 
1261  for (; imodule != modules_.end(); ++imodule) {
1262  std::set<PixelChannel> channelsOnThisModule = translation->getChannelsOnModule(*imodule);
1263  for (std::set<PixelChannel>::const_iterator channelsOnThisModule_itr = channelsOnThisModule.begin();
1264  channelsOnThisModule_itr != channelsOnThisModule.end();
1265  ++channelsOnThisModule_itr) {
1266  const PixelHdwAddress& channel_hdwaddress = translation->getHdwAddress(*channelsOnThisModule_itr);
1267  unsigned int fednumber = channel_hdwaddress.fednumber();
1268  fedcrates.insert(fedconfig->crateFromFEDNumber(fednumber));
1269  }
1270  }
1271 
1272  return fedcrates;
1273 }
1274 
1275 std::set<unsigned int> PixelCalibConfiguration::getFECCrates(const PixelNameTranslation* translation,
1276  const PixelFECConfig* fecconfig) const {
1277  assert(rocAndModuleListsBuilt_);
1278 
1279  std::set<unsigned int> feccrates;
1280  assert(!modules_.empty());
1281  std::set<PixelModuleName>::iterator imodule = modules_.begin();
1282 
1283  for (; imodule != modules_.end(); ++imodule) {
1284  std::set<PixelChannel> channelsOnThisModule = translation->getChannelsOnModule(*imodule);
1285  for (std::set<PixelChannel>::const_iterator channelsOnThisModule_itr = channelsOnThisModule.begin();
1286  channelsOnThisModule_itr != channelsOnThisModule.end();
1287  ++channelsOnThisModule_itr) {
1288  const PixelHdwAddress& channel_hdwaddress = translation->getHdwAddress(*channelsOnThisModule_itr);
1289  unsigned int fecnumber = channel_hdwaddress.fecnumber();
1290  feccrates.insert(fecconfig->crateFromFECNumber(fecnumber));
1291  }
1292  }
1293 
1294  return feccrates;
1295 }
1296 
1298  const PixelPortcardMap* portcardmap,
1299  const std::map<std::string, PixelPortCardConfig*>& mapNamePortCard,
1300  const PixelTKFECConfig* tkfecconfig) const {
1301  assert(rocAndModuleListsBuilt_);
1302 
1303  std::set<unsigned int> tkfeccrates;
1304  assert(!modules_.empty());
1305  std::set<PixelModuleName>::iterator imodule = modules_.begin();
1306 
1307  for (; imodule != modules_.end(); ++imodule) {
1308  // implement this by module --(PixelPortcardMap)-> port card(s) --(PixelPortCardConfig)-> FEC # --(PixelFECConfig theTKFECConfiguration_)-> crate
1309  const std::set<std::string> portCards = portcardmap->portcards(*imodule);
1310  for (std::set<std::string>::const_iterator portCards_itr = portCards.begin(); portCards_itr != portCards.end();
1311  ++portCards_itr) {
1312  const std::string portcardname = *portCards_itr;
1313  std::map<std::string, PixelPortCardConfig*>::const_iterator portcardconfig_itr =
1314  mapNamePortCard.find(portcardname);
1315  assert(portcardconfig_itr != mapNamePortCard.end());
1316  PixelPortCardConfig* portcardconfig = portcardconfig_itr->second;
1317  std::string TKFECID = portcardconfig->getTKFECID();
1318  tkfeccrates.insert(tkfecconfig->crateFromTKFECID(TKFECID));
1319  }
1320  }
1321 
1322  return tkfeccrates;
1323 }
1324 
1325 std::ostream& pos::operator<<(std::ostream& s, const PixelCalibConfiguration& calib) {
1326  if (!calib.parameters_.empty()) {
1327  s << "Parameters:" << std::endl;
1328  for (std::map<std::string, std::string>::const_iterator paramItr = calib.parameters_.begin();
1329  paramItr != calib.parameters_.end();
1330  ++paramItr) {
1331  s << paramItr->first << " " << paramItr->second << std::endl;
1332  }
1333  }
1334 
1335  s << "Rows:" << std::endl;
1336  for (unsigned int i = 0; i < calib.rows_.size(); i++) {
1337  for (unsigned int j = 0; j < calib.rows_[i].size(); j++) {
1338  s << calib.rows_[i][j] << " " << std::endl;
1339  }
1340  s << "|" << std::endl;
1341  }
1342 
1343  s << "Cols:" << std::endl;
1344  for (unsigned int i = 0; i < calib.cols_.size(); i++) {
1345  for (unsigned int j = 0; j < calib.cols_[i].size(); j++) {
1346  s << calib.cols_[i][j] << " " << std::endl;
1347  }
1348  s << "|" << std::endl;
1349  }
1350 
1351  s << "Vcal:" << std::endl;
1352 
1353  //s << calib.vcal_<<std::endl;
1354 
1355  s << "Vcthr:" << std::endl;
1356 
1357  s << calib.dacs_[0].first() << " " << calib.dacs_[0].last() << " " << calib.dacs_[0].step() << std::endl;
1358 
1359  s << "CalDel:" << std::endl;
1360 
1361  s << calib.dacs_[1].first() << " " << calib.dacs_[0].last() << " " << calib.dacs_[1].step() << std::endl;
1362 
1363  s << "Repeat:" << std::endl;
1364 
1365  s << calib.ntrigger_ << std::endl;
1366 
1367  return s;
1368 }
1369 
1371  unsigned int irows,
1372  unsigned int icols,
1373  pos::PixelROCMaskBits* masks,
1374  pos::PixelROCTrimBits* trims,
1375  const PixelHdwAddress& theROC) const {
1376  for (unsigned int irow = 0; irow < rows_[irows].size(); irow++) {
1377  for (unsigned int icol = 0; icol < cols_[icols].size(); icol++) {
1378  /* std::cout << "Will turn on pixel col="
1379  <<cols_[icols][icol]
1380  <<" row="<<rows_[irows][irow]<<std::endl;
1381  */
1382  unsigned int bits = trims->trim(cols_[icols][icol], rows_[irows][irow]);
1383 
1384  //if masks==0 always enable pixel
1385  if (masks == nullptr || masks->mask(cols_[icols][icol], rows_[irows][irow]))
1386  bits |= 0x80;
1387 
1388  pixelFEC->progpix(theROC.mfec(),
1389  theROC.mfecchannel(),
1390  theROC.hubaddress(),
1391  theROC.portaddress(),
1392  theROC.rocid(),
1393  cols_[icols][icol],
1394  rows_[irows][irow],
1395  bits,
1396  _bufferData);
1397  }
1398  }
1399 }
1400 
1402  unsigned int irows,
1403  unsigned int icols,
1404  pos::PixelROCTrimBits* trims,
1405  const PixelHdwAddress& theROC) const {
1406  for (unsigned int irow = 0; irow < rows_[irows].size(); irow++) {
1407  for (unsigned int icol = 0; icol < cols_[icols].size(); icol++) {
1408  /* std::cout << "Will turn off pixel col="
1409  <<cols_[old_icols][icol]
1410  <<" row="<<rows_[old_irows][irow]<<std::endl;
1411  */
1412  unsigned int bits = trims->trim(cols_[icols][icol], rows_[irows][irow]);
1413  pixelFEC->progpix(theROC.mfec(),
1414  theROC.mfecchannel(),
1415  theROC.hubaddress(),
1416  theROC.portaddress(),
1417  theROC.rocid(),
1418  cols_[icols][icol],
1419  rows_[irows][irow],
1420  bits,
1421  _bufferData);
1422  }
1423  }
1424 }
1425 
1427  pos::PixelROCTrimBits* trims,
1428  const PixelHdwAddress& theROC) const {
1429  //cout<<" disable ROC "<<theROC.hubaddress()<<" "<<theROC.rocid()<<endl;
1430  //FIXME This should be done with more efficient commands!
1431  for (unsigned int row = 0; row < 80; row++) {
1432  for (unsigned int col = 0; col < 52; col++) {
1433  unsigned int bits = trims->trim(col, row);
1434  pixelFEC->progpix(theROC.mfec(),
1435  theROC.mfecchannel(),
1436  theROC.hubaddress(),
1437  theROC.portaddress(),
1438  theROC.rocid(),
1439  col,
1440  row,
1441  bits,
1442  _bufferData);
1443  }
1444  }
1445 }
1446 
1448  std::map<std::string, std::string>::const_iterator itr = parameters_.find(parameterName);
1449  if (itr == parameters_.end()) // parameterName is not in the list
1450  {
1451  return "";
1452  } else {
1453  return itr->second;
1454  }
1455 }
1456 
1458  //FIXME this is not tested for all the use cases...
1459 
1460  if (!dir.empty())
1461  dir += "/";
1462  std::string filename = dir + "calib.dat";
1463  std::ofstream out(filename.c_str());
1464 
1465  out << "Mode: " << mode_ << endl;
1466  if (singleROC_)
1467  out << "SingleROC" << endl;
1468  if (!parameters_.empty()) {
1469  out << "Parameters:" << endl;
1470  std::map<std::string, std::string>::const_iterator it = parameters_.begin();
1471  for (; it != parameters_.end(); ++it) {
1472  out << it->first << " " << it->second << endl;
1473  }
1474  }
1475  out << "Rows:" << endl;
1476  for (unsigned int i = 0; i < rows_.size(); i++) {
1477  for (unsigned int j = 0; j < rows_[i].size(); j++) {
1478  out << rows_[i][j] << " ";
1479  }
1480  if (i != rows_.size() - 1)
1481  out << "|";
1482  out << endl;
1483  }
1484  out << "Cols:" << endl;
1485  for (unsigned int i = 0; i < cols_.size(); i++) {
1486  for (unsigned int j = 0; j < cols_[i].size(); j++) {
1487  out << cols_[i][j] << " ";
1488  }
1489  if (i != cols_.size() - 1)
1490  out << "|";
1491  out << endl;
1492  }
1493 
1494  if (highVCalRange_) {
1495  out << "VcalHigh" << endl;
1496  } else {
1497  out << "VcalLow" << endl;
1498  }
1499 
1500  for (unsigned int i = 0; i < dacs_.size(); i++) {
1501  if (dacs_[i].uniformSteps()) {
1502  if (dacs_[i].first() != dacs_[i].last()) {
1503  out << "Scan: " << dacs_[i].name() << " ";
1504  out << dacs_[i].first() << " ";
1505  out << dacs_[i].last() << " ";
1506  out << dacs_[i].step() << endl;
1507  } else {
1508  out << "Set: " << dacs_[i].name() << " ";
1509  out << dacs_[i].first() << endl;
1510  }
1511  } else {
1512  out << "ScanValues: " << dacs_[i].name() << " ";
1513  for (unsigned int ival = 0; ival < dacs_[i].getNPoints(); ival++) {
1514  out << dacs_[i].value(ival) << " ";
1515  }
1516  out << " -1" << endl;
1517  }
1518  }
1519 
1520  out << "Repeat:" << endl;
1521  out << ntrigger_ << endl;
1522 
1523  if (usesROCList_) {
1524  out << "Rocs:" << endl;
1525  } else {
1526  out << "ToCalibrate:" << endl;
1527  }
1528  for (unsigned int i = 0; i < rocListInstructions_.size(); i++) {
1529  out << rocListInstructions_[i] << endl;
1530  if (rocListInstructions_[i] == "+" || rocListInstructions_[i] == "-") {
1531  out << " ";
1532  } else {
1533  out << endl;
1534  }
1535  }
1536 
1537  out.close();
1538 }
1539 
1541  unsigned int returnValue = 0;
1542  for (std::vector<std::vector<unsigned int> >::const_iterator rows_itr = rows_.begin(); rows_itr != rows_.end();
1543  ++rows_itr) {
1544  for (std::vector<std::vector<unsigned int> >::const_iterator cols_itr = cols_.begin(); cols_itr != cols_.end();
1545  ++cols_itr) {
1546  unsigned int theSize = rows_itr->size() * cols_itr->size();
1547  returnValue = max(returnValue, theSize);
1548  }
1549  }
1550  return returnValue;
1551 }
1552 
1553 std::set<std::pair<unsigned int, unsigned int> > PixelCalibConfiguration::pixelsWithHits(unsigned int state) const {
1554  std::set<std::pair<unsigned int, unsigned int> > pixels;
1555  // column # row #
1556 
1557  for (std::vector<unsigned int>::const_iterator col_itr = cols_[colCounter(state)].begin();
1558  col_itr != cols_[colCounter(state)].end();
1559  ++col_itr) {
1560  for (std::vector<unsigned int>::const_iterator row_itr = rows_[rowCounter(state)].begin();
1561  row_itr != rows_[rowCounter(state)].end();
1562  ++row_itr) {
1563  pixels.insert(std::pair<unsigned int, unsigned int>(*col_itr, *row_itr));
1564  }
1565  }
1566 
1567  return pixels;
1568 }
1569 
1571  for (unsigned int i = 0; i < numberOfScanVariables(); i++) {
1572  if (scanName(i) == name) {
1573  return true;
1574  }
1575  }
1576  return false;
1577 }
1578 
1579 //=============================================================================================
1581  int version,
1582  std::string path,
1583  std::ofstream* outstream,
1584  std::ofstream* out1stream,
1585  std::ofstream* out2stream) const {
1586  std::string mthn = "[PixelCalibConfiguration::writeXMLHeader()]\t\t ";
1587  std::stringstream maskFullPath;
1588 
1589  writeASCII(path);
1590 
1591  maskFullPath << path << "/PixelCalib_Test_" << PixelTimeFormatter::getmSecTime() << ".xml";
1592  std::cout << __LINE__ << "]\t" << mthn << "Writing to: " << maskFullPath.str() << std::endl;
1593 
1594  outstream->open(maskFullPath.str().c_str());
1595 
1596  *outstream << "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>" << std::endl;
1597  *outstream << "<ROOT xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>" << std::endl;
1598  *outstream << "" << std::endl;
1599  *outstream << " <!-- " << mthn << "-->" << std::endl;
1600  *outstream << "" << std::endl;
1601  *outstream << " <HEADER>" << std::endl;
1602  *outstream << " <TYPE>" << std::endl;
1603  *outstream << " <EXTENSION_TABLE_NAME>PIXEL_CALIB_CLOB</EXTENSION_TABLE_NAME>" << std::endl;
1604  *outstream << " <NAME>Calibration Object Clob</NAME>" << std::endl;
1605  *outstream << " </TYPE>" << std::endl;
1606  *outstream << " <RUN>" << std::endl;
1607  *outstream << " <RUN_TYPE>Gain Calibration</RUN_TYPE>" << std::endl;
1608  *outstream << " <RUN_NUMBER>1</RUN_NUMBER>" << std::endl;
1609  *outstream << " <RUN_BEGIN_TIMESTAMP>" << PixelTimeFormatter::getTime() << "</RUN_BEGIN_TIMESTAMP>" << std::endl;
1610  *outstream << " <LOCATION>CERN P5</LOCATION>" << std::endl;
1611  *outstream << " </RUN>" << std::endl;
1612  *outstream << " </HEADER>" << std::endl;
1613  *outstream << "" << std::endl;
1614  *outstream << " <DATA_SET>" << std::endl;
1615  *outstream << "" << std::endl;
1616  *outstream << " <VERSION>" << version << "</VERSION>" << std::endl;
1617  *outstream << " <COMMENT_DESCRIPTION>" << getComment() << "</COMMENT_DESCRIPTION>" << std::endl;
1618  *outstream << " <CREATED_BY_USER>" << getAuthor() << "</CREATED_BY_USER>" << std::endl;
1619  *outstream << "" << std::endl;
1620  *outstream << " <PART>" << std::endl;
1621  *outstream << " <NAME_LABEL>CMS-PIXEL-ROOT</NAME_LABEL>" << std::endl;
1622  *outstream << " <KIND_OF_PART>Detector ROOT</KIND_OF_PART>" << std::endl;
1623  *outstream << " </PART>" << std::endl;
1624 }
1625 
1626 //=============================================================================================
1627 void PixelCalibConfiguration::writeXML(std::ofstream* outstream,
1628  std::ofstream* out1stream,
1629  std::ofstream* out2stream) const {
1630  std::string mthn = "[PixelCalibConfiguration::writeXML()]\t\t ";
1631 
1632  *outstream << " " << std::endl;
1633  *outstream << " <DATA>" << std::endl;
1634  *outstream << " <CALIB_OBJ_DATA_FILE>./calib.dat</CALIB_OBJ_DATA_FILE>" << std::endl;
1635  *outstream << " <CALIB_TYPE>calib</CALIB_TYPE>" << std::endl;
1636  *outstream << " </DATA>" << std::endl;
1637  *outstream << " " << std::endl;
1638 }
1639 
1640 //=============================================================================================
1641 void PixelCalibConfiguration::writeXMLTrailer(std::ofstream* outstream,
1642  std::ofstream* out1stream,
1643  std::ofstream* out2stream) const {
1644  std::string mthn = "[PixelCalibConfiguration::writeXMLTrailer()]\t\t ";
1645 
1646  *outstream << " </DATA_SET>" << std::endl;
1647  *outstream << "</ROOT>" << std::endl;
1648 
1649  outstream->close();
1650  std::cout << __LINE__ << "]\t" << mthn << "Data written " << std::endl;
1651 }
std::vector< PixelROCName > getROCsFromModule(const PixelModuleName &aModule) const
unsigned int mfec() const
const PixelHdwAddress * getHdwAddress(const PixelROCName &aROC) const
std::set< unsigned int > getFEDCrates(const PixelNameTranslation *translation, const PixelFEDConfig *fedconfig) const
std::vector< std::vector< unsigned int > > rows_
bool scanningROCForState(PixelROCName roc, unsigned int state) const
This class specifies which FEC boards are used and how they are addressed.
This file contains the base class for "pixel configuration data" management.
unsigned int crateFromTKFECID(std::string TKFECID) const
unsigned int numberOfScanVariables() const
unsigned int crateFromFEDNumber(unsigned int fednumber) const
std::set< PixelChannel > channels_
void disablePixels(PixelFECConfigInterface *pixelFEC, unsigned int irows, unsigned int icols, pos::PixelROCTrimBits *trims, const PixelHdwAddress &theROC) const
std::vector< std::pair< unsigned int, std::vector< unsigned int > > > fedCardsAndChannels_
PixelROCTrimBits * trims_
const unsigned int k_DACAddress_WBC
Definition: PixelDACNames.h:70
const std::set< PixelModuleName > & moduleList() const
std::string mode() const override
unsigned int portaddress() const
void buildROCAndModuleLists(const PixelNameTranslation *translation, const PixelDetectorConfig *detconfig)
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision bits
std::vector< PixelROCInfo > rocInfo_
unsigned int fednumber() const
std::set< unsigned int > getFECCrates(const PixelNameTranslation *translation, const PixelFECConfig *fecconfig) const
unsigned int trim(unsigned int col, unsigned int row) const
std::set< unsigned int > getTKFECCrates(const PixelPortcardMap *portcardmap, const std::map< std::string, PixelPortCardConfig * > &mapNamePortCard, const PixelTKFECConfig *tkfecconfig) const
unsigned int rowCounter(unsigned int state) const
unsigned int numROCsCalibratedOnChannel(PixelROCName roc) const
unsigned int rocid() const
std::vector< std::pair< unsigned int, std::vector< unsigned int > > > & fedCardsAndChannels(unsigned int crate, PixelNameTranslation *translation, PixelFEDConfig *fedconfig, PixelDetectorConfig *detconfig) const
void buildROCAndModuleListsFromROCSet(const std::set< PixelROCName > &rocSet)
std::string TBMChannelString() const
Definition: PixelChannel.h:31
std::ostream & operator<<(std::ostream &s, const PixelCalibConfiguration &calib)
std::vector< PixelDACScanRange > dacs_
bool containsScan(std::string name) const
unsigned int mfecchannel() const
static std::string getmSecTime(void)
unsigned int crateFromFECNumber(unsigned int fecnumber) const
const PixelHdwAddress * hdwadd_
PixelCalibConfiguration(std::string filename="")
std::vector< std::pair< unsigned int, unsigned int > > defaultDACs_
void writeASCII(std::string dir="") const override
This class specifies which TKFEC boards are used and how they are addressed.
unsigned int iScan(std::string dac) const
static std::string getTime(void)
std::map< unsigned int, std::set< unsigned int > > getFEDsAndChannels(PixelNameTranslation *translation)
This class implements the steps that are used in a scan over Threshold and CalDelay.
std::string getComment() const
unsigned int fedchannel() const
unsigned int hubaddress() const
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
unsigned int colCounter(unsigned int state) const
This is the documentation about PixelDetectorConfig...
void writeXMLHeader(pos::PixelConfigKey key, int version, std::string path, std::ofstream *out, std::ofstream *out1=nullptr, std::ofstream *out2=nullptr) const override
This class provides utility methods to manipulate ASCII formatted timestamps.
Store mfec, mfecchannel etc.
std::vector< std::string > rocListInstructions_
std::set< std::pair< unsigned int, unsigned int > > pixelsWithHits(unsigned int state) const
std::string getAuthor() const
const std::string k_DACName_Vcal
Definition: PixelDACNames.h:37
std::string parameterValue(std::string parameterName) const
PixelROCMaskBits * masks_
const std::vector< PixelModuleName > & getModuleList() const
ii
Definition: cuy.py:590
void nextFECState(std::map< unsigned int, PixelFECConfigInterface * > &pixelFECs, PixelDetectorConfig *detconfig, PixelNameTranslation *trans, std::map< pos::PixelModuleName, pos::PixelMaskBase * > *masks, std::map< pos::PixelModuleName, pos::PixelTrimBase * > *trims, std::map< pos::PixelModuleName, pos::PixelDACSettings * > *dacss, unsigned int state) const
This is the documentation about PixelNameTranslation...
This class implements..
std::map< PixelROCName, unsigned int > ROCNumberOnChannelAmongThoseCalibrated_
unsigned int scanCounter(std::string dac, unsigned int state) const
std::string modulename() const
std::set< PixelModuleName > modules_
void writeXMLTrailer(std::ofstream *out, std::ofstream *out1=nullptr, std::ofstream *out2=nullptr) const override
virtual int progpix(int mfec, int mfecchannel, int hubaddress, int portaddress, int rocid, int coladdr, int rowaddress, unsigned char databyte, bool buffermode=false)=0
std::list< const PixelROCName * > getROCs() const
void enablePixels(PixelFECConfigInterface *pixelFEC, unsigned int irows, unsigned int icols, pos::PixelROCMaskBits *masks, pos::PixelROCTrimBits *trims, const PixelHdwAddress &theROC) const
std::set< PixelChannel > getChannelsOnModule(const PixelModuleName &aModule) const
This class implements..
std::map< std::string, std::string > parameters_
unsigned int ROCNumberOnChannelAmongThoseCalibrated(PixelROCName roc) const
This is the documentation about PixelNameTranslation...
This class implements..
This class implements..
Definition: PixelROCName.h:23
This class implements..
This class implements..
unsigned int scanROC(unsigned int state) const
#define begin
Definition: vmac.h:32
This is the documentation about PixelFEDConfig...
std::vector< PixelROCName > rocs_
bool containsModule(const PixelModuleName &moduleToFind) const
unsigned int mask(unsigned int col, unsigned int row) const
const std::set< std::string > portcards(const PixelModuleName &aModule) const
std::string scanName(unsigned int iscan) const
col
Definition: cuy.py:1010
const std::vector< PixelROCName > & getROCsFromChannel(const PixelChannel &aChannel) const
unsigned int fecnumber() const
A dummy class with ALL public variables.
step
Definition: StallMonitor.cc:94
unsigned int scanValue(std::string dac, unsigned int state, PixelROCName roc) const
void buildObjectsDependingOnTheNameTranslation(const PixelNameTranslation *aNameTranslation)
const std::map< PixelROCName, PixelROCStatus > & getROCsList() const
std::map< PixelROCName, unsigned int > numROCsCalibratedOnChannel_
tmp
align.sh
Definition: createJobs.py:716
void writeXML(pos::PixelConfigKey key, int version, std::string path) const override
Definition: vlib.h:198
std::vector< std::vector< unsigned int > > cols_
const PixelChannel & getChannelForROC(const PixelROCName &aROC) const
void clear(ROCstatus stat)
This class implements the steps that are used in a scan over Threshold and CalDelay.