CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/CalibFormats/SiStripObjects/src/SiStripDetCabling.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 // Package:     SiStripObjects
00003 // Class  :     SiStripDetCabling
00004 // Original Author:  dkcira
00005 //         Created:  Wed Mar 22 12:24:33 CET 2006
00006 // $Id: SiStripDetCabling.cc,v 1.23 2010/03/29 12:32:37 demattia Exp $
00007 #include "FWCore/Utilities/interface/typelookup.h"
00008 #include "CalibFormats/SiStripObjects/interface/SiStripDetCabling.h"
00009 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00010 #include "DataFormats/SiStripDetId/interface/SiStripSubStructure.h"
00011 #include "DataFormats/SiStripDetId/interface/TIBDetId.h"
00012 #include "DataFormats/SiStripDetId/interface/TIDDetId.h"
00013 #include "DataFormats/SiStripDetId/interface/TOBDetId.h"
00014 #include "DataFormats/SiStripDetId/interface/TECDetId.h"
00015 
00016 //---- default constructor / destructor
00017 SiStripDetCabling::SiStripDetCabling() : fedCabling_(0) {}
00018 SiStripDetCabling::~SiStripDetCabling() {}
00019 
00020 //---- construct detector view (DetCabling) out of readout view (FedCabling)
00021 SiStripDetCabling::SiStripDetCabling(const SiStripFedCabling& fedcabling) : fullcabling_(), connected_(), detected_(), undetected_(), fedCabling_(&fedcabling)
00022 {
00023   // --- CONNECTED = have fedid and i2cAddr
00024   // create fullcabling_, loop over vector of FedChannelConnection, either make new element of map, or add to appropriate vector of existing map element
00025   // get feds list (vector) from fedcabling object - these are the active FEDs
00026   const std::vector<uint16_t>& feds = fedcabling.feds();
00027   std::vector<uint16_t>::const_iterator ifed;
00028   for ( ifed = feds.begin(); ifed != feds.end(); ifed++ ) { // iterate over active feds, get all their FedChannelConnection-s
00029     const std::vector<FedChannelConnection>& conns = fedcabling.connections( *ifed );
00030     std::vector<FedChannelConnection>::const_iterator iconn;
00031     for ( iconn = conns.begin(); iconn != conns.end(); iconn++ ) { // loop over FedChannelConnection objects
00032       addDevices(*iconn, fullcabling_); // leave separate method, in case you will need to add devices also after constructing
00033       bool have_fed_id = iconn->fedId();
00034       std::vector<int> vector_of_connected_apvs;
00035       if(have_fed_id){ // these apvpairs are seen from the readout
00036         // there can be at most 6 APVs on one DetId: 0,1,2,3,4,5
00037         int which_apv_pair = iconn->apvPairNumber(); // APVPair (0,1) for 512 strips and (0,1,2) for 768 strips
00038         
00039         // patch needed to take into account invalid detids or apvPairs
00040         if( iconn->detId()==0 ||  
00041             iconn->detId() == sistrip::invalid32_ ||  
00042             iconn->apvPairNumber() == sistrip::invalid_  ||
00043             iconn->nApvPairs() == sistrip::invalid_ ) {
00044           continue;
00045         }
00046 
00047         if(iconn->i2cAddr(0)) vector_of_connected_apvs.push_back(2*which_apv_pair + 0); // first apv of the pair
00048         if(iconn->i2cAddr(1)) vector_of_connected_apvs.push_back(2*which_apv_pair + 1); // second apv of the pair
00049       }
00050       if(vector_of_connected_apvs.size() != 0){ // add only is smth. there, obviously
00051         std::map<uint32_t, std::vector<int> > map_of_connected_apvs;
00052         map_of_connected_apvs.insert(std::make_pair(iconn->detId(),vector_of_connected_apvs));
00053         addFromSpecificConnection(connected_, map_of_connected_apvs, 0);
00054       }
00055     }
00056   }
00057   // --- DETECTED = do not have fedid but have i2cAddr
00058   const std::vector<FedChannelConnection>& detected_fed_connections = fedcabling.detected();
00059   for(std::vector<FedChannelConnection>::const_iterator idtct = detected_fed_connections.begin(); idtct != detected_fed_connections.end(); idtct++){
00060     addDevices(*idtct, fullcabling_);
00061     bool have_fed_id = idtct->fedId();
00062     std::vector<int> vector_of_detected_apvs;
00063     if(! have_fed_id){
00064       int which_apv_pair = idtct->apvPairNumber(); // APVPair (0,1) for 512 strips and (0,1,2) for 768 strips
00065       if(idtct->i2cAddr(0)) vector_of_detected_apvs.push_back(2*which_apv_pair + 0); // first apv of the pair
00066       if(idtct->i2cAddr(1)) vector_of_detected_apvs.push_back(2*which_apv_pair + 1); // second apv of the pair
00067     }
00068     if(vector_of_detected_apvs.size() != 0){ // add only is smth. there, obviously
00069       std::map<uint32_t,std::vector<int> > map_of_detected_apvs;
00070       map_of_detected_apvs.insert(std::make_pair(idtct->detId(),vector_of_detected_apvs));
00071       addFromSpecificConnection(detected_, map_of_detected_apvs, 1);
00072     }
00073   }
00074   // --- UNDETECTED = have neither fedid nor i2caddr
00075   const std::vector<FedChannelConnection>& undetected_fed_connections = fedcabling.undetected();
00076   for(std::vector<FedChannelConnection>::const_iterator iudtct = undetected_fed_connections.begin(); iudtct != undetected_fed_connections.end(); iudtct++){
00077     addDevices(*iudtct, fullcabling_);
00078     bool have_fed_id = iudtct->fedId();
00079     std::vector<int> vector_of_undetected_apvs;
00080     if(! have_fed_id){
00081       int which_apv_pair = iudtct->apvPairNumber(); // APVPair (0,1) for 512 strips and (0,1,2) for 768 strips
00082       if(iudtct->i2cAddr(0)) vector_of_undetected_apvs.push_back(2*which_apv_pair + 0); // first apv of the pair
00083       if(iudtct->i2cAddr(1)) vector_of_undetected_apvs.push_back(2*which_apv_pair + 1); // second apv of the pair
00084     }
00085     if(vector_of_undetected_apvs.size() != 0){ // add only is smth. there, obviously
00086       std::map<uint32_t, std::vector<int> > map_of_undetected_apvs;
00087       map_of_undetected_apvs.insert(std::make_pair(iudtct->detId(),vector_of_undetected_apvs));
00088       addFromSpecificConnection(undetected_, map_of_undetected_apvs, 2);
00089     }
00090   }
00091 }
00092 
00093 //---- add to certain connections
00094 void SiStripDetCabling::addDevices( const FedChannelConnection& conn, 
00095                                     std::map< uint32_t, std::vector<FedChannelConnection> >& conns ){
00096   if( conn.detId() && conn.detId() != sistrip::invalid32_ &&  // check for valid detid
00097       conn.apvPairNumber() != sistrip::invalid_ ) {           // check for valid apv pair number
00098     if( conn.fedId()==0 || conn.fedId()==sistrip::invalid_ ){
00099       edm::LogInfo("") << " SiStripDetCabling::addDevices for connection associated to detid " << conn.detId() << " apvPairNumber " << conn.apvPairNumber() << "the fedId is " << conn.fedId();
00100       return;
00101     }
00102     // check cached vector size is sufficient
00103     // if not, resize
00104     if( conn.apvPairNumber() >= conns[conn.detId()].size() ) {
00105       conns[conn.detId()].resize( conn.apvPairNumber()+1 );
00106     }
00107     // add latest connection object
00108     conns[conn.detId()][conn.apvPairNumber()] = conn;
00109   }
00110 }
00111 
00112 //----
00113 void SiStripDetCabling::addDevices(const FedChannelConnection & conn){ // by default add to fullcabling_ connections - special case of above class
00114  addDevices(conn, fullcabling_ ); // add to fullcabling_
00115 }
00116 
00117 //---- get vector of connected modules. replaces getActiveDetectorRawIds method - avoid use of static
00118 void SiStripDetCabling::addActiveDetectorsRawIds(std::vector<uint32_t> & vector_to_fill_with_detids ) const{
00119  for(std::map< uint32_t, std::vector<int> >::const_iterator conn_it = connected_.begin(); conn_it!=connected_.end(); conn_it++){
00120    vector_to_fill_with_detids.push_back(conn_it->first);
00121  }
00122  // no elements added to vector_to_fill_with_detids is empty connected_
00123 }
00124 
00125 //---- get vector of all modules.
00126 void SiStripDetCabling::addAllDetectorsRawIds(std::vector<uint32_t> & vector_to_fill_with_detids ) const{
00127  for(std::map< uint32_t, std::vector<int> >::const_iterator conn_it = connected_.begin(); conn_it!=connected_.end(); conn_it++){
00128    vector_to_fill_with_detids.push_back(conn_it->first);
00129  }
00130  for(std::map< uint32_t, std::vector<int> >::const_iterator conn_it = detected_.begin(); conn_it!=detected_.end(); conn_it++){
00131    vector_to_fill_with_detids.push_back(conn_it->first);
00132  }
00133  for(std::map< uint32_t, std::vector<int> >::const_iterator conn_it = undetected_.begin(); conn_it!=undetected_.end(); conn_it++){
00134    vector_to_fill_with_detids.push_back(conn_it->first);
00135  }
00136  // no elements added to vector_to_fill_with_detids is empty connected_, detected_.begin and undetected_.begin
00137 }
00138 
00139 //----
00140 const std::vector<FedChannelConnection>& SiStripDetCabling::getConnections(uint32_t det_id ) const{ // return all connections corresponding to one det_id
00141   std::map< uint32_t, std::vector<FedChannelConnection> >::const_iterator detcabl_it = fullcabling_.find(det_id); // has to be const_iterator because this function cannot change data members
00142   if( ! (detcabl_it==fullcabling_.end()) ){  // found detid in fullcabling_
00143     return ( detcabl_it->second );
00144   }else{ // DKwarn : is there need for output message here telling det_id does not exist?
00145     static std::vector<FedChannelConnection> default_empty_fedchannelconnection;
00146     return default_empty_fedchannelconnection;
00147   }
00148 }
00149 
00150 //----
00151 const FedChannelConnection& SiStripDetCabling::getConnection( uint32_t det_id, unsigned short apv_pair ) const{
00152   const std::vector<FedChannelConnection>& fcconns = getConnections(det_id);
00153   for(std::vector<FedChannelConnection>::const_iterator iconn = fcconns.begin(); iconn!=fcconns.end();iconn++){
00154     if ( (iconn->apvPairNumber()) == apv_pair){ // check if apvPairNumber() of present FedChannelConnection is the same as requested one
00155       return (*iconn); // if yes, return the FedChannelConnection object
00156     }
00157   }
00158   // if did not match none of the above, return some default value - DKwarn : also output message?
00159   static FedChannelConnection default_empty_fedchannelconnection;
00160   return default_empty_fedchannelconnection;
00161 }
00162 
00163 //----
00164 const unsigned int SiStripDetCabling::getDcuId( uint32_t det_id ) const{
00165   const std::vector<FedChannelConnection>& fcconns = getConnections( det_id );
00166   if(fcconns.size()!=0) {
00167     // patch needed to take into account the possibility that the first component of fcconns is invalid
00168     for(size_t i=0;i<fcconns.size();++i)       
00169       if (fcconns.at(i).detId() != sistrip::invalid32_ && fcconns.at(i).detId() != 0 )
00170         return ( fcconns.at(i) ).dcuId(); // get dcuId of first element - when you build check this consistency
00171   }
00172   // default if none of the above is fulfilled
00173   unsigned int default_zero_value = 0;
00174   return default_zero_value;
00175 }
00176 
00177 //---- one can find the nr of apvs from fullcabling_ -> std::vector<FedChannelConnection> -> size * 2
00178 const uint16_t SiStripDetCabling::nApvPairs(uint32_t det_id) const{
00179  const std::vector<FedChannelConnection>& fcconns = getConnections( det_id );
00180  if(fcconns.size()!=0) {
00181    // patch needed to take into account the possibility that the first component of fcconns is invalid
00182    for(size_t i=0;i<fcconns.size();++i)       
00183      if (fcconns.at(i).nApvPairs() != sistrip::invalid_)
00184        return fcconns.at(i).nApvPairs(); // nr of apvpairs for associated module
00185  }else{
00186    return 0;
00187  }
00188  return 0;
00189 }
00190 
00191 //---- map of detector to list of APVs for APVs seen from FECs and FEDs
00192 void SiStripDetCabling::addConnected ( std::map<uint32_t, std::vector<int> > & map_to_add_to) const{
00193   addFromSpecificConnection(map_to_add_to, connected_);
00194 }
00195 
00196 //--- map of detector to list of APVs for APVs seen neither from FECS or FEDs
00197 void SiStripDetCabling::addDetected( std::map<uint32_t, std::vector<int> > & map_to_add_to) const{
00198   addFromSpecificConnection(map_to_add_to, detected_);
00199 }
00200 
00201 //---- map of detector to list of APVs for APVs seen neither from FECS or FEDs
00202 void SiStripDetCabling::addUnDetected( std::map<uint32_t, std::vector<int> > & map_to_add_to) const{
00203   addFromSpecificConnection(map_to_add_to, undetected_);
00204 }
00205 
00206 //----  map of detector to list of APVs that are not connected - combination of addDetected and addUnDetected
00207 void SiStripDetCabling::addNotConnectedAPVs( std::map<uint32_t, std::vector<int> > & map_to_add_to) const{
00208   addFromSpecificConnection(map_to_add_to, detected_);
00209   addFromSpecificConnection(map_to_add_to, undetected_);
00210 }
00211 
00212 //----
00213 void SiStripDetCabling::addFromSpecificConnection( std::map<uint32_t, std::vector<int> > & map_to_add_to,
00214                                                    const std::map< uint32_t, std::vector<int> > & specific_connection,
00215                                                    const int connectionType ) const {
00216   for(std::map< uint32_t, std::vector<int> >::const_iterator conn_it = specific_connection.begin(); conn_it!=specific_connection.end(); ++conn_it){
00217     uint32_t new_detid = conn_it->first;
00218     std::vector<int> new_apv_vector = conn_it->second;
00219     std::map<uint32_t, std::vector<int> >::iterator it = map_to_add_to.find(new_detid);
00220     if( it == map_to_add_to.end() ){ // detid does not exist in map, add new entry
00221       std::sort(new_apv_vector.begin(),new_apv_vector.end()); // not very efficient sort, time consuming?
00222       map_to_add_to.insert(std::make_pair(new_detid,new_apv_vector));
00223 
00224       // Count the number of detIds per layer. Doing it in this "if" we count each detId only once
00225       // (otherwise it would be counted once per APV pair)
00226       // ATTENTION: consider changing the loop content to avoid this
00227       // This is the expected full number of modules (double sided are counted twice because the two
00228       // sides have different detId).
00229       // TIB1 : 336, TIB2 : 432, TIB3 : 540, TIB4 : 648
00230       // TID : each disk has 48+48+40 (ring1+ring2+ring3)
00231       // TOB1 : 504, TOB2 : 576, TOB3 : 648, TOB4 : 720, TOB5 : 792, TOB6 : 888
00232       // TEC1 : Total number of modules = 6400.
00233       if( connectionType != -1 ) {
00234         connectionCount[connectionType][layerSearch(new_detid)]++;
00235       }
00236     }else{                    // detid exists already, add to its vector - if its not there already . . .
00237       std::vector<int> existing_apv_vector = it->second;
00238       for(std::vector<int>::iterator inew = new_apv_vector.begin(); inew != new_apv_vector.end(); inew++ ){
00239         bool there_already = false;
00240         for(std::vector<int>::iterator iold = existing_apv_vector.begin(); iold != existing_apv_vector.end(); iold++){
00241           if (*iold == *inew){
00242             there_already = true;
00243             break; // leave the loop
00244           }
00245         }
00246         if( ! there_already ){
00247           existing_apv_vector.push_back(*inew);
00248           std::sort(existing_apv_vector.begin(),existing_apv_vector.end()); // not very efficient sort, time consuming?
00249         }else{
00250           //edm::LogWarning("Logical") << "apv "<<*inew<<" already exists in the detector module "<<new_detid;
00251         }
00252       }
00253     }
00254   }
00255 }
00256 
00257 int16_t SiStripDetCabling::layerSearch( const uint32_t detId ) const
00258 {
00259   if(SiStripDetId(detId).subDetector()==SiStripDetId::TIB){
00260     TIBDetId D(detId);
00261     return D.layerNumber();
00262   } else if (SiStripDetId(detId).subDetector()==SiStripDetId::TID){
00263     TIDDetId D(detId);
00264     // side: 1 = negative, 2 = positive
00265     return 10+(D.side() -1)*3 + D.wheel();
00266   } else if (SiStripDetId(detId).subDetector()==SiStripDetId::TOB){
00267     TOBDetId D(detId);
00268     return 100+D.layerNumber();
00269   } else if (SiStripDetId(detId).subDetector()==SiStripDetId::TEC){
00270     TECDetId D(detId);
00271     // side: 1 = negative, 2 = positive
00272     return 1000+(D.side() -1)*9 + D.wheel();
00273   }
00274   return 0;
00275 }
00276 
00278 uint32_t SiStripDetCabling::detNumber(const std::string & subDet, const uint16_t layer, const int connectionType) const {
00279   uint16_t subDetLayer = layer;
00280   // TIB = 1, TID = 2, TOB = 3, TEC = 4
00281   if( subDet == "TID-" ) subDetLayer += 10;
00282   else if( subDet == "TID+" ) subDetLayer += 10 + 3;
00283   else if( subDet == "TOB" ) subDetLayer += 100;
00284   else if( subDet == "TEC-" ) subDetLayer += 1000;
00285   else if( subDet == "TEC+" ) subDetLayer += 1000 + 9;
00286   else if( subDet != "TIB" ) {
00287     LogDebug("SiStripDetCabling") << "Error: Wrong subDet. Please use one of TIB, TID, TOB, TEC." << std::endl;
00288     return 0;
00289   }
00290   return connectionCount[connectionType][subDetLayer];
00291 }
00292 
00293 //---- map of all connected, detected, undetected to contiguous Ids - map reset first!
00294 void SiStripDetCabling::getAllDetectorsContiguousIds(std::map<uint32_t, unsigned int>& allToContiguous) const{
00295   allToContiguous.clear(); // reset map
00296   std::vector<uint32_t> all; addAllDetectorsRawIds(all); std::sort(all.begin(), all.end()); // get all detids and sort them
00297   unsigned int contiguousIndex = 0;
00298   for(std::vector<uint32_t>::const_iterator idet = all.begin(); idet!= all.end(); ++idet){
00299      ++contiguousIndex;
00300      allToContiguous.insert(std::make_pair(*idet,contiguousIndex)); 
00301   }
00302 }
00303 
00304 //---- map of all connected - map reset first!
00305 void SiStripDetCabling::getActiveDetectorsContiguousIds(std::map<uint32_t, unsigned int>& connectedToContiguous) const{
00306   connectedToContiguous.clear(); // reset map
00307   std::vector<uint32_t> connected; addAllDetectorsRawIds(connected); std::sort(connected.begin(), connected.end()); // get connected detids and sort them (not strictly necessary)
00308   std::map<uint32_t, unsigned int> allToContiguous; getAllDetectorsContiguousIds(allToContiguous); // create map of all indices
00309   for(std::vector<uint32_t>::const_iterator idet = connected.begin(); idet!= connected.end(); ++idet){ // select only the indices for active detectors
00310     std::map<uint32_t, unsigned int>::iterator deco = allToContiguous.find(*idet);
00311     if(deco!=allToContiguous.end()){
00312        connectedToContiguous.insert(*deco);
00313     }
00314   }
00315 }
00316 
00317 bool SiStripDetCabling::IsConnected(const uint32_t& det_id) const {
00318   return IsInMap(det_id,connected_);
00319 }
00320 
00321 bool SiStripDetCabling::IsDetected(const uint32_t& det_id) const {
00322   return IsInMap(det_id,detected_);
00323 }
00324 bool SiStripDetCabling::IsUndetected(const uint32_t& det_id) const{
00325   return IsInMap(det_id,undetected_);
00326 }
00327 bool SiStripDetCabling::IsInMap(const uint32_t& det_id, const std::map<uint32_t, std::vector<int> > & map) const{
00328   std::map< uint32_t, std::vector<int> >::const_iterator it=map.find(det_id);
00329   return (it!=map.end());
00330 }
00331 
00332 // -----------------------------------------------------------------------------
00334 void SiStripDetCabling::print( std::stringstream& ss ) const {
00335   uint32_t valid = 0;
00336   uint32_t total = 0;
00337   typedef std::vector<FedChannelConnection> Conns;
00338   typedef std::map<uint32_t,Conns> ConnsMap;
00339   ConnsMap::const_iterator ii = fullcabling_.begin();
00340   ConnsMap::const_iterator jj = fullcabling_.end();
00341   ss << "[SiStripDetCabling::" << __func__ << "]"
00342      << " Printing DET cabling for " << fullcabling_.size()
00343      << " modules " << std::endl;
00344   for ( ; ii != jj; ++ii ) {
00345     ss << "Printing " << ii->second.size()
00346        << " connections for DetId: " << ii->first << std::endl;
00347     Conns::const_iterator iii = ii->second.begin();
00348     Conns::const_iterator jjj = ii->second.end();
00349     for ( ; iii != jjj; ++iii ) { 
00350       if ( iii->isConnected() ) { valid++; }
00351       total++;
00352       ss << *iii << std::endl; 
00353     }
00354   }
00355   ss << "Number of connected:   " << valid << std::endl
00356      << "Number of connections: " << total << std::endl;
00357 }
00358 
00359 void SiStripDetCabling::printSummary(std::stringstream& ss) const {
00360   for( int connectionType = 0; connectionType < 3; ++connectionType ) {
00361     if( connectionType == 0 ) ss << "Connected modules:" << std::endl;
00362     else if( connectionType == 1 ) ss << "Detected modules:" << std::endl;
00363     else ss << "Undetected modules:" << std::endl;
00364     ss << "SubDet and layer\t modules" << std::endl;
00365     std::map< int16_t, uint32_t >::const_iterator iter = connectionCount[connectionType].begin();
00366     for( ; iter != connectionCount[connectionType].end(); ++iter ) {
00367       uint32_t subDetLayer = iter->first;
00368       uint32_t modules = iter->second;
00369       if( int(subDetLayer/10) == 0 ) {
00370         ss << "TIB \t layer " << subDetLayer << " \t" << modules << std::endl;
00371       }
00372       else if( int(subDetLayer/100) == 0 ) {
00373         int layer = subDetLayer%10;
00374         if( layer <= 3 ) ss << "TID- \t disk  " << layer << "\t" << modules << std::endl;
00375         else ss << "TID+ \t disk  " << layer-3 << "\t" << modules << std::endl;
00376       }
00377       else if( int(subDetLayer/1000) == 0 ) {
00378         int layer = subDetLayer%100;
00379         ss << "TOB \t layer " << layer << " \t" << modules << std::endl;
00380       }
00381       else {
00382         int layer = subDetLayer%100;
00383         if( layer <= 9 ) ss << "TEC- \t disk  " << layer << " \t" << modules << std::endl;
00384         else ss << "TEC+ \t disk  " << layer-9 << " \t" << modules << std::endl;
00385       }
00386     }
00387   }
00388 }
00389 
00390 void SiStripDetCabling::printDebug(std::stringstream& ss) const {
00391   print(ss);
00392 }