CMS 3D CMS Logo

SiStripConfigDb.cc

Go to the documentation of this file.
00001 // Last commit: $Id: SiStripConfigDb.cc,v 1.72 2008/07/03 09:29:21 bainbrid Exp $
00002 
00003 #include "OnlineDB/SiStripConfigDb/interface/SiStripConfigDb.h"
00004 #include "DataFormats/SiStripCommon/interface/SiStripConstants.h"
00005 #include "DataFormats/SiStripCommon/interface/SiStripEnumsAndStrings.h"
00006 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00007 #include <iostream>
00008 #include <fstream>
00009 
00010 using namespace sistrip;
00011 
00012 // -----------------------------------------------------------------------------
00013 // 
00014 uint32_t SiStripConfigDb::cntr_ = 0;
00015 
00016 // -----------------------------------------------------------------------------
00017 // 
00018 bool SiStripConfigDb::allowCalibUpload_ = false;
00019 
00020 // -----------------------------------------------------------------------------
00021 // 
00022 SiStripConfigDb::SiStripConfigDb( const edm::ParameterSet& pset,
00023                                   const edm::ActivityRegistry& activity ) :
00024   factory_(0), 
00025   dbCache_(0), 
00026   dbParams_(),
00027   // Local cache
00028   connections_(), 
00029   devices_(), 
00030   feds_(), 
00031   dcuDetIds_(), 
00032 #ifdef USING_NEW_DATABASE_MODEL
00033   analyses_(),
00034 #endif
00035   apvDevices_(),
00036   muxDevices_(),
00037   dcuDevices_(),
00038   lldDevices_(),
00039   pllDevices_(),
00040   dohDevices_(),
00041   typedDevices_(), 
00042   fedIds_(),
00043   // Misc
00044   usingStrips_(true),
00045   openConnection_(false)
00046 {
00047   cntr_++;
00048   edm::LogVerbatim(mlConfigDb_)
00049     << "[SiStripConfigDb::" << __func__ << "]"
00050     << " Constructing database service..."
00051     << " (Class instance: " << cntr_ << ")";
00052   
00053   // Set DB connection parameters
00054   dbParams_.reset();
00055   dbParams_.pset( pset );
00056   //edm::LogVerbatim(mlConfigDb_) << dbParams_; 
00057 
00058   // Open connection
00059   openDbConnection();
00060   
00061 }
00062 
00063 // -----------------------------------------------------------------------------
00064 //
00065 SiStripConfigDb::~SiStripConfigDb() {
00066   closeDbConnection(); 
00067   LogTrace(mlConfigDb_)
00068     << "[SiStripConfigDb::" << __func__ << "]"
00069     << " Destructing object...";
00070   if ( cntr_ ) { cntr_--; }
00071 }
00072 
00073 // -----------------------------------------------------------------------------
00074 // 
00075 SiStripConfigDb::DeviceAddress::DeviceAddress() : 
00076   fecCrate_(sistrip::invalid_), 
00077   fecSlot_(sistrip::invalid_), 
00078   fecRing_(sistrip::invalid_), 
00079   ccuAddr_(sistrip::invalid_), 
00080   ccuChan_(sistrip::invalid_), 
00081   lldChan_(sistrip::invalid_), 
00082   i2cAddr_(sistrip::invalid_),
00083   fedId_(sistrip::invalid_),
00084   feUnit_(sistrip::invalid_),
00085   feChan_(sistrip::invalid_)
00086 { reset(); }
00087 
00088 // -----------------------------------------------------------------------------
00089 // 
00090 void SiStripConfigDb::DeviceAddress::reset() { 
00091   fecCrate_ = sistrip::invalid_; 
00092   fecSlot_  = sistrip::invalid_; 
00093   fecRing_  = sistrip::invalid_; 
00094   ccuAddr_  = sistrip::invalid_; 
00095   ccuChan_  = sistrip::invalid_; 
00096   lldChan_  = sistrip::invalid_;
00097   i2cAddr_  = sistrip::invalid_;
00098   fedId_    = sistrip::invalid_;
00099   feUnit_   = sistrip::invalid_;
00100   feChan_   = sistrip::invalid_;
00101 }
00102 
00103 // -----------------------------------------------------------------------------
00104 // 
00105 void SiStripConfigDb::openDbConnection() {
00106 
00107   LogTrace(mlConfigDb_) 
00108     << "[SiStripConfigDb::" << __func__ << "]"
00109     << " Opening connection to database...";
00110   
00111   // Check if connection already exists
00112   if ( openConnection_ ) {
00113     edm::LogWarning(mlConfigDb_) 
00114       << "[SiStripConfigDb::" << __func__ << "]"
00115       << " Connection already open!";
00116     return;
00117   }
00118   openConnection_ = true;
00119 
00120   // Establish database connection
00121   if ( dbParams_.usingDb() ) { 
00122     if ( dbParams_.usingDbCache() ) { usingDatabaseCache(); }
00123     else { usingDatabase(); }
00124   } else { usingXmlFiles(); }
00125 
00126   std::stringstream ss;
00127   ss << "[SiStripConfigDb::" << __func__ << "]"
00128      << " Database connection parameters: "
00129      << std::endl << dbParams_;
00130   edm::LogVerbatim(mlConfigDb_) << ss.str();
00131 
00132   // Clear local caches
00133   clearLocalCache();
00134 
00135   LogTrace(mlConfigDb_) 
00136     << "[SiStripConfigDb::" << __func__ << "]"
00137     << " Opened connection to database!";
00138   
00139 }
00140 
00141 // -----------------------------------------------------------------------------
00142 //
00143 void SiStripConfigDb::closeDbConnection() {
00144 
00145   LogTrace(mlConfigDb_) 
00146     << "[SiStripConfigDb::" << __func__ << "]"
00147     << " Closing connection to database...";
00148 
00149   // Check if connection exists
00150   if ( !openConnection_ ) {
00151     edm::LogWarning(mlConfigDb_) 
00152       << "[SiStripConfigDb::" << __func__ << "]"
00153       << " No connection open!";
00154     return;
00155   }
00156   openConnection_ = false;
00157   
00158   // Clear local caches
00159   clearLocalCache();
00160   
00161   try { 
00162     if ( factory_ ) { delete factory_; }
00163   } catch (...) { handleException( __func__, "Attempting to delete DeviceFactory object..." ); }
00164   factory_ = 0; 
00165 
00166 #ifdef USING_NEW_DATABASE_MODEL
00167   try { 
00168     if ( dbCache_ ) { delete dbCache_; }
00169   } catch (...) { handleException( __func__, "Attempting to delete DbClient object..." ); }
00170   dbCache_ = 0; 
00171 #endif
00172   
00173   LogTrace(mlConfigDb_) 
00174     << "[SiStripConfigDb::" << __func__ << "]"
00175     << " Closed connection to database...";
00176   
00177 }
00178 
00179 // -----------------------------------------------------------------------------
00180 //
00181 void SiStripConfigDb::clearLocalCache() {
00182 
00183   LogTrace(mlConfigDb_) 
00184     << "[SiStripConfigDb::" << __func__ << "]"
00185     << " Clearing local caches...";
00186 
00187   clearFedConnections();
00188   clearDeviceDescriptions();
00189   clearFedDescriptions();
00190   clearDcuDetIds();
00191 #ifdef USING_NEW_DATABASE_MODEL
00192   clearAnalysisDescriptions();
00193 #endif
00194 
00195   typedDevices_.clear();
00196   fedIds_.clear();
00197 
00198 }
00199 
00200 // -----------------------------------------------------------------------------
00201 //
00202 DeviceFactory* const SiStripConfigDb::deviceFactory( std::string method_name ) const { 
00203   if ( factory_ ) { return factory_; }
00204   else { 
00205     if ( method_name != "" ) { 
00206       stringstream ss;
00207       ss << "[SiStripConfigDb::" << __func__ << "]"
00208          << " NULL pointer to DeviceFactory requested by" 
00209          << " method SiStripConfigDb::" << method_name << "()!";
00210       edm::LogWarning(mlConfigDb_) << ss.str();
00211     }
00212     return 0;
00213   }
00214 }
00215 
00216 // -----------------------------------------------------------------------------
00217 //
00218 DbClient* const SiStripConfigDb::databaseCache( std::string method_name ) const { 
00219   if ( dbCache_ ) { return dbCache_; }
00220   else { 
00221     if ( method_name != "" ) { 
00222       stringstream ss;
00223       ss << "[SiStripConfigDb::" << __func__ << "]"
00224          << " NULL pointer to DbClient requested by" 
00225          << " method SiStripConfigDb::" << method_name << "()!";
00226       edm::LogWarning(mlConfigDb_) << ss.str();
00227     }
00228     return 0;
00229   }
00230 }
00231 
00232 // -----------------------------------------------------------------------------
00233 //
00234 void SiStripConfigDb::usingDatabase() {
00235   
00236   // Retrieve connection params from CONFDB env. var. and override .cfg values 
00237   std::string user = "";
00238   std::string passwd = "";
00239   std::string path = "";
00240   DbAccess::getDbConfiguration( user, passwd, path );
00241   if ( user != "" && passwd != "" && path != "" ) {
00242 
00243     std::stringstream ss;
00244     ss << "[SiStripConfigDb::" << __func__ << "]"
00245        << " Setting \"user/passwd@path\" to \""
00246        << user << "/" << passwd << "@" << path
00247        << "\" using 'CONFDB' environmental variable";
00248     if ( dbParams_.user() != null_ || 
00249          dbParams_.passwd() != null_ || 
00250          dbParams_.path() != null_ ) { 
00251       ss << " (Overwriting existing value of \""
00252          << dbParams_.user() << "/" 
00253          << dbParams_.passwd() << "@" 
00254          << dbParams_.path() 
00255          << "\" read from .cfg file)";
00256     }
00257     edm::LogVerbatim(mlConfigDb_) << ss.str() << std::endl;
00258     dbParams_.confdb( user, passwd, path );
00259 
00260   } else if ( dbParams_.user() != null_ && 
00261               dbParams_.passwd() != null_ && 
00262               dbParams_.path() != null_ ) { 
00263 
00264     std::stringstream ss;
00265     ss << "[SiStripConfigDb::" << __func__ << "]"
00266        << " Setting \"user/passwd@path\" to \""
00267        << dbParams_.user() << "/" 
00268        << dbParams_.passwd() << "@" 
00269        << dbParams_.path() 
00270        << "\" using 'ConfDb' configurable read from .cfg file";
00271     edm::LogVerbatim(mlConfigDb_) << ss.str();
00272 
00273   } else {
00274     edm::LogWarning(mlConfigDb_)
00275       << "[SiStripConfigDb::" << __func__ << "]"
00276       << " Unable to retrieve 'user/passwd@path' parameters"
00277       << " from 'CONFDB' environmental variable or .cfg file"
00278       << " (present value is \"" 
00279       << user << "/" 
00280       << passwd << "@" 
00281       << path 
00282       << "\"). Aborting connection to database...";
00283     return;
00284   }
00285   
00286   // Check TNS_ADMIN environmental variable
00287   std::string pattern = "TNS_ADMIN";
00288   std::string tns_admin = "/afs/cern.ch/project/oracle/admin";
00289   if ( getenv( pattern.c_str() ) != NULL ) { 
00290     tns_admin = getenv( pattern.c_str() ); 
00291     edm::LogVerbatim(mlConfigDb_)
00292       << "[SiStripConfigDb::" << __func__ << "]"
00293       << " TNS_ADMIN is set to: \"" 
00294       << tns_admin << "\"";
00295   } else {
00296     edm::LogWarning(mlConfigDb_)
00297       << "[SiStripConfigDb::" << __func__ << "]"
00298       << " TNS_ADMIN is not set!"
00299       << " Trying to use /afs and setting to: \"" 
00300       << tns_admin << "\"";
00301   }
00302 
00303   // Retrieve TNS_ADMIN from .cfg file and override
00304   if ( !dbParams_.tnsAdmin().empty() ) {
00305     std::stringstream ss;
00306     ss << "[SiStripConfigDb::" << __func__ << "]"
00307        << " Overriding TNS_ADMIN value using cfg file!" << std::endl
00308        << "  Original value : \"" << tns_admin << "\"!" << std::endl
00309        << "  New value      : \"" << dbParams_.tnsAdmin() << "\"!";
00310     tns_admin = dbParams_.tnsAdmin();
00311     edm::LogVerbatim(mlConfigDb_) << ss.str();
00312   }
00313   
00314   // Remove trailing slash and set TNS_ADMIN
00315   if ( tns_admin.empty() ) { tns_admin = "."; }
00316   std::string slash = tns_admin.substr( tns_admin.size()-1, 1 ); 
00317   if ( slash == sistrip::dir_ ) { tns_admin = tns_admin.substr( 0, tns_admin.size()-1 ); }
00318   setenv( pattern.c_str(), tns_admin.c_str(), 1 ); 
00319   
00320   // Check if database is found in tnsnames.ora file
00321   std::string filename( tns_admin + "/tnsnames.ora" ); 
00322   std::ifstream tnsnames_ora( filename.c_str() );
00323   bool ok = false;
00324   if ( tnsnames_ora.is_open() ) {
00325     std::string line;
00326     while ( !tnsnames_ora.eof() ) {
00327       getline( tnsnames_ora, line );
00328       if ( !dbParams_.path().empty() && 
00329            line.find( dbParams_.path() ) != std::string::npos ) { ok = true; }
00330     }
00331   } else {
00332     edm::LogWarning(mlConfigDb_)
00333       << "[SiStripConfigDb::" << __func__ << "]"
00334       << " Cannot open file \""
00335       << filename << "\"";
00336   }
00337 
00338   if ( ok ) {
00339     LogTrace(mlConfigDb_)
00340       << "[SiStripConfigDb::" << __func__ << "]"
00341       << " Found database account \"" 
00342       << dbParams_.path() << "\" in file \""
00343       << filename << "\"!";
00344   } else {
00345     edm::LogWarning(mlConfigDb_)
00346       << "[SiStripConfigDb::" << __func__ << "]"
00347       << " Cannot find database account \"" 
00348       << dbParams_.path() << "\" in file \""
00349       << filename << "\""
00350       << " Aborting connection to database...";
00351     return; 
00352   }
00353   
00354   // Create device factory object
00355   try { 
00356     LogTrace(mlConfigDb_)
00357       << "[SiStripConfigDb::" << __func__ << "]"
00358       << " Creating DeviceFactory object...";
00359     factory_ = new DeviceFactory( dbParams_.user(), 
00360                                   dbParams_.passwd(), 
00361                                   dbParams_.path() ); 
00362     LogTrace(mlConfigDb_)
00363       << "[SiStripConfigDb::" << __func__ << "]"
00364       << " Created DeviceFactory object!";
00365   } catch (...) { 
00366     std::stringstream ss; 
00367     ss << "Failed to connect to database using parameters '" 
00368        << dbParams_.user() << "/" 
00369        << dbParams_.passwd() << "@" 
00370        << dbParams_.path() 
00371        << "' and partitions '" 
00372        << dbParams_.partitionNames( dbParams_.partitionNames() ) << "'";
00373     handleException( __func__, ss.str() );
00374     return;
00375   }
00376   
00377   // Check for valid pointer to DeviceFactory
00378   if ( deviceFactory(__func__) ) { 
00379     std::stringstream ss;
00380     ss << "[SiStripConfigDb::" << __func__ << "]"
00381        << " DeviceFactory created at address 0x" 
00382        << std::hex << std::setw(8) << std::setfill('0') << factory_ << std::dec
00383        << ", using database account with parameters '" 
00384        << dbParams_.user() << "/" 
00385        << dbParams_.passwd() << "@" 
00386        << dbParams_.path();
00387     LogTrace(mlConfigDb_) << ss.str();
00388   } else {
00389     edm::LogError(mlConfigDb_)
00390       << "[SiStripConfigDb::" << __func__ << "]"
00391       << " NULL pointer to DeviceFactory!"
00392       << " Unable to connect to database using connection parameters '" 
00393       << dbParams_.user() << "/" 
00394       << dbParams_.passwd() << "@" 
00395       << dbParams_.path()
00396       << "' and partitions '" 
00397       << dbParams_.partitionNames( dbParams_.partitionNames() ) << "'";
00398     return; 
00399   }
00400   
00401   try { 
00402     deviceFactory(__func__)->setUsingDb( dbParams_.usingDb() ); 
00403   } catch (...) { 
00404     handleException( __func__, "Attempted to 'setUsingDb'" );
00405   }
00406   
00407   // Retrieve partition name from ENV_CMS_TK_PARTITION env. var. and override .cfg value
00408   std::string partition = "ENV_CMS_TK_PARTITION";
00409   if ( getenv(partition.c_str()) != NULL ) { 
00410     
00411     std::stringstream ss;
00412     ss << "[SiStripConfigDb::" << __func__ << "]"
00413        << " Setting \"partitions\" to \""
00414        << getenv( partition.c_str() )
00415        << "\" using 'ENV_CMS_TK_PARTITION' environmental variable";
00416     if ( !dbParams_.partitionNames().empty() ) {
00417       ss << " (Overwriting existing value of \""
00418          << dbParams_.partitionNames( dbParams_.partitionNames() )
00419          << "\" read from .cfg file)";
00420     }
00421     edm::LogVerbatim(mlConfigDb_) << ss.str() << std::endl;
00422     
00423     // Build partitions from env. var.
00424     std::vector<std::string> partitions = dbParams_.partitionNames( getenv( partition.c_str() ) );
00425     if ( !partitions.empty() ) {
00426       dbParams_.clearPartitions();
00427       std::vector<std::string>::iterator ii = partitions.begin();
00428       std::vector<std::string>::iterator jj = partitions.end();
00429       for ( ; ii != jj; ++ii ) {
00430         SiStripPartition partition( *ii );
00431         dbParams_.addPartition( partition );
00432       }
00433     }
00434 
00435   } else if ( !dbParams_.partitionNames().empty() ) {
00436     std::stringstream ss;
00437     ss << "[SiStripConfigDb::" << __func__ << "]"
00438        << " Setting \"partitions\" to \""
00439        << dbParams_.partitionNames( dbParams_.partitionNames() )
00440        << "\" using 'PartitionName' configurables read from .cfg file";
00441     edm::LogVerbatim(mlConfigDb_) << ss.str();
00442   } else { 
00443     edm::LogWarning(mlConfigDb_)
00444       << "[SiStripConfigDb::" << __func__ << "]"
00445       << " Unable to retrieve 'partition' parameter"
00446       << " from 'CONFDB' environmental variable or .cfg file!"
00447       << " Aborting connection to database...";
00448     return;
00449   } 
00450 
00451   // Check if should use current state, run number or versions
00452   SiStripDbParams::SiStripPartitions::iterator ip = dbParams_.partitions().begin();
00453   SiStripDbParams::SiStripPartitions::iterator jp = dbParams_.partitions().end();
00454   for ( ; ip != jp; ++ip ) { ip->second.update( this ); }
00455   
00456 }
00457 
00458 // -----------------------------------------------------------------------------
00459 //
00460 void SiStripConfigDb::usingDatabaseCache() {
00461   
00462   // Reset all DbParams except for those concerning database cache
00463   SiStripDbParams temp;
00464   temp = dbParams_;
00465   dbParams_.reset();
00466   dbParams_.usingDb( temp.usingDb() );
00467   dbParams_.usingDbCache( temp.usingDbCache() );
00468   dbParams_.sharedMemory( temp.sharedMemory() );
00469 
00470   // Add default partition 
00471   dbParams_.addPartition( SiStripPartition( SiStripPartition::defaultPartitionName_ ) );
00472   
00473   // Check shared memory name from .cfg file
00474   if ( dbParams_.sharedMemory().empty() ) {
00475     std::stringstream ss;
00476     ss << "[SiStripConfigDb::" << __func__ << "]"
00477        << " Empty string for shared memory name!" 
00478        << " Cannot accept shared memory!";
00479     edm::LogError(mlConfigDb_) << ss.str();
00480     return;
00481   }
00482   
00483   // Create database cache object
00484 #ifdef USING_NEW_DATABASE_MODEL
00485   try { 
00486     LogTrace(mlConfigDb_)
00487       << "[SiStripConfigDb::" << __func__ << "]"
00488       << " Creating DbClient object...";
00489     dbCache_ = new DbClient( dbParams_.sharedMemory() );
00490     LogTrace(mlConfigDb_)
00491       << "[SiStripConfigDb::" << __func__ << "]"
00492       << " Created DbClient object...";
00493   } catch (...) { 
00494     std::stringstream ss; 
00495     ss << "Failed to connect to database cache using shared memory name: '" 
00496        << dbParams_.sharedMemory() << "'!";
00497     handleException( __func__, ss.str() );
00498     return;
00499   }
00500 #endif
00501   
00502   // Check for valid pointer to DbClient object
00503   if ( databaseCache(__func__) ) { 
00504     std::stringstream ss;
00505     ss << "[SiStripConfigDb::" << __func__ << "]"
00506        << " DbClient object created at address 0x" 
00507        << std::hex << std::setw(8) << std::setfill('0') << dbCache_ << std::dec
00508        << " using shared memory name '" 
00509        << dbParams_.sharedMemory() << "'"; 
00510     LogTrace(mlConfigDb_) << ss.str();
00511   } else {
00512     edm::LogError(mlConfigDb_)
00513       << "[SiStripConfigDb::" << __func__ << "]"
00514       << " NULL pointer to DbClient object!"
00515       << " Unable to connect to database cache using shared memory name '" 
00516       << dbParams_.sharedMemory() << "'"; 
00517     return; 
00518   }
00519   
00520   // Try retrieve descriptions from Database Client
00521 #ifdef USING_NEW_DATABASE_MODEL
00522   try { 
00523     databaseCache(__func__)->parse(); 
00524   } catch (...) { 
00525     handleException( __func__, "Attempted to called DbClient::parse() method" );
00526   }
00527 #endif
00528   
00529 }
00530 
00531 // -----------------------------------------------------------------------------
00532 //
00533 void SiStripConfigDb::usingXmlFiles() {
00534   LogTrace(mlConfigDb_)
00535     << "[SiStripConfigDb::" << __func__ << "]"
00536     << " Using XML description files...";
00537 
00538   // Create device factory object
00539   try { 
00540     factory_ = new DeviceFactory(); 
00541   } catch (...) { 
00542     handleException( __func__, "Attempting to create DeviceFactory for use with xml files" );
00543   }
00544   
00545  // Check for valid pointer to DeviceFactory
00546   if ( deviceFactory(__func__) ) { 
00547     std::stringstream ss;
00548     ss << "[SiStripConfigDb::" << __func__ << "]"
00549        << " DeviceFactory created at address 0x" 
00550        << std::hex << std::setw(8) << std::setfill('0') << factory_ << std::dec
00551        << ", using XML description files";
00552     LogTrace(mlConfigDb_) << ss.str();
00553   } else {    
00554     edm::LogError(mlConfigDb_)
00555       << "[SiStripConfigDb::" << __func__ << "]"
00556       << " NULL pointer to DeviceFactory!"
00557       << " Unable to connect to database!";
00558     return; 
00559   }
00560   
00561   try { 
00562     deviceFactory(__func__)->setUsingDb( dbParams_.usingDb() );
00563   } catch (...) { 
00564     handleException( __func__, "Attempted to 'setUsingDb'" );
00565   }
00566 
00567   try { 
00568 #ifndef USING_NEW_DATABASE_MODEL
00569     deviceFactory(__func__)->createInputFileAccess();
00570 #endif
00571   } catch (...) { 
00572     handleException( __func__, "Attempted to 'createInputFileAccess'" ); 
00573   }
00574 
00575   // Iterate through partitions
00576   SiStripDbParams::SiStripPartitions::const_iterator ip = dbParams_.partitions().begin();
00577   SiStripDbParams::SiStripPartitions::const_iterator jp = dbParams_.partitions().end();
00578   for ( ; ip != jp; ++ip ) {
00579     
00580     // Input module.xml file
00581     if ( ip->second.inputModuleXml() == "" ) {
00582       edm::LogWarning(mlConfigDb_)
00583         << "[SiStripConfigDb::" << __func__ << "]"
00584         << " NULL path to input 'module.xml' file!";
00585     } else {
00586       if ( checkFileExists( ip->second.inputModuleXml() ) ) { 
00587         try { 
00588 #ifdef USING_NEW_DATABASE_MODEL
00589           deviceFactory(__func__)->addConnectionFileName( ip->second.inputModuleXml() ); 
00590 #else
00591           deviceFactory(__func__)->addFedFecConnectionFileName( ip->second.inputModuleXml() ); 
00592 #endif
00593         } catch (...) { 
00594           handleException( __func__ ); 
00595         }
00596         LogTrace(mlConfigDb_)
00597           << "[SiStripConfigDb::" << __func__ << "]"
00598           << " Added input 'module.xml' file: " << ip->second.inputModuleXml();
00599       } else {
00600         edm::LogWarning(mlConfigDb_)
00601           << "[SiStripConfigDb::" << __func__ << "]"
00602           << " No 'module.xml' file found at " << ip->second.inputModuleXml();
00603         ip->second.inputModuleXml() = ""; 
00604       }
00605     }
00606   
00607     // Input dcuinfo.xml file
00608     if ( ip->second.inputDcuInfoXml() == "" ) {
00609       edm::LogWarning(mlConfigDb_)
00610         << "[SiStripConfigDb::" << __func__ << "]"
00611         << " NULL path to input 'dcuinfo.xml' file!";
00612     } else { 
00613       if ( checkFileExists( ip->second.inputDcuInfoXml() ) ) { 
00614         try { 
00615           deviceFactory(__func__)->addTkDcuInfoFileName( ip->second.inputDcuInfoXml() ); 
00616         } catch (...) { 
00617           handleException( __func__ ); 
00618         }
00619         LogTrace(mlConfigDb_)
00620           << "[SiStripConfigDb::" << __func__ << "]"
00621           << " Added 'dcuinfo.xml' file: " << ip->second.inputDcuInfoXml();
00622       } else {
00623         edm::LogWarning(mlConfigDb_)
00624           << "[SiStripConfigDb::" << __func__ << "]"
00625           << " No 'dcuinfo.xml' file found at " << ip->second.inputDcuInfoXml();
00626         ip->second.inputDcuInfoXml() = ""; 
00627       } 
00628     }
00629 
00630     // Input FEC xml files
00631     if ( ip->second.inputFecXml().empty() ) {
00632       edm::LogWarning(mlConfigDb_) 
00633         << "[SiStripConfigDb::" << __func__ << "]"
00634         << " NULL paths to input 'fec.xml' files!";
00635     } else {
00636       std::vector<std::string>::iterator iter = ip->second.inputFecXml().begin();
00637       for ( ; iter != ip->second.inputFecXml().end(); iter++ ) {
00638         if ( *iter == "" ) {
00639           edm::LogWarning(mlConfigDb_)
00640             << "[SiStripConfigDb::" << __func__ << "]"
00641             << " NULL path to input 'fec.xml' file!";
00642         } else {
00643           if ( checkFileExists( *iter ) ) { 
00644             try { 
00645               deviceFactory(__func__)->addFecFileName( *iter ); 
00646             } catch (...) { handleException( __func__ ); }
00647             LogTrace(mlConfigDb_) 
00648               << "[SiStripConfigDb::" << __func__ << "]"
00649               << " Added 'fec.xml' file: " << *iter;
00650           } else {
00651             edm::LogWarning(mlConfigDb_) 
00652               << "[SiStripConfigDb::" << __func__ << "]"
00653               << " No 'fec.xml' file found at " << *iter;
00654             *iter = ""; 
00655           } 
00656         }
00657       }
00658     }
00659     
00660     // Input FED xml files
00661     if ( ip->second.inputFedXml().empty() ) {
00662       edm::LogWarning(mlConfigDb_) 
00663         << "[SiStripConfigDb::" << __func__ << "]"
00664         << " NULL paths to input 'fed.xml' files!";
00665     } else {
00666       std::vector<std::string>::iterator iter = ip->second.inputFedXml().begin();
00667       for ( ; iter != ip->second.inputFedXml().end(); iter++ ) {
00668         if ( *iter == "" ) {
00669           edm::LogWarning(mlConfigDb_) 
00670             << "[SiStripConfigDb::" << __func__ << "]"
00671             << " NULL path to input 'fed.xml' file!";
00672         } else {
00673           if ( checkFileExists( *iter ) ) { 
00674             try { 
00675                 deviceFactory(__func__)->addFedFileName( *iter ); 
00676             } catch (...) { 
00677               handleException( __func__ ); 
00678             }
00679             LogTrace(mlConfigDb_) 
00680               << "[SiStripConfigDb::" << __func__ << "]"
00681               << " Added 'fed.xml' file: " << *iter;
00682           } else {
00683             edm::LogWarning(mlConfigDb_) 
00684               << "[SiStripConfigDb::" << __func__ << "]"
00685               << " No 'fed.xml' file found at " << *iter;
00686             *iter = ""; 
00687           } 
00688         }
00689       }
00690     }
00691 
00692   }
00693 
00694   // Output module.xml file
00695   if ( dbParams_.outputModuleXml() == "" ) { 
00696     edm::LogWarning(mlConfigDb_) 
00697       << "[SiStripConfigDb::" << __func__ << "]"
00698       << " NULL path to output 'module.xml' file!"
00699       << " Setting to '/tmp/module.xml'...";
00700     dbParams_.outputModuleXml() = "/tmp/module.xml"; 
00701   } else {
00702     try { 
00703 #ifdef USING_NEW_DATABASE_MODEL
00704       ConnectionFactory* factory = deviceFactory(__func__);
00705 #else
00706       FedFecConnectionDeviceFactory* factory = deviceFactory(__func__);
00707 #endif
00708       factory->setOutputFileName( dbParams_.outputModuleXml() ); 
00709     } catch (...) { 
00710       handleException( __func__, "Problems setting output 'module.xml' file!" ); 
00711     }
00712   }
00713 
00714   // Output dcuinfo.xml file
00715   if ( dbParams_.outputDcuInfoXml() == "" ) { 
00716     edm::LogWarning(mlConfigDb_) 
00717       << "[SiStripConfigDb::" << __func__ << "]"
00718       << " NULL path to output 'dcuinfo.xml' file!"
00719       << " Setting to '/tmp/dcuinfo.xml'...";
00720     dbParams_.outputModuleXml() = "/tmp/dcuinfo.xml"; 
00721   } else {
00722     try { 
00723       TkDcuInfoFactory* factory = deviceFactory(__func__);
00724       factory->setOutputFileName( dbParams_.outputDcuInfoXml() ); 
00725     } catch (...) { 
00726       handleException( __func__, "Problems setting output 'dcuinfo.xml' file!" ); 
00727     }
00728   }
00729 
00730   // Output fec.xml file
00731   if ( dbParams_.outputFecXml() == "" ) {
00732     edm::LogWarning(mlConfigDb_) 
00733       << "[SiStripConfigDb::" << __func__ << "]"
00734       << " NULL path to output 'fec.xml' file!"
00735       << " Setting to '/tmp/fec.xml'...";
00736     dbParams_.outputFecXml() = "/tmp/fec.xml";
00737   } else {
00738     try { 
00739       FecDeviceFactory* factory = deviceFactory(__func__);
00740       factory->setOutputFileName( dbParams_.outputFecXml() ); 
00741     } catch (...) { 
00742       handleException( __func__, "Problems setting output 'fec.xml' file!" ); 
00743     }
00744   }
00745 
00746   // Output fed.xml file
00747   if ( dbParams_.outputFedXml() == "" ) {
00748     edm::LogWarning(mlConfigDb_) 
00749       << "[SiStripConfigDb::" << __func__ << "]"
00750       << " NULL path to output 'fed.xml' file!"
00751       << " Setting to '/tmp/fed.xml'...";
00752     dbParams_.outputFedXml() = "/tmp/fed.xml";
00753   } else {
00754     try { 
00755       Fed9U::Fed9UDeviceFactory* factory = deviceFactory(__func__);
00756       factory->setOutputFileName( dbParams_.outputFedXml() ); 
00757     } catch (...) { 
00758       handleException( __func__, "Problems setting output 'fed.xml' file!" ); 
00759     }
00760   }
00761 
00762 }
00763 
00764 // -----------------------------------------------------------------------------
00765 // 
00766 void SiStripConfigDb::handleException( const std::string& method_name,
00767                                        const std::string& extra_info ) const { 
00768 
00769   std::stringstream ss;
00770   try {
00771     throw; // rethrow caught exception to be dealt with below
00772   } 
00773 
00774   catch ( const cms::Exception& e ) { 
00775     ss << " Caught cms::Exception in method "
00776        << method_name << " with message: " << std::endl 
00777        << e.what();
00778     if ( extra_info != "" ) { ss << "Additional info: " << extra_info << std::endl; }
00779     //throw e; // rethrow cms::Exception
00780   }
00781   
00782   catch ( const oracle::occi::SQLException& e ) { 
00783     ss << " Caught oracle::occi::SQLException in method "
00784        << method_name << " with message: " << std::endl 
00785        << e.getMessage();
00786     if ( extra_info != "" ) { ss << "Additional info: " << extra_info << std::endl; }
00787     //throw cms::Exception(mlConfigDb_) << ss.str() << std::endl;
00788   }
00789 
00790   catch ( const FecExceptionHandler& e ) {
00791     ss << " Caught FecExceptionHandler exception in method "
00792        << method_name << " with message: " << std::endl 
00793 #ifdef USING_NEW_DATABASE_MODEL
00794        << const_cast<FecExceptionHandler&>(e).what();
00795 #else
00796        << const_cast<FecExceptionHandler&>(e).getMessage();
00797 #endif
00798     if ( extra_info != "" ) { ss << "Additional info: " << extra_info << std::endl; }
00799     //throw cms::Exception(mlConfigDb_) << ss.str() << std::endl;
00800   }
00801 
00802 //   catch ( const Fed9UDeviceFactoryException& e ) {
00803 //     ss << " Caught Fed9UDeviceFactoryException exception in method "
00804 //        << method_name << " with message: " << std::endl 
00805 //        << e.what();
00806 //     if ( extra_info != "" ) { ss << "Additional info: " << extra_info << std::endl; }
00807 //     //throw cms::Exception(mlConfigDb_) << ss.str() << std::endl;
00808 //   }
00809 
00810   catch ( const ICUtils::ICException& e ) {
00811     ss << " Caught ICUtils::ICException in method "
00812        << method_name << " with message: " << std::endl 
00813        << e.what();
00814     if ( extra_info != "" ) { ss << "Additional info: " << extra_info << std::endl; }
00815     //throw cms::Exception(mlConfigDb_) << ss.str() << std::endl;
00816   }
00817 
00818   catch ( const exception& e ) {
00819     ss << " Caught std::exception in method "
00820        << method_name << " with message: " << std::endl 
00821        << e.what();
00822     if ( extra_info != "" ) { ss << "Additional info: " << extra_info << std::endl; }
00823     //throw cms::Exception(mlConfigDb_) << ss.str() << std::endl;
00824   }
00825 
00826   catch (...) {
00827     ss << " Caught unknown exception in method "
00828        << method_name << " (No message) " << std::endl;
00829     if ( extra_info != "" ) { ss << "Additional info: " << extra_info << std::endl; }
00830     //throw cms::Exception(mlConfigDb_) << ss.str() << std::endl;
00831   }
00832   
00833   // Message
00834   edm::LogError(mlConfigDb_) << ss.str();
00835   
00836 }
00837 
00838 // -----------------------------------------------------------------------------
00839 //
00840 bool SiStripConfigDb::checkFileExists( const std::string& path ) {
00841   fstream fs; 
00842   fs.open( path.c_str(), ios::in );
00843   if( !fs.is_open() ) { return false; }
00844   fs.close();
00845   return true;
00846 }
00847 
00848 // -----------------------------------------------------------------------------
00849 //
00850 void SiStripConfigDb::runs( SiStripConfigDb::Runs& runs ) const {
00851   
00852   runs.clear();
00853   
00854   // Check DF pointer
00855   DeviceFactory* const df = deviceFactory(__func__);
00856   if ( !df ) {
00857     edm::LogError(mlConfigDb_)
00858       << "[SiStripPartition::" << __func__ << "]"
00859       << " NULL pointer to DeviceFactory object!";
00860     return;
00861   }
00862   
00863   // Retrieve runs
00864   tkRunVector all;
00865 #ifdef USING_NEW_DATABASE_MODEL  
00866   all = df->getAllRuns();
00867 #else
00868   all = *(df->getAllRuns());
00869 #endif
00870 
00871   // Iterate through tkRunVector
00872   tkRunVector::const_iterator ii = all.begin();
00873   tkRunVector::const_iterator jj = all.end();
00874   for ( ; ii != jj; ++ii ) {
00875 
00876     // Check TkRun pointer
00877     if ( *ii ) {
00878 
00879       // Retrieve run type
00880       uint16_t type = (*ii)->getModeId( (*ii)->getMode() );
00881       sistrip::RunType temp = sistrip::UNKNOWN_RUN_TYPE;
00882       if      ( type ==  1 ) { temp = sistrip::PHYSICS; }
00883       else if ( type ==  2 ) { temp = sistrip::PEDESTALS; }
00884       else if ( type ==  3 ) { temp = sistrip::CALIBRATION; }
00885       else if ( type == 33 ) { temp = sistrip::CALIBRATION_DECO; }
00886       else if ( type ==  4 ) { temp = sistrip::OPTO_SCAN; }
00887       else if ( type ==  5 ) { temp = sistrip::APV_TIMING; }
00888       else if ( type ==  6 ) { temp = sistrip::APV_LATENCY; }
00889       else if ( type ==  7 ) { temp = sistrip::FINE_DELAY_PLL; }
00890       else if ( type == 10 ) { temp = sistrip::MULTI_MODE; }
00891       else if ( type ==  8 ) { temp = sistrip::FINE_DELAY_TTC; }
00892       else if ( type == 12 ) { temp = sistrip::FED_TIMING; }
00893       else if ( type == 13 ) { temp = sistrip::FED_CABLING; }
00894       else if ( type == 14 ) { temp = sistrip::VPSP_SCAN; }
00895       else if ( type == 15 ) { temp = sistrip::DAQ_SCOPE_MODE; }
00896       else if ( type == 16 ) { temp = sistrip::QUITE_FAST_CABLING; }
00897       else if ( type == 17 ) { temp = sistrip::FINE_DELAY; }
00898       else if ( type == 18 ) { temp = sistrip::PHYSICS_ZS; }
00899       else if ( type == 19 ) { temp = sistrip::CALIBRATION_SCAN; }
00900       else if ( type == 20 ) { temp = sistrip::CALIBRATION_SCAN_DECO; }
00901       else if ( type == 21 ) { temp = sistrip::FAST_CABLING; }
00902       else if ( type ==  0 ) { temp = sistrip::UNDEFINED_RUN_TYPE; }
00903       else                   { temp = sistrip::UNKNOWN_RUN_TYPE; }
00904       
00905       // Store run details
00906       Run r;
00907       r.type_      = temp;
00908       r.partition_ = (*ii)->getPartitionName();
00909       r.number_    = (*ii)->getRunNumber();
00910       runs.push_back(r);
00911 
00912     } else {
00913       edm::LogWarning(mlConfigDb_)
00914         << "[SiStripPartition::" << __func__ << "]"
00915         << " NULL pointer to TkRun object!";
00916     }
00917 
00918   }
00919 
00920 }
00921 
00922 // -----------------------------------------------------------------------------
00923 //
00924 void SiStripConfigDb::runs( const SiStripConfigDb::Runs& in,
00925                             SiStripConfigDb::RunsByType& out,
00926                             std::string optional_partition ) const {
00927   
00928   out.clear();
00929   
00930   // Check partition name (if not empty string)
00931   if ( !optional_partition.empty() ) {
00932     SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partition( optional_partition );
00933     if ( iter == dbParams_.partitions().end() ) { 
00934       edm::LogWarning(mlConfigDb_)
00935         << "[SiStripPartition::" << __func__ << "]"
00936         << " Partition name not found!";
00937       return; 
00938     }
00939   }
00940 
00941   // Iterate through runs
00942   Runs::const_iterator ii = in.begin();
00943   Runs::const_iterator jj = in.end();
00944   for ( ; ii != jj; ++ii ) {
00945     // Check partition name
00946     if ( ii->partition_ == optional_partition || optional_partition == "" ) { 
00947       // Check run type
00948       if ( ii->type_ != sistrip::UNKNOWN_RUN_TYPE &&
00949            ii->type_ != sistrip::UNDEFINED_RUN_TYPE ) { 
00950         // Check run number
00951         if ( ii->number_ ) { 
00952           bool found = false;
00953           if ( out.find( ii->type_ ) != out.end() ) {
00954             Runs::const_iterator irun = out[ ii->type_ ].begin();
00955             Runs::const_iterator jrun = out[ ii->type_ ].end();
00956             while ( !found && irun != jrun ) {
00957               if ( irun->number_ == ii->number_ ) { found = true; }
00958               ++irun;
00959             }
00960           }
00961           // Check if run number already found
00962           if ( !found ) { 
00963             out[ ii->type_ ].push_back( *ii ); 
00964           } else {
00965             //        edm::LogWarning(mlConfigDb_)
00966             //          << "[SiStripPartition::" << __func__ << "]"
00967             //          << " Run number already found!";
00968           }
00969         } else {
00970           //        edm::LogWarning(mlConfigDb_)
00971           //          << "[SiStripPartition::" << __func__ << "]"
00972           //          << " NULL run number!";
00973         }
00974       } else {
00975         //        edm::LogWarning(mlConfigDb_)
00976         //          << "[SiStripPartition::" << __func__ << "]"
00977         //          << " Unexpected run type!";
00978       }
00979     } else {
00980       //        edm::LogWarning(mlConfigDb_)
00981       //          << "[SiStripPartition::" << __func__ << "]"
00982       //          << " Partition name does not match!";
00983     }
00984 
00985   }
00986 
00987 }
00988 
00989 // -----------------------------------------------------------------------------
00990 //
00991 void SiStripConfigDb::runs( const SiStripConfigDb::Runs& in,
00992                             SiStripConfigDb::RunsByPartition& out,
00993                             sistrip::RunType optional_type ) const {
00994   
00995   out.clear();
00996 
00997   // Iterate through runs
00998   Runs::const_iterator ii = in.begin();
00999   Runs::const_iterator jj = in.end();
01000   for ( ; ii != jj; ++ii ) {
01001     // Check partition name
01002     if ( ii->partition_ != "" ) {
01003       // Check run type
01004       if ( ii->type_ == optional_type || optional_type == sistrip::UNDEFINED_RUN_TYPE ) { 
01005         // Check run number
01006         if ( ii->number_ ) { 
01007           bool found = false;
01008           if ( out.find( ii->partition_ ) != out.end() ) {
01009             Runs::const_iterator irun = out[ ii->partition_ ].begin();
01010             Runs::const_iterator jrun = out[ ii->partition_ ].end();
01011             while ( !found && irun != jrun ) {
01012               if ( irun->number_ == ii->number_ ) { found = true; }
01013               ++irun;
01014             }
01015           }
01016           // Check if run number already found
01017           if ( !found ) { 
01018             out[ ii->partition_ ].push_back( *ii ); 
01019           } else {
01020             //        edm::LogWarning(mlConfigDb_)
01021             //          << "[SiStripPartition::" << __func__ << "]"
01022             //          << " Run number already found!";
01023           }
01024         } else {
01025           //        edm::LogWarning(mlConfigDb_)
01026           //          << "[SiStripPartition::" << __func__ << "]"
01027           //          << " NULL run number!";
01028         }
01029       } else {
01030         //        edm::LogWarning(mlConfigDb_)
01031         //          << "[SiStripPartition::" << __func__ << "]"
01032         //          << " Run type does not match!";
01033       }
01034     } else {
01035       //        edm::LogWarning(mlConfigDb_)
01036       //          << "[SiStripPartition::" << __func__ << "]"
01037       //          << " NULL value for partition!";
01038     }
01039 
01040   }
01041 
01042 }
01043 
01044 // -----------------------------------------------------------------------------
01045 //
01046 void SiStripConfigDb::partitions( std::list<std::string>& partitions ) const {
01047 
01048   partitions.clear();
01049 
01050   // Check DF pointer
01051   DeviceFactory* const df = deviceFactory(__func__);
01052   if ( !df ) {
01053     edm::LogError(mlConfigDb_)
01054       << "[SiStripPartition::" << __func__ << "]"
01055       << " NULL pointer to DeviceFactory object!";
01056     return;
01057   }
01058 
01059 #ifdef USING_NEW_DATABASE_MODEL
01060   partitions = df->getAllPartitionNames();
01061 #else
01062   edm::LogWarning(mlConfigDb_)
01063     << "[SiStripPartition::" << __func__ << "]"
01064     << " No partitions returned for old model!";
01065 #endif
01066   
01067 }
01068 
01069   

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