CMS 3D CMS Logo

FedConnections.cc

Go to the documentation of this file.
00001 // Last commit: $Id: FedConnections.cc,v 1.30 2008/06/06 14:48:53 bainbrid 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 #ifdef USING_NEW_DATABASE_MODEL
00038             
00039             // Retrieve connections
00040             FedConnectionsV tmp1;
00041             deviceFactory(__func__)->getConnectionDescriptions( iter->second.partitionName(), 
00042                                                                 tmp1,
00043                                                                 iter->second.cabVersion().first,
00044                                                                 iter->second.cabVersion().second,
00045                                                                 false ); //@@ do not get DISABLED connections
00046             
00047             // Make local copy 
00048             ConnectionFactory::vectorCopyI( tmp2, tmp1, true );
00049             
00050 #else // USING_NEW_DATABASE_MODEL
00051             
00052             static bool once = true;
00053             if ( once ) { deviceFactory(__func__)->createInputDBAccess(); once = false; }
00054             
00055             deviceFactory(__func__)->setInputDBVersion( iter->second.partitionName(),
00056                                                         iter->second.cabVersion().first,
00057                                                         iter->second.cabVersion().second );
00058             
00059             for ( uint16_t iconn = 0; iconn < deviceFactory(__func__)->getNumberOfFedChannel(); iconn++ ) {
00060               tmp2.push_back( deviceFactory(__func__)->getFedChannelConnection( iconn ) ); 
00061             }
00062             
00063 #endif // USING_NEW_DATABASE_MODEL
00064             
00065             // Add to cache
00066             connections_.loadNext( iter->second.partitionName(), tmp2 );
00067             
00068             // Some debug
00069             FedConnectionsRange conns = connections_.find( iter->second.partitionName() );
00070             std::stringstream ss;
00071             ss << "[SiStripConfigDb::" << __func__ << "]"
00072                << " Downloaded " << conns.size() 
00073                << " FED connections to local cache for partition \""
00074                << iter->second.partitionName() << "\"" << std::endl;
00075             ss << "[SiStripConfigDb::" << __func__ << "]"
00076                << " Cache holds FED connections for " 
00077                << connections_.size() << " partitions.";
00078             LogTrace(mlConfigDb_) << ss.str();
00079             
00080           }
00081           
00082         }
00083 
00084       }
00085 
00086     } else { // Use database cache
00087         
00088 #ifdef USING_NEW_DATABASE_MODEL
00089         
00090       FedConnectionsV* tmp1 = databaseCache(__func__)->getConnections();
00091       
00092       if ( tmp1 ) { 
00093         
00094         // Make local copy 
00095         FedConnectionsV tmp2;
00096         ConnectionFactory::vectorCopyI( tmp2, *tmp1, true );
00097         
00098         // Add to cache
00099         connections_.loadNext( SiStripPartition::defaultPartitionName_, tmp2 );
00100         
00101       } else {
00102         edm::LogWarning(mlConfigDb_)
00103           << "[SiStripConfigDb::" << __func__ << "]"
00104           << " NULL pointer to FedConnections vector!";
00105       }
00106     
00107 #endif // USING_NEW_DATABASE_MODEL
00108       
00109     }
00110     
00111   } catch (...) { handleException( __func__ ); }
00112   
00113   // Create range object
00114   uint16_t np = 0;
00115   uint16_t nc = 0;
00116   FedConnectionsRange conns;
00117   if ( partition != "" ) { 
00118     conns = connections_.find( partition );
00119     np = 1;
00120     nc = conns.size();
00121   } else { 
00122     if ( !connections_.empty() ) {
00123       conns = FedConnectionsRange( connections_.find( dbParams_.partitions().begin()->second.partitionName() ).begin(),
00124                                    connections_.find( (--(dbParams_.partitions().end()))->second.partitionName() ).end() );
00125     } else { conns = connections_.emptyRange(); }
00126     np = connections_.size();
00127     nc = conns.size();
00128   }
00129   
00130   stringstream ss; 
00131   ss << "[SiStripConfigDb::" << __func__ << "]"
00132      << " Found " << nc << " FED connections";
00133   if ( !dbParams_.usingDb() ) { ss << " in " << dbParams_.inputModuleXmlFiles().size() << " 'module.xml' file(s)"; }
00134   else { if ( !dbParams_.usingDbCache() )  { ss << " in " << np << " database partition(s)"; } 
00135   else { ss << " from shared memory name '" << dbParams_.sharedMemory() << "'"; } }
00136   if ( connections_.empty() ) { edm::LogWarning(mlConfigDb_) << ss.str(); }
00137   else { LogTrace(mlConfigDb_) << ss.str(); }
00138 
00139   return conns;
00140   
00141 }
00142 
00143 // -----------------------------------------------------------------------------
00144 // 
00145 void SiStripConfigDb::addFedConnections( std::string partition, FedConnectionsV& conns ) {
00146 
00147   if ( !deviceFactory(__func__) ) { return; }
00148 
00149   if ( partition.empty() ) { 
00150     stringstream ss; 
00151     ss << "[SiStripConfigDb::" << __func__ << "]" 
00152        << " Partition string is empty,"
00153        << " therefore cannot add FED connections to local cache!"; 
00154     edm::LogWarning(mlConfigDb_) << ss.str(); 
00155     return; 
00156   }
00157   
00158   if ( conns.empty() ) { 
00159     stringstream ss; 
00160     ss << "[SiStripConfigDb::" << __func__ << "]" 
00161        << " Vector of FED connections is empty,"
00162        << " therefore cannot add FED connections to local cache!"; 
00163     edm::LogWarning(mlConfigDb_) << ss.str(); 
00164     return; 
00165   }
00166 
00167   SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
00168   SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
00169   for ( ; iter != jter; ++iter ) { if ( partition == iter->second.partitionName() ) { break; } }
00170   if ( iter == dbParams_.partitions().end() ) { 
00171     stringstream ss; 
00172     ss << "[SiStripConfigDb::" << __func__ << "]" 
00173        << " Partition \"" << partition
00174        << "\" not found in partition list, "
00175        << " therefore cannot add FED connections!";
00176     edm::LogWarning(mlConfigDb_) << ss.str(); 
00177     return; 
00178   }
00179   
00180   FedConnectionsRange range = connections_.find( partition );
00181   if ( range == connections_.emptyRange() ) {
00182     
00183     // Make local copy 
00184     FedConnectionsV tmp;
00185 #ifdef USING_NEW_DATABASE_MODEL
00186     ConnectionFactory::vectorCopyI( tmp, conns, true );
00187 #else
00188     tmp = conns;
00189 #endif
00190     
00191     // Add to local cache
00192     connections_.loadNext( partition, tmp );
00193 
00194     // Some debug
00195     std::stringstream ss;
00196     ss << "[SiStripConfigDb::" << __func__ << "]"
00197        << " Added " << conns.size() 
00198        << " FED connections to local cache for partition \""
00199        << partition << "\"" << std::endl;
00200     ss << "[SiStripConfigDb::" << __func__ << "]"
00201        << " Cache holds FED connections for " 
00202        << connections_.size() << " partitions.";
00203     LogTrace(mlConfigDb_) << ss.str();
00204     
00205   } else {
00206     stringstream ss; 
00207     ss << "[SiStripConfigDb::" << __func__ << "]" 
00208        << " Partition \"" << partition
00209        << "\" already found in local cache, "
00210        << " therefore cannot add new FED connections!";
00211     edm::LogWarning(mlConfigDb_) << ss.str(); 
00212     return; 
00213   }
00214   
00215 }
00216 
00217 // -----------------------------------------------------------------------------
00218 // 
00219 void SiStripConfigDb::uploadFedConnections( std::string partition ) {
00220 
00221   if ( dbParams_.usingDbCache() ) {
00222     edm::LogWarning(mlConfigDb_)
00223       << "[SiStripConfigDb::" << __func__ << "]" 
00224       << " Using database cache! No uploads allowed!"; 
00225     return;
00226   }
00227 
00228   if ( !deviceFactory(__func__) ) { return; }
00229   
00230   if ( connections_.empty() ) { 
00231     stringstream ss; 
00232     ss << "[SiStripConfigDb::" << __func__ << "]" 
00233        << " Found no cached FED connections, therefore no upload!"; 
00234     edm::LogWarning(mlConfigDb_) << ss.str(); 
00235     return; 
00236   }
00237 
00238   if ( dbParams_.usingDb() ) {
00239     
00240     try { 
00241       
00242       SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
00243       SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
00244       for ( ; iter != jter; ++iter ) {
00245 
00246         if ( partition == "" || partition == iter->second.partitionName() ) {
00247 
00248           FedConnectionsRange range = connections_.find( iter->second.partitionName() );
00249           if ( range != connections_.emptyRange() ) {
00250 
00251             FedConnectionsV conns( range.begin(), range.end() );
00252             
00253 #ifdef USING_NEW_DATABASE_MODEL
00254             deviceFactory(__func__)->setConnectionDescriptions( conns,
00255                                                                 iter->second.partitionName(),
00256                                                                 &(iter->second.cabVersion().first),
00257                                                                 &(iter->second.cabVersion().second),
00258                                                                 true ); // new major version
00259 #else 
00260             FedConnectionsV::iterator ifed = conns.begin();
00261             FedConnectionsV::iterator jfed = conns.end();
00262             for ( ; ifed != jfed; ++ifed ) {
00263               deviceFactory(__func__)->addFedChannelConnection( *ifed );
00264             }
00265             deviceFactory(__func__)->upload();
00266 #endif
00267 
00268             // Some debug
00269             std::stringstream ss;
00270             ss << "[SiStripConfigDb::" << __func__ << "]"
00271                << " Uploaded " << conns.size() 
00272                << " FED connections to database for partition \""
00273                << iter->second.partitionName() << "\".";
00274             LogTrace(mlConfigDb_) << ss.str();
00275 
00276           } else {
00277             stringstream ss; 
00278             ss << "[SiStripConfigDb::" << __func__ << "]" 
00279                << " Vector of FED connections is empty for partition \"" 
00280                << iter->second.partitionName()
00281                << "\", therefore aborting upload for this partition!";
00282             edm::LogWarning(mlConfigDb_) << ss.str(); 
00283             continue; 
00284           }
00285 
00286         } else {
00287           //      stringstream ss; 
00288           //      ss << "[SiStripConfigDb::" << __func__ << "]" 
00289           //         << " Cannot find partition \"" << partition
00290           //         << "\" in cached partitions list: \""
00291           //         << dbParams_.partitionNames( dbParams_.partitionNames() ) 
00292           //         << "\", therefore aborting upload for this partition!";
00293           //      edm::LogWarning(mlConfigDb_) << ss.str(); 
00294         }
00295 
00296       }
00297 
00298     } catch (...) { handleException( __func__ ); }
00299     
00300   } else {
00301     
00302 #ifndef USING_NEW_DATABASE_MODEL
00303 
00304     ofstream out( dbParams_.outputModuleXml().c_str() );
00305     out << "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>" << endl
00306         << "<!DOCTYPE TrackerDescription SYSTEM \"https://cmsdoc.cern.ch/cms/cmt/System_aspects/Daq/dtd/trackerdescription.dtd\">" << endl
00307         << "<TrackerDescription>" << endl
00308         << "<FedChannelList>" << endl;
00309     SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
00310     SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
00311     for ( ; iter != jter; ++iter ) {
00312       if ( partition == "" || partition == iter->second.partitionName() ) {
00313         FedConnectionsRange range = connections_.find( iter->second.partitionName() );
00314         if ( range != connections_.emptyRange() ) {
00315           FedConnectionsV::const_iterator ifed = range.begin();
00316           FedConnectionsV::const_iterator jfed = range.end();
00317           for ( ; ifed != jfed; ++ifed ) { (*ifed)->toXML(out); out << endl; }
00318         }
00319       }
00320     }
00321     out << "</FedChannelList>" << endl
00322         << "</TrackerDescription>" << endl;
00323     out.close();
00324 
00325 #endif
00326 
00327   }
00328 
00329   allowCalibUpload_ = true;
00330   
00331 }
00332 
00333 // -----------------------------------------------------------------------------
00334 // 
00335 void SiStripConfigDb::clearFedConnections( std::string partition ) {
00336   LogTrace(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]";
00337   
00338   if ( connections_.empty() ) { 
00339     stringstream ss; 
00340     ss << "[SiStripConfigDb::" << __func__ << "]" 
00341        << " Found no cached FED connections!"; 
00342     //edm::LogWarning(mlConfigDb_) << ss.str(); 
00343     return; 
00344   }
00345   
00346   // Reproduce temporary cache for "all partitions except specified one" (or clear all if none specified)
00347   FedConnections temporary_cache;
00348   if ( partition == ""  ) { temporary_cache = FedConnections(); }
00349   else {
00350     SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
00351     SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
00352     for ( ; iter != jter; ++iter ) {
00353       if ( partition != iter->second.partitionName() ) {
00354         FedConnectionsRange range = connections_.find( iter->second.partitionName() );
00355         if ( range != connections_.emptyRange() ) {
00356           temporary_cache.loadNext( partition, FedConnectionsV( range.begin(), range.end() ) );
00357         } else {
00358           //      stringstream ss; 
00359           //      ss << "[SiStripConfigDb::" << __func__ << "]" 
00360           //         << " Cannot find partition \"" << iter->second.partitionName()
00361           //         << "\" in local cache!";
00362           //      edm::LogWarning(mlConfigDb_) << ss.str(); 
00363         }
00364       }
00365     }
00366   }
00367 
00368   // Delete objects in local cache for specified partition (or all if not specified) 
00369   FedConnectionsRange conns;
00370   if ( partition == "" ) { 
00371     if ( !connections_.empty() ) {
00372       conns = FedConnectionsRange( connections_.find( dbParams_.partitions().begin()->second.partitionName() ).begin(),
00373                                    connections_.find( (--(dbParams_.partitions().end()))->second.partitionName() ).end() );
00374     } else { conns = connections_.emptyRange(); }
00375   } else {
00376     SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
00377     SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
00378     for ( ; iter != jter; ++iter ) { if ( partition == iter->second.partitionName() ) { break; } }
00379     conns = connections_.find( iter->second.partitionName() );
00380   }
00381   
00382   if ( conns != connections_.emptyRange() ) {
00383 
00384 #ifdef USING_NEW_DATABASE_MODEL 
00385     FedConnectionsV::const_iterator ifed = conns.begin();
00386     FedConnectionsV::const_iterator jfed = conns.end();
00387     for ( ; ifed != jfed; ++ifed ) { if ( *ifed ) { delete *ifed; } }
00388 #endif
00389     
00390   } else {
00391     stringstream ss; 
00392     ss << "[SiStripConfigDb::" << __func__ << "]";
00393     if ( partition == "" ) { ss << " Found no FED connections in local cache!"; }
00394     else { ss << " Found no FED connections in local cache for partition \"" << partition << "\"!"; }
00395     edm::LogWarning(mlConfigDb_) << ss.str(); 
00396   }
00397   
00398   // Overwrite local cache with temporary cache
00399   connections_ = temporary_cache; 
00400 
00401 }
00402 
00403 // -----------------------------------------------------------------------------
00404 // 
00405 void SiStripConfigDb::printFedConnections( std::string partition ) {
00406 
00407   std::stringstream ss;
00408   ss << "[SiStripConfigDb::" << __func__ << "]"
00409      << " Contents of FedConnections container:" << std::endl;
00410   ss << " Number of partitions: " << connections_.size() << std::endl;
00411   
00412   // Loop through partitions
00413   uint16_t cntr = 0;
00414   FedConnections::const_iterator iconn = connections_.begin();
00415   FedConnections::const_iterator jconn = connections_.end();
00416   for ( ; iconn != jconn; ++iconn ) {
00417 
00418     cntr++;
00419     if ( partition == "" || partition == iconn->first ) {
00420       
00421       ss << "  Partition number   : " << cntr << " (out of " << connections_.size() << ")" << std::endl;
00422       ss << "  Partition name     : \"" << iconn->first << "\"" << std::endl;
00423       ss << "  Num of connections : " << iconn->second.size() << std::endl;
00424 
00425       // Extract FED ids and channels
00426       std::map< uint16_t, vector<uint16_t> > feds;
00427       FedConnectionsV::const_iterator iter = iconn->second.begin();
00428       FedConnectionsV::const_iterator jter = iconn->second.end();
00429       for ( ; iter != jter; ++iter ) { 
00430         if ( *iter ) { 
00431           uint16_t fed_id = (*iter)->getFedId();
00432           uint16_t fed_ch = (*iter)->getFedChannel();
00433           if ( find( feds[fed_id].begin(), feds[fed_id].end(), fed_ch ) == feds[fed_id].end() ) { 
00434             feds[fed_id].push_back( fed_ch ); 
00435           }
00436         }
00437       }
00438       
00439       // Sort contents
00440       std::map< uint16_t, std::vector<uint16_t> > tmp;
00441       std::map< uint16_t, std::vector<uint16_t> >::const_iterator ii = feds.begin();
00442       std::map< uint16_t, std::vector<uint16_t> >::const_iterator jj = feds.end();
00443       for ( ; ii != jj; ++ii ) {
00444         std::vector<uint16_t> temp = ii->second;
00445         std::sort( temp.begin(), temp.end() );
00446         std::vector<uint16_t>::const_iterator iii = temp.begin();
00447         std::vector<uint16_t>::const_iterator jjj = temp.end();
00448         for ( ; iii != jjj; ++iii ) { tmp[ii->first].push_back( *iii ); }
00449       }
00450       feds.clear();
00451       feds = tmp;
00452       
00453       // Print FED ids and channels
00454       std::map< uint16_t, std::vector<uint16_t> >::const_iterator ifed = feds.begin();
00455       std::map< uint16_t, std::vector<uint16_t> >::const_iterator jfed = feds.end();
00456       for ( ; ifed != jfed; ++ifed ) {
00457         ss << "  Found " << std::setw(2) << ifed->second.size()
00458            << " channels for FED id " << std::setw(3) << ifed->first << " : ";
00459         if ( !ifed->second.empty() ) { 
00460           uint16_t first = ifed->second.front();
00461           uint16_t last = ifed->second.front();
00462           std::vector<uint16_t>::const_iterator ichan = ifed->second.begin();
00463           std::vector<uint16_t>::const_iterator jchan = ifed->second.end();
00464           for ( ; ichan != jchan; ++ichan ) { 
00465             if ( ichan != ifed->second.begin() ) {
00466               if ( *ichan != last+1 ) { 
00467                 ss << std::setw(2) << first << "->" << std::setw(2) << last << ", ";
00468                 if ( ichan != ifed->second.end() ) { first = *(ichan+1); }
00469               } 
00470             }
00471             last = *ichan;
00472           }
00473           if ( first != last ) { ss << std::setw(2) << first << "->" << std::setw(2) << last; }
00474           ss << std::endl;
00475         }
00476       }
00477       
00478     }
00479     
00480   }
00481   
00482   LogTrace(mlConfigDb_) << ss.str();
00483 
00484 }

Generated on Tue Jun 9 17:40:51 2009 for CMSSW by  doxygen 1.5.4