CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_1/src/OnlineDB/SiStripConfigDb/src/SiStripConfigDb.cc

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