CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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:
46 
49  boost::shared_ptr<coral::ISessionProxy> cmDbSession;
51 };
52 
53 
54 template<typename SiStripPayload>
56  m_connectionPool(),
57  m_configMapDb( iConfig.getParameter< std::string >("configMapDatabase") ),
58  m_condDb( iConfig.getParameter< std::string >("conditionDatabase") ),
59  m_localCondDbFile( iConfig.getParameter< std::string >("condDbFile") ),
60  m_targetTag( iConfig.getParameter< std::string >("targetTag") ),
61  m_since( iConfig.getParameter< uint32_t >("since") ),
62  p_type( cond::demangledName(typeid(SiStripPayload)) ),
63  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
78  cond::persistency::Session condDbSession = m_connectionPool.createSession( m_condDb );
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  boost::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 = boost::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  // query the STRIP_CONFIG_TO_PAYLOAD_MAP table
191  cmDbSession->transaction().start( true );
192  coral::ITable& cmTable = cmDbSession->nominalSchema().tableHandle( "STRIP_CONFIG_TO_PAYLOAD_MAP" );
193  std::unique_ptr<coral::IQuery> query( cmTable.newQuery() );
194  query->addToOutputList( "PAYLOAD_HASH" );
195  query->defineOutputType( "PAYLOAD_HASH", coral::AttributeSpecification::typeNameForType<std::string>() );
196  // also print these for debugging
197  query->addToOutputList( "PAYLOAD_TYPE" );
198  query->addToOutputList( "CONFIG_STRING" );
199  query->addToOutputList( "INSERTION_TIME" );
200  std::string whereClause( "CONFIG_HASH = :CONFIG_HASH" );
201  coral::AttributeList whereData;
202  whereData.extend<std::string>( "CONFIG_HASH" );
203  whereData.begin()->data< std::string >() = configHash;
204  query->setCondition( whereClause, whereData );
205  coral::ICursor& cursor = query->execute();
206  std::string p_hash;
207  if( cursor.next() ){
208  // the payload has been found ( fast O2O case )
209  p_hash = cursor.currentRow()["PAYLOAD_HASH"].data<std::string>();
210  edm::LogInfo("SiStripPayloadHandler") << "[SiStripPayloadHandler::" << __func__ << "] "
211  << "Found associated payload hash " << p_hash
212  << "\n... type=" << cursor.currentRow()["PAYLOAD_TYPE"].data<std::string>()
213  << "\n... config=" << cursor.currentRow()["CONFIG_STRING"].data<std::string>()
214  << "\n... insertion_time=" << cursor.currentRow()["INSERTION_TIME"].data<coral::TimeStamp>().toString();
215  }
216  cmDbSession->transaction().commit();
217 
218  return p_hash;
219 }
220 
221 template<typename SiStripPayload>
223  edm::LogInfo("SiStripPayloadHandler") << "[SiStripPayloadHandler::" << __func__ << "] "
224  << "Updating the config to payload hash map...";
225  // create a writable transaction
226  cmDbSession->transaction().start( false );
227  coral::ITable& cmTable = cmDbSession->nominalSchema().tableHandle( "STRIP_CONFIG_TO_PAYLOAD_MAP" );
228  coral::AttributeList insertData;
229  insertData.extend<std::string>( "CONFIG_HASH" );
230  insertData.extend<std::string>( "PAYLOAD_HASH" );
231  // also insert these for bookkeeping
232  insertData.extend<std::string>( "PAYLOAD_TYPE" );
233  insertData.extend<std::string>( "CONFIG_STRING" );
234  insertData.extend<coral::TimeStamp>( "INSERTION_TIME" );
235  insertData["CONFIG_HASH"].data<std::string>() = configHash;
236  insertData["PAYLOAD_HASH"].data<std::string>() = payloadHash;
237  insertData["PAYLOAD_TYPE"].data<std::string>() = p_type;
238  insertData["CONFIG_STRING"].data<std::string>() = p_cfgstr;
239  insertData["INSERTION_TIME"].data<coral::TimeStamp>() = coral::TimeStamp::now(); // UTC time
240  cmTable.dataEditor().insertRow( insertData );
241  cmDbSession->transaction().commit();
242  edm::LogInfo("SiStripPayloadHandler") << "[SiStripPayloadHandler::" << __func__ << "] "
243  << "Updated with mapping (configHash : payloadHash)" << configHash << " : " << payloadHash;
244 }
245 
246 // -------------------------------------------------------
247 
250 
253 
256 
259 
262 
265 
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
boost::shared_ptr< coral::ISessionProxy > cmDbSession
edm::Service< SiStripCondObjBuilderFromDb > condObjBuilder
SiStripPayloadHandler< SiStripNoises > SiStripO2ONoises
SiStripPayloadHandler< SiStripFedCabling > SiStripO2OFedCabling
std::string queryConfigMap(std::string configHash)
boost::shared_ptr< coral::ISessionProxy > createCoralSession(const std::string &connectionString, bool writeCapable=false)
#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
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:173
void setDescription(const std::string &description)
Definition: IOVEditor.cc:117
Transaction & transaction()
Definition: Session.cc:66
SiStripPayloadHandler< SiStripPedestals > SiStripO2OPedestals
void setParameters(const edm::ParameterSet &connectionPset)
std::string demangledName(const std::type_info &typeInfo)
Definition: ClassUtils.cc:159
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
Hash payloadId
Definition: Types.h:56
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:139
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:177
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
tuple query
Definition: o2o.py:269
tuple cout
Definition: gather_cfg.py:145
virtual void analyze(const edm::Event &evt, const edm::EventSetup &evtSetup)
SiStripPayloadHandler< SiStripThreshold > SiStripO2OThreshold
SiStripPayloadHandler< SiStripLatency > SiStripO2OLatency