Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #include "CalibFormats/SiPixelObjects/interface/PixelDetectorConfig.h"
00009 #include "CalibFormats/SiPixelObjects/interface/PixelTimeFormatter.h"
00010 #include <fstream>
00011 #include <iostream>
00012 #include <sstream>
00013 #include <ios>
00014 #include <stdexcept>
00015 #include <assert.h>
00016 #include <stdio.h>
00017
00018 using namespace std;
00019 using namespace pos;
00020
00021
00022 PixelDetectorConfig::PixelDetectorConfig(std::vector< std::vector < std::string> > &tableMat):PixelConfigBase("","",""){
00023
00024 std::string mthn = "]\t[PixelDetectorConfig::PixelDetectorConfig()]\t\t " ;
00025 std::vector< std::string > ins = tableMat[0];
00026 std::map<std::string , int > colM;
00027 std::vector<std::string > colNames;
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 colNames.push_back("CONFIG_KEY" );
00040 colNames.push_back("KEY_TYPE" );
00041 colNames.push_back("KEY_ALIAS" );
00042 colNames.push_back("VERSION" );
00043 colNames.push_back("KIND_OF_COND");
00044 colNames.push_back("ROC_NAME" );
00045 colNames.push_back("ROC_STATUS" );
00046
00047 for(unsigned int c = 0 ; c < ins.size() ; c++){
00048 for(unsigned int n=0; n<colNames.size(); n++){
00049 if(tableMat[0][c] == colNames[n]){
00050 colM[colNames[n]] = c;
00051 break;
00052 }
00053 }
00054 }
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065 modules_.clear();
00066 rocs_.clear() ;
00067 std::string module= "";
00068 for(unsigned int r = 1 ; r < tableMat.size() ; r++)
00069 {
00070 PixelROCName roc(tableMat[r][colM["ROC_NAME"]]) ;
00071 PixelROCStatus rocstatus;
00072 std::string status = tableMat[r][colM["ROC_STATUS"]];
00073
00074
00075
00076 if(status.find("on") != string::npos)
00077 {
00078 status = "" ;
00079 }
00080 if (status!=""){
00081 rocstatus.set(status);
00082 }
00083 rocs_[roc]=rocstatus;
00084 if (!rocstatus.get(PixelROCStatus::noInit)){
00085 PixelModuleName module(tableMat[r][colM["ROC_NAME"]]);
00086 if (!containsModule(module)) {
00087 modules_.push_back(module);
00088 }
00089 }
00090 }
00091
00092
00093
00094 }
00095
00096
00097
00098 PixelDetectorConfig::PixelDetectorConfig(std::string filename):
00099 PixelConfigBase("","",""){
00100
00101 std::string mthn = "[PixelDetectorConfig::PixelDetectorConfig()]\t\t " ;
00102
00103 if (filename[filename.size()-1]=='t'){
00104
00105 std::ifstream in(filename.c_str());
00106
00107 if (!in.good()){
00108 std::cout << __LINE__ << "]\t" << mthn << "Could not open: " << filename << std::endl;
00109 throw std::runtime_error("Failed to open file "+filename);
00110 }
00111 else {
00112 std::cout << __LINE__ << "]\t" << mthn << "Opened: " << filename << std::endl;
00113 }
00114
00115 if (in.eof()){
00116 std::cout << __LINE__ << "]\t" << mthn << "EOF before reading anything!" << std::endl;
00117 throw std::runtime_error("File seems to be empty "+filename);
00118 }
00119
00120
00121 modules_.clear();
00122 rocs_.clear() ;
00123
00124 std::string module;
00125
00126 in >> module;
00127
00128 if (module=="Rocs:") {
00129 std::cout << __LINE__ << "]\t" << mthn << "New format of detconfig"<<std::endl;
00130
00131 std::string rocname;
00132 in >> rocname;
00133 while (!in.eof()){
00134
00135 PixelROCName roc(rocname);
00136 std::string line;
00137 getline(in,line);
00138
00139 istringstream instring(line);
00140 PixelROCStatus rocstatus;
00141 std::string status;
00142 while (!instring.eof()) {
00143 instring >> status;
00144
00145 if (status!=""){
00146 rocstatus.set(status);
00147 }
00148 }
00149 rocs_[roc]=rocstatus;
00150 if (!rocstatus.get(PixelROCStatus::noInit)){
00151 PixelModuleName module(rocname);
00152 if (!containsModule(module)) {
00153 modules_.push_back(module);
00154 }
00155 }
00156 in >> rocname;
00157 }
00158 return;
00159 }
00160
00161
00162
00163
00164 if (in.eof()) std::cout << mthn << "EOF after reading first module name" << std::endl;
00165
00166 std::cout << __LINE__ << "]\t" << mthn << "Old format of detconfig"<<std::endl;
00167 while (!in.eof()){
00168
00169
00170
00171 PixelModuleName moduleName(module);
00172
00173 modules_.push_back(moduleName);
00174
00175 in >> module;
00176
00177 assert(modules_.size()<10000);
00178
00179 }
00180
00181 in.close();
00182
00183 }
00184 else{
00185
00186 assert(0);
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252 }
00253
00254
00255
00256
00257
00258 }
00259
00260 unsigned int PixelDetectorConfig::getNModules() const {
00261
00262 return modules_.size();
00263
00264 }
00265
00266 PixelModuleName PixelDetectorConfig::getModule(unsigned int i) const {
00267
00268 return modules_[i];
00269
00270 }
00271
00272 std::set <unsigned int> PixelDetectorConfig::getFEDs(PixelNameTranslation* translation) const
00273 {
00274
00275 std::set <unsigned int> feds;
00276 assert(modules_.size()!=0);
00277 std::vector<PixelModuleName>::const_iterator imodule=modules_.begin();
00278
00279 for (;imodule!=modules_.end();++imodule) {
00280
00281 std::set<PixelChannel> channelsOnThisModule = translation->getChannelsOnModule(*imodule);
00282 for ( std::set<PixelChannel>::const_iterator channelsOnThisModule_itr = channelsOnThisModule.begin(); channelsOnThisModule_itr != channelsOnThisModule.end(); channelsOnThisModule_itr++ )
00283 {
00284 const PixelHdwAddress& channel_hdwaddress = translation->getHdwAddress(*channelsOnThisModule_itr);
00285 unsigned int fednumber=channel_hdwaddress.fednumber();
00286 feds.insert(fednumber);
00287 }
00288
00289 }
00290
00291 return feds;
00292 }
00293
00294
00295
00296 std::map <unsigned int, std::set<unsigned int> > PixelDetectorConfig::getFEDsAndChannels(PixelNameTranslation* translation) const
00297 {
00298
00299
00300 std::map <unsigned int, std::set<unsigned int> > fedsChannels;
00301 assert(modules_.size()!=0);
00302 std::vector<PixelModuleName>::const_iterator imodule=modules_.begin();
00303
00304 for (;imodule!=modules_.end();++imodule) {
00305
00306 std::set<PixelChannel> channelsOnThisModule = translation->getChannelsOnModule(*imodule);
00307 for ( std::set<PixelChannel>::const_iterator channelsOnThisModule_itr = channelsOnThisModule.begin(); channelsOnThisModule_itr != channelsOnThisModule.end(); channelsOnThisModule_itr++ )
00308 {
00309 const PixelHdwAddress& channel_hdwaddress = translation->getHdwAddress(*channelsOnThisModule_itr);
00310 unsigned int fednumber=channel_hdwaddress.fednumber();
00311 unsigned int fedchannel=channel_hdwaddress.fedchannel();
00312 fedsChannels[fednumber].insert(fedchannel);
00313 }
00314
00315 }
00316
00317 return fedsChannels;
00318 }
00319
00320 bool PixelDetectorConfig::containsModule(const PixelModuleName& moduleToFind) const
00321 {
00322 for ( std::vector<PixelModuleName>::const_iterator modules_itr = modules_.begin(); modules_itr != modules_.end(); modules_itr++ )
00323 {
00324 if ( *modules_itr == moduleToFind ) return true;
00325 }
00326 return false;
00327 }
00328
00329
00330 void PixelDetectorConfig::writeASCII(std::string dir) const {
00331
00332 std::stringstream s ; s << __LINE__ << "]\t[PixelDetectorConfig::writeASCII()]\t\t " ;
00333 std::string mthn = s.str() ;
00334
00335 if (dir!="") dir+="/";
00336 std::string filename=dir+"detectconfig.dat";
00337
00338 std::ofstream out(filename.c_str(), std::ios_base::out) ;
00339 if(!out) {
00340 std::cout << __LINE__ << "]\t" << mthn << "Could not open file " << filename << " for write" << std::endl ;
00341 exit(1);
00342 }
00343
00344
00345 if(rocs_.size() == 0)
00346 {
00347 std::vector<PixelModuleName>::const_iterator imodule=modules_.begin();
00348
00349 for (;imodule!=modules_.end();++imodule)
00350 {
00351 out << *imodule << std::endl;
00352 }
00353 }
00354 else
00355 {
00356 out << "Rocs:" << endl ;
00357 std::map<PixelROCName, PixelROCStatus>::const_iterator irocs = rocs_.begin();
00358 for(; irocs != rocs_.end() ; irocs++)
00359 {
00360 out << (irocs->first).rocname() << " " << (irocs->second).statusName() << endl ;
00361 }
00362 }
00363
00364 out.close();
00365
00366 }
00367
00368
00369 void PixelDetectorConfig::writeXMLHeader(pos::PixelConfigKey key,
00370 int version,
00371 std::string path,
00372 std::ofstream *outstream,
00373 std::ofstream *out1stream,
00374 std::ofstream *out2stream) const
00375 {
00376 std::string mthn = "]\t[PixelDetectorConfig::writeXMLHeader()]\t\t\t " ;
00377 std::stringstream fullPath ;
00378 fullPath << path << "/Pixel_DetectorConfig_" << PixelTimeFormatter::getmSecTime() << ".xml" ;
00379 cout << __LINE__ << mthn << "Writing to: " << fullPath.str() << endl ;
00380
00381 outstream->open(fullPath.str().c_str()) ;
00382
00383 if( !outstream->good() )
00384 {
00385 cout << __LINE__ << mthn << "FATAL: could not open file " << fullPath.str() << endl ;
00386 assert(0) ;
00387 }
00388
00389 *outstream << "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>" << std::endl ;
00390 *outstream << "<ROOT xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>" << std::endl ;
00391 *outstream << "" << std::endl ;
00392 *outstream << " <!-- " << mthn << "-->" << std::endl ;
00393 *outstream << "" << std::endl ;
00394 *outstream << " <HEADER>" << std::endl ;
00395 *outstream << " <TYPE>" << std::endl ;
00396 *outstream << " <EXTENSION_TABLE_NAME>PIXEL_DETECTOR_CONFIG</EXTENSION_TABLE_NAME>" << std::endl ;
00397 *outstream << " <NAME>Pixel Detector Configuration</NAME>" << std::endl ;
00398 *outstream << " </TYPE>" << std::endl ;
00399 *outstream << " <RUN>" << std::endl ;
00400 *outstream << " <RUN_TYPE>Pixel Detector Configuration test</RUN_TYPE>" << std::endl ;
00401 *outstream << " <RUN_NUMBER>1</RUN_NUMBER>" << std::endl ;
00402 *outstream << " <RUN_BEGIN_TIMESTAMP>" << pos::PixelTimeFormatter::getTime() << "</RUN_BEGIN_TIMESTAMP>" << std::endl ;
00403 *outstream << " <LOCATION>CERN P5</LOCATION>" << std::endl ;
00404 *outstream << " </RUN>" << std::endl ;
00405 *outstream << " </HEADER>" << std::endl ;
00406 *outstream << "" << std::endl ;
00407 *outstream << " <DATA_SET>" << std::endl ;
00408 *outstream << " " << std::endl ;
00409 *outstream << " <VERSION>" << version << "</VERSION>" << std::endl ;
00410 *outstream << " <COMMENT_DESCRIPTION>" << getComment() << "</COMMENT_DESCRIPTION>" << std::endl ;
00411 *outstream << " <CREATED_BY_USER>" << getAuthor() << "</CREATED_BY_USER>" << std::endl ;
00412 *outstream << " " << std::endl ;
00413 *outstream << " <PART>" << std::endl ;
00414 *outstream << " <NAME_LABEL>CMS-PIXEL-ROOT</NAME_LABEL>" << std::endl ;
00415 *outstream << " <KIND_OF_PART>Detector ROOT</KIND_OF_PART>" << std::endl ;
00416 *outstream << " </PART>" << std::endl ;
00417
00418 }
00419
00420
00421 void PixelDetectorConfig::writeXML( std::ofstream *outstream,
00422 std::ofstream *out1stream,
00423 std::ofstream *out2stream) const
00424 {
00425 std::stringstream s ; s << __LINE__ << "]\t[PixelDetectorConfig::writeXML()]\t\t\t " ;
00426 std::string mthn = s.str() ;
00427 if(rocs_.size() == 0)
00428 {
00429 std::vector<PixelModuleName>::const_iterator imodule=modules_.begin();
00430
00431
00432 for (;imodule!=modules_.end();++imodule)
00433 {
00434 *outstream << " <DATA>" << std::endl ;
00435
00436 *outstream << " <ROC_STATUS>on</ROC_STATUS>" << std::endl ;
00437 *outstream << " </DATA>" << std::endl ;
00438 *outstream << " " << std::endl ;
00439 }
00440 }
00441 else
00442 {
00443 std::map<PixelROCName, PixelROCStatus>::const_iterator irocs = rocs_.begin();
00444 for(; irocs != rocs_.end() ; irocs++)
00445 {
00446 std::string sts = (irocs->second).statusName() ;
00447 if( sts == "" ) {sts = "on" ;}
00448 *outstream << " " << std::endl ;
00449 *outstream << " <DATA>" << std::endl ;
00450 *outstream << " <ROC_NAME>" << (irocs->first).rocname() << "</ROC_NAME>" << std::endl ;
00451 *outstream << " <ROC_STATUS>" << sts << "</ROC_STATUS>" << std::endl ;
00452 *outstream << " </DATA>" << std::endl ;
00453 }
00454 }
00455 }
00456
00457
00458 void PixelDetectorConfig::writeXMLTrailer(std::ofstream *outstream,
00459 std::ofstream *out1stream,
00460 std::ofstream *out2stream) const
00461 {
00462 std::stringstream s ; s << __LINE__ << "]\t[PixelDetectorConfig::writeXMLTrailer()]\t\t\t " ;
00463 std::string mthn = s.str() ;
00464
00465 *outstream << " " << std::endl ;
00466 *outstream << " </DATA_SET>" << std::endl ;
00467 *outstream << "</ROOT> " << std::endl ;
00468
00469 outstream->close() ;
00470 }
00471
00472 void PixelDetectorConfig::writeXML(pos::PixelConfigKey key, int version, std::string path) const
00473 {
00474 std::stringstream s ; s << __LINE__ << "]\t[PixelDetectorConfig::writeXML()]\t\t\t " ;
00475 std::string mthn = s.str() ;
00476
00477 std::stringstream fullPath ;
00478
00479 fullPath << path << "/Pixel_DetectorConfig.xml" ;
00480 cout << __LINE__ << "]\t" << mthn << "Writing to: " << fullPath.str() << std::endl ;
00481
00482 std::ofstream out(fullPath.str().c_str()) ;
00483
00484 out << "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>" << std::endl ;
00485 out << "<ROOT xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>" << std::endl ;
00486 out << " <HEADER>" << std::endl ;
00487 out << " <TYPE>" << std::endl ;
00488 out << " <EXTENSION_TABLE_NAME>PIXEL_DETECTOR_CONFIG</EXTENSION_TABLE_NAME>" << std::endl ;
00489 out << " <NAME>Pixel Detector Configuration</NAME>" << std::endl ;
00490 out << " </TYPE>" << std::endl ;
00491 out << " <RUN>" << std::endl ;
00492 out << " <RUN_TYPE>Pixel Detector Configuration test</RUN_TYPE>" << std::endl ;
00493 out << " <RUN_NUMBER>1</RUN_NUMBER>" << std::endl ;
00494 out << " <RUN_BEGIN_TIMESTAMP>" << pos::PixelTimeFormatter::getTime() << "</RUN_BEGIN_TIMESTAMP>" << std::endl ;
00495 out << " <COMMENT_DESCRIPTION>Test of DetectorConfig xml</COMMENT_DESCRIPTION>" << std::endl ;
00496 out << " <LOCATION>CERN TAC</LOCATION>" << std::endl ;
00497 out << " <CREATED_BY_USER>Dario Menasce</CREATED_BY_USER>" << std::endl ;
00498 out << " </RUN>" << std::endl ;
00499 out << " </HEADER>" << std::endl ;
00500 out << "" << std::endl ;
00501 out << "" << std::endl ;
00502
00503 if(rocs_.size() == 0)
00504 {
00505 std::vector<PixelModuleName>::const_iterator imodule=modules_.begin();
00506
00507
00508 for (;imodule!=modules_.end();++imodule)
00509 {
00510 out << " <DATA>" << std::endl ;
00511
00512 out << " <ROC_STATUS>on</ROC_STATUS>" << std::endl ;
00513 out << " </DATA>" << std::endl ;
00514 out << " " << std::endl ;
00515 }
00516 }
00517 else
00518 {
00519 std::map<PixelROCName, PixelROCStatus>::const_iterator irocs = rocs_.begin();
00520 for(; irocs != rocs_.end() ; irocs++)
00521 {
00522 std::string sts = (irocs->second).statusName() ;
00523 if( sts == "" ) {sts = "on" ;}
00524 out << " <DATA_SET>" << std::endl ;
00525 out << " <VERSION>" << version << "</VERSION>" << std::endl ;
00526 out << " <PART>" << std::endl ;
00527 out << " <NAME_LABEL>" << (irocs->first).rocname() << "</NAME_LABEL>" << std::endl ;
00528 out << " <KIND_OF_PART>ROC</KIND_OF_PART>" << std::endl ;
00529 out << " </PART>" << std::endl ;
00530 out << " <DATA>" << std::endl ;
00531 out << " <ROC_NAME>" << (irocs->first).rocname() << "</ROC_NAME>" << std::endl ;
00532 out << " <ROC_STATUS>" << sts << "</ROC_STATUS>" << std::endl ;
00533 out << " </DATA>" << std::endl ;
00534 out << " </DATA_SET>" << std::endl ;
00535 out << " " << std::endl ;
00536 }
00537 }
00538 out << " </DATA_SET>" << std::endl ;
00539 out << "</ROOT> " << std::endl ;
00540 out.close() ;
00541 assert(0) ;
00542 }
00543
00544
00545 void PixelDetectorConfig::addROC( PixelROCName &theROC)
00546 {
00547 std::stringstream s ; s << __LINE__ << "]\t[PixelDetectorConfig::addROC()]\t\t\t\t " ;
00548 std::string mthn = s.str() ;
00549
00550 std::map<PixelROCName, PixelROCStatus>::iterator theROCIt = rocs_.find(theROC) ;
00551 if( theROCIt == rocs_.end() )
00552 {
00553 PixelROCStatus theStatus ;
00554 theStatus.reset() ;
00555 rocs_[theROC] = theStatus ;
00556
00557 } else {
00558 theROCIt->second.reset() ;
00559
00560 }
00561 }
00562
00563
00564 void PixelDetectorConfig::addROC( PixelROCName &theROC, string statusLabel)
00565 {
00566 std::stringstream s ; s << __LINE__ << "]\t[PixelDetectorConfig::addROC()]\t\t\t\t " ;
00567 std::string mthn = s.str() ;
00568
00569 std::map<PixelROCName, PixelROCStatus>::iterator theROCIt = rocs_.find(theROC) ;
00570 if( theROCIt == rocs_.end() )
00571 {
00572 PixelROCStatus theStatus ;
00573 theStatus.set(statusLabel) ;
00574 theStatus.reset() ;
00575 rocs_[theROC] = theStatus ;
00576
00577 } else {
00578 theROCIt->second.set(statusLabel) ;
00579
00580 }
00581 }
00582
00583
00584 void PixelDetectorConfig::removeROC(PixelROCName &theROC)
00585 {
00586 std::string mthn = "[PixelDetectorConfig::removeROC()]\t\t\t\t " ;
00587
00588 std::map<PixelROCName, PixelROCStatus>::iterator theROCIt = rocs_.find(theROC) ;
00589 if( theROCIt != rocs_.end() )
00590 {
00591 theROCIt->second.set("noInit") ;
00592
00593 } else {
00594 PixelROCStatus theStatus ;
00595 theStatus.set("noInit") ;
00596 rocs_[theROC] = theStatus ;
00597
00598 }
00599 }
00600
00601
00602
00603
00604
00605
00606
00607
00608