1 #ifndef RecoLuminosity_LumiProducer_CMSRunSummary2DB_h 2 #define RecoLuminosity_LumiProducer_CMSRunSummary2DB_h 3 #include "CoralBase/AttributeList.h" 4 #include "CoralBase/Attribute.h" 5 #include "CoralBase/AttributeSpecification.h" 6 #include "CoralBase/Exception.h" 7 #include "CoralBase/TimeStamp.h" 8 #include "RelationalAccess/ConnectionService.h" 9 #include "RelationalAccess/ISessionProxy.h" 10 #include "RelationalAccess/ITransaction.h" 11 #include "RelationalAccess/ITypeConverter.h" 12 #include "RelationalAccess/IQuery.h" 13 #include "RelationalAccess/ICursor.h" 14 #include "RelationalAccess/ISchema.h" 15 #include "RelationalAccess/IView.h" 16 #include "RelationalAccess/ITable.h" 17 #include "RelationalAccess/ITableDataEditor.h" 18 #include "RelationalAccess/IBulkOperation.h" 36 #include <boost/regex.hpp> 37 #include <boost/tokenizer.hpp> 71 std::ifstream csvfile;
72 csvfile.open(csvsource.c_str());
74 std::cout <<
"[warning] unable to open file: " << csvsource << std::endl;
77 typedef boost::tokenizer<boost::escaped_list_separator<char> > Tokenizer;
78 std::vector<std::string>
record;
80 while (std::getline(csvfile, line)) {
82 record.assign(tok.begin(), tok.end());
83 if (record.size() < 3)
97 bool isPhysics =
false;
100 boost::match_results<std::string::const_iterator> what;
101 const boost::regex lexpr(
"^TSC_.+_collisions_.+");
102 boost::regex_match(lk, what, lexpr, boost::match_default);
105 const boost::regex hexpr(
"^/cdaq/physics/.+");
106 boost::regex_match(hk, what, hexpr, boost::match_default);
109 return (isCollision && isPhysics);
125 std::string runsessionParamTable(
"RUNSESSION_PARAMETER");
126 coral::ConnectionService* svc =
new coral::ConnectionService;
136 if (cutpos != std::string::npos) {
137 dbsource =
m_source.substr(0, cutpos);
138 csvsource =
m_source.substr(cutpos + 1);
141 coral::ISessionProxy* runinfosession = svc->connect(dbsource, coral::ReadOnly);
143 coral::ITypeConverter& tpc = runinfosession->typeConverter();
144 tpc.setCppTypeForSqlType(
"unsigned int",
"NUMBER(38)");
145 runinfosession->transaction().start(
true);
146 coral::ISchema& runinfoschemaHandle = runinfosession->schema(runinfoschema);
147 if (!runinfoschemaHandle.existsTable(runsessionParamTable)) {
149 std::string(
"non-existing table " + runsessionParamTable),
"CMSRunSummary2DB",
"retrieveData");
151 coral::IQuery* amodetagQuery = runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
152 coral::AttributeList amodetagOutput;
153 amodetagOutput.extend(
"amodetag",
typeid(
std::string));
154 coral::AttributeList amodetagCondition;
155 amodetagCondition = coral::AttributeList();
156 amodetagCondition.extend(
"name",
typeid(
std::string));
157 amodetagCondition.extend(
"runnumber",
typeid(
unsigned int));
159 amodetagCondition[
"runnumber"].data<
unsigned int>() = runnumber;
160 amodetagQuery->addToOutputList(
"distinct(STRING_VALUE)");
161 amodetagQuery->setCondition(
"NAME=:name AND RUNNUMBER=:runnumber", amodetagCondition);
163 amodetagQuery->defineOutput(amodetagOutput);
164 coral::ICursor& amodetagCursor = amodetagQuery->execute();
165 std::vector<std::string> amodes;
166 while (amodetagCursor.next()) {
167 const coral::AttributeList& row = amodetagCursor.currentRow();
168 amodes.push_back(row[
"amodetag"].data<std::string>());
175 for (std::vector<std::string>::iterator it = amodes.begin(); it != amodes.end(); ++it) {
176 if (it->find(
"PHYS") == std::string::npos)
180 if (amd.empty() && !amodes.empty()) {
181 amd = *(amodes.begin());
188 delete amodetagQuery;
190 coral::IQuery* egevQuery = runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
191 coral::AttributeList egevOutput;
193 coral::AttributeList egevCondition;
194 egevCondition = coral::AttributeList();
196 egevCondition.extend(
"runnumber",
typeid(
unsigned int));
198 egevCondition[
"runnumber"].data<
unsigned int>() = runnumber;
199 egevQuery->addToOutputList(
"distinct(STRING_VALUE)");
200 egevQuery->setCondition(
"NAME=:name AND RUNNUMBER=:runnumber", egevCondition);
201 egevQuery->defineOutput(egevOutput);
202 coral::ICursor& egevCursor = egevQuery->execute();
204 while (egevCursor.next()) {
205 const coral::AttributeList& row = egevCursor.currentRow();
208 if (tmpgev > result.
egev) {
209 result.
egev = tmpgev;
212 if (result.
egev == 0) {
217 coral::IQuery* seqQuery = runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
218 coral::AttributeList seqBindVariableList;
219 seqBindVariableList.extend(
"runnumber",
typeid(
unsigned int));
220 seqBindVariableList.extend(
"name",
typeid(
std::string));
222 seqBindVariableList[
"runnumber"].data<
unsigned int>() = runnumber;
224 seqQuery->setCondition(
"RUNNUMBER =:runnumber AND NAME =:name", seqBindVariableList);
225 seqQuery->addToOutputList(
"STRING_VALUE");
226 coral::ICursor& seqCursor = seqQuery->execute();
228 while (seqCursor.next()) {
229 const coral::AttributeList& row = seqCursor.currentRow();
234 coral::IQuery* hltkeyQuery = runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
235 coral::AttributeList hltkeyBindVariableList;
236 hltkeyBindVariableList.extend(
"runnumber",
typeid(
unsigned int));
237 hltkeyBindVariableList.extend(
"name",
typeid(
std::string));
239 hltkeyBindVariableList[
"runnumber"].data<
unsigned int>() = runnumber;
241 hltkeyQuery->setCondition(
"RUNNUMBER =:runnumber AND NAME =:name", hltkeyBindVariableList);
242 hltkeyQuery->addToOutputList(
"STRING_VALUE");
243 coral::ICursor& hltkeyCursor = hltkeyQuery->execute();
245 while (hltkeyCursor.next()) {
246 const coral::AttributeList& row = hltkeyCursor.currentRow();
251 coral::IQuery* fillQuery = runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
252 coral::AttributeList fillBindVariableList;
253 fillBindVariableList.extend(
"runnumber",
typeid(
unsigned int));
254 fillBindVariableList.extend(
"name",
typeid(
std::string));
256 fillBindVariableList[
"runnumber"].data<
unsigned int>() = runnumber;
258 fillQuery->setCondition(
"RUNNUMBER =:runnumber AND NAME =:name", fillBindVariableList);
259 fillQuery->addToOutputList(
"STRING_VALUE");
260 fillQuery->addToOrderList(
"TIME");
262 coral::ICursor& fillCursor = fillQuery->execute();
264 while (fillCursor.next()) {
265 const coral::AttributeList& row = fillCursor.currentRow();
275 coral::IQuery* startTQuery = runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
276 coral::AttributeList startTVariableList;
277 startTVariableList.extend(
"runnumber",
typeid(
unsigned int));
278 startTVariableList.extend(
"name",
typeid(
std::string));
280 startTVariableList[
"runnumber"].data<
unsigned int>() = runnumber;
282 startTQuery->setCondition(
"RUNNUMBER =:runnumber AND NAME =:name", startTVariableList);
283 startTQuery->addToOutputList(
"TIME");
284 coral::ICursor& startTCursor = startTQuery->execute();
286 while (startTCursor.next()) {
287 const coral::AttributeList& row = startTCursor.currentRow();
288 result.
startT = row[
"TIME"].data<coral::TimeStamp>();
291 coral::IQuery* stopTQuery = runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
292 coral::AttributeList stopTVariableList;
293 stopTVariableList.extend(
"runnumber",
typeid(
unsigned int));
294 stopTVariableList.extend(
"name",
typeid(
std::string));
296 stopTVariableList[
"runnumber"].data<
unsigned int>() = runnumber;
298 stopTQuery->setCondition(
"RUNNUMBER =:runnumber AND NAME =:name", stopTVariableList);
299 stopTQuery->addToOutputList(
"TIME");
300 coral::ICursor& stopTCursor = stopTQuery->execute();
302 while (stopTCursor.next()) {
303 const coral::AttributeList& row = stopTCursor.currentRow();
304 result.
stopT = row[
"TIME"].data<coral::TimeStamp>();
307 coral::IQuery* l1keyQuery = runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
308 coral::AttributeList l1keyOutput;
310 coral::AttributeList l1keyCondition;
311 l1keyCondition = coral::AttributeList();
312 l1keyCondition.extend(
"name",
typeid(
std::string));
313 l1keyCondition.extend(
"runnumber",
typeid(
unsigned int));
315 l1keyCondition[
"runnumber"].data<
unsigned int>() = runnumber;
316 l1keyQuery->addToOutputList(
"STRING_VALUE");
317 l1keyQuery->setCondition(
"NAME=:name AND RUNNUMBER=:runnumber", l1keyCondition);
319 l1keyQuery->defineOutput(l1keyOutput);
320 coral::ICursor& l1keyCursor = l1keyQuery->execute();
321 while (l1keyCursor.next()) {
322 const coral::AttributeList& row = l1keyCursor.currentRow();
327 runinfosession->transaction().rollback();
328 delete runinfosession;
332 runinfosession->transaction().commit();
333 delete runinfosession;
335 if (!csvsource.empty()) {
341 std::cout <<
"result for run " << runnumber <<
" : sequence : " << result.
sequence 342 <<
" : hltkey : " << result.
hltkey <<
" : fillnumber : " << result.
fillnumber 343 <<
" : l1key : " << result.
l1key <<
" : amodetag :" << result.
amodetag <<
" : egev : " << result.
egev 348 coral::ISessionProxy* destsession = svc->connect(
m_dest, coral::Update);
350 coral::ITypeConverter& desttpc = destsession->typeConverter();
351 desttpc.setCppTypeForSqlType(
"unsigned int",
"NUMBER(10)");
353 destsession->transaction().start(
false);
354 coral::ISchema& destschema = destsession->nominalSchema();
356 coral::AttributeList runData;
357 destruntable.dataEditor().rowBuffer(runData);
358 runData[
"RUNNUM"].data<
unsigned int>() = runnumber;
362 runData[
"STARTTIME"].data<coral::TimeStamp>() = result.
startT;
363 runData[
"STOPTIME"].data<coral::TimeStamp>() = result.
stopT;
365 runData[
"EGEV"].data<
unsigned int>() = (
unsigned int)result.
egev;
369 destruntable.dataEditor().insertRow(runData);
371 std::cout <<
"database problem " << er.what() << std::endl;
372 destsession->transaction().rollback();
377 destsession->transaction().commit();
384 std::istringstream myStream(s);
unsigned int str2int(const std::string &s) const
CMSRunSummary2DB(const std::string &dest)
void parseFillCSV(const std::string &csvsource, cmsrunsum &result)
unsigned long long retrieveData(unsigned int runnumber) override
const std::string dataType() const override
void setAuthentication(const std::string &authPath)
bool isCollisionRun(const lumi::CMSRunSummary2DB::cmsrunsum &rundata)
const std::string sourceType() const override
~CMSRunSummary2DB() override
#define DEFINE_EDM_PLUGIN(factory, type, name)
static const std::string cmsrunsummaryTableName()