CMS 3D CMS Logo

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