File indexing completed on 2024-04-06 12:26:38
0001 #include "RecoLuminosity/LumiProducer/interface/RevisionDML.h"
0002 #include "RelationalAccess/ISchema.h"
0003 #include "RelationalAccess/ITable.h"
0004 #include "RelationalAccess/ITableDataEditor.h"
0005 #include "RelationalAccess/IQuery.h"
0006 #include "RelationalAccess/ICursor.h"
0007 #include "RelationalAccess/SchemaException.h"
0008 #include "CoralBase/AttributeList.h"
0009 #include "CoralBase/Attribute.h"
0010 #include "CoralBase/AttributeSpecification.h"
0011 #include "CoralBase/TimeStamp.h"
0012 #include "RecoLuminosity/LumiProducer/interface/LumiNames.h"
0013 #include "RecoLuminosity/LumiProducer/interface/idDealer.h"
0014 #include "RecoLuminosity/LumiProducer/interface/Exception.h"
0015 #include <algorithm>
0016 unsigned long long lumi::RevisionDML::getEntryInBranchByName(coral::ISchema& schema,
0017 const std::string& datatableName,
0018 const std::string& entryname,
0019 const std::string& branchname) {
0020 unsigned long long entry_id = 0;
0021 coral::IQuery* qHandle = schema.newQuery();
0022 qHandle->addToTableList(lumi::LumiNames::entryTableName(datatableName), "e");
0023 qHandle->addToTableList(lumi::LumiNames::revisionTableName(), "r");
0024 qHandle->addToOutputList("e.ENTRY_ID", "entry_id");
0025
0026 coral::AttributeList qCondition;
0027 qCondition.extend("entryname", typeid(std::string));
0028 qCondition.extend("branchname", typeid(std::string));
0029 qCondition["entryname"].data<std::string>() = entryname;
0030 qCondition["branchname"].data<std::string>() = branchname;
0031 std::string qConditionStr("r.REVISION_ID=e.REVISION_ID and e.NAME=:entryname AND r.BRANCH_NAME=:branchname");
0032
0033 coral::AttributeList qResult;
0034 qResult.extend("entry_id", typeid(unsigned long long));
0035 qHandle->defineOutput(qResult);
0036 qHandle->setCondition(qConditionStr, qCondition);
0037 coral::ICursor& cursor = qHandle->execute();
0038 while (cursor.next()) {
0039 entry_id = cursor.currentRow()["entry_id"].data<unsigned long long>();
0040 }
0041 delete qHandle;
0042 return entry_id;
0043 }
0044 void lumi::RevisionDML::bookNewEntry(coral::ISchema& schema,
0045 const std::string& datatableName,
0046 lumi::RevisionDML::Entry& entry) {
0047 lumi::idDealer idg(schema);
0048 const std::string entrytableName = lumi::LumiNames::entryTableName(datatableName);
0049 entry.revision_id = idg.generateNextIDForTable(lumi::LumiNames::revisionTableName());
0050 entry.data_id = idg.generateNextIDForTable(datatableName);
0051 entry.entry_id = idg.generateNextIDForTable(entrytableName);
0052 }
0053 void lumi::RevisionDML::bookNewRevision(coral::ISchema& schema,
0054 const std::string& datatableName,
0055 lumi::RevisionDML::Entry& revision) {
0056 lumi::idDealer idg(schema);
0057 revision.revision_id = idg.generateNextIDForTable(lumi::LumiNames::revisionTableName());
0058 revision.data_id = idg.generateNextIDForTable(datatableName);
0059 }
0060 void lumi::RevisionDML::addEntry(coral::ISchema& schema,
0061 const std::string& datatableName,
0062 const lumi::RevisionDML::Entry& entry,
0063 unsigned long long branchid,
0064 const std::string& branchname) {
0065 coral::AttributeList revdata;
0066 revdata.extend("REVISION_ID", typeid(unsigned long long));
0067 revdata.extend("BRANCH_ID", typeid(unsigned long long));
0068 revdata.extend("BRANCH_NAME", typeid(std::string));
0069 revdata.extend("CTIME", typeid(coral::TimeStamp));
0070 revdata["REVISION_ID"].data<unsigned long long>() = entry.revision_id;
0071 revdata["BRANCH_ID"].data<unsigned long long>() = branchid;
0072 revdata["BRANCH_NAME"].data<std::string>() = branchname;
0073 revdata["CTIME"].data<coral::TimeStamp>() = coral::TimeStamp::now();
0074 const std::string revTableName = lumi::LumiNames::revisionTableName();
0075 schema.tableHandle(revTableName).dataEditor().insertRow(revdata);
0076
0077 coral::AttributeList entrydata;
0078 entrydata.extend("REVISION_ID", typeid(unsigned long long));
0079 entrydata.extend("ENTRY_ID", typeid(unsigned long long));
0080 entrydata.extend("NAME", typeid(std::string));
0081 entrydata["REVISION_ID"].data<unsigned long long>() = entry.revision_id;
0082 entrydata["ENTRY_ID"].data<unsigned long long>() = entry.entry_id;
0083 entrydata["NAME"].data<std::string>() = entry.entry_name;
0084 const std::string entryTableName = lumi::LumiNames::entryTableName(datatableName);
0085 schema.tableHandle(entryTableName).dataEditor().insertRow(entrydata);
0086
0087 coral::AttributeList revmapdata;
0088 revmapdata.extend("REVISION_ID", typeid(unsigned long long));
0089 revmapdata.extend("DATA_ID", typeid(unsigned long long));
0090
0091 revmapdata["REVISION_ID"].data<unsigned long long>() = entry.revision_id;
0092 revmapdata["DATA_ID"].data<unsigned long long>() = entry.data_id;
0093
0094 const std::string revmapTableName = lumi::LumiNames::revmapTableName(datatableName);
0095 schema.tableHandle(revmapTableName).dataEditor().insertRow(revmapdata);
0096 }
0097 void lumi::RevisionDML::addRevision(coral::ISchema& schema,
0098 const std::string& datatableName,
0099 const lumi::RevisionDML::Entry& revision,
0100 unsigned long long branchid,
0101 std::string& branchname) {
0102 coral::AttributeList revdata;
0103 revdata.extend("REVISION_ID", typeid(unsigned long long));
0104 revdata.extend("BRANCH_ID", typeid(unsigned long long));
0105 revdata.extend("BRANCH_NAME", typeid(std::string));
0106 revdata.extend("CTIME", typeid(coral::TimeStamp));
0107 revdata["REVISION_ID"].data<unsigned long long>() = revision.revision_id;
0108 revdata["BRANCH_ID"].data<unsigned long long>() = branchid;
0109 revdata["BRANCH_NAME"].data<std::string>() = branchname;
0110 revdata["CTIME"].data<coral::TimeStamp>() = coral::TimeStamp::now();
0111 schema.tableHandle(lumi::LumiNames::revisionTableName()).dataEditor().insertRow(revdata);
0112 coral::AttributeList revmapdata;
0113 revmapdata.extend("REVISION_ID", typeid(unsigned long long));
0114 revmapdata.extend("DATA_ID", typeid(unsigned long long));
0115 revmapdata["REVISION_ID"].data<unsigned long long>() = revision.revision_id;
0116 revmapdata["DATA_ID"].data<unsigned long long>() = revision.data_id;
0117 const std::string revmapTableName = lumi::LumiNames::revmapTableName(datatableName);
0118 schema.tableHandle(revmapTableName).dataEditor().insertRow(revmapdata);
0119 }
0120 void lumi::RevisionDML::insertLumiRunData(coral::ISchema& schema, const lumi::RevisionDML::LumiEntry& lumientry) {
0121 coral::AttributeList lumirundata;
0122 lumirundata.extend("DATA_ID", typeid(unsigned long long));
0123 lumirundata.extend("ENTRY_ID", typeid(unsigned long long));
0124 lumirundata.extend("ENTRY_NAME", typeid(std::string));
0125 lumirundata.extend("RUNNUM", typeid(unsigned int));
0126 lumirundata.extend("SOURCE", typeid(std::string));
0127 lumirundata.extend("NOMINALEGEV", typeid(float));
0128 lumirundata.extend("NCOLLIDINGBUNCHES", typeid(unsigned int));
0129 lumirundata["DATA_ID"].data<unsigned long long>() = lumientry.data_id;
0130 lumirundata["ENTRY_ID"].data<unsigned long long>() = lumientry.entry_id;
0131 lumirundata["ENTRY_NAME"].data<std::string>() = lumientry.entry_name;
0132 lumirundata["RUNNUM"].data<unsigned int>() = lumientry.runnumber;
0133 lumirundata["SOURCE"].data<std::string>() = lumientry.source;
0134 lumirundata["NOMINALEGEV"].data<float>() = lumientry.bgev;
0135 lumirundata["NCOLLIDINGBUNCHES"].data<unsigned int>() = lumientry.ncollidingbunches;
0136 const std::string lumidataTableName = lumi::LumiNames::lumidataTableName();
0137 schema.tableHandle(lumidataTableName).dataEditor().insertRow(lumirundata);
0138 }
0139 void lumi::RevisionDML::insertTrgRunData(coral::ISchema& schema, const lumi::RevisionDML::TrgEntry& trgentry) {
0140 coral::AttributeList trgrundata;
0141 trgrundata.extend("DATA_ID", typeid(unsigned long long));
0142 trgrundata.extend("ENTRY_ID", typeid(unsigned long long));
0143 trgrundata.extend("ENTRY_NAME", typeid(std::string));
0144 trgrundata.extend("RUNNUM", typeid(unsigned int));
0145 trgrundata.extend("SOURCE", typeid(std::string));
0146 trgrundata.extend("BITZERONAME", typeid(std::string));
0147 trgrundata.extend("BITNAMECLOB", typeid(std::string));
0148 trgrundata["DATA_ID"].data<unsigned long long>() = trgentry.data_id;
0149 trgrundata["ENTRY_ID"].data<unsigned long long>() = trgentry.entry_id;
0150 trgrundata["ENTRY_NAME"].data<std::string>() = trgentry.entry_name;
0151 trgrundata["RUNNUM"].data<unsigned int>() = trgentry.runnumber;
0152 trgrundata["SOURCE"].data<std::string>() = trgentry.source;
0153 trgrundata["BITZERONAME"].data<std::string>() = trgentry.bitzeroname;
0154 trgrundata["BITNAMECLOB"].data<std::string>() = trgentry.bitnames;
0155 const std::string trgdataTableName = lumi::LumiNames::trgdataTableName();
0156 schema.tableHandle(trgdataTableName).dataEditor().insertRow(trgrundata);
0157 }
0158 void lumi::RevisionDML::insertHltRunData(coral::ISchema& schema, const lumi::RevisionDML::HltEntry& hltentry) {
0159 coral::AttributeList hltrundata;
0160 hltrundata.extend("DATA_ID", typeid(unsigned long long));
0161 hltrundata.extend("ENTRY_ID", typeid(unsigned long long));
0162 hltrundata.extend("ENTRY_NAME", typeid(std::string));
0163 hltrundata.extend("RUNNUM", typeid(unsigned int));
0164 hltrundata.extend("SOURCE", typeid(std::string));
0165 hltrundata.extend("NPATH", typeid(unsigned int));
0166 hltrundata.extend("PATHNAMECLOB", typeid(std::string));
0167 hltrundata["DATA_ID"].data<unsigned long long>() = hltentry.data_id;
0168 hltrundata["ENTRY_ID"].data<unsigned long long>() = hltentry.entry_id;
0169 hltrundata["ENTRY_NAME"].data<std::string>() = hltentry.entry_name;
0170 hltrundata["RUNNUM"].data<unsigned int>() = hltentry.runnumber;
0171 hltrundata["SOURCE"].data<std::string>() = hltentry.source;
0172 hltrundata["NPATH"].data<unsigned int>() = hltentry.npath;
0173 hltrundata["PATHNAMECLOB"].data<std::string>() = hltentry.pathnames;
0174 const std::string hltdataTableName = lumi::LumiNames::hltdataTableName();
0175 schema.tableHandle(hltdataTableName).dataEditor().insertRow(hltrundata);
0176 }
0177
0178 unsigned long long lumi::RevisionDML::currentHFDataTagId(coral::ISchema& schema) {
0179 unsigned long long currentdatatagid = 0;
0180 std::vector<unsigned long long> alltagids;
0181 coral::IQuery* qHandle = schema.newQuery();
0182 qHandle->addToTableList(lumi::LumiNames::tagsTableName());
0183 qHandle->addToOutputList("TAGID");
0184 coral::AttributeList qResult;
0185 qResult.extend("TAGID", typeid(unsigned long long));
0186 qHandle->defineOutput(qResult);
0187 coral::ICursor& cursor = qHandle->execute();
0188 while (cursor.next()) {
0189 if (!cursor.currentRow()["TAGID"].isNull()) {
0190 alltagids.push_back(cursor.currentRow()["TAGID"].data<unsigned long long>());
0191 }
0192 }
0193 delete qHandle;
0194 if (!alltagids.empty()) {
0195 std::vector<unsigned long long>::iterator currentdatatagidIt = std::max_element(alltagids.begin(), alltagids.end());
0196 currentdatatagid = *currentdatatagidIt;
0197 }
0198 return currentdatatagid;
0199 }
0200
0201 unsigned long long lumi::RevisionDML::HFDataTagIdByName(coral::ISchema& schema, const std::string& datatagname) {
0202 unsigned long long datatagid = 0;
0203 coral::IQuery* qHandle = schema.newQuery();
0204 qHandle->addToTableList(lumi::LumiNames::tagsTableName());
0205 const std::string conditionStr("TAGNAME=:tagname");
0206 coral::AttributeList condition;
0207 condition.extend("tagname", typeid(std::string));
0208 condition["tagname"].data<std::string>() = datatagname;
0209 qHandle->addToOutputList("TAGID");
0210 coral::AttributeList qResult;
0211 qResult.extend("TAGID", typeid(unsigned long long));
0212 qHandle->setCondition(conditionStr, condition);
0213 qHandle->defineOutput(qResult);
0214 coral::ICursor& cursor = qHandle->execute();
0215 while (cursor.next()) {
0216 if (!cursor.currentRow()["TAGID"].isNull()) {
0217 datatagid = cursor.currentRow()["TAGID"].data<unsigned long long>();
0218 }
0219 }
0220 delete qHandle;
0221 return datatagid;
0222 }
0223
0224 unsigned long long lumi::RevisionDML::addRunToCurrentHFDataTag(coral::ISchema& schema,
0225 unsigned int runnum,
0226 unsigned long long lumiid,
0227 unsigned long long trgid,
0228 unsigned long long hltid,
0229 const std::string& patchcomment) {
0230 unsigned long long currenttagid = currentHFDataTagId(schema);
0231 coral::AttributeList tagrundata;
0232 tagrundata.extend("TAGID", typeid(unsigned long long));
0233 tagrundata.extend("RUNNUM", typeid(unsigned int));
0234 tagrundata.extend("LUMIDATAID", typeid(unsigned long long));
0235 tagrundata.extend("TRGDATAID", typeid(unsigned long long));
0236 tagrundata.extend("HLTDATAID", typeid(unsigned long long));
0237 tagrundata.extend("CREATIONTIME", typeid(coral::TimeStamp));
0238 tagrundata.extend("COMMENT", typeid(std::string));
0239 tagrundata["TAGID"].data<unsigned long long>() = currenttagid;
0240 tagrundata["RUNNUM"].data<unsigned int>() = runnum;
0241 tagrundata["LUMIDATAID"].data<unsigned long long>() = lumiid;
0242 tagrundata["TRGDATAID"].data<unsigned long long>() = trgid;
0243 tagrundata["HLTDATAID"].data<unsigned long long>() = hltid;
0244 tagrundata["CREATIONTIME"].data<coral::TimeStamp>() = coral::TimeStamp::now();
0245 tagrundata["COMMENT"].data<std::string>() = patchcomment;
0246 const std::string tagrunTableName = lumi::LumiNames::tagRunsTableName();
0247 try {
0248 schema.tableHandle(lumi::LumiNames::tagRunsTableName()).dataEditor().insertRow(tagrundata);
0249 } catch (const coral::DuplicateEntryInUniqueKeyException& er) {
0250 throw lumi::duplicateRunInDataTagException("", "addRunToCurrentHFDataTag", "RevisionDML");
0251 }
0252 return currenttagid;
0253 }
0254
0255 lumi::RevisionDML::DataID lumi::RevisionDML::dataIDForRun(coral::ISchema& schema,
0256 unsigned int runnum,
0257 unsigned long long tagid) {
0258 lumi::RevisionDML::DataID result;
0259 coral::IQuery* qHandle = schema.newQuery();
0260 qHandle->addToTableList(lumi::LumiNames::tagRunsTableName());
0261 qHandle->addToOutputList("LUMIDATAID");
0262 qHandle->addToOutputList("TRGDATAID");
0263 qHandle->addToOutputList("HLTDATAID");
0264 coral::AttributeList qResult;
0265 qResult.extend("LUMIDATAID", typeid(unsigned long long));
0266 qResult.extend("TRGDATAID", typeid(unsigned long long));
0267 qResult.extend("HLTDATAID", typeid(unsigned long long));
0268 qHandle->defineOutput(qResult);
0269 coral::AttributeList qCondition;
0270 qCondition.extend("tagid", typeid(unsigned long long));
0271 qCondition.extend("runnum", typeid(unsigned int));
0272 qCondition["tagid"].data<unsigned long long>() = tagid;
0273 qCondition["runnum"].data<unsigned int>() = runnum;
0274 std::string qConditionStr("TAGID<=:tagid AND RUNNUM=:runnum");
0275 qHandle->setCondition(qConditionStr, qCondition);
0276 coral::ICursor& cursor = qHandle->execute();
0277 unsigned long long minlumid = 0;
0278 unsigned long long mintrgid = 0;
0279 unsigned long long minhltid = 0;
0280 while (cursor.next()) {
0281 if (!cursor.currentRow()["LUMIDATAID"].isNull()) {
0282 unsigned long long lumiid = cursor.currentRow()["LUMIDATAID"].data<unsigned long long>();
0283 if (lumiid > minlumid) {
0284 result.lumi_id = lumiid;
0285 }
0286 }
0287 if (!cursor.currentRow()["TRGDATAID"].isNull()) {
0288 unsigned long long trgid = cursor.currentRow()["TRGDATAID"].data<unsigned long long>();
0289 if (trgid > mintrgid) {
0290 result.trg_id = trgid;
0291 }
0292 }
0293 if (!cursor.currentRow()["HLTDATAID"].isNull()) {
0294 unsigned long long hltid = cursor.currentRow()["HLTDATAID"].data<unsigned long long>();
0295 if (hltid > minhltid) {
0296 result.hlt_id = hltid;
0297 }
0298 }
0299 }
0300 delete qHandle;
0301 return result;
0302 }