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>
44 virtual const std::string
dataType()
const;
46 unsigned int str2int(
const std::string&
s)
const;
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;
84 std::string fillnum=record[0];
87 std::string ncollidingbunchesStr=record[2];
95 bool isCollision=
false;
97 std::string hk=rundata.
hltkey;
98 std::string lk=rundata.
l1key;
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);
121 std::string runinfoschema(
"CMS_RUNINFO");
122 std::string runsessionParamTable(
"RUNSESSION_PARAMETER");
123 coral::ConnectionService*
svc=
new coral::ConnectionService;
132 std::string csvsource(
"");
133 if(cutpos!=std::string::npos){
135 csvsource=
m_source.substr(cutpos+1);
138 coral::ISessionProxy* runinfosession=svc->connect(dbsource,coral::ReadOnly);
140 coral::ITypeConverter& tpc=runinfosession->typeConverter();
141 tpc.setCppTypeForSqlType(
"unsigned int",
"NUMBER(38)");
142 runinfosession->transaction().start(
true);
143 coral::ISchema& runinfoschemaHandle=runinfosession->schema(runinfoschema);
144 if(!runinfoschemaHandle.existsTable(runsessionParamTable)){
145 throw lumi::Exception(std::string(
"non-existing table "+runsessionParamTable),
"CMSRunSummary2DB",
"retrieveData");
147 coral::IQuery* amodetagQuery=runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
148 coral::AttributeList amodetagOutput;
149 amodetagOutput.extend(
"amodetag",
typeid(std::string));
150 coral::AttributeList amodetagCondition;
151 amodetagCondition=coral::AttributeList();
152 amodetagCondition.extend(
"name",
typeid(std::string));
153 amodetagCondition.extend(
"runnumber",
typeid(
unsigned int));
154 amodetagCondition[
"name"].data<std::string>()=std::string(
"CMS.SCAL:AMODEtag");
155 amodetagCondition[
"runnumber"].data<
unsigned int>()=runnumber;
156 amodetagQuery->addToOutputList(
"distinct(STRING_VALUE)");
157 amodetagQuery->setCondition(
"NAME=:name AND RUNNUMBER=:runnumber",amodetagCondition);
159 amodetagQuery->defineOutput(amodetagOutput);
160 coral::ICursor& amodetagCursor=amodetagQuery->execute();
161 std::vector<std::string> amodes;
162 while (amodetagCursor.next()){
163 const coral::AttributeList& row=amodetagCursor.currentRow();
164 amodes.push_back(row[
"amodetag"].data<std::string>());
171 for(std::vector<std::string>::iterator it=amodes.begin();it!=amodes.end();++it){
172 if(it->find(
"PHYS")==std::string::npos)
continue;
175 if(amd.size()==0&&amodes.size()!=0){
176 amd=*(amodes.begin());
179 amd=std::string(
"PROTPHYS");
183 delete amodetagQuery;
185 coral::IQuery* egevQuery=runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
186 coral::AttributeList egevOutput;
187 egevOutput.extend(
"egev",
typeid(std::string));
188 coral::AttributeList egevCondition;
189 egevCondition=coral::AttributeList();
190 egevCondition.extend(
"name",
typeid(std::string));
191 egevCondition.extend(
"runnumber",
typeid(
unsigned int));
192 egevCondition[
"name"].data<std::string>()=std::string(
"CMS.SCAL:EGEV");
193 egevCondition[
"runnumber"].data<
unsigned int>()=runnumber;
194 egevQuery->addToOutputList(
"distinct(STRING_VALUE)");
195 egevQuery->setCondition(
"NAME=:name AND RUNNUMBER=:runnumber",egevCondition);
196 egevQuery->defineOutput(egevOutput);
197 coral::ICursor& egevCursor=egevQuery->execute();
199 while (egevCursor.next()){
200 const coral::AttributeList& row=egevCursor.currentRow();
201 std::string egevstr=row[
"egev"].data<std::string>();
203 if(tmpgev>result.
egev){
212 coral::IQuery* seqQuery=runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
213 coral::AttributeList seqBindVariableList;
214 seqBindVariableList.extend(
"runnumber",
typeid(
unsigned int));
215 seqBindVariableList.extend(
"name",
typeid(std::string));
217 seqBindVariableList[
"runnumber"].data<
unsigned int>()=runnumber;
218 seqBindVariableList[
"name"].data<std::string>()=std::string(
"CMS.LVL0:SEQ_NAME");
219 seqQuery->setCondition(
"RUNNUMBER =:runnumber AND NAME =:name",seqBindVariableList);
220 seqQuery->addToOutputList(
"STRING_VALUE");
221 coral::ICursor& seqCursor=seqQuery->execute();
223 while( seqCursor.next() ){
224 const coral::AttributeList& row=seqCursor.currentRow();
225 result.
sequence=row[
"STRING_VALUE"].data<std::string>();
229 coral::IQuery* hltkeyQuery=runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
230 coral::AttributeList hltkeyBindVariableList;
231 hltkeyBindVariableList.extend(
"runnumber",
typeid(
unsigned int));
232 hltkeyBindVariableList.extend(
"name",
typeid(std::string));
234 hltkeyBindVariableList[
"runnumber"].data<
unsigned int>()=runnumber;
235 hltkeyBindVariableList[
"name"].data<std::string>()=std::string(
"CMS.LVL0:HLT_KEY_DESCRIPTION");
236 hltkeyQuery->setCondition(
"RUNNUMBER =:runnumber AND NAME =:name",hltkeyBindVariableList);
237 hltkeyQuery->addToOutputList(
"STRING_VALUE");
238 coral::ICursor& hltkeyCursor=hltkeyQuery->execute();
240 while( hltkeyCursor.next() ){
241 const coral::AttributeList& row=hltkeyCursor.currentRow();
242 result.
hltkey=row[
"STRING_VALUE"].data<std::string>();
246 coral::IQuery* fillQuery=runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
247 coral::AttributeList fillBindVariableList;
248 fillBindVariableList.extend(
"runnumber",
typeid(
unsigned int));
249 fillBindVariableList.extend(
"name",
typeid(std::string));
251 fillBindVariableList[
"runnumber"].data<
unsigned int>()=runnumber;
252 fillBindVariableList[
"name"].data<std::string>()=std::string(
"CMS.SCAL:FILLN");
253 fillQuery->setCondition(
"RUNNUMBER =:runnumber AND NAME =:name",fillBindVariableList);
254 fillQuery->addToOutputList(
"STRING_VALUE");
255 fillQuery->addToOrderList(
"TIME");
257 coral::ICursor& fillCursor=fillQuery->execute();
259 while( fillCursor.next() ){
260 const coral::AttributeList& row=fillCursor.currentRow();
262 result.
fillnumber=row[
"STRING_VALUE"].data<std::string>();
270 coral::IQuery* startTQuery=runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
271 coral::AttributeList startTVariableList;
272 startTVariableList.extend(
"runnumber",
typeid(
unsigned int));
273 startTVariableList.extend(
"name",
typeid(std::string));
275 startTVariableList[
"runnumber"].data<
unsigned int>()=runnumber;
276 startTVariableList[
"name"].data<std::string>()=std::string(
"CMS.LVL0:START_TIME_T");
277 startTQuery->setCondition(
"RUNNUMBER =:runnumber AND NAME =:name",startTVariableList);
278 startTQuery->addToOutputList(
"TIME");
279 coral::ICursor& startTCursor=startTQuery->execute();
281 while( startTCursor.next() ){
282 const coral::AttributeList& row=startTCursor.currentRow();
283 result.
startT=row[
"TIME"].data<coral::TimeStamp>();
286 coral::IQuery* stopTQuery=runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
287 coral::AttributeList stopTVariableList;
288 stopTVariableList.extend(
"runnumber",
typeid(
unsigned int));
289 stopTVariableList.extend(
"name",
typeid(std::string));
291 stopTVariableList[
"runnumber"].data<
unsigned int>()=runnumber;
292 stopTVariableList[
"name"].data<std::string>()=std::string(
"CMS.LVL0:STOP_TIME_T");
293 stopTQuery->setCondition(
"RUNNUMBER =:runnumber AND NAME =:name",stopTVariableList);
294 stopTQuery->addToOutputList(
"TIME");
295 coral::ICursor& stopTCursor=stopTQuery->execute();
297 while( stopTCursor.next() ){
298 const coral::AttributeList& row=stopTCursor.currentRow();
299 result.
stopT=row[
"TIME"].data<coral::TimeStamp>();
302 coral::IQuery* l1keyQuery=runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
303 coral::AttributeList l1keyOutput;
304 l1keyOutput.extend(
"l1key",
typeid(std::string));
305 coral::AttributeList l1keyCondition;
306 l1keyCondition=coral::AttributeList();
307 l1keyCondition.extend(
"name",
typeid(std::string));
308 l1keyCondition.extend(
"runnumber",
typeid(
unsigned int));
309 l1keyCondition[
"name"].data<std::string>()=std::string(
"CMS.TRG:TSC_KEY");
310 l1keyCondition[
"runnumber"].data<
unsigned int>()=runnumber;
311 l1keyQuery->addToOutputList(
"STRING_VALUE");
312 l1keyQuery->setCondition(
"NAME=:name AND RUNNUMBER=:runnumber",l1keyCondition);
314 l1keyQuery->defineOutput(l1keyOutput);
315 coral::ICursor& l1keyCursor=l1keyQuery->execute();
316 while (l1keyCursor.next()){
317 const coral::AttributeList& row=l1keyCursor.currentRow();
318 result.
l1key=row[
"l1key"].data<std::string>();
322 runinfosession->transaction().rollback();
323 delete runinfosession;
327 runinfosession->transaction().commit();
328 delete runinfosession;
330 if(csvsource.size()!=0){
341 coral::ITypeConverter& desttpc=destsession->typeConverter();
342 desttpc.setCppTypeForSqlType(
"unsigned int",
"NUMBER(10)");
344 destsession->transaction().start(
false);
345 coral::ISchema& destschema=destsession->nominalSchema();
347 coral::AttributeList runData;
348 destruntable.dataEditor().rowBuffer(runData);
349 runData[
"RUNNUM"].data<
unsigned int>()=runnumber;
351 runData[
"SEQUENCE"].data<std::string>()=result.
sequence;
352 runData[
"HLTKEY"].data<std::string>()=result.
hltkey;
353 runData[
"STARTTIME"].data<coral::TimeStamp>()=result.
startT;
354 runData[
"STOPTIME"].data<coral::TimeStamp>()=result.
stopT;
355 runData[
"AMODETAG"].data<std::string>()=result.
amodetag;
356 runData[
"EGEV"].data<
unsigned int>()=(
unsigned int)result.
egev;
357 runData[
"L1KEY"].data<std::string>()=result.
l1key;
358 runData[
"FILLSCHEME"].data<std::string>()=result.
fillscheme;
360 destruntable.dataEditor().insertRow(runData);
362 std::cout<<
"database problem "<<er.what()<<std::endl;
363 destsession->transaction().rollback();
368 destsession->transaction().commit();
372 return "CMSRUNSUMMARY";
378 std::istringstream myStream(s);
383 throw lumi::Exception(std::string(
"str2int error"),
"str2int",
"CMSRunSummary2DB");
virtual const std::string sourceType() const
unsigned int str2int(const std::string &s) const
virtual ~CMSRunSummary2DB()
virtual const std::string dataType() const
virtual void retrieveData(unsigned int runnumber)
CMSRunSummary2DB(const std::string &dest)
void parseFillCSV(const std::string &csvsource, cmsrunsum &result)
void setAuthentication(const std::string &authPath)
bool isCollisionRun(const lumi::CMSRunSummary2DB::cmsrunsum &rundata)
#define DEFINE_EDM_PLUGIN(factory, type, name)
static const std::string cmsrunsummaryTableName()