Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:14:49

0001 #include "CalibTracker/SiStripDCS/interface/SiStripPsuDetIdMap.h"
0002 #include "FWCore/Utilities/interface/Exception.h"
0003 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0004 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
0005 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0006 #include "FWCore/ServiceRegistry/interface/Service.h"
0007 
0008 #include "OnlineDB/SiStripConfigDb/interface/SiStripConfigDb.h"
0009 #include "DataFormats/SiStripCommon/interface/SiStripConstants.h"
0010 #include "DataFormats/SiStripCommon/interface/SiStripEnumsAndStrings.h"
0011 #include <cstdlib>
0012 #include <iostream>
0013 #include <iomanip>
0014 #include <sstream>
0015 #include <string>
0016 
0017 using namespace sistrip;
0018 
0019 // only one constructor
0020 SiStripPsuDetIdMap::SiStripPsuDetIdMap() {
0021   LogTrace("SiStripPsuDetIdMap") << "[SiStripPsuDetIdMap::" << __func__ << "] Constructing ...";
0022 }
0023 // destructor
0024 SiStripPsuDetIdMap::~SiStripPsuDetIdMap() {
0025   LogTrace("SiStripPsuDetIdMap") << "[SiStripPsuDetIdMap::" << __func__ << "] Destructing ...";
0026 }
0027 
0028 // Build PSU-DETID map
0029 void SiStripPsuDetIdMap::BuildMap(const std::string& mapFile, const bool debug) {
0030   BuildMap(mapFile, debug, LVMap, HVMap, HVUnmapped_Map, HVCrosstalking_Map);
0031 }
0032 
0033 void SiStripPsuDetIdMap::BuildMap(const std::string& mapFile, std::vector<std::pair<uint32_t, std::string> >& rawmap) {
0034   //This method is a remnant of the old method, that provided a vector type of map, based on the
0035   //raw reading of a file, with no processing.
0036   //FIXME:
0037   //This is not currently used, but I think we could slim this down to just a vector with
0038   //the detIDs since the PSUChannel part of the excludedlist (if it ever is in a file) is never used!
0039   edm::FileInPath file(mapFile.c_str());
0040   std::ifstream ifs(file.fullPath().c_str());
0041   string line;
0042   while (getline(ifs, line)) {
0043     if (!line.empty()) {
0044       // split the line and insert in the map
0045       stringstream ss(line);
0046       string PSUChannel;
0047       uint32_t detId;
0048       ss >> detId;
0049       ss >> PSUChannel;
0050       rawmap.push_back(std::make_pair(detId, PSUChannel));
0051     }
0052   }
0053 }
0054 
0055 //The following is the currently used method (called from SiStripDetVOffBuilder::buildPSUdetIdMap)
0056 void SiStripPsuDetIdMap::BuildMap(
0057     const std::string& mapFile,
0058     const bool debug,
0059     PsuDetIdMap& LVmap,
0060     PsuDetIdMap& HVmap,
0061     PsuDetIdMap& HVUnmappedmap,
0062     PsuDetIdMap& HVCrosstalkingmap)  //Maybe it would be nicer to return the map instead of using a reference...
0063 {
0064   //This method reads the map from the mapfile indicated in the cfg
0065   //It populates the 4 maps (private data members of the SiStripPSUDetIdMap in question) (all maps are std::map<std::string,uint32_t > ):
0066   //LVMap
0067   //HVMap
0068   //HVUnmapped_Map
0069   //HVCrosstalking_Map
0070   //These maps are accessed, based on the LV/HV case, to extract the detIDs connected to a given PSUChannel...
0071   //see the getDetIDs method...
0072   edm::FileInPath file(mapFile.c_str());
0073   std::ifstream ifs(file.fullPath().c_str());
0074   string line;
0075   while (getline(ifs, line)) {
0076     if (!line.empty()) {
0077       // split the line and insert in the map
0078       stringstream ss(line);
0079       string PSUChannel;
0080       uint32_t detId;
0081       ss >> detId;
0082       ss >> PSUChannel;
0083       //Old "vector of pairs" map!
0084       //map.push_back( std::make_pair(detId, dpName) );//This "map" is normally the pgMap of the map of which we are executing BuildMap()...
0085       //Using a map to make the look-up easy and avoid lots of lookup loops.
0086       std::string PSU = PSUChannel.substr(0, PSUChannel.size() - 10);
0087       std::string Channel = PSUChannel.substr(PSUChannel.size() - 10);
0088       LVmap[PSU].push_back(detId);  // LVmap uses simply the PSU since there is no channel distinction necessary
0089       if (Channel == "channel000") {
0090         HVUnmappedmap[PSU].push_back(
0091             detId);  //Populate HV Unmapped map, by PSU listing all detids unmapped in that PSU (not necessarily all will be unmapped)
0092       } else if (Channel == "channel999") {
0093         HVCrosstalkingmap[PSU].push_back(
0094             detId);  //Populate HV Crosstalking map, by PSU listing all detids crosstalking in that PSU (usually all will be unmapped)
0095       } else {
0096         HVmap[PSUChannel].push_back(detId);  //HV map for HV mapped channels, populated by PSU channel!
0097       }
0098     }
0099   }
0100 
0101   //Remove duplicates for all 4 maps
0102   for (PsuDetIdMap::iterator psu = LVMap.begin(); psu != LVMap.end(); psu++) {
0103     RemoveDuplicateDetIDs(psu->second);
0104   }
0105   for (PsuDetIdMap::iterator psuchan = HVMap.begin(); psuchan != HVMap.end(); psuchan++) {
0106     RemoveDuplicateDetIDs(psuchan->second);
0107   }
0108   for (PsuDetIdMap::iterator psu = HVUnmapped_Map.begin(); psu != HVUnmapped_Map.end(); psu++) {
0109     RemoveDuplicateDetIDs(psu->second);
0110   }
0111   for (PsuDetIdMap::iterator psu = HVCrosstalking_Map.begin(); psu != HVCrosstalking_Map.end(); psu++) {
0112     RemoveDuplicateDetIDs(psu->second);
0113   }
0114   if (debug) {
0115     //Print out all the 4 maps:
0116     std::cout << "Dumping the LV map" << std::endl;
0117     std::cout << "PSU->detids" << std::endl;
0118     for (PsuDetIdMap::iterator psu = LVMap.begin(); psu != LVMap.end(); psu++) {
0119       std::cout << psu->first << " corresponds to following detids" << endl;
0120       for (unsigned int i = 0; i < psu->second.size(); i++) {
0121         std::cout << "\t\t" << psu->second[i] << std::endl;
0122       }
0123     }
0124     std::cout << "Dumping the HV map for HV mapped channels" << std::endl;
0125     std::cout << "PSUChannel->detids" << std::endl;
0126     for (PsuDetIdMap::iterator psuchan = HVMap.begin(); psuchan != HVMap.end(); psuchan++) {
0127       std::cout << psuchan->first << " corresponds to following detids" << endl;
0128       for (unsigned int i = 0; i < psuchan->second.size(); i++) {
0129         std::cout << "\t\t" << psuchan->second[i] << std::endl;
0130       }
0131     }
0132     std::cout << "Dumping the HV map for HV UNmapped channels" << std::endl;
0133     std::cout << "PSU->detids" << std::endl;
0134     for (PsuDetIdMap::iterator psu = HVUnmapped_Map.begin(); psu != HVUnmapped_Map.end(); psu++) {
0135       std::cout << psu->first << " corresponds to following detids" << endl;
0136       for (unsigned int i = 0; i < psu->second.size(); i++) {
0137         std::cout << "\t\t" << psu->second[i] << std::endl;
0138       }
0139     }
0140     std::cout << "Dumping the HV map for HV Crosstalking channels" << std::endl;
0141     std::cout << "PSU->detids" << std::endl;
0142     for (PsuDetIdMap::iterator psu = HVCrosstalking_Map.begin(); psu != HVCrosstalking_Map.end(); psu++) {
0143       std::cout << psu->first << " corresponds to following detids" << endl;
0144       for (unsigned int i = 0; i < psu->second.size(); i++) {
0145         std::cout << "\t\t" << psu->second[i] << std::endl;
0146       }
0147     }
0148     //Could add here consistency checks against the list of detIDs for Strip or Pixels
0149     //Number of total detIDs LVMapped, HV Mapped, HVunmapped, HV crosstalking...
0150   }
0151 }
0152 
0153 void SiStripPsuDetIdMap::RemoveDuplicateDetIDs(std::vector<uint32_t>& detids) {
0154   //Function to remove duplicates from a vector of detids
0155   if (!detids.empty()) {  //Leave empty vector alone ;)
0156     std::sort(detids.begin(), detids.end());
0157     std::vector<uint32_t>::iterator it = std::unique(detids.begin(), detids.end());
0158     detids.resize(it - detids.begin());
0159   }
0160 }
0161 
0162 std::vector<uint32_t> SiStripPsuDetIdMap::getLvDetID(std::string PSU) {
0163   //Function that returns a vector with all detids associated with a PSU
0164   //(no channel information is saved in the map since it is not relevant for LV!)
0165   if (LVMap.find(PSU) != LVMap.end()) {
0166     return LVMap[PSU];
0167   } else {
0168     std::vector<uint32_t> detids;
0169     return detids;
0170   }
0171 }
0172 
0173 void SiStripPsuDetIdMap::getHvDetID(std::string PSUChannel,
0174                                     std::vector<uint32_t>& ids,
0175                                     std::vector<uint32_t>& unmapped_ids,
0176                                     std::vector<uint32_t>& crosstalking_ids) {
0177   //Function that (via reference parameters) populates ids, unmapped_ids, crosstalking_ids vectors of detids associated with a given PSU *HV* channel.
0178   if (HVMap.find(PSUChannel) != HVMap.end()) {
0179     ids = HVMap[PSUChannel];
0180   }
0181   //Extract the PSU to check the unmapped and crosstalking maps too corresponding to this channel
0182   std::string PSU = PSUChannel.substr(0, PSUChannel.size() - 10);
0183   if (HVUnmapped_Map.find(PSU) != HVUnmapped_Map.end()) {
0184     unmapped_ids = HVUnmapped_Map[PSU];
0185   }
0186   if (HVCrosstalking_Map.find(PSU) != HVCrosstalking_Map.end()) {
0187     crosstalking_ids = HVCrosstalking_Map[PSU];
0188   }
0189 }
0190 
0191 // This method needs to be updated once HV channel mapping is known
0192 // Currently, channel number is ignored for mapping purposes
0193 // check both PG and CG as the channels should be unique
0194 
0195 void SiStripPsuDetIdMap::getDetID(std::string PSUChannel,
0196                                   const bool debug,
0197                                   std::vector<uint32_t>& detids,
0198                                   std::vector<uint32_t>& unmapped_detids,
0199                                   std::vector<uint32_t>& crosstalking_detids) {
0200   //This function takes as argument the PSUChannel (i.e. the dpname as it comes from the PVSS query, e.g. cms_trk_dcs_02:CAEN/CMS_TRACKER_SY1527_2/branchController05/easyCrate0/easyBoard12/channel001)
0201   //And it returns 3 vectors:
0202   //1-detids->all the detids positively matching the PSUChannel in question
0203   //2-unmapped_detids->the detids that are matching the PSU in question but that are not HV mapped
0204   //3-crosstalking_detids->the detids that are matching the PSU in question but exhibit the HV channel cross-talking behavior (they are ON as long as ANY of the 2 HV channels of the supply is ON, so they only go OFF when both channels are OFF)
0205   //The second and third vectors are only relevant for the HV case, when unmapped and cross-talking channels need further processing before being turned ON and OFF.
0206 
0207   const std::string& PSUChannelFromQuery = PSUChannel;
0208 
0209   //Get the channel to see if it is LV or HV, they will be treated differently
0210   std::string ChannelFromQuery = PSUChannelFromQuery.substr(PSUChannelFromQuery.size() - 10);
0211   //Get the PSU from Query, to be used for LVMap and for the HVUnmapped and HVCrosstalking maps:
0212   std::string PSUFromQuery = PSUChannelFromQuery.substr(0, PSUChannelFromQuery.size() - 10);
0213   if (debug) {
0214     //FIXME:
0215     //Should handle all the couts with MessageLogger!
0216     std::cout << "DPNAME from QUERY: " << PSUChannelFromQuery << ", Channel: " << ChannelFromQuery
0217               << "PSU: " << PSUFromQuery << std::endl;
0218   }
0219 
0220   //First prepare the strings needed to do the matching of the PSUChannel from the query to the ones in the map
0221 
0222   //Handle the LV case first:
0223   if (ChannelFromQuery == "channel000" or ChannelFromQuery == "channel001") {
0224     //For LV channels we need to look for any detID that is reported either as channel000 (not HV mapped)
0225     //but also as channel002 and channel003 (if they are HV mapped), or as channel999 (if they are in a crosstalking PSU)
0226     //Get the PSU to do a PSU-only matching to get all detIDs connected to the LV channel:
0227     //Now loop over the map!
0228     //for (PsuDetIdMap::iterator iter = pgMap.begin(); iter != pgMap.end(); iter++) {
0229     //  std::string PSUFromMap = iter->second.substr(0,iter->second.size()-10);
0230     //  //Careful if you uncomment this cout: it prints 15148 lines when checking for 1 psu name match! (meant for debugging of course)
0231     //  //std::cout<<"Truncated DPNAME from MAP: "<<PSUFromMap<<std::endl;
0232     //  if (PSUFromQuery == PSUFromMap) {
0233     //    detids.push_back(iter->first); //And fill the detids vector with the all detids matching the PSU from the query!
0234     //  }
0235     //}
0236     //No need to loop over if we use an actual map!
0237 
0238     if (LVMap.find(PSUFromQuery) != LVMap.end()) {
0239       detids = LVMap[PSUFromQuery];
0240     }
0241   }
0242   //Handle the HV case too:
0243   else if (ChannelFromQuery == "channel002" or ChannelFromQuery == "channel003") {
0244     //For the HV channel we need to look at the actual positive matching detIDs,
0245     //but also to the unmapped one (channel000) and the crosstalking ones (channel999).
0246     //Assemble the corresponding channel000 (unmapped channels) replacing the last character in PSUChannelFromQuery:
0247     //  std::string ZeroedPSUChannelFromQuery= PSUChannelFromQuery;
0248     //  ZeroedPSUChannelFromQuery.replace(ZeroedPSUChannelFromQuery.size()-1,1,"0");
0249     //  //Same for channel999 for the crosstalking channels:
0250     //  //std::string NineNineNine='999';
0251     //  std::string NinedPSUChannelFromQuery= PSUChannelFromQuery;
0252     //  NinedPSUChannelFromQuery.replace(NinedPSUChannelFromQuery.size()-3,3,"999");
0253     //  //std::string NinedPSUChannelFromQuery= PSUChannelFromQuery.substr(0,PSUChannelFromQuery.size()-3);// + '999';
0254     //  //Now loop over the map!
0255     //  for (PsuDetIdMap::iterator iter = pgMap.begin(); iter != pgMap.end(); iter++) {
0256     //    std::string PSUChannelFromMap = iter->second;
0257     //    //Careful if you uncomment this cout: it prints 15148 lines when checking for 1 psu name match! (meant for debugging of course)
0258     //    //std::cout<<"Truncated DPNAME from MAP: "<<PSUFromMap<<std::endl;
0259     //    if (PSUChannelFromMap==PSUChannelFromQuery)  {
0260     //      detids.push_back(iter->first); //Fill the detids vector with the all detids matching the PSUChannel from the query!
0261     //    }
0262     //    if (PSUChannelFromMap==ZeroedPSUChannelFromQuery) {
0263     //      unmapped_detids.push_back(iter->first); //Fill the unmapped_detids vector with the all detids matching the channel000 for the PSU from the query!
0264     //      if (debug) { //BEWARE: this debug printouts can become very heavy! 1 print out per detID matched!
0265     //        std::cout<<"Matched one of the HV-UNMAPPED channels: "<<ZeroedPSUChannelFromQuery<<std::endl;
0266     //        std::cout<<"Corresponding to detID: "<<iter->first<<std::endl;
0267     //        //for (unsigned int i_nohvmap_detid=0;i_nohvmap_detid < iter->first.size();i_nohvmap_detid++) {
0268     //        //  cout<< iter->first[i_nohvmap_detid] << std::endl;
0269     //      }
0270     //    }
0271     //    if (PSUChannelFromMap==NinedPSUChannelFromQuery) {
0272     //      crosstalking_detids.push_back(iter->first); //Fill the crosstalking_detids vector with the all detids matching the channel999 for the PSU from the query!
0273     //    }
0274     //  }
0275     if (HVMap.find(PSUChannelFromQuery) != HVMap.end()) {
0276       detids = HVMap[PSUChannelFromQuery];
0277     } else if (HVUnmapped_Map.find(PSUFromQuery) != HVUnmapped_Map.end()) {
0278       unmapped_detids = HVUnmapped_Map[PSUFromQuery];
0279     } else if (HVCrosstalking_Map.find(PSUFromQuery) != HVCrosstalking_Map.end()) {
0280       crosstalking_detids = HVCrosstalking_Map[PSUFromQuery];
0281     }
0282   }
0283   //
0284   //
0285   //  //With the new code above that makes use of the channel00X information in the map
0286   //  //we should no more need to remove duplicates by construction.
0287   //  //The following code was used when there was no channel information in the map,
0288   //  //to elegantly eliminate duplicates.
0289   //  //We can now use it as a cross-check (still removing duplicates in case they happen, but writing a message out)
0290   //
0291   //  // remove duplicates
0292   //
0293   //  //First sort detIDs vector, so that duplicates will be consecutive
0294   //  if (!detids.empty()) {
0295   //    std::sort(detids.begin(),detids.end());
0296   //    //Then use the forward iterator unique from STD that basically removes all consecutive duplicates from the vector
0297   //    //and reports a forward iterator pointing to the new end of the sequence
0298   //    std::vector<uint32_t>::iterator it = std::unique(detids.begin(),detids.end());
0299   //    if (it!=detids.end()) {
0300   //      std::cout<<"ARGH! It seems we found duplicate detIDs in the map corresponding to this PSUChannel: "<<PSUChannelFromQuery<<std::endl;
0301   //      detids.resize( it - detids.begin() );
0302   //    }
0303   //    if (debug) {
0304   //      std::cout<<"Matched the following detIDs to PSU channel from query "<<PSUChannelFromQuery <<":"<<std::endl;
0305   //      for (std::vector<uint32_t>::iterator i_detid=detids.begin();i_detid!=detids.end(); i_detid++) {
0306   //    std::cout<<*i_detid<<std::endl;;
0307   //      }
0308   //    }
0309   //  }
0310   //  //Same for unmapped detIDs:
0311   //  if (!unmapped_detids.empty()) {
0312   //    std::sort(unmapped_detids.begin(),unmapped_detids.end());
0313   //    //Then use the forward iterator unique from STD that basically removes all consecutive duplicates from the vector
0314   //    //and reports a forward iterator pointing to the new end of the sequence
0315   //    std::vector<uint32_t>::iterator it = std::unique(unmapped_detids.begin(),unmapped_detids.end());
0316   //    if (it!=unmapped_detids.end()) {
0317   //      std::cout<<"ARGH! It seems we found duplicate unmapped_detids in the map corresponding to this PSUChannel: "<<PSUChannelFromQuery<<std::endl;
0318   //      unmapped_detids.resize( it - unmapped_detids.begin() );
0319   //    }
0320   //    if (debug) {
0321   //      std::cout<<"Matched the following unmapped_detids to PSU channel from query "<<PSUChannelFromQuery <<":"<<std::endl;
0322   //      for (std::vector<uint32_t>::iterator i_detid=unmapped_detids.begin();i_detid!=unmapped_detids.end(); i_detid++) {
0323   //    std::cout<<*i_detid<<std::endl;;
0324   //      }
0325   //    }
0326   //  }
0327   //  //Finally, same for crosstalking detIDs:
0328   //  if (!crosstalking_detids.empty()) {
0329   //    std::sort(crosstalking_detids.begin(),crosstalking_detids.end());
0330   //    //Then use the forward iterator unique from STD that basically removes all consecutive duplicates from the vector
0331   //    //and reports a forward iterator pointing to the new end of the sequence
0332   //    std::vector<uint32_t>::iterator it = std::unique(crosstalking_detids.begin(),crosstalking_detids.end());
0333   //    if (it!=crosstalking_detids.end()) {
0334   //      std::cout<<"ARGH! It seems we found duplicate crosstalking_detids in the map corresponding to this PSUChannel: "<<PSUChannelFromQuery<<std::endl;
0335   //      crosstalking_detids.resize( it - crosstalking_detids.begin() );
0336   //    }
0337   //    if (debug) {
0338   //      std::cout<<"Matched the following crosstalking_detids to PSU channel from query "<<PSUChannelFromQuery <<":"<<std::endl;
0339   //      for (std::vector<uint32_t>::iterator i_detid=crosstalking_detids.begin();i_detid!=crosstalking_detids.end(); i_detid++) {
0340   //    std::cout<<*i_detid<<std::endl;;
0341   //      }
0342   //    }
0343   //  }
0344   //
0345   //  //Using reference parameters since we are returning multiple objects.
0346   //  //return detids;
0347 }
0348 
0349 // returns PSU channel name for a given DETID
0350 std::string SiStripPsuDetIdMap::getPSUName(uint32_t detid) {
0351   std::vector<std::pair<uint32_t, std::string> >::iterator iter;
0352   for (iter = pgMap.begin(); iter != pgMap.end(); iter++) {
0353     if (iter->first && iter->first == detid) {
0354       return iter->second;
0355     }
0356   }
0357   // if we reach here, then we didn't find the detid in the map
0358   return "UNKNOWN";
0359 }
0360 
0361 std::string SiStripPsuDetIdMap::getPSUName(uint32_t detid, std::string group) {
0362   std::vector<std::pair<uint32_t, std::string> >::iterator iter;
0363   if (group == "PG") {
0364     for (iter = pgMap.begin(); iter != pgMap.end(); iter++) {
0365       if (iter->first && iter->first == detid) {
0366         return iter->second;
0367       }
0368     }
0369   }
0370   if (group == "CG") {
0371     for (iter = cgMap.begin(); iter != cgMap.end(); iter++) {
0372       if (iter->first && iter->first == detid) {
0373         return iter->second;
0374       }
0375     }
0376   }
0377   // if we reach here, then we didn't find the detid in the map
0378   return "UNKNOWN";
0379 }
0380 
0381 // returns the PVSS name for a given DETID
0382 std::string SiStripPsuDetIdMap::getDetectorLocation(uint32_t detid) {
0383   for (unsigned int i = 0; i < pgMap.size(); i++) {
0384     if (pgMap[i].first == detid) {
0385       return detectorLocations[i];
0386     }
0387   }
0388   return "UNKNOWN";
0389 }
0390 
0391 // returns the PVSS name for a given DETID, depending on specified map
0392 std::string SiStripPsuDetIdMap::getDetectorLocation(uint32_t detid, std::string group) {
0393   if (group == "PG") {
0394     for (unsigned int i = 0; i < pgMap.size(); i++) {
0395       if (pgMap[i].first == detid) {
0396         return detectorLocations[i];
0397       }
0398     }
0399   }
0400   if (group == "CG") {
0401     for (unsigned int i = 0; i < cgMap.size(); i++) {
0402       if (cgMap[i].first == detid) {
0403         return controlLocations[i];
0404       }
0405     }
0406   }
0407   return "UNKNOWN";
0408 }
0409 
0410 // returns the PVSS name for a given PSU channel
0411 std::string SiStripPsuDetIdMap::getDetectorLocation(std::string PSUChannel) {
0412   for (unsigned int i = 0; i < pgMap.size(); i++) {
0413     if (pgMap[i].second == PSUChannel) {
0414       return detectorLocations[i];
0415     }
0416   }
0417   for (unsigned int i = 0; i < cgMap.size(); i++) {
0418     if (cgMap[i].second == PSUChannel) {
0419       return controlLocations[i];
0420     }
0421   }
0422   return "UNKNOWN";
0423 }
0424 
0425 // returns the DCU ID for a given PSU channel
0426 uint32_t SiStripPsuDetIdMap::getDcuId(std::string PSUChannel) {
0427   for (unsigned int i = 0; i < pgMap.size(); i++) {
0428     if (pgMap[i].second == PSUChannel) {
0429       return dcuIds[i];
0430     }
0431   }
0432   for (unsigned int i = 0; i < cgMap.size(); i++) {
0433     if (cgMap[i].second == PSUChannel) {
0434       return cgDcuIds[i];
0435     }
0436   }
0437   return 0;
0438 }
0439 
0440 uint32_t SiStripPsuDetIdMap::getDcuId(uint32_t detid) {
0441   for (unsigned int i = 0; i < pgMap.size(); i++) {
0442     if (pgMap[i].first == detid) {
0443       return dcuIds[i];
0444     }
0445   }
0446   return 0;
0447 }
0448 
0449 // determine if a given PSU channel is HV or not
0450 int SiStripPsuDetIdMap::IsHVChannel(std::string PSUChannel) {
0451   // isHV = 0 means LV, = 1 means HV, = -1 means error
0452   int isHV = 0;
0453   std::string::size_type loc = PSUChannel.find("channel", 0);
0454   if (loc != std::string::npos) {
0455     std::string chNumber = PSUChannel.substr(loc + 7, 3);
0456     if (chNumber == "002" || chNumber == "003") {
0457       isHV = 1;
0458     } else if (chNumber == "000" || chNumber == "001") {
0459       isHV = 0;
0460     } else {
0461       edm::LogWarning("SiStripPsuDetIdMap")
0462           << "[SiStripPsuDetIdMap::" << __func__ << "] channel number of unexpected format, setting error flag!";
0463       isHV = -1;
0464     }
0465   } else {
0466     edm::LogWarning("SiStripPsuDetIdMap") << "[SiStripPsuDetIdMap::" << __func__
0467                                           << "] channel number not located in PSU channel name, setting error flag!";
0468     isHV = -1;
0469   }
0470   return isHV;
0471 }
0472 
0473 void SiStripPsuDetIdMap::clone(DcuPsuVector& input, DcuPsuVector& output) {
0474   output.clear();
0475   for (unsigned int i = 0; i < input.size(); i++) {
0476     output.push_back(new TkDcuPsuMap(*(input[i])));
0477   }
0478 }
0479 
0480 void SiStripPsuDetIdMap::printMap() {
0481   stringstream pg;
0482   pg << "Map of power supplies to DET IDs: " << std::endl << "-- PSU name --          -- Det Id --" << std::endl;
0483   for (unsigned int p = 0; p < pgMap.size(); p++) {
0484     pg << pgMap[p].first << "         " << pgMap[p].second << std::endl;
0485   }
0486   edm::LogInfo("SiStripPsuDetIdMap") << "[SiStripPsuDetIdMap::" << __func__ << "] " << pg.str();
0487 }
0488 
0489 void SiStripPsuDetIdMap::printControlMap() {
0490   stringstream cg;
0491   cg << "Map of control power supplies to DET IDs: " << std::endl
0492      << "-- PSU name --                -- Det Id --" << std::endl;
0493   for (unsigned int p = 0; p < cgMap.size(); p++) {
0494     cg << cgMap[p].first << "         " << cgMap[p].second << std::endl;
0495   }
0496   edm::LogInfo("SiStripPsuDetIdMap") << "[SiStripPsuDetIdMap::" << __func__ << "] " << cg.str();
0497 }
0498 
0499 std::vector<std::pair<uint32_t, std::string> > SiStripPsuDetIdMap::getDcuPsuMap() {
0500   if (!pgMap.empty()) {
0501     return pgMap;
0502   }
0503   std::vector<std::pair<uint32_t, std::string> > emptyVec;
0504   return emptyVec;
0505 }
0506 
0507 void SiStripPsuDetIdMap::checkMapInputValues(const SiStripConfigDb::DcuDetIdsV& dcuDetIds_,
0508                                              const DcuPsuVector& dcuPsus_) {
0509   std::cout << "Number of entries in DCU-PSU map:    " << dcuPsus_.size() << std::endl;
0510   std::cout << "Number of entries in DCU-DETID map:  " << dcuDetIds_.size() << std::endl;
0511   std::cout << std::endl;
0512 
0513   std::vector<bool> ddUsed(dcuDetIds_.size(), false);
0514   std::vector<bool> dpUsed(dcuPsus_.size(), false);
0515 
0516   for (unsigned int dp = 0; dp < dcuPsus_.size(); dp++) {
0517     for (unsigned int dd = 0; dd < dcuDetIds_.size(); dd++) {
0518       if (dcuPsus_[dp]->getDcuHardId() == dcuDetIds_[dd].second->getDcuHardId()) {
0519         dpUsed[dp] = true;
0520         ddUsed[dd] = true;
0521       }
0522     }
0523   }
0524   unsigned int numDpUsed = 0, numDpNotUsed = 0;
0525   for (unsigned int dp = 0; dp < dpUsed.size(); dp++) {
0526     if (dpUsed[dp]) {
0527       numDpUsed++;
0528     } else {
0529       numDpNotUsed++;
0530     }
0531   }
0532 
0533   std::cout << "Number of used DCU-PSU entries:   " << numDpUsed << std::endl;
0534   std::cout << "Number of unused DCU-PSU entries: " << numDpNotUsed << std::endl;
0535 
0536   unsigned int numDdUsed = 0, numDdNotUsed = 0;
0537   for (unsigned int dd = 0; dd < ddUsed.size(); dd++) {
0538     if (ddUsed[dd]) {
0539       numDdUsed++;
0540     } else {
0541       numDdNotUsed++;
0542     }
0543   }
0544 
0545   std::cout << "Number of used DCU-DETID entries:   " << numDdUsed << std::endl;
0546   std::cout << "Number of unused DCU-DETID entries: " << numDdNotUsed << std::endl;
0547   std::cout << std::endl;
0548   std::cout << "Size of PSU-DETID map:              " << pgMap.size() << std::endl;
0549   std::cout << "Size of detectorLocations:          " << detectorLocations.size() << std::endl;
0550 }
0551 
0552 //std::vector< std::pair<uint32_t, SiStripConfigDb::DeviceAddress> > SiStripPsuDetIdMap::retrieveDcuDeviceAddresses(std::string partition) {
0553 std::vector<std::pair<std::vector<uint16_t>, std::vector<uint32_t> > > SiStripPsuDetIdMap::retrieveDcuDeviceAddresses(
0554     std::string partition) {
0555   // get the DB parameters
0556   SiStripDbParams dbParams_ = db_->dbParams();
0557   SiStripDbParams::SiStripPartitions::const_iterator iter;
0558 
0559   std::vector<std::pair<uint32_t, SiStripConfigDb::DeviceAddress> > resultVec;
0560 
0561   SiStripConfigDb::DeviceDescriptionsV dcuDevices_;
0562   SiStripConfigDb::DeviceType device_ = DCU;
0563 
0564   for (iter = dbParams_.partitions().begin(); iter != dbParams_.partitions().end(); ++iter) {
0565     if (partition.empty() || partition == iter->second.partitionName()) {
0566       if (iter->second.partitionName() == SiStripPartition::defaultPartitionName_) {
0567         continue;
0568       }
0569       if (iter->second.dcuVersion().first > 0 && iter->second.fecVersion().first > 0) {
0570         SiStripConfigDb::DeviceDescriptionsRange range =
0571             db_->getDeviceDescriptions(device_, iter->second.partitionName());
0572         if (!range.empty()) {
0573           SiStripConfigDb::DeviceDescriptionsV nextVec(range.begin(), range.end());
0574           for (unsigned int i = 0; i < nextVec.size(); i++) {
0575             dcuDescription* desc = dynamic_cast<dcuDescription*>(nextVec[i]);
0576             resultVec.push_back(std::make_pair(desc->getDcuHardId(), db_->deviceAddress(*(nextVec[i]))));
0577           }
0578         }
0579       }
0580     }
0581   }
0582 
0583   std::vector<std::pair<std::vector<uint16_t>, std::vector<uint32_t> > > testVec;
0584   std::vector<std::pair<uint32_t, SiStripConfigDb::DeviceAddress> >::iterator reorg_iter = resultVec.begin();
0585 
0586   for (; reorg_iter != resultVec.end(); reorg_iter++) {
0587     std::vector<uint16_t> fecInfo(4, 0);
0588     fecInfo[0] = reorg_iter->second.fecCrate_;
0589     fecInfo[1] = reorg_iter->second.fecSlot_;
0590     fecInfo[2] = reorg_iter->second.fecRing_;
0591     fecInfo[3] = reorg_iter->second.ccuAddr_;
0592     std::vector<uint32_t> dcuids;
0593     std::vector<std::pair<uint32_t, SiStripConfigDb::DeviceAddress> >::iterator jter = reorg_iter;
0594     for (; jter != resultVec.end(); jter++) {
0595       if (reorg_iter->second.fecCrate_ == jter->second.fecCrate_ &&
0596           reorg_iter->second.fecSlot_ == jter->second.fecSlot_ &&
0597           reorg_iter->second.fecRing_ == jter->second.fecRing_ &&
0598           reorg_iter->second.ccuAddr_ == jter->second.ccuAddr_) {
0599         dcuids.push_back(jter->first);
0600       }
0601     }
0602     // handle duplicates
0603     bool isDup = false;
0604     for (unsigned int i = 0; i < testVec.size(); i++) {
0605       if (fecInfo == testVec[i].first) {
0606         isDup = true;
0607         dcuids.insert(dcuids.end(), (testVec[i].second).begin(), (testVec[i].second).end());
0608         std::sort(dcuids.begin(), dcuids.end());
0609         std::vector<uint32_t>::iterator it = std::unique(dcuids.begin(), dcuids.end());
0610         dcuids.resize(it - dcuids.begin());
0611         testVec[i].second = dcuids;
0612       }
0613     }
0614     if (!isDup) {
0615       std::sort(dcuids.begin(), dcuids.end());
0616       std::vector<uint32_t>::iterator it = std::unique(dcuids.begin(), dcuids.end());
0617       dcuids.resize(it - dcuids.begin());
0618       testVec.push_back(std::make_pair(fecInfo, dcuids));
0619     }
0620   }
0621   //  return resultVec;
0622   return testVec;
0623 }
0624 
0625 std::vector<uint32_t> SiStripPsuDetIdMap::findDcuIdFromDeviceAddress(uint32_t dcuid_) {
0626   std::vector<std::pair<std::vector<uint16_t>, std::vector<uint32_t> > >::iterator iter =
0627       dcu_device_addr_vector.begin();
0628   std::vector<std::pair<std::vector<uint16_t>, std::vector<uint32_t> > >::iterator res_iter =
0629       dcu_device_addr_vector.end();
0630   std::vector<uint32_t> pgDcu;
0631 
0632   for (; iter != dcu_device_addr_vector.end(); iter++) {
0633     std::vector<uint32_t> dcuids = iter->second;
0634     std::vector<uint32_t>::iterator dcu_iter = std::find(dcuids.begin(), dcuids.end(), dcuid_);
0635     bool alreadyFound = false;
0636     if (res_iter != dcu_device_addr_vector.end()) {
0637       alreadyFound = true;
0638     }
0639     if (dcu_iter != dcuids.end()) {
0640       res_iter = iter;
0641       if (!alreadyFound) {
0642         for (unsigned int i = 0; i < dcuids.size(); i++) {
0643           if (dcuids[i] != dcuid_) {
0644             pgDcu.push_back(dcuids[i]);
0645           }
0646         }
0647       } else {
0648         std::cout << "Oh oh ... we have a duplicate :-(" << std::endl;
0649       }
0650     }
0651   }
0652   return pgDcu;
0653 }