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
//
// This class provides the mapping between
// portcards and the modules controlled by
// the card
//
//
//

#include "CalibFormats/SiPixelObjects/interface/PixelPortcardMap.h"
#include "CalibFormats/SiPixelObjects/interface/PixelTimeFormatter.h"

#include <sstream>
#include <cassert>
#include <stdexcept>

using namespace pos;
using namespace std;

PixelPortcardMap::PixelPortcardMap(std::vector<std::vector<std::string> > &tableMat) : PixelConfigBase(" ", " ", " ") {
  std::string mthn = "[PixelPortcardMap::PixelPortcardMap()]\t\t\t    ";
  std::vector<std::string> ins = tableMat[0];
  std::map<std::string, int> colM;
  std::vector<std::string> colNames;
  /*
  EXTENSION_TABLE_NAME: PIXEL_PORTCARD_MAP (VIEW: CONF_KEY_PORTCARD_MAP_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)
  PORT_CARD				    NOT NULL VARCHAR2(200)
  PANEL_NAME				    NOT NULL VARCHAR2(200)
  TBM_MODE					     VARCHAR2(200)
  AOH_CHAN				    NOT NULL NUMBER(38)
*/
  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("PORT_CARD");
  colNames.push_back("PANEL_NAME");
  colNames.push_back("TBM_MODE");
  colNames.push_back("AOH_CHAN");
  /*
  colNames.push_back("CONFIG_KEY_ID" );
  colNames.push_back("CONFG_KEY"     );
  colNames.push_back("VERSION"       );
  colNames.push_back("KIND_OF_COND"  );
  colNames.push_back("SERIAL_NUMBER" );
  colNames.push_back("PORT_CARD"     );
  colNames.push_back("PANEL_NAME"    );
  colNames.push_back("TBM_MODE"      );
  colNames.push_back("AOH_CHAN"      );
 */
  for (unsigned int c = 0; 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=0; n<colNames.size(); n++)
    {
      if(colM.find(colNames[n]) == colM.end())
	{
	  std::cerr << __LINE__ << "]\t" << mthn 
	            << "Couldn't find in the database the column with name " << colNames[n] << std::endl;
	  assert(0);
	}
    }
  */

  std::string portcardname;
  std::string modulename;
  unsigned int aoh;
  std::string aohstring;
  std::string tbmChannel;

  for (unsigned int r = 1; r < tableMat.size(); r++) {  //Goes to every row of the Matrix

    portcardname = tableMat[r][colM["PORT_CARD"]];
    modulename = tableMat[r][colM["PANEL_NAME"]];
    aohstring = tableMat[r][colM["AOH_CHAN"]];
    tbmChannel = tableMat[r][colM["TBM_MODE"]];
    //     cout << "[PixelPortcardMap::PixelPortcardMap()]\t\t\t    "
    // 	 << "Portcardname: " << portcardname
    // 	 << "\tmodulename: "   << modulename
    // 	 << "\taohstring: "    << aohstring
    // 	 << "\ttbmChannel:"   << tbmChannel
    // 	 << endl ;
    //aohname.erase(0,20);  // Is going to be change when Umesh put a AOH Channel column in the view.
    aoh = (((unsigned int)atoi(aohstring.c_str())));
    //std::cout<<aoh<<std::endl;
    PixelModuleName module(modulename);
    if (module.modulename() != modulename) {
      std::cout << __LINE__ << "]\t" << mthn << "Modulename: " << modulename << std::endl;
      std::cout << __LINE__ << "]\t" << mthn << "Parsed to : " << module.modulename() << std::endl;
      assert(0);
    }
    if (tbmChannel.empty()) {
      tbmChannel = "A";  // assert(0); // add TBMChannel to the input, then remove assert
    }
    PixelChannel channel(module, tbmChannel);
    std::pair<std::string, int> portcardAndAOH(portcardname, aoh);
    map_[channel] = portcardAndAOH;
  }  //end for r

}  //end constructor
//*****************************************************************************

PixelPortcardMap::PixelPortcardMap(std::string filename) : PixelConfigBase(" ", " ", " ") {
  std::string mthn = "[PixelPortcardMap::PixelPortcardMap()]\t\t\t    ";
  std::ifstream in(filename.c_str());

  if (!in.good()) {
    std::cout << __LINE__ << "]\t" << mthn << "Could not open: " << filename << std::endl;
    throw std::runtime_error("Failed to open file " + filename);
  } else {
    std::cout << __LINE__ << "]\t" << mthn << "Reading from: " << filename << std::endl;
  }

  std::string dummy;

  in >> dummy;
  in >> dummy;
  in >> dummy;
  in >> dummy;
  in >> dummy;

  do {
    std::string portcardname;
    std::string modulename;
    std::string TBMChannel = "A";
    std::string aoh_string;
    unsigned int aoh;

    in >> portcardname >> modulename >> aoh_string;
    if (aoh_string == "A" ||
        aoh_string == "B")  // Optionally, the TBM channel may be specified after the module name.  Check for this.
    {
      TBMChannel = aoh_string;
      in >> aoh_string;
    }
    aoh = atoi(aoh_string.c_str());

    if (!in.eof()) {
      PixelModuleName module(modulename);
      if (module.modulename() != modulename) {
        std::cout << __LINE__ << "]\t" << mthn << "Modulename: " << modulename << std::endl;
        std::cout << __LINE__ << "]\t" << mthn << "Parsed to : " << module.modulename() << std::endl;
        assert(0);
      }

      PixelChannel channel(module, TBMChannel);
      std::pair<std::string, int> portcardAndAOH(portcardname, aoh);
      map_[channel] = portcardAndAOH;
    }

  } while (!in.eof());
}

/*const std::map<PixelModuleName, int>& PixelPortcardMap::modules(std::string portcard) const{

  std::map<std::string,std::map<PixelModuleName, int> >::const_iterator theportcard=map_.find(portcard);

  if (theportcard==map_.end()) {
    std::cout << "Could not find portcard with name:"<<portcard<<std::endl;
  }

  return theportcard->second;

}*/

PixelPortcardMap::~PixelPortcardMap() {}

void PixelPortcardMap::writeASCII(std::string dir) const {
  std::string mthn = "[PixelPortcardMap::writeASCII()]\t\t\t\t    ";
  if (!dir.empty())
    dir += "/";
  string filename = dir + "portcardmap.dat";

  ofstream out(filename.c_str());
  if (!out.good()) {
    cout << __LINE__ << "]\t" << mthn << "Could not open file: " << filename << endl;
    assert(0);
  }

  out << "# Portcard          Module                     AOH channel" << endl;
  std::map<PixelChannel, std::pair<std::string, int> >::const_iterator i = map_.begin();
  for (; i != map_.end(); ++i) {
    out << i->second.first << "   " << i->first.module() << "       " << i->first.TBMChannel() << "       "
        << i->second.second << endl;
  }
  out.close();
}

const std::set<std::pair<std::string, int> > PixelPortcardMap::PortCardAndAOHs(const PixelModuleName &aModule) const {
  std::set<std::pair<std::string, int> > returnThis;

  // Loop over the entire map, searching for elements matching PixelModuleName.  Add matching elements to returnThis.
  for (std::map<PixelChannel, std::pair<std::string, int> >::const_iterator map_itr = map_.begin();
       map_itr != map_.end();
       ++map_itr) {
    if (map_itr->first.modulename() == aModule.modulename()) {
      returnThis.insert(map_itr->second);
    }
  }

  return returnThis;
}

// Added by Dario for Debbie (the PixelPortcardMap::portcards is way to slow for the interactive tool)
bool PixelPortcardMap::getName(std::string moduleName, std::string &portcardName) {
  for (std::map<PixelChannel, std::pair<std::string, int> >::const_iterator map_itr = map_.begin();
       map_itr != map_.end();
       ++map_itr) {
    if (map_itr->first.modulename() == moduleName) {
      portcardName = map_itr->second.first;
      return true;
    }
  }
  return false;
}

const std::set<std::string> PixelPortcardMap::portcards(const PixelModuleName &aModule) const {
  std::set<std::string> returnThis;
  const std::set<std::pair<std::string, int> > portCardAndAOHs = PortCardAndAOHs(aModule);
  for (std::set<std::pair<std::string, int> >::const_iterator portCardAndAOHs_itr = portCardAndAOHs.begin();
       portCardAndAOHs_itr != portCardAndAOHs.end();
       ++portCardAndAOHs_itr) {
    returnThis.insert((*portCardAndAOHs_itr).first);
  }
  return returnThis;
}

const std::pair<std::string, int> PixelPortcardMap::PortCardAndAOH(const PixelModuleName &aModule,
                                                                   const PixelTBMChannel &TBMChannel) const {
  return PortCardAndAOH(PixelChannel(aModule, TBMChannel));
}

const std::pair<std::string, int> PixelPortcardMap::PortCardAndAOH(const PixelModuleName &aModule,
                                                                   const std::string &TBMChannel) const {
  return PortCardAndAOH(PixelChannel(aModule, TBMChannel));
}

const std::pair<std::string, int> PixelPortcardMap::PortCardAndAOH(const PixelChannel &aChannel) const {
  std::map<PixelChannel, std::pair<std::string, int> >::const_iterator found = map_.find(aChannel);
  if (found == map_.end()) {
    std::pair<std::string, int> returnThis("none", 0);
    return returnThis;
  } else {
    return found->second;
  }
}

std::set<PixelModuleName> PixelPortcardMap::modules(std::string portCardName) const {
  std::set<PixelModuleName> returnThis;

  // Loop over the entire map, searching for elements matching portCardName.  Add matching elements to returnThis.
  for (std::map<PixelChannel, std::pair<std::string, int> >::const_iterator map_itr = map_.begin();
       map_itr != map_.end();
       ++map_itr) {
    if (map_itr->second.first == portCardName) {
      returnThis.insert(map_itr->first.module());
    }
  }

  return returnThis;
}

std::set<std::string> PixelPortcardMap::portcards(const PixelDetectorConfig *detconfig) {
  std::set<std::string> returnThis;

  if (detconfig != nullptr) {
    //still done done in an awkward way, but this avoids an
    //double nested loop that we had in the first implementation
    const std::vector<PixelModuleName> &moduleList = detconfig->getModuleList();
    std::set<std::string> moduleNames;
    for (std::vector<PixelModuleName>::const_iterator it = moduleList.begin(), it_end = moduleList.end(); it != it_end;
         ++it) {
      moduleNames.insert(it->modulename());
    }

    for (std::map<PixelChannel, std::pair<std::string, int> >::const_iterator map_itr = map_.begin();
         map_itr != map_.end();
         ++map_itr) {
      if (moduleNames.find(map_itr->first.modulename()) != moduleNames.end()) {
        returnThis.insert(map_itr->second.first);
      }
    }

  } else {
    for (std::map<PixelChannel, std::pair<std::string, int> >::const_iterator map_itr = map_.begin();
         map_itr != map_.end();
         ++map_itr) {
      returnThis.insert(map_itr->second.first);
    }
  }

  return returnThis;
}
//=============================================================================================
void PixelPortcardMap::writeXMLHeader(pos::PixelConfigKey key,
                                      int version,
                                      std::string path,
                                      std::ofstream *outstream,
                                      std::ofstream *out1stream,
                                      std::ofstream *out2stream) const {
  std::string mthn = "[PixelPortcardMap::writeXMLHeader()]\t\t\t    ";
  std::stringstream fullPath;
  fullPath << path << "/Pixel_PortCardMap_" << 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 << " <HEADER>" << std::endl;
  *outstream << "  <TYPE>" << std::endl;
  *outstream << "   <EXTENSION_TABLE_NAME>PIXEL_PORTCARD_MAP</EXTENSION_TABLE_NAME>" << std::endl;
  *outstream << "   <NAME>Pixel Port Card Map</NAME>" << std::endl;
  *outstream << "  </TYPE>" << std::endl;
  *outstream << "  <RUN>" << std::endl;
  *outstream << "   <RUN_TYPE>Pixel Port Card Map</RUN_TYPE>" << std::endl;
  *outstream << "   <RUN_NUMBER>1</RUN_NUMBER>" << std::endl;
  *outstream << "   <RUN_BEGIN_TIMESTAMP>" << pos::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 << "  <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 << "  <VERSION>" << version << "</VERSION>" << std::endl;
  *outstream << "  <COMMENT_DESCRIPTION>" << getComment() << "</COMMENT_DESCRIPTION>" << std::endl;
  *outstream << "  <CREATED_BY_USER>" << getAuthor() << "</CREATED_BY_USER>" << std::endl;
}

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

  std::map<PixelChannel, std::pair<std::string, int> >::const_iterator i = map_.begin();
  for (; i != map_.end(); ++i) {
    *outstream << "  <DATA>" << std::endl;
    *outstream << "   <PORT_CARD>" << i->second.first << "</PORT_CARD>" << std::endl;
    *outstream << "   <PANEL_NAME>" << i->first.module() << "</PANEL_NAME>" << std::endl;
    *outstream << "   <TBM_MODE>" << i->first.TBMChannel() << "</TBM_MODE>" << std::endl;
    *outstream << "   <AOH_CHAN>" << i->second.second << "</AOH_CHAN>" << std::endl;
    *outstream << "  </DATA>" << std::endl;
  }
}

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

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

  outstream->close();
}