Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-05-31 22:36:08

0001 
0002 #include "OnlineDB/SiStripConfigDb/interface/SiStripConfigDb.h"
0003 #include "DataFormats/SiStripCommon/interface/SiStripConstants.h"
0004 #include "DataFormats/SiStripCommon/interface/SiStripEnumsAndStrings.h"
0005 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0006 #include <iostream>
0007 #include <fstream>
0008 
0009 using namespace sistrip;
0010 
0011 // -----------------------------------------------------------------------------
0012 //
0013 std::atomic<uint32_t> SiStripConfigDb::cntr_{0};
0014 
0015 // -----------------------------------------------------------------------------
0016 //
0017 std::atomic<bool> SiStripConfigDb::allowCalibUpload_{false};
0018 
0019 // -----------------------------------------------------------------------------
0020 //
0021 SiStripConfigDb::SiStripConfigDb(const edm::ParameterSet& pset, const edm::ActivityRegistry& activity)
0022     : factory_(nullptr),
0023       dbCache_(nullptr),
0024       dbParams_(),
0025       // Local cache
0026       connections_(),
0027       devices_(),
0028       feds_(),
0029       dcuDetIds_(),
0030       analyses_(),
0031       apvDevices_(),
0032       muxDevices_(),
0033       dcuDevices_(),
0034       lldDevices_(),
0035       pllDevices_(),
0036       dohDevices_(),
0037       typedDevices_(),
0038       fedIds_(),
0039       // Misc
0040       usingStrips_(true),
0041       openConnection_(false) {
0042   auto count = ++cntr_;
0043   edm::LogVerbatim(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0044                                 << " Constructing database service..."
0045                                 << " (Class instance: " << count << ")";
0046 
0047   // Set DB connection parameters
0048   dbParams_.reset();
0049   dbParams_.pset(pset);
0050   //edm::LogVerbatim(mlConfigDb_) << dbParams_;
0051 
0052   // Open connection
0053   openDbConnection();
0054 }
0055 
0056 // -----------------------------------------------------------------------------
0057 //
0058 SiStripConfigDb::~SiStripConfigDb() {
0059   closeDbConnection();
0060   LogTrace(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0061                         << " Destructing object...";
0062   --cntr_;
0063 }
0064 
0065 // -----------------------------------------------------------------------------
0066 //
0067 SiStripConfigDb::DeviceAddress::DeviceAddress()
0068     : fecCrate_(sistrip::invalid_),
0069       fecSlot_(sistrip::invalid_),
0070       fecRing_(sistrip::invalid_),
0071       ccuAddr_(sistrip::invalid_),
0072       ccuChan_(sistrip::invalid_),
0073       lldChan_(sistrip::invalid_),
0074       i2cAddr_(sistrip::invalid_),
0075       fedId_(sistrip::invalid_),
0076       feUnit_(sistrip::invalid_),
0077       feChan_(sistrip::invalid_) {
0078   reset();
0079 }
0080 
0081 // -----------------------------------------------------------------------------
0082 //
0083 void SiStripConfigDb::DeviceAddress::reset() {
0084   fecCrate_ = sistrip::invalid_;
0085   fecSlot_ = sistrip::invalid_;
0086   fecRing_ = sistrip::invalid_;
0087   ccuAddr_ = sistrip::invalid_;
0088   ccuChan_ = sistrip::invalid_;
0089   lldChan_ = sistrip::invalid_;
0090   i2cAddr_ = sistrip::invalid_;
0091   fedId_ = sistrip::invalid_;
0092   feUnit_ = sistrip::invalid_;
0093   feChan_ = sistrip::invalid_;
0094 }
0095 
0096 // -----------------------------------------------------------------------------
0097 //
0098 void SiStripConfigDb::openDbConnection() {
0099   LogTrace(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0100                         << " Opening connection to database...";
0101 
0102   // Check if connection already exists
0103   if (openConnection_) {
0104     edm::LogWarning(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0105                                  << " Connection already open!";
0106     return;
0107   }
0108   openConnection_ = true;
0109 
0110   // Establish database connection
0111   if (dbParams_.usingDb()) {
0112     if (dbParams_.usingDbCache()) {
0113       usingDatabaseCache();
0114     } else {
0115       usingDatabase();
0116     }
0117   } else {
0118     usingXmlFiles();
0119   }
0120 
0121   std::stringstream ss;
0122   ss << "[SiStripConfigDb::" << __func__ << "]"
0123      << " Database connection parameters: " << std::endl
0124      << dbParams_;
0125   edm::LogVerbatim(mlConfigDb_) << ss.str();
0126 
0127   // Clear local caches
0128   clearLocalCache();
0129 
0130   LogTrace(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0131                         << " Opened connection to database!";
0132 }
0133 
0134 // -----------------------------------------------------------------------------
0135 //
0136 void SiStripConfigDb::closeDbConnection() {
0137   LogTrace(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0138                         << " Closing connection to database...";
0139 
0140   // Check if connection exists
0141   if (!openConnection_) {
0142     edm::LogWarning(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0143                                  << " No connection open!";
0144     return;
0145   }
0146   openConnection_ = false;
0147 
0148   // Clear local caches
0149   clearLocalCache();
0150 
0151   try {
0152     if (factory_) {
0153       delete factory_;
0154     }
0155   } catch (...) {
0156     handleException(__func__, "Attempting to delete DeviceFactory object...");
0157   }
0158   factory_ = nullptr;
0159 
0160   try {
0161     if (dbCache_) {
0162       delete dbCache_;
0163     }
0164   } catch (...) {
0165     handleException(__func__, "Attempting to delete DbClient object...");
0166   }
0167   dbCache_ = nullptr;
0168 
0169   LogTrace(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0170                         << " Closed connection to database...";
0171 }
0172 
0173 // -----------------------------------------------------------------------------
0174 //
0175 void SiStripConfigDb::clearLocalCache() {
0176   LogTrace(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0177                         << " Clearing local caches...";
0178 
0179   clearFedConnections();
0180   clearDeviceDescriptions();
0181   clearFedDescriptions();
0182   clearDcuDetIds();
0183   clearAnalysisDescriptions();
0184 
0185   typedDevices_.clear();
0186   fedIds_.clear();
0187 }
0188 
0189 // -----------------------------------------------------------------------------
0190 //
0191 DeviceFactory* const SiStripConfigDb::deviceFactory(std::string method_name) const {
0192   if (factory_) {
0193     return factory_;
0194   } else {
0195     if (!method_name.empty()) {
0196       stringstream ss;
0197       ss << "[SiStripConfigDb::" << __func__ << "]"
0198          << " NULL pointer to DeviceFactory requested by"
0199          << " method SiStripConfigDb::" << method_name << "()!";
0200       edm::LogWarning(mlConfigDb_) << ss.str();
0201     }
0202     return nullptr;
0203   }
0204 }
0205 
0206 // -----------------------------------------------------------------------------
0207 //
0208 DbClient* const SiStripConfigDb::databaseCache(std::string method_name) const {
0209   if (dbCache_) {
0210     return dbCache_;
0211   } else {
0212     if (!method_name.empty()) {
0213       stringstream ss;
0214       ss << "[SiStripConfigDb::" << __func__ << "]"
0215          << " NULL pointer to DbClient requested by"
0216          << " method SiStripConfigDb::" << method_name << "()!";
0217       edm::LogWarning(mlConfigDb_) << ss.str();
0218     }
0219     return nullptr;
0220   }
0221 }
0222 
0223 // -----------------------------------------------------------------------------
0224 //
0225 void SiStripConfigDb::usingDatabase() {
0226   // Retrieve connection params from CONFDB env. var. and override .cfg values
0227   std::string user = "";
0228   std::string passwd = "";
0229   std::string pToPrint = "******";
0230   std::string path = "";
0231   DbAccess::getDbConfiguration(user, passwd, path);
0232   if (!user.empty() && !passwd.empty() && !path.empty()) {
0233     std::stringstream ss;
0234     ss << "[SiStripConfigDb::" << __func__ << "]"
0235        << " Setting \"user/passwd@path\" to \"" << user << "/" << pToPrint << "@" << path
0236        << "\" using 'CONFDB' environmental variable";
0237     if (dbParams_.user() != null_ || dbParams_.passwd() != null_ || dbParams_.path() != null_) {
0238       ss << " (Overwriting existing value of \"" << dbParams_.user() << "/" << pToPrint << "@" << dbParams_.path()
0239          << "\" read from .cfg file)";
0240     }
0241     edm::LogVerbatim(mlConfigDb_) << ss.str() << std::endl;
0242     dbParams_.confdb(user, passwd, path);
0243 
0244   } else if (dbParams_.user() != null_ && dbParams_.passwd() != null_ && dbParams_.path() != null_) {
0245     std::stringstream ss;
0246     ss << "[SiStripConfigDb::" << __func__ << "]"
0247        << " Setting \"user/passwd@path\" to \"" << dbParams_.user() << "/" << pToPrint << "@" << dbParams_.path()
0248        << "\" using 'ConfDb' configurable read from .cfg file";
0249     edm::LogVerbatim(mlConfigDb_) << ss.str();
0250 
0251   } else {
0252     edm::LogWarning(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0253                                  << " Unable to retrieve 'user/passwd@path' parameters"
0254                                  << " from 'CONFDB' environmental variable or .cfg file"
0255                                  << " (present value is \"" << user << "/" << pToPrint << "@" << path
0256                                  << "\"). Aborting connection to database...";
0257     return;
0258   }
0259 
0260   // Check TNS_ADMIN environmental variable
0261   std::string pattern = "TNS_ADMIN";
0262   std::string tns_admin = "/afs/cern.ch/project/oracle/admin";
0263   if (std::getenv(pattern.c_str()) != nullptr) {
0264     tns_admin = std::getenv(pattern.c_str());
0265     edm::LogVerbatim(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0266                                   << " TNS_ADMIN is set to: \"" << tns_admin << "\"";
0267   } else {
0268     edm::LogWarning(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0269                                  << " TNS_ADMIN is not set!"
0270                                  << " Trying to use /afs and setting to: \"" << tns_admin << "\"";
0271   }
0272 
0273   // Retrieve TNS_ADMIN from .cfg file and override
0274   if (!dbParams_.tnsAdmin().empty()) {
0275     std::stringstream ss;
0276     ss << "[SiStripConfigDb::" << __func__ << "]"
0277        << " Overriding TNS_ADMIN value using cfg file!" << std::endl
0278        << "  Original value : \"" << tns_admin << "\"!" << std::endl
0279        << "  New value      : \"" << dbParams_.tnsAdmin() << "\"!";
0280     tns_admin = dbParams_.tnsAdmin();
0281     edm::LogVerbatim(mlConfigDb_) << ss.str();
0282   }
0283 
0284   // Remove trailing slash and set TNS_ADMIN
0285   if (tns_admin.empty()) {
0286     tns_admin = ".";
0287   }
0288   std::string slash = tns_admin.substr(tns_admin.size() - 1, 1);
0289   if (slash == sistrip::dir_) {
0290     tns_admin = tns_admin.substr(0, tns_admin.size() - 1);
0291   }
0292   setenv(pattern.c_str(), tns_admin.c_str(), 1);
0293 
0294   // Check if database is found in tnsnames.ora file
0295   std::string filename(tns_admin + "/tnsnames.ora");
0296   std::ifstream tnsnames_ora(filename.c_str());
0297   bool ok = false;
0298   if (tnsnames_ora.is_open()) {
0299     std::string line;
0300     while (!tnsnames_ora.eof()) {
0301       getline(tnsnames_ora, line);
0302       if (!dbParams_.path().empty() && line.find(dbParams_.path()) != std::string::npos) {
0303         ok = true;
0304       }
0305     }
0306   } else {
0307     edm::LogWarning(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0308                                  << " Cannot open file \"" << filename << "\"";
0309   }
0310 
0311   if (ok) {
0312     LogTrace(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0313                           << " Found database account \"" << dbParams_.path() << "\" in file \"" << filename << "\"!";
0314   } else {
0315     edm::LogWarning(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0316                                  << " Cannot find database account \"" << dbParams_.path() << "\" in file \""
0317                                  << filename << "\""
0318                                  << " Aborting connection to database...";
0319     return;
0320   }
0321 
0322   // Create device factory object
0323   try {
0324     LogTrace(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0325                           << " Creating DeviceFactory object...";
0326     factory_ = new DeviceFactory(dbParams_.user(), dbParams_.passwd(), dbParams_.path());
0327     LogTrace(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0328                           << " Created DeviceFactory object!";
0329   } catch (...) {
0330     std::stringstream ss;
0331     ss << "Failed to connect to database using parameters '" << dbParams_.user() << "/" << pToPrint << "@"
0332        << dbParams_.path() << "' and partitions '" << dbParams_.partitionNames(dbParams_.partitionNames()) << "'";
0333     handleException(__func__, ss.str());
0334     return;
0335   }
0336 
0337   // Check for valid pointer to DeviceFactory
0338   if (deviceFactory(__func__)) {
0339     std::stringstream ss;
0340     ss << "[SiStripConfigDb::" << __func__ << "]"
0341        << " DeviceFactory created at address 0x" << std::hex << std::setw(8) << std::setfill('0') << factory_
0342        << std::dec << ", using database account with parameters '" << dbParams_.user() << "/" << pToPrint << "@"
0343        << dbParams_.path();
0344     LogTrace(mlConfigDb_) << ss.str();
0345   } else {
0346     edm::LogError(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0347                                << " NULL pointer to DeviceFactory!"
0348                                << " Unable to connect to database using connection parameters '" << dbParams_.user()
0349                                << "/" << pToPrint << "@" << dbParams_.path() << "' and partitions '"
0350                                << dbParams_.partitionNames(dbParams_.partitionNames()) << "'";
0351     return;
0352   }
0353 
0354   try {
0355     deviceFactory(__func__)->setUsingDb(dbParams_.usingDb());
0356   } catch (...) {
0357     handleException(__func__, "Attempted to 'setUsingDb'");
0358   }
0359 
0360   // Retrieve partition name from ENV_CMS_TK_PARTITION env. var. and override .cfg value
0361   std::string partition = "ENV_CMS_TK_PARTITION";
0362   if (std::getenv(partition.c_str()) != nullptr) {
0363     std::stringstream ss;
0364     ss << "[SiStripConfigDb::" << __func__ << "]"
0365        << " Setting \"partitions\" to \"" << std::getenv(partition.c_str())
0366        << "\" using 'ENV_CMS_TK_PARTITION' environmental variable";
0367     if (!dbParams_.partitionNames().empty()) {
0368       ss << " (Overwriting existing value of \"" << dbParams_.partitionNames(dbParams_.partitionNames())
0369          << "\" read from .cfg file)";
0370     }
0371     edm::LogVerbatim(mlConfigDb_) << ss.str() << std::endl;
0372 
0373     // Build partitions from env. var.
0374     std::vector<std::string> partitions = dbParams_.partitionNames(std::getenv(partition.c_str()));
0375     if (!partitions.empty()) {
0376       dbParams_.clearPartitions();
0377       std::vector<std::string>::iterator ii = partitions.begin();
0378       std::vector<std::string>::iterator jj = partitions.end();
0379       for (; ii != jj; ++ii) {
0380         SiStripPartition partition(*ii);
0381         dbParams_.addPartition(partition);
0382       }
0383     }
0384 
0385   } else if (!dbParams_.partitionNames().empty()) {
0386     std::stringstream ss;
0387     ss << "[SiStripConfigDb::" << __func__ << "]"
0388        << " Setting \"partitions\" to \"" << dbParams_.partitionNames(dbParams_.partitionNames())
0389        << "\" using 'PartitionName' configurables read from .cfg file";
0390     edm::LogVerbatim(mlConfigDb_) << ss.str();
0391   } else {
0392     edm::LogWarning(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0393                                  << " Unable to retrieve 'partition' parameter"
0394                                  << " from 'CONFDB' environmental variable or .cfg file!"
0395                                  << " Aborting connection to database...";
0396     return;
0397   }
0398 
0399   // Check if should use current state, run number or versions
0400   SiStripDbParams::SiStripPartitions::iterator ip = dbParams_.partitions().begin();
0401   SiStripDbParams::SiStripPartitions::iterator jp = dbParams_.partitions().end();
0402   for (; ip != jp; ++ip) {
0403     ip->second.update(this);
0404   }
0405 }
0406 
0407 // -----------------------------------------------------------------------------
0408 //
0409 void SiStripConfigDb::usingDatabaseCache() {
0410   // Reset all DbParams except for those concerning database cache
0411   SiStripDbParams temp;
0412   temp = dbParams_;
0413   dbParams_.reset();
0414   dbParams_.usingDb(temp.usingDb());
0415   dbParams_.usingDbCache(temp.usingDbCache());
0416   dbParams_.sharedMemory(temp.sharedMemory());
0417 
0418   // Add default partition
0419   dbParams_.addPartition(SiStripPartition(SiStripPartition::defaultPartitionName_));
0420 
0421   // Check shared memory name from .cfg file
0422   if (dbParams_.sharedMemory().empty()) {
0423     std::stringstream ss;
0424     ss << "[SiStripConfigDb::" << __func__ << "]"
0425        << " Empty string for shared memory name!"
0426        << " Cannot accept shared memory!";
0427     edm::LogError(mlConfigDb_) << ss.str();
0428     return;
0429   }
0430 
0431   // Create database cache object
0432   try {
0433     LogTrace(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0434                           << " Creating DbClient object...";
0435     dbCache_ = new DbClient(dbParams_.sharedMemory());
0436     LogTrace(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0437                           << " Created DbClient object...";
0438   } catch (...) {
0439     std::stringstream ss;
0440     ss << "Failed to connect to database cache using shared memory name: '" << dbParams_.sharedMemory() << "'!";
0441     handleException(__func__, ss.str());
0442     return;
0443   }
0444 
0445   // Check for valid pointer to DbClient object
0446   if (databaseCache(__func__)) {
0447     std::stringstream ss;
0448     ss << "[SiStripConfigDb::" << __func__ << "]"
0449        << " DbClient object created at address 0x" << std::hex << std::setw(8) << std::setfill('0') << dbCache_
0450        << std::dec << " using shared memory name '" << dbParams_.sharedMemory() << "'";
0451     LogTrace(mlConfigDb_) << ss.str();
0452   } else {
0453     edm::LogError(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0454                                << " NULL pointer to DbClient object!"
0455                                << " Unable to connect to database cache using shared memory name '"
0456                                << dbParams_.sharedMemory() << "'";
0457     return;
0458   }
0459 
0460   // Try retrieve descriptions from Database Client
0461   try {
0462     databaseCache(__func__)->parse();
0463   } catch (...) {
0464     handleException(__func__, "Attempted to called DbClient::parse() method");
0465   }
0466 }
0467 
0468 // -----------------------------------------------------------------------------
0469 //
0470 void SiStripConfigDb::usingXmlFiles() {
0471   LogTrace(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0472                         << " Using XML description files...";
0473 
0474   // Create device factory object
0475   try {
0476     factory_ = new DeviceFactory();
0477   } catch (...) {
0478     handleException(__func__, "Attempting to create DeviceFactory for use with xml files");
0479   }
0480 
0481   // Check for valid pointer to DeviceFactory
0482   if (deviceFactory(__func__)) {
0483     std::stringstream ss;
0484     ss << "[SiStripConfigDb::" << __func__ << "]"
0485        << " DeviceFactory created at address 0x" << std::hex << std::setw(8) << std::setfill('0') << factory_
0486        << std::dec << ", using XML description files";
0487     LogTrace(mlConfigDb_) << ss.str();
0488   } else {
0489     edm::LogError(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0490                                << " NULL pointer to DeviceFactory!"
0491                                << " Unable to connect to database!";
0492     return;
0493   }
0494 
0495   try {
0496     deviceFactory(__func__)->setUsingDb(dbParams_.usingDb());
0497   } catch (...) {
0498     handleException(__func__, "Attempted to 'setUsingDb'");
0499   }
0500 
0501   // Iterate through partitions
0502   SiStripDbParams::SiStripPartitions::const_iterator ip = dbParams_.partitions().begin();
0503   SiStripDbParams::SiStripPartitions::const_iterator jp = dbParams_.partitions().end();
0504   for (; ip != jp; ++ip) {
0505     // Input module.xml file
0506     if (ip->second.inputModuleXml().empty()) {
0507       edm::LogWarning(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0508                                    << " NULL path to input 'module.xml' file!";
0509     } else {
0510       if (checkFileExists(ip->second.inputModuleXml())) {
0511         try {
0512           deviceFactory(__func__)->addConnectionFileName(ip->second.inputModuleXml());
0513         } catch (...) {
0514           handleException(__func__);
0515         }
0516         LogTrace(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0517                               << " Added input 'module.xml' file: " << ip->second.inputModuleXml();
0518       } else {
0519         edm::LogWarning(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0520                                      << " No 'module.xml' file found at " << ip->second.inputModuleXml();
0521         ip->second.inputModuleXml() = "";
0522       }
0523     }
0524 
0525     // Input dcuinfo.xml file
0526     if (ip->second.inputDcuInfoXml().empty()) {
0527       edm::LogWarning(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0528                                    << " NULL path to input 'dcuinfo.xml' file!";
0529     } else {
0530       if (checkFileExists(ip->second.inputDcuInfoXml())) {
0531         try {
0532           deviceFactory(__func__)->addTkDcuInfoFileName(ip->second.inputDcuInfoXml());
0533         } catch (...) {
0534           handleException(__func__);
0535         }
0536         LogTrace(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0537                               << " Added 'dcuinfo.xml' file: " << ip->second.inputDcuInfoXml();
0538       } else {
0539         edm::LogWarning(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0540                                      << " No 'dcuinfo.xml' file found at " << ip->second.inputDcuInfoXml();
0541         ip->second.inputDcuInfoXml() = "";
0542       }
0543     }
0544 
0545     // Input FEC xml files
0546     if (ip->second.inputFecXml().empty()) {
0547       edm::LogWarning(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0548                                    << " NULL paths to input 'fec.xml' files!";
0549     } else {
0550       for (const auto& iter : ip->second.inputFecXml()) {
0551         if (iter.empty()) {
0552           edm::LogWarning(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0553                                        << " NULL path to input 'fec.xml' file!";
0554         } else {
0555           if (checkFileExists(iter)) {
0556             try {
0557               deviceFactory(__func__)->addFecFileName(iter);
0558             } catch (...) {
0559               handleException(__func__);
0560             }
0561             LogTrace(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0562                                   << " Added 'fec.xml' file: " << iter;
0563           } else {
0564             edm::LogWarning(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0565                                          << " No 'fec.xml' file found at " << iter;
0566           }
0567         }
0568       }
0569     }
0570 
0571     // Input FED xml files
0572     if (ip->second.inputFedXml().empty()) {
0573       edm::LogWarning(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0574                                    << " NULL paths to input 'fed.xml' files!";
0575     } else {
0576       for (const auto& iter : ip->second.inputFedXml()) {
0577         if (iter.empty()) {
0578           edm::LogWarning(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0579                                        << " NULL path to input 'fed.xml' file!";
0580         } else {
0581           if (checkFileExists(iter)) {
0582             try {
0583               deviceFactory(__func__)->addFedFileName(iter);
0584             } catch (...) {
0585               handleException(__func__);
0586             }
0587             LogTrace(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0588                                   << " Added 'fed.xml' file: " << iter;
0589           } else {
0590             edm::LogWarning(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0591                                          << " No 'fed.xml' file found at " << iter;
0592           }
0593         }
0594       }
0595     }
0596   }
0597 
0598   // Output module.xml file
0599   if (dbParams_.outputModuleXml().empty()) {
0600     edm::LogWarning(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0601                                  << " NULL path to output 'module.xml' file!"
0602                                  << " Setting to '/tmp/module.xml'...";
0603     dbParams_.outputModuleXml() = "/tmp/module.xml";
0604   } else {
0605     try {
0606       ConnectionFactory* factory = deviceFactory(__func__);
0607       factory->setOutputFileName(dbParams_.outputModuleXml());
0608     } catch (...) {
0609       handleException(__func__, "Problems setting output 'module.xml' file!");
0610     }
0611   }
0612 
0613   // Output dcuinfo.xml file
0614   if (dbParams_.outputDcuInfoXml().empty()) {
0615     edm::LogWarning(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0616                                  << " NULL path to output 'dcuinfo.xml' file!"
0617                                  << " Setting to '/tmp/dcuinfo.xml'...";
0618     dbParams_.outputModuleXml() = "/tmp/dcuinfo.xml";
0619   } else {
0620     try {
0621       TkDcuInfoFactory* factory = deviceFactory(__func__);
0622       factory->setOutputFileName(dbParams_.outputDcuInfoXml());
0623     } catch (...) {
0624       handleException(__func__, "Problems setting output 'dcuinfo.xml' file!");
0625     }
0626   }
0627 
0628   // Output fec.xml file
0629   if (dbParams_.outputFecXml().empty()) {
0630     edm::LogWarning(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0631                                  << " NULL path to output 'fec.xml' file!"
0632                                  << " Setting to '/tmp/fec.xml'...";
0633     dbParams_.outputFecXml() = "/tmp/fec.xml";
0634   } else {
0635     try {
0636       FecDeviceFactory* factory = deviceFactory(__func__);
0637       factory->setOutputFileName(dbParams_.outputFecXml());
0638     } catch (...) {
0639       handleException(__func__, "Problems setting output 'fec.xml' file!");
0640     }
0641   }
0642 
0643   // Output fed.xml file
0644   if (dbParams_.outputFedXml().empty()) {
0645     edm::LogWarning(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0646                                  << " NULL path to output 'fed.xml' file!"
0647                                  << " Setting to '/tmp/fed.xml'...";
0648     dbParams_.outputFedXml() = "/tmp/fed.xml";
0649   } else {
0650     try {
0651       Fed9U::Fed9UDeviceFactory* factory = deviceFactory(__func__);
0652       factory->setOutputFileName(dbParams_.outputFedXml());
0653     } catch (...) {
0654       handleException(__func__, "Problems setting output 'fed.xml' file!");
0655     }
0656   }
0657 }
0658 
0659 // -----------------------------------------------------------------------------
0660 //
0661 void SiStripConfigDb::handleException(const std::string& method_name, const std::string& extra_info) const {
0662   std::stringstream ss;
0663   try {
0664     throw;  // rethrow caught exception to be dealt with below
0665   }
0666 
0667   catch (const cms::Exception& e) {
0668     ss << " Caught cms::Exception in method " << method_name << " with message: " << std::endl << e.what();
0669     if (!extra_info.empty()) {
0670       ss << "Additional info: " << extra_info << std::endl;
0671     }
0672     //throw e; // rethrow cms::Exception
0673   }
0674 
0675   catch (const oracle::occi::SQLException& e) {
0676     ss << " Caught oracle::occi::SQLException in method " << method_name << " with message: " << std::endl
0677        << e.getMessage();
0678     if (!extra_info.empty()) {
0679       ss << "Additional info: " << extra_info << std::endl;
0680     }
0681     //throw cms::Exception(mlConfigDb_) << ss.str() << std::endl;
0682   }
0683 
0684   catch (const FecExceptionHandler& e) {
0685     ss << " Caught FecExceptionHandler exception in method " << method_name << " with message: " << std::endl
0686        << const_cast<FecExceptionHandler&>(e).what();
0687     if (!extra_info.empty()) {
0688       ss << "Additional info: " << extra_info << std::endl;
0689     }
0690     //throw cms::Exception(mlConfigDb_) << ss.str() << std::endl;
0691   }
0692 
0693   //   catch ( const Fed9UDeviceFactoryException& e ) {
0694   //     ss << " Caught Fed9UDeviceFactoryException exception in method "
0695   //        << method_name << " with message: " << std::endl
0696   //        << e.what();
0697   //     if ( extra_info != "" ) { ss << "Additional info: " << extra_info << std::endl; }
0698   //     //throw cms::Exception(mlConfigDb_) << ss.str() << std::endl;
0699   //   }
0700 
0701   catch (const ICUtils::ICException& e) {
0702     ss << " Caught ICUtils::ICException in method " << method_name << " with message: " << std::endl << e.what();
0703     if (!extra_info.empty()) {
0704       ss << "Additional info: " << extra_info << std::endl;
0705     }
0706     //throw cms::Exception(mlConfigDb_) << ss.str() << std::endl;
0707   }
0708 
0709   catch (const exception& e) {
0710     ss << " Caught std::exception in method " << method_name << " with message: " << std::endl << e.what();
0711     if (!extra_info.empty()) {
0712       ss << "Additional info: " << extra_info << std::endl;
0713     }
0714     //throw cms::Exception(mlConfigDb_) << ss.str() << std::endl;
0715   }
0716 
0717   catch (...) {
0718     ss << " Caught unknown exception in method " << method_name << " (No message) " << std::endl;
0719     if (!extra_info.empty()) {
0720       ss << "Additional info: " << extra_info << std::endl;
0721     }
0722     //throw cms::Exception(mlConfigDb_) << ss.str() << std::endl;
0723   }
0724 
0725   // Message
0726   edm::LogError(mlConfigDb_) << ss.str();
0727 }
0728 
0729 // -----------------------------------------------------------------------------
0730 //
0731 bool SiStripConfigDb::checkFileExists(const std::string& path) {
0732   fstream fs;
0733   fs.open(path.c_str(), ios::in);
0734   if (!fs.is_open()) {
0735     return false;
0736   }
0737   fs.close();
0738   return true;
0739 }
0740 
0741 // -----------------------------------------------------------------------------
0742 //
0743 void SiStripConfigDb::runs(SiStripConfigDb::Runs& runs) const {
0744   runs.clear();
0745 
0746   // Check DF pointer
0747   DeviceFactory* const df = deviceFactory(__func__);
0748   if (!df) {
0749     edm::LogError(mlConfigDb_) << "[SiStripPartition::" << __func__ << "]"
0750                                << " NULL pointer to DeviceFactory object!";
0751     return;
0752   }
0753 
0754   // Retrieve runs
0755   tkRunVector all;
0756   all = df->getAllRuns();
0757 
0758   // Iterate through tkRunVector
0759   tkRunVector::const_iterator ii = all.begin();
0760   tkRunVector::const_iterator jj = all.end();
0761   for (; ii != jj; ++ii) {
0762     // Check TkRun pointer
0763     if (*ii) {
0764       // Retrieve run type
0765       uint16_t type = (*ii)->getModeId((*ii)->getMode());
0766       sistrip::RunType temp = sistrip::UNKNOWN_RUN_TYPE;
0767       if (type == 1) {
0768         temp = sistrip::PHYSICS;
0769       } else if (type == 2) {
0770         temp = sistrip::PEDESTALS;
0771       } else if (type == 3) {
0772         temp = sistrip::CALIBRATION;
0773       } else if (type == 33) {
0774         temp = sistrip::CALIBRATION_DECO;
0775       } else if (type == 4) {
0776         temp = sistrip::OPTO_SCAN;
0777       } else if (type == 5) {
0778         temp = sistrip::APV_TIMING;
0779       } else if (type == 6) {
0780         temp = sistrip::APV_LATENCY;
0781       } else if (type == 7) {
0782         temp = sistrip::FINE_DELAY_PLL;
0783       } else if (type == 10) {
0784         temp = sistrip::MULTI_MODE;
0785       } else if (type == 8) {
0786         temp = sistrip::FINE_DELAY_TTC;
0787       } else if (type == 12) {
0788         temp = sistrip::FED_TIMING;
0789       } else if (type == 13) {
0790         temp = sistrip::FED_CABLING;
0791       } else if (type == 14) {
0792         temp = sistrip::VPSP_SCAN;
0793       } else if (type == 15) {
0794         temp = sistrip::DAQ_SCOPE_MODE;
0795       } else if (type == 16) {
0796         temp = sistrip::QUITE_FAST_CABLING;
0797       } else if (type == 17) {
0798         temp = sistrip::FINE_DELAY;
0799       } else if (type == 18) {
0800         temp = sistrip::PHYSICS_ZS;
0801       } else if (type == 19) {
0802         temp = sistrip::CALIBRATION_SCAN;
0803       } else if (type == 20) {
0804         temp = sistrip::CALIBRATION_SCAN_DECO;
0805       } else if (type == 21) {
0806         temp = sistrip::FAST_CABLING;
0807       } else if (type == 0) {
0808         temp = sistrip::UNDEFINED_RUN_TYPE;
0809       } else {
0810         temp = sistrip::UNKNOWN_RUN_TYPE;
0811       }
0812 
0813       // Store run details
0814       Run r;
0815       r.type_ = temp;
0816       r.partition_ = (*ii)->getPartitionName();
0817       r.number_ = (*ii)->getRunNumber();
0818       runs.push_back(r);
0819 
0820     } else {
0821       edm::LogWarning(mlConfigDb_) << "[SiStripPartition::" << __func__ << "]"
0822                                    << " NULL pointer to TkRun object!";
0823     }
0824   }
0825 }
0826 
0827 // -----------------------------------------------------------------------------
0828 //
0829 void SiStripConfigDb::runs(const SiStripConfigDb::Runs& in,
0830                            SiStripConfigDb::RunsByType& out,
0831                            std::string optional_partition) const {
0832   out.clear();
0833 
0834   // Check partition name (if not empty string)
0835   if (!optional_partition.empty()) {
0836     SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partition(optional_partition);
0837     if (iter == dbParams_.partitions().end()) {
0838       edm::LogWarning(mlConfigDb_) << "[SiStripPartition::" << __func__ << "]"
0839                                    << " Partition name not found!";
0840       return;
0841     }
0842   }
0843 
0844   // Iterate through runs
0845   Runs::const_iterator ii = in.begin();
0846   Runs::const_iterator jj = in.end();
0847   for (; ii != jj; ++ii) {
0848     // Check partition name
0849     if (ii->partition_ == optional_partition || optional_partition.empty()) {
0850       // Check run type
0851       if (ii->type_ != sistrip::UNKNOWN_RUN_TYPE && ii->type_ != sistrip::UNDEFINED_RUN_TYPE) {
0852         // Check run number
0853         if (ii->number_) {
0854           bool found = false;
0855           if (out.find(ii->type_) != out.end()) {
0856             Runs::const_iterator irun = out[ii->type_].begin();
0857             Runs::const_iterator jrun = out[ii->type_].end();
0858             while (!found && irun != jrun) {
0859               if (irun->number_ == ii->number_) {
0860                 found = true;
0861               }
0862               ++irun;
0863             }
0864           }
0865           // Check if run number already found
0866           if (!found) {
0867             out[ii->type_].push_back(*ii);
0868           } else {
0869             //        edm::LogWarning(mlConfigDb_)
0870             //      << "[SiStripPartition::" << __func__ << "]"
0871             //      << " Run number already found!";
0872           }
0873         } else {
0874           //        edm::LogWarning(mlConfigDb_)
0875           //          << "[SiStripPartition::" << __func__ << "]"
0876           //          << " NULL run number!";
0877         }
0878       } else {
0879         //    edm::LogWarning(mlConfigDb_)
0880         //      << "[SiStripPartition::" << __func__ << "]"
0881         //      << " Unexpected run type!";
0882       }
0883     } else {
0884       //    edm::LogWarning(mlConfigDb_)
0885       //      << "[SiStripPartition::" << __func__ << "]"
0886       //      << " Partition name does not match!";
0887     }
0888   }
0889 }
0890 
0891 // -----------------------------------------------------------------------------
0892 //
0893 void SiStripConfigDb::runs(const SiStripConfigDb::Runs& in,
0894                            SiStripConfigDb::RunsByPartition& out,
0895                            sistrip::RunType optional_type) const {
0896   out.clear();
0897 
0898   // Iterate through runs
0899   Runs::const_iterator ii = in.begin();
0900   Runs::const_iterator jj = in.end();
0901   for (; ii != jj; ++ii) {
0902     // Check partition name
0903     if (!ii->partition_.empty()) {
0904       // Check run type
0905       if (ii->type_ == optional_type || optional_type == sistrip::UNDEFINED_RUN_TYPE) {
0906         // Check run number
0907         if (ii->number_) {
0908           bool found = false;
0909           if (out.find(ii->partition_) != out.end()) {
0910             Runs::const_iterator irun = out[ii->partition_].begin();
0911             Runs::const_iterator jrun = out[ii->partition_].end();
0912             while (!found && irun != jrun) {
0913               if (irun->number_ == ii->number_) {
0914                 found = true;
0915               }
0916               ++irun;
0917             }
0918           }
0919           // Check if run number already found
0920           if (!found) {
0921             out[ii->partition_].push_back(*ii);
0922           } else {
0923             //        edm::LogWarning(mlConfigDb_)
0924             //      << "[SiStripPartition::" << __func__ << "]"
0925             //      << " Run number already found!";
0926           }
0927         } else {
0928           //        edm::LogWarning(mlConfigDb_)
0929           //          << "[SiStripPartition::" << __func__ << "]"
0930           //          << " NULL run number!";
0931         }
0932       } else {
0933         //    edm::LogWarning(mlConfigDb_)
0934         //      << "[SiStripPartition::" << __func__ << "]"
0935         //      << " Run type does not match!";
0936       }
0937     } else {
0938       //    edm::LogWarning(mlConfigDb_)
0939       //      << "[SiStripPartition::" << __func__ << "]"
0940       //      << " NULL value for partition!";
0941     }
0942   }
0943 }
0944 
0945 // -----------------------------------------------------------------------------
0946 //
0947 void SiStripConfigDb::partitions(std::list<std::string>& partitions) const {
0948   partitions.clear();
0949 
0950   // Check DF pointer
0951   DeviceFactory* const df = deviceFactory(__func__);
0952   if (!df) {
0953     edm::LogError(mlConfigDb_) << "[SiStripPartition::" << __func__ << "]"
0954                                << " NULL pointer to DeviceFactory object!";
0955     return;
0956   }
0957 
0958   partitions = df->getAllPartitionNames();
0959 }