CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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:
42  CMSRunSummary2DB( const std::string& dest);
43  virtual void retrieveData( unsigned int runnumber );
44  virtual const std::string dataType() const;
45  virtual const std::string sourceType() const;
46  unsigned int str2int(const std::string& s) const;
47  virtual ~CMSRunSummary2DB();
48  private:
49  struct cmsrunsum{
50  std::string l1key;
51  std::string amodetag;
52  int egev;
53  std::string hltkey;
54  std::string fillnumber; //convert to number when write into lumi
55  std::string sequence;
56  std::string fillscheme;
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;
79  std::string line;
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  }
93  CMSRunSummary2DB::CMSRunSummary2DB(const std::string& dest):DataPipe(dest){}
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  }
121  std::string runinfoschema("CMS_RUNINFO");
122  std::string runsessionParamTable("RUNSESSION_PARAMETER");
123  coral::ConnectionService* svc=new coral::ConnectionService;
124  lumi::DBConfig dbconf(*svc);
125  if(!m_authpath.empty()){
127  }
128 
129  //std::cout<<"m_source "<<m_source<<std::endl;
130  std::string::size_type cutpos=m_source.find(';');
131  std::string dbsource=m_source;
132  std::string csvsource("");
133  if(cutpos!=std::string::npos){
134  dbsource=m_source.substr(0,cutpos);
135  csvsource=m_source.substr(cutpos+1);
136  }
137  //std::cout<<"dbsource: "<<dbsource<<" , csvsource: "<<csvsource<<std::endl;
138  coral::ISessionProxy* runinfosession=svc->connect(dbsource,coral::ReadOnly);
139  try{
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");
146  }
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);
158  //amodetagQuery->limitReturnedRows(1);
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>());
165  //result.amodetag=row["amodetag"].data<std::string>();
166  }
167  //
168  //priority pick the one contains PHYS if not found pick the first
169  //
170  std::string amd;
171  for(std::vector<std::string>::iterator it=amodes.begin();it!=amodes.end();++it){
172  if(it->find("PHYS")==std::string::npos) continue;
173  amd=*it;
174  }
175  if(amd.size()==0&&amodes.size()!=0){
176  amd=*(amodes.begin());
177  }
178  if(amd.size()==0){
179  amd=std::string("PROTPHYS");//last resort
180  }
181  //std::cout<<"amd "<<amd<<std::endl;
182  result.amodetag=amd;
183  delete amodetagQuery;
184 
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();
198  result.egev=0;
199  while (egevCursor.next()){
200  const coral::AttributeList& row=egevCursor.currentRow();
201  std::string egevstr=row["egev"].data<std::string>();
202  int tmpgev=str2int(egevstr);
203  if(tmpgev>result.egev){
204  result.egev=tmpgev;
205  }
206  }
207  if(result.egev==0){
208  result.egev=3500;//last resort
209  }
210  delete egevQuery;
211 
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));
216 
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();
222 
223  while( seqCursor.next() ){
224  const coral::AttributeList& row=seqCursor.currentRow();
225  result.sequence=row["STRING_VALUE"].data<std::string>();
226  }
227  delete seqQuery;
228 
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));
233 
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();
239 
240  while( hltkeyCursor.next() ){
241  const coral::AttributeList& row=hltkeyCursor.currentRow();
242  result.hltkey=row["STRING_VALUE"].data<std::string>();
243  }
244  delete hltkeyQuery;
245 
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));
250 
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");
256  //fillQuery->limitReturnedRows(1);
257  coral::ICursor& fillCursor=fillQuery->execute();
258  unsigned int cc=0;
259  while( fillCursor.next() ){
260  const coral::AttributeList& row=fillCursor.currentRow();
261  if (cc==0){
262  result.fillnumber=row["STRING_VALUE"].data<std::string>();
263  }
264  ++cc;
265  }
266  delete fillQuery;
267  if (result.fillnumber.empty()){
268  throw nonCollisionException("retrieveData","CMSRunSummary2DB");
269  }
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));
274 
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();
280 
281  while( startTCursor.next() ){
282  const coral::AttributeList& row=startTCursor.currentRow();
283  result.startT=row["TIME"].data<coral::TimeStamp>();
284  }
285  delete startTQuery;
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));
290 
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();
296 
297  while( stopTCursor.next() ){
298  const coral::AttributeList& row=stopTCursor.currentRow();
299  result.stopT=row["TIME"].data<coral::TimeStamp>();
300  }
301  delete stopTQuery;
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);
313  //l1keyQuery->limitReturnedRows(1);
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>();
319  }
320  delete l1keyQuery;
321  }catch( const coral::Exception& er){
322  runinfosession->transaction().rollback();
323  delete runinfosession;
324  delete svc;
325  throw er;
326  }
327  runinfosession->transaction().commit();
328  delete runinfosession;
329 
330  if(csvsource.size()!=0){
331  parseFillCSV(csvsource,result);
332  }else{
333  result.fillscheme=std::string("");
334  result.ncollidingbunches=0;
335  }
336  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;
337 
338  //std::cout<<"connecting to dest "<<m_dest<<std::endl;
339  coral::ISessionProxy* destsession=svc->connect(m_dest,coral::Update);
340 
341  coral::ITypeConverter& desttpc=destsession->typeConverter();
342  desttpc.setCppTypeForSqlType("unsigned int","NUMBER(10)");
343  try{
344  destsession->transaction().start(false);
345  coral::ISchema& destschema=destsession->nominalSchema();
346  coral::ITable& destruntable=destschema.tableHandle(LumiNames::cmsrunsummaryTableName());
347  coral::AttributeList runData;
348  destruntable.dataEditor().rowBuffer(runData);
349  runData["RUNNUM"].data<unsigned int>()=runnumber;
350  runData["FILLNUM"].data<unsigned int>()=str2int(result.fillnumber);
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;
359  runData["NCOLLIDINGBUNCHES"].data<unsigned int>()=result.ncollidingbunches;
360  destruntable.dataEditor().insertRow(runData);
361  }catch( const coral::Exception& er){
362  std::cout<<"database problem "<<er.what()<<std::endl;
363  destsession->transaction().rollback();
364  delete destsession;
365  delete svc;
366  throw er;
367  }
368  destsession->transaction().commit();
369  delete svc;
370  }
371  const std::string CMSRunSummary2DB::dataType() const{
372  return "CMSRUNSUMMARY";
373  }
374  const std::string CMSRunSummary2DB::sourceType() const{
375  return "DB";
376  }
377  unsigned int CMSRunSummary2DB::str2int(const std::string& s) const{
378  std::istringstream myStream(s);
379  unsigned int i;
380  if(myStream>>i){
381  return i;
382  }else{
383  throw lumi::Exception(std::string("str2int error"),"str2int","CMSRunSummary2DB");
384  }
385  }
387 }//ns lumi
390 #endif
int i
Definition: DBlmapReader.cc:9
virtual const std::string sourceType() const
unsigned int str2int(const std::string &s) const
JetCorrectorParameters::Record record
Definition: classes.h:11
tuple lumi
Definition: fjr2json.py:35
virtual const std::string dataType() const
virtual void retrieveData(unsigned int runnumber)
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
list rundata
Definition: lumiPlot.py:399
tuple result
Definition: query.py:137
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
std::string m_authpath
Definition: DataPipe.h:29
tuple cout
Definition: gather_cfg.py:121
#define DEFINE_EDM_PLUGIN(factory, type, name)
static const std::string cmsrunsummaryTableName()
Definition: LumiNames.cc:2
tuple destsession
Definition: DBCopy.py:270