CMS 3D CMS Logo

SiStripPayloadHandler.cc
Go to the documentation of this file.
5 
6 #include <iostream>
7 #include <sstream>
8 #include <typeinfo>
9 #include <openssl/sha.h>
10 
12 
13 #include "RelationalAccess/ITransaction.h"
14 #include "RelationalAccess/ISessionProxy.h"
15 #include "RelationalAccess/ISchema.h"
16 #include "RelationalAccess/ITable.h"
17 #include "RelationalAccess/IQuery.h"
18 #include "RelationalAccess/ICursor.h"
19 #include "RelationalAccess/ITableDataEditor.h"
20 #include "CoralBase/Attribute.h"
21 #include "CoralBase/AttributeList.h"
22 #include "CoralBase/TimeStamp.h"
23 
25 
26 template <typename SiStripPayload>
28 public:
29  explicit SiStripPayloadHandler(const edm::ParameterSet& iConfig );
30  virtual ~SiStripPayloadHandler();
31  virtual void analyze( const edm::Event& evt, const edm::EventSetup& evtSetup);
32  virtual void endJob();
33 
34 private:
37  void updateConfigMap(std::string configHash, std::string payloadHash);
38 
39 private:
47 
51 };
52 
53 
54 template<typename SiStripPayload>
57  m_configMapDb( iConfig.getParameter< std::string >("configMapDatabase") ),
58  m_condDb( iConfig.getParameter< std::string >("conditionDatabase") ),
59  m_cfgMapDbFile( iConfig.getParameter< std::string >("cfgMapDbFile") ),
60  m_localCondDbFile( iConfig.getParameter< std::string >("condDbFile") ),
61  m_targetTag( iConfig.getParameter< std::string >("targetTag") ),
62  m_since( iConfig.getParameter< uint32_t >("since") ),
63  p_type( cond::demangledName(typeid(SiStripPayload)) ),
64  p_cfgstr( condObjBuilder->getConfigString(typeid(SiStripPayload)) ){
67 }
68 
69 template<typename SiStripPayload>
71 }
72 
73 template<typename SiStripPayload>
75 
76  // some extra work: Getting the payload hash of the last IOV from condDb
77  // Will be compared with the new payload and reported
79  condDbSession.transaction().start( true );
80  cond::persistency::IOVProxy iovProxy = condDbSession.readIov( m_targetTag );
81  cond::Hash last_hash = iovProxy.getLast().payloadId;
82 
83  // that's the final goal: obtain the payload to store into the sqlite file for the upload into the DB
84  std::shared_ptr<SiStripPayload> payloadToUpload;
85  // first compute the hash of the configuration
86  std::string configHash = makeConfigHash();
87  // query the configMap DB to find the corresponding payload hash
88  std::string mappedPayloadHash = queryConfigMap(configHash);
89  bool mapUpToDate = false;
90 
91  if ( !mappedPayloadHash.empty() ){
92  // the payload has been found ( fast O2O case )
93  // copy the payload from condtition database
94  mapUpToDate = true;
95  edm::LogInfo("SiStripPayloadHandler") << "[SiStripPayloadHandler::" << __func__ << "] "
96  << "Try retrieving payload from " << m_condDb;
97  payloadToUpload = condDbSession.fetchPayload<SiStripPayload>( mappedPayloadHash );
98  std::cout << "@@@[FastO2O:true]@@@" << std::endl;
99  edm::LogInfo("SiStripPayloadHandler") << "[SiStripPayloadHandler::" << __func__ << "] "
100  << " ... Payload is copied from offline condition database.";
101  }else {
102  // start the long O2O...
103  edm::LogInfo("SiStripPayloadHandler") << "[SiStripPayloadHandler::" << __func__ << "] "
104  << "NO mapping payload hash found. Will run the long O2O. ";
105  SiStripPayload *obj = nullptr;
106  condObjBuilder->getValue(obj);
107  payloadToUpload = std::shared_ptr<SiStripPayload>(obj);
108  std::cout << "@@@[FastO2O:false]@@@" << std::endl;
109  edm::LogInfo("SiStripPayloadHandler") << "[SiStripPayloadHandler::" << __func__ << "] "
110  << " ... New payload has been created.";
111  }
112  condDbSession.transaction().commit();
113 
114  // write payload and iov in the local file
115  edm::LogInfo("SiStripPayloadHandler") << "[SiStripPayloadHandler::" << __func__ << "] "
116  << "Write payload to local sqlite file: " << m_localCondDbFile;
117  cond::persistency::Session localFileSession = m_connectionPool.createSession( m_localCondDbFile, true );
118  localFileSession.transaction().start( false );
119  // write the payload
120  cond::Hash thePayloadHash = localFileSession.storePayload<SiStripPayload>( *payloadToUpload );
121  cond::persistency::IOVEditor iovEditor = localFileSession.createIov<SiStripPayload>( m_targetTag, cond::runnumber );
122  iovEditor.setDescription( "New IOV" );
123  // inserting the iov
124  iovEditor.insert( m_since, thePayloadHash );
125  iovEditor.flush();
126  localFileSession.transaction().commit();
127  edm::LogInfo("SiStripPayloadHandler") << "[SiStripPayloadHandler::" << __func__ << "] "
128  << "Payload " << thePayloadHash << " inserted to sqlite with IOV " << m_since;
129 
130  // last step, update the configMap if required
131  if( !mapUpToDate ){
132  updateConfigMap(configHash, thePayloadHash);
133  }
134 
135  // finish the extra work: Compare the new payload with last IOV
136  if (last_hash == thePayloadHash){
137  std::cout << "@@@[PayloadChange:false]@@@" << last_hash << std::endl;
138  }else {
139  std::cout << "@@@[PayloadChange:true]@@@" << last_hash << " -> " << thePayloadHash << std::endl;
140  }
141 
142 }
143 
144 template<typename SiStripPayload>
146 }
147 
148 template<typename SiStripPayload>
150 
151  edm::LogInfo("SiStripPayloadHandler") << "[SiStripPayloadHandler::" << __func__ << "] "
152  << "Convert config string to SHA-1 hash for " << p_type
153  << "\n... config: " << p_cfgstr;
154 
155  // calcuate SHA-1 hash using openssl
156  // adapted from cond::persistency::makeHash() in CondCore/CondDB/src/IOVSchema.cc
157  SHA_CTX ctx;
158  if( !SHA1_Init( &ctx ) ){
159  throw cms::Exception("SHA1 initialization error.");
160  }
161  if( !SHA1_Update( &ctx, p_type.c_str(), p_type.size() ) ){
162  throw cms::Exception("SHA1 processing error (1).");
163  }
164  if( !SHA1_Update( &ctx, p_cfgstr.c_str(), p_cfgstr.size() ) ){
165  throw cms::Exception("SHA1 processing error (2).");
166  }
167  unsigned char hash[SHA_DIGEST_LENGTH];
168  if( !SHA1_Final(hash, &ctx) ){
169  throw cms::Exception("SHA1 finalization error.");
170  }
171 
172  char tmp[SHA_DIGEST_LENGTH*2+1];
173  // re-write bytes in hex
174  for (unsigned int i = 0; i < SHA_DIGEST_LENGTH; i++) {
175  ::sprintf(&tmp[i * 2], "%02x", hash[i]);
176  }
177  tmp[SHA_DIGEST_LENGTH*2] = 0;
178 
179  edm::LogInfo("SiStripPayloadHandler") << "[SiStripPayloadHandler::" << __func__ << "] "
180  "... hash: " << tmp;
181  return tmp;
182 
183 }
184 
185 template<typename SiStripPayload>
187  edm::LogInfo("SiStripPayloadHandler") << "[SiStripPayloadHandler::" << __func__ << "] "
188  << "Query " << m_configMapDb << " to see if the payload is already in DB.";
189 
190  auto cmDbSession = m_connectionPool.createCoralSession( m_configMapDb );
191  // query the STRIP_CONFIG_TO_PAYLOAD_MAP table
192  cmDbSession->transaction().start( true );
193  coral::ITable& cmTable = cmDbSession->nominalSchema().tableHandle( "STRIP_CONFIG_TO_PAYLOAD_MAP" );
194  std::unique_ptr<coral::IQuery> query( cmTable.newQuery() );
195  query->addToOutputList( "PAYLOAD_HASH" );
196  query->defineOutputType( "PAYLOAD_HASH", coral::AttributeSpecification::typeNameForType<std::string>() );
197  // also print these for debugging
198  query->addToOutputList( "PAYLOAD_TYPE" );
199  query->addToOutputList( "CONFIG_STRING" );
200  query->addToOutputList( "INSERTION_TIME" );
201  std::string whereClause( "CONFIG_HASH = :CONFIG_HASH" );
202  coral::AttributeList whereData;
203  whereData.extend<std::string>( "CONFIG_HASH" );
204  whereData.begin()->data< std::string >() = configHash;
205  query->setCondition( whereClause, whereData );
206  coral::ICursor& cursor = query->execute();
207  std::string p_hash;
208  if( cursor.next() ){
209  // the payload has been found ( fast O2O case )
210  p_hash = cursor.currentRow()["PAYLOAD_HASH"].data<std::string>();
211  edm::LogInfo("SiStripPayloadHandler") << "[SiStripPayloadHandler::" << __func__ << "] "
212  << "Found associated payload hash " << p_hash
213  << "\n... type=" << cursor.currentRow()["PAYLOAD_TYPE"].data<std::string>()
214  << "\n... config=" << cursor.currentRow()["CONFIG_STRING"].data<std::string>()
215  << "\n... insertion_time=" << cursor.currentRow()["INSERTION_TIME"].data<coral::TimeStamp>().toString();
216  }
217  cmDbSession->transaction().commit();
218 
219  return p_hash;
220 }
221 
222 template<typename SiStripPayload>
224  edm::LogInfo("SiStripPayloadHandler") << "[SiStripPayloadHandler::" << __func__ << "] "
225  << "Updating the config to payload hash map to " << m_cfgMapDbFile;
226 
227  // create a writable transaction
228  auto cmSQLiteSession = m_connectionPool.createCoralSession( m_cfgMapDbFile, true );
229  cmSQLiteSession->transaction().start( false );
230  coral::ITable& cmTable = cmSQLiteSession->nominalSchema().tableHandle( "STRIP_CONFIG_TO_PAYLOAD_MAP" );
231  coral::AttributeList insertData;
232  insertData.extend<std::string>( "CONFIG_HASH" );
233  insertData.extend<std::string>( "PAYLOAD_HASH" );
234  // also insert these for bookkeeping
235  insertData.extend<std::string>( "PAYLOAD_TYPE" );
236  insertData.extend<std::string>( "CONFIG_STRING" );
237  insertData.extend<coral::TimeStamp>( "INSERTION_TIME" );
238  insertData["CONFIG_HASH"].data<std::string>() = configHash;
239  insertData["PAYLOAD_HASH"].data<std::string>() = payloadHash;
240  insertData["PAYLOAD_TYPE"].data<std::string>() = p_type;
241  insertData["CONFIG_STRING"].data<std::string>() = p_cfgstr;
242  insertData["INSERTION_TIME"].data<coral::TimeStamp>() = coral::TimeStamp::now(); // UTC time
243  cmTable.dataEditor().insertRow( insertData );
244  cmSQLiteSession->transaction().commit();
245  edm::LogInfo("SiStripPayloadHandler") << "[SiStripPayloadHandler::" << __func__ << "] "
246  << "Updated with mapping (configHash : payloadHash)" << configHash << " : " << payloadHash;
247 }
248 
249 // -------------------------------------------------------
250 
253 
256 
259 
262 
265 
268 
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
edm::Service< SiStripCondObjBuilderFromDb > condObjBuilder
SiStripPayloadHandler< SiStripNoises > SiStripO2ONoises
SiStripPayloadHandler< SiStripFedCabling > SiStripO2OFedCabling
std::string queryConfigMap(std::string configHash)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
void start(bool readOnly=true)
Definition: Session.cc:22
void updateConfigMap(std::string configHash, std::string payloadHash)
IOVEditor createIov(const std::string &tag, cond::TimeType timeType, cond::SynchronizationType synchronizationType=cond::SYNCH_ANY)
Definition: Session.h:180
void setDescription(const std::string &description)
Definition: IOVEditor.cc:128
Transaction & transaction()
Definition: Session.cc:66
def query(query_str, verbose=False)
Definition: das.py:5
SiStripPayloadHandler< SiStripPedestals > SiStripO2OPedestals
void setParameters(const edm::ParameterSet &connectionPset)
Definition: query.py:1
IOVProxy readIov(const std::string &tag, bool full=false)
Definition: Session.cc:81
SiStripPayloadHandler< SiStripBadStrip > SiStripO2OBadStrip
unsigned long long Time_t
Definition: Time.h:16
SiStripPayloadHandler< SiStripApvGain > SiStripO2OApvGain
std::string toString(const char *format,...)
Definition: xdaq_compat.cc:4
void getValue(SiStripFedCabling *&val)
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
void insert(cond::Time_t since, const cond::Hash &payloadHash, bool checkType=false)
Definition: IOVEditor.cc:152
SiStripPayloadHandler(const edm::ParameterSet &iConfig)
cond::Hash storePayload(const T &payload, const boost::posix_time::ptime &creationTime=boost::posix_time::microsec_clock::universal_time())
Definition: Session.h:184
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
Definition: plugin.cc:24
virtual void analyze(const edm::Event &evt, const edm::EventSetup &evtSetup)
SiStripPayloadHandler< SiStripThreshold > SiStripO2OThreshold
SiStripPayloadHandler< SiStripLatency > SiStripO2OLatency
std::shared_ptr< coral::ISessionProxy > createCoralSession(const std::string &connectionString, bool writeCapable=false)
std::shared_ptr< T > fetchPayload(const cond::Hash &payloadHash)
Definition: Session.h:197