Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:58:14

0001 //
0002 // This class provide a base class for the
0003 // pixel ROC DAC data for the pixel FEC configuration
0004 //
0005 //
0006 //
0007 //
0008 
0009 #include "CalibFormats/SiPixelObjects/interface/PixelDACSettings.h"
0010 #include "CalibFormats/SiPixelObjects/interface/PixelROCDACSettings.h"
0011 #include "CalibFormats/SiPixelObjects/interface/PixelDACNames.h"
0012 #include "CalibFormats/SiPixelObjects/interface/PixelTimeFormatter.h"
0013 #include <fstream>
0014 #include <iostream>
0015 #include <ios>
0016 #include <cassert>
0017 #include <stdexcept>
0018 #include <map>
0019 #include <sstream>
0020 #include <sys/time.h>
0021 #include <cstdlib>
0022 
0023 using namespace pos;
0024 
0025 namespace {
0026   const bool readTemperatures = false;
0027   //const bool readTemperatures = true;
0028   //const int temperatureReg = 0x9; // hardwire to fixed reference voltage 0x8 + 0x1
0029   const int temperatureReg = 0x1;  // hardwire to the usefull range, change to range 1, Marco's request, 25/10/11
0030 }  // namespace
0031 
0032 PixelDACSettings::PixelDACSettings(std::string filename) : PixelConfigBase("", "", "") {
0033   std::string mthn = "[PixelDACSettings::PixelDACSettings()]\t\t\t    ";
0034 
0035   if (filename[filename.size() - 1] == 't') {
0036     std::ifstream in(filename.c_str());
0037 
0038     if (!in.good()) {
0039       std::cout << __LINE__ << "]\t" << mthn << "Could not open: " << filename << std::endl;
0040       // assert(0); //in case of failure, we don't want POS to die here
0041       throw std::runtime_error("Failed to open file " + filename);
0042     } else {
0043       // std::cout << "Opened:"<<filename<<std::endl;
0044     }
0045 
0046     dacsettings_.clear();
0047 
0048     std::string tag;
0049 
0050     in >> tag;
0051 
0052     while (!in.eof()) {
0053       PixelROCName rocid(in);
0054 
0055       //        std::cout << "[PixelDACSettings::PixelDACSettings()] DAC setting ROC id:"<<rocid<<std::endl;
0056 
0057       PixelROCDACSettings tmp;
0058 
0059       tmp.read(in, rocid);
0060 
0061       //        std::cout << "[PixelDACSettings::PixelDACSettings()] DACSetting: " << std::endl << tmp << std::endl ;
0062       dacsettings_.push_back(tmp);
0063 
0064       in >> tag;
0065 
0066       assert(dacsettings_.size() < 100);
0067     }
0068 
0069     in.close();
0070 
0071   } else {
0072     std::ifstream in(filename.c_str(), std::ios::binary);
0073 
0074     if (!in.good()) {
0075       std::cout << __LINE__ << "]\t" << mthn << "Could not open: " << filename << std::endl;
0076       assert(0);
0077     } else {
0078       std::cout << __LINE__ << "]\t" << mthn << "Opened: " << filename << std::endl;
0079     }
0080 
0081     char nchar;
0082 
0083     in.read(&nchar, 1);
0084 
0085     std::string s1;
0086 
0087     //wrote these lines of code without ref. needs to be fixed
0088     for (int i = 0; i < nchar; i++) {
0089       char c;
0090       in >> c;
0091       s1.push_back(c);
0092     }
0093 
0094     //std::cout << __LINE__ << "]\t" << mthn << "READ ROC name: " << s1 << std::endl;
0095 
0096     dacsettings_.clear();
0097 
0098     while (!in.eof()) {
0099       //std::cout << __LINE__ << "]\t" << mthn << "read s1   : " << s1    << std::endl;
0100 
0101       PixelROCName rocid(s1);
0102 
0103       //std::cout << __LINE__ << "]\t" << mthn << "read rocid: " << rocid << std::endl;
0104 
0105       PixelROCDACSettings tmp;
0106 
0107       tmp.readBinary(in, rocid);
0108 
0109       dacsettings_.push_back(tmp);
0110 
0111       in.read(&nchar, 1);
0112 
0113       s1.clear();
0114 
0115       if (in.eof())
0116         continue;
0117 
0118       //wrote these lines of code without ref. needs to be fixed
0119       for (int i = 0; i < nchar; i++) {
0120         char c;
0121         in >> c;
0122         s1.push_back(c);
0123       }
0124     }
0125 
0126     in.close();
0127   }
0128 
0129   //std::cout << __LINE__ << "]\t" << mthn << "Read dac settings for "<<dacsettings_.size()<<" ROCs"<<std::endl;
0130 }
0131 // modified by MR on 10-01-2008 14:48:19
0132 PixelDACSettings::PixelDACSettings(PixelROCDACSettings& rocname) : PixelConfigBase("", "", "") {
0133   dacsettings_.push_back(rocname);
0134 }
0135 
0136 // modified by MR on 24-01-2008 14:27:35a
0137 void PixelDACSettings::addROC(PixelROCDACSettings& rocname) { dacsettings_.push_back(rocname); }
0138 
0139 PixelDACSettings::PixelDACSettings(std::vector<std::vector<std::string> >& tableMat) : PixelConfigBase("", "", "") {
0140   /*
0141  EXTENSION_TABLE_NAME: ROC_DAC_SETTINGS_COL (VIEW: CONF_KEY_ROCDAC_COL_V)
0142  
0143  CONFIG_KEY                NOT NULL VARCHAR2(80)
0144  KEY_TYPE                  NOT NULL VARCHAR2(80)
0145  KEY_ALIAS                 NOT NULL VARCHAR2(80)
0146  VERSION                        VARCHAR2(40)
0147  KIND_OF_COND                  NOT NULL VARCHAR2(40)
0148  ROC_NAME                       VARCHAR2(200)
0149  VDD                       NOT NULL NUMBER(38)
0150  VANA                      NOT NULL NUMBER(38)
0151  VSF                       NOT NULL NUMBER(38)
0152  VCOMP                     NOT NULL NUMBER(38)
0153  VLEAK                     NOT NULL NUMBER(38)
0154  VRGPR                     NOT NULL NUMBER(38)
0155  VWLLPR                    NOT NULL NUMBER(38)
0156  VRGSH                     NOT NULL NUMBER(38)
0157  VWLLSH                    NOT NULL NUMBER(38)
0158  VHLDDEL                   NOT NULL NUMBER(38)
0159  VTRIM                     NOT NULL NUMBER(38)
0160  VCTHR                     NOT NULL NUMBER(38)
0161  VIBIAS_BUS                NOT NULL NUMBER(38)
0162  VIBIAS_SF                 NOT NULL NUMBER(38)
0163  VOFFSETOP                 NOT NULL NUMBER(38)
0164  VBIASOP                   NOT NULL NUMBER(38)
0165  VOFFSETRO                 NOT NULL NUMBER(38)
0166  VION                      NOT NULL NUMBER(38)
0167  VIBIAS_PH                 NOT NULL NUMBER(38)
0168  VIBIAS_DAC                NOT NULL NUMBER(38)
0169  VIBIAS_ROC                NOT NULL NUMBER(38)
0170  VICOLOR                   NOT NULL NUMBER(38)
0171  VNPIX                     NOT NULL NUMBER(38)
0172  VSUMCOL                   NOT NULL NUMBER(38)
0173  VCAL                      NOT NULL NUMBER(38)
0174  CALDEL                    NOT NULL NUMBER(38)
0175  TEMPRANGE                 NOT NULL NUMBER(38)
0176  WBC                       NOT NULL NUMBER(38)
0177  CHIPCONTREG                   NOT NULL NUMBER(38)
0178 */
0179   //   std::multimap<std::string,std::pair<std::string,int > > pDSM;
0180   //  std::stringstream currentRocName;
0181   std::vector<std::string> ins = tableMat[0];
0182   std::string mthn("[PixelDACSettings::PixelDACSettings()] ");
0183   std::string dacName;
0184   std::istringstream dbin;
0185   //   int dacValue;
0186   int skipColumns = 0;
0187   std::map<std::string, int> colM;
0188   std::vector<std::string> colNames;
0189   std::map<std::string, std::string> nameTranslation;
0190 
0191   colNames.push_back("CONFIG_KEY");
0192   colNames.push_back("KEY_TYPE");
0193   colNames.push_back("KEY_ALIAS");
0194   colNames.push_back("VERSION");
0195   colNames.push_back("KIND_OF_COND");
0196   colNames.push_back("ROC_NAME");
0197   colNames.push_back("VDD");
0198   colNames.push_back("VANA");
0199   colNames.push_back("VSF");
0200   colNames.push_back("VCOMP");
0201   colNames.push_back("VLEAK");
0202   colNames.push_back("VRGPR");
0203   colNames.push_back("VWLLPR");
0204   colNames.push_back("VRGSH");
0205   colNames.push_back("VWLLSH");
0206   colNames.push_back("VHLDDEL");
0207   colNames.push_back("VTRIM");
0208   colNames.push_back("VCTHR");
0209   colNames.push_back("VIBIAS_BUS");
0210   colNames.push_back("VIBIAS_SF");
0211   colNames.push_back("VOFFSETOP");
0212   colNames.push_back("VBIASOP");
0213   colNames.push_back("VOFFSETRO");
0214   colNames.push_back("VION");
0215   colNames.push_back("VIBIAS_PH");
0216   colNames.push_back("VIBIAS_DAC");
0217   colNames.push_back("VIBIAS_ROC");
0218   colNames.push_back("VICOLOR");
0219   colNames.push_back("VNPIX");
0220   colNames.push_back("VSUMCOL");
0221   colNames.push_back("VCAL");
0222   colNames.push_back("CALDEL");
0223   colNames.push_back("TEMPRANGE");
0224   colNames.push_back("WBC");
0225   colNames.push_back("CHIPCONTREG");
0226 
0227   nameTranslation["VDD"] = k_DACName_Vdd;
0228   nameTranslation["VANA"] = k_DACName_Vana;
0229   nameTranslation["VSF"] = k_DACName_Vsf;
0230   nameTranslation["VCOMP"] = k_DACName_Vcomp;
0231   nameTranslation["VLEAK"] = k_DACName_Vleak;
0232   nameTranslation["VRGPR"] = k_DACName_VrgPr;
0233   nameTranslation["VWLLPR"] = k_DACName_VwllPr;
0234   nameTranslation["VRGSH"] = k_DACName_VrgSh;
0235   nameTranslation["VWLLSH"] = k_DACName_VwllSh;
0236   nameTranslation["VHLDDEL"] = k_DACName_VHldDel;
0237   nameTranslation["VTRIM"] = k_DACName_Vtrim;
0238   nameTranslation["VCTHR"] = k_DACName_VcThr;
0239   nameTranslation["VIBIAS_BUS"] = k_DACName_VIbias_bus;
0240   nameTranslation["VIBIAS_SF"] = k_DACName_VIbias_sf;
0241   nameTranslation["VOFFSETOP"] = k_DACName_VOffsetOp;
0242   nameTranslation["VBIASOP"] = k_DACName_VbiasOp;
0243   nameTranslation["VOFFSETRO"] = k_DACName_VOffsetRO;
0244   nameTranslation["VION"] = k_DACName_VIon;
0245   nameTranslation["VIBIAS_PH"] = k_DACName_VIbias_PH;
0246   nameTranslation["VIBIAS_DAC"] = k_DACName_VIbias_DAC;
0247   nameTranslation["VIBIAS_ROC"] = k_DACName_VIbias_roc;
0248   nameTranslation["VICOLOR"] = k_DACName_VIColOr;
0249   nameTranslation["VNPIX"] = k_DACName_Vnpix;
0250   nameTranslation["VSUMCOL"] = k_DACName_VsumCol;
0251   nameTranslation["VCAL"] = k_DACName_Vcal;
0252   nameTranslation["CALDEL"] = k_DACName_CalDel;
0253   nameTranslation["TEMPRANGE"] = k_DACName_TempRange;
0254   nameTranslation["WBC"] = k_DACName_WBC;
0255   nameTranslation["CHIPCONTREG"] = k_DACName_ChipContReg;
0256 
0257   // modified by MR on 25-02-2008 10:00:45
0258   // colM stores the index (referred to tableMat) where the specified dac setting is store!!!
0259   for (unsigned int c = skipColumns; c < ins.size(); c++) {
0260     for (unsigned int n = 0; n < colNames.size(); n++) {
0261       if (tableMat[0][c] == colNames[n]) {
0262         colM[colNames[n]] = c;
0263         break;
0264       }
0265     }
0266   }  //end for
0267   for (unsigned int n = skipColumns; n < colNames.size(); n++) {
0268     if (colM.find(colNames[n]) == colM.end()) {
0269       std::cerr << "[PixelDACSettings::PixelDACSettings()]\tCouldn't find in the database the column with name "
0270                 << colNames[n] << std::endl;
0271       assert(0);
0272     }
0273   }
0274 
0275   dacsettings_.clear();
0276   //   struct timeval  start_time  ;
0277   //   struct timeval  end_time    ;
0278   //   gettimeofday(&start_time, (struct timezone *)0 );
0279   for (unsigned int r = 1; r < tableMat.size(); r++) {  //Goes to every row of the Matrix
0280     // currentRocName.str("");
0281     // currentRocName << tableMat[r][colM["NAME"]] ;
0282     //currentRocName << "FPix_BmI_D" << tableMat[r][colM["HDISK_POSN"]]
0283     //     << "_BLD"       << tableMat[r][colM["BLD_POSN"]]
0284     //     << "_PNL"       << tableMat[r][colM["PANEL_POSITION"]]
0285     //     << "_PLQ"       << tableMat[r][colM["PLAQ_POS"]]
0286     //     << "_ROC"       << tableMat[r][colM["ROC_POSN"]];
0287 
0288     // modified by MR on 25-02-2008 10:04:55
0289     PixelROCName rocid(tableMat[r][colM["ROC_NAME"]]);
0290     PixelROCDACSettings tmp(rocid);
0291     std::ostringstream dacs("");
0292     // +6 to get rid of the first 5 columns not pertaining DAC Settings...
0293     for (unsigned int n = skipColumns + 6; n < colNames.size(); n++) {
0294       dacs << nameTranslation[colNames[n]] << ": " << atoi(tableMat[r][colM[colNames[n]]].c_str()) << std::endl;
0295       //       dacName  = colNames[n];
0296       //       dacValue = atoi(tableMat[r][colM[colNames[n]]].c_str());
0297       //       pDSM.insert(std::pair<std::string,std::pair<std::string,int> >(tableMat[r][colM["ROC_NAME"]],std::pair<std::string,int>(dacName,dacValue)));
0298       //       std::cout << "On " << tableMat[r][colM["ROC_NAME"]] << " DAC:\t" << dacName << " value:\t" << dacValue<< std::endl ;
0299       //       tmp.setDac(dacName, dacValue) ;
0300     }
0301     //     tmp.setDACs(tmpDACs) ;
0302     dbin.str(dacs.str());
0303     tmp.read(dbin, rocid);
0304     dacsettings_.push_back(tmp);
0305   }  //end for r
0306   //   gettimeofday(&end_time, (struct timezone *)0 );
0307   //   int total_usecs = (end_time.tv_sec - start_time.tv_sec)*1000000 + (end_time.tv_usec - start_time.tv_usec);
0308   //   std::cout << mthn << "Time taken : " << total_usecs / 1000000.  << " secs" << std::endl;
0309 
0310   //   dacsettings_.clear();
0311   //   std::string currentRocName2 = "";
0312   //   for(std::multimap<std::string,std::pair<std::string,int> >::iterator tableMapIt=pDSM.begin(); tableMapIt!= pDSM.end(); tableMapIt++){
0313   //     if(currentRocName2 != tableMapIt->first){
0314   //       std::cout << tableMapIt->first << std::endl;
0315   //       std::cout << tableMapIt->second.first << std::endl;
0316   //       std::cout << tableMapIt->second.second << std::endl;
0317   //       currentRocName2 = tableMapIt->first;
0318   //       PixelROCName rocid(currentRocName2);
0319 
0320   //       // std::cout << "DAC setting ROC id:"<<rocid<<std::endl;
0321 
0322   //       PixelROCDACSettings tmp(rocid);
0323 
0324   //       //       tmp.read(in,rocid);
0325 
0326   //       dacsettings_.push_back(tmp);
0327   //     }//end if
0328   //     dacsettings_[dacsettings_.size()-1].setDac(tableMapIt->second.first,tableMapIt->second.second);
0329   //   }//end for
0330 
0331   //   for(unsigned int w = 0 ; w < dacsettings_.size() ; w++)
0332   //     {
0333 
0334   //       PixelROCDACSettings tmp2 = dacsettings_[w];
0335   //       //   std::cout<<tmp2<<std::endl;
0336   //     }
0337   //  std::cout<<"Number of ROCs in the PixelDACSettings::PixelDACSettings(vector <vector<string> >):"<<dacsettings_.size()<<std::endl;
0338   //  std::cout << "[PixelDACSettings::PixelDACSettings(std::vector)] before end of constructor" << std::endl ;
0339 }  //end PDSMatrix constructor
0340 //end added by Umesh
0341 
0342 PixelROCDACSettings PixelDACSettings::getDACSettings(int ROCId) const { return dacsettings_[ROCId]; }
0343 
0344 PixelROCDACSettings* PixelDACSettings::getDACSettings(PixelROCName name) {
0345   for (unsigned int i = 0; i < dacsettings_.size(); i++) {
0346     if (dacsettings_[i].getROCName() == name)
0347       return &(dacsettings_[i]);
0348   }
0349 
0350   return nullptr;
0351 }
0352 
0353 void PixelDACSettings::writeBinary(std::string filename) const {
0354   std::ofstream out(filename.c_str(), std::ios::binary);
0355 
0356   for (unsigned int i = 0; i < dacsettings_.size(); i++) {
0357     dacsettings_[i].writeBinary(out);
0358   }
0359 }
0360 
0361 void PixelDACSettings::writeASCII(std::string dir) const {
0362   std::string mthn = "[PixelDACSettings::writeASCII()]\t\t\t    ";
0363   PixelModuleName module(dacsettings_[0].getROCName().rocname());
0364 
0365   std::string filename = dir + "/ROC_DAC_module_" + module.modulename() + ".dat";
0366   std::cout << __LINE__ << "]\t" << mthn << "Writing to file " << filename << std::endl;
0367   std::ofstream out(filename.c_str());
0368 
0369   for (unsigned int i = 0; i < dacsettings_.size(); i++) {
0370     dacsettings_[i].writeASCII(out);
0371   }
0372 }
0373 
0374 //=============================================================================================
0375 void PixelDACSettings::writeXMLHeader(pos::PixelConfigKey key,
0376                                       int version,
0377                                       std::string path,
0378                                       std::ofstream* outstream,
0379                                       std::ofstream* out1stream,
0380                                       std::ofstream* out2stream) const {
0381   std::string mthn = "[PixelDACSettings::writeXMLHeader()]\t\t\t    ";
0382   std::stringstream fullPath;
0383 
0384   fullPath << path << "/Pixel_RocDacSettings_" << PixelTimeFormatter::getmSecTime() << ".xml";
0385   std::cout << __LINE__ << "]\t" << mthn << "Writing to: " << fullPath.str() << std::endl;
0386 
0387   outstream->open(fullPath.str().c_str());
0388 
0389   *outstream << "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>" << std::endl;
0390   *outstream << "<ROOT xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>" << std::endl;
0391   *outstream << "" << std::endl;
0392   *outstream << " <!-- " << mthn << "-->" << std::endl;
0393   *outstream << "" << std::endl;
0394   *outstream << " <HEADER>" << std::endl;
0395   *outstream << "  <TYPE>" << std::endl;
0396   *outstream << "   <EXTENSION_TABLE_NAME>ROC_DAC_SETTINGS_COL</EXTENSION_TABLE_NAME>" << std::endl;
0397   *outstream << "   <NAME>ROC DAC Settings Col</NAME>" << std::endl;
0398   *outstream << "  </TYPE>" << std::endl;
0399   *outstream << "  <RUN>" << std::endl;
0400   *outstream << "   <RUN_TYPE>ROC DAC Settings</RUN_TYPE>" << std::endl;
0401   *outstream << "   <RUN_NUMBER>1</RUN_NUMBER>" << std::endl;
0402   *outstream << "   <RUN_BEGIN_TIMESTAMP>" << PixelTimeFormatter::getTime() << "</RUN_BEGIN_TIMESTAMP>" << std::endl;
0403   *outstream << "   <LOCATION>CERN P5</LOCATION>" << std::endl;
0404   *outstream << "  </RUN>" << std::endl;
0405   *outstream << " </HEADER>" << std::endl;
0406   *outstream << "" << std::endl;
0407   *outstream << " <DATA_SET>" << std::endl;
0408   *outstream << "  <VERSION>" << version << "</VERSION>" << std::endl;
0409   *outstream << "  <COMMENT_DESCRIPTION>" << getComment() << "</COMMENT_DESCRIPTION>" << std::endl;
0410   *outstream << "  <CREATED_BY_USER>" << getAuthor() << "</CREATED_BY_USER>" << std::endl;
0411   *outstream << " " << std::endl;
0412   *outstream << "  <PART>" << std::endl;
0413   *outstream << "   <NAME_LABEL>CMS-PIXEL-ROOT</NAME_LABEL>" << std::endl;
0414   *outstream << "   <KIND_OF_PART>Detector ROOT</KIND_OF_PART>" << std::endl;
0415   *outstream << "  </PART>" << std::endl;
0416   *outstream << " " << std::endl;
0417 
0418   std::cout << __LINE__ << "]\t" << mthn << "Header written" << std::endl;
0419 }
0420 
0421 //=============================================================================================
0422 void PixelDACSettings::writeXML(std::ofstream* outstream, std::ofstream* out1stream, std::ofstream* out2stream) const {
0423   std::string mthn = "[PixelDACSettings::writeXML()]\t\t\t    ";
0424 
0425   for (unsigned int i = 0; i < dacsettings_.size(); i++) {
0426     dacsettings_[i].writeXML(outstream);
0427   }
0428 }
0429 
0430 //=============================================================================================
0431 void PixelDACSettings::writeXMLTrailer(std::ofstream* outstream,
0432                                        std::ofstream* out1stream,
0433                                        std::ofstream* out2stream) const {
0434   std::string mthn = "[PixelDACSettings::writeXMLTrailer()]\t\t\t    ";
0435 
0436   *outstream << " </DATA_SET>" << std::endl;
0437   *outstream << "</ROOT>" << std::endl;
0438 
0439   outstream->close();
0440   std::cout << __LINE__ << "]\t" << mthn << "Data written" << std::endl;
0441 }
0442 
0443 /* O B S O L E T E -----
0444 
0445 //=============================================================================================
0446 void PixelDACSettings::writeXML(pos::PixelConfigKey key, int version, std::string path) const {
0447   std::string mthn = "[PixelDACSettings::writeXML()]\t\t\t    " ;
0448   std::stringstream fullPath ;
0449 
0450   PixelModuleName module(dacsettings_[0].getROCName().rocname());
0451   fullPath << path << "/dacsettings_" << module.modulename() << ".xml" ;
0452   std::cout << mthn << "Writing to: |" << fullPath.str()  << "|" << std::endl ;
0453   
0454 
0455   std::ofstream outstream(fullPath.str().c_str()) ;
0456   
0457   out << "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"                   << std::endl ;
0458   out << "<ROOT xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>"                  << std::endl ;
0459   out << " <HEADER>"                                             << std::endl ;
0460   out << "  <TYPE>"                                          << std::endl ;
0461   out << "   <EXTENSION_TABLE_NAME>ROC_DAC_SETTINGS_COL</EXTENSION_TABLE_NAME>"          << std::endl ;
0462   out << "   <NAME>ROC DAC Settings Col</NAME>"                          << std::endl ;
0463   out << "  </TYPE>"                                             << std::endl ;
0464   out << "  <RUN>"                                           << std::endl ;
0465   out << "   <RUN_TYPE>test</RUN_TYPE>"                                  << std::endl ;
0466   out << "   <RUN_NUMBER>1</RUN_NUMBER>"                                 << std::endl ;
0467   out << "   <RUN_BEGIN_TIMESTAMP>" << PixelTimeFormatter::getTime() << "</RUN_BEGIN_TIMESTAMP>" << std::endl ;
0468   out << "   <COMMENT_DESCRIPTION>Test of DAC Settings xml</COMMENT_DESCRIPTION>"        << std::endl ;
0469   out << "   <LOCATION>CERN TAC</LOCATION>"                              << std::endl ;
0470   out << "   <CREATED_BY_USER>Dario Menasce</CREATED_BY_USER>"                       << std::endl ;
0471   out << "  </RUN>"                                          << std::endl ;
0472   out << " </HEADER>"                                            << std::endl ;
0473   out << ""                                              << std::endl ;
0474   out << " <DATA_SET>"                                                                   << std::endl ;
0475   out << "  <VERSION>" << version << "</VERSION>"                                        << std::endl ;
0476   out << " "                                                                             << std::endl ;
0477   out << "  <PART>"                                                                      << std::endl ;
0478   out << "   <NAME_LABEL>CMS-PIXEL-ROOT</NAME_LABEL>"                                    << std::endl ;
0479   out << "   <KIND_OF_PART>Detector ROOT</KIND_OF_PART>"                                 << std::endl ;
0480   out << "  </PART>"                                                                     << std::endl ;
0481   out << " "                                                                             << std::endl ;
0482 
0483   for(unsigned int i=0;i<dacsettings_.size();i++){
0484 //    dacsettings_[i].writeXML(out, key, version, path);
0485   }
0486 
0487   out << " </DATA_SET>"                                                                          << std::endl ;
0488   out << "</ROOT>"                                                                               << std::endl ;
0489 
0490   out.close() ;
0491   std::cout << mthn << "Data written"                                                            << std::endl ;
0492 }
0493 */
0494 
0495 //=============================================================================================
0496 void PixelDACSettings::generateConfiguration(PixelFECConfigInterface* pixelFEC,
0497                                              PixelNameTranslation* trans,
0498                                              PixelDetectorConfig* detconfig,
0499                                              bool HVon) const {
0500   bool bufferData = true;
0501 
0502   std::vector<unsigned int> dacs;
0503 
0504   //pixelFEC->fecDebug(1);  //FIXME someday maybe don't want to take the time
0505 
0506   for (unsigned int i = 0; i < dacsettings_.size(); i++) {  // loop over ROCs
0507 
0508     bool disableRoc = rocIsDisabled(detconfig, dacsettings_[i].getROCName());
0509 
0510     dacsettings_[i].getDACs(dacs);
0511 
0512     PixelHdwAddress theROC = *(trans->getHdwAddress(dacsettings_[i].getROCName()));
0513 
0514     //Need to set readout speed (40MHz) and Vcal range (0-1800 mV) and enable the chip
0515 
0516     // This is not needed. The ControlReg is programmed in setAllDAC(). d.k. 21.01.11
0517     //     pixelFEC->progdac(theROC.mfec(),
0518     //            theROC.mfecchannel(),
0519     //            theROC.hubaddress(),
0520     //            theROC.portaddress(),
0521     //            theROC.rocid(),
0522     //            0xfd,
0523     //            controlreg,
0524     //            bufferData);
0525 
0526     if (!HVon || disableRoc)
0527       dacs[11] = 0;  //set Vcthr DAC to 0 (Vcthr is DAC 12=11+1)
0528     //    std::cout<<" ; setting VcThr to "<<dacs[11]<<std::endl; //for debugging
0529     pixelFEC->setAllDAC(theROC, dacs, bufferData);
0530 
0531     // start with no pixels on for calibration
0532     pixelFEC->clrcal(
0533         theROC.mfec(), theROC.mfecchannel(), theROC.hubaddress(), theROC.portaddress(), theROC.rocid(), bufferData);
0534 
0535     const bool kmeKLUDGE = false;
0536     if (kmeKLUDGE)  //enable one pixel per ROC for calibration (all the time!)
0537     {
0538       unsigned int col = 0;
0539       //for(unsigned int col=0;col<52;col+=50) //try 0, 50
0540       {
0541         pixelFEC->calpix(theROC.mfec(),
0542                          theROC.mfecchannel(),
0543                          theROC.hubaddress(),
0544                          theROC.portaddress(),
0545                          theROC.rocid(),
0546                          col,  //column
0547                          0,    //row
0548                          1,    //caldata
0549                          bufferData);
0550       }
0551     }
0552 
0553     // enable all the double columns
0554     for (int dcol = 0; dcol < 26; dcol++) {
0555       pixelFEC->dcolenable(theROC.mfec(),
0556                            theROC.mfecchannel(),
0557                            theROC.hubaddress(),
0558                            theROC.portaddress(),
0559                            theROC.rocid(),
0560                            dcol,
0561                            1,
0562                            bufferData);
0563     }
0564 
0565     if (!HVon || disableRoc) {  //HV off
0566       int controlreg = dacsettings_[i].getControlRegister();
0567       //      std::cout << "[PixelDACSettings::generateConfiguration] HV off! ROC control reg to be set to: " <<  (controlreg|0x2) <<std::endl;
0568       pixelFEC->progdac(theROC.mfec(),
0569                         theROC.mfecchannel(),
0570                         theROC.hubaddress(),
0571                         theROC.portaddress(),
0572                         theROC.rocid(),
0573                         0xfd,
0574                         controlreg | 0x2,  //=010 in binary. should disable the chip
0575                         bufferData);
0576     }  //HV off
0577 
0578     // Now program (again) the temperature register to make sure it is the last one
0579     // and appears in the LastDAC
0580     if (readTemperatures) {
0581       //     std::cout<<"ROC="<<dacsettings_[i].getROCName()<<" ; VcThr set to "<<dacs[11]
0582       //       << " ROC control reg to be set to: " <<  dacs[28] <<" LastDAC=Temp"<<std::endl;
0583       if ((theROC.mfec() == 1) && (theROC.mfecchannel() == 1) && (theROC.hubaddress() == 0) &&
0584           (theROC.portaddress() == 0) && (theROC.rocid()))
0585         std::cout << "ROC=" << dacsettings_[i].getROCName() << " ROC control reg to be set to: " << dacs[28]
0586                   << " LastDAC=Temp " << temperatureReg << std::endl;
0587       //int temperatureReg = dacs[26];  // overwrite with the number from DB
0588       pixelFEC->progdac(theROC.mfec(),
0589                         theROC.mfecchannel(),
0590                         theROC.hubaddress(),
0591                         theROC.portaddress(),
0592                         theROC.rocid(),
0593                         0x1B,
0594                         temperatureReg,
0595                         bufferData);
0596     } else {
0597       //      std::cout<<"ROC="<<dacsettings_[i].getROCName()<<" ; VcThr set to "<<dacs[11]
0598       //           << " ROC control reg to be set to: " <<  dacs[28] <<" LastDAC=Vcal"<<std::endl;
0599       if ((theROC.mfec() == 1) && (theROC.mfecchannel() == 1) && (theROC.hubaddress() == 0) &&
0600           (theROC.portaddress() == 0) && (theROC.rocid()))
0601         std::cout << "ROC=" << dacsettings_[i].getROCName() << " ROC control reg to be set to: " << dacs[28]
0602                   << " LastDAC=Vcal" << std::endl;
0603       // VCAL
0604       pixelFEC->progdac(theROC.mfec(),
0605                         theROC.mfecchannel(),
0606                         theROC.hubaddress(),
0607                         theROC.portaddress(),
0608                         theROC.rocid(),
0609                         0x19,
0610                         200,
0611                         bufferData);
0612     }
0613 
0614   }  // end ROC loop
0615 
0616   if (bufferData) {  // Send data to the FEC
0617     pixelFEC->qbufsend();
0618   }
0619 }
0620 
0621 void PixelDACSettings::setVcthrDisable(PixelFECConfigInterface* pixelFEC, PixelNameTranslation* trans) const {
0622   //the point here is to set Vcthr to 0
0623   //then disable the ROCs
0624 
0625   //note -- no need to look at the detconfig here, because we're going to disable everything no matter what
0626 
0627   bool bufferData = true;
0628 
0629   std::vector<unsigned int> dacs;
0630 
0631   for (unsigned int i = 0; i < dacsettings_.size(); i++) {  //loop over the ROCs
0632 
0633     dacsettings_[i].getDACs(dacs);
0634     int controlreg = dacsettings_[i].getControlRegister();
0635 
0636     PixelHdwAddress theROC = *(trans->getHdwAddress(dacsettings_[i].getROCName()));
0637 
0638     //std::cout<<"disabling ROC="<<dacsettings_[i].getROCName()<<std::endl;
0639     pixelFEC->progdac(theROC.mfec(),
0640                       theROC.mfecchannel(),
0641                       theROC.hubaddress(),
0642                       theROC.portaddress(),
0643                       theROC.rocid(),
0644                       12,  //12 == Vcthr
0645                       0,   //set Vcthr to 0
0646                       bufferData);
0647 
0648     //this should disable the roc
0649     pixelFEC->progdac(theROC.mfec(),
0650                       theROC.mfecchannel(),
0651                       theROC.hubaddress(),
0652                       theROC.portaddress(),
0653                       theROC.rocid(),
0654                       0xfd,
0655                       controlreg | 0x2,
0656                       bufferData);
0657 
0658     // Now program (again) the temperature register to make sure it is the last one
0659     // and appears in the LastDAC
0660     if (readTemperatures) {
0661       //int temperatureReg = dacs[26];  // value from DB
0662       pixelFEC->progdac(theROC.mfec(),
0663                         theROC.mfecchannel(),
0664                         theROC.hubaddress(),
0665                         theROC.portaddress(),
0666                         theROC.rocid(),
0667                         0x1B,
0668                         temperatureReg,
0669                         bufferData);
0670     } else {
0671       // VCAL
0672       pixelFEC->progdac(theROC.mfec(),
0673                         theROC.mfecchannel(),
0674                         theROC.hubaddress(),
0675                         theROC.portaddress(),
0676                         theROC.rocid(),
0677                         0x19,
0678                         200,
0679                         bufferData);
0680     }
0681   }
0682 
0683   if (bufferData) {  //just copying the way it was done in the existing method
0684     pixelFEC->qbufsend();
0685   }
0686 }
0687 
0688 void PixelDACSettings::setVcthrEnable(PixelFECConfigInterface* pixelFEC,
0689                                       PixelNameTranslation* trans,
0690                                       PixelDetectorConfig* detconfig) const {
0691   //the point here is to set Vcthr to the nominal values
0692   //then enable the ROCs
0693 
0694   bool bufferData = true;
0695 
0696   std::vector<unsigned int> dacs;
0697 
0698   for (unsigned int i = 0; i < dacsettings_.size(); i++) {  //loop over the ROCs
0699 
0700     bool disableRoc =
0701         rocIsDisabled(detconfig, dacsettings_[i].getROCName());  //don't enable ROCs that are disabled in the detconfig
0702 
0703     //std::cout<<"ROC="<<dacsettings_[i].getROCName()<<" ; VcThr set to "<<dacs[11]
0704     //       << " ; ROC control reg to be set to: " <<  controlreg <<std::endl;
0705 
0706     if (!disableRoc) {  // Disable
0707 
0708       dacsettings_[i].getDACs(dacs);
0709       int controlreg = dacsettings_[i].getControlRegister();
0710 
0711       PixelHdwAddress theROC = *(trans->getHdwAddress(dacsettings_[i].getROCName()));
0712 
0713       pixelFEC->progdac(theROC.mfec(),
0714                         theROC.mfecchannel(),
0715                         theROC.hubaddress(),
0716                         theROC.portaddress(),
0717                         theROC.rocid(),
0718                         12,  //12 == Vcthr
0719                         dacs[11],
0720                         bufferData);
0721 
0722       //enable the roc (assuming controlreg was set for the roc to be enabled)
0723 
0724       pixelFEC->progdac(theROC.mfec(),
0725                         theROC.mfecchannel(),
0726                         theROC.hubaddress(),
0727                         theROC.portaddress(),
0728                         theROC.rocid(),
0729                         0xfd,
0730                         controlreg,
0731                         bufferData);
0732 
0733       // Now program (again) the temperature register to make sure it is the last one
0734       // and appears in the LastDAC
0735       if (readTemperatures) {
0736         //int temperatureReg = dacs[26];  // value from DB
0737         pixelFEC->progdac(theROC.mfec(),
0738                           theROC.mfecchannel(),
0739                           theROC.hubaddress(),
0740                           theROC.portaddress(),
0741                           theROC.rocid(),
0742                           0x1B,
0743                           temperatureReg,
0744                           bufferData);
0745       } else {
0746         // VCAL
0747         pixelFEC->progdac(theROC.mfec(),
0748                           theROC.mfecchannel(),
0749                           theROC.hubaddress(),
0750                           theROC.portaddress(),
0751                           theROC.rocid(),
0752                           0x19,
0753                           200,
0754                           bufferData);
0755       }
0756 
0757     }  // end disable
0758 
0759   }  // loop over ROCs
0760 
0761   if (bufferData) {  // Send data to FEC
0762     pixelFEC->qbufsend();
0763   }
0764 }
0765 
0766 bool PixelDACSettings::rocIsDisabled(const PixelDetectorConfig* detconfig, const PixelROCName rocname) const {
0767   const std::map<PixelROCName, PixelROCStatus>& roclist = detconfig->getROCsList();
0768   const std::map<PixelROCName, PixelROCStatus>::const_iterator iroc = roclist.find(rocname);
0769   assert(iroc != roclist.end());  // the roc name should always be found
0770   PixelROCStatus thisROCstatus = iroc->second;
0771 
0772   return thisROCstatus.get(PixelROCStatus::noAnalogSignal);
0773 }
0774 
0775 std::ostream& operator<<(std::ostream& s, const PixelDACSettings& dacs) {
0776   s << dacs.getDACSettings(0) << std::endl;
0777 
0778   return s;
0779 }