CMS 3D CMS Logo

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

Go to the documentation of this file.
00001 // Last commit: $Id: FedConnections.cc,v 1.33 2009/04/06 16:57:28 lowette 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             deviceFactory(__func__)->setConnectionDescriptions( conns,
00233                                                                 iter->second.partitionName(),
00234                                                                 &(iter->second.cabVersion().first),
00235                                                                 &(iter->second.cabVersion().second),
00236                                                                 true ); // new major version
00237 
00238             // Some debug
00239             std::stringstream ss;
00240             ss << "[SiStripConfigDb::" << __func__ << "]"
00241                << " Uploaded " << conns.size() 
00242                << " FED connections to database for partition \""
00243                << iter->second.partitionName() << "\".";
00244             LogTrace(mlConfigDb_) << ss.str();
00245 
00246           } else {
00247             stringstream ss; 
00248             ss << "[SiStripConfigDb::" << __func__ << "]" 
00249                << " Vector of FED connections is empty for partition \"" 
00250                << iter->second.partitionName()
00251                << "\", therefore aborting upload for this partition!";
00252             edm::LogWarning(mlConfigDb_) << ss.str(); 
00253             continue; 
00254           }
00255 
00256         } else {
00257           //      stringstream ss; 
00258           //      ss << "[SiStripConfigDb::" << __func__ << "]" 
00259           //         << " Cannot find partition \"" << partition
00260           //         << "\" in cached partitions list: \""
00261           //         << dbParams_.partitionNames( dbParams_.partitionNames() ) 
00262           //         << "\", therefore aborting upload for this partition!";
00263           //      edm::LogWarning(mlConfigDb_) << ss.str(); 
00264         }
00265 
00266       }
00267 
00268     } catch (...) { handleException( __func__ ); }
00269     
00270   }
00271 
00272   allowCalibUpload_ = true;
00273   
00274 }
00275 
00276 // -----------------------------------------------------------------------------
00277 // 
00278 void SiStripConfigDb::clearFedConnections( std::string partition ) {
00279   LogTrace(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]";
00280   
00281   if ( connections_.empty() ) { 
00282     stringstream ss; 
00283     ss << "[SiStripConfigDb::" << __func__ << "]" 
00284        << " Found no cached FED connections!"; 
00285     //edm::LogWarning(mlConfigDb_) << ss.str(); 
00286     return; 
00287   }
00288   
00289   // Reproduce temporary cache for "all partitions except specified one" (or clear all if none specified)
00290   FedConnections temporary_cache;
00291   if ( partition == ""  ) { temporary_cache = FedConnections(); }
00292   else {
00293     SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
00294     SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
00295     for ( ; iter != jter; ++iter ) {
00296       if ( partition != iter->second.partitionName() ) {
00297         FedConnectionsRange range = connections_.find( iter->second.partitionName() );
00298         if ( range != connections_.emptyRange() ) {
00299           temporary_cache.loadNext( partition, FedConnectionsV( range.begin(), range.end() ) );
00300         } else {
00301           //      stringstream ss; 
00302           //      ss << "[SiStripConfigDb::" << __func__ << "]" 
00303           //         << " Cannot find partition \"" << iter->second.partitionName()
00304           //         << "\" in local cache!";
00305           //      edm::LogWarning(mlConfigDb_) << ss.str(); 
00306         }
00307       }
00308     }
00309   }
00310 
00311   // Delete objects in local cache for specified partition (or all if not specified) 
00312   FedConnectionsRange conns;
00313   if ( partition == "" ) { 
00314     if ( !connections_.empty() ) {
00315       conns = FedConnectionsRange( connections_.find( dbParams_.partitions().begin()->second.partitionName() ).begin(),
00316                                    connections_.find( (--(dbParams_.partitions().end()))->second.partitionName() ).end() );
00317     } else { conns = connections_.emptyRange(); }
00318   } else {
00319     SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
00320     SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
00321     for ( ; iter != jter; ++iter ) { if ( partition == iter->second.partitionName() ) { break; } }
00322     conns = connections_.find( iter->second.partitionName() );
00323   }
00324   
00325   if ( conns != connections_.emptyRange() ) {
00326     FedConnectionsV::const_iterator ifed = conns.begin();
00327     FedConnectionsV::const_iterator jfed = conns.end();
00328     for ( ; ifed != jfed; ++ifed ) { if ( *ifed ) { delete *ifed; } }
00329   } else {
00330     stringstream ss; 
00331     ss << "[SiStripConfigDb::" << __func__ << "]";
00332     if ( partition == "" ) { ss << " Found no FED connections in local cache!"; }
00333     else { ss << " Found no FED connections in local cache for partition \"" << partition << "\"!"; }
00334     edm::LogWarning(mlConfigDb_) << ss.str(); 
00335   }
00336   
00337   // Overwrite local cache with temporary cache
00338   connections_ = temporary_cache; 
00339 
00340 }
00341 
00342 // -----------------------------------------------------------------------------
00343 // 
00344 void SiStripConfigDb::printFedConnections( std::string partition ) {
00345 
00346   std::stringstream ss;
00347   ss << "[SiStripConfigDb::" << __func__ << "]"
00348      << " Contents of FedConnections container:" << std::endl;
00349   ss << " Number of partitions: " << connections_.size() << std::endl;
00350   
00351   // Loop through partitions
00352   uint16_t cntr = 0;
00353   FedConnections::const_iterator iconn = connections_.begin();
00354   FedConnections::const_iterator jconn = connections_.end();
00355   for ( ; iconn != jconn; ++iconn ) {
00356 
00357     cntr++;
00358     if ( partition == "" || partition == iconn->first ) {
00359       
00360       ss << "  Partition number   : " << cntr << " (out of " << connections_.size() << ")" << std::endl;
00361       ss << "  Partition name     : \"" << iconn->first << "\"" << std::endl;
00362       ss << "  Num of connections : " << iconn->second.size() << std::endl;
00363 
00364       // Extract FED ids and channels
00365       std::map< uint16_t, vector<uint16_t> > feds;
00366       FedConnectionsV::const_iterator iter = iconn->second.begin();
00367       FedConnectionsV::const_iterator jter = iconn->second.end();
00368       for ( ; iter != jter; ++iter ) { 
00369         if ( *iter ) { 
00370           uint16_t fed_id = (*iter)->getFedId();
00371           uint16_t fed_ch = (*iter)->getFedChannel();
00372           if ( find( feds[fed_id].begin(), feds[fed_id].end(), fed_ch ) == feds[fed_id].end() ) { 
00373             feds[fed_id].push_back( fed_ch ); 
00374           }
00375         }
00376       }
00377       
00378       // Sort contents
00379       std::map< uint16_t, std::vector<uint16_t> > tmp;
00380       std::map< uint16_t, std::vector<uint16_t> >::const_iterator ii = feds.begin();
00381       std::map< uint16_t, std::vector<uint16_t> >::const_iterator jj = feds.end();
00382       for ( ; ii != jj; ++ii ) {
00383         std::vector<uint16_t> temp = ii->second;
00384         std::sort( temp.begin(), temp.end() );
00385         std::vector<uint16_t>::const_iterator iii = temp.begin();
00386         std::vector<uint16_t>::const_iterator jjj = temp.end();
00387         for ( ; iii != jjj; ++iii ) { tmp[ii->first].push_back( *iii ); }
00388       }
00389       feds.clear();
00390       feds = tmp;
00391       
00392       // Print FED ids and channels
00393       std::map< uint16_t, std::vector<uint16_t> >::const_iterator ifed = feds.begin();
00394       std::map< uint16_t, std::vector<uint16_t> >::const_iterator jfed = feds.end();
00395       for ( ; ifed != jfed; ++ifed ) {
00396         ss << "  Found " << std::setw(2) << ifed->second.size()
00397            << " channels for FED id " << std::setw(3) << ifed->first << " : ";
00398         if ( !ifed->second.empty() ) { 
00399           uint16_t first = ifed->second.front();
00400           uint16_t last = ifed->second.front();
00401           std::vector<uint16_t>::const_iterator ichan = ifed->second.begin();
00402           std::vector<uint16_t>::const_iterator jchan = ifed->second.end();
00403           for ( ; ichan != jchan; ++ichan ) { 
00404             if ( ichan != ifed->second.begin() ) {
00405               if ( *ichan != last+1 ) { 
00406                 ss << std::setw(2) << first << "->" << std::setw(2) << last << ", ";
00407                 if ( ichan != ifed->second.end() ) { first = *(ichan+1); }
00408               } 
00409             }
00410             last = *ichan;
00411           }
00412           if ( first != last ) { ss << std::setw(2) << first << "->" << std::setw(2) << last; }
00413           ss << std::endl;
00414         }
00415       }
00416       
00417     }
00418     
00419   }
00420   
00421   LogTrace(mlConfigDb_) << ss.str();
00422 
00423 }