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)
continue;
99 boost::match_results<std::string::const_iterator> what;
100 const boost::regex lexpr(
"^TSC_.+_collisions_.+");
101 boost::regex_match(lk,what,lexpr,boost::match_default);
102 if(what[0].
matched) isCollision=
true;
103 const boost::regex hexpr(
"^/cdaq/physics/.+");
104 boost::regex_match(hk,what,hexpr,boost::match_default);
105 if(what[0].matched) isPhysics=
true;
106 return (isCollision&&isPhysics);
123 std::string runsessionParamTable(
"RUNSESSION_PARAMETER");
124 coral::ConnectionService*
svc=
new coral::ConnectionService;
134 if(cutpos!=std::string::npos){
136 csvsource=
m_source.substr(cutpos+1);
139 coral::ISessionProxy* runinfosession=svc->connect(dbsource,coral::ReadOnly);
141 coral::ITypeConverter& tpc=runinfosession->typeConverter();
142 tpc.setCppTypeForSqlType(
"unsigned int",
"NUMBER(38)");
143 runinfosession->transaction().start(
true);
144 coral::ISchema& runinfoschemaHandle=runinfosession->schema(runinfoschema);
145 if(!runinfoschemaHandle.existsTable(runsessionParamTable)){
148 coral::IQuery* amodetagQuery=runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
149 coral::AttributeList amodetagOutput;
150 amodetagOutput.extend(
"amodetag",
typeid(
std::string));
151 coral::AttributeList amodetagCondition;
152 amodetagCondition=coral::AttributeList();
153 amodetagCondition.extend(
"name",
typeid(
std::string));
154 amodetagCondition.extend(
"runnumber",
typeid(
unsigned int));
156 amodetagCondition[
"runnumber"].data<
unsigned int>()=runnumber;
157 amodetagQuery->addToOutputList(
"distinct(STRING_VALUE)");
158 amodetagQuery->setCondition(
"NAME=:name AND RUNNUMBER=:runnumber",amodetagCondition);
160 amodetagQuery->defineOutput(amodetagOutput);
161 coral::ICursor& amodetagCursor=amodetagQuery->execute();
162 std::vector<std::string> amodes;
163 while (amodetagCursor.next()){
164 const coral::AttributeList& row=amodetagCursor.currentRow();
165 amodes.push_back(row[
"amodetag"].data<std::string>());
172 for(std::vector<std::string>::iterator it=amodes.begin();it!=amodes.end();++it){
173 if(it->find(
"PHYS")==std::string::npos)
continue;
176 if(amd.size()==0&&amodes.size()!=0){
177 amd=*(amodes.begin());
184 delete amodetagQuery;
186 coral::IQuery* egevQuery=runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
187 coral::AttributeList egevOutput;
189 coral::AttributeList egevCondition;
190 egevCondition=coral::AttributeList();
192 egevCondition.extend(
"runnumber",
typeid(
unsigned int));
194 egevCondition[
"runnumber"].data<
unsigned int>()=runnumber;
195 egevQuery->addToOutputList(
"distinct(STRING_VALUE)");
196 egevQuery->setCondition(
"NAME=:name AND RUNNUMBER=:runnumber",egevCondition);
197 egevQuery->defineOutput(egevOutput);
198 coral::ICursor& egevCursor=egevQuery->execute();
200 while (egevCursor.next()){
201 const coral::AttributeList& row=egevCursor.currentRow();
204 if(tmpgev>result.
egev){
213 coral::IQuery* seqQuery=runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
214 coral::AttributeList seqBindVariableList;
215 seqBindVariableList.extend(
"runnumber",
typeid(
unsigned int));
216 seqBindVariableList.extend(
"name",
typeid(
std::string));
218 seqBindVariableList[
"runnumber"].data<
unsigned int>()=runnumber;
220 seqQuery->setCondition(
"RUNNUMBER =:runnumber AND NAME =:name",seqBindVariableList);
221 seqQuery->addToOutputList(
"STRING_VALUE");
222 coral::ICursor& seqCursor=seqQuery->execute();
224 while( seqCursor.next() ){
225 const coral::AttributeList& row=seqCursor.currentRow();
230 coral::IQuery* hltkeyQuery=runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
231 coral::AttributeList hltkeyBindVariableList;
232 hltkeyBindVariableList.extend(
"runnumber",
typeid(
unsigned int));
233 hltkeyBindVariableList.extend(
"name",
typeid(
std::string));
235 hltkeyBindVariableList[
"runnumber"].data<
unsigned int>()=runnumber;
237 hltkeyQuery->setCondition(
"RUNNUMBER =:runnumber AND NAME =:name",hltkeyBindVariableList);
238 hltkeyQuery->addToOutputList(
"STRING_VALUE");
239 coral::ICursor& hltkeyCursor=hltkeyQuery->execute();
241 while( hltkeyCursor.next() ){
242 const coral::AttributeList& row=hltkeyCursor.currentRow();
247 coral::IQuery* fillQuery=runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
248 coral::AttributeList fillBindVariableList;
249 fillBindVariableList.extend(
"runnumber",
typeid(
unsigned int));
250 fillBindVariableList.extend(
"name",
typeid(
std::string));
252 fillBindVariableList[
"runnumber"].data<
unsigned int>()=runnumber;
254 fillQuery->setCondition(
"RUNNUMBER =:runnumber AND NAME =:name",fillBindVariableList);
255 fillQuery->addToOutputList(
"STRING_VALUE");
256 fillQuery->addToOrderList(
"TIME");
258 coral::ICursor& fillCursor=fillQuery->execute();
260 while( fillCursor.next() ){
261 const coral::AttributeList& row=fillCursor.currentRow();
271 coral::IQuery* startTQuery=runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
272 coral::AttributeList startTVariableList;
273 startTVariableList.extend(
"runnumber",
typeid(
unsigned int));
274 startTVariableList.extend(
"name",
typeid(
std::string));
276 startTVariableList[
"runnumber"].data<
unsigned int>()=runnumber;
278 startTQuery->setCondition(
"RUNNUMBER =:runnumber AND NAME =:name",startTVariableList);
279 startTQuery->addToOutputList(
"TIME");
280 coral::ICursor& startTCursor=startTQuery->execute();
282 while( startTCursor.next() ){
283 const coral::AttributeList& row=startTCursor.currentRow();
284 result.
startT=row[
"TIME"].data<coral::TimeStamp>();
287 coral::IQuery* stopTQuery=runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
288 coral::AttributeList stopTVariableList;
289 stopTVariableList.extend(
"runnumber",
typeid(
unsigned int));
290 stopTVariableList.extend(
"name",
typeid(
std::string));
292 stopTVariableList[
"runnumber"].data<
unsigned int>()=runnumber;
294 stopTQuery->setCondition(
"RUNNUMBER =:runnumber AND NAME =:name",stopTVariableList);
295 stopTQuery->addToOutputList(
"TIME");
296 coral::ICursor& stopTCursor=stopTQuery->execute();
298 while( stopTCursor.next() ){
299 const coral::AttributeList& row=stopTCursor.currentRow();
300 result.
stopT=row[
"TIME"].data<coral::TimeStamp>();
303 coral::IQuery* l1keyQuery=runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
304 coral::AttributeList l1keyOutput;
306 coral::AttributeList l1keyCondition;
307 l1keyCondition=coral::AttributeList();
309 l1keyCondition.extend(
"runnumber",
typeid(
unsigned int));
311 l1keyCondition[
"runnumber"].data<
unsigned int>()=runnumber;
312 l1keyQuery->addToOutputList(
"STRING_VALUE");
313 l1keyQuery->setCondition(
"NAME=:name AND RUNNUMBER=:runnumber",l1keyCondition);
315 l1keyQuery->defineOutput(l1keyOutput);
316 coral::ICursor& l1keyCursor=l1keyQuery->execute();
317 while (l1keyCursor.next()){
318 const coral::AttributeList& row=l1keyCursor.currentRow();
323 runinfosession->transaction().rollback();
324 delete runinfosession;
328 runinfosession->transaction().commit();
329 delete runinfosession;
331 if(csvsource.size()!=0){
340 coral::ISessionProxy* destsession=svc->connect(
m_dest,coral::Update);
342 coral::ITypeConverter& desttpc=destsession->typeConverter();
343 desttpc.setCppTypeForSqlType(
"unsigned int",
"NUMBER(10)");
345 destsession->transaction().start(
false);
346 coral::ISchema& destschema=destsession->nominalSchema();
348 coral::AttributeList runData;
349 destruntable.dataEditor().rowBuffer(runData);
350 runData[
"RUNNUM"].data<
unsigned int>()=runnumber;
354 runData[
"STARTTIME"].data<coral::TimeStamp>()=result.
startT;
355 runData[
"STOPTIME"].data<coral::TimeStamp>()=result.
stopT;
357 runData[
"EGEV"].data<
unsigned int>()=(
unsigned int)result.
egev;
361 destruntable.dataEditor().insertRow(runData);
363 std::cout<<
"database problem "<<er.what()<<std::endl;
364 destsession->transaction().rollback();
369 destsession->transaction().commit();
374 return "CMSRUNSUMMARY";
380 std::istringstream myStream(s);
unsigned int str2int(const std::string &s) const
virtual ~CMSRunSummary2DB()
CMSRunSummary2DB(const std::string &dest)
void parseFillCSV(const std::string &csvsource, cmsrunsum &result)
virtual unsigned long long retrieveData(unsigned int runnumber) override
virtual const std::string dataType() const override
void setAuthentication(const std::string &authPath)
bool isCollisionRun(const lumi::CMSRunSummary2DB::cmsrunsum &rundata)
virtual const std::string sourceType() const override
#define DEFINE_EDM_PLUGIN(factory, type, name)
static const std::string cmsrunsummaryTableName()