CMS 3D CMS Logo

SiStripDetVOffHandler.cc
Go to the documentation of this file.
6 
7 #include <iostream>
8 #include <sstream>
9 
11 
13 
14 
16 public:
17  explicit SiStripDetVOffHandler(const edm::ParameterSet& iConfig );
18  ~SiStripDetVOffHandler() override;
19  void analyze( const edm::Event& evt, const edm::EventSetup& evtSetup) override;
20  void endJob() override;
21 
22 private:
27 
29 
30  std::vector< std::pair<SiStripDetVOff*,cond::Time_t> > newPayloads;
32 };
33 
36  m_condDb( iConfig.getParameter< std::string >("conditionDatabase") ),
37  m_localCondDbFile( iConfig.getParameter< std::string >("condDbFile") ),
38  m_targetTag( iConfig.getParameter< std::string >("targetTag") ),
39  maxTimeBeforeNewIOV_( iConfig.getUntrackedParameter< int >("maxTimeBeforeNewIOV", 24) ){
42  // get last IOV from local sqlite file if "conditionDatabase" is empty
43  if (m_condDb.empty()) m_condDb = m_localCondDbFile;
44 }
45 
47 }
48 
49 void SiStripDetVOffHandler::analyze(const edm::Event& evt, const edm::EventSetup& evtSetup) {
50  // get last payload from condDb
51  cond::Time_t lastIov = 0;
52  std::shared_ptr<SiStripDetVOff> lastPayload;
53 
54  edm::LogInfo("SiStripDetVOffHandler") << "[SiStripDetVOffHandler::" << __func__ << "] "
55  << "Retrieve last IOV from " << m_condDb;
56  cond::persistency::Session condDbSession = m_connectionPool.createSession( m_condDb );
57  condDbSession.transaction().start( true );
58  if ( m_condDb.find("sqlite")==0 && (!condDbSession.existsDatabase()) ){
59  // Source of last IOV is empty
60  edm::LogInfo("SiStripDetVOffHandler") << "[SiStripDetVOffHandler::" << __func__ << "] "
61  << "No information can be retrieved from " << m_condDb << " because the file is empty.\n"
62  << "Will assume all HV/LV's are off.";
63  }else{
64  cond::persistency::IOVProxy iovProxy = condDbSession.readIov( m_targetTag );
65  cond::Hash lastPayloadHash = iovProxy.getLast().payloadId;
66  if ( !lastPayloadHash.empty() ) {
67  lastPayload = condDbSession.fetchPayload<SiStripDetVOff>( lastPayloadHash );
68  lastIov = iovProxy.getLast().since; // move to LastValidatedTime?
69  }
70  edm::LogInfo("SiStripDetVOffHandler") << "[SiStripDetVOffHandler::" << __func__ << "] "
71  << " ... last IOV: " << lastIov << " , " << "last Payload: " << lastPayloadHash;
72  }
73  condDbSession.transaction().commit();
74 
76  evtSetup.get<TrackerTopologyRcd>().get(tTopo);
77 
78  // build the object!
79  newPayloads.clear();
80  modHVBuilder->setLastSiStripDetVOff( lastPayload.get(), lastIov );
83  edm::LogInfo("SiStripDetVOffHandler") << "[SiStripDetVOffHandler::" << __func__ << "] "
84  << "Finished building " << newPayloads.size() << " new payloads.";
85 
86  // write the payloads to sqlite file
87  edm::LogInfo("SiStripDetVOffHandler") << "[SiStripDetVOffHandler::" << __func__ << "] "
88  << "Write new payloads to sqlite file " << m_localCondDbFile;
89  cond::persistency::Session localFileSession = m_connectionPool.createSession( m_localCondDbFile, true );
90  localFileSession.transaction().start( false );
91  if ( lastPayload && newPayloads.size() == 1 && *newPayloads[0].first == *lastPayload ){
92  // if no HV/LV transition was found in this period
93  edm::LogInfo("SiStripDetVOffHandler") << "[SiStripDetVOffHandler::" << __func__ << "] "
94  << "No HV/LV transition was found from PVSS query.";
95  bool forceNewIOV = true;
96  if (maxTimeBeforeNewIOV_ < 0)
97  forceNewIOV = false;
98  else {
99  auto deltaT = cond::time::to_boost(newPayloads[0].second) - cond::time::to_boost(lastIov);
100  forceNewIOV = deltaT > boost::posix_time::hours(maxTimeBeforeNewIOV_);
101  }
102  if ( !forceNewIOV ){
103  newPayloads.erase(newPayloads.begin());
104  edm::LogInfo("SiStripDetVOffHandler") << "[SiStripDetVOffHandler::" << __func__ << "] "
105  << " ... No payload transfered.";
106  }else {
107  edm::LogInfo("SiStripDetVOffHandler") << "[SiStripDetVOffHandler::" << __func__ << "] "
108  << " ... The last IOV is too old. Will start a new IOV from " << newPayloads[0].second
109  << "(" << boost::posix_time::to_simple_string(cond::time::to_boost(newPayloads[0].second)) << ") with the same payload.";
110  }
111  }
112 
114  if (localFileSession.existsDatabase() && localFileSession.existsIov(m_targetTag)){
115  edm::LogWarning("SiStripDetVOffHandler") << "[SiStripDetVOffHandler::" << __func__ << "] "
116  << "IOV of tag " << m_targetTag << " already exists in sqlite file " << m_localCondDbFile;
117  iovEditor = localFileSession.editIov(m_targetTag);
118  }else {
119  iovEditor = localFileSession.createIov<SiStripDetVOff>( m_targetTag, cond::timestamp );
120  iovEditor.setDescription( "New IOV" );
121  }
122  for (const auto &payload : newPayloads){
123  cond::Hash thePayloadHash = localFileSession.storePayload<SiStripDetVOff>( *payload.first );
124  iovEditor.insert( payload.second, thePayloadHash );
125  }
126  iovEditor.flush();
127  localFileSession.transaction().commit();
128 
129  edm::LogInfo("SiStripDetVOffHandler") << "[SiStripDetVOffHandler::" << __func__ << "] "
130  << newPayloads.size() << " payloads written to sqlite file.";
131 
132 }
133 
135 }
136 
137 // -------------------------------------------------------
T getParameter(std::string const &) const
std::vector< std::pair< SiStripDetVOff *, cond::Time_t > > getModulesVOff()
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
void start(bool readOnly=true)
Definition: Session.cc:22
void analyze(const edm::Event &evt, const edm::EventSetup &evtSetup) override
Time_t since
Definition: Types.h:55
IOVEditor createIov(const std::string &tag, cond::TimeType timeType, cond::SynchronizationType synchronizationType=cond::SYNCH_ANY)
Definition: Session.h:185
void setDescription(const std::string &description)
Definition: IOVEditor.cc:128
Transaction & transaction()
Definition: Session.cc:66
void setParameters(const edm::ParameterSet &connectionPset)
U second(std::pair< T, U > const &p)
std::vector< std::pair< SiStripDetVOff *, cond::Time_t > > newPayloads
payload
payload postfix for testing
IOVProxy readIov(const std::string &tag, bool full=false)
Definition: Session.cc:81
unsigned long long Time_t
Definition: Time.h:16
edm::Service< SiStripDetVOffBuilder > modHVBuilder
Hash payloadId
Definition: Types.h:57
Session createSession(const std::string &connectionString, bool writeCapable=false)
std::string Hash
Definition: Types.h:45
cond::persistency::ConnectionPool m_connectionPool
IOVEditor editIov(const std::string &tag)
Definition: Session.cc:148
void insert(cond::Time_t since, const cond::Hash &payloadHash, bool checkType=false)
Definition: IOVEditor.cc:152
const T & get() const
Definition: EventSetup.h:55
bool existsIov(const std::string &tag)
Definition: Session.cc:97
void setLastSiStripDetVOff(SiStripDetVOff *lastPayload, cond::Time_t lastTimeStamp)
cond::Hash storePayload(const T &payload, const boost::posix_time::ptime &creationTime=boost::posix_time::microsec_clock::universal_time())
Definition: Session.h:189
SiStripDetVOffHandler SiStripO2ODetVOff
SiStripDetVOffHandler(const edm::ParameterSet &iConfig)
void BuildDetVOffObj(const TrackerTopology *trackerTopo)
T const * product() const
Definition: ESHandle.h:86
boost::posix_time::ptime to_boost(Time_t iValue)
std::shared_ptr< T > fetchPayload(const cond::Hash &payloadHash)
Definition: Session.h:215