00001
00002
00003
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
00038 throw std::runtime_error("Failed to open file "+filename);
00039 }
00040 else {
00041
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
00057
00058 PixelROCDACSettings tmp;
00059
00060 tmp.read(in,rocid);
00061
00062
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
00093 for(int i=0;i< nchar; i++){
00094 char c;
00095 in >>c;
00096 s1.push_back(c);
00097 }
00098
00099
00100
00101 dacsettings_.clear();
00102
00103
00104 while (!in.eof()){
00105
00106
00107
00108 PixelROCName rocid(s1);
00109
00110
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
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
00143
00144
00145 }
00146
00147 PixelDACSettings::PixelDACSettings(PixelROCDACSettings &rocname):
00148 PixelConfigBase("","","") {
00149 dacsettings_.push_back(rocname) ;
00150 }
00151
00152
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
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202 std::vector< std::string > ins = tableMat[0];
00203 std::string mthn("[PixelDACSettings::PixelDACSettings()] ") ;
00204 std::string dacName;
00205 std::istringstream dbin ;
00206
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
00279
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 }
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
00298
00299
00300 for(unsigned int r = 1 ; r < tableMat.size() ; r++){
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310 PixelROCName rocid(tableMat[r][colM["ROC_NAME"]]);
00311 PixelROCDACSettings tmp(rocid);
00312 std::ostringstream dacs("") ;
00313
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
00318
00319
00320
00321
00322 }
00323
00324 dbin.str(dacs.str()) ;
00325 tmp.read(dbin, rocid) ;
00326 dacsettings_.push_back(tmp) ;
00327 }
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362 }
00363
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
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
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
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
00549
00550 int controlreg=dacsettings_[i].getControlRegister();
00551
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
00563 if (!HVon || disableRoc) dacs[11]=0;
00564
00565 pixelFEC->setAllDAC(theROC,dacs,bufferData);
00566
00567
00568 pixelFEC->clrcal(theROC.mfec(),
00569 theROC.mfecchannel(),
00570 theROC.hubaddress(),
00571 theROC.portaddress(),
00572 theROC.rocid(),
00573 bufferData);
00574
00575
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) {
00588
00589 pixelFEC->progdac(theROC.mfec(),
00590 theROC.mfecchannel(),
00591 theROC.hubaddress(),
00592 theROC.portaddress(),
00593 theROC.rocid(),
00594 0xfd,
00595 controlreg | 0x2,
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
00608
00609
00610
00611
00612 bool bufferData=true;
00613
00614 std::vector<unsigned int> dacs;
00615
00616 for(unsigned int i=0;i<dacsettings_.size();i++){
00617
00618 dacsettings_[i].getDACs(dacs);
00619 int controlreg=dacsettings_[i].getControlRegister();
00620
00621 PixelHdwAddress theROC=*(trans->getHdwAddress(dacsettings_[i].getROCName()));
00622
00623
00624 pixelFEC->progdac(theROC.mfec(),
00625 theROC.mfecchannel(),
00626 theROC.hubaddress(),
00627 theROC.portaddress(),
00628 theROC.rocid(),
00629 12,
00630 0,
00631 bufferData);
00632
00633
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) {
00646 pixelFEC->qbufsend();
00647 }
00648 }
00649
00650 void PixelDACSettings::setVcthrEnable(PixelFECConfigInterface* pixelFEC, PixelNameTranslation* trans, PixelDetectorConfig* detconfig) const {
00651
00652
00653
00654 bool bufferData=true;
00655
00656 std::vector<unsigned int> dacs;
00657
00658 for(unsigned int i=0;i<dacsettings_.size();i++){
00659
00660 bool disableRoc = rocIsDisabled(detconfig, dacsettings_[i].getROCName());
00661
00662 dacsettings_[i].getDACs(dacs);
00663 int controlreg=dacsettings_[i].getControlRegister();
00664
00665 PixelHdwAddress theROC=*(trans->getHdwAddress(dacsettings_[i].getROCName()));
00666
00667
00668
00669
00670 if (!disableRoc) {
00671 pixelFEC->progdac(theROC.mfec(),
00672 theROC.mfecchannel(),
00673 theROC.hubaddress(),
00674 theROC.portaddress(),
00675 theROC.rocid(),
00676 12,
00677 dacs[11],
00678 bufferData);
00679
00680
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());
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