CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/src/OnlineDB/SiStripConfigDb/src/SiStripDbParams.cc

Go to the documentation of this file.
00001 // Last commit: $Id: SiStripDbParams.cc,v 1.14 2010/11/29 20:42:01 wmtan Exp $
00002 
00003 #include "OnlineDB/SiStripConfigDb/interface/SiStripDbParams.h"
00004 #include "DataFormats/SiStripCommon/interface/SiStripEnumsAndStrings.h"
00005 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00006 #include <iostream>
00007 
00008 using namespace sistrip;
00009 
00010 // -----------------------------------------------------------------------------
00011 // 
00012 SiStripDbParams::SiStripDbParams() :
00013   usingDb_(false),
00014   confdb_(null_),
00015   user_(null_),
00016   passwd_(null_),
00017   path_(null_),
00018   usingDbCache_(false),
00019   sharedMemory_(""),
00020   tnsAdmin_(""),
00021   partitions_(),
00022   outputModuleXml_("/tmp/module.xml"),
00023   outputDcuInfoXml_("/tmp/dcuinfo.xml"),
00024   outputFecXml_("/tmp/fec.xml"),
00025   outputFedXml_("/tmp/fed.xml")
00026 {;}
00027 
00028 // -----------------------------------------------------------------------------
00029 // 
00030 SiStripDbParams::SiStripDbParams( const SiStripDbParams& input ) :
00031   usingDb_( input.usingDb() ),
00032   confdb_( input.confdb() ),
00033   user_( input.user() ),
00034   passwd_( input.passwd() ),
00035   path_( input.path() ),
00036   usingDbCache_( input.usingDbCache() ),
00037   sharedMemory_( input.sharedMemory() ),
00038   tnsAdmin_( input.tnsAdmin() ),
00039   partitions_( input.partitions().begin(), input.partitions().end() ),
00040   outputModuleXml_( input.outputModuleXml() ),
00041   outputDcuInfoXml_( input.outputDcuInfoXml() ),
00042   outputFecXml_( input.outputFecXml() ),
00043   outputFedXml_( input.outputFedXml() )
00044 {;}
00045 
00046 // -----------------------------------------------------------------------------
00047 // 
00048 SiStripDbParams& SiStripDbParams::operator= ( const SiStripDbParams& input ) {
00049   usingDb_ = input.usingDb();
00050   confdb_ = input.confdb();
00051   user_ = input.user();
00052   passwd_ = input.passwd();
00053   path_ = input.path();
00054   confdb( confdb_ );
00055   usingDbCache_ = input.usingDbCache();
00056   sharedMemory_ = input.sharedMemory();
00057   tnsAdmin_ = input.tnsAdmin();
00058   partitions_.clear();
00059   partitions_ = SiStripPartitions( input.partitions().begin(), input.partitions().end() );
00060   outputModuleXml_ = input.outputModuleXml();
00061   outputDcuInfoXml_ = input.outputDcuInfoXml();
00062   outputFecXml_ = input.outputFecXml(); 
00063   outputFedXml_ = input.outputFedXml();
00064   return *this;
00065 }
00066 
00067 // -----------------------------------------------------------------------------
00068 // 
00069 bool SiStripDbParams::operator== ( const SiStripDbParams& input ) const {
00070   if ( !( usingDb_ == input.usingDb() &&
00071           confdb_ == input.confdb() && 
00072           user_ == input.user() &&
00073           passwd_ == input.passwd() &&
00074           path_ == input.path() &&
00075           usingDbCache_ == input.usingDbCache() &&
00076           sharedMemory_ == input.sharedMemory() &&
00077           tnsAdmin_ == input.tnsAdmin() &&
00078           outputModuleXml_ == input.outputModuleXml() &&
00079           outputDcuInfoXml_ == input.outputDcuInfoXml() &&
00080           outputFecXml_ == input.outputFecXml() && 
00081           outputFedXml_ == input.outputFedXml() ) ) { return false; }
00082   if ( partitionsSize() != input.partitionsSize() ) { return false; }
00083 //   SiStripPartitions::const_iterator ii = input.partitions().begin();
00084 //   SiStripPartitions::const_iterator jj = input.partitions().end();
00085 //   SiStripPartitions::const_iterator iter = partitions_.begin();
00086 //   for ( ; ii != jj; ++ii ) {
00087 //     if ( ii->first != iter->first || ii->second != iter->second ) { return false; }
00088 //     iter++;
00089 //   }
00090 //   return true;
00091   return partitions_ == input.partitions_;
00092 }
00093 
00094 // -----------------------------------------------------------------------------
00095 // 
00096 bool SiStripDbParams::operator!= ( const SiStripDbParams& input ) const {
00097   return !( *this == input );
00098 }
00099 
00100 // -----------------------------------------------------------------------------
00101 // 
00102 SiStripDbParams::~SiStripDbParams() {
00103   reset();
00104 }
00105 
00106 // -----------------------------------------------------------------------------
00107 // 
00108 void SiStripDbParams::reset() {
00109   usingDb_      = false;
00110   confdb_       = null_;
00111   user_         = null_;
00112   passwd_       = null_;
00113   path_         = null_;
00114   usingDbCache_ = false;
00115   sharedMemory_ = "";
00116   tnsAdmin_     = "";
00117   partitions_.clear();
00118   confdb( confdb_ );
00119   outputModuleXml_  = "/tmp/module.xml";
00120   outputDcuInfoXml_ = "/tmp/dcuinfo.xml";
00121   outputFecXml_     = "/tmp/fec.xml";
00122   outputFedXml_     = "/tmp/fed.xml";
00123 }
00124 
00125 // -----------------------------------------------------------------------------
00126 // 
00127 void SiStripDbParams::addPartition( const SiStripPartition& in ) {
00128 
00129   if ( in.partitionName() == "" ) {
00130     std::stringstream ss;
00131     ss << "[SiStripDbParams::" << __func__ << "]"
00132        << " Attempting to add partition with null name!";
00133     //edm::LogWarning(mlConfigDb_) << ss.str();
00134   }
00135 
00136   SiStripPartitions::const_iterator iter = partitions_.find( in.partitionName() );
00137   if ( iter == partitions_.end() ) { 
00138     partitions_[in.partitionName()] = in; 
00139     if ( in.partitionName() != SiStripPartition::defaultPartitionName_ ) {
00140       std::stringstream ss;
00141       ss << "[SiStripDbParams::" << __func__ << "]"
00142          << " Added new partition with name \"" 
00143          << in.partitionName() << "\"";
00144       //ss << std::endl << partitions_[in.partitionName()];
00145       ss << " (Currently have " 
00146          << partitions_.size()
00147          << " partitions in cache...)";
00148       LogTrace(mlConfigDb_) << ss.str();
00149     }
00150   } else {
00151     std::stringstream ss;
00152     ss << "[SiStripDbParams::" << __func__ << "]"
00153        << " Partition with name \""
00154        << in.partitionName()
00155        << "\" already found!"
00156        << " Not adding to cache...";
00157     edm::LogWarning(mlConfigDb_) << ss.str();
00158   }
00159 
00160 }
00161 
00162 // -----------------------------------------------------------------------------
00163 // 
00164 void SiStripDbParams::pset( const edm::ParameterSet& cfg ) {
00165 
00166   // "Common" configurables
00167   usingDb_      = cfg.getUntrackedParameter<bool>( "UsingDb", false ); 
00168   usingDbCache_ = cfg.getUntrackedParameter<bool>( "UsingDbCache", false ); 
00169   sharedMemory_ = cfg.getUntrackedParameter<std::string>( "SharedMemory", "" ); 
00170   tnsAdmin_     = cfg.getUntrackedParameter<std::string>( "TNS_ADMIN", "" );
00171   confdb( cfg.getUntrackedParameter<std::string>( "ConfDb", "") );
00172 
00173   // Check if top-level PSet (containing partition-level Psets) exists
00174   std::string partitions = "Partitions";
00175   std::vector<std::string> str = cfg.getParameterNamesForType<edm::ParameterSet>(false);
00176   std::vector<std::string>::const_iterator istr = std::find( str.begin(), str.end(), partitions );
00177   if ( istr != str.end() ) {
00178 
00179     // Retrieve top-level PSet containing partition-level Psets
00180     edm::ParameterSet psets = cfg.getUntrackedParameter<edm::ParameterSet>( partitions );
00181     
00182     // Extract names of partition-level PSets
00183     std::vector<std::string> names = psets.getParameterNamesForType<edm::ParameterSet>(false);
00184     
00185     // Iterator through PSets names, retrieve PSet for each partition and extract info
00186     std::vector<std::string>::iterator iname = names.begin();
00187     std::vector<std::string>::iterator jname = names.end();
00188     for ( ; iname != jname; ++iname ) {
00189       edm::ParameterSet pset = psets.getUntrackedParameter<edm::ParameterSet>( *iname );
00190       SiStripPartition tmp;
00191       tmp.pset( pset );
00192       addPartition( tmp );
00193     }
00194 
00195   }
00196   
00197   // Ensure at least one "default" partition
00198   if ( partitions_.empty() ) { addPartition( SiStripPartition() ); }
00199 
00200   // Set output XML files
00201   outputModuleXml_  = cfg.getUntrackedParameter<std::string>( "OutputModuleXml", "/tmp/module.xml" );
00202   outputDcuInfoXml_ = cfg.getUntrackedParameter<std::string>( "OutputDcuInfoXml", "/tmp/dcuinfo.xml" );
00203   outputFecXml_     = cfg.getUntrackedParameter<std::string>( "OutputFecXml", "/tmp/fec.xml" );
00204   outputFedXml_     = cfg.getUntrackedParameter<std::string>( "OutputFedXml", "/tmp/fed.xml" );
00205   
00206 }
00207 
00208 // -----------------------------------------------------------------------------
00209 // 
00210 void SiStripDbParams::confdb( const std::string& confdb ) {
00211   confdb_ = confdb;
00212   size_t ipass = confdb.find("/");
00213   size_t ipath = confdb.find("@");
00214   if ( ipass != std::string::npos && 
00215        ipath != std::string::npos ) {
00216     user_   = confdb.substr(0,ipass); 
00217     passwd_ = confdb.substr(ipass+1,ipath-ipass-1); 
00218     path_   = confdb.substr(ipath+1,confdb.size());
00219   } else {
00220     user_   = null_;
00221     passwd_ = null_;
00222     path_   = null_;
00223   }
00224 }
00225 
00226 // -----------------------------------------------------------------------------
00227 // 
00228 void SiStripDbParams::confdb( const std::string& user,
00229                               const std::string& passwd,
00230                               const std::string& path ) {
00231   if ( user != "" && passwd != "" && path != "" &&
00232        user != null_ && passwd != null_ && path != null_ ) {
00233     user_   = user;
00234     passwd_ = passwd;
00235     path_   = path;
00236   } else {
00237     user_   = null_;
00238     passwd_ = null_;
00239     path_   = null_;
00240   }
00241   confdb_ = user_ + "/" + passwd_ + "@" + path_;
00242 }
00243 
00244 // -----------------------------------------------------------------------------
00245 // 
00246 SiStripDbParams::SiStripPartitions::const_iterator SiStripDbParams::partition( std::string partition_name ) const {
00247   SiStripDbParams::SiStripPartitions::const_iterator ii = partitions().begin();
00248   SiStripDbParams::SiStripPartitions::const_iterator jj = partitions().end();
00249   for ( ; ii != jj; ++ii ) { if ( partition_name == ii->second.partitionName() ) { return ii; } }
00250   return partitions().end(); 
00251 }
00252 
00253 // -----------------------------------------------------------------------------
00254 // 
00255 SiStripDbParams::SiStripPartitions::iterator SiStripDbParams::partition( std::string partition_name ) {
00256   SiStripDbParams::SiStripPartitions::iterator ii = partitions().begin();
00257   SiStripDbParams::SiStripPartitions::iterator jj = partitions().end();
00258   for ( ; ii != jj; ++ii ) { if ( partition_name == ii->second.partitionName() ) { return ii; } }
00259   return partitions().end(); 
00260 }
00261 
00262 // -----------------------------------------------------------------------------
00263 // 
00264 std::vector<std::string> SiStripDbParams::partitionNames() const {
00265   std::vector<std::string> partitions;
00266   SiStripPartitions::const_iterator ii = partitions_.begin();
00267   SiStripPartitions::const_iterator jj = partitions_.end();
00268   for ( ; ii != jj; ++ii ) { 
00269     if ( std::find( partitions.begin(), 
00270                     partitions.end(), 
00271                     ii->second.partitionName() ) == partitions.end() ) {
00272       if ( !ii->second.partitionName().empty() ) { partitions.push_back( ii->second.partitionName() ); }
00273     } else {
00274       edm::LogWarning(mlConfigDb_)
00275         << "[SiStripConfigDb::" << __func__ << "]"
00276         << " Partition " << ii->second.partitionName()
00277         << " already found! Not adding to vector...";
00278     }
00279   }
00280   return partitions;
00281 }
00282 
00283 // -----------------------------------------------------------------------------
00284 // 
00285 std::vector<std::string> SiStripDbParams::partitionNames( std::string input ) const {
00286   std::istringstream ss(input);
00287   std::vector<std::string> partitions;
00288   std::string delimiter = ":";
00289   std::string token;
00290   while ( getline( ss, token, ':' ) ) { if ( !token.empty() ) { partitions.push_back(token); } }
00291   return partitions;
00292 }
00293 
00294 // -----------------------------------------------------------------------------
00295 // 
00296 std::string SiStripDbParams::partitionNames( const std::vector<std::string>& partitions ) const {
00297   std::stringstream ss;
00298   std::vector<std::string>::const_iterator ii = partitions.begin();
00299   std::vector<std::string>::const_iterator jj = partitions.end();
00300   bool first = true;
00301   for ( ; ii != jj; ++ii ) { 
00302     if ( !ii->empty() ) { 
00303       first ? ss << *ii : ss << ":" << *ii; 
00304       first = false;
00305     }
00306   }
00307   return ss.str();
00308 }
00309 
00310 // -----------------------------------------------------------------------------
00311 // 
00312 void SiStripDbParams::print( std::stringstream& ss ) const {
00313 
00314   ss << " Using database account     : " << std::boolalpha << usingDb_ << std::noboolalpha << std::endl;
00315   ss << " Using XML files            : " << std::boolalpha << !usingDb_ << std::noboolalpha << std::endl;
00316   ss << " Using database cache       : " << std::boolalpha << usingDbCache_ << std::noboolalpha << std::endl;
00317   if ( usingDbCache_ ) { 
00318     ss << " Shared memory name         : " << std::boolalpha << sharedMemory_ << std::noboolalpha << std::endl;
00319   }
00320 
00321   if ( !usingDbCache_ ) {
00322 
00323     if ( usingDb_ ) {
00324       ss << " Database account (ConfDb)  : " << confdb_ << std::endl;
00325     }
00326     
00327     ss << " Number of partitions       : " << partitions_.size();
00328     if ( partitions_.empty() ) { 
00329       if ( !usingDbCache_ ) { ss << " (Empty!)"; }
00330       else { ss << " (Using database cache!)"; }
00331     }
00332     ss << std::endl;
00333     
00334     uint16_t cntr = 0;
00335     SiStripPartitions::const_iterator ii = partitions_.begin();
00336     SiStripPartitions::const_iterator jj = partitions_.end();
00337     for ( ; ii != jj; ++ii ) { 
00338       cntr++;
00339       ss << " Partition #" << cntr << " (out of " << partitions_.size() << "):" << std::endl;
00340       ii->second.print( ss, usingDb_ ); 
00341     }
00342     
00343     if ( !usingDb_ ) {
00344       ss << " Output \"module.xml\" file   : " << outputModuleXml_ << std::endl
00345          << " Output \"dcuinfo.xml\" file  : " << outputDcuInfoXml_ << std::endl
00346          << " Output \"fec.xml\" file(s)   : " << outputFecXml_ << std::endl
00347          << " Output \"fed.xml\" file(s)   : " << outputFedXml_ << std::endl;
00348     }
00349 
00350   }
00351  
00352 }
00353 
00354 // -----------------------------------------------------------------------------
00355 // 
00356 std::ostream& operator<< ( std::ostream& os, const SiStripDbParams& params ) {
00357   std::stringstream ss;
00358   params.print(ss);
00359   os << ss.str();
00360   return os;
00361 }
00362 
00363 // -----------------------------------------------------------------------------
00364 // 
00365 std::vector<std::string> SiStripDbParams::inputModuleXmlFiles() const {
00366   std::vector<std::string> files;
00367   SiStripPartitions::const_iterator ii = partitions_.begin();
00368   SiStripPartitions::const_iterator jj = partitions_.end();
00369   for ( ; ii != jj; ++ii ) { files.insert( files.end(), ii->second.inputModuleXml() ); }
00370   return files;
00371 }
00372 
00373 // -----------------------------------------------------------------------------
00374 // 
00375 std::vector<std::string> SiStripDbParams::inputDcuInfoXmlFiles() const {
00376   std::vector<std::string> files;
00377   SiStripPartitions::const_iterator ii = partitions_.begin();
00378   SiStripPartitions::const_iterator jj = partitions_.end();
00379   for ( ; ii != jj; ++ii ) { files.insert( files.end(), ii->second.inputDcuInfoXml() ); }
00380   return files;
00381 }
00382 
00383 // -----------------------------------------------------------------------------
00384 // 
00385 std::vector<std::string> SiStripDbParams::inputFecXmlFiles() const {
00386   std::vector<std::string> files;
00387   SiStripPartitions::const_iterator ii = partitions_.begin();
00388   SiStripPartitions::const_iterator jj = partitions_.end();
00389   for ( ; ii != jj; ++ii ) { files.insert( files.end(), ii->second.inputFecXml().begin(), ii->second.inputFecXml().end() ); }
00390   return files;
00391 }
00392 
00393 // -----------------------------------------------------------------------------
00394 // 
00395 std::vector<std::string> SiStripDbParams::inputFedXmlFiles() const {
00396   std::vector<std::string> files;
00397   SiStripPartitions::const_iterator ii = partitions_.begin();
00398   SiStripPartitions::const_iterator jj = partitions_.end();
00399   for ( ; ii != jj; ++ii ) { files.insert( files.end(), ii->second.inputFedXml().begin(), ii->second.inputFedXml().end() ); }
00400   return files;
00401 }