Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-07-05 04:09:21

0001 #ifndef SISTRIPDETVOFF_SRC_BUILDER_H
0002 #define SISTRIPDETVOFF_SRC_BUILDER_H
0003 #define USING_NEW_CORAL
0004 
0005 #include "FWCore/ServiceRegistry/interface/Service.h"
0006 #include "FWCore/Framework/interface/EventSetup.h"
0007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0008 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0009 
0010 #include "CondFormats/SiStripObjects/interface/SiStripDetVOff.h"
0011 #include "CondFormats/DataRecord/interface/SiStripCondDataRecords.h"
0012 
0013 #include "CalibTracker/SiStripDCS/interface/SiStripCoralIface.h"
0014 #include "CalibTracker/SiStripDCS/interface/SiStripPsuDetIdMap.h"
0015 // #include "CalibTracker/SiStripDCS/interface/SiStripPsuDetIdMapFromFile.h"
0016 
0017 #include "CoralBase/TimeStamp.h"
0018 #include "CondFormats/Common/interface/Time.h"
0019 #include "CondFormats/Common/interface/TimeConversions.h"
0020 //#include "DataFormats/Provenance/interface/Timestamp.h"
0021 
0022 #include <fstream>
0023 #include <iostream>
0024 #include <memory>
0025 
0026 /** 
0027    \class SiStripDetVOffBuilder
0028    \brief Builds the SiStripDetVOff object for transfer by O2O
0029    \author J.Cole modified by Marco De Mattia
0030 */
0031 
0032 // Unit test class for SiStripDetVOffBuilder
0033 class TestSiStripDetVOffBuilder;
0034 
0035 class SiStripDetVOffBuilder {
0036   friend class TestSiStripDetVOffBuilder;
0037 
0038 public:
0039   /** Destructor. */
0040   ~SiStripDetVOffBuilder();
0041   /** Default constructor. */
0042   SiStripDetVOffBuilder(const edm::ParameterSet&, const edm::ActivityRegistry&);
0043   /** Build the SiStripDetVOff object for transfer. */
0044   void BuildDetVOffObj();
0045   /** Return modules Off vector of objects. */
0046   std::vector<std::pair<SiStripDetVOff*, cond::Time_t> > getModulesVOff() {
0047     reduction(deltaTmin_, maxIOVlength_);
0048     return modulesOff;
0049   }
0050   /** Return statistics about payloads transferred for storage in logDB. */
0051   std::vector<std::vector<uint32_t> > getPayloadStats() { return payloadStats; }
0052   /** Store the last payload transferred to DB as starting point for creation of new object list.
0053       ONLY WORKS FOR STATUSCHANGE OPTION. */
0054   void setLastSiStripDetVOff(SiStripDetVOff* lastPayload, cond::Time_t lastTimeStamp);
0055 
0056   /// Operates the reduction of the fast sequences of ramping up and down of the voltages
0057   void reduce(std::vector<std::pair<SiStripDetVOff*, cond::Time_t> >::iterator& it,
0058               std::vector<std::pair<SiStripDetVOff*, cond::Time_t> >::iterator& initialIt,
0059               std::vector<std::pair<SiStripDetVOff*, cond::Time_t> >& resultVec,
0060               const bool last = false);
0061 
0062   void reduction(const uint32_t deltaTmin, const uint32_t maxIOVlength);
0063 
0064   /// Removes IOVs as dictated by reduction
0065   void discardIOVs(std::vector<std::pair<SiStripDetVOff*, cond::Time_t> >::iterator& it,
0066                    std::vector<std::pair<SiStripDetVOff*, cond::Time_t> >::iterator& initialIt,
0067                    std::vector<std::pair<SiStripDetVOff*, cond::Time_t> >& resultVec,
0068                    const bool last,
0069                    const unsigned int first);
0070   bool FileExists(std::string filename);
0071 
0072 private:
0073   // typedefs
0074   typedef std::vector<std::pair<std::vector<uint32_t>, coral::TimeStamp> > DetIdTimeStampVector;
0075 
0076   bool whichQuery;
0077 
0078   void printPar(std::stringstream& ss, const std::vector<int>& par);
0079 
0080   std::string timeToStream(const coral::TimeStamp& coralTime, const string& comment = "");
0081   std::string timeToStream(const cond::Time_t& condTime, const string& comment = "");
0082 
0083   /** Returns the PSU channel setting, based on date.  Works from DP ID. */
0084   int findSetting(uint32_t id,
0085                   const coral::TimeStamp& changeDate,
0086                   const std::vector<uint32_t>& settingID,
0087                   const std::vector<coral::TimeStamp>& settingDate);
0088 
0089   /** Returns the PSU channel setting, based on date.  Works from PSU channel name. Overloaded. */
0090   int findSetting(std::string dpname,
0091                   const coral::TimeStamp& changeDate,
0092                   const std::vector<std::string>& settingDpname,
0093                   const std::vector<coral::TimeStamp>& settingDate);
0094 
0095   /** Extract the lastValue values from file rather than from the PVSS cond DB. */
0096   void readLastValueFromFile(std::vector<uint32_t>& dpIDs,
0097                              std::vector<float>& vmonValues,
0098                              std::vector<coral::TimeStamp>& dateChange);
0099 
0100   /** Utility code to convert a coral timestamp to the correct time format for O2O timestamp. */
0101   cond::Time_t getCondTime(const coral::TimeStamp& coralTime);
0102 
0103   /** Utility code to convert an O2O timestamp into a coral timestamp. */
0104   coral::TimeStamp getCoralTime(cond::Time_t iovTime);
0105 
0106   /** Utility code to remove all the duplicates from a vector of uint32_t. */
0107   void removeDuplicates(std::vector<uint32_t>& vec);
0108   /** */
0109   cond::Time_t findMostRecentTimeStamp(const std::vector<coral::TimeStamp>& coralDate);
0110 
0111   // member data
0112   std::vector<std::vector<uint32_t> > payloadStats;
0113   std::vector<std::pair<SiStripDetVOff*, cond::Time_t> > modulesOff;
0114   std::pair<SiStripDetVOff*, cond::Time_t> lastStoredCondObj;
0115 
0116   // configurable parameters
0117   std::string onlineDbConnectionString;
0118   std::string authenticationPath;
0119   std::string whichTable;
0120   std::string lastValueFileName;
0121   bool fromFile;
0122   std::string psuDetIdMapFile_;
0123   bool debug_;
0124   coral::TimeStamp tmax, tmin, tsetmin;
0125   std::vector<int> tDefault, tmax_par, tmin_par, tset_par;
0126   uint32_t deltaTmin_, maxIOVlength_;
0127 
0128   std::string detIdListFile_;
0129   std::string excludedDetIdListFile_;
0130   // Threshold to consider a high voltage channel on
0131   double highVoltageOnThreshold_;
0132 
0133   // Structure used to store variables needed when building the database objects
0134   struct TimesAndValues {
0135     TimesAndValues() : latestTime(0) {}
0136     std::vector<coral::TimeStamp> changeDate;  // used by both
0137     std::vector<std::string> dpname;           // only used by DB access, not file access
0138     std::vector<float> actualValue;            // only used by DB access, not file access
0139     std::vector<uint32_t> dpid;                // only used by file access
0140     std::vector<int> actualStatus;             // filled using actualValue info
0141     cond::Time_t latestTime;                   // used for timestamp when using lastValue from file
0142   };
0143 
0144   struct DetIdListTimeAndStatus {
0145     DetIdListTimeAndStatus() : notMatched(0) {}
0146     DetIdTimeStampVector detidV;
0147     std::vector<bool> StatusGood;
0148     unsigned int notMatched;
0149     std::vector<std::string> psuName;
0150     std::vector<unsigned int> isHV;
0151   };
0152 
0153   void statusChange(cond::Time_t& lastTime, TimesAndValues& tStruct);
0154   void lastValue(TimesAndValues& tStruct);
0155   void lastValueFromFile(TimesAndValues& tStruct);
0156 
0157   void buildPSUdetIdMap(TimesAndValues& tStruct, DetIdListTimeAndStatus& dStruct);
0158 
0159   void setPayloadStats(const uint32_t afterV, const uint32_t numAdded, const uint32_t numRemoved);
0160   std::pair<int, int> extractDetIdVector(const unsigned int i,
0161                                          SiStripDetVOff* modV,
0162                                          DetIdListTimeAndStatus& detIdStruct);
0163 
0164   std::unique_ptr<SiStripCoralIface> coralInterface;
0165 };
0166 #endif