CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC4_patch1/src/CalibFormats/SiPixelObjects/src/PixelDACSettings.cc

Go to the documentation of this file.
00001 //
00002 // This class provide a base class for the
00003 // pixel ROC DAC data for the pixel FEC configuration
00004 //
00005 //
00006 //
00007 //
00008 
00009 
00010 #include "CalibFormats/SiPixelObjects/interface/PixelDACSettings.h"
00011 #include "CalibFormats/SiPixelObjects/interface/PixelROCDACSettings.h"
00012 #include "CalibFormats/SiPixelObjects/interface/PixelDACNames.h"
00013 #include "CalibFormats/SiPixelObjects/interface/PixelTimeFormatter.h"
00014 #include <fstream>
00015 #include <iostream>
00016 #include <ios>
00017 #include <assert.h>
00018 #include <stdexcept>
00019 #include <map>
00020 #include <sstream>
00021 #include <sys/time.h>
00022 #include <cstdlib>
00023 
00024 using namespace pos;
00025 
00026 PixelDACSettings::PixelDACSettings(std::string filename):
00027   PixelConfigBase("","",""){
00028 
00029   std::string mthn = "[PixelDACSettings::PixelDACSettings()]\t\t\t    " ;
00030   
00031   if (filename[filename.size()-1]=='t'){
00032 
00033     std::ifstream in(filename.c_str());
00034 
00035     if (!in.good()){
00036       std::cout << __LINE__ << "]\t" << mthn << "Could not open: " << filename << std::endl;
00037       // assert(0); //in case of failure, we don't want POS to die here
00038       throw std::runtime_error("Failed to open file "+filename);
00039     }
00040     else {
00041       // std::cout << "Opened:"<<filename<<std::endl;
00042     }
00043         
00044         
00045     dacsettings_.clear();
00046         
00047     std::string tag;
00048         
00049     in >> tag;
00050 
00051     while (!in.eof()){
00052             
00053             
00054       PixelROCName rocid(in);
00055 
00056       //            std::cout << "[PixelDACSettings::PixelDACSettings()] DAC setting ROC id:"<<rocid<<std::endl;
00057             
00058       PixelROCDACSettings tmp;
00059 
00060       tmp.read(in,rocid);
00061             
00062       //            std::cout << "[PixelDACSettings::PixelDACSettings()] DACSetting: " << std::endl << tmp << std::endl ;
00063       dacsettings_.push_back(tmp);
00064             
00065       in >> tag;
00066             
00067       assert(dacsettings_.size()<100);
00068             
00069     }
00070         
00071     in.close();
00072 
00073   }
00074   else{
00075 
00076     std::ifstream in(filename.c_str(),std::ios::binary);
00077 
00078     if (!in.good()){
00079       std::cout << __LINE__ << "]\t" << mthn << "Could not open: " << filename << std::endl;
00080       assert(0);
00081     }
00082     else {
00083       std::cout << __LINE__ << "]\t" << mthn << "Opened: "         << filename << std::endl;
00084     }
00085 
00086     char nchar;
00087 
00088     in.read(&nchar,1);
00089         
00090     std::string s1;
00091 
00092     //wrote these lines of code without ref. needs to be fixed
00093     for(int i=0;i< nchar; i++){
00094       char c;
00095       in >>c;
00096       s1.push_back(c);
00097     }
00098 
00099     //std::cout << __LINE__ << "]\t" << mthn << "READ ROC name: " << s1 << std::endl;
00100 
00101     dacsettings_.clear();
00102 
00103 
00104     while (!in.eof()){
00105 
00106       //std::cout << __LINE__ << "]\t" << mthn << "read s1   : " << s1    << std::endl;
00107 
00108       PixelROCName rocid(s1);
00109 
00110       //std::cout << __LINE__ << "]\t" << mthn << "read rocid: " << rocid << std::endl;
00111             
00112       PixelROCDACSettings tmp;
00113       
00114       tmp.readBinary(in, rocid);
00115 
00116       dacsettings_.push_back(tmp);
00117 
00118 
00119       in.read(&nchar,1);
00120 
00121       s1.clear();
00122 
00123       if (in.eof()) continue;
00124 
00125       //wrote these lines of code without ref. needs to be fixed
00126       for(int i=0;i< nchar; i++){
00127         char c;
00128         in >>c;
00129         s1.push_back(c);
00130       }
00131 
00132 
00133     }
00134 
00135     in.close();
00136 
00137 
00138 
00139   }
00140 
00141 
00142   //std::cout << __LINE__ << "]\t" << mthn << "Read dac settings for "<<dacsettings_.size()<<" ROCs"<<std::endl;
00143 
00144 
00145 }
00146 // modified by MR on 10-01-2008 14:48:19
00147 PixelDACSettings::PixelDACSettings(PixelROCDACSettings &rocname):
00148   PixelConfigBase("","","") {
00149   dacsettings_.push_back(rocname) ;
00150 }
00151 
00152 // modified by MR on 24-01-2008 14:27:35a
00153 void PixelDACSettings::addROC(PixelROCDACSettings &rocname)
00154 {
00155   dacsettings_.push_back(rocname) ;
00156 }
00157 
00158 PixelDACSettings::PixelDACSettings(std::vector< std::vector<std::string> > &tableMat): PixelConfigBase("","","")
00159 {
00160 
00161 /*
00162  EXTENSION_TABLE_NAME: ROC_DAC_SETTINGS_COL (VIEW: CONF_KEY_ROCDAC_COL_V)
00163  
00164  CONFIG_KEY                                NOT NULL VARCHAR2(80)
00165  KEY_TYPE                                  NOT NULL VARCHAR2(80)
00166  KEY_ALIAS                                 NOT NULL VARCHAR2(80)
00167  VERSION                                            VARCHAR2(40)
00168  KIND_OF_COND                              NOT NULL VARCHAR2(40)
00169  ROC_NAME                                           VARCHAR2(200)
00170  VDD                                       NOT NULL NUMBER(38)
00171  VANA                                      NOT NULL NUMBER(38)
00172  VSF                                       NOT NULL NUMBER(38)
00173  VCOMP                                     NOT NULL NUMBER(38)
00174  VLEAK                                     NOT NULL NUMBER(38)
00175  VRGPR                                     NOT NULL NUMBER(38)
00176  VWLLPR                                    NOT NULL NUMBER(38)
00177  VRGSH                                     NOT NULL NUMBER(38)
00178  VWLLSH                                    NOT NULL NUMBER(38)
00179  VHLDDEL                                   NOT NULL NUMBER(38)
00180  VTRIM                                     NOT NULL NUMBER(38)
00181  VCTHR                                     NOT NULL NUMBER(38)
00182  VIBIAS_BUS                                NOT NULL NUMBER(38)
00183  VIBIAS_SF                                 NOT NULL NUMBER(38)
00184  VOFFSETOP                                 NOT NULL NUMBER(38)
00185  VBIASOP                                   NOT NULL NUMBER(38)
00186  VOFFSETRO                                 NOT NULL NUMBER(38)
00187  VION                                      NOT NULL NUMBER(38)
00188  VIBIAS_PH                                 NOT NULL NUMBER(38)
00189  VIBIAS_DAC                                NOT NULL NUMBER(38)
00190  VIBIAS_ROC                                NOT NULL NUMBER(38)
00191  VICOLOR                                   NOT NULL NUMBER(38)
00192  VNPIX                                     NOT NULL NUMBER(38)
00193  VSUMCOL                                   NOT NULL NUMBER(38)
00194  VCAL                                      NOT NULL NUMBER(38)
00195  CALDEL                                    NOT NULL NUMBER(38)
00196  TEMPRANGE                                 NOT NULL NUMBER(38)
00197  WBC                                       NOT NULL NUMBER(38)
00198  CHIPCONTREG                               NOT NULL NUMBER(38)
00199 */
00200 //   std::multimap<std::string,std::pair<std::string,int > > pDSM;
00201   //  std::stringstream currentRocName;
00202   std::vector< std::string > ins = tableMat[0];
00203   std::string mthn("[PixelDACSettings::PixelDACSettings()] ") ;
00204   std::string dacName;
00205   std::istringstream dbin ;
00206 //   int dacValue;
00207   int skipColumns = 0 ;
00208   std::map<std::string , int > colM;
00209   std::vector<std::string > colNames;
00210   std::map<std::string, std::string> nameTranslation ;
00211 
00212   colNames.push_back("CONFIG_KEY"  );
00213   colNames.push_back("KEY_TYPE"    );
00214   colNames.push_back("KEY_ALIAS"   );
00215   colNames.push_back("VERSION"     );
00216   colNames.push_back("KIND_OF_COND");
00217   colNames.push_back("ROC_NAME"    );
00218   colNames.push_back("VDD"         );
00219   colNames.push_back("VANA"        );
00220   colNames.push_back("VSF"         );
00221   colNames.push_back("VCOMP"       );
00222   colNames.push_back("VLEAK"       );
00223   colNames.push_back("VRGPR"       );
00224   colNames.push_back("VWLLPR"      );
00225   colNames.push_back("VRGSH"       );
00226   colNames.push_back("VWLLSH"      );
00227   colNames.push_back("VHLDDEL"     );
00228   colNames.push_back("VTRIM"       );
00229   colNames.push_back("VCTHR"       );
00230   colNames.push_back("VIBIAS_BUS"  );
00231   colNames.push_back("VIBIAS_SF"   );
00232   colNames.push_back("VOFFSETOP"   );
00233   colNames.push_back("VBIASOP"     );
00234   colNames.push_back("VOFFSETRO"   );
00235   colNames.push_back("VION"        );
00236   colNames.push_back("VIBIAS_PH"   );
00237   colNames.push_back("VIBIAS_DAC"  );
00238   colNames.push_back("VIBIAS_ROC"  );
00239   colNames.push_back("VICOLOR"     );
00240   colNames.push_back("VNPIX"       );
00241   colNames.push_back("VSUMCOL"     );
00242   colNames.push_back("VCAL"        );
00243   colNames.push_back("CALDEL"      );
00244   colNames.push_back("TEMPRANGE"   );
00245   colNames.push_back("WBC"         );
00246   colNames.push_back("CHIPCONTREG" );
00247 
00248   nameTranslation["VDD"]          = k_DACName_Vdd ;
00249   nameTranslation["VANA"]         = k_DACName_Vana;               
00250   nameTranslation["VSF"]          = k_DACName_Vsf;                 
00251   nameTranslation["VCOMP"]        = k_DACName_Vcomp;             
00252   nameTranslation["VLEAK"]        = k_DACName_Vleak;             
00253   nameTranslation["VRGPR"]        = k_DACName_VrgPr;             
00254   nameTranslation["VWLLPR"]       = k_DACName_VwllPr;           
00255   nameTranslation["VRGSH"]        = k_DACName_VrgSh;             
00256   nameTranslation["VWLLSH"]       = k_DACName_VwllSh;           
00257   nameTranslation["VHLDDEL"]      = k_DACName_VHldDel;         
00258   nameTranslation["VTRIM"]        = k_DACName_Vtrim;             
00259   nameTranslation["VCTHR"]        = k_DACName_VcThr;             
00260   nameTranslation["VIBIAS_BUS"]   = k_DACName_VIbias_bus;  
00261   nameTranslation["VIBIAS_SF"]    = k_DACName_VIbias_sf;     
00262   nameTranslation["VOFFSETOP"]    = k_DACName_VOffsetOp;     
00263   nameTranslation["VBIASOP"]      = k_DACName_VbiasOp;         
00264   nameTranslation["VOFFSETRO"]    = k_DACName_VOffsetRO;     
00265   nameTranslation["VION"]         = k_DACName_VIon;               
00266   nameTranslation["VIBIAS_PH"]    = k_DACName_VIbias_PH;     
00267   nameTranslation["VIBIAS_DAC"]   = k_DACName_VIbias_DAC;   
00268   nameTranslation["VIBIAS_ROC"]   = k_DACName_VIbias_roc;   
00269   nameTranslation["VICOLOR"]      = k_DACName_VIColOr;         
00270   nameTranslation["VNPIX"]        = k_DACName_Vnpix;             
00271   nameTranslation["VSUMCOL"]      = k_DACName_VsumCol;         
00272   nameTranslation["VCAL"]         = k_DACName_Vcal;               
00273   nameTranslation["CALDEL"]       = k_DACName_CalDel;           
00274   nameTranslation["TEMPRANGE"]    = k_DACName_TempRange;     
00275   nameTranslation["WBC"]          = k_DACName_WBC;                 
00276   nameTranslation["CHIPCONTREG"]  = k_DACName_ChipContReg; 
00277 
00278   // modified by MR on 25-02-2008 10:00:45
00279   // colM stores the index (referred to tableMat) where the specified dac setting is store!!!
00280   for(unsigned int c = skipColumns ; c < ins.size() ; c++){
00281     for(unsigned int n=0; n<colNames.size(); n++){
00282       if(tableMat[0][c] == colNames[n]){
00283         colM[colNames[n]] = c;
00284         break;
00285       }
00286     }
00287   }//end for
00288   for(unsigned int n=skipColumns; n<colNames.size(); n++){
00289     if(colM.find(colNames[n]) == colM.end()){
00290       std::cerr << "[PixelDACSettings::PixelDACSettings()]\tCouldn't find in the database the column with name " << colNames[n] << std::endl;
00291       assert(0);
00292     }
00293   }
00294 
00295         
00296   dacsettings_.clear();
00297 //   struct timeval  start_time  ;
00298 //   struct timeval  end_time    ;
00299 //   gettimeofday(&start_time, (struct timezone *)0 );
00300   for(unsigned int r = 1 ; r < tableMat.size() ; r++){    //Goes to every row of the Matrix
00301     // currentRocName.str("");
00302     // currentRocName << tableMat[r][colM["NAME"]] ; 
00303     //currentRocName << "FPix_BmI_D" << tableMat[r][colM["HDISK_POSN"]]                 
00304     //     << "_BLD"       << tableMat[r][colM["BLD_POSN"]]                  
00305     //     << "_PNL"       << tableMat[r][colM["PANEL_POSITION"]]            
00306     //     << "_PLQ"       << tableMat[r][colM["PLAQ_POS"]]                 
00307     //     << "_ROC"       << tableMat[r][colM["ROC_POSN"]];                
00308                    
00309     // modified by MR on 25-02-2008 10:04:55
00310     PixelROCName rocid(tableMat[r][colM["ROC_NAME"]]);
00311     PixelROCDACSettings tmp(rocid);
00312     std::ostringstream dacs("") ;
00313     // +6 to get rid of the first 5 columns not pertaining DAC Settings...
00314     for(unsigned int n=skipColumns+6; n<colNames.size(); n++)
00315       {
00316         dacs << nameTranslation[colNames[n]] <<": "<< atoi(tableMat[r][colM[colNames[n]]].c_str()) << std::endl ;
00317         //       dacName  = colNames[n];
00318         //       dacValue = atoi(tableMat[r][colM[colNames[n]]].c_str());
00319         //       pDSM.insert(std::pair<std::string,std::pair<std::string,int> >(tableMat[r][colM["ROC_NAME"]],std::pair<std::string,int>(dacName,dacValue)));
00320         //       std::cout << "On " << tableMat[r][colM["ROC_NAME"]] << " DAC:\t" << dacName << " value:\t" << dacValue<< std::endl ;
00321         //       tmp.setDac(dacName, dacValue) ;
00322       }
00323 //     tmp.setDACs(tmpDACs) ;
00324     dbin.str(dacs.str()) ;
00325     tmp.read(dbin, rocid) ;
00326     dacsettings_.push_back(tmp) ;
00327   }//end for r
00328 //   gettimeofday(&end_time, (struct timezone *)0 );
00329 //   int total_usecs = (end_time.tv_sec - start_time.tv_sec)*1000000 + (end_time.tv_usec - start_time.tv_usec);
00330 //   std::cout << mthn << "Time taken : " << total_usecs / 1000000.  << " secs" << std::endl;
00331   
00332 //   dacsettings_.clear();
00333 //   std::string currentRocName2 = "";
00334 //   for(std::multimap<std::string,std::pair<std::string,int> >::iterator tableMapIt=pDSM.begin(); tableMapIt!= pDSM.end(); tableMapIt++){
00335 //     if(currentRocName2 != tableMapIt->first){
00336 //       std::cout << tableMapIt->first << std::endl;
00337 //       std::cout << tableMapIt->second.first << std::endl;
00338 //       std::cout << tableMapIt->second.second << std::endl;
00339 //       currentRocName2 = tableMapIt->first;
00340 //       PixelROCName rocid(currentRocName2);
00341       
00342 //       // std::cout << "DAC setting ROC id:"<<rocid<<std::endl;
00343   
00344 //       PixelROCDACSettings tmp(rocid);
00345       
00346 //       //       tmp.read(in,rocid);
00347             
00348 //       dacsettings_.push_back(tmp);
00349 //     }//end if
00350 //     dacsettings_[dacsettings_.size()-1].setDac(tableMapIt->second.first,tableMapIt->second.second);
00351 //   }//end for 
00352   
00353   
00354 //   for(unsigned int w = 0 ; w < dacsettings_.size() ; w++)
00355 //     {
00356   
00357 //       PixelROCDACSettings tmp2 = dacsettings_[w];
00358 //       //   std::cout<<tmp2<<std::endl;
00359 //     }   
00360   //  std::cout<<"Number of ROCs in the PixelDACSettings::PixelDACSettings(vector <vector<string> >):"<<dacsettings_.size()<<std::endl; 
00361   //  std::cout << "[PixelDACSettings::PixelDACSettings(std::vector)] before end of constructor" << std::endl ;
00362 }//end PDSMatrix constructor
00363 //end added by Umesh
00364 
00365 PixelROCDACSettings PixelDACSettings::getDACSettings(int ROCId) const {
00366 
00367   return dacsettings_[ROCId];
00368 
00369 }
00370  
00371 PixelROCDACSettings* PixelDACSettings::getDACSettings(PixelROCName name){
00372 
00373   for(unsigned int i=0;i<dacsettings_.size();i++){
00374     if (dacsettings_[i].getROCName()==name) return &(dacsettings_[i]);
00375   }
00376 
00377   return 0;
00378 
00379 }
00380  
00381 void PixelDACSettings::writeBinary(std::string filename) const {
00382 
00383   std::ofstream out(filename.c_str(),std::ios::binary);
00384 
00385   for(unsigned int i=0;i<dacsettings_.size();i++){
00386     dacsettings_[i].writeBinary(out);
00387   }
00388 
00389 }
00390 
00391 
00392 void PixelDACSettings::writeASCII(std::string dir) const {
00393 
00394   std::string mthn = "[PixelDACSettings::writeASCII()]\t\t\t    " ;
00395   PixelModuleName module(dacsettings_[0].getROCName().rocname());
00396 
00397   std::string filename=dir+"/ROC_DAC_module_"+module.modulename()+".dat";
00398   std::cout << __LINE__ << "]\t" << mthn << "Writing to file " << filename << std::endl ; 
00399   std::ofstream out(filename.c_str());
00400   
00401   for(unsigned int i=0;i<dacsettings_.size();i++){
00402     dacsettings_[i].writeASCII(out);
00403   }
00404 
00405 }
00406 
00407 //=============================================================================================
00408 void PixelDACSettings::writeXMLHeader(pos::PixelConfigKey key, 
00409                                       int version, 
00410                                       std::string path, 
00411                                       std::ofstream *outstream,
00412                                       std::ofstream *out1stream,
00413                                       std::ofstream *out2stream) const {
00414   std::string mthn = "[PixelDACSettings::writeXMLHeader()]\t\t\t    " ;
00415   std::stringstream fullPath ;
00416 
00417   fullPath << path << "/Pixel_RocDacSettings_" << PixelTimeFormatter::getmSecTime() << ".xml" ;
00418   std::cout << __LINE__ << "]\t" << mthn << "Writing to: " << fullPath.str()  << std::endl ;
00419 
00420   outstream->open(fullPath.str().c_str()) ;
00421   
00422   *outstream << "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"                               << std::endl ;
00423   *outstream << "<ROOT xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>"                          << std::endl ;
00424   *outstream << ""                                                                                      << std::endl ; 
00425   *outstream << " <!-- " << mthn << "-->"                                                               << std::endl ; 
00426   *outstream << ""                                                                                      << std::endl ; 
00427   *outstream << " <HEADER>"                                                                             << std::endl ;
00428   *outstream << "  <TYPE>"                                                                              << std::endl ;
00429   *outstream << "   <EXTENSION_TABLE_NAME>ROC_DAC_SETTINGS_COL</EXTENSION_TABLE_NAME>"                  << std::endl ;
00430   *outstream << "   <NAME>ROC DAC Settings Col</NAME>"                                                  << std::endl ;
00431   *outstream << "  </TYPE>"                                                                             << std::endl ;
00432   *outstream << "  <RUN>"                                                                               << std::endl ;
00433   *outstream << "   <RUN_TYPE>ROC DAC Settings</RUN_TYPE>"                                              << std::endl ;
00434   *outstream << "   <RUN_NUMBER>1</RUN_NUMBER>"                                                         << std::endl ;
00435   *outstream << "   <RUN_BEGIN_TIMESTAMP>" << PixelTimeFormatter::getTime() << "</RUN_BEGIN_TIMESTAMP>" << std::endl ;
00436   *outstream << "   <LOCATION>CERN P5</LOCATION>"                                                       << std::endl ; 
00437   *outstream << "  </RUN>"                                                                              << std::endl ;
00438   *outstream << " </HEADER>"                                                                            << std::endl ;
00439   *outstream << ""                                                                                      << std::endl ;
00440   *outstream << " <DATA_SET>"                                                                           << std::endl ;
00441   *outstream << "  <VERSION>"             << version      << "</VERSION>"                               << std::endl ;
00442   *outstream << "  <COMMENT_DESCRIPTION>" << getComment() << "</COMMENT_DESCRIPTION>"                   << std::endl ;
00443   *outstream << "  <CREATED_BY_USER>"     << getAuthor()  << "</CREATED_BY_USER>"                       << std::endl ;
00444   *outstream << " "                                                                                     << std::endl ;
00445   *outstream << "  <PART>"                                                                              << std::endl ;
00446   *outstream << "   <NAME_LABEL>CMS-PIXEL-ROOT</NAME_LABEL>"                                            << std::endl ;
00447   *outstream << "   <KIND_OF_PART>Detector ROOT</KIND_OF_PART>"                                         << std::endl ;
00448   *outstream << "  </PART>"                                                                             << std::endl ;
00449   *outstream << " "                                                                                     << std::endl ;
00450 
00451   std::cout << __LINE__ << "]\t" << mthn << "Header written" << std::endl ;
00452 }
00453 
00454 //=============================================================================================
00455 void PixelDACSettings::writeXML( std::ofstream *outstream,
00456                                  std::ofstream *out1stream,
00457                                  std::ofstream *out2stream) const {
00458   std::string mthn = "[PixelDACSettings::writeXML()]\t\t\t    " ;
00459 
00460   for(unsigned int i=0;i<dacsettings_.size();i++){
00461     dacsettings_[i].writeXML(outstream);
00462   }
00463 }
00464 
00465 //=============================================================================================
00466 void PixelDACSettings::writeXMLTrailer(std::ofstream *outstream,
00467                                        std::ofstream *out1stream,
00468                                        std::ofstream *out2stream) const {
00469   std::string mthn = "[PixelDACSettings::writeXMLTrailer()]\t\t\t    " ;
00470 
00471   *outstream << " </DATA_SET>"                                                                         << std::endl ;
00472   *outstream << "</ROOT>"                                                                              << std::endl ;
00473 
00474   outstream->close() ;
00475   std::cout << __LINE__ << "]\t" << mthn << "Data written"                                             << std::endl ;
00476 }
00477 
00478 /* O B S O L E T E -----
00479 
00480 //=============================================================================================
00481 void PixelDACSettings::writeXML(pos::PixelConfigKey key, int version, std::string path) const {
00482   std::string mthn = "[PixelDACSettings::writeXML()]\t\t\t    " ;
00483   std::stringstream fullPath ;
00484 
00485   PixelModuleName module(dacsettings_[0].getROCName().rocname());
00486   fullPath << path << "/dacsettings_" << module.modulename() << ".xml" ;
00487   std::cout << mthn << "Writing to: |" << fullPath.str()  << "|" << std::endl ;
00488   
00489 
00490   std::ofstream outstream(fullPath.str().c_str()) ;
00491   
00492   out << "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"                               << std::endl ;
00493   out << "<ROOT xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>"                          << std::endl ;
00494   out << " <HEADER>"                                                                             << std::endl ;
00495   out << "  <TYPE>"                                                                              << std::endl ;
00496   out << "   <EXTENSION_TABLE_NAME>ROC_DAC_SETTINGS_COL</EXTENSION_TABLE_NAME>"                  << std::endl ;
00497   out << "   <NAME>ROC DAC Settings Col</NAME>"                                                  << std::endl ;
00498   out << "  </TYPE>"                                                                             << std::endl ;
00499   out << "  <RUN>"                                                                               << std::endl ;
00500   out << "   <RUN_TYPE>test</RUN_TYPE>"                                                          << std::endl ;
00501   out << "   <RUN_NUMBER>1</RUN_NUMBER>"                                                         << std::endl ;
00502   out << "   <RUN_BEGIN_TIMESTAMP>" << PixelTimeFormatter::getTime() << "</RUN_BEGIN_TIMESTAMP>" << std::endl ;
00503   out << "   <COMMENT_DESCRIPTION>Test of DAC Settings xml</COMMENT_DESCRIPTION>"                << std::endl ;
00504   out << "   <LOCATION>CERN TAC</LOCATION>"                                                      << std::endl ;
00505   out << "   <CREATED_BY_USER>Dario Menasce</CREATED_BY_USER>"                                   << std::endl ;
00506   out << "  </RUN>"                                                                              << std::endl ;
00507   out << " </HEADER>"                                                                            << std::endl ;
00508   out << ""                                                                                      << std::endl ;
00509   out << " <DATA_SET>"                                                                           << std::endl ;
00510   out << "  <VERSION>" << version << "</VERSION>"                                                << std::endl ;
00511   out << " "                                                                                     << std::endl ;
00512   out << "  <PART>"                                                                              << std::endl ;
00513   out << "   <NAME_LABEL>CMS-PIXEL-ROOT</NAME_LABEL>"                                            << std::endl ;
00514   out << "   <KIND_OF_PART>Detector ROOT</KIND_OF_PART>"                                         << std::endl ;
00515   out << "  </PART>"                                                                             << std::endl ;
00516   out << " "                                                                                     << std::endl ;
00517 
00518   for(unsigned int i=0;i<dacsettings_.size();i++){
00519 //    dacsettings_[i].writeXML(out, key, version, path);
00520   }
00521 
00522   out << " </DATA_SET>"                                                                          << std::endl ;
00523   out << "</ROOT>"                                                                               << std::endl ;
00524 
00525   out.close() ;
00526   std::cout << mthn << "Data written"                                                            << std::endl ;
00527 }
00528 */
00529 
00530 //=============================================================================================
00531 void PixelDACSettings::generateConfiguration(PixelFECConfigInterface* pixelFEC,
00532                                              PixelNameTranslation* trans, PixelDetectorConfig* detconfig, bool HVon) const{
00533 
00534   bool bufferData=true; 
00535 
00536   std::vector<unsigned int> dacs;
00537 
00538   //pixelFEC->fecDebug(1);  //FIXME someday maybe don't want to take the time
00539 
00540   for(unsigned int i=0;i<dacsettings_.size();i++){
00541 
00542     bool disableRoc = rocIsDisabled(detconfig, dacsettings_[i].getROCName());
00543 
00544     dacsettings_[i].getDACs(dacs);
00545 
00546     PixelHdwAddress theROC=*(trans->getHdwAddress(dacsettings_[i].getROCName()));
00547 
00548     //Need to set readout speed (40MHz) and Vcal range (0-1800 mV) and enable the chip
00549 
00550     int controlreg=dacsettings_[i].getControlRegister();
00551     //std::cout << "[PixelDACSettings::generateConfiguration] ROC control reg to be set to: " <<  controlreg <<std::endl;
00552 
00553     pixelFEC->progdac(theROC.mfec(),
00554                       theROC.mfecchannel(),
00555                       theROC.hubaddress(),
00556                       theROC.portaddress(),
00557                       theROC.rocid(),
00558                       0xfd,
00559                       controlreg,
00560                       bufferData);
00561 
00562     //    std::cout<<"ROC="<<dacsettings_[i].getROCName()<<" ; VcThr set to "<<dacs[11]<<std::flush; //for debugging
00563     if (!HVon || disableRoc)    dacs[11]=0; //set Vcthr DAC to 0 (Vcthr is DAC 12=11+1)
00564     //    std::cout<<" ; setting VcThr to "<<dacs[11]<<std::endl; //for debugging
00565     pixelFEC->setAllDAC(theROC,dacs,bufferData);
00566 
00567     // start with no pixels on for calibration
00568     pixelFEC->clrcal(theROC.mfec(), 
00569                      theROC.mfecchannel(), 
00570                      theROC.hubaddress(), 
00571                      theROC.portaddress(),  
00572                      theROC.rocid(),
00573                      bufferData);
00574 
00575     // enable all the double columns
00576     for(int dcol=0;dcol<26;dcol++){
00577       pixelFEC->dcolenable(theROC.mfec(),
00578                            theROC.mfecchannel(),
00579                            theROC.hubaddress(),
00580                            theROC.portaddress(),
00581                            theROC.rocid(),
00582                            dcol,
00583                            1,
00584                            bufferData);
00585     }
00586 
00587     if (!HVon || disableRoc) { //HV off
00588       //      std::cout << "[PixelDACSettings::generateConfiguration] HV off! ROC control reg to be set to: " <<  (controlreg|0x2) <<std::endl;
00589       pixelFEC->progdac(theROC.mfec(),
00590                         theROC.mfecchannel(),
00591                         theROC.hubaddress(),
00592                         theROC.portaddress(),
00593                         theROC.rocid(),
00594                         0xfd,
00595                         controlreg | 0x2, //=010 in binary. should disable the chip
00596                         bufferData);
00597     }
00598   }
00599 
00600   if (bufferData) {
00601     pixelFEC->qbufsend();
00602   }
00603 
00604 }
00605 
00606 void PixelDACSettings::setVcthrDisable(PixelFECConfigInterface* pixelFEC, PixelNameTranslation* trans ) const {
00607   //the point here is to set Vcthr to 0
00608   //then disable the ROCs
00609 
00610   //note -- no need to look at the detconfig here, because we're going to disable everything no matter what
00611 
00612   bool bufferData=true;
00613 
00614   std::vector<unsigned int> dacs;
00615 
00616   for(unsigned int i=0;i<dacsettings_.size();i++){ //loop over the ROCs
00617 
00618     dacsettings_[i].getDACs(dacs);
00619     int controlreg=dacsettings_[i].getControlRegister();
00620 
00621     PixelHdwAddress theROC=*(trans->getHdwAddress(dacsettings_[i].getROCName()));
00622 
00623     //std::cout<<"disabling ROC="<<dacsettings_[i].getROCName()<<std::endl;
00624     pixelFEC->progdac(theROC.mfec(),
00625                       theROC.mfecchannel(),
00626                       theROC.hubaddress(),
00627                       theROC.portaddress(),
00628                       theROC.rocid(),
00629                       12, //12 == Vcthr
00630                       0, //set Vcthr to 0
00631                       bufferData);
00632 
00633     //this should disable the roc
00634     pixelFEC->progdac(theROC.mfec(),
00635                       theROC.mfecchannel(),
00636                       theROC.hubaddress(),
00637                       theROC.portaddress(),
00638                       theROC.rocid(),
00639                       0xfd,
00640                       controlreg | 0x2,
00641                       bufferData);
00642 
00643   }
00644 
00645   if (bufferData) { //just copying the way it was done in the existing method
00646     pixelFEC->qbufsend();
00647   }
00648 }
00649 
00650 void PixelDACSettings::setVcthrEnable(PixelFECConfigInterface* pixelFEC, PixelNameTranslation* trans, PixelDetectorConfig* detconfig) const {
00651   //the point here is to set Vcthr to the nominal values
00652   //then enable the ROCs
00653 
00654   bool bufferData=true;
00655 
00656   std::vector<unsigned int> dacs;
00657 
00658   for(unsigned int i=0;i<dacsettings_.size();i++){ //loop over the ROCs
00659 
00660     bool disableRoc = rocIsDisabled(detconfig, dacsettings_[i].getROCName()); //don't enable ROCs that are disabled in the detconfig
00661 
00662     dacsettings_[i].getDACs(dacs);
00663     int controlreg=dacsettings_[i].getControlRegister();
00664 
00665     PixelHdwAddress theROC=*(trans->getHdwAddress(dacsettings_[i].getROCName()));
00666 
00667     //std::cout<<"ROC="<<dacsettings_[i].getROCName()<<" ; VcThr set to "<<dacs[11]
00668     //       << " ; ROC control reg to be set to: " <<  controlreg <<std::endl;
00669 
00670     if (!disableRoc) {
00671       pixelFEC->progdac(theROC.mfec(),
00672                         theROC.mfecchannel(),
00673                         theROC.hubaddress(),
00674                         theROC.portaddress(),
00675                         theROC.rocid(),
00676                         12, //12 == Vcthr
00677                         dacs[11],
00678                         bufferData);
00679       
00680       //enable the roc (assuming controlreg was set for the roc to be enabled)
00681       
00682       pixelFEC->progdac(theROC.mfec(),
00683                         theROC.mfecchannel(),
00684                         theROC.hubaddress(),
00685                         theROC.portaddress(),
00686                         theROC.rocid(),
00687                         0xfd,
00688                         controlreg,
00689                         bufferData);
00690       
00691     }
00692   }
00693 
00694   if (bufferData) {
00695     pixelFEC->qbufsend();
00696   }
00697 
00698 }
00699 
00700 bool PixelDACSettings::rocIsDisabled(const PixelDetectorConfig* detconfig, const PixelROCName rocname) const {
00701 
00702   const std::map<PixelROCName, PixelROCStatus> & roclist=detconfig->getROCsList();
00703   const std::map<PixelROCName, PixelROCStatus>::const_iterator iroc = roclist.find(rocname);
00704   assert(iroc != roclist.end());    // the roc name should always be found
00705   PixelROCStatus thisROCstatus = iroc->second;
00706 
00707   return thisROCstatus.get(PixelROCStatus::noAnalogSignal);
00708 
00709 }
00710 
00711 std::ostream& operator<<(std::ostream& s, const PixelDACSettings& dacs){
00712 
00713   s << dacs.getDACSettings(0) <<std::endl; 
00714 
00715   return s;
00716 
00717 }
00718