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 <assert.h>
00015 #include <stdio.h>
00016
00017 using namespace std;
00018 using namespace pos;
00019
00020
00021 PixelDetectorConfig::PixelDetectorConfig(std::vector< std::vector < std::string> > &tableMat):PixelConfigBase("","",""){
00022
00023 std::vector< std::string > ins = tableMat[0];
00024 std::map<std::string , int > colM;
00025 std::vector<std::string > colNames;
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 colNames.push_back("CONFIG_KEY_ID");
00037 colNames.push_back("CONFIG_KEY");
00038 colNames.push_back("VERSION");
00039 colNames.push_back("KIND_OF_COND");
00040 colNames.push_back("SERIAL_NUMBER");
00041 colNames.push_back("ROC_NAME");
00042 colNames.push_back("PANEL_NAME");
00043 colNames.push_back("ROC_STATUS");
00044
00045 for(unsigned int c = 0 ; c < ins.size() ; c++){
00046 for(unsigned int n=0; n<colNames.size(); n++){
00047 if(tableMat[0][c] == colNames[n]){
00048 colM[colNames[n]] = c;
00049 break;
00050 }
00051 }
00052 }
00053 for(unsigned int n=0; n<colNames.size(); n++){
00054 if(colM.find(colNames[n]) == colM.end()){
00055 std::cerr << "[PixelDetectorConfig::PixelDetectorConfig()]\tCouldn't find in the database the column with name " << colNames[n] << std::endl;
00056 assert(0);
00057 }
00058 }
00059
00060
00061 modules_.clear();
00062 rocs_.clear() ;
00063 std::string module= "";
00064 for(unsigned int r = 1 ; r < tableMat.size() ; r++)
00065 {
00066 PixelROCName roc(tableMat[r][colM["ROC_NAME"]]) ;
00067 PixelROCStatus rocstatus;
00068 std::string status = tableMat[r][colM["ROC_STATUS"]];
00069
00070
00071
00072 if(status.find("ON") != string::npos)
00073 {
00074 status = "" ;
00075 }
00076 if (status!=""){
00077 rocstatus.set(status);
00078 }
00079 rocs_[roc]=rocstatus;
00080 if (!rocstatus.get(PixelROCStatus::noInit)){
00081
00082 PixelModuleName module(tableMat[r][colM["PANEL_NAME"]]);
00083 if (!containsModule(module)) {
00084 modules_.push_back(module);
00085 }
00086 }
00087 }
00088
00089 std::cout<<"Number of Modules in Detector Configuration Class:"<<getNModules()<<std::endl;
00090
00091 }
00092
00093
00094
00095 PixelDetectorConfig::PixelDetectorConfig(std::string filename):
00096 PixelConfigBase("","",""){
00097
00098 if (filename[filename.size()-1]=='t'){
00099
00100 std::ifstream in(filename.c_str());
00101
00102 if (!in.good()){
00103 std::cout << "[PixelDetectorConfig::PixelDetectorConfig()]\t\t Could not open: "<<filename<<std::endl;
00104 assert(0);
00105 }
00106 else {
00107 std::cout << "[PixelDetectorConfig::PixelDetectorConfig()]\t\t Opened: "<<filename<<std::endl;
00108 }
00109
00110 if (in.eof()){
00111 std::cout << "[PixelDetectorConfig::PixelDetectorConfig()]\t\t EOF before reading anything!"<<std::endl;
00112 ::abort();
00113 }
00114
00115
00116 modules_.clear();
00117 rocs_.clear() ;
00118
00119 std::string module;
00120
00121 in >> module;
00122
00123 if (module=="Rocs:") {
00124 std::cout << "[PixelDetectorConfig::PixelDetectorConfig()]\t\t New format of detconfig"<<std::endl;
00125
00126 std::string rocname;
00127 in >> rocname;
00128 while (!in.eof()){
00129
00130 PixelROCName roc(rocname);
00131 std::string line;
00132 getline(in,line);
00133
00134 istringstream instring(line);
00135 PixelROCStatus rocstatus;
00136 std::string status;
00137 while (!instring.eof()) {
00138 instring >> status;
00139
00140 if (status!=""){
00141 rocstatus.set(status);
00142 }
00143 }
00144 rocs_[roc]=rocstatus;
00145 if (!rocstatus.get(PixelROCStatus::noInit)){
00146 PixelModuleName module(rocname);
00147 if (!containsModule(module)) {
00148 modules_.push_back(module);
00149 }
00150 }
00151 in >> rocname;
00152 }
00153 return;
00154 }
00155
00156
00157
00158
00159 if (in.eof()) std::cout << "[PixelDetectorConfig::PixelDetectorConfig()]\t\t EOF after reading first module name"
00160 << std::endl;
00161
00162 std::cout << "[PixelDetectorConfig::PixelDetectorConfig()]\t\t Old format of detconfig"<<std::endl;
00163 while (!in.eof()){
00164
00165
00166
00167 PixelModuleName moduleName(module);
00168
00169 modules_.push_back(moduleName);
00170
00171 in >> module;
00172
00173 assert(modules_.size()<10000);
00174
00175 }
00176
00177 in.close();
00178
00179 }
00180 else{
00181
00182 assert(0);
00183
00184
00185
00186
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 unsigned int PixelDetectorConfig::getNModules() const {
00257
00258 return modules_.size();
00259
00260 }
00261
00262 PixelModuleName PixelDetectorConfig::getModule(unsigned int i) const {
00263
00264 return modules_[i];
00265
00266 }
00267
00268 std::set <unsigned int> PixelDetectorConfig::getFEDs(PixelNameTranslation* translation) const
00269 {
00270
00271 std::set <unsigned int> feds;
00272 assert(modules_.size()!=0);
00273 std::vector<PixelModuleName>::const_iterator imodule=modules_.begin();
00274
00275 for (;imodule!=modules_.end();++imodule) {
00276
00277 std::set<PixelChannel> channelsOnThisModule = translation->getChannelsOnModule(*imodule);
00278 for ( std::set<PixelChannel>::const_iterator channelsOnThisModule_itr = channelsOnThisModule.begin(); channelsOnThisModule_itr != channelsOnThisModule.end(); channelsOnThisModule_itr++ )
00279 {
00280 const PixelHdwAddress& channel_hdwaddress = translation->getHdwAddress(*channelsOnThisModule_itr);
00281 unsigned int fednumber=channel_hdwaddress.fednumber();
00282 feds.insert(fednumber);
00283 }
00284
00285 }
00286
00287 return feds;
00288 }
00289
00290
00291
00292 std::map <unsigned int, std::set<unsigned int> > PixelDetectorConfig::getFEDsAndChannels(PixelNameTranslation* translation) const
00293 {
00294
00295
00296 std::map <unsigned int, std::set<unsigned int> > fedsChannels;
00297 assert(modules_.size()!=0);
00298 std::vector<PixelModuleName>::const_iterator imodule=modules_.begin();
00299
00300 for (;imodule!=modules_.end();++imodule) {
00301
00302 std::set<PixelChannel> channelsOnThisModule = translation->getChannelsOnModule(*imodule);
00303 for ( std::set<PixelChannel>::const_iterator channelsOnThisModule_itr = channelsOnThisModule.begin(); channelsOnThisModule_itr != channelsOnThisModule.end(); channelsOnThisModule_itr++ )
00304 {
00305 const PixelHdwAddress& channel_hdwaddress = translation->getHdwAddress(*channelsOnThisModule_itr);
00306 unsigned int fednumber=channel_hdwaddress.fednumber();
00307 unsigned int fedchannel=channel_hdwaddress.fedchannel();
00308 fedsChannels[fednumber].insert(fedchannel);
00309 }
00310
00311 }
00312
00313 return fedsChannels;
00314 }
00315
00316 bool PixelDetectorConfig::containsModule(const PixelModuleName& moduleToFind) const
00317 {
00318 for ( std::vector<PixelModuleName>::const_iterator modules_itr = modules_.begin(); modules_itr != modules_.end(); modules_itr++ )
00319 {
00320 if ( *modules_itr == moduleToFind ) return true;
00321 }
00322 return false;
00323 }
00324
00325
00326 void PixelDetectorConfig::writeASCII(std::string dir) const {
00327
00328 if (dir!="") dir+="/";
00329 std::string filename=dir+"detectconfig.dat";
00330
00331 std::ofstream out(filename.c_str(), std::ios_base::out) ;
00332 if(!out) {
00333 std::cout << "[PixelDetectorConfig::writeASCII()]\t\t Could not open file " << filename << " for write" << std::endl ;
00334 exit(1);
00335 }
00336
00337
00338 if(rocs_.size() == 0)
00339 {
00340 std::vector<PixelModuleName>::const_iterator imodule=modules_.begin();
00341
00342 for (;imodule!=modules_.end();++imodule)
00343 {
00344 out << *imodule << std::endl;
00345 }
00346 }
00347 else
00348 {
00349 out << "Rocs:" << endl ;
00350 std::map<PixelROCName, PixelROCStatus>::const_iterator irocs = rocs_.begin();
00351 for(; irocs != rocs_.end() ; irocs++)
00352 {
00353 out << (irocs->first).rocname() << " " << (irocs->second).statusName() << endl ;
00354 }
00355 }
00356
00357 out.close();
00358
00359 }
00360
00361
00362 void PixelDetectorConfig::writeXML(pos::PixelConfigKey key, int version, std::string path) const {
00363 std::string mthn = "[PixelDetectorConfig::writeXML()]\t\t\t " ;
00364 std::stringstream fullPath ;
00365
00366 fullPath << path << "/detectorconfig.xml" ;
00367 cout << mthn << "Writing to: " << fullPath.str() << endl ;
00368
00369 std::ofstream out(fullPath.str().c_str()) ;
00370
00371 out << "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>" << endl ;
00372 out << "<ROOT xmlns:xsi='https://www.w3.org/2001/XMLSchema-instance'>" << endl ;
00373 out << " <HEADER>" << endl ;
00374 out << " <TYPE>" << endl ;
00375 out << " <EXTENSION_TABLE_NAME>FPIX_DETECTOR_CONFIG</EXTENSION_TABLE_NAME>" << endl ;
00376 out << " <NAME>FPix Detector Configuration</NAME>" << endl ;
00377 out << " </TYPE>" << endl ;
00378 out << " <RUN>" << endl ;
00379 out << " <RUN_TYPE>test</RUN_TYPE>" << endl ;
00380 out << " <RUN_NUMBER>1</RUN_NUMBER>" << endl ;
00381 out << " <RUN_BEGIN_TIMESTAMP>" << pos::PixelTimeFormatter::getTime() << "</RUN_BEGIN_TIMESTAMP>" << endl ;
00382 out << " <COMMENT_DESCRIPTION>Test of DetectorConfig xml</COMMENT_DESCRIPTION>" << endl ;
00383 out << " <LOCATION>CERN TAC</LOCATION>" << endl ;
00384 out << " <INITIATED_BY_USER>Dario Menasce</INITIATED_BY_USER>" << endl ;
00385 out << " </RUN>" << endl ;
00386 out << " </HEADER>" << endl ;
00387 out << "" << endl ;
00388 out << " <DATA_SET>" << endl ;
00389 out << " <VERSION>" << version << "</VERSION>" << endl ;
00390 out << " <PART>" << endl ;
00391 out << " <NAME_LABEL>CMS-PIXEL-ROOT</NAME_LABEL>" << endl ;
00392 out << " <KIND_OF_PART>Detector ROOT</KIND_OF_PART>" << endl ;
00393 out << " </PART>" << endl ;
00394 out << "" << endl ;
00395
00396 if(rocs_.size() == 0)
00397 {
00398 std::vector<PixelModuleName>::const_iterator imodule=modules_.begin();
00399
00400
00401 for (;imodule!=modules_.end();++imodule)
00402 {
00403 out << " <DATA>" << endl ;
00404
00405 out << " <ROC_STATUS>on</ROC_STATUS>" << endl ;
00406 out << " </DATA>" << endl ;
00407 out << " " << endl ;
00408 }
00409 }
00410 else
00411 {
00412 std::map<PixelROCName, PixelROCStatus>::const_iterator irocs = rocs_.begin();
00413 for(; irocs != rocs_.end() ; irocs++)
00414 {
00415 std::string sts = (irocs->second).statusName() ;
00416 if( sts == "" ) {sts = "on" ;}
00417 out << " <DATA>" << endl ;
00418 out << " <ROC_NAME>" << (irocs->first).rocname() << "</ROC_NAME>" << endl ;
00419 out << " <ROC_STATUS>" << sts << "</ROC_STATUS>" << endl ;
00420 out << " </DATA>" << endl ;
00421 out << " " << endl ;
00422 }
00423 }
00424 out << " </DATA_SET>" << endl ;
00425 out << "</ROOT> " << endl ;
00426 out.close() ;
00427 assert(0) ;
00428 }
00429
00430
00431 void PixelDetectorConfig::addROC( PixelROCName &theROC)
00432 {
00433 std::string mthn = "[PixelDetectorConfig::addROC()]\t\t\t\t" ;
00434 std::map<PixelROCName, PixelROCStatus>::iterator theROCIt = rocs_.find(theROC) ;
00435 if( theROCIt == rocs_.end() )
00436 {
00437 PixelROCStatus theStatus ;
00438 theStatus.reset() ;
00439 rocs_[theROC] = theStatus ;
00440
00441 } else {
00442 theROCIt->second.reset() ;
00443
00444 }
00445 }
00446
00447
00448 void PixelDetectorConfig::addROC( PixelROCName &theROC, string statusLabel)
00449 {
00450 std::string mthn = "[PixelDetectorConfig::addROC()]\t\t\t\t" ;
00451 std::map<PixelROCName, PixelROCStatus>::iterator theROCIt = rocs_.find(theROC) ;
00452 if( theROCIt == rocs_.end() )
00453 {
00454 PixelROCStatus theStatus ;
00455 theStatus.set(statusLabel) ;
00456 theStatus.reset() ;
00457 rocs_[theROC] = theStatus ;
00458
00459 } else {
00460 theROCIt->second.set(statusLabel) ;
00461
00462 }
00463 }
00464
00465
00466 void PixelDetectorConfig::removeROC(PixelROCName &theROC)
00467 {
00468 std::string mthn = "[PixelDetectorConfig::removeROC()]\t\t\t\t" ;
00469 std::map<PixelROCName, PixelROCStatus>::iterator theROCIt = rocs_.find(theROC) ;
00470 if( theROCIt != rocs_.end() )
00471 {
00472 theROCIt->second.set("noInit") ;
00473
00474 } else {
00475 PixelROCStatus theStatus ;
00476 theStatus.set("noInit") ;
00477 rocs_[theROC] = theStatus ;
00478
00479 }
00480 }
00481
00482
00483
00484
00485
00486
00487
00488
00489