CMS 3D CMS Logo

CMSRunSummary2DB.cc
Go to the documentation of this file.
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"
31 #include <iostream>
32 #include <sstream>
33 #include <fstream>
34 #include <vector>
35 #include <string>
36 #include <boost/regex.hpp>
37 #include <boost/tokenizer.hpp>
38 
39 namespace lumi{
40  class CMSRunSummary2DB : public DataPipe{
41  public:
43  virtual unsigned long long retrieveData( unsigned int runnumber ) override;
44  virtual const std::string dataType() const override;
45  virtual const std::string sourceType() const override;
46  unsigned int str2int(const std::string& s) const;
47  virtual ~CMSRunSummary2DB();
48  private:
49  struct cmsrunsum{
52  int egev;
54  std::string fillnumber; //convert to number when write into lumi
58  coral::TimeStamp startT;
59  coral::TimeStamp stopT;
60  };
62  void parseFillCSV(const std::string& csvsource, cmsrunsum& result);
63  };//cl CMSRunSummary2DB
64  //
65  //implementation
66  //
67  void
69  result.fillscheme=std::string("");
70  result.ncollidingbunches=0;
71  std::ifstream csvfile;
72  csvfile.open(csvsource.c_str());
73  if(!csvfile){
74  std::cout<<"[warning] unable to open file: "<<csvsource<<std::endl;
75  return;
76  }
77  typedef boost::tokenizer< boost::escaped_list_separator<char> > Tokenizer;
78  std::vector<std::string> record;
80  while(std::getline(csvfile,line)){
81  Tokenizer tok(line);
82  record.assign(tok.begin(),tok.end());
83  if(record.size()<3) continue;
84  std::string fillnum=record[0];
85  if(fillnum==result.fillnumber){
86  result.fillscheme=record[1];
87  std::string ncollidingbunchesStr=record[2];
88  result.ncollidingbunches=str2int(ncollidingbunchesStr);
89  break;
90  }
91  }
92  }
95  bool isCollision=false;
96  bool isPhysics=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);
107  }
108  unsigned long long
122  std::string runinfoschema("CMS_RUNINFO");
123  std::string runsessionParamTable("RUNSESSION_PARAMETER");
124  coral::ConnectionService* svc=new coral::ConnectionService;
125  lumi::DBConfig dbconf(*svc);
126  if(!m_authpath.empty()){
128  }
129 
130  //std::cout<<"m_source "<<m_source<<std::endl;
131  std::string::size_type cutpos=m_source.find(';');
132  std::string dbsource=m_source;
133  std::string csvsource("");
134  if(cutpos!=std::string::npos){
135  dbsource=m_source.substr(0,cutpos);
136  csvsource=m_source.substr(cutpos+1);
137  }
138  //std::cout<<"dbsource: "<<dbsource<<" , csvsource: "<<csvsource<<std::endl;
139  coral::ISessionProxy* runinfosession=svc->connect(dbsource,coral::ReadOnly);
140  try{
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)){
146  throw lumi::Exception(std::string("non-existing table "+runsessionParamTable),"CMSRunSummary2DB","retrieveData");
147  }
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));
155  amodetagCondition["name"].data<std::string>()=std::string("CMS.SCAL:AMODEtag");
156  amodetagCondition["runnumber"].data<unsigned int>()=runnumber;
157  amodetagQuery->addToOutputList("distinct(STRING_VALUE)");
158  amodetagQuery->setCondition("NAME=:name AND RUNNUMBER=:runnumber",amodetagCondition);
159  //amodetagQuery->limitReturnedRows(1);
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>());
166  //result.amodetag=row["amodetag"].data<std::string>();
167  }
168  //
169  //priority pick the one contains PHYS if not found pick the first
170  //
171  std::string amd;
172  for(std::vector<std::string>::iterator it=amodes.begin();it!=amodes.end();++it){
173  if(it->find("PHYS")==std::string::npos) continue;
174  amd=*it;
175  }
176  if(amd.size()==0&&amodes.size()!=0){
177  amd=*(amodes.begin());
178  }
179  if(amd.size()==0){
180  amd=std::string("PROTPHYS");//last resort
181  }
182  //std::cout<<"amd "<<amd<<std::endl;
183  result.amodetag=amd;
184  delete amodetagQuery;
185 
186  coral::IQuery* egevQuery=runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
187  coral::AttributeList egevOutput;
188  egevOutput.extend("egev",typeid(std::string));
189  coral::AttributeList egevCondition;
190  egevCondition=coral::AttributeList();
191  egevCondition.extend("name",typeid(std::string));
192  egevCondition.extend("runnumber",typeid(unsigned int));
193  egevCondition["name"].data<std::string>()=std::string("CMS.SCAL:EGEV");
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();
199  result.egev=0;
200  while (egevCursor.next()){
201  const coral::AttributeList& row=egevCursor.currentRow();
202  std::string egevstr=row["egev"].data<std::string>();
203  int tmpgev=str2int(egevstr);
204  if(tmpgev>result.egev){
205  result.egev=tmpgev;
206  }
207  }
208  if(result.egev==0){
209  result.egev=3500;//last resort
210  }
211  delete egevQuery;
212 
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));
217 
218  seqBindVariableList["runnumber"].data<unsigned int>()=runnumber;
219  seqBindVariableList["name"].data<std::string>()=std::string("CMS.LVL0:SEQ_NAME");
220  seqQuery->setCondition("RUNNUMBER =:runnumber AND NAME =:name",seqBindVariableList);
221  seqQuery->addToOutputList("STRING_VALUE");
222  coral::ICursor& seqCursor=seqQuery->execute();
223 
224  while( seqCursor.next() ){
225  const coral::AttributeList& row=seqCursor.currentRow();
226  result.sequence=row["STRING_VALUE"].data<std::string>();
227  }
228  delete seqQuery;
229 
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));
234 
235  hltkeyBindVariableList["runnumber"].data<unsigned int>()=runnumber;
236  hltkeyBindVariableList["name"].data<std::string>()=std::string("CMS.LVL0:HLT_KEY_DESCRIPTION");
237  hltkeyQuery->setCondition("RUNNUMBER =:runnumber AND NAME =:name",hltkeyBindVariableList);
238  hltkeyQuery->addToOutputList("STRING_VALUE");
239  coral::ICursor& hltkeyCursor=hltkeyQuery->execute();
240 
241  while( hltkeyCursor.next() ){
242  const coral::AttributeList& row=hltkeyCursor.currentRow();
243  result.hltkey=row["STRING_VALUE"].data<std::string>();
244  }
245  delete hltkeyQuery;
246 
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));
251 
252  fillBindVariableList["runnumber"].data<unsigned int>()=runnumber;
253  fillBindVariableList["name"].data<std::string>()=std::string("CMS.SCAL:FILLN");
254  fillQuery->setCondition("RUNNUMBER =:runnumber AND NAME =:name",fillBindVariableList);
255  fillQuery->addToOutputList("STRING_VALUE");
256  fillQuery->addToOrderList("TIME");
257  //fillQuery->limitReturnedRows(1);
258  coral::ICursor& fillCursor=fillQuery->execute();
259  unsigned int cc=0;
260  while( fillCursor.next() ){
261  const coral::AttributeList& row=fillCursor.currentRow();
262  if (cc==0){
263  result.fillnumber=row["STRING_VALUE"].data<std::string>();
264  }
265  ++cc;
266  }
267  delete fillQuery;
268  if (result.fillnumber.empty()){
269  throw nonCollisionException("retrieveData","CMSRunSummary2DB");
270  }
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));
275 
276  startTVariableList["runnumber"].data<unsigned int>()=runnumber;
277  startTVariableList["name"].data<std::string>()=std::string("CMS.LVL0:START_TIME_T");
278  startTQuery->setCondition("RUNNUMBER =:runnumber AND NAME =:name",startTVariableList);
279  startTQuery->addToOutputList("TIME");
280  coral::ICursor& startTCursor=startTQuery->execute();
281 
282  while( startTCursor.next() ){
283  const coral::AttributeList& row=startTCursor.currentRow();
284  result.startT=row["TIME"].data<coral::TimeStamp>();
285  }
286  delete startTQuery;
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));
291 
292  stopTVariableList["runnumber"].data<unsigned int>()=runnumber;
293  stopTVariableList["name"].data<std::string>()=std::string("CMS.LVL0:STOP_TIME_T");
294  stopTQuery->setCondition("RUNNUMBER =:runnumber AND NAME =:name",stopTVariableList);
295  stopTQuery->addToOutputList("TIME");
296  coral::ICursor& stopTCursor=stopTQuery->execute();
297 
298  while( stopTCursor.next() ){
299  const coral::AttributeList& row=stopTCursor.currentRow();
300  result.stopT=row["TIME"].data<coral::TimeStamp>();
301  }
302  delete stopTQuery;
303  coral::IQuery* l1keyQuery=runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
304  coral::AttributeList l1keyOutput;
305  l1keyOutput.extend("l1key",typeid(std::string));
306  coral::AttributeList l1keyCondition;
307  l1keyCondition=coral::AttributeList();
308  l1keyCondition.extend("name",typeid(std::string));
309  l1keyCondition.extend("runnumber",typeid(unsigned int));
310  l1keyCondition["name"].data<std::string>()=std::string("CMS.TRG:TSC_KEY");
311  l1keyCondition["runnumber"].data<unsigned int>()=runnumber;
312  l1keyQuery->addToOutputList("STRING_VALUE");
313  l1keyQuery->setCondition("NAME=:name AND RUNNUMBER=:runnumber",l1keyCondition);
314  //l1keyQuery->limitReturnedRows(1);
315  l1keyQuery->defineOutput(l1keyOutput);
316  coral::ICursor& l1keyCursor=l1keyQuery->execute();
317  while (l1keyCursor.next()){
318  const coral::AttributeList& row=l1keyCursor.currentRow();
319  result.l1key=row["l1key"].data<std::string>();
320  }
321  delete l1keyQuery;
322  }catch( const coral::Exception& er){
323  runinfosession->transaction().rollback();
324  delete runinfosession;
325  delete svc;
326  throw er;
327  }
328  runinfosession->transaction().commit();
329  delete runinfosession;
330 
331  if(csvsource.size()!=0){
332  parseFillCSV(csvsource,result);
333  }else{
334  result.fillscheme=std::string("");
335  result.ncollidingbunches=0;
336  }
337  std::cout<<"result for run "<<runnumber<<" : sequence : "<<result.sequence<<" : hltkey : "<<result.hltkey<<" : fillnumber : "<<result.fillnumber<<" : l1key : "<<result.l1key<<" : amodetag :"<<result.amodetag<<" : egev : "<<result.egev<<" : fillscheme "<<result.fillscheme<<" : ncollidingbunches : "<<result.ncollidingbunches<<std::endl;
338 
339  //std::cout<<"connecting to dest "<<m_dest<<std::endl;
340  coral::ISessionProxy* destsession=svc->connect(m_dest,coral::Update);
341 
342  coral::ITypeConverter& desttpc=destsession->typeConverter();
343  desttpc.setCppTypeForSqlType("unsigned int","NUMBER(10)");
344  try{
345  destsession->transaction().start(false);
346  coral::ISchema& destschema=destsession->nominalSchema();
347  coral::ITable& destruntable=destschema.tableHandle(LumiNames::cmsrunsummaryTableName());
348  coral::AttributeList runData;
349  destruntable.dataEditor().rowBuffer(runData);
350  runData["RUNNUM"].data<unsigned int>()=runnumber;
351  runData["FILLNUM"].data<unsigned int>()=str2int(result.fillnumber);
352  runData["SEQUENCE"].data<std::string>()=result.sequence;
353  runData["HLTKEY"].data<std::string>()=result.hltkey;
354  runData["STARTTIME"].data<coral::TimeStamp>()=result.startT;
355  runData["STOPTIME"].data<coral::TimeStamp>()=result.stopT;
356  runData["AMODETAG"].data<std::string>()=result.amodetag;
357  runData["EGEV"].data<unsigned int>()=(unsigned int)result.egev;
358  runData["L1KEY"].data<std::string>()=result.l1key;
359  runData["FILLSCHEME"].data<std::string>()=result.fillscheme;
360  runData["NCOLLIDINGBUNCHES"].data<unsigned int>()=result.ncollidingbunches;
361  destruntable.dataEditor().insertRow(runData);
362  }catch( const coral::Exception& er){
363  std::cout<<"database problem "<<er.what()<<std::endl;
364  destsession->transaction().rollback();
365  delete destsession;
366  delete svc;
367  throw er;
368  }
369  destsession->transaction().commit();
370  delete svc;
371  return 0;
372  }
374  return "CMSRUNSUMMARY";
375  }
377  return "DB";
378  }
379  unsigned int CMSRunSummary2DB::str2int(const std::string& s) const{
380  std::istringstream myStream(s);
381  unsigned int i;
382  if(myStream>>i){
383  return i;
384  }else{
385  throw lumi::Exception(std::string("str2int error"),"str2int","CMSRunSummary2DB");
386  }
387  }
389 }//ns lumi
392 #endif
unsigned int str2int(const std::string &s) const
JetCorrectorParameters::Record record
Definition: classes.h:7
CMSRunSummary2DB(const std::string &dest)
uint16_t size_type
void parseFillCSV(const std::string &csvsource, cmsrunsum &result)
std::string m_dest
Definition: DataPipe.h:27
virtual unsigned long long retrieveData(unsigned int runnumber) override
virtual const std::string dataType() const override
void setAuthentication(const std::string &authPath)
Definition: DBConfig.cc:15
bool isCollisionRun(const lumi::CMSRunSummary2DB::cmsrunsum &rundata)
std::string m_source
Definition: DataPipe.h:28
virtual const std::string sourceType() const override
std::string m_authpath
Definition: DataPipe.h:29
#define DEFINE_EDM_PLUGIN(factory, type, name)
static const std::string cmsrunsummaryTableName()
Definition: LumiNames.cc:2