test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SiStripDetVOffHandler.cc
Go to the documentation of this file.
5 
6 #include <iostream>
7 #include <sstream>
8 
10 
12 
13 
15 public:
16  explicit SiStripDetVOffHandler(const edm::ParameterSet& iConfig );
17  virtual ~SiStripDetVOffHandler();
18  virtual void analyze( const edm::Event& evt, const edm::EventSetup& evtSetup);
19  virtual void endJob();
20 
21 private:
26 
28 
29  std::vector< std::pair<SiStripDetVOff*,cond::Time_t> > newPayloads;
31 };
32 
34  m_connectionPool(),
35  m_condDb( iConfig.getParameter< std::string >("conditionDatabase") ),
36  m_localCondDbFile( iConfig.getParameter< std::string >("condDbFile") ),
37  m_targetTag( iConfig.getParameter< std::string >("targetTag") ),
38  maxTimeBeforeNewIOV_( iConfig.getUntrackedParameter< int >("maxTimeBeforeNewIOV", 24) ){
41  // get last IOV from local sqlite file if "conditionDatabase" is empty
42  if (m_condDb.empty()) m_condDb = m_localCondDbFile;
43 }
44 
46 }
47 
48 void SiStripDetVOffHandler::analyze(const edm::Event& evt, const edm::EventSetup& evtSetup) {
49  // get last payload from condDb
50  cond::Time_t lastIov = 0;
51  boost::shared_ptr<SiStripDetVOff> lastPayload;
52 
53  edm::LogInfo("SiStripDetVOffHandler") << "[SiStripDetVOffHandler::" << __func__ << "] "
54  << "Retrieve last IOV from " << m_condDb;
55  cond::persistency::Session condDbSession = m_connectionPool.createSession( m_condDb );
56  condDbSession.transaction().start( true );
57  if ( m_condDb.find("sqlite")==0 && (!condDbSession.existsDatabase()) ){
58  // Source of last IOV is empty
59  edm::LogInfo("SiStripDetVOffHandler") << "[SiStripDetVOffHandler::" << __func__ << "] "
60  << "No information can be retrieved from " << m_condDb << " because the file is empty.\n"
61  << "Will assume all HV/LV's are off.";
62  }else{
63  cond::persistency::IOVProxy iovProxy = condDbSession.readIov( m_targetTag );
64  cond::Hash lastPayloadHash = iovProxy.getLast().payloadId;
65  if ( !lastPayloadHash.empty() ) {
66  lastPayload = condDbSession.fetchPayload<SiStripDetVOff>( lastPayloadHash );
67  lastIov = iovProxy.getLast().since; // move to LastValidatedTime?
68  }
69  edm::LogInfo("SiStripDetVOffHandler") << "[SiStripDetVOffHandler::" << __func__ << "] "
70  << " ... last IOV: " << lastIov << " , " << "last Payload: " << lastPayloadHash;
71  }
72  condDbSession.transaction().commit();
73 
74  // build the object!
75  newPayloads.clear();
76  modHVBuilder->setLastSiStripDetVOff( lastPayload.get(), lastIov );
79  edm::LogInfo("SiStripDetVOffHandler") << "[SiStripDetVOffHandler::" << __func__ << "] "
80  << "Finished building " << newPayloads.size() << " new payloads.";
81 
82  // write the payloads to sqlite file
83  edm::LogInfo("SiStripDetVOffHandler") << "[SiStripDetVOffHandler::" << __func__ << "] "
84  << "Write new payloads to sqlite file " << m_localCondDbFile;
85  cond::persistency::Session localFileSession = m_connectionPool.createSession( m_localCondDbFile, true );
86  localFileSession.transaction().start( false );
87  if ( lastPayload && newPayloads.size() == 1 && *newPayloads[0].first == *lastPayload ){
88  // if no HV/LV transition was found in this period
89  edm::LogInfo("SiStripDetVOffHandler") << "[SiStripDetVOffHandler::" << __func__ << "] "
90  << "No HV/LV transition was found from PVSS query.";
91  bool forceNewIOV = true;
92  if (maxTimeBeforeNewIOV_ < 0)
93  forceNewIOV = false;
94  else {
95  auto deltaT = cond::time::to_boost(newPayloads[0].second) - cond::time::to_boost(lastIov);
96  forceNewIOV = deltaT > boost::posix_time::hours(maxTimeBeforeNewIOV_);
97  }
98  if ( !forceNewIOV ){
99  newPayloads.erase(newPayloads.begin());
100  edm::LogInfo("SiStripDetVOffHandler") << "[SiStripDetVOffHandler::" << __func__ << "] "
101  << " ... No payload transfered.";
102  }else {
103  edm::LogInfo("SiStripDetVOffHandler") << "[SiStripDetVOffHandler::" << __func__ << "] "
104  << " ... The last IOV is too old. Will start a new IOV from " << newPayloads[0].second
105  << "(" << boost::posix_time::to_simple_string(cond::time::to_boost(newPayloads[0].second)) << ") with the same payload.";
106  }
107  }
108 
110  if (localFileSession.existsDatabase() && localFileSession.existsIov(m_targetTag)){
111  edm::LogWarning("SiStripDetVOffHandler") << "[SiStripDetVOffHandler::" << __func__ << "] "
112  << "IOV of tag " << m_targetTag << " already exists in sqlite file " << m_localCondDbFile;
113  iovEditor = localFileSession.editIov(m_targetTag);
114  }else {
115  iovEditor = localFileSession.createIov<SiStripDetVOff>( m_targetTag, cond::timestamp );
116  iovEditor.setDescription( "New IOV" );
117  }
118  for (const auto &payload : newPayloads){
119  cond::Hash thePayloadHash = localFileSession.storePayload<SiStripDetVOff>( *payload.first );
120  iovEditor.insert( payload.second, thePayloadHash );
121  }
122  iovEditor.flush();
123  localFileSession.transaction().commit();
124 
125  edm::LogInfo("SiStripDetVOffHandler") << "[SiStripDetVOffHandler::" << __func__ << "] "
126  << newPayloads.size() << " payloads written to sqlite file.";
127 
128 }
129 
131 }
132 
133 // -------------------------------------------------------
T getParameter(std::string const &) const
std::vector< std::pair< SiStripDetVOff *, cond::Time_t > > getModulesVOff()
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
boost::shared_ptr< T > fetchPayload(const cond::Hash &payloadHash)
Definition: Session.h:190
void start(bool readOnly=true)
Definition: Session.cc:22
Time_t since
Definition: Types.h:54
IOVEditor createIov(const std::string &tag, cond::TimeType timeType, cond::SynchronizationType synchronizationType=cond::SYNCH_ANY)
Definition: Session.h:173
void setDescription(const std::string &description)
Definition: IOVEditor.cc:117
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
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:56
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:141
void insert(cond::Time_t since, const cond::Hash &payloadHash, bool checkType=false)
Definition: IOVEditor.cc:139
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:177
SiStripDetVOffHandler SiStripO2ODetVOff
SiStripDetVOffHandler(const edm::ParameterSet &iConfig)
boost::posix_time::ptime to_boost(Time_t iValue)
virtual void analyze(const edm::Event &evt, const edm::EventSetup &evtSetup)