File indexing completed on 2024-04-06 12:26:34
0001 #ifndef RecoLuminosity_LumiProducer_CMSRunSummary2DB_h
0002 #define RecoLuminosity_LumiProducer_CMSRunSummary2DB_h
0003 #include "CoralBase/AttributeList.h"
0004 #include "CoralBase/Attribute.h"
0005 #include "CoralBase/AttributeSpecification.h"
0006 #include "CoralBase/Exception.h"
0007 #include "CoralBase/TimeStamp.h"
0008 #include "RelationalAccess/ConnectionService.h"
0009 #include "RelationalAccess/ISessionProxy.h"
0010 #include "RelationalAccess/ITransaction.h"
0011 #include "RelationalAccess/ITypeConverter.h"
0012 #include "RelationalAccess/IQuery.h"
0013 #include "RelationalAccess/ICursor.h"
0014 #include "RelationalAccess/ISchema.h"
0015 #include "RelationalAccess/IView.h"
0016 #include "RelationalAccess/ITable.h"
0017 #include "RelationalAccess/ITableDataEditor.h"
0018 #include "RelationalAccess/IBulkOperation.h"
0019 #include "RecoLuminosity/LumiProducer/interface/DataPipe.h"
0020 #include "RecoLuminosity/LumiProducer/interface/LumiNames.h"
0021 #include "RecoLuminosity/LumiProducer/interface/idDealer.h"
0022 #include "RecoLuminosity/LumiProducer/interface/Exception.h"
0023 #include "RecoLuminosity/LumiProducer/interface/DBConfig.h"
0024 #include "RecoLuminosity/LumiProducer/interface/ConstantDef.h"
0025 #include "RecoLuminosity/LumiProducer/interface/DataPipe.h"
0026 #include "RecoLuminosity/LumiProducer/interface/LumiNames.h"
0027 #include "RecoLuminosity/LumiProducer/interface/idDealer.h"
0028 #include "RecoLuminosity/LumiProducer/interface/Exception.h"
0029 #include "RecoLuminosity/LumiProducer/interface/DBConfig.h"
0030 #include "RecoLuminosity/LumiProducer/interface/ConstantDef.h"
0031 #include <iostream>
0032 #include <sstream>
0033 #include <fstream>
0034 #include <vector>
0035 #include <string>
0036 #include <boost/regex.hpp>
0037 #include <boost/tokenizer.hpp>
0038
0039 namespace lumi {
0040 class CMSRunSummary2DB : public DataPipe {
0041 public:
0042 CMSRunSummary2DB(const std::string& dest);
0043 unsigned long long retrieveData(unsigned int runnumber) override;
0044 const std::string dataType() const override;
0045 const std::string sourceType() const override;
0046 unsigned int str2int(const std::string& s) const;
0047 ~CMSRunSummary2DB() override;
0048
0049 private:
0050 struct cmsrunsum {
0051 std::string l1key;
0052 std::string amodetag;
0053 int egev;
0054 std::string hltkey;
0055 std::string fillnumber;
0056 std::string sequence;
0057 std::string fillscheme;
0058 int ncollidingbunches;
0059 coral::TimeStamp startT;
0060 coral::TimeStamp stopT;
0061 };
0062 bool isCollisionRun(const lumi::CMSRunSummary2DB::cmsrunsum& rundata);
0063 void parseFillCSV(const std::string& csvsource, cmsrunsum& result);
0064 };
0065
0066
0067
0068 void CMSRunSummary2DB::parseFillCSV(const std::string& csvsource, lumi::CMSRunSummary2DB::cmsrunsum& result) {
0069 result.fillscheme = std::string("");
0070 result.ncollidingbunches = 0;
0071 std::ifstream csvfile;
0072 csvfile.open(csvsource.c_str());
0073 if (!csvfile) {
0074 std::cout << "[warning] unable to open file: " << csvsource << std::endl;
0075 return;
0076 }
0077 typedef boost::tokenizer<boost::escaped_list_separator<char> > Tokenizer;
0078 std::vector<std::string> record;
0079 std::string line;
0080 while (std::getline(csvfile, line)) {
0081 Tokenizer tok(line);
0082 record.assign(tok.begin(), tok.end());
0083 if (record.size() < 3)
0084 continue;
0085 std::string fillnum = record[0];
0086 if (fillnum == result.fillnumber) {
0087 result.fillscheme = record[1];
0088 std::string ncollidingbunchesStr = record[2];
0089 result.ncollidingbunches = str2int(ncollidingbunchesStr);
0090 break;
0091 }
0092 }
0093 }
0094 CMSRunSummary2DB::CMSRunSummary2DB(const std::string& dest) : DataPipe(dest) {}
0095 bool CMSRunSummary2DB::isCollisionRun(const lumi::CMSRunSummary2DB::cmsrunsum& rundata) {
0096 bool isCollision = false;
0097 bool isPhysics = false;
0098 std::string hk = rundata.hltkey;
0099 std::string lk = rundata.l1key;
0100 boost::match_results<std::string::const_iterator> what;
0101 const boost::regex lexpr("^TSC_.+_collisions_.+");
0102 boost::regex_match(lk, what, lexpr, boost::match_default);
0103 if (what[0].matched)
0104 isCollision = true;
0105 const boost::regex hexpr("^/cdaq/physics/.+");
0106 boost::regex_match(hk, what, hexpr, boost::match_default);
0107 if (what[0].matched)
0108 isPhysics = true;
0109 return (isCollision && isPhysics);
0110 }
0111 unsigned long long CMSRunSummary2DB::retrieveData(unsigned int runnumber) {
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123 cmsrunsum result;
0124 std::string runinfoschema("CMS_RUNINFO");
0125 std::string runsessionParamTable("RUNSESSION_PARAMETER");
0126 coral::ConnectionService* svc = new coral::ConnectionService;
0127 lumi::DBConfig dbconf(*svc);
0128 if (!m_authpath.empty()) {
0129 dbconf.setAuthentication(m_authpath);
0130 }
0131
0132
0133 std::string::size_type cutpos = m_source.find(';');
0134 std::string dbsource = m_source;
0135 std::string csvsource("");
0136 if (cutpos != std::string::npos) {
0137 dbsource = m_source.substr(0, cutpos);
0138 csvsource = m_source.substr(cutpos + 1);
0139 }
0140
0141 coral::ISessionProxy* runinfosession = svc->connect(dbsource, coral::ReadOnly);
0142 try {
0143 coral::ITypeConverter& tpc = runinfosession->typeConverter();
0144 tpc.setCppTypeForSqlType("unsigned int", "NUMBER(38)");
0145 runinfosession->transaction().start(true);
0146 coral::ISchema& runinfoschemaHandle = runinfosession->schema(runinfoschema);
0147 if (!runinfoschemaHandle.existsTable(runsessionParamTable)) {
0148 throw lumi::Exception(
0149 std::string("non-existing table " + runsessionParamTable), "CMSRunSummary2DB", "retrieveData");
0150 }
0151 coral::IQuery* amodetagQuery = runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
0152 coral::AttributeList amodetagOutput;
0153 amodetagOutput.extend("amodetag", typeid(std::string));
0154 coral::AttributeList amodetagCondition;
0155 amodetagCondition = coral::AttributeList();
0156 amodetagCondition.extend("name", typeid(std::string));
0157 amodetagCondition.extend("runnumber", typeid(unsigned int));
0158 amodetagCondition["name"].data<std::string>() = std::string("CMS.SCAL:AMODEtag");
0159 amodetagCondition["runnumber"].data<unsigned int>() = runnumber;
0160 amodetagQuery->addToOutputList("distinct(STRING_VALUE)");
0161 amodetagQuery->setCondition("NAME=:name AND RUNNUMBER=:runnumber", amodetagCondition);
0162
0163 amodetagQuery->defineOutput(amodetagOutput);
0164 coral::ICursor& amodetagCursor = amodetagQuery->execute();
0165 std::vector<std::string> amodes;
0166 while (amodetagCursor.next()) {
0167 const coral::AttributeList& row = amodetagCursor.currentRow();
0168 amodes.push_back(row["amodetag"].data<std::string>());
0169
0170 }
0171
0172
0173
0174 std::string amd;
0175 for (std::vector<std::string>::iterator it = amodes.begin(); it != amodes.end(); ++it) {
0176 if (it->find("PHYS") == std::string::npos)
0177 continue;
0178 amd = *it;
0179 }
0180 if (amd.empty() && !amodes.empty()) {
0181 amd = *(amodes.begin());
0182 }
0183 if (amd.empty()) {
0184 amd = std::string("PROTPHYS");
0185 }
0186
0187 result.amodetag = amd;
0188 delete amodetagQuery;
0189
0190 coral::IQuery* egevQuery = runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
0191 coral::AttributeList egevOutput;
0192 egevOutput.extend("egev", typeid(std::string));
0193 coral::AttributeList egevCondition;
0194 egevCondition = coral::AttributeList();
0195 egevCondition.extend("name", typeid(std::string));
0196 egevCondition.extend("runnumber", typeid(unsigned int));
0197 egevCondition["name"].data<std::string>() = std::string("CMS.SCAL:EGEV");
0198 egevCondition["runnumber"].data<unsigned int>() = runnumber;
0199 egevQuery->addToOutputList("distinct(STRING_VALUE)");
0200 egevQuery->setCondition("NAME=:name AND RUNNUMBER=:runnumber", egevCondition);
0201 egevQuery->defineOutput(egevOutput);
0202 coral::ICursor& egevCursor = egevQuery->execute();
0203 result.egev = 0;
0204 while (egevCursor.next()) {
0205 const coral::AttributeList& row = egevCursor.currentRow();
0206 std::string egevstr = row["egev"].data<std::string>();
0207 int tmpgev = str2int(egevstr);
0208 if (tmpgev > result.egev) {
0209 result.egev = tmpgev;
0210 }
0211 }
0212 if (result.egev == 0) {
0213 result.egev = 3500;
0214 }
0215 delete egevQuery;
0216
0217 coral::IQuery* seqQuery = runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
0218 coral::AttributeList seqBindVariableList;
0219 seqBindVariableList.extend("runnumber", typeid(unsigned int));
0220 seqBindVariableList.extend("name", typeid(std::string));
0221
0222 seqBindVariableList["runnumber"].data<unsigned int>() = runnumber;
0223 seqBindVariableList["name"].data<std::string>() = std::string("CMS.LVL0:SEQ_NAME");
0224 seqQuery->setCondition("RUNNUMBER =:runnumber AND NAME =:name", seqBindVariableList);
0225 seqQuery->addToOutputList("STRING_VALUE");
0226 coral::ICursor& seqCursor = seqQuery->execute();
0227
0228 while (seqCursor.next()) {
0229 const coral::AttributeList& row = seqCursor.currentRow();
0230 result.sequence = row["STRING_VALUE"].data<std::string>();
0231 }
0232 delete seqQuery;
0233
0234 coral::IQuery* hltkeyQuery = runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
0235 coral::AttributeList hltkeyBindVariableList;
0236 hltkeyBindVariableList.extend("runnumber", typeid(unsigned int));
0237 hltkeyBindVariableList.extend("name", typeid(std::string));
0238
0239 hltkeyBindVariableList["runnumber"].data<unsigned int>() = runnumber;
0240 hltkeyBindVariableList["name"].data<std::string>() = std::string("CMS.LVL0:HLT_KEY_DESCRIPTION");
0241 hltkeyQuery->setCondition("RUNNUMBER =:runnumber AND NAME =:name", hltkeyBindVariableList);
0242 hltkeyQuery->addToOutputList("STRING_VALUE");
0243 coral::ICursor& hltkeyCursor = hltkeyQuery->execute();
0244
0245 while (hltkeyCursor.next()) {
0246 const coral::AttributeList& row = hltkeyCursor.currentRow();
0247 result.hltkey = row["STRING_VALUE"].data<std::string>();
0248 }
0249 delete hltkeyQuery;
0250
0251 coral::IQuery* fillQuery = runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
0252 coral::AttributeList fillBindVariableList;
0253 fillBindVariableList.extend("runnumber", typeid(unsigned int));
0254 fillBindVariableList.extend("name", typeid(std::string));
0255
0256 fillBindVariableList["runnumber"].data<unsigned int>() = runnumber;
0257 fillBindVariableList["name"].data<std::string>() = std::string("CMS.SCAL:FILLN");
0258 fillQuery->setCondition("RUNNUMBER =:runnumber AND NAME =:name", fillBindVariableList);
0259 fillQuery->addToOutputList("STRING_VALUE");
0260 fillQuery->addToOrderList("TIME");
0261
0262 coral::ICursor& fillCursor = fillQuery->execute();
0263 unsigned int cc = 0;
0264 while (fillCursor.next()) {
0265 const coral::AttributeList& row = fillCursor.currentRow();
0266 if (cc == 0) {
0267 result.fillnumber = row["STRING_VALUE"].data<std::string>();
0268 }
0269 ++cc;
0270 }
0271 delete fillQuery;
0272 if (result.fillnumber.empty()) {
0273 throw nonCollisionException("retrieveData", "CMSRunSummary2DB");
0274 }
0275 coral::IQuery* startTQuery = runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
0276 coral::AttributeList startTVariableList;
0277 startTVariableList.extend("runnumber", typeid(unsigned int));
0278 startTVariableList.extend("name", typeid(std::string));
0279
0280 startTVariableList["runnumber"].data<unsigned int>() = runnumber;
0281 startTVariableList["name"].data<std::string>() = std::string("CMS.LVL0:START_TIME_T");
0282 startTQuery->setCondition("RUNNUMBER =:runnumber AND NAME =:name", startTVariableList);
0283 startTQuery->addToOutputList("TIME");
0284 coral::ICursor& startTCursor = startTQuery->execute();
0285
0286 while (startTCursor.next()) {
0287 const coral::AttributeList& row = startTCursor.currentRow();
0288 result.startT = row["TIME"].data<coral::TimeStamp>();
0289 }
0290 delete startTQuery;
0291 coral::IQuery* stopTQuery = runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
0292 coral::AttributeList stopTVariableList;
0293 stopTVariableList.extend("runnumber", typeid(unsigned int));
0294 stopTVariableList.extend("name", typeid(std::string));
0295
0296 stopTVariableList["runnumber"].data<unsigned int>() = runnumber;
0297 stopTVariableList["name"].data<std::string>() = std::string("CMS.LVL0:STOP_TIME_T");
0298 stopTQuery->setCondition("RUNNUMBER =:runnumber AND NAME =:name", stopTVariableList);
0299 stopTQuery->addToOutputList("TIME");
0300 coral::ICursor& stopTCursor = stopTQuery->execute();
0301
0302 while (stopTCursor.next()) {
0303 const coral::AttributeList& row = stopTCursor.currentRow();
0304 result.stopT = row["TIME"].data<coral::TimeStamp>();
0305 }
0306 delete stopTQuery;
0307 coral::IQuery* l1keyQuery = runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
0308 coral::AttributeList l1keyOutput;
0309 l1keyOutput.extend("l1key", typeid(std::string));
0310 coral::AttributeList l1keyCondition;
0311 l1keyCondition = coral::AttributeList();
0312 l1keyCondition.extend("name", typeid(std::string));
0313 l1keyCondition.extend("runnumber", typeid(unsigned int));
0314 l1keyCondition["name"].data<std::string>() = std::string("CMS.TRG:TSC_KEY");
0315 l1keyCondition["runnumber"].data<unsigned int>() = runnumber;
0316 l1keyQuery->addToOutputList("STRING_VALUE");
0317 l1keyQuery->setCondition("NAME=:name AND RUNNUMBER=:runnumber", l1keyCondition);
0318
0319 l1keyQuery->defineOutput(l1keyOutput);
0320 coral::ICursor& l1keyCursor = l1keyQuery->execute();
0321 while (l1keyCursor.next()) {
0322 const coral::AttributeList& row = l1keyCursor.currentRow();
0323 result.l1key = row["l1key"].data<std::string>();
0324 }
0325 delete l1keyQuery;
0326 } catch (const coral::Exception& er) {
0327 runinfosession->transaction().rollback();
0328 delete runinfosession;
0329 delete svc;
0330 throw er;
0331 }
0332 runinfosession->transaction().commit();
0333 delete runinfosession;
0334
0335 if (!csvsource.empty()) {
0336 parseFillCSV(csvsource, result);
0337 } else {
0338 result.fillscheme = std::string("");
0339 result.ncollidingbunches = 0;
0340 }
0341 std::cout << "result for run " << runnumber << " : sequence : " << result.sequence
0342 << " : hltkey : " << result.hltkey << " : fillnumber : " << result.fillnumber
0343 << " : l1key : " << result.l1key << " : amodetag :" << result.amodetag << " : egev : " << result.egev
0344 << " : fillscheme " << result.fillscheme << " : ncollidingbunches : " << result.ncollidingbunches
0345 << std::endl;
0346
0347
0348 coral::ISessionProxy* destsession = svc->connect(m_dest, coral::Update);
0349
0350 coral::ITypeConverter& desttpc = destsession->typeConverter();
0351 desttpc.setCppTypeForSqlType("unsigned int", "NUMBER(10)");
0352 try {
0353 destsession->transaction().start(false);
0354 coral::ISchema& destschema = destsession->nominalSchema();
0355 coral::ITable& destruntable = destschema.tableHandle(LumiNames::cmsrunsummaryTableName());
0356 coral::AttributeList runData;
0357 destruntable.dataEditor().rowBuffer(runData);
0358 runData["RUNNUM"].data<unsigned int>() = runnumber;
0359 runData["FILLNUM"].data<unsigned int>() = str2int(result.fillnumber);
0360 runData["SEQUENCE"].data<std::string>() = result.sequence;
0361 runData["HLTKEY"].data<std::string>() = result.hltkey;
0362 runData["STARTTIME"].data<coral::TimeStamp>() = result.startT;
0363 runData["STOPTIME"].data<coral::TimeStamp>() = result.stopT;
0364 runData["AMODETAG"].data<std::string>() = result.amodetag;
0365 runData["EGEV"].data<unsigned int>() = (unsigned int)result.egev;
0366 runData["L1KEY"].data<std::string>() = result.l1key;
0367 runData["FILLSCHEME"].data<std::string>() = result.fillscheme;
0368 runData["NCOLLIDINGBUNCHES"].data<unsigned int>() = result.ncollidingbunches;
0369 destruntable.dataEditor().insertRow(runData);
0370 } catch (const coral::Exception& er) {
0371 std::cout << "database problem " << er.what() << std::endl;
0372 destsession->transaction().rollback();
0373 delete destsession;
0374 delete svc;
0375 throw er;
0376 }
0377 destsession->transaction().commit();
0378 delete svc;
0379 return 0;
0380 }
0381 const std::string CMSRunSummary2DB::dataType() const { return "CMSRUNSUMMARY"; }
0382 const std::string CMSRunSummary2DB::sourceType() const { return "DB"; }
0383 unsigned int CMSRunSummary2DB::str2int(const std::string& s) const {
0384 std::istringstream myStream(s);
0385 unsigned int i;
0386 if (myStream >> i) {
0387 return i;
0388 } else {
0389 throw lumi::Exception(std::string("str2int error"), "str2int", "CMSRunSummary2DB");
0390 }
0391 }
0392 CMSRunSummary2DB::~CMSRunSummary2DB() {}
0393 }
0394 #include "RecoLuminosity/LumiProducer/interface/DataPipeFactory.h"
0395 DEFINE_EDM_PLUGIN(lumi::DataPipeFactory, lumi::CMSRunSummary2DB, "CMSRunSummary2DB");
0396 #endif