00001 #include "CalibTracker/SiStripDCS/interface/SiStripPsuDetIdMap.h"
00002 #include "FWCore/Utilities/interface/Exception.h"
00003 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00004 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
00005 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00006 #include "FWCore/ServiceRegistry/interface/Service.h"
00007
00008 #include "OnlineDB/SiStripConfigDb/interface/SiStripConfigDb.h"
00009 #include "DataFormats/SiStripCommon/interface/SiStripConstants.h"
00010 #include "DataFormats/SiStripCommon/interface/SiStripEnumsAndStrings.h"
00011 #include <cstdlib>
00012 #include <iostream>
00013 #include <iomanip>
00014 #include <sstream>
00015 #include <string>
00016
00017 using namespace sistrip;
00018
00019
00020 SiStripPsuDetIdMap::SiStripPsuDetIdMap() { LogTrace("SiStripPsuDetIdMap") << "[SiStripPsuDetIdMap::" << __func__ << "] Constructing ..."; }
00021
00022 SiStripPsuDetIdMap::~SiStripPsuDetIdMap() {LogTrace("SiStripPsuDetIdMap") << "[SiStripPsuDetIdMap::" << __func__ << "] Destructing ..."; }
00023
00024
00025 void SiStripPsuDetIdMap::BuildMap( const std::string & mapFile, const bool debug )
00026 {
00027 BuildMap(mapFile, debug, LVMap, HVMap, HVUnmapped_Map, HVCrosstalking_Map);
00028 }
00029
00030 void SiStripPsuDetIdMap::BuildMap( const std::string & mapFile, std::vector<std::pair<uint32_t,std::string> > & rawmap) {
00031
00032
00033
00034
00035
00036 edm::FileInPath file(mapFile.c_str());
00037 ifstream ifs( file.fullPath().c_str() );
00038 string line;
00039 while( getline( ifs, line ) ) {
00040 if( line != "" ) {
00041
00042 stringstream ss(line);
00043 string PSUChannel;
00044 uint32_t detId;
00045 ss >> detId;
00046 ss >> PSUChannel;
00047 rawmap.push_back(std::make_pair(detId, PSUChannel) );
00048 }
00049 }
00050 }
00051
00052
00053 void SiStripPsuDetIdMap::BuildMap( const std::string & mapFile, const bool debug, PsuDetIdMap & LVmap, PsuDetIdMap & HVmap,PsuDetIdMap & HVUnmappedmap,PsuDetIdMap & HVCrosstalkingmap )
00054 {
00055
00056
00057
00058
00059
00060
00061
00062
00063 edm::FileInPath file(mapFile.c_str());
00064 ifstream ifs( file.fullPath().c_str() );
00065 string line;
00066 while( getline( ifs, line ) ) {
00067 if( line != "" ) {
00068
00069 stringstream ss(line);
00070 string PSUChannel;
00071 uint32_t detId;
00072 ss >> detId;
00073 ss >> PSUChannel;
00074
00075
00076
00077 std::string PSU=PSUChannel.substr(0,PSUChannel.size()-10);
00078 std::string Channel=PSUChannel.substr(PSUChannel.size()-10);
00079 LVmap[PSU].push_back(detId);
00080 if (Channel=="channel000") {
00081 HVUnmappedmap[PSU].push_back(detId);
00082 }
00083 else if (Channel=="channel999") {
00084 HVCrosstalkingmap[PSU].push_back(detId);
00085 }
00086 else {
00087 HVmap[PSUChannel].push_back(detId);
00088 }
00089 }
00090 }
00091
00092
00093 for (PsuDetIdMap::iterator psu = LVMap.begin(); psu != LVMap.end(); psu++) {
00094 RemoveDuplicateDetIDs(psu->second);
00095 }
00096 for (PsuDetIdMap::iterator psuchan = HVMap.begin(); psuchan != HVMap.end(); psuchan++) {
00097 RemoveDuplicateDetIDs(psuchan->second);
00098 }
00099 for (PsuDetIdMap::iterator psu = HVUnmapped_Map.begin(); psu != HVUnmapped_Map.end(); psu++) {
00100 RemoveDuplicateDetIDs(psu->second);
00101 }
00102 for (PsuDetIdMap::iterator psu = HVCrosstalking_Map.begin(); psu != HVCrosstalking_Map.end(); psu++) {
00103 RemoveDuplicateDetIDs(psu->second);
00104 }
00105 if (debug) {
00106
00107 std::cout<<"Dumping the LV map"<<std::endl;
00108 std::cout<<"PSU->detids"<<std::endl;
00109 for (PsuDetIdMap::iterator psu = LVMap.begin(); psu != LVMap.end(); psu++) {
00110 std::cout<<psu->first<<" corresponds to following detids"<<endl;
00111 for (unsigned int i=0; i<psu->second.size(); i++) {
00112 std::cout<<"\t\t"<<psu->second[i]<<std::endl;
00113 }
00114 }
00115 std::cout<<"Dumping the HV map for HV mapped channels"<<std::endl;
00116 std::cout<<"PSUChannel->detids"<<std::endl;
00117 for (PsuDetIdMap::iterator psuchan = HVMap.begin(); psuchan != HVMap.end(); psuchan++) {
00118 std::cout<<psuchan->first<<" corresponds to following detids"<<endl;
00119 for (unsigned int i=0; i<psuchan->second.size(); i++) {
00120 std::cout<<"\t\t"<<psuchan->second[i]<<std::endl;
00121 }
00122 }
00123 std::cout<<"Dumping the HV map for HV UNmapped channels"<<std::endl;
00124 std::cout<<"PSU->detids"<<std::endl;
00125 for (PsuDetIdMap::iterator psu = HVUnmapped_Map.begin(); psu != HVUnmapped_Map.end(); psu++) {
00126 std::cout<<psu->first<<" corresponds to following detids"<<endl;
00127 for (unsigned int i=0; i<psu->second.size(); i++) {
00128 std::cout<<"\t\t"<<psu->second[i]<<std::endl;
00129 }
00130 }
00131 std::cout<<"Dumping the HV map for HV Crosstalking channels"<<std::endl;
00132 std::cout<<"PSU->detids"<<std::endl;
00133 for (PsuDetIdMap::iterator psu = HVCrosstalking_Map.begin(); psu != HVCrosstalking_Map.end(); psu++) {
00134 std::cout<<psu->first<<" corresponds to following detids"<<endl;
00135 for (unsigned int i=0; i<psu->second.size(); i++) {
00136 std::cout<<"\t\t"<<psu->second[i]<<std::endl;
00137 }
00138 }
00139
00140
00141 }
00142 }
00143
00144 void SiStripPsuDetIdMap::RemoveDuplicateDetIDs(std::vector<uint32_t> & detids) {
00145
00146 if (!detids.empty()) {
00147 std::sort(detids.begin(),detids.end());
00148 std::vector<uint32_t>::iterator it = std::unique(detids.begin(),detids.end());
00149 detids.resize( it - detids.begin() );
00150 }
00151 }
00152
00153 std::vector<uint32_t> SiStripPsuDetIdMap::getLvDetID(std::string PSU) {
00154
00155
00156 if (LVMap.find(PSU)!=LVMap.end()) {
00157 return LVMap[PSU];
00158 }
00159 else {
00160 std::vector<uint32_t> detids;
00161 return detids;
00162 }
00163 }
00164
00165 void SiStripPsuDetIdMap::getHvDetID(std::string PSUChannel, std::vector<uint32_t> & ids, std::vector<uint32_t> & unmapped_ids, std::vector<uint32_t> & crosstalking_ids ) {
00166
00167 if (HVMap.find(PSUChannel)!=HVMap.end()) {
00168 ids=HVMap[PSUChannel];
00169 }
00170
00171 std::string PSU = PSUChannel.substr(0,PSUChannel.size()-10);
00172 if (HVUnmapped_Map.find(PSU)!=HVUnmapped_Map.end()) {
00173 unmapped_ids=HVUnmapped_Map[PSU];
00174 }
00175 if (HVCrosstalking_Map.find(PSU)!=HVCrosstalking_Map.end()) {
00176 crosstalking_ids=HVCrosstalking_Map[PSU];
00177 }
00178 }
00179
00180
00181
00182
00183
00184 void SiStripPsuDetIdMap::getDetID(std::string PSUChannel,const bool debug,std::vector<uint32_t> & detids,std::vector<uint32_t> & unmapped_detids,std::vector<uint32_t> & crosstalking_detids ) {
00185
00186
00187
00188
00189
00190
00191
00192 std::string PSUChannelFromQuery = PSUChannel;
00193
00194
00195 std::string ChannelFromQuery=PSUChannelFromQuery.substr(PSUChannelFromQuery.size()-10);
00196
00197 std::string PSUFromQuery=PSUChannelFromQuery.substr(0,PSUChannelFromQuery.size()-10);
00198 if (debug) {
00199
00200
00201 std::cout << "DPNAME from QUERY: "<<PSUChannelFromQuery<<", Channel: "<<ChannelFromQuery<<"PSU: "<<PSUFromQuery<<std::endl;
00202 }
00203
00204
00205
00206
00207 if (ChannelFromQuery=="channel000" or ChannelFromQuery=="channel001") {
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222 if (LVMap.find(PSUFromQuery)!=LVMap.end()) {
00223 detids=LVMap[PSUFromQuery];
00224 }
00225 }
00226
00227 else if (ChannelFromQuery=="channel002" or ChannelFromQuery=="channel003") {
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 if (HVMap.find(PSUChannelFromQuery)!=HVMap.end()) {
00260 detids=HVMap[PSUChannelFromQuery];
00261 }
00262 else if (HVUnmapped_Map.find(PSUFromQuery)!=HVUnmapped_Map.end()) {
00263 unmapped_detids=HVUnmapped_Map[PSUFromQuery];
00264 }
00265 else if (HVCrosstalking_Map.find(PSUFromQuery)!=HVCrosstalking_Map.end()) {
00266 crosstalking_detids=HVCrosstalking_Map[PSUFromQuery];
00267 }
00268 }
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334 }
00335
00336
00337 std::string SiStripPsuDetIdMap::getPSUName(uint32_t detid) {
00338 std::vector< std::pair<uint32_t, std::string> >::iterator iter;
00339 for (iter = pgMap.begin(); iter != pgMap.end(); iter++) {
00340 if (iter->first && iter->first == detid) {return iter->second;}
00341 }
00342
00343 return "UNKNOWN";
00344 }
00345
00346 std::string SiStripPsuDetIdMap::getPSUName(uint32_t detid, std::string group) {
00347 std::vector< std::pair<uint32_t, std::string> >::iterator iter;
00348 if (group == "PG") {
00349 for (iter = pgMap.begin(); iter != pgMap.end(); iter++) {
00350 if (iter->first && iter->first == detid) {return iter->second;}
00351 }
00352 }
00353 if (group == "CG") {
00354 for (iter = cgMap.begin(); iter != cgMap.end(); iter++) {
00355 if (iter->first && iter->first == detid) {return iter->second;}
00356 }
00357 }
00358
00359 return "UNKNOWN";
00360 }
00361
00362
00363 std::string SiStripPsuDetIdMap::getDetectorLocation(uint32_t detid) {
00364 for (unsigned int i = 0; i < pgMap.size(); i++) {
00365 if (pgMap[i].first == detid) {return detectorLocations[i];}
00366 }
00367 return "UNKNOWN";
00368 }
00369
00370
00371 std::string SiStripPsuDetIdMap::getDetectorLocation(uint32_t detid, std::string group) {
00372 if (group == "PG") {
00373 for (unsigned int i = 0; i < pgMap.size(); i++) {
00374 if (pgMap[i].first == detid) {return detectorLocations[i];}
00375 }
00376 }
00377 if (group == "CG") {
00378 for (unsigned int i = 0; i < cgMap.size(); i++) {
00379 if (cgMap[i].first == detid) {return controlLocations[i];}
00380 }
00381 }
00382 return "UNKNOWN";
00383 }
00384
00385
00386 std::string SiStripPsuDetIdMap::getDetectorLocation(std::string PSUChannel) {
00387 for (unsigned int i = 0; i < pgMap.size(); i++) {
00388 if (pgMap[i].second == PSUChannel) {return detectorLocations[i];}
00389 }
00390 for (unsigned int i = 0; i < cgMap.size(); i++) {
00391 if (cgMap[i].second == PSUChannel) {return controlLocations[i];}
00392 }
00393 return "UNKNOWN";
00394 }
00395
00396
00397 uint32_t SiStripPsuDetIdMap::getDcuId(std::string PSUChannel) {
00398 for (unsigned int i = 0; i < pgMap.size(); i++) {
00399 if (pgMap[i].second == PSUChannel) {return dcuIds[i];}
00400 }
00401 for (unsigned int i = 0; i < cgMap.size(); i++) {
00402 if (cgMap[i].second == PSUChannel) {return cgDcuIds[i];}
00403 }
00404 return 0;
00405 }
00406
00407 uint32_t SiStripPsuDetIdMap::getDcuId(uint32_t detid) {
00408 for (unsigned int i = 0; i < pgMap.size(); i++) {
00409 if (pgMap[i].first == detid) {return dcuIds[i];}
00410 }
00411 return 0;
00412 }
00413
00414
00415 int SiStripPsuDetIdMap::IsHVChannel(std::string PSUChannel) {
00416
00417 int isHV = 0;
00418 std::string::size_type loc = PSUChannel.find( "channel", 0 );
00419 if (loc != std::string::npos) {
00420 std::string chNumber = PSUChannel.substr(loc+7,3);
00421 if (chNumber == "002" || chNumber == "003") {
00422 isHV = 1;
00423 } else if (chNumber == "000" || chNumber == "001") {
00424 isHV = 0;
00425 } else {
00426 edm::LogWarning("SiStripPsuDetIdMap") << "[SiStripPsuDetIdMap::" << __func__ << "] channel number of unexpected format, setting error flag!";
00427 isHV = -1;
00428 }
00429 } else {
00430 edm::LogWarning("SiStripPsuDetIdMap") << "[SiStripPsuDetIdMap::" << __func__ << "] channel number not located in PSU channel name, setting error flag!";
00431 isHV = -1;
00432 }
00433 return isHV;
00434 }
00435
00436 void SiStripPsuDetIdMap::clone(DcuPsuVector &input, DcuPsuVector &output) {
00437 output.clear();
00438 for (unsigned int i = 0; i < input.size(); i++) {
00439 output.push_back(new TkDcuPsuMap(*(input[i])));
00440 }
00441 }
00442
00443 void SiStripPsuDetIdMap::printMap() {
00444 stringstream pg;
00445 pg << "Map of power supplies to DET IDs: " << std::endl
00446 << "-- PSU name -- -- Det Id --" << std::endl;
00447 for (unsigned int p = 0; p < pgMap.size(); p++) {
00448 pg << pgMap[p].first << " " << pgMap[p].second << std::endl;
00449 }
00450 edm::LogInfo("SiStripPsuDetIdMap") << "[SiStripPsuDetIdMap::" << __func__ << "] " << pg.str();
00451 }
00452
00453 void SiStripPsuDetIdMap::printControlMap() {
00454 stringstream cg;
00455 cg << "Map of control power supplies to DET IDs: " << std::endl
00456 << "-- PSU name -- -- Det Id --" << std::endl;
00457 for (unsigned int p = 0; p < cgMap.size(); p++) {
00458 cg << cgMap[p].first << " " << cgMap[p].second << std::endl;
00459 }
00460 edm::LogInfo("SiStripPsuDetIdMap") << "[SiStripPsuDetIdMap::" << __func__ << "] " << cg.str();
00461 }
00462
00463 std::vector< std::pair<uint32_t, std::string> > SiStripPsuDetIdMap::getDcuPsuMap() {
00464 if (pgMap.size() != 0) { return pgMap; }
00465 std::vector< std::pair<uint32_t, std::string> > emptyVec;
00466 return emptyVec;
00467 }
00468
00469 void SiStripPsuDetIdMap::checkMapInputValues(SiStripConfigDb::DcuDetIdsV dcuDetIds_, DcuPsuVector dcuPsus_) {
00470 std::cout << "Number of entries in DCU-PSU map: " << dcuPsus_.size() << std::endl;
00471 std::cout << "Number of entries in DCU-DETID map: " << dcuDetIds_.size() << std::endl;
00472 std::cout << std::endl;
00473
00474 std::vector<bool> ddUsed(dcuDetIds_.size(),false);
00475 std::vector<bool> dpUsed(dcuPsus_.size(),false);
00476
00477 for (unsigned int dp = 0; dp < dcuPsus_.size(); dp++) {
00478 for (unsigned int dd = 0; dd < dcuDetIds_.size(); dd++) {
00479 if (dcuPsus_[dp]->getDcuHardId() == dcuDetIds_[dd].second->getDcuHardId()) {
00480 dpUsed[dp] = true;
00481 ddUsed[dd] = true;
00482 }
00483 }
00484 }
00485 unsigned int numDpUsed = 0, numDpNotUsed = 0;
00486 for (unsigned int dp = 0; dp < dpUsed.size(); dp++) {
00487 if (dpUsed[dp]) { numDpUsed++; }
00488 else { numDpNotUsed++; }
00489 }
00490
00491 std::cout << "Number of used DCU-PSU entries: " << numDpUsed << std::endl;
00492 std::cout << "Number of unused DCU-PSU entries: " << numDpNotUsed << std::endl;
00493
00494 unsigned int numDdUsed = 0, numDdNotUsed = 0;
00495 for (unsigned int dd = 0; dd < ddUsed.size(); dd++) {
00496 if (ddUsed[dd]) { numDdUsed++; }
00497 else { numDdNotUsed++; }
00498 }
00499
00500 std::cout << "Number of used DCU-DETID entries: " << numDdUsed << std::endl;
00501 std::cout << "Number of unused DCU-DETID entries: " << numDdNotUsed << std::endl;
00502 std::cout << std::endl;
00503 std::cout << "Size of PSU-DETID map: " << pgMap.size() << std::endl;
00504 std::cout << "Size of detectorLocations: " << detectorLocations.size() << std::endl;
00505 }
00506
00507
00508 std::vector< std::pair< std::vector<uint16_t> , std::vector<uint32_t> > > SiStripPsuDetIdMap::retrieveDcuDeviceAddresses(std::string partition) {
00509
00510 SiStripDbParams dbParams_ = db_->dbParams();
00511 SiStripDbParams::SiStripPartitions::const_iterator iter;
00512
00513 std::vector< std::pair<uint32_t, SiStripConfigDb::DeviceAddress> > resultVec;
00514
00515 SiStripConfigDb::DeviceDescriptionsV dcuDevices_;
00516 SiStripConfigDb::DeviceType device_ = DCU;
00517
00518 for (iter = dbParams_.partitions().begin(); iter != dbParams_.partitions().end(); ++iter) {
00519 if ( partition == "" || partition == iter->second.partitionName() ) {
00520 if ( iter->second.partitionName() == SiStripPartition::defaultPartitionName_ ) { continue; }
00521 if (iter->second.dcuVersion().first > 0 && iter->second.fecVersion().first > 0) {
00522 SiStripConfigDb::DeviceDescriptionsRange range = db_->getDeviceDescriptions(device_,iter->second.partitionName());
00523 if (!range.empty()) {
00524 SiStripConfigDb::DeviceDescriptionsV nextVec( range.begin(), range.end() );
00525 for (unsigned int i = 0; i < nextVec.size(); i++) {
00526 dcuDescription * desc = dynamic_cast<dcuDescription *>(nextVec[i]);
00527 resultVec.push_back( std::make_pair( desc->getDcuHardId(), db_->deviceAddress(*(nextVec[i])) ) );
00528 }
00529 }
00530 }
00531 }
00532 }
00533
00534 std::vector< std::pair< std::vector<uint16_t> , std::vector<uint32_t> > > testVec;
00535 std::vector< std::pair<uint32_t, SiStripConfigDb::DeviceAddress> >::iterator reorg_iter = resultVec.begin();
00536
00537 for ( ; reorg_iter != resultVec.end(); reorg_iter++) {
00538 std::vector<uint16_t> fecInfo(4,0);
00539 fecInfo[0] = reorg_iter->second.fecCrate_;
00540 fecInfo[1] = reorg_iter->second.fecSlot_;
00541 fecInfo[2] = reorg_iter->second.fecRing_;
00542 fecInfo[3] = reorg_iter->second.ccuAddr_;
00543 std::vector<uint32_t> dcuids;
00544 std::vector< std::pair<uint32_t, SiStripConfigDb::DeviceAddress> >::iterator jter = reorg_iter;
00545 for ( ; jter != resultVec.end(); jter++) {
00546 if (reorg_iter->second.fecCrate_ == jter->second.fecCrate_ &&
00547 reorg_iter->second.fecSlot_ == jter->second.fecSlot_ &&
00548 reorg_iter->second.fecRing_ == jter->second.fecRing_ &&
00549 reorg_iter->second.ccuAddr_ == jter->second.ccuAddr_) {
00550 dcuids.push_back(jter->first);
00551 }
00552 }
00553
00554 bool isDup = false;
00555 for (unsigned int i = 0; i < testVec.size(); i++) {
00556 if (fecInfo == testVec[i].first) {
00557 isDup = true;
00558 dcuids.insert(dcuids.end(), (testVec[i].second).begin(), (testVec[i].second).end() );
00559 std::sort(dcuids.begin(),dcuids.end());
00560 std::vector<uint32_t>::iterator it = std::unique(dcuids.begin(),dcuids.end());
00561 dcuids.resize( it - dcuids.begin() );
00562 testVec[i].second = dcuids;
00563 }
00564 }
00565 if (!isDup) {
00566 std::sort(dcuids.begin(),dcuids.end());
00567 std::vector<uint32_t>::iterator it = std::unique(dcuids.begin(),dcuids.end());
00568 dcuids.resize( it - dcuids.begin() );
00569 testVec.push_back(std::make_pair(fecInfo,dcuids));
00570 }
00571 }
00572
00573 return testVec;
00574 }
00575
00576 std::vector<uint32_t> SiStripPsuDetIdMap::findDcuIdFromDeviceAddress(uint32_t dcuid_) {
00577 std::vector< std::pair< std::vector<uint16_t> , std::vector<uint32_t> > >::iterator iter = dcu_device_addr_vector.begin();
00578 std::vector< std::pair< std::vector<uint16_t> , std::vector<uint32_t> > >::iterator res_iter = dcu_device_addr_vector.end();
00579 std::vector<uint32_t> pgDcu;
00580
00581 for ( ; iter != dcu_device_addr_vector.end(); iter++) {
00582 std::vector<uint32_t> dcuids = iter->second;
00583 std::vector<uint32_t>::iterator dcu_iter = std::find(dcuids.begin(),dcuids.end(),dcuid_);
00584 bool alreadyFound = false;
00585 if (res_iter != dcu_device_addr_vector.end()) {alreadyFound = true;}
00586 if (dcu_iter != dcuids.end()) {
00587 res_iter = iter;
00588 if (!alreadyFound) {
00589 for (unsigned int i = 0; i < dcuids.size(); i++) {
00590 if (dcuids[i] != dcuid_) {pgDcu.push_back(dcuids[i]);}
00591 }
00592 } else {
00593 std::cout << "Oh oh ... we have a duplicate :-(" << std::endl;
00594 }
00595 }
00596 }
00597 return pgDcu;
00598 }
00599
00600