00001
00002
00003
00004
00005
00006
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
00017 SiStripDetCabling::SiStripDetCabling() : fedCabling_(0) {}
00018 SiStripDetCabling::~SiStripDetCabling() {}
00019
00020
00021 SiStripDetCabling::SiStripDetCabling(const SiStripFedCabling& fedcabling) : fullcabling_(), connected_(), detected_(), undetected_(), fedCabling_(&fedcabling)
00022 {
00023
00024
00025
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++ ) {
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++ ) {
00032 addDevices(*iconn, fullcabling_);
00033 bool have_fed_id = iconn->fedId();
00034 std::vector<int> vector_of_connected_apvs;
00035 if(have_fed_id){
00036
00037 int which_apv_pair = iconn->apvPairNumber();
00038
00039
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);
00048 if(iconn->i2cAddr(1)) vector_of_connected_apvs.push_back(2*which_apv_pair + 1);
00049 }
00050 if(vector_of_connected_apvs.size() != 0){
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
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();
00065 if(idtct->i2cAddr(0)) vector_of_detected_apvs.push_back(2*which_apv_pair + 0);
00066 if(idtct->i2cAddr(1)) vector_of_detected_apvs.push_back(2*which_apv_pair + 1);
00067 }
00068 if(vector_of_detected_apvs.size() != 0){
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
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();
00082 if(iudtct->i2cAddr(0)) vector_of_undetected_apvs.push_back(2*which_apv_pair + 0);
00083 if(iudtct->i2cAddr(1)) vector_of_undetected_apvs.push_back(2*which_apv_pair + 1);
00084 }
00085 if(vector_of_undetected_apvs.size() != 0){
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
00094 void SiStripDetCabling::addDevices( const FedChannelConnection& conn,
00095 std::map< uint32_t, std::vector<FedChannelConnection> >& conns ){
00096 if( conn.detId() && conn.detId() != sistrip::invalid32_ &&
00097 conn.apvPairNumber() != sistrip::invalid_ ) {
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
00103
00104 if( conn.apvPairNumber() >= conns[conn.detId()].size() ) {
00105 conns[conn.detId()].resize( conn.apvPairNumber()+1 );
00106 }
00107
00108 conns[conn.detId()][conn.apvPairNumber()] = conn;
00109 }
00110 }
00111
00112
00113 void SiStripDetCabling::addDevices(const FedChannelConnection & conn){
00114 addDevices(conn, fullcabling_ );
00115 }
00116
00117
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
00123 }
00124
00125
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
00137 }
00138
00139
00140 const std::vector<FedChannelConnection>& SiStripDetCabling::getConnections(uint32_t det_id ) const{
00141 std::map< uint32_t, std::vector<FedChannelConnection> >::const_iterator detcabl_it = fullcabling_.find(det_id);
00142 if( ! (detcabl_it==fullcabling_.end()) ){
00143 return ( detcabl_it->second );
00144 }else{
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){
00155 return (*iconn);
00156 }
00157 }
00158
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
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();
00171 }
00172
00173 unsigned int default_zero_value = 0;
00174 return default_zero_value;
00175 }
00176
00177
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
00182 for(size_t i=0;i<fcconns.size();++i)
00183 if (fcconns.at(i).nApvPairs() != sistrip::invalid_)
00184 return fcconns.at(i).nApvPairs();
00185 }else{
00186 return 0;
00187 }
00188 return 0;
00189 }
00190
00191
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
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
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
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() ){
00221 std::sort(new_apv_vector.begin(),new_apv_vector.end());
00222 map_to_add_to.insert(std::make_pair(new_detid,new_apv_vector));
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233 if( connectionType != -1 ) {
00234 connectionCount[connectionType][layerSearch(new_detid)]++;
00235 }
00236 }else{
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;
00244 }
00245 }
00246 if( ! there_already ){
00247 existing_apv_vector.push_back(*inew);
00248 std::sort(existing_apv_vector.begin(),existing_apv_vector.end());
00249 }else{
00250
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
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
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
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
00294 void SiStripDetCabling::getAllDetectorsContiguousIds(std::map<uint32_t, unsigned int>& allToContiguous) const{
00295 allToContiguous.clear();
00296 std::vector<uint32_t> all; addAllDetectorsRawIds(all); std::sort(all.begin(), all.end());
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
00305 void SiStripDetCabling::getActiveDetectorsContiguousIds(std::map<uint32_t, unsigned int>& connectedToContiguous) const{
00306 connectedToContiguous.clear();
00307 std::vector<uint32_t> connected; addAllDetectorsRawIds(connected); std::sort(connected.begin(), connected.end());
00308 std::map<uint32_t, unsigned int> allToContiguous; getAllDetectorsContiguousIds(allToContiguous);
00309 for(std::vector<uint32_t>::const_iterator idet = connected.begin(); idet!= connected.end(); ++idet){
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 }