Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:16

0001 
0002 #ifndef OnlineDB_SiStripConfigDb_SiStripConfigDb_h
0003 #define OnlineDB_SiStripConfigDb_SiStripConfigDb_h
0004 
0005 #define DATABASE  // Needed by DeviceFactory API! Do not comment!
0006 //#define USING_DATABASE_MASKING
0007 
0008 #include "FWCore/Utilities/interface/Exception.h"
0009 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0010 #include "FWCore/ServiceRegistry/interface/ActivityRegistry.h"
0011 #include "DataFormats/Common/interface/MapOfVectors.h"
0012 #include "DataFormats/SiStripCommon/interface/ConstantsForRunType.h"
0013 #include "DataFormats/SiStripCommon/interface/SiStripConstants.h"
0014 #include "DataFormats/SiStripCommon/interface/SiStripFecKey.h"
0015 #include "CalibFormats/SiStripObjects/interface/SiStripFecCabling.h"
0016 #include "OnlineDB/SiStripConfigDb/interface/SiStripDbParams.h"
0017 #include "DeviceFactory.h"
0018 #include "boost/range/iterator_range.hpp"
0019 #include <iostream>
0020 #include <fstream>
0021 #include <sstream>
0022 #include <ostream>
0023 #include <vector>
0024 #include <string>
0025 #include <list>
0026 #include <map>
0027 #include <atomic>
0028 
0029 #include "DbClient.h"
0030 #include <cstdint>
0031 
0032 namespace sistrip {
0033   static const uint16_t FEC_CRATE_OFFSET = 0;  //@@ temporary
0034   static const uint16_t FEC_RING_OFFSET = 0;   //@@ temporary
0035 }  // namespace sistrip
0036 
0037 // Friend class
0038 namespace cms {
0039   class SiStripO2O;
0040 }
0041 
0042 /** 
0043    \class SiStripConfigDb
0044    \brief An interface class to the DeviceFactory
0045    \author R.Bainbridge
0046 */
0047 class SiStripConfigDb {
0048 public:
0049   // ---------- Constructors, destructors ----------
0050 
0051   /** Constructor when using the "service" mode, which takes as an
0052       argument a ParameterSet (containing the database connection
0053       parameters). */
0054   SiStripConfigDb(const edm::ParameterSet&, const edm::ActivityRegistry&);
0055 
0056   /** Default destructor. */
0057   ~SiStripConfigDb();
0058 
0059   // ---------- PROTECTED INTERFACE ----------
0060 
0061 protected:
0062   /*
0063     Access to the configuration database is reserved solely for the
0064     commissioning (database) client and the online-to-offline transfer
0065     tool. If you wish to use this interface to the configuration
0066     database, then please contact one of the package administrators.
0067   */
0068 
0069   // ESSources and O2O
0070   friend class SiStripFedCablingBuilderFromDb;
0071   friend class SiStripPedestalsBuilderFromDb;
0072   friend class SiStripNoiseBuilderFromDb;
0073   friend class SiStripCondObjBuilderFromDb;
0074   friend class SiStripPsuDetIdMap;
0075 
0076   // Commissioning clients
0077   friend class SiStripCommissioningDbClient;
0078   friend class SiStripCommissioningOfflineDbClient;
0079   friend class CommissioningHistosUsingDb;
0080   friend class FastFedCablingHistosUsingDb;
0081   friend class FedCablingHistosUsingDb;
0082   friend class ApvTimingHistosUsingDb;
0083   friend class OptoScanHistosUsingDb;
0084   friend class PedestalsHistosUsingDb;
0085   friend class PedsFullNoiseHistosUsingDb;
0086   friend class PedsOnlyHistosUsingDb;
0087   friend class NoiseHistosUsingDb;
0088   friend class VpspScanHistosUsingDb;
0089   friend class LatencyHistosUsingDb;
0090   friend class FineDelayHistosUsingDb;
0091   friend class CalibrationHistosUsingDb;
0092   friend class DaqScopeModeHistosUsingDb;
0093 
0094   // Utility and tests
0095   friend class SiStripPartition;
0096   friend class testSiStripConfigDb;
0097 
0098   // ---------- Typedefs ----------
0099 
0100   // FED connections
0101   typedef ConnectionDescription FedConnection;
0102   typedef edm::MapOfVectors<std::string, FedConnection*> FedConnections;
0103   typedef FedConnections::range FedConnectionsRange;
0104   typedef std::vector<FedConnection*> FedConnectionsV;
0105 
0106   // Device descriptions
0107   typedef enumDeviceType DeviceType;
0108   typedef deviceDescription DeviceDescription;
0109   typedef edm::MapOfVectors<std::string, DeviceDescription*> DeviceDescriptions;
0110   typedef DeviceDescriptions::range DeviceDescriptionsRange;
0111   typedef std::vector<DeviceDescription*> DeviceDescriptionsV;
0112 
0113   // FED descriptions
0114   typedef Fed9U::Fed9UDescription FedDescription;
0115   typedef edm::MapOfVectors<std::string, FedDescription*> FedDescriptions;
0116   typedef FedDescriptions::range FedDescriptionsRange;
0117   typedef std::vector<FedDescription*> FedDescriptionsV;
0118 
0119   // FED ids
0120   typedef std::vector<uint16_t> FedIds;
0121   typedef boost::iterator_range<FedIds::const_iterator> FedIdsRange;
0122 
0123   // DCU-DetId map
0124   typedef Sgi::hash_map<unsigned long, TkDcuInfo*> DcuDetIdMap;
0125   typedef std::pair<uint32_t, TkDcuInfo*> DcuDetId;
0126   typedef edm::MapOfVectors<std::string, DcuDetId> DcuDetIds;
0127   typedef DcuDetIds::range DcuDetIdsRange;
0128   typedef std::vector<DcuDetId> DcuDetIdsV;
0129 
0130   // Analysis descriptions
0131   typedef CommissioningAnalysisDescription::commissioningType AnalysisType;
0132   typedef CommissioningAnalysisDescription AnalysisDescription;
0133   typedef edm::MapOfVectors<std::string, AnalysisDescription*> AnalysisDescriptions;
0134   typedef AnalysisDescriptions::range AnalysisDescriptionsRange;
0135   typedef std::vector<AnalysisDescription*> AnalysisDescriptionsV;
0136 
0137   // ---------- Useful structs ----------
0138 
0139   /** Class that holds addresses that uniquely identify a hardware
0140       component within the control system. */
0141   class DeviceAddress {
0142   public:
0143     DeviceAddress();
0144     void reset();
0145     uint16_t fecCrate_;
0146     uint16_t fecSlot_;
0147     uint16_t fecRing_;
0148     uint16_t ccuAddr_;
0149     uint16_t ccuChan_;
0150     uint16_t lldChan_;
0151     uint16_t i2cAddr_;
0152     uint16_t fedId_;
0153     uint16_t feUnit_;
0154     uint16_t feChan_;
0155   };
0156 
0157   // ---------- Connection and useful methods ----------
0158 
0159   /** Establishes connection to DeviceFactory API. */
0160   void openDbConnection();
0161 
0162   /** Closes connection to DeviceFactory API. */
0163   void closeDbConnection();
0164 
0165   /** Returns database connection parameters. */
0166   inline const SiStripDbParams& dbParams() const;
0167 
0168   /** Returns whether using database or xml files. */
0169   inline bool usingDb() const;
0170 
0171   /** Returns pointer to DeviceFactory API, with check if NULL. */
0172   DeviceFactory* const deviceFactory(std::string method_name = "") const;
0173 
0174   /** Returns pointer to DeviceFactory API, with check if NULL. */
0175   DbClient* const databaseCache(std::string method_name = "") const;
0176 
0177   // ---------- Run numbers for partitions and run types ----------
0178 
0179   class Run {
0180   public:
0181     sistrip::RunType type_;
0182     std::string partition_;
0183     uint16_t number_;
0184     Run() : type_(sistrip::UNDEFINED_RUN_TYPE), partition_(""), number_(0) { ; }
0185   };
0186 
0187   typedef std::vector<Run> Runs;
0188 
0189   typedef std::map<sistrip::RunType, Runs> RunsByType;
0190 
0191   typedef std::map<std::string, Runs> RunsByPartition;
0192 
0193   /** Retrieves all runs from database. */
0194   void runs(Runs&) const;
0195 
0196   /** Runs organsed by type, optionally for given partition. */
0197   void runs(const Runs& in, RunsByType& out, std::string optional_partition = "") const;
0198 
0199   /** Runs organsed by partition, optionally for given type. */
0200   void runs(const Runs& in, RunsByPartition& out, sistrip::RunType optional_type = sistrip::UNDEFINED_RUN_TYPE) const;
0201 
0202   /** Retrieves all partitions names from database. */
0203   void partitions(std::list<std::string>&) const;
0204 
0205   //@@ SiStripPartition::Versions ???
0206 
0207   // ---------- FED connections ----------
0208 
0209   /** Returns local cache (just for given partition if specified). */
0210   FedConnectionsRange getFedConnections(std::string partition = "");
0211 
0212   /** Add to local cache (just for given partition if specified). */
0213   void addFedConnections(std::string partition, FedConnectionsV&);
0214 
0215   /** Uploads to database (just for given partition if specified). */
0216   void uploadFedConnections(std::string partition = "");
0217 
0218   /** Clears local cache (just for given partition if specified). */
0219   void clearFedConnections(std::string partition = "");
0220 
0221   /** Prints local cache (just for given partition if specified). */
0222   void printFedConnections(std::string partition = "");
0223 
0224   // ---------- FEC / Front-End devices ----------
0225 
0226   /** Returns local cache (just for given partition if specified). */
0227   DeviceDescriptionsRange getDeviceDescriptions(std::string partition = "");
0228 
0229   /** Returns (pair of iterators to) descriptions of given type. */
0230   /** (APV25, APVMUX, DCU, LASERDRIVER, PLL, DOH). */
0231   DeviceDescriptionsRange getDeviceDescriptions(DeviceType, std::string partition = "");
0232 
0233   /** Adds to local cache (just for given partition if specified). */
0234   void addDeviceDescriptions(std::string partition, DeviceDescriptionsV&);
0235 
0236   /** Uploads to database (just for given partition if specified). */
0237   void uploadDeviceDescriptions(std::string partition = "");
0238 
0239   /** Clears local cache (just for given partition if specified). */
0240   void clearDeviceDescriptions(std::string partition = "");
0241 
0242   /** Prints local cache (just for given partition if specified). */
0243   void printDeviceDescriptions(std::string partition = "");
0244 
0245   /** Extracts unique hardware address of device from description. */
0246   DeviceAddress deviceAddress(const deviceDescription&);  //@@ uses temp offsets!
0247 
0248   // ---------- FED descriptions ----------
0249 
0250   /** Returns local cache (just for given partition if specified). */
0251   FedDescriptionsRange getFedDescriptions(std::string partition = "");
0252 
0253   /** Adds to local cache (just for given partition if specified). */
0254   void addFedDescriptions(std::string partition, FedDescriptionsV&);
0255 
0256   /** Uploads to database (just for given partition if specified). */
0257   void uploadFedDescriptions(std::string partition = "");
0258 
0259   /** Clears local cache (just for given partition if specified). */
0260   void clearFedDescriptions(std::string partition = "");
0261 
0262   /** Prints local cache (just for given partition if specified). */
0263   void printFedDescriptions(std::string partition = "");
0264 
0265   /** Extracts FED ids from FED descriptions. */
0266   FedIdsRange getFedIds(std::string partition = "");
0267 
0268   /** Strip-level info enabled/disabled within FED descriptions. */
0269   inline bool usingStrips() const;
0270 
0271   /** Enables/disables strip-level info within FED descriptions. */
0272   inline void usingStrips(bool);
0273 
0274   // ---------- DCU-DetId info ----------
0275 
0276   /** Returns local cache (just for given partition if specified). */
0277   DcuDetIdsRange getDcuDetIds(std::string partition = "");
0278 
0279   /** Adds to local cache (just for given partition if specified). */
0280   void addDcuDetIds(std::string partition, DcuDetIdsV&);
0281 
0282   /** Uploads to database (just for given partition if specified). */
0283   void uploadDcuDetIds(std::string partition = "");
0284 
0285   /** Clears local cache (just for given partition if specified). */
0286   void clearDcuDetIds(std::string partition = "");
0287 
0288   /** Prints local cache (just for given partition if specified). */
0289   void printDcuDetIds(std::string partition = "");
0290 
0291   /** Utility method. */
0292   static DcuDetIdsV::const_iterator findDcuDetId(DcuDetIdsV::const_iterator begin,
0293                                                  DcuDetIdsV::const_iterator end,
0294                                                  uint32_t dcu_id);
0295 
0296   /** Utility method. */
0297   static DcuDetIdsV::iterator findDcuDetId(DcuDetIdsV::iterator begin, DcuDetIdsV::iterator end, uint32_t dcu_id);
0298 
0299   // ---------- Commissioning analyses ----------
0300 
0301   /** Returns local cache (just for given partition if specified). */
0302   AnalysisDescriptionsRange getAnalysisDescriptions(AnalysisType, std::string partition = "");
0303 
0304   /** Adds to local cache (just for given partition if specified). */
0305   void addAnalysisDescriptions(std::string partition, AnalysisDescriptionsV&);
0306 
0307   /** Uploads to database (just for given partition if specified). */
0308   void uploadAnalysisDescriptions(bool calibration_for_physics = false, std::string partition = "");
0309 
0310   /** Clears local cache (just for given partition if specified). */
0311   void clearAnalysisDescriptions(std::string partition = "");
0312 
0313   /** Prints local cache (just for given partition if specified). */
0314   void printAnalysisDescriptions(std::string partition = "");
0315 
0316   /** Extracts unique hardware address of device from description. */
0317   DeviceAddress deviceAddress(const AnalysisDescription&);  //@@ uses temp offsets!
0318 
0319   /** Returns string for given analysis type. */
0320   std::string analysisType(AnalysisType) const;
0321 
0322 private:
0323   // ---------- Private methods ----------
0324 
0325   /** */
0326   void clearLocalCache();
0327 
0328   /** */
0329   void usingDatabase();
0330 
0331   /** */
0332   void usingDatabaseCache();
0333 
0334   /** */
0335   void usingXmlFiles();
0336 
0337   /** Handles exceptions thrown by software. */
0338   void handleException(const std::string& method_name, const std::string& extra_info = "") const;
0339 
0340   /** Checks whether file at "path" exists or not. */
0341   bool checkFileExists(const std::string& path);
0342 
0343   /** Returns device identifier based on device type. */
0344   std::string deviceType(const enumDeviceType& device_type) const;
0345 
0346   void clone(const DcuDetIdMap& in, DcuDetIdsV& out) const;
0347 
0348   void clone(const DcuDetIdsV& in, DcuDetIdMap& out) const;
0349 
0350   void clone(const DcuDetIdsV& in, DcuDetIdsV& out) const;
0351 
0352   // ---------- Database connection, partitions and versions ----------
0353 
0354   /** Pointer to the DeviceFactory API. */
0355   DeviceFactory* factory_;
0356 
0357   /** Pointer to the DbClient class. */
0358   DbClient* dbCache_;
0359 
0360   /** Instance of struct that holds all DB connection parameters. */
0361   SiStripDbParams dbParams_;
0362 
0363   // ---------- Local cache of vectors ----------
0364 
0365   /** FED-FEC connection descriptions. */
0366   FedConnections connections_;
0367 
0368   /** Device descriptions (including DCUs). */
0369   DeviceDescriptions devices_;
0370 
0371   /** Fed9U descriptions. */
0372   FedDescriptions feds_;
0373 
0374   /** DcuId-DetId map (map of TkDcuInfo objects). */
0375   DcuDetIds dcuDetIds_;
0376 
0377   /** Analysis descriptions for given commissioning run. */
0378   AnalysisDescriptions analyses_;
0379 
0380   /** Cache for devices of given type. */
0381   DeviceDescriptionsV apvDevices_;
0382 
0383   /** Cache for devices of given type. */
0384   DeviceDescriptionsV muxDevices_;
0385 
0386   /** Cache for devices of given type. */
0387   DeviceDescriptionsV dcuDevices_;
0388 
0389   /** Cache for devices of given type. */
0390   DeviceDescriptionsV lldDevices_;
0391 
0392   /** Cache for devices of given type. */
0393   DeviceDescriptionsV pllDevices_;
0394 
0395   /** Cache for devices of given type. */
0396   DeviceDescriptionsV dohDevices_;
0397 
0398   /** Cache for devices of given type. */
0399   DeviceDescriptionsV typedDevices_;
0400 
0401   /** FED ids. */
0402   FedIds fedIds_;
0403 
0404   // ---------- Miscellaneous ----------
0405 
0406   /** Switch to enable/disable transfer of strip information. */
0407   bool usingStrips_;
0408 
0409   /** */
0410   bool openConnection_;
0411 
0412   /** Static counter of instances of this class. */
0413   static std::atomic<uint32_t> cntr_;
0414 
0415   static std::atomic<bool> allowCalibUpload_;
0416 };
0417 
0418 // ---------- Inline methods ----------
0419 
0420 /** Returns database connection parameters. */
0421 const SiStripDbParams& SiStripConfigDb::dbParams() const { return dbParams_; }
0422 
0423 /** Indicates whether DB (true) or XML files (false) are used. */
0424 bool SiStripConfigDb::usingDb() const { return dbParams_.usingDb(); }
0425 
0426 /** Indicates whether FED strip info is uploaded/downloaded. */
0427 bool SiStripConfigDb::usingStrips() const { return usingStrips_; }
0428 
0429 /** Switches on/off of upload/download for FED strip info. */
0430 void SiStripConfigDb::usingStrips(bool using_strips) { usingStrips_ = using_strips; }
0431 
0432 #endif  // OnlineDB_SiStripConfigDb_SiStripConfigDb_h