CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_1/src/OnlineDB/SiStripConfigDb/src/FedConnections.cc

Go to the documentation of this file.
00001 // Last commit: $Id: FedConnections.cc,v 1.35 2011/09/02 11:25:25 eulisse Exp $
00002 
00003 #include "OnlineDB/SiStripConfigDb/interface/SiStripConfigDb.h"
00004 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00005 #include <ostream>
00006 
00007 using namespace std;
00008 using namespace sistrip;
00009 
00010 // -----------------------------------------------------------------------------
00011 // 
00012 SiStripConfigDb::FedConnectionsRange SiStripConfigDb::getFedConnections( std::string partition ) {
00013 
00014   // Check
00015   if ( ( !dbParams_.usingDbCache() && !deviceFactory(__func__) ) ||
00016        (  dbParams_.usingDbCache() && !databaseCache(__func__) ) ) { 
00017     return connections_.emptyRange();
00018   }
00019   
00020   try {
00021     
00022     if ( !dbParams_.usingDbCache() ) { 
00023       
00024       SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
00025       SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
00026       for ( ; iter != jter; ++iter ) {
00027         
00028         if ( partition == "" || partition == iter->second.partitionName() ) {
00029 
00030           if ( iter->second.partitionName() == SiStripPartition::defaultPartitionName_ ) { continue; }
00031           
00032           FedConnectionsRange range = connections_.find( iter->second.partitionName() );
00033           if ( range == connections_.emptyRange() ) {
00034 
00035             FedConnectionsV tmp2;
00036             
00037             // Retrieve connections
00038             FedConnectionsV tmp1;
00039             deviceFactory(__func__)->getConnectionDescriptions( iter->second.partitionName(), 
00040                                                                 tmp1,
00041                                                                 iter->second.cabVersion().first,
00042                                                                 iter->second.cabVersion().second,
00043                                                                 //#ifdef USING_DATABASE_MASKING
00044                                                                 iter->second.maskVersion().first,
00045                                                                 iter->second.maskVersion().second,
00046                                                                 //#endif
00047                                                                 false ); //@@ do not get DISABLED connections
00048             
00049             // Make local copy 
00050             ConnectionFactory::vectorCopyI( tmp2, tmp1, true );
00051             
00052             // Add to cache
00053             connections_.loadNext( iter->second.partitionName(), tmp2 );
00054             
00055             // Some debug
00056             FedConnectionsRange conns = connections_.find( iter->second.partitionName() );
00057             std::stringstream ss;
00058             ss << "[SiStripConfigDb::" << __func__ << "]"
00059                << " Downloaded " << conns.size() 
00060                << " FED connections to local cache for partition \""
00061                << iter->second.partitionName() << "\"" << std::endl;
00062             ss << "[SiStripConfigDb::" << __func__ << "]"
00063                << " Cache holds FED connections for " 
00064                << connections_.size() << " partitions.";
00065             LogTrace(mlConfigDb_) << ss.str();
00066             
00067           }
00068           
00069         }
00070 
00071       }
00072 
00073     } else { // Use database cache
00074         
00075       FedConnectionsV* tmp1 = databaseCache(__func__)->getConnections();
00076       
00077       if ( tmp1 ) { 
00078         
00079         // Make local copy 
00080         FedConnectionsV tmp2;
00081         ConnectionFactory::vectorCopyI( tmp2, *tmp1, true );
00082         
00083         // Add to cache
00084         connections_.loadNext( SiStripPartition::defaultPartitionName_, tmp2 );
00085         
00086       } else {
00087         edm::LogWarning(mlConfigDb_)
00088           << "[SiStripConfigDb::" << __func__ << "]"
00089           << " NULL pointer to FedConnections vector!";
00090       }
00091       
00092     }
00093     
00094   } catch (...) { handleException( __func__ ); }
00095   
00096   // Create range object
00097   uint16_t np = 0;
00098   uint16_t nc = 0;
00099   FedConnectionsRange conns;
00100   if ( partition != "" ) { 
00101     conns = connections_.find( partition );
00102     np = 1;
00103     nc = conns.size();
00104   } else { 
00105     if ( !connections_.empty() ) {
00106       conns = FedConnectionsRange( connections_.find( dbParams_.partitions().begin()->second.partitionName() ).begin(),
00107                                    connections_.find( (--(dbParams_.partitions().end()))->second.partitionName() ).end() );
00108     } else { conns = connections_.emptyRange(); }
00109     np = connections_.size();
00110     nc = conns.size();
00111   }
00112   
00113   stringstream ss; 
00114   ss << "[SiStripConfigDb::" << __func__ << "]"
00115      << " Found " << nc << " FED connections";
00116   if ( !dbParams_.usingDb() ) { ss << " in " << dbParams_.inputModuleXmlFiles().size() << " 'module.xml' file(s)"; }
00117   else { if ( !dbParams_.usingDbCache() )  { ss << " in " << np << " database partition(s)"; } 
00118   else { ss << " from shared memory name '" << dbParams_.sharedMemory() << "'"; } }
00119   if ( connections_.empty() ) { edm::LogWarning(mlConfigDb_) << ss.str(); }
00120   else { LogTrace(mlConfigDb_) << ss.str(); }
00121 
00122   return conns;
00123   
00124 }
00125 
00126 // -----------------------------------------------------------------------------
00127 // 
00128 void SiStripConfigDb::addFedConnections( std::string partition, FedConnectionsV& conns ) {
00129 
00130   if ( !deviceFactory(__func__) ) { return; }
00131 
00132   if ( partition.empty() ) { 
00133     stringstream ss; 
00134     ss << "[SiStripConfigDb::" << __func__ << "]" 
00135        << " Partition string is empty,"
00136        << " therefore cannot add FED connections to local cache!"; 
00137     edm::LogWarning(mlConfigDb_) << ss.str(); 
00138     return; 
00139   }
00140   
00141   if ( conns.empty() ) { 
00142     stringstream ss; 
00143     ss << "[SiStripConfigDb::" << __func__ << "]" 
00144        << " Vector of FED connections is empty,"
00145        << " therefore cannot add FED connections to local cache!"; 
00146     edm::LogWarning(mlConfigDb_) << ss.str(); 
00147     return; 
00148   }
00149 
00150   SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
00151   SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
00152   for ( ; iter != jter; ++iter ) { if ( partition == iter->second.partitionName() ) { break; } }
00153   if ( iter == dbParams_.partitions().end() ) { 
00154     stringstream ss; 
00155     ss << "[SiStripConfigDb::" << __func__ << "]" 
00156        << " Partition \"" << partition
00157        << "\" not found in partition list, "
00158        << " therefore cannot add FED connections!";
00159     edm::LogWarning(mlConfigDb_) << ss.str(); 
00160     return; 
00161   }
00162   
00163   FedConnectionsRange range = connections_.find( partition );
00164   if ( range == connections_.emptyRange() ) {
00165     
00166     // Make local copy 
00167     FedConnectionsV tmp;
00168     ConnectionFactory::vectorCopyI( tmp, conns, true );
00169     
00170     // Add to local cache
00171     connections_.loadNext( partition, tmp );
00172 
00173     // Some debug
00174     std::stringstream ss;
00175     ss << "[SiStripConfigDb::" << __func__ << "]"
00176        << " Added " << conns.size() 
00177        << " FED connections to local cache for partition \""
00178        << partition << "\"" << std::endl;
00179     ss << "[SiStripConfigDb::" << __func__ << "]"
00180        << " Cache holds FED connections for " 
00181        << connections_.size() << " partitions.";
00182     LogTrace(mlConfigDb_) << ss.str();
00183     
00184   } else {
00185     stringstream ss; 
00186     ss << "[SiStripConfigDb::" << __func__ << "]" 
00187        << " Partition \"" << partition
00188        << "\" already found in local cache, "
00189        << " therefore cannot add new FED connections!";
00190     edm::LogWarning(mlConfigDb_) << ss.str(); 
00191     return; 
00192   }
00193   
00194 }
00195 
00196 // -----------------------------------------------------------------------------
00197 // 
00198 void SiStripConfigDb::uploadFedConnections( std::string partition ) {
00199 
00200   if ( dbParams_.usingDbCache() ) {
00201     edm::LogWarning(mlConfigDb_)
00202       << "[SiStripConfigDb::" << __func__ << "]" 
00203       << " Using database cache! No uploads allowed!"; 
00204     return;
00205   }
00206 
00207   if ( !deviceFactory(__func__) ) { return; }
00208   
00209   if ( connections_.empty() ) { 
00210     stringstream ss; 
00211     ss << "[SiStripConfigDb::" << __func__ << "]" 
00212        << " Found no cached FED connections, therefore no upload!"; 
00213     edm::LogWarning(mlConfigDb_) << ss.str(); 
00214     return; 
00215   }
00216 
00217   if ( dbParams_.usingDb() ) {
00218     
00219     try { 
00220       
00221       SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
00222       SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
00223       for ( ; iter != jter; ++iter ) {
00224 
00225         if ( partition == "" || partition == iter->second.partitionName() ) {
00226 
00227           FedConnectionsRange range = connections_.find( iter->second.partitionName() );
00228           if ( range != connections_.emptyRange() ) {
00229 
00230             FedConnectionsV conns( range.begin(), range.end() );
00231             
00232             SiStripPartition::Versions cabVersion = iter->second.cabVersion();
00233             deviceFactory(__func__)->setConnectionDescriptions( conns,
00234                                                                 iter->second.partitionName(),
00235                                                                 &(cabVersion.first),
00236                                                                 &(cabVersion.second),
00237                                                                 true ); // new major version
00238 
00239             // Some debug
00240             std::stringstream ss;
00241             ss << "[SiStripConfigDb::" << __func__ << "]"
00242                << " Uploaded " << conns.size() 
00243                << " FED connections to database for partition \""
00244                << iter->second.partitionName() << "\".";
00245             LogTrace(mlConfigDb_) << ss.str();
00246 
00247           } else {
00248             stringstream ss; 
00249             ss << "[SiStripConfigDb::" << __func__ << "]" 
00250                << " Vector of FED connections is empty for partition \"" 
00251                << iter->second.partitionName()
00252                << "\", therefore aborting upload for this partition!";
00253             edm::LogWarning(mlConfigDb_) << ss.str(); 
00254             continue; 
00255           }
00256 
00257         } else {
00258           //      stringstream ss; 
00259           //      ss << "[SiStripConfigDb::" << __func__ << "]" 
00260           //         << " Cannot find partition \"" << partition
00261           //         << "\" in cached partitions list: \""
00262           //         << dbParams_.partitionNames( dbParams_.partitionNames() ) 
00263           //         << "\", therefore aborting upload for this partition!";
00264           //      edm::LogWarning(mlConfigDb_) << ss.str(); 
00265         }
00266 
00267       }
00268 
00269     } catch (...) { handleException( __func__ ); }
00270     
00271   }
00272 
00273   allowCalibUpload_ = true;
00274   
00275 }
00276 
00277 // -----------------------------------------------------------------------------
00278 // 
00279 void SiStripConfigDb::clearFedConnections( std::string partition ) {
00280   LogTrace(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]";
00281   
00282   if ( connections_.empty() ) { 
00283     stringstream ss; 
00284     ss << "[SiStripConfigDb::" << __func__ << "]" 
00285        << " Found no cached FED connections!"; 
00286     //edm::LogWarning(mlConfigDb_) << ss.str(); 
00287     return; 
00288   }
00289   
00290   // Reproduce temporary cache for "all partitions except specified one" (or clear all if none specified)
00291   FedConnections temporary_cache;
00292   if ( partition == ""  ) { temporary_cache = FedConnections(); }
00293   else {
00294     SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
00295     SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
00296     for ( ; iter != jter; ++iter ) {
00297       if ( partition != iter->second.partitionName() ) {
00298         FedConnectionsRange range = connections_.find( iter->second.partitionName() );
00299         if ( range != connections_.emptyRange() ) {
00300           temporary_cache.loadNext( partition, FedConnectionsV( range.begin(), range.end() ) );
00301         } else {
00302           //      stringstream ss; 
00303           //      ss << "[SiStripConfigDb::" << __func__ << "]" 
00304           //         << " Cannot find partition \"" << iter->second.partitionName()
00305           //         << "\" in local cache!";
00306           //      edm::LogWarning(mlConfigDb_) << ss.str(); 
00307         }
00308       }
00309     }
00310   }
00311 
00312   // Delete objects in local cache for specified partition (or all if not specified) 
00313   FedConnectionsRange conns;
00314   if ( partition == "" ) { 
00315     if ( !connections_.empty() ) {
00316       conns = FedConnectionsRange( connections_.find( dbParams_.partitions().begin()->second.partitionName() ).begin(),
00317                                    connections_.find( (--(dbParams_.partitions().end()))->second.partitionName() ).end() );
00318     } else { conns = connections_.emptyRange(); }
00319   } else {
00320     SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
00321     SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
00322     for ( ; iter != jter; ++iter ) { if ( partition == iter->second.partitionName() ) { break; } }
00323     conns = connections_.find( iter->second.partitionName() );
00324   }
00325   
00326   if ( conns != connections_.emptyRange() ) {
00327     FedConnectionsV::const_iterator ifed = conns.begin();
00328     FedConnectionsV::const_iterator jfed = conns.end();
00329     for ( ; ifed != jfed; ++ifed ) { if ( *ifed ) { delete *ifed; } }
00330   } else {
00331     stringstream ss; 
00332     ss << "[SiStripConfigDb::" << __func__ << "]";
00333     if ( partition == "" ) { ss << " Found no FED connections in local cache!"; }
00334     else { ss << " Found no FED connections in local cache for partition \"" << partition << "\"!"; }
00335     edm::LogWarning(mlConfigDb_) << ss.str(); 
00336   }
00337   
00338   // Overwrite local cache with temporary cache
00339   connections_ = temporary_cache; 
00340 
00341 }
00342 
00343 // -----------------------------------------------------------------------------
00344 // 
00345 void SiStripConfigDb::printFedConnections( std::string partition ) {
00346 
00347   std::stringstream ss;
00348   ss << "[SiStripConfigDb::" << __func__ << "]"
00349      << " Contents of FedConnections container:" << std::endl;
00350   ss << " Number of partitions: " << connections_.size() << std::endl;
00351   
00352   // Loop through partitions
00353   uint16_t cntr = 0;
00354   FedConnections::const_iterator iconn = connections_.begin();
00355   FedConnections::const_iterator jconn = connections_.end();
00356   for ( ; iconn != jconn; ++iconn ) {
00357 
00358     cntr++;
00359     if ( partition == "" || partition == iconn->first ) {
00360       
00361       ss << "  Partition number   : " << cntr << " (out of " << connections_.size() << ")" << std::endl;
00362       ss << "  Partition name     : \"" << iconn->first << "\"" << std::endl;
00363       ss << "  Num of connections : " << iconn->second.size() << std::endl;
00364 
00365       // Extract FED ids and channels
00366       std::map< uint16_t, vector<uint16_t> > feds;
00367       FedConnectionsV::const_iterator iter = iconn->second.begin();
00368       FedConnectionsV::const_iterator jter = iconn->second.end();
00369       for ( ; iter != jter; ++iter ) { 
00370         if ( *iter ) { 
00371           uint16_t fed_id = (*iter)->getFedId();
00372           uint16_t fed_ch = (*iter)->getFedChannel();
00373           if ( find( feds[fed_id].begin(), feds[fed_id].end(), fed_ch ) == feds[fed_id].end() ) { 
00374             feds[fed_id].push_back( fed_ch ); 
00375           }
00376         }
00377       }
00378       
00379       // Sort contents
00380       std::map< uint16_t, std::vector<uint16_t> > tmp;
00381       std::map< uint16_t, std::vector<uint16_t> >::const_iterator ii = feds.begin();
00382       std::map< uint16_t, std::vector<uint16_t> >::const_iterator jj = feds.end();
00383       for ( ; ii != jj; ++ii ) {
00384         std::vector<uint16_t> temp = ii->second;
00385         std::sort( temp.begin(), temp.end() );
00386         std::vector<uint16_t>::const_iterator iii = temp.begin();
00387         std::vector<uint16_t>::const_iterator jjj = temp.end();
00388         for ( ; iii != jjj; ++iii ) { tmp[ii->first].push_back( *iii ); }
00389       }
00390       feds.clear();
00391       feds = tmp;
00392       
00393       // Print FED ids and channels
00394       std::map< uint16_t, std::vector<uint16_t> >::const_iterator ifed = feds.begin();
00395       std::map< uint16_t, std::vector<uint16_t> >::const_iterator jfed = feds.end();
00396       for ( ; ifed != jfed; ++ifed ) {
00397         ss << "  Found " << std::setw(2) << ifed->second.size()
00398            << " channels for FED id " << std::setw(3) << ifed->first << " : ";
00399         if ( !ifed->second.empty() ) { 
00400           uint16_t first = ifed->second.front();
00401           uint16_t last = ifed->second.front();
00402           std::vector<uint16_t>::const_iterator ichan = ifed->second.begin();
00403           std::vector<uint16_t>::const_iterator jchan = ifed->second.end();
00404           for ( ; ichan != jchan; ++ichan ) { 
00405             if ( ichan != ifed->second.begin() ) {
00406               if ( *ichan != last+1 ) { 
00407                 ss << std::setw(2) << first << "->" << std::setw(2) << last << ", ";
00408                 if ( ichan != ifed->second.end() ) { first = *(ichan+1); }
00409               } 
00410             }
00411             last = *ichan;
00412           }
00413           if ( first != last ) { ss << std::setw(2) << first << "->" << std::setw(2) << last; }
00414           ss << std::endl;
00415         }
00416       }
00417       
00418     }
00419     
00420   }
00421   
00422   LogTrace(mlConfigDb_) << ss.str();
00423 
00424 }