CMS 3D CMS Logo

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

Go to the documentation of this file.
00001 // Last commit: $Id: FedDescriptions.cc,v 1.33 2011/09/02 11:25:25 eulisse 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           SiStripPartition::Versions fedVersion = iter->second.fedVersion();
00235           deviceFactory(__func__)->setFed9UDescriptions( feds,
00236                                                          iter->second.partitionName(),
00237                                                          (uint16_t*)(&(fedVersion.first)), 
00238                                                          (uint16_t*)(&(fedVersion.second)),
00239                                                          1 ); // new major version
00240 
00241           // Some debug
00242           std::stringstream ss;
00243           ss << "[SiStripConfigDb::" << __func__ << "]"
00244              << " Uploaded " << feds.size() 
00245              << " FED descriptions to database for partition \""
00246              << iter->second.partitionName() << "\"";
00247           LogTrace(mlConfigDb_) << ss.str();
00248           
00249         } else {
00250           stringstream ss; 
00251           ss << "[SiStripConfigDb::" << __func__ << "]" 
00252              << " Vector of FED descriptions is empty for partition \"" 
00253              << iter->second.partitionName()
00254              << "\", therefore aborting upload for this partition!";
00255           edm::LogWarning(mlConfigDb_) << ss.str(); 
00256           continue; 
00257         }
00258         
00259       } else {
00260         //        stringstream ss; 
00261         //        ss << "[SiStripConfigDb::" << __func__ << "]" 
00262         //           << " Cannot find partition \"" << partition
00263         //           << "\" in cached partitions list: \""
00264         //           << dbParams_.partitionNames( dbParams_.partitionNames() ) 
00265         //           << "\", therefore aborting upload for this partition!";
00266         //        edm::LogWarning(mlConfigDb_) << ss.str(); 
00267       }
00268       
00269     }
00270     
00271   } catch (...) { handleException( __func__ ); }
00272   
00273   allowCalibUpload_ = true;
00274   
00275 }
00276 
00277 // -----------------------------------------------------------------------------
00278 // 
00279 void SiStripConfigDb::clearFedDescriptions( std::string partition ) {
00280   LogTrace(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]";
00281   
00282   if ( feds_.empty() ) { 
00283     stringstream ss; 
00284     ss << "[SiStripConfigDb::" << __func__ << "]" 
00285        << " Found no cached FED descriptions!"; 
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   FedDescriptions temporary_cache;
00292   if ( partition == ""  ) { temporary_cache = FedDescriptions(); }
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         FedDescriptionsRange range = feds_.find( iter->second.partitionName() );
00299         if ( range != feds_.emptyRange() ) {
00300           temporary_cache.loadNext( partition, FedDescriptionsV( range.begin(), range.end() ) );
00301         }
00302       } else {
00303         FedDescriptionsRange range = feds_.find( iter->second.partitionName() );
00304         if ( range != feds_.emptyRange() ) {
00305           LogTrace(mlConfigDb_) 
00306             << "[SiStripConfigDb::" << __func__ << "]"
00307             << " Deleting FED descriptions for partition \""
00308             << iter->second.partitionName()
00309             << "\" from local cache...";
00310         }
00311       }
00312     }
00313   }
00314   
00315   // Delete objects in local cache for specified partition (or all if not specified) 
00316   FedDescriptionsRange feds;
00317   if ( partition == "" ) { 
00318     if ( !feds_.empty() ) {
00319       feds = FedDescriptionsRange( feds_.find( dbParams_.partitions().begin()->second.partitionName() ).begin(),
00320                                    feds_.find( (--(dbParams_.partitions().end()))->second.partitionName() ).end() );
00321     } else { feds = feds_.emptyRange(); }
00322   } else {
00323     SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
00324     SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
00325     for ( ; iter != jter; ++iter ) { if ( partition == iter->second.partitionName() ) { break; } }
00326     feds = feds_.find( iter->second.partitionName() );
00327   }
00328   
00329   if ( feds != feds_.emptyRange() ) {
00330     FedDescriptionsV::const_iterator ifed = feds.begin();
00331     FedDescriptionsV::const_iterator jfed = feds.end();
00332     for ( ; ifed != jfed; ++ifed ) { if ( *ifed ) { delete *ifed; } }
00333   } else {
00334     stringstream ss; 
00335     ss << "[SiStripConfigDb::" << __func__ << "]";
00336     if ( partition == "" ) { ss << " Found no FED descriptions in local cache!"; }
00337     else { ss << " Found no FED descriptions in local cache for partition \"" << partition << "\"!"; }
00338     edm::LogWarning(mlConfigDb_) << ss.str(); 
00339   }
00340   
00341   // Overwrite local cache with temporary cache
00342   feds_ = temporary_cache; 
00343 
00344 }
00345 
00346 // -----------------------------------------------------------------------------
00347 // 
00348 void SiStripConfigDb::printFedDescriptions( std::string partition ) {
00349 
00350   std::stringstream ss;
00351   ss << "[SiStripConfigDb::" << __func__ << "]"
00352      << " Contents of FedDescriptions container:" << std::endl;
00353   ss << " Number of partitions: " << feds_.size() << std::endl;
00354 
00355   // Loop through partitions
00356   uint16_t cntr = 0;
00357   FedDescriptions::const_iterator iconn = feds_.begin();
00358   FedDescriptions::const_iterator jconn = feds_.end();
00359   for ( ; iconn != jconn; ++iconn ) {
00360 
00361     cntr++;
00362     if ( partition == "" || partition == iconn->first ) {
00363       
00364       ss << "  Partition number : " << cntr << " (out of " << feds_.size() << ")" << std::endl;
00365       ss << "  Partition name   : \"" << iconn->first << "\"" << std::endl;
00366       ss << "  Num of FED ids   : " << iconn->second.size() << std::endl;
00367 
00368       // Extract FED crates and ids
00369       std::map< uint16_t, vector<uint16_t> > feds;
00370       FedDescriptionsV::const_iterator iter = iconn->second.begin();
00371       FedDescriptionsV::const_iterator jter = iconn->second.end();
00372       for ( ; iter != jter; ++iter ) { 
00373         if ( *iter ) { 
00374           uint16_t key = (*iter)->getCrateNumber();
00375           uint16_t data = (*iter)->getFedId();
00376           if ( find( feds[key].begin(), feds[key].end(), data ) == feds[key].end() ) { 
00377             feds[key].push_back( data ); 
00378           }
00379         }
00380       }
00381       
00382       // Sort contents
00383       std::map< uint16_t, std::vector<uint16_t> > tmp;
00384       std::map< uint16_t, std::vector<uint16_t> >::const_iterator ii = feds.begin();
00385       std::map< uint16_t, std::vector<uint16_t> >::const_iterator jj = feds.end();
00386       for ( ; ii != jj; ++ii ) {
00387         std::vector<uint16_t> temp = ii->second;
00388         std::sort( temp.begin(), temp.end() );
00389         std::vector<uint16_t>::const_iterator iii = temp.begin();
00390         std::vector<uint16_t>::const_iterator jjj = temp.end();
00391         for ( ; iii != jjj; ++iii ) { tmp[ii->first].push_back( *iii ); }
00392       }
00393       feds.clear();
00394       feds = tmp;
00395       
00396       // Print FED crates and ids
00397       std::map< uint16_t, std::vector<uint16_t> >::const_iterator ifed = feds.begin();
00398       std::map< uint16_t, std::vector<uint16_t> >::const_iterator jfed = feds.end();
00399       for ( ; ifed != jfed; ++ifed ) {
00400         ss << "  Found " << std::setw(2) << ifed->second.size()
00401            << " FED ids for crate number " << std::setw(2) << ifed->first << " : ";
00402         if ( !ifed->second.empty() ) { 
00403           uint16_t first = ifed->second.front();
00404           uint16_t last = ifed->second.front();
00405           std::vector<uint16_t>::const_iterator icrate = ifed->second.begin();
00406           std::vector<uint16_t>::const_iterator jcrate = ifed->second.end();
00407           for ( ; icrate != jcrate; ++icrate ) { 
00408             if ( icrate != ifed->second.begin() ) {
00409               if ( *icrate != last+1 ) { 
00410                 ss << std::setw(2) << first << "->" << std::setw(2) << last << ", ";
00411                 if ( icrate != ifed->second.end() ) { first = *(icrate+1); }
00412               } 
00413             }
00414             last = *icrate;
00415           }
00416           if ( first != last ) { ss << std::setw(2) << first << "->" << std::setw(2) << last; }
00417           ss << std::endl;
00418         }
00419       }
00420       
00421     }
00422     
00423   }
00424   
00425   LogTrace(mlConfigDb_) << ss.str();
00426 
00427 }
00428 
00429 // -----------------------------------------------------------------------------
00431 SiStripConfigDb::FedIdsRange SiStripConfigDb::getFedIds( std::string partition ) {
00432   
00433   fedIds_.clear();
00434   
00435   if ( ( !dbParams_.usingDbCache() && !deviceFactory(__func__) ) ||
00436        (  dbParams_.usingDbCache() && !databaseCache(__func__) ) ) { 
00437     return FedIdsRange( fedIds_.end(), fedIds_.end() );
00438   }
00439   
00440   try { 
00441 
00442     // Inhibit download of strip info
00443     bool using_strips = usingStrips_;
00444     if ( factory_ ) { factory_->setUsingStrips( false ); }
00445     FedDescriptionsRange feds = getFedDescriptions( partition );
00446     if ( factory_ ) { factory_->setUsingStrips( using_strips ); }
00447     
00448     if ( !feds.empty() ) {
00449       FedDescriptionsV::const_iterator ifed = feds.begin();
00450       FedDescriptionsV::const_iterator jfed = feds.end();
00451       for ( ; ifed != jfed; ++ifed ) { 
00452         if ( *ifed ) { fedIds_.push_back( (*ifed)->getFedId() ); }
00453         else {
00454           edm::LogError(mlCabling_)
00455             << "[SiStripConfigDb::" << __func__ << "]"
00456             << " NULL pointer to FedDescription!";
00457           continue;
00458         }
00459       }
00460     }
00461     
00462   } catch (...) { handleException( __func__ ); }
00463   
00464   if ( fedIds_.empty() ) {
00465     edm::LogWarning(mlConfigDb_)
00466       << "[SiStripConfigDb::" << __func__ << "]"
00467       << " No FED ids found!"; 
00468   }
00469   
00470   return FedIdsRange( fedIds_.begin(), fedIds_.end() );
00471 
00472 }
00473