CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/OnlineDB/SiStripConfigDb/src/FedDescriptions.cc

Go to the documentation of this file.
00001 // Last commit: $Id: FedDescriptions.cc,v 1.32 2009/04/06 16:57:28 lowette Exp $
00002 
00003 #include "OnlineDB/SiStripConfigDb/interface/SiStripConfigDb.h"
00004 #include "CondFormats/SiStripObjects/interface/SiStripFedCabling.h"
00005 #include "CondFormats/SiStripObjects/interface/FedChannelConnection.h"
00006 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00007 
00008 using namespace std;
00009 using namespace sistrip;
00010 
00011 // -----------------------------------------------------------------------------
00012 // 
00013 SiStripConfigDb::FedDescriptionsRange SiStripConfigDb::getFedDescriptions( std::string partition ) {
00014 
00015   // Check
00016   if ( ( !dbParams_.usingDbCache() && !deviceFactory(__func__) ) ||
00017        (  dbParams_.usingDbCache() && !databaseCache(__func__) ) ) { 
00018     return feds_.emptyRange(); 
00019   }
00020 
00021   try {
00022 
00023     if ( !dbParams_.usingDbCache() ) { 
00024 
00025       SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
00026       SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
00027       for ( ; iter != jter; ++iter ) {
00028         
00029         if ( partition == "" || partition == iter->second.partitionName() ) {
00030 
00031           if ( iter->second.partitionName() == SiStripPartition::defaultPartitionName_ ) { continue; }
00032 
00033           FedDescriptionsRange range = feds_.find( iter->second.partitionName() );
00034           if ( range == feds_.emptyRange() ) {
00035 
00036             // Extract versions
00037             deviceFactory(__func__)->setUsingStrips( usingStrips_ );
00038             int16_t major = iter->second.fedVersion().first; 
00039             int16_t minor = iter->second.fedVersion().second; 
00040             if ( iter->second.fedVersion().first == 0 && 
00041                  iter->second.fedVersion().second == 0 ) {
00042               major = -1; //@@ "current state" for fed factory!
00043               minor = -1; //@@ "current state" for fed factory!
00044             }
00045 
00046             // Retrive FED descriptions
00047             FedDescriptionsV tmp1;
00048             tmp1 = *( deviceFactory(__func__)->getFed9UDescriptions( iter->second.partitionName(), 
00049                                                                      major, 
00050                                                                      minor ) );
00051             
00052             // Make local copy 
00053             FedDescriptionsV tmp2;
00054             Fed9U::Fed9UDeviceFactory::vectorCopy( tmp2, tmp1 );
00055             
00056             // Add to cache
00057             feds_.loadNext( iter->second.partitionName(), tmp2 );
00058 
00059             // Some debug
00060             FedDescriptionsRange feds = feds_.find( iter->second.partitionName() );
00061             std::stringstream ss;
00062             ss << "[SiStripConfigDb::" << __func__ << "]"
00063                << " Downloaded " << feds.size() 
00064                << " FED descriptions to local cache for partition \""
00065                << iter->second.partitionName() << "\"" << std::endl;
00066             ss << "[SiStripConfigDb::" << __func__ << "]"
00067                << " Cache holds FED descriptions for " 
00068                << feds_.size() << " partitions.";
00069             LogTrace(mlConfigDb_) << ss.str();
00070             
00071           }
00072 
00073         }
00074 
00075       }
00076             
00077     } else { // Using database cache
00078 
00079       FedDescriptionsV* tmp1 = databaseCache(__func__)->getFed9UDescriptions();
00080       
00081       if ( tmp1 ) { 
00082         
00083         // Make local copy 
00084         FedDescriptionsV tmp2;
00085         Fed9U::Fed9UDeviceFactory::vectorCopy( tmp2, *tmp1 );
00086         
00087         // Add to cache
00088         feds_.loadNext( SiStripPartition::defaultPartitionName_, tmp2 );
00089         
00090       } else {
00091         edm::LogWarning(mlConfigDb_)
00092           << "[SiStripConfigDb::" << __func__ << "]"
00093           << " NULL pointer to FED descriptions vector!";
00094       }
00095 
00096     }
00097 
00098   } catch (... ) { handleException( __func__ ); }
00099   
00100   // Create range object
00101   uint16_t np = 0;
00102   uint16_t nc = 0;
00103   FedDescriptionsRange feds;
00104   if ( partition != "" ) { 
00105     feds = feds_.find( partition );
00106     np = 1;
00107     nc = feds.size();
00108   } else {  
00109     if ( !feds_.empty() ) {
00110       feds = FedDescriptionsRange( feds_.find( dbParams_.partitions().begin()->second.partitionName() ).begin(),
00111                                    feds_.find( (--(dbParams_.partitions().end()))->second.partitionName() ).end() );
00112     } else { feds = feds_.emptyRange(); }
00113     np = feds_.size();
00114     nc = feds.size();
00115   }
00116   
00117   stringstream ss; 
00118   ss << "[SiStripConfigDb::" << __func__ << "]"
00119      << " Found " << nc << " FED descriptions";
00120   if ( !dbParams_.usingDb() ) { ss << " in " << dbParams_.inputFedXmlFiles().size() << " 'fed.xml' file(s)"; }
00121   else { if ( !dbParams_.usingDbCache() )  { ss << " in " << np << " database partition(s)"; } 
00122   else { ss << " from shared memory name '" << dbParams_.sharedMemory() << "'"; } }
00123   if ( feds_.empty() ) { edm::LogWarning(mlConfigDb_) << ss.str(); }
00124   else { LogTrace(mlConfigDb_) << ss.str(); }
00125   
00126   return feds;
00127   
00128 }
00129 
00130 // -----------------------------------------------------------------------------
00131 // 
00132 void SiStripConfigDb::addFedDescriptions( std::string partition, FedDescriptionsV& feds ) {
00133 
00134   if ( !deviceFactory(__func__) ) { return; }
00135 
00136   if ( partition.empty() ) { 
00137     stringstream ss; 
00138     ss << "[SiStripConfigDb::" << __func__ << "]" 
00139        << " Partition string is empty,"
00140        << " therefore cannot add FED descriptions to local cache!"; 
00141     edm::LogWarning(mlConfigDb_) << ss.str(); 
00142     return; 
00143   }
00144   
00145   if ( feds.empty() ) { 
00146     stringstream ss; 
00147     ss << "[SiStripConfigDb::" << __func__ << "]" 
00148        << " Vector of FED descriptions is empty,"
00149        << " therefore cannot add FED descriptions to local cache!"; 
00150     edm::LogWarning(mlConfigDb_) << ss.str(); 
00151     return; 
00152   }
00153 
00154   SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
00155   SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
00156   for ( ; iter != jter; ++iter ) { if ( partition == iter->second.partitionName() ) { break; } }
00157   if ( iter == dbParams_.partitions().end() ) { 
00158     stringstream ss; 
00159     ss << "[SiStripConfigDb::" << __func__ << "]" 
00160        << " Partition \"" << partition
00161        << "\" not found in partition list, "
00162        << " therefore cannot add FED descriptions!";
00163     edm::LogWarning(mlConfigDb_) << ss.str(); 
00164     return; 
00165   }
00166   
00167   FedDescriptionsRange range = feds_.find( partition );
00168   if ( range == feds_.emptyRange() ) {
00169     
00170     // Make local copy 
00171     FedDescriptionsV tmp;
00172     Fed9U::Fed9UDeviceFactory::vectorCopy( tmp, feds );
00173 
00174     // Add to local cache
00175     feds_.loadNext( partition, tmp );
00176     
00177     // Some debug
00178     std::stringstream ss;
00179     ss << "[SiStripConfigDb::" << __func__ << "]"
00180        << " Added " << feds.size() 
00181        << " FED descriptions to local cache for partition \""
00182        << iter->second.partitionName() << "\"" << std::endl;
00183     ss << "[SiStripConfigDb::" << __func__ << "]"
00184        << " Cache holds FED descriptions for " 
00185        << feds_.size() << " partitions.";
00186     LogTrace(mlConfigDb_) << ss.str();
00187     
00188   } else {
00189     stringstream ss; 
00190     ss << "[SiStripConfigDb::" << __func__ << "]" 
00191        << " Partition \"" << partition
00192        << "\" already found in local cache, "
00193        << " therefore cannot add new FED descriptions!";
00194     edm::LogWarning(mlConfigDb_) << ss.str(); 
00195     return; 
00196   }
00197   
00198 }
00199 
00200 // -----------------------------------------------------------------------------
00201 // 
00202 void SiStripConfigDb::uploadFedDescriptions( std::string partition ) { 
00203   
00204   if ( dbParams_.usingDbCache() ) {
00205     edm::LogWarning(mlConfigDb_)
00206       << "[SiStripConfigDb::" << __func__ << "]" 
00207       << " Using database cache! No uploads allowed!"; 
00208     return;
00209   }
00210   
00211   if ( !deviceFactory(__func__) ) { return; }
00212   
00213   if ( feds_.empty() ) { 
00214     stringstream ss; 
00215     ss << "[SiStripConfigDb::" << __func__ << "]" 
00216        << " Found no cached FED descriptions, therefore no upload!"; 
00217     edm::LogWarning(mlConfigDb_) << ss.str(); 
00218     return; 
00219   }
00220   
00221   try { 
00222 
00223     SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
00224     SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
00225     for ( ; iter != jter; ++iter ) {
00226       
00227       if ( partition == "" || partition == iter->second.partitionName() ) {
00228         
00229         FedDescriptionsRange range = feds_.find( iter->second.partitionName() );
00230         if ( range != feds_.emptyRange() ) {
00231           
00232           FedDescriptionsV feds( range.begin(), range.end() );
00233           
00234           deviceFactory(__func__)->setFed9UDescriptions( feds,
00235                                                          iter->second.partitionName(),
00236                                                          (uint16_t*)(&iter->second.fedVersion().first), 
00237                                                          (uint16_t*)(&iter->second.fedVersion().second),
00238                                                          1 ); // new major version
00239 
00240           // Some debug
00241           std::stringstream ss;
00242           ss << "[SiStripConfigDb::" << __func__ << "]"
00243              << " Uploaded " << feds.size() 
00244              << " FED descriptions to database for partition \""
00245              << iter->second.partitionName() << "\"";
00246           LogTrace(mlConfigDb_) << ss.str();
00247           
00248         } else {
00249           stringstream ss; 
00250           ss << "[SiStripConfigDb::" << __func__ << "]" 
00251              << " Vector of FED descriptions is empty for partition \"" 
00252              << iter->second.partitionName()
00253              << "\", therefore aborting upload for this partition!";
00254           edm::LogWarning(mlConfigDb_) << ss.str(); 
00255           continue; 
00256         }
00257         
00258       } else {
00259         //        stringstream ss; 
00260         //        ss << "[SiStripConfigDb::" << __func__ << "]" 
00261         //           << " Cannot find partition \"" << partition
00262         //           << "\" in cached partitions list: \""
00263         //           << dbParams_.partitionNames( dbParams_.partitionNames() ) 
00264         //           << "\", therefore aborting upload for this partition!";
00265         //        edm::LogWarning(mlConfigDb_) << ss.str(); 
00266       }
00267       
00268     }
00269     
00270   } catch (...) { handleException( __func__ ); }
00271   
00272   allowCalibUpload_ = true;
00273   
00274 }
00275 
00276 // -----------------------------------------------------------------------------
00277 // 
00278 void SiStripConfigDb::clearFedDescriptions( std::string partition ) {
00279   LogTrace(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]";
00280   
00281   if ( feds_.empty() ) { 
00282     stringstream ss; 
00283     ss << "[SiStripConfigDb::" << __func__ << "]" 
00284        << " Found no cached FED descriptions!"; 
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   FedDescriptions temporary_cache;
00291   if ( partition == ""  ) { temporary_cache = FedDescriptions(); }
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         FedDescriptionsRange range = feds_.find( iter->second.partitionName() );
00298         if ( range != feds_.emptyRange() ) {
00299           temporary_cache.loadNext( partition, FedDescriptionsV( range.begin(), range.end() ) );
00300         }
00301       } else {
00302         FedDescriptionsRange range = feds_.find( iter->second.partitionName() );
00303         if ( range != feds_.emptyRange() ) {
00304           LogTrace(mlConfigDb_) 
00305             << "[SiStripConfigDb::" << __func__ << "]"
00306             << " Deleting FED descriptions for partition \""
00307             << iter->second.partitionName()
00308             << "\" from local cache...";
00309         }
00310       }
00311     }
00312   }
00313   
00314   // Delete objects in local cache for specified partition (or all if not specified) 
00315   FedDescriptionsRange feds;
00316   if ( partition == "" ) { 
00317     if ( !feds_.empty() ) {
00318       feds = FedDescriptionsRange( feds_.find( dbParams_.partitions().begin()->second.partitionName() ).begin(),
00319                                    feds_.find( (--(dbParams_.partitions().end()))->second.partitionName() ).end() );
00320     } else { feds = feds_.emptyRange(); }
00321   } else {
00322     SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
00323     SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
00324     for ( ; iter != jter; ++iter ) { if ( partition == iter->second.partitionName() ) { break; } }
00325     feds = feds_.find( iter->second.partitionName() );
00326   }
00327   
00328   if ( feds != feds_.emptyRange() ) {
00329     FedDescriptionsV::const_iterator ifed = feds.begin();
00330     FedDescriptionsV::const_iterator jfed = feds.end();
00331     for ( ; ifed != jfed; ++ifed ) { if ( *ifed ) { delete *ifed; } }
00332   } else {
00333     stringstream ss; 
00334     ss << "[SiStripConfigDb::" << __func__ << "]";
00335     if ( partition == "" ) { ss << " Found no FED descriptions in local cache!"; }
00336     else { ss << " Found no FED descriptions in local cache for partition \"" << partition << "\"!"; }
00337     edm::LogWarning(mlConfigDb_) << ss.str(); 
00338   }
00339   
00340   // Overwrite local cache with temporary cache
00341   feds_ = temporary_cache; 
00342 
00343 }
00344 
00345 // -----------------------------------------------------------------------------
00346 // 
00347 void SiStripConfigDb::printFedDescriptions( std::string partition ) {
00348 
00349   std::stringstream ss;
00350   ss << "[SiStripConfigDb::" << __func__ << "]"
00351      << " Contents of FedDescriptions container:" << std::endl;
00352   ss << " Number of partitions: " << feds_.size() << std::endl;
00353 
00354   // Loop through partitions
00355   uint16_t cntr = 0;
00356   FedDescriptions::const_iterator iconn = feds_.begin();
00357   FedDescriptions::const_iterator jconn = feds_.end();
00358   for ( ; iconn != jconn; ++iconn ) {
00359 
00360     cntr++;
00361     if ( partition == "" || partition == iconn->first ) {
00362       
00363       ss << "  Partition number : " << cntr << " (out of " << feds_.size() << ")" << std::endl;
00364       ss << "  Partition name   : \"" << iconn->first << "\"" << std::endl;
00365       ss << "  Num of FED ids   : " << iconn->second.size() << std::endl;
00366 
00367       // Extract FED crates and ids
00368       std::map< uint16_t, vector<uint16_t> > feds;
00369       FedDescriptionsV::const_iterator iter = iconn->second.begin();
00370       FedDescriptionsV::const_iterator jter = iconn->second.end();
00371       for ( ; iter != jter; ++iter ) { 
00372         if ( *iter ) { 
00373           uint16_t key = (*iter)->getCrateNumber();
00374           uint16_t data = (*iter)->getFedId();
00375           if ( find( feds[key].begin(), feds[key].end(), data ) == feds[key].end() ) { 
00376             feds[key].push_back( data ); 
00377           }
00378         }
00379       }
00380       
00381       // Sort contents
00382       std::map< uint16_t, std::vector<uint16_t> > tmp;
00383       std::map< uint16_t, std::vector<uint16_t> >::const_iterator ii = feds.begin();
00384       std::map< uint16_t, std::vector<uint16_t> >::const_iterator jj = feds.end();
00385       for ( ; ii != jj; ++ii ) {
00386         std::vector<uint16_t> temp = ii->second;
00387         std::sort( temp.begin(), temp.end() );
00388         std::vector<uint16_t>::const_iterator iii = temp.begin();
00389         std::vector<uint16_t>::const_iterator jjj = temp.end();
00390         for ( ; iii != jjj; ++iii ) { tmp[ii->first].push_back( *iii ); }
00391       }
00392       feds.clear();
00393       feds = tmp;
00394       
00395       // Print FED crates and ids
00396       std::map< uint16_t, std::vector<uint16_t> >::const_iterator ifed = feds.begin();
00397       std::map< uint16_t, std::vector<uint16_t> >::const_iterator jfed = feds.end();
00398       for ( ; ifed != jfed; ++ifed ) {
00399         ss << "  Found " << std::setw(2) << ifed->second.size()
00400            << " FED ids for crate number " << std::setw(2) << ifed->first << " : ";
00401         if ( !ifed->second.empty() ) { 
00402           uint16_t first = ifed->second.front();
00403           uint16_t last = ifed->second.front();
00404           std::vector<uint16_t>::const_iterator icrate = ifed->second.begin();
00405           std::vector<uint16_t>::const_iterator jcrate = ifed->second.end();
00406           for ( ; icrate != jcrate; ++icrate ) { 
00407             if ( icrate != ifed->second.begin() ) {
00408               if ( *icrate != last+1 ) { 
00409                 ss << std::setw(2) << first << "->" << std::setw(2) << last << ", ";
00410                 if ( icrate != ifed->second.end() ) { first = *(icrate+1); }
00411               } 
00412             }
00413             last = *icrate;
00414           }
00415           if ( first != last ) { ss << std::setw(2) << first << "->" << std::setw(2) << last; }
00416           ss << std::endl;
00417         }
00418       }
00419       
00420     }
00421     
00422   }
00423   
00424   LogTrace(mlConfigDb_) << ss.str();
00425 
00426 }
00427 
00428 // -----------------------------------------------------------------------------
00430 SiStripConfigDb::FedIdsRange SiStripConfigDb::getFedIds( std::string partition ) {
00431   
00432   fedIds_.clear();
00433   
00434   if ( ( !dbParams_.usingDbCache() && !deviceFactory(__func__) ) ||
00435        (  dbParams_.usingDbCache() && !databaseCache(__func__) ) ) { 
00436     return FedIdsRange( fedIds_.end(), fedIds_.end() );
00437   }
00438   
00439   try { 
00440 
00441     // Inhibit download of strip info
00442     bool using_strips = usingStrips_;
00443     if ( factory_ ) { factory_->setUsingStrips( false ); }
00444     FedDescriptionsRange feds = getFedDescriptions( partition );
00445     if ( factory_ ) { factory_->setUsingStrips( using_strips ); }
00446     
00447     if ( !feds.empty() ) {
00448       FedDescriptionsV::const_iterator ifed = feds.begin();
00449       FedDescriptionsV::const_iterator jfed = feds.end();
00450       for ( ; ifed != jfed; ++ifed ) { 
00451         if ( *ifed ) { fedIds_.push_back( (*ifed)->getFedId() ); }
00452         else {
00453           edm::LogError(mlCabling_)
00454             << "[SiStripConfigDb::" << __func__ << "]"
00455             << " NULL pointer to FedDescription!";
00456           continue;
00457         }
00458       }
00459     }
00460     
00461   } catch (...) { handleException( __func__ ); }
00462   
00463   if ( fedIds_.empty() ) {
00464     edm::LogWarning(mlConfigDb_)
00465       << "[SiStripConfigDb::" << __func__ << "]"
00466       << " No FED ids found!"; 
00467   }
00468   
00469   return FedIdsRange( fedIds_.begin(), fedIds_.end() );
00470 
00471 }
00472