Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
//
// This class provide a base class for the
// pixel ROC DAC data for the pixel FEC configuration
//
//
//
//

#include "CalibFormats/SiPixelObjects/interface/PixelDACSettings.h"
#include "CalibFormats/SiPixelObjects/interface/PixelROCDACSettings.h"
#include "CalibFormats/SiPixelObjects/interface/PixelDACNames.h"
#include "CalibFormats/SiPixelObjects/interface/PixelTimeFormatter.h"
#include <fstream>
#include <iostream>
#include <ios>
#include <cassert>
#include <stdexcept>
#include <map>
#include <sstream>
#include <sys/time.h>
#include <cstdlib>

using namespace pos;

namespace {
  const bool readTemperatures = false;
  //const bool readTemperatures = true;
  //const int temperatureReg = 0x9; // hardwire to fixed reference voltage 0x8 + 0x1
  const int temperatureReg = 0x1;  // hardwire to the usefull range, change to range 1, Marco's request, 25/10/11
}  // namespace

PixelDACSettings::PixelDACSettings(std::string filename) : PixelConfigBase("", "", "") {
  std::string mthn = "[PixelDACSettings::PixelDACSettings()]\t\t\t    ";

  if (filename[filename.size() - 1] == 't') {
    std::ifstream in(filename.c_str());

    if (!in.good()) {
      std::cout << __LINE__ << "]\t" << mthn << "Could not open: " << filename << std::endl;
      // assert(0); //in case of failure, we don't want POS to die here
      throw std::runtime_error("Failed to open file " + filename);
    } else {
      // std::cout << "Opened:"<<filename<<std::endl;
    }

    dacsettings_.clear();

    std::string tag;

    in >> tag;

    while (!in.eof()) {
      PixelROCName rocid(in);

      //	    std::cout << "[PixelDACSettings::PixelDACSettings()] DAC setting ROC id:"<<rocid<<std::endl;

      PixelROCDACSettings tmp;

      tmp.read(in, rocid);

      //	    std::cout << "[PixelDACSettings::PixelDACSettings()] DACSetting: " << std::endl << tmp << std::endl ;
      dacsettings_.push_back(tmp);

      in >> tag;

      assert(dacsettings_.size() < 100);
    }

    in.close();

  } else {
    std::ifstream in(filename.c_str(), std::ios::binary);

    if (!in.good()) {
      std::cout << __LINE__ << "]\t" << mthn << "Could not open: " << filename << std::endl;
      assert(0);
    } else {
      std::cout << __LINE__ << "]\t" << mthn << "Opened: " << filename << std::endl;
    }

    char nchar;

    in.read(&nchar, 1);

    std::string s1;

    //wrote these lines of code without ref. needs to be fixed
    for (int i = 0; i < nchar; i++) {
      char c;
      in >> c;
      s1.push_back(c);
    }

    //std::cout << __LINE__ << "]\t" << mthn << "READ ROC name: " << s1 << std::endl;

    dacsettings_.clear();

    while (!in.eof()) {
      //std::cout << __LINE__ << "]\t" << mthn << "read s1   : " << s1    << std::endl;

      PixelROCName rocid(s1);

      //std::cout << __LINE__ << "]\t" << mthn << "read rocid: " << rocid << std::endl;

      PixelROCDACSettings tmp;

      tmp.readBinary(in, rocid);

      dacsettings_.push_back(tmp);

      in.read(&nchar, 1);

      s1.clear();

      if (in.eof())
        continue;

      //wrote these lines of code without ref. needs to be fixed
      for (int i = 0; i < nchar; i++) {
        char c;
        in >> c;
        s1.push_back(c);
      }
    }

    in.close();
  }

  //std::cout << __LINE__ << "]\t" << mthn << "Read dac settings for "<<dacsettings_.size()<<" ROCs"<<std::endl;
}
// modified by MR on 10-01-2008 14:48:19
PixelDACSettings::PixelDACSettings(PixelROCDACSettings& rocname) : PixelConfigBase("", "", "") {
  dacsettings_.push_back(rocname);
}

// modified by MR on 24-01-2008 14:27:35a
void PixelDACSettings::addROC(PixelROCDACSettings& rocname) { dacsettings_.push_back(rocname); }

PixelDACSettings::PixelDACSettings(std::vector<std::vector<std::string> >& tableMat) : PixelConfigBase("", "", "") {
  /*
 EXTENSION_TABLE_NAME: ROC_DAC_SETTINGS_COL (VIEW: CONF_KEY_ROCDAC_COL_V)
 
 CONFIG_KEY				   NOT NULL VARCHAR2(80)
 KEY_TYPE				   NOT NULL VARCHAR2(80)
 KEY_ALIAS				   NOT NULL VARCHAR2(80)
 VERSION					    VARCHAR2(40)
 KIND_OF_COND				   NOT NULL VARCHAR2(40)
 ROC_NAME					    VARCHAR2(200)
 VDD					   NOT NULL NUMBER(38)
 VANA					   NOT NULL NUMBER(38)
 VSF					   NOT NULL NUMBER(38)
 VCOMP					   NOT NULL NUMBER(38)
 VLEAK					   NOT NULL NUMBER(38)
 VRGPR					   NOT NULL NUMBER(38)
 VWLLPR 				   NOT NULL NUMBER(38)
 VRGSH					   NOT NULL NUMBER(38)
 VWLLSH 				   NOT NULL NUMBER(38)
 VHLDDEL				   NOT NULL NUMBER(38)
 VTRIM					   NOT NULL NUMBER(38)
 VCTHR					   NOT NULL NUMBER(38)
 VIBIAS_BUS				   NOT NULL NUMBER(38)
 VIBIAS_SF				   NOT NULL NUMBER(38)
 VOFFSETOP				   NOT NULL NUMBER(38)
 VBIASOP				   NOT NULL NUMBER(38)
 VOFFSETRO				   NOT NULL NUMBER(38)
 VION					   NOT NULL NUMBER(38)
 VIBIAS_PH				   NOT NULL NUMBER(38)
 VIBIAS_DAC				   NOT NULL NUMBER(38)
 VIBIAS_ROC				   NOT NULL NUMBER(38)
 VICOLOR				   NOT NULL NUMBER(38)
 VNPIX					   NOT NULL NUMBER(38)
 VSUMCOL				   NOT NULL NUMBER(38)
 VCAL					   NOT NULL NUMBER(38)
 CALDEL 				   NOT NULL NUMBER(38)
 TEMPRANGE				   NOT NULL NUMBER(38)
 WBC					   NOT NULL NUMBER(38)
 CHIPCONTREG				   NOT NULL NUMBER(38)
*/
  //   std::multimap<std::string,std::pair<std::string,int > > pDSM;
  //  std::stringstream currentRocName;
  std::vector<std::string> ins = tableMat[0];
  std::string mthn("[PixelDACSettings::PixelDACSettings()] ");
  std::string dacName;
  std::istringstream dbin;
  //   int dacValue;
  int skipColumns = 0;
  std::map<std::string, int> colM;
  std::vector<std::string> colNames;
  std::map<std::string, std::string> nameTranslation;

  colNames.push_back("CONFIG_KEY");
  colNames.push_back("KEY_TYPE");
  colNames.push_back("KEY_ALIAS");
  colNames.push_back("VERSION");
  colNames.push_back("KIND_OF_COND");
  colNames.push_back("ROC_NAME");
  colNames.push_back("VDD");
  colNames.push_back("VANA");
  colNames.push_back("VSF");
  colNames.push_back("VCOMP");
  colNames.push_back("VLEAK");
  colNames.push_back("VRGPR");
  colNames.push_back("VWLLPR");
  colNames.push_back("VRGSH");
  colNames.push_back("VWLLSH");
  colNames.push_back("VHLDDEL");
  colNames.push_back("VTRIM");
  colNames.push_back("VCTHR");
  colNames.push_back("VIBIAS_BUS");
  colNames.push_back("VIBIAS_SF");
  colNames.push_back("VOFFSETOP");
  colNames.push_back("VBIASOP");
  colNames.push_back("VOFFSETRO");
  colNames.push_back("VION");
  colNames.push_back("VIBIAS_PH");
  colNames.push_back("VIBIAS_DAC");
  colNames.push_back("VIBIAS_ROC");
  colNames.push_back("VICOLOR");
  colNames.push_back("VNPIX");
  colNames.push_back("VSUMCOL");
  colNames.push_back("VCAL");
  colNames.push_back("CALDEL");
  colNames.push_back("TEMPRANGE");
  colNames.push_back("WBC");
  colNames.push_back("CHIPCONTREG");

  nameTranslation["VDD"] = k_DACName_Vdd;
  nameTranslation["VANA"] = k_DACName_Vana;
  nameTranslation["VSF"] = k_DACName_Vsf;
  nameTranslation["VCOMP"] = k_DACName_Vcomp;
  nameTranslation["VLEAK"] = k_DACName_Vleak;
  nameTranslation["VRGPR"] = k_DACName_VrgPr;
  nameTranslation["VWLLPR"] = k_DACName_VwllPr;
  nameTranslation["VRGSH"] = k_DACName_VrgSh;
  nameTranslation["VWLLSH"] = k_DACName_VwllSh;
  nameTranslation["VHLDDEL"] = k_DACName_VHldDel;
  nameTranslation["VTRIM"] = k_DACName_Vtrim;
  nameTranslation["VCTHR"] = k_DACName_VcThr;
  nameTranslation["VIBIAS_BUS"] = k_DACName_VIbias_bus;
  nameTranslation["VIBIAS_SF"] = k_DACName_VIbias_sf;
  nameTranslation["VOFFSETOP"] = k_DACName_VOffsetOp;
  nameTranslation["VBIASOP"] = k_DACName_VbiasOp;
  nameTranslation["VOFFSETRO"] = k_DACName_VOffsetRO;
  nameTranslation["VION"] = k_DACName_VIon;
  nameTranslation["VIBIAS_PH"] = k_DACName_VIbias_PH;
  nameTranslation["VIBIAS_DAC"] = k_DACName_VIbias_DAC;
  nameTranslation["VIBIAS_ROC"] = k_DACName_VIbias_roc;
  nameTranslation["VICOLOR"] = k_DACName_VIColOr;
  nameTranslation["VNPIX"] = k_DACName_Vnpix;
  nameTranslation["VSUMCOL"] = k_DACName_VsumCol;
  nameTranslation["VCAL"] = k_DACName_Vcal;
  nameTranslation["CALDEL"] = k_DACName_CalDel;
  nameTranslation["TEMPRANGE"] = k_DACName_TempRange;
  nameTranslation["WBC"] = k_DACName_WBC;
  nameTranslation["CHIPCONTREG"] = k_DACName_ChipContReg;

  // modified by MR on 25-02-2008 10:00:45
  // colM stores the index (referred to tableMat) where the specified dac setting is store!!!
  for (unsigned int c = skipColumns; c < ins.size(); c++) {
    for (unsigned int n = 0; n < colNames.size(); n++) {
      if (tableMat[0][c] == colNames[n]) {
        colM[colNames[n]] = c;
        break;
      }
    }
  }  //end for
  for (unsigned int n = skipColumns; n < colNames.size(); n++) {
    if (colM.find(colNames[n]) == colM.end()) {
      std::cerr << "[PixelDACSettings::PixelDACSettings()]\tCouldn't find in the database the column with name "
                << colNames[n] << std::endl;
      assert(0);
    }
  }

  dacsettings_.clear();
  //   struct timeval  start_time  ;
  //   struct timeval  end_time    ;
  //   gettimeofday(&start_time, (struct timezone *)0 );
  for (unsigned int r = 1; r < tableMat.size(); r++) {  //Goes to every row of the Matrix
    // currentRocName.str("");
    // currentRocName << tableMat[r][colM["NAME"]] ;
    //currentRocName << "FPix_BmI_D" << tableMat[r][colM["HDISK_POSN"]]
    //	   << "_BLD"       << tableMat[r][colM["BLD_POSN"]]
    //	   << "_PNL"       << tableMat[r][colM["PANEL_POSITION"]]
    //	   << "_PLQ"       << tableMat[r][colM["PLAQ_POS"]]
    //	   << "_ROC"       << tableMat[r][colM["ROC_POSN"]];

    // modified by MR on 25-02-2008 10:04:55
    PixelROCName rocid(tableMat[r][colM["ROC_NAME"]]);
    PixelROCDACSettings tmp(rocid);
    std::ostringstream dacs("");
    // +6 to get rid of the first 5 columns not pertaining DAC Settings...
    for (unsigned int n = skipColumns + 6; n < colNames.size(); n++) {
      dacs << nameTranslation[colNames[n]] << ": " << atoi(tableMat[r][colM[colNames[n]]].c_str()) << std::endl;
      //       dacName  = colNames[n];
      //       dacValue = atoi(tableMat[r][colM[colNames[n]]].c_str());
      //       pDSM.insert(std::pair<std::string,std::pair<std::string,int> >(tableMat[r][colM["ROC_NAME"]],std::pair<std::string,int>(dacName,dacValue)));
      //       std::cout << "On " << tableMat[r][colM["ROC_NAME"]] << " DAC:\t" << dacName << " value:\t" << dacValue<< std::endl ;
      //       tmp.setDac(dacName, dacValue) ;
    }
    //     tmp.setDACs(tmpDACs) ;
    dbin.str(dacs.str());
    tmp.read(dbin, rocid);
    dacsettings_.push_back(tmp);
  }  //end for r
  //   gettimeofday(&end_time, (struct timezone *)0 );
  //   int total_usecs = (end_time.tv_sec - start_time.tv_sec)*1000000 + (end_time.tv_usec - start_time.tv_usec);
  //   std::cout << mthn << "Time taken : " << total_usecs / 1000000.  << " secs" << std::endl;

  //   dacsettings_.clear();
  //   std::string currentRocName2 = "";
  //   for(std::multimap<std::string,std::pair<std::string,int> >::iterator tableMapIt=pDSM.begin(); tableMapIt!= pDSM.end(); tableMapIt++){
  //     if(currentRocName2 != tableMapIt->first){
  //       std::cout << tableMapIt->first << std::endl;
  //       std::cout << tableMapIt->second.first << std::endl;
  //       std::cout << tableMapIt->second.second << std::endl;
  //       currentRocName2 = tableMapIt->first;
  //       PixelROCName rocid(currentRocName2);

  //       // std::cout << "DAC setting ROC id:"<<rocid<<std::endl;

  //       PixelROCDACSettings tmp(rocid);

  //       //       tmp.read(in,rocid);

  //       dacsettings_.push_back(tmp);
  //     }//end if
  //     dacsettings_[dacsettings_.size()-1].setDac(tableMapIt->second.first,tableMapIt->second.second);
  //   }//end for

  //   for(unsigned int w = 0 ; w < dacsettings_.size() ; w++)
  //     {

  //       PixelROCDACSettings tmp2 = dacsettings_[w];
  //       //   std::cout<<tmp2<<std::endl;
  //     }
  //  std::cout<<"Number of ROCs in the PixelDACSettings::PixelDACSettings(vector <vector<string> >):"<<dacsettings_.size()<<std::endl;
  //  std::cout << "[PixelDACSettings::PixelDACSettings(std::vector)] before end of constructor" << std::endl ;
}  //end PDSMatrix constructor
//end added by Umesh

PixelROCDACSettings PixelDACSettings::getDACSettings(int ROCId) const { return dacsettings_[ROCId]; }

PixelROCDACSettings* PixelDACSettings::getDACSettings(PixelROCName name) {
  for (unsigned int i = 0; i < dacsettings_.size(); i++) {
    if (dacsettings_[i].getROCName() == name)
      return &(dacsettings_[i]);
  }

  return nullptr;
}

void PixelDACSettings::writeBinary(std::string filename) const {
  std::ofstream out(filename.c_str(), std::ios::binary);

  for (unsigned int i = 0; i < dacsettings_.size(); i++) {
    dacsettings_[i].writeBinary(out);
  }
}

void PixelDACSettings::writeASCII(std::string dir) const {
  std::string mthn = "[PixelDACSettings::writeASCII()]\t\t\t    ";
  PixelModuleName module(dacsettings_[0].getROCName().rocname());

  std::string filename = dir + "/ROC_DAC_module_" + module.modulename() + ".dat";
  std::cout << __LINE__ << "]\t" << mthn << "Writing to file " << filename << std::endl;
  std::ofstream out(filename.c_str());

  for (unsigned int i = 0; i < dacsettings_.size(); i++) {
    dacsettings_[i].writeASCII(out);
  }
}

//=============================================================================================
void PixelDACSettings::writeXMLHeader(pos::PixelConfigKey key,
                                      int version,
                                      std::string path,
                                      std::ofstream* outstream,
                                      std::ofstream* out1stream,
                                      std::ofstream* out2stream) const {
  std::string mthn = "[PixelDACSettings::writeXMLHeader()]\t\t\t    ";
  std::stringstream fullPath;

  fullPath << path << "/Pixel_RocDacSettings_" << PixelTimeFormatter::getmSecTime() << ".xml";
  std::cout << __LINE__ << "]\t" << mthn << "Writing to: " << fullPath.str() << std::endl;

  outstream->open(fullPath.str().c_str());

  *outstream << "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>" << std::endl;
  *outstream << "<ROOT xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>" << std::endl;
  *outstream << "" << std::endl;
  *outstream << " <!-- " << mthn << "-->" << std::endl;
  *outstream << "" << std::endl;
  *outstream << " <HEADER>" << std::endl;
  *outstream << "  <TYPE>" << std::endl;
  *outstream << "   <EXTENSION_TABLE_NAME>ROC_DAC_SETTINGS_COL</EXTENSION_TABLE_NAME>" << std::endl;
  *outstream << "   <NAME>ROC DAC Settings Col</NAME>" << std::endl;
  *outstream << "  </TYPE>" << std::endl;
  *outstream << "  <RUN>" << std::endl;
  *outstream << "   <RUN_TYPE>ROC DAC Settings</RUN_TYPE>" << std::endl;
  *outstream << "   <RUN_NUMBER>1</RUN_NUMBER>" << std::endl;
  *outstream << "   <RUN_BEGIN_TIMESTAMP>" << PixelTimeFormatter::getTime() << "</RUN_BEGIN_TIMESTAMP>" << std::endl;
  *outstream << "   <LOCATION>CERN P5</LOCATION>" << std::endl;
  *outstream << "  </RUN>" << std::endl;
  *outstream << " </HEADER>" << std::endl;
  *outstream << "" << std::endl;
  *outstream << " <DATA_SET>" << std::endl;
  *outstream << "  <VERSION>" << version << "</VERSION>" << std::endl;
  *outstream << "  <COMMENT_DESCRIPTION>" << getComment() << "</COMMENT_DESCRIPTION>" << std::endl;
  *outstream << "  <CREATED_BY_USER>" << getAuthor() << "</CREATED_BY_USER>" << std::endl;
  *outstream << " " << std::endl;
  *outstream << "  <PART>" << std::endl;
  *outstream << "   <NAME_LABEL>CMS-PIXEL-ROOT</NAME_LABEL>" << std::endl;
  *outstream << "   <KIND_OF_PART>Detector ROOT</KIND_OF_PART>" << std::endl;
  *outstream << "  </PART>" << std::endl;
  *outstream << " " << std::endl;

  std::cout << __LINE__ << "]\t" << mthn << "Header written" << std::endl;
}

//=============================================================================================
void PixelDACSettings::writeXML(std::ofstream* outstream, std::ofstream* out1stream, std::ofstream* out2stream) const {
  std::string mthn = "[PixelDACSettings::writeXML()]\t\t\t    ";

  for (unsigned int i = 0; i < dacsettings_.size(); i++) {
    dacsettings_[i].writeXML(outstream);
  }
}

//=============================================================================================
void PixelDACSettings::writeXMLTrailer(std::ofstream* outstream,
                                       std::ofstream* out1stream,
                                       std::ofstream* out2stream) const {
  std::string mthn = "[PixelDACSettings::writeXMLTrailer()]\t\t\t    ";

  *outstream << " </DATA_SET>" << std::endl;
  *outstream << "</ROOT>" << std::endl;

  outstream->close();
  std::cout << __LINE__ << "]\t" << mthn << "Data written" << std::endl;
}

/* O B S O L E T E -----

//=============================================================================================
void PixelDACSettings::writeXML(pos::PixelConfigKey key, int version, std::string path) const {
  std::string mthn = "[PixelDACSettings::writeXML()]\t\t\t    " ;
  std::stringstream fullPath ;

  PixelModuleName module(dacsettings_[0].getROCName().rocname());
  fullPath << path << "/dacsettings_" << module.modulename() << ".xml" ;
  std::cout << mthn << "Writing to: |" << fullPath.str()  << "|" << std::endl ;
  

  std::ofstream outstream(fullPath.str().c_str()) ;
  
  out << "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"		         	 << std::endl ;
  out << "<ROOT xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>" 	         	 << std::endl ;
  out << " <HEADER>"								         	 << std::endl ;
  out << "  <TYPE>"								         	 << std::endl ;
  out << "   <EXTENSION_TABLE_NAME>ROC_DAC_SETTINGS_COL</EXTENSION_TABLE_NAME>" 	 	 << std::endl ;
  out << "   <NAME>ROC DAC Settings Col</NAME>" 					 	 << std::endl ;
  out << "  </TYPE>"								         	 << std::endl ;
  out << "  <RUN>"								         	 << std::endl ;
  out << "   <RUN_TYPE>test</RUN_TYPE>" 					         	 << std::endl ;
  out << "   <RUN_NUMBER>1</RUN_NUMBER>"					         	 << std::endl ;
  out << "   <RUN_BEGIN_TIMESTAMP>" << PixelTimeFormatter::getTime() << "</RUN_BEGIN_TIMESTAMP>" << std::endl ;
  out << "   <COMMENT_DESCRIPTION>Test of DAC Settings xml</COMMENT_DESCRIPTION>"	 	 << std::endl ;
  out << "   <LOCATION>CERN TAC</LOCATION>"					         	 << std::endl ;
  out << "   <CREATED_BY_USER>Dario Menasce</CREATED_BY_USER>"  		         	 << std::endl ;
  out << "  </RUN>"								         	 << std::endl ;
  out << " </HEADER>"								         	 << std::endl ;
  out << ""									         	 << std::endl ;
  out << " <DATA_SET>"                                                             		 << std::endl ;
  out << "  <VERSION>" << version << "</VERSION>"                                  		 << std::endl ;
  out << " "                                                                       		 << std::endl ;
  out << "  <PART>"                                                                		 << std::endl ;
  out << "   <NAME_LABEL>CMS-PIXEL-ROOT</NAME_LABEL>"                              		 << std::endl ;
  out << "   <KIND_OF_PART>Detector ROOT</KIND_OF_PART>"                           		 << std::endl ;
  out << "  </PART>"                                                               		 << std::endl ;
  out << " "                                                                       		 << std::endl ;

  for(unsigned int i=0;i<dacsettings_.size();i++){
//    dacsettings_[i].writeXML(out, key, version, path);
  }

  out << " </DATA_SET>"                                                                          << std::endl ;
  out << "</ROOT>"                                                                               << std::endl ;

  out.close() ;
  std::cout << mthn << "Data written"                                                            << std::endl ;
}
*/

//=============================================================================================
void PixelDACSettings::generateConfiguration(PixelFECConfigInterface* pixelFEC,
                                             PixelNameTranslation* trans,
                                             PixelDetectorConfig* detconfig,
                                             bool HVon) const {
  bool bufferData = true;

  std::vector<unsigned int> dacs;

  //pixelFEC->fecDebug(1);  //FIXME someday maybe don't want to take the time

  for (unsigned int i = 0; i < dacsettings_.size(); i++) {  // loop over ROCs

    bool disableRoc = rocIsDisabled(detconfig, dacsettings_[i].getROCName());

    dacsettings_[i].getDACs(dacs);

    PixelHdwAddress theROC = *(trans->getHdwAddress(dacsettings_[i].getROCName()));

    //Need to set readout speed (40MHz) and Vcal range (0-1800 mV) and enable the chip

    // This is not needed. The ControlReg is programmed in setAllDAC(). d.k. 21.01.11
    //     pixelFEC->progdac(theROC.mfec(),
    // 		      theROC.mfecchannel(),
    // 		      theROC.hubaddress(),
    // 		      theROC.portaddress(),
    // 		      theROC.rocid(),
    // 		      0xfd,
    // 		      controlreg,
    // 		      bufferData);

    if (!HVon || disableRoc)
      dacs[11] = 0;  //set Vcthr DAC to 0 (Vcthr is DAC 12=11+1)
    //    std::cout<<" ; setting VcThr to "<<dacs[11]<<std::endl; //for debugging
    pixelFEC->setAllDAC(theROC, dacs, bufferData);

    // start with no pixels on for calibration
    pixelFEC->clrcal(
        theROC.mfec(), theROC.mfecchannel(), theROC.hubaddress(), theROC.portaddress(), theROC.rocid(), bufferData);

    const bool kmeKLUDGE = false;
    if (kmeKLUDGE)  //enable one pixel per ROC for calibration (all the time!)
    {
      unsigned int col = 0;
      //for(unsigned int col=0;col<52;col+=50) //try 0, 50
      {
        pixelFEC->calpix(theROC.mfec(),
                         theROC.mfecchannel(),
                         theROC.hubaddress(),
                         theROC.portaddress(),
                         theROC.rocid(),
                         col,  //column
                         0,    //row
                         1,    //caldata
                         bufferData);
      }
    }

    // enable all the double columns
    for (int dcol = 0; dcol < 26; dcol++) {
      pixelFEC->dcolenable(theROC.mfec(),
                           theROC.mfecchannel(),
                           theROC.hubaddress(),
                           theROC.portaddress(),
                           theROC.rocid(),
                           dcol,
                           1,
                           bufferData);
    }

    if (!HVon || disableRoc) {  //HV off
      int controlreg = dacsettings_[i].getControlRegister();
      //      std::cout << "[PixelDACSettings::generateConfiguration] HV off! ROC control reg to be set to: " <<  (controlreg|0x2) <<std::endl;
      pixelFEC->progdac(theROC.mfec(),
                        theROC.mfecchannel(),
                        theROC.hubaddress(),
                        theROC.portaddress(),
                        theROC.rocid(),
                        0xfd,
                        controlreg | 0x2,  //=010 in binary. should disable the chip
                        bufferData);
    }  //HV off

    // Now program (again) the temperature register to make sure it is the last one
    // and appears in the LastDAC
    if (readTemperatures) {
      //     std::cout<<"ROC="<<dacsettings_[i].getROCName()<<" ; VcThr set to "<<dacs[11]
      //       << " ROC control reg to be set to: " <<  dacs[28] <<" LastDAC=Temp"<<std::endl;
      if ((theROC.mfec() == 1) && (theROC.mfecchannel() == 1) && (theROC.hubaddress() == 0) &&
          (theROC.portaddress() == 0) && (theROC.rocid()))
        std::cout << "ROC=" << dacsettings_[i].getROCName() << " ROC control reg to be set to: " << dacs[28]
                  << " LastDAC=Temp " << temperatureReg << std::endl;
      //int temperatureReg = dacs[26];  // overwrite with the number from DB
      pixelFEC->progdac(theROC.mfec(),
                        theROC.mfecchannel(),
                        theROC.hubaddress(),
                        theROC.portaddress(),
                        theROC.rocid(),
                        0x1B,
                        temperatureReg,
                        bufferData);
    } else {
      //      std::cout<<"ROC="<<dacsettings_[i].getROCName()<<" ; VcThr set to "<<dacs[11]
      //	       << " ROC control reg to be set to: " <<  dacs[28] <<" LastDAC=Vcal"<<std::endl;
      if ((theROC.mfec() == 1) && (theROC.mfecchannel() == 1) && (theROC.hubaddress() == 0) &&
          (theROC.portaddress() == 0) && (theROC.rocid()))
        std::cout << "ROC=" << dacsettings_[i].getROCName() << " ROC control reg to be set to: " << dacs[28]
                  << " LastDAC=Vcal" << std::endl;
      // VCAL
      pixelFEC->progdac(theROC.mfec(),
                        theROC.mfecchannel(),
                        theROC.hubaddress(),
                        theROC.portaddress(),
                        theROC.rocid(),
                        0x19,
                        200,
                        bufferData);
    }

  }  // end ROC loop

  if (bufferData) {  // Send data to the FEC
    pixelFEC->qbufsend();
  }
}

void PixelDACSettings::setVcthrDisable(PixelFECConfigInterface* pixelFEC, PixelNameTranslation* trans) const {
  //the point here is to set Vcthr to 0
  //then disable the ROCs

  //note -- no need to look at the detconfig here, because we're going to disable everything no matter what

  bool bufferData = true;

  std::vector<unsigned int> dacs;

  for (unsigned int i = 0; i < dacsettings_.size(); i++) {  //loop over the ROCs

    dacsettings_[i].getDACs(dacs);
    int controlreg = dacsettings_[i].getControlRegister();

    PixelHdwAddress theROC = *(trans->getHdwAddress(dacsettings_[i].getROCName()));

    //std::cout<<"disabling ROC="<<dacsettings_[i].getROCName()<<std::endl;
    pixelFEC->progdac(theROC.mfec(),
                      theROC.mfecchannel(),
                      theROC.hubaddress(),
                      theROC.portaddress(),
                      theROC.rocid(),
                      12,  //12 == Vcthr
                      0,   //set Vcthr to 0
                      bufferData);

    //this should disable the roc
    pixelFEC->progdac(theROC.mfec(),
                      theROC.mfecchannel(),
                      theROC.hubaddress(),
                      theROC.portaddress(),
                      theROC.rocid(),
                      0xfd,
                      controlreg | 0x2,
                      bufferData);

    // Now program (again) the temperature register to make sure it is the last one
    // and appears in the LastDAC
    if (readTemperatures) {
      //int temperatureReg = dacs[26];  // value from DB
      pixelFEC->progdac(theROC.mfec(),
                        theROC.mfecchannel(),
                        theROC.hubaddress(),
                        theROC.portaddress(),
                        theROC.rocid(),
                        0x1B,
                        temperatureReg,
                        bufferData);
    } else {
      // VCAL
      pixelFEC->progdac(theROC.mfec(),
                        theROC.mfecchannel(),
                        theROC.hubaddress(),
                        theROC.portaddress(),
                        theROC.rocid(),
                        0x19,
                        200,
                        bufferData);
    }
  }

  if (bufferData) {  //just copying the way it was done in the existing method
    pixelFEC->qbufsend();
  }
}

void PixelDACSettings::setVcthrEnable(PixelFECConfigInterface* pixelFEC,
                                      PixelNameTranslation* trans,
                                      PixelDetectorConfig* detconfig) const {
  //the point here is to set Vcthr to the nominal values
  //then enable the ROCs

  bool bufferData = true;

  std::vector<unsigned int> dacs;

  for (unsigned int i = 0; i < dacsettings_.size(); i++) {  //loop over the ROCs

    bool disableRoc =
        rocIsDisabled(detconfig, dacsettings_[i].getROCName());  //don't enable ROCs that are disabled in the detconfig

    //std::cout<<"ROC="<<dacsettings_[i].getROCName()<<" ; VcThr set to "<<dacs[11]
    //	     << " ; ROC control reg to be set to: " <<  controlreg <<std::endl;

    if (!disableRoc) {  // Disable

      dacsettings_[i].getDACs(dacs);
      int controlreg = dacsettings_[i].getControlRegister();

      PixelHdwAddress theROC = *(trans->getHdwAddress(dacsettings_[i].getROCName()));

      pixelFEC->progdac(theROC.mfec(),
                        theROC.mfecchannel(),
                        theROC.hubaddress(),
                        theROC.portaddress(),
                        theROC.rocid(),
                        12,  //12 == Vcthr
                        dacs[11],
                        bufferData);

      //enable the roc (assuming controlreg was set for the roc to be enabled)

      pixelFEC->progdac(theROC.mfec(),
                        theROC.mfecchannel(),
                        theROC.hubaddress(),
                        theROC.portaddress(),
                        theROC.rocid(),
                        0xfd,
                        controlreg,
                        bufferData);

      // Now program (again) the temperature register to make sure it is the last one
      // and appears in the LastDAC
      if (readTemperatures) {
        //int temperatureReg = dacs[26];  // value from DB
        pixelFEC->progdac(theROC.mfec(),
                          theROC.mfecchannel(),
                          theROC.hubaddress(),
                          theROC.portaddress(),
                          theROC.rocid(),
                          0x1B,
                          temperatureReg,
                          bufferData);
      } else {
        // VCAL
        pixelFEC->progdac(theROC.mfec(),
                          theROC.mfecchannel(),
                          theROC.hubaddress(),
                          theROC.portaddress(),
                          theROC.rocid(),
                          0x19,
                          200,
                          bufferData);
      }

    }  // end disable

  }  // loop over ROCs

  if (bufferData) {  // Send data to FEC
    pixelFEC->qbufsend();
  }
}

bool PixelDACSettings::rocIsDisabled(const PixelDetectorConfig* detconfig, const PixelROCName rocname) const {
  const std::map<PixelROCName, PixelROCStatus>& roclist = detconfig->getROCsList();
  const std::map<PixelROCName, PixelROCStatus>::const_iterator iroc = roclist.find(rocname);
  assert(iroc != roclist.end());  // the roc name should always be found
  PixelROCStatus thisROCstatus = iroc->second;

  return thisROCstatus.get(PixelROCStatus::noAnalogSignal);
}

std::ostream& operator<<(std::ostream& s, const PixelDACSettings& dacs) {
  s << dacs.getDACSettings(0) << std::endl;

  return s;
}