CMS 3D CMS Logo

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

Go to the documentation of this file.
00001 // Last commit: $Id: DcuDetIds.cc,v 1.7 2009/04/06 16:57:28 lowette Exp $
00002 
00003 #include "OnlineDB/SiStripConfigDb/interface/SiStripConfigDb.h"
00004 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00005 
00006 using namespace std;
00007 using namespace sistrip;
00008 
00009 // -----------------------------------------------------------------------------
00010 // 
00011 SiStripConfigDb::DcuDetIdsRange SiStripConfigDb::getDcuDetIds( std::string partition ) {
00012 
00013   // Check
00014   if ( ( !dbParams_.usingDbCache() && !deviceFactory(__func__) ) ||
00015        (  dbParams_.usingDbCache() && !databaseCache(__func__) ) ) { 
00016     return dcuDetIds_.emptyRange(); 
00017   }
00018   
00019   try {
00020 
00021     if ( !dbParams_.usingDbCache() ) { 
00022 
00023       SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
00024       SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
00025       for ( ; iter != jter; ++iter ) {
00026         
00027         if ( partition == "" || partition == iter->second.partitionName() ) {
00028           
00029           if ( iter->second.partitionName() == SiStripPartition::defaultPartitionName_ ) { continue; }
00030 
00031           DcuDetIdsRange range = dcuDetIds_.find( iter->second.partitionName() );
00032           if ( range == dcuDetIds_.emptyRange() ) {
00033             
00034             deviceFactory(__func__)->addDetIdPartition( iter->second.partitionName(),
00035                                                         iter->second.dcuVersion().first, 
00036                                                         iter->second.dcuVersion().second );
00037             
00038             // Retrieve DCU-DetId map
00039             DcuDetIdMap src = deviceFactory(__func__)->getInfos(); 
00040             
00041             // Make local copy 
00042             DcuDetIdsV dst;
00043             clone( src, dst );
00044             
00045             // Add to cache
00046             dcuDetIds_.loadNext( iter->second.partitionName(), dst );
00047             
00048           }
00049 
00050         }
00051 
00052       }
00053       
00054     } else { // Using database cache
00055       
00056       // Retrieve DCU-DetId map
00057       DcuDetIdMap* src = databaseCache(__func__)->getInfos(); 
00058 
00059       if ( src ) { 
00060         
00061         // Make local copy 
00062         DcuDetIdsV dst;
00063         clone( *src, dst ); 
00064         
00065         // Add to cache
00066         dcuDetIds_.loadNext( SiStripPartition::defaultPartitionName_, dst );
00067         
00068       } else {
00069         edm::LogWarning(mlConfigDb_)
00070           << "[SiStripConfigDb::" << __func__ << "]"
00071           << " NULL pointer to Dcu-DetId map!";
00072       }
00073       
00074     }
00075     
00076   } catch (... ) { handleException( __func__ ); }
00077 
00078   // Create range object
00079   uint16_t np = 0;
00080   uint16_t nc = 0;
00081   DcuDetIdsRange range;
00082   if ( partition != "" ) { 
00083     range = dcuDetIds_.find( partition );
00084     np = 1;
00085     nc = range.size();
00086   } else { 
00087     if ( !dcuDetIds_.empty() ) {
00088       range = DcuDetIdsRange( dcuDetIds_.find( dbParams_.partitions().begin()->second.partitionName() ).begin(),
00089                               dcuDetIds_.find( (--(dbParams_.partitions().end()))->second.partitionName() ).end() );
00090     } else { range = dcuDetIds_.emptyRange(); }
00091     np = dcuDetIds_.size();
00092     nc = range.size();
00093   }
00094   
00095   stringstream ss; 
00096   ss << "[SiStripConfigDb::" << __func__ << "]"
00097      << " Found " << nc << " entries in DCU-DetId map";
00098   if ( !dbParams_.usingDb() ) { ss << " in " << dbParams_.inputDcuInfoXmlFiles().size() << " 'module.xml' file(s)"; }
00099   else { if ( !dbParams_.usingDbCache() )  { ss << " in " << np << " database partition(s)"; } 
00100   else { ss << " from shared memory name '" << dbParams_.sharedMemory() << "'"; } }
00101   if ( dcuDetIds_.empty() ) { edm::LogWarning(mlConfigDb_) << ss.str(); }
00102   else { LogTrace(mlConfigDb_) << ss.str(); }
00103 
00104   return range;
00105 }
00106 
00107 // -----------------------------------------------------------------------------
00108 // 
00109 void SiStripConfigDb::addDcuDetIds( std::string partition, DcuDetIdsV& dcus ) {
00110 
00111   stringstream ss; 
00112   ss << "[SiStripConfigDb::" << __func__ << "]" 
00113      << " Cannot add to local cache! This functionality not allowed!";
00114   edm::LogWarning(mlConfigDb_) << ss.str(); 
00115   
00116   //   if ( !deviceFactory(__func__) ) { return; }
00117   
00118   //   if ( partition.empty() ) { 
00119   //     stringstream ss; 
00120   //     ss << "[SiStripConfigDb::" << __func__ << "]" 
00121   //        << " Partition string is empty,"
00122   //        << " therefore cannot add DCU-DetId map to local cache!"; 
00123   //     edm::LogWarning(mlConfigDb_) << ss.str(); 
00124   //     return; 
00125   //   }
00126   
00127   //   if ( dcus.empty() ) { 
00128   //     stringstream ss; 
00129   //     ss << "[SiStripConfigDb::" << __func__ << "]" 
00130   //        << " Vector of DCU-DetId map is empty,"
00131   //        << " therefore cannot add DCU-DetId map to local cache!"; 
00132   //     edm::LogWarning(mlConfigDb_) << ss.str(); 
00133   //     return; 
00134   //   }
00135 
00136   //   SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
00137   //   SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
00138   //   for ( ; iter != jter; ++iter ) { if ( partition == iter->second.partitionName() ) { break; } }
00139   //   if ( iter == dbParams_.partitions().end() ) { 
00140   //     stringstream ss; 
00141   //     ss << "[SiStripConfigDb::" << __func__ << "]" 
00142   //        << " Partition \"" << partition
00143   //        << "\" not found in partition list, "
00144   //        << " therefore cannot add DCU-DetId map!";
00145   //     edm::LogWarning(mlConfigDb_) << ss.str(); 
00146   //     return; 
00147   //   }
00148   
00149   //   DcuDetIdsRange range = dcuDetIds_.find( partition );
00150   //   if ( range == dcuDetIds_.emptyRange() ) {
00151     
00152   //     // Make local copy 
00153   //     DcuDetIdsV dst;
00154   //     clone( dcus, dst );
00155     
00156   //     // Add to local cache
00157   //     dcuDetIds_.loadNext( partition, dst );
00158     
00159   //     // Some debug
00160   //     std::stringstream ss;
00161   //     ss << "[SiStripConfigDb::" << __func__ << "]"
00162   //        << " Added " << dst.size() 
00163   //        << " DCU-DetId map to local cache for partition \""
00164   //        << partition << "\"" << std::endl;
00165   //     ss << "[SiStripConfigDb::" << __func__ << "]"
00166   //        << " Cache holds DCU-DetId map for " 
00167   //        << dcuDetIds_.size() << " partitions.";
00168   //     LogTrace(mlConfigDb_) << ss.str();
00169     
00170   //   } else {
00171   //     stringstream ss; 
00172   //     ss << "[SiStripConfigDb::" << __func__ << "]" 
00173   //        << " Partition \"" << partition
00174   //        << "\" already found in local cache, "
00175   //        << " therefore cannot add new DCU-DetId map!";
00176   //     edm::LogWarning(mlConfigDb_) << ss.str(); 
00177   //     return; 
00178   //   }
00179   
00180 }
00181 
00182 // -----------------------------------------------------------------------------
00183 // 
00184 void SiStripConfigDb::uploadDcuDetIds( std::string partition ) {
00185 
00186   stringstream ss; 
00187   ss << "[SiStripConfigDb::" << __func__ << "]" 
00188      << " Cannot upload to database! This functionality not allowed!";
00189   edm::LogWarning(mlConfigDb_) << ss.str(); 
00190   
00191   /*
00192     addAllDetId => all detids
00193     addAllDetId => to download (up to you)
00194     change in the detids
00195     setTkDcuInfo
00196     getCurrentStates
00197     setCurrentState
00198     addDetIpartiton
00199     AddAllDetId
00200   */
00201   
00202   //   if ( dbParams_.usingDbCache() ) {
00203   //     edm::LogWarning(mlConfigDb_)
00204   //       << "[SiStripConfigDb::" << __func__ << "]" 
00205   //       << " Using database cache! No uploads allowed!"; 
00206   //     return;
00207   //   }
00208 
00209   //   if ( !deviceFactory(__func__) ) { return; }
00210   
00211   //   if ( dcuDetIds_.empty() ) { 
00212   //     stringstream ss; 
00213   //     ss << "[SiStripConfigDb::" << __func__ << "]" 
00214   //        << " Found no cached DCU-DetId map, therefore no upload!"; 
00215   //     edm::LogWarning(mlConfigDb_) << ss.str(); 
00216   //     return; 
00217   //   }
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   //    DcuDetIdsRange range = dcuDetIds_.find( iter->second.partitionName() );
00228   //    if ( range != dcuDetIds_.emptyRange() ) {
00229           
00230   //      // Extract 
00231   //      DcuDetIdMap dst;
00232   //      clone( DcuDetIdsV( range.begin(), range.end() ), dst );
00233   //      deviceFactory(__func__)->setTkDcuInfo( dst );
00234   //      getcurrentstate
00235   //      deviceFactory(__func__)->addAllDetId();
00236           
00237   //      // Some debug
00238   //      std::stringstream ss;
00239   //      ss << "[SiStripConfigDb::" << __func__ << "]"
00240   //         << " Uploaded " << dst.size() 
00241   //         << " DCU-DetId map to DB/xml for partition \""
00242   //         << iter->second.partitionName() << "\".";
00243   //      LogTrace(mlConfigDb_) << ss.str();
00244           
00245   //    } else {
00246   //      stringstream ss; 
00247   //      ss << "[SiStripConfigDb::" << __func__ << "]" 
00248   //         << " Vector of DCU-DetId map is empty for partition \"" 
00249   //         << iter->second.partitionName()
00250   //         << "\", therefore aborting upload for this partition!";
00251   //      edm::LogWarning(mlConfigDb_) << ss.str(); 
00252   //      continue; 
00253   //    }
00254         
00255   //       } else {
00256   //    //        stringstream ss; 
00257   //    //        ss << "[SiStripConfigDb::" << __func__ << "]" 
00258   //    //           << " Cannot find partition \"" << partition
00259   //    //           << "\" in cached partitions list: \""
00260   //    //           << dbParams_.partitionNames( dbParams_.partitionNames() ) 
00261   //    //           << "\", therefore aborting upload for this partition!";
00262   //    //        edm::LogWarning(mlConfigDb_) << ss.str(); 
00263   //       }
00264       
00265   //     }
00266     
00267   //   } catch (... ) { handleException( __func__, "Problems updating objects in TkDcuInfoFactory!" ); }
00268   
00269 }
00270 
00271 // -----------------------------------------------------------------------------
00272 // 
00273 void SiStripConfigDb::clearDcuDetIds( std::string partition ) {
00274   LogTrace(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]";
00275   
00276   if ( dcuDetIds_.empty() ) { 
00277     stringstream ss; 
00278     ss << "[SiStripConfigDb::" << __func__ << "]" 
00279        << " Found no cached DCU-DetId map!"; 
00280     //edm::LogWarning(mlConfigDb_) << ss.str(); 
00281     return; 
00282   }
00283   
00284   // Reproduce temporary cache for "all partitions except specified one" (or clear all if none specified)
00285   DcuDetIds temporary_cache;
00286   if ( partition == ""  ) { temporary_cache = DcuDetIds(); }
00287   else {
00288     SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
00289     SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
00290     for ( ; iter != jter; ++iter ) {
00291       if ( partition != iter->second.partitionName() ) {
00292         DcuDetIdsRange range = dcuDetIds_.find( iter->second.partitionName() );
00293         if ( range != dcuDetIds_.emptyRange() ) {
00294           temporary_cache.loadNext( partition, DcuDetIdsV( range.begin(), range.end() ) );
00295         } else {
00296           //      stringstream ss; 
00297           //      ss << "[SiStripConfigDb::" << __func__ << "]" 
00298           //         << " Cannot find partition \"" << iter->second.partitionName()
00299           //         << "\" in local cache!";
00300           //      edm::LogWarning(mlConfigDb_) << ss.str(); 
00301         }
00302       }
00303     }
00304   }
00305   
00306   // Delete objects in local cache for specified partition (or all if not specified) 
00307   DcuDetIdsRange dcus;
00308   if ( partition == "" ) { 
00309     if ( !dcuDetIds_.empty() ) {
00310       dcus = DcuDetIdsRange( dcuDetIds_.find( dbParams_.partitions().begin()->second.partitionName() ).begin(),
00311                              dcuDetIds_.find( (--(dbParams_.partitions().end()))->second.partitionName() ).end() );
00312     } else { dcus = dcuDetIds_.emptyRange(); }
00313   } else {
00314     SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
00315     SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
00316     for ( ; iter != jter; ++iter ) { if ( partition == iter->second.partitionName() ) { break; } }
00317     dcus = dcuDetIds_.find( iter->second.partitionName() );
00318   }
00319   
00320   if ( dcus != dcuDetIds_.emptyRange() ) {
00321     DcuDetIdsV::const_iterator ifed = dcus.begin();
00322     DcuDetIdsV::const_iterator jfed = dcus.end();
00323     for ( ; ifed != jfed; ++ifed ) { if ( ifed->second ) { delete ifed->second; } }
00324   } else {
00325     stringstream ss; 
00326     ss << "[SiStripConfigDb::" << __func__ << "]";
00327     if ( partition == "" ) { ss << " Found no DCU-DetId map in local cache!"; }
00328     else { ss << " Found no DCU-DetId map in local cache for partition \"" << partition << "\"!"; }
00329     edm::LogWarning(mlConfigDb_) << ss.str(); 
00330   }
00331   
00332   // Overwrite local cache with temporary cache
00333   dcuDetIds_ = temporary_cache; 
00334 
00335 }
00336 
00337 // -----------------------------------------------------------------------------
00338 // 
00339 void SiStripConfigDb::printDcuDetIds( std::string partition ) {
00340 
00341   std::stringstream ss;
00342   ss << "[SiStripConfigDb::" << __func__ << "]"
00343      << " Contents of DcuDetIds container:" << std::endl;
00344   ss << " Number of partitions: " << dcuDetIds_.size() << std::endl;
00345 
00346   // Loop through partitions
00347   uint16_t cntr = 0;
00348   DcuDetIds::const_iterator idcu = dcuDetIds_.begin();
00349   DcuDetIds::const_iterator jdcu = dcuDetIds_.end();
00350   for ( ; idcu != jdcu; ++idcu ) {
00351 
00352     cntr++;
00353     if ( partition == "" || partition == idcu->first ) {
00354       
00355       ss << "  Partition number      : " << cntr << " (out of " << dcuDetIds_.size() << ")" << std::endl;
00356       ss << "  Partition name        : \"" << idcu->first << "\"" << std::endl;
00357       ss << "  Size of DCU-DetId map : " << idcu->second.size() << std::endl;
00358       
00359     }
00360     
00361   }
00362   
00363   LogTrace(mlConfigDb_) << ss.str();
00364   
00365 }
00366 
00367 // -----------------------------------------------------------------------------
00368 // 
00369 void SiStripConfigDb::clone( const DcuDetIdMap& input, DcuDetIdsV& output ) const {
00370   output.clear();
00371   DcuDetIdMap::const_iterator ii = input.begin();
00372   DcuDetIdMap::const_iterator jj = input.end();
00373   for ( ; ii != jj; ++ii ) { if ( ii->second ) { output.push_back( std::make_pair( ii->first, new TkDcuInfo( *(ii->second) ) ) ); } }
00374 }
00375 
00376 // -----------------------------------------------------------------------------
00377 // 
00378 void SiStripConfigDb::clone( const DcuDetIdsV& input, DcuDetIdMap& output ) const {
00379   output.clear();
00380   DcuDetIdsV::const_iterator ii = input.begin();
00381   DcuDetIdsV::const_iterator jj = input.end();
00382   for ( ; ii != jj; ++ii ) { if ( ii->second ) { output[ii->first] = new TkDcuInfo( *(ii->second) ); } }
00383 }
00384 
00385 // -----------------------------------------------------------------------------
00386 // 
00387 void SiStripConfigDb::clone( const DcuDetIdsV& input, DcuDetIdsV& output ) const {
00388   output.clear();
00389   DcuDetIdsV::const_iterator ii = input.begin();
00390   DcuDetIdsV::const_iterator jj = input.end();
00391   for ( ; ii != jj; ++ii ) { if ( ii->second ) { output.push_back( std::make_pair( ii->first, new TkDcuInfo( *(ii->second) ) ) ); } }
00392 }
00393 
00394 // -----------------------------------------------------------------------------
00395 // 
00396 SiStripConfigDb::DcuDetIdsV::const_iterator SiStripConfigDb::findDcuDetId( DcuDetIdsV::const_iterator begin, 
00397                                                                            DcuDetIdsV::const_iterator end, 
00398                                                                            uint32_t dcu_id  ) {
00399   DcuDetIdsV::const_iterator iter = begin;
00400   DcuDetIdsV::const_iterator jter = end;
00401   for ( ; iter != jter; ++iter  ) {
00402     if ( iter->second && iter->second->getDcuHardId() == dcu_id ) { return iter; }
00403   }
00404   return end;
00405 }
00406 
00407 // -----------------------------------------------------------------------------
00408 // 
00409 SiStripConfigDb::DcuDetIdsV::iterator SiStripConfigDb::findDcuDetId( DcuDetIdsV::iterator begin, 
00410                                                                      DcuDetIdsV::iterator end, 
00411                                                                      uint32_t dcu_id  ) {
00412   DcuDetIdsV::iterator iter = begin;
00413   DcuDetIdsV::iterator jter = end;
00414   for ( ; iter != jter; ++iter  ) {
00415     if ( iter->second && iter->second->getDcuHardId() == dcu_id ) { return iter; }
00416   }
00417   return end;
00418 }