14 #include "RelationalAccess/ITransaction.h" 15 #include "RelationalAccess/ISessionProxy.h" 16 #include "RelationalAccess/ISchema.h" 17 #include "RelationalAccess/ITable.h" 18 #include "RelationalAccess/IQuery.h" 19 #include "RelationalAccess/ICursor.h" 20 #include "RelationalAccess/ITableDataEditor.h" 21 #include "RelationalAccess/TableDescription.h" 22 #include "CoralBase/Attribute.h" 23 #include "CoralBase/AttributeList.h" 24 #include "CoralBase/TimeStamp.h" 28 template <
typename SiStripPayload>
56 template <
typename SiStripPayload>
59 m_cfgMapSchemaName(iConfig.getUntrackedParameter<
std::
string>(
"cfgMapSchemaName",
"CMS_COND_O2O")),
60 m_cfgMapTableName(iConfig.getUntrackedParameter<
std::
string>(
"cfgMapTableName",
"STRIP_CONFIG_TO_PAYLOAD_MAP")),
61 m_condDb(iConfig.getParameter<
std::
string>(
"conditionDatabase")),
62 m_localCondDbFile(iConfig.getParameter<
std::
string>(
"condDbFile")),
63 m_targetTag(iConfig.getParameter<
std::
string>(
"targetTag")),
64 m_since(iConfig.getParameter<uint32_t>(
"since")),
65 p_type(
cond::demangledName(typeid(SiStripPayload))) {
68 if (iConfig.
exists(
"configMapDatabase"))
70 if (iConfig.
exists(
"cfgMapDbFile"))
76 template <
typename SiStripPayload>
79 template <
typename SiStripPayload>
89 std::shared_ptr<SiStripPayload> payloadToUpload;
93 std::string mappedPayloadHash = queryConfigMap(configHash);
94 bool mapUpToDate =
false;
96 if (!mappedPayloadHash.empty()) {
100 edm::LogInfo(
"SiStripPayloadHandler") <<
"[SiStripPayloadHandler::" << __func__ <<
"] " 101 <<
"Try retrieving payload from " << m_condDb;
102 payloadToUpload = condDbSession.
fetchPayload<SiStripPayload>(mappedPayloadHash);
103 std::cout <<
"@@@[FastO2O:true]@@@" << std::endl;
104 edm::LogInfo(
"SiStripPayloadHandler") <<
"[SiStripPayloadHandler::" << __func__ <<
"] " 105 <<
" ... Payload is copied from offline condition database.";
108 edm::LogInfo(
"SiStripPayloadHandler") <<
"[SiStripPayloadHandler::" << __func__ <<
"] " 109 <<
"NO mapping payload hash found. Will run the long O2O. ";
110 SiStripPayload*
obj =
nullptr;
117 payloadToUpload = std::shared_ptr<SiStripPayload>(
obj);
118 std::cout <<
"@@@[FastO2O:false]@@@" << std::endl;
119 edm::LogInfo(
"SiStripPayloadHandler") <<
"[SiStripPayloadHandler::" << __func__ <<
"] " 120 <<
" ... New payload has been created.";
125 edm::LogInfo(
"SiStripPayloadHandler") <<
"[SiStripPayloadHandler::" << __func__ <<
"] " 126 <<
"Write payload to local sqlite file: " << m_localCondDbFile;
134 iovEditor.
insert(m_since, thePayloadHash);
137 edm::LogInfo(
"SiStripPayloadHandler") <<
"[SiStripPayloadHandler::" << __func__ <<
"] " 138 <<
"Payload " << thePayloadHash <<
" inserted to sqlite with IOV " << m_since;
142 updateConfigMap(configHash, thePayloadHash);
146 if (last_hash == thePayloadHash) {
147 std::cout <<
"@@@[PayloadChange:false]@@@" << last_hash << std::endl;
149 std::cout <<
"@@@[PayloadChange:true]@@@" << last_hash <<
" -> " << thePayloadHash << std::endl;
153 template <
typename SiStripPayload>
156 template <
typename SiStripPayload>
158 edm::LogInfo(
"SiStripPayloadHandler") <<
"[SiStripPayloadHandler::" << __func__ <<
"] " 159 <<
"Convert config string to SHA-1 hash for " << p_type
160 <<
"\n... config: " << p_cfgstr;
166 const EVP_MD* md = EVP_get_digestbyname(
"SHA1");
167 if (!EVP_DigestInit_ex(mdctx, md,
nullptr)) {
170 if (!EVP_DigestUpdate(mdctx, p_type.c_str(), p_type.size())) {
173 if (!EVP_DigestUpdate(mdctx, p_cfgstr.c_str(), p_cfgstr.size())) {
176 unsigned char hash[EVP_MAX_MD_SIZE];
178 if (!EVP_DigestFinal_ex(mdctx,
hash, &md_len)) {
183 char tmp[EVP_MAX_MD_SIZE * 2 + 1];
185 for (
unsigned int i = 0;
i < md_len;
i++) {
190 edm::LogInfo(
"SiStripPayloadHandler") <<
"[SiStripPayloadHandler::" << __func__
197 template <
typename SiStripPayload>
199 if (m_configMapDb.empty())
202 edm::LogInfo(
"SiStripPayloadHandler") <<
"[SiStripPayloadHandler::" << __func__ <<
"] " 203 <<
"Query " << m_configMapDb <<
" to see if the payload is already in DB.";
205 auto cmDbSession = m_connectionPool.createCoralSession(m_configMapDb);
207 cmDbSession->transaction().start(
true);
208 coral::ITable& cmTable = cmDbSession->schema(m_cfgMapSchemaName).tableHandle(m_cfgMapTableName);
209 std::unique_ptr<coral::IQuery>
query(cmTable.newQuery());
210 query->addToOutputList(
"PAYLOAD_HASH");
211 query->defineOutputType(
"PAYLOAD_HASH", coral::AttributeSpecification::typeNameForType<std::string>());
213 query->addToOutputList(
"PAYLOAD_TYPE");
214 query->addToOutputList(
"CONFIG_STRING");
215 query->addToOutputList(
"INSERTION_TIME");
216 std::string whereClause(
"CONFIG_HASH = :CONFIG_HASH");
217 coral::AttributeList whereData;
219 whereData.begin()->data<
std::string>() = configHash;
220 query->setCondition(whereClause, whereData);
221 coral::ICursor& cursor =
query->execute();
225 p_hash = cursor.currentRow()[
"PAYLOAD_HASH"].data<
std::string>();
227 <<
"[SiStripPayloadHandler::" << __func__ <<
"] " 228 <<
"Found associated payload hash " << p_hash
229 <<
"\n... type=" << cursor.currentRow()[
"PAYLOAD_TYPE"].data<
std::string>()
230 <<
"\n... config=" << cursor.currentRow()[
"CONFIG_STRING"].data<
std::string>()
231 <<
"\n... insertion_time=" << cursor.currentRow()[
"INSERTION_TIME"].data<coral::TimeStamp>().
toString();
233 cmDbSession->transaction().commit();
238 template <
typename SiStripPayload>
240 if (m_cfgMapDbFile.empty())
243 edm::LogInfo(
"SiStripPayloadHandler") <<
"[SiStripPayloadHandler::" << __func__ <<
"] " 244 <<
"Updating the config to payload hash map to " << m_cfgMapDbFile;
247 auto cmSQLiteSession = m_connectionPool.createCoralSession(m_cfgMapDbFile,
true);
248 cmSQLiteSession->transaction().start(
false);
250 if (!cmSQLiteSession->nominalSchema().existsTable(m_cfgMapTableName)) {
252 coral::TableDescription mapTable;
253 mapTable.setName(m_cfgMapTableName);
254 mapTable.insertColumn(
"CONFIG_HASH", coral::AttributeSpecification::typeNameForType<std::string>());
255 mapTable.insertColumn(
"PAYLOAD_HASH", coral::AttributeSpecification::typeNameForType<std::string>());
256 mapTable.insertColumn(
"PAYLOAD_TYPE", coral::AttributeSpecification::typeNameForType<std::string>());
257 mapTable.insertColumn(
"CONFIG_STRING", coral::AttributeSpecification::typeNameForType<std::string>());
258 mapTable.insertColumn(
"INSERTION_TIME", coral::AttributeSpecification::typeNameForType<coral::TimeStamp>());
259 mapTable.setPrimaryKey(
"CONFIG_HASH");
260 mapTable.setNotNullConstraint(
"CONFIG_HASH");
261 mapTable.setNotNullConstraint(
"PAYLOAD_HASH");
262 mapTable.setNotNullConstraint(
"PAYLOAD_TYPE");
263 mapTable.setNotNullConstraint(
"CONFIG_STRING");
264 mapTable.setNotNullConstraint(
"INSERTION_TIME");
265 cmSQLiteSession->nominalSchema().createTable(mapTable);
268 coral::ITable& cmTable = cmSQLiteSession->nominalSchema().tableHandle(m_cfgMapTableName);
269 coral::AttributeList insertData;
275 insertData.extend<coral::TimeStamp>(
"INSERTION_TIME");
276 insertData[
"CONFIG_HASH"].data<
std::string>() = configHash;
277 insertData[
"PAYLOAD_HASH"].data<
std::string>() = payloadHash;
278 insertData[
"PAYLOAD_TYPE"].data<
std::string>() = p_type;
279 insertData[
"CONFIG_STRING"].data<
std::string>() = p_cfgstr;
281 cmTable.dataEditor().insertRow(insertData);
282 cmSQLiteSession->transaction().commit();
283 edm::LogInfo(
"SiStripPayloadHandler") <<
"[SiStripPayloadHandler::" << __func__ <<
"] " 284 <<
"Updated with mapping (configHash : payloadHash)" << configHash <<
" : "
T getParameter(std::string const &) const
SiStripPayloadHandler< SiStripNoises > SiStripO2ONoises
SiStripPayloadHandler< SiStripFedCabling > SiStripO2OFedCabling
std::string queryConfigMap(std::string configHash)
#define DEFINE_FWK_MODULE(type)
std::string m_cfgMapTableName
void start(bool readOnly=true)
bool exists(std::string const ¶meterName) const
checks if a parameter exists
void updateConfigMap(std::string configHash, std::string payloadHash)
IOVEditor createIov(const std::string &tag, cond::TimeType timeType, cond::SynchronizationType synchronizationType=cond::SYNCH_ANY)
std::unique_ptr< T > fetchPayload(const cond::Hash &payloadHash)
~SiStripPayloadHandler() override
void setDescription(const std::string &description)
void analyze(const edm::Event &evt, const edm::EventSetup &evtSetup) override
Transaction & transaction()
SiStripPayloadHandler< SiStripPedestals > SiStripO2OPedestals
void setParameters(const edm::ParameterSet &connectionPset)
SiStripPayloadHandler< SiStripBadStrip > SiStripO2OBadStrip
unsigned long long Time_t
SiStripPayloadHandler< SiStripApvGain > SiStripO2OApvGain
void getValue(SiStripFedCabling *&val)
std::string m_cfgMapSchemaName
void setLastIovGain(std::shared_ptr< SiStripApvGain > gain)
std::string m_cfgMapDbFile
IOVProxy readIov(const std::string &tag)
cond::persistency::ConnectionPool m_connectionPool
Log< level::Info, false > LogInfo
void insert(cond::Time_t since, const cond::Hash &payloadHash, bool checkType=false)
SiStripPayloadHandler(const edm::ParameterSet &iConfig)
std::string getConfigString(const std::type_info &typeInfo)
cond::Hash storePayload(const T &payload, const boost::posix_time::ptime &creationTime=boost::posix_time::microsec_clock::universal_time())
std::string m_localCondDbFile
std::string m_configMapDb
SiStripPayloadHandler< SiStripThreshold > SiStripO2OThreshold
SiStripPayloadHandler< SiStripLatency > SiStripO2OLatency
std::string makeConfigHash()