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  unsigned long long retrieveData(unsigned int runnumber) override;
44  const std::string dataType() const override;
45  const std::string sourceType() const override;
46  unsigned int str2int(const std::string& s) const;
47  ~CMSRunSummary2DB() override;
48 
49  private:
50  struct cmsrunsum {
53  int egev;
55  std::string fillnumber; //convert to number when write into lumi
59  coral::TimeStamp startT;
60  coral::TimeStamp stopT;
61  };
63  void parseFillCSV(const std::string& csvsource, cmsrunsum& result);
64  }; //cl CMSRunSummary2DB
65  //
66  //implementation
67  //
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)
84  continue;
85  std::string fillnum = record[0];
86  if (fillnum == result.fillnumber) {
87  result.fillscheme = record[1];
88  std::string ncollidingbunchesStr = record[2];
89  result.ncollidingbunches = str2int(ncollidingbunchesStr);
90  break;
91  }
92  }
93  }
96  bool isCollision = false;
97  bool isPhysics = false;
98  std::string hk = rundata.hltkey;
99  std::string lk = rundata.l1key;
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);
103  if (what[0].matched)
104  isCollision = true;
105  const boost::regex hexpr("^/cdaq/physics/.+");
106  boost::regex_match(hk, what, hexpr, boost::match_default);
107  if (what[0].matched)
108  isPhysics = true;
109  return (isCollision && isPhysics);
110  }
111  unsigned long long CMSRunSummary2DB::retrieveData(unsigned int runnumber) {
124  std::string runinfoschema("CMS_RUNINFO");
125  std::string runsessionParamTable("RUNSESSION_PARAMETER");
126  coral::ConnectionService* svc = new coral::ConnectionService;
127  lumi::DBConfig dbconf(*svc);
128  if (!m_authpath.empty()) {
130  }
131 
132  //std::cout<<"m_source "<<m_source<<std::endl;
133  std::string::size_type cutpos = m_source.find(';');
134  std::string dbsource = m_source;
135  std::string csvsource("");
136  if (cutpos != std::string::npos) {
137  dbsource = m_source.substr(0, cutpos);
138  csvsource = m_source.substr(cutpos + 1);
139  }
140  //std::cout<<"dbsource: "<<dbsource<<" , csvsource: "<<csvsource<<std::endl;
141  coral::ISessionProxy* runinfosession = svc->connect(dbsource, coral::ReadOnly);
142  try {
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)) {
148  throw lumi::Exception(
149  std::string("non-existing table " + runsessionParamTable), "CMSRunSummary2DB", "retrieveData");
150  }
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));
158  amodetagCondition["name"].data<std::string>() = std::string("CMS.SCAL:AMODEtag");
159  amodetagCondition["runnumber"].data<unsigned int>() = runnumber;
160  amodetagQuery->addToOutputList("distinct(STRING_VALUE)");
161  amodetagQuery->setCondition("NAME=:name AND RUNNUMBER=:runnumber", amodetagCondition);
162  //amodetagQuery->limitReturnedRows(1);
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>());
169  //result.amodetag=row["amodetag"].data<std::string>();
170  }
171  //
172  //priority pick the one contains PHYS if not found pick the first
173  //
174  std::string amd;
175  for (std::vector<std::string>::iterator it = amodes.begin(); it != amodes.end(); ++it) {
176  if (it->find("PHYS") == std::string::npos)
177  continue;
178  amd = *it;
179  }
180  if (amd.empty() && !amodes.empty()) {
181  amd = *(amodes.begin());
182  }
183  if (amd.empty()) {
184  amd = std::string("PROTPHYS"); //last resort
185  }
186  //std::cout<<"amd "<<amd<<std::endl;
187  result.amodetag = amd;
188  delete amodetagQuery;
189 
190  coral::IQuery* egevQuery = runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
191  coral::AttributeList egevOutput;
192  egevOutput.extend("egev", typeid(std::string));
193  coral::AttributeList egevCondition;
194  egevCondition = coral::AttributeList();
195  egevCondition.extend("name", typeid(std::string));
196  egevCondition.extend("runnumber", typeid(unsigned int));
197  egevCondition["name"].data<std::string>() = std::string("CMS.SCAL:EGEV");
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();
203  result.egev = 0;
204  while (egevCursor.next()) {
205  const coral::AttributeList& row = egevCursor.currentRow();
206  std::string egevstr = row["egev"].data<std::string>();
207  int tmpgev = str2int(egevstr);
208  if (tmpgev > result.egev) {
209  result.egev = tmpgev;
210  }
211  }
212  if (result.egev == 0) {
213  result.egev = 3500; //last resort
214  }
215  delete egevQuery;
216 
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));
221 
222  seqBindVariableList["runnumber"].data<unsigned int>() = runnumber;
223  seqBindVariableList["name"].data<std::string>() = std::string("CMS.LVL0:SEQ_NAME");
224  seqQuery->setCondition("RUNNUMBER =:runnumber AND NAME =:name", seqBindVariableList);
225  seqQuery->addToOutputList("STRING_VALUE");
226  coral::ICursor& seqCursor = seqQuery->execute();
227 
228  while (seqCursor.next()) {
229  const coral::AttributeList& row = seqCursor.currentRow();
230  result.sequence = row["STRING_VALUE"].data<std::string>();
231  }
232  delete seqQuery;
233 
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));
238 
239  hltkeyBindVariableList["runnumber"].data<unsigned int>() = runnumber;
240  hltkeyBindVariableList["name"].data<std::string>() = std::string("CMS.LVL0:HLT_KEY_DESCRIPTION");
241  hltkeyQuery->setCondition("RUNNUMBER =:runnumber AND NAME =:name", hltkeyBindVariableList);
242  hltkeyQuery->addToOutputList("STRING_VALUE");
243  coral::ICursor& hltkeyCursor = hltkeyQuery->execute();
244 
245  while (hltkeyCursor.next()) {
246  const coral::AttributeList& row = hltkeyCursor.currentRow();
247  result.hltkey = row["STRING_VALUE"].data<std::string>();
248  }
249  delete hltkeyQuery;
250 
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));
255 
256  fillBindVariableList["runnumber"].data<unsigned int>() = runnumber;
257  fillBindVariableList["name"].data<std::string>() = std::string("CMS.SCAL:FILLN");
258  fillQuery->setCondition("RUNNUMBER =:runnumber AND NAME =:name", fillBindVariableList);
259  fillQuery->addToOutputList("STRING_VALUE");
260  fillQuery->addToOrderList("TIME");
261  //fillQuery->limitReturnedRows(1);
262  coral::ICursor& fillCursor = fillQuery->execute();
263  unsigned int cc = 0;
264  while (fillCursor.next()) {
265  const coral::AttributeList& row = fillCursor.currentRow();
266  if (cc == 0) {
267  result.fillnumber = row["STRING_VALUE"].data<std::string>();
268  }
269  ++cc;
270  }
271  delete fillQuery;
272  if (result.fillnumber.empty()) {
273  throw nonCollisionException("retrieveData", "CMSRunSummary2DB");
274  }
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));
279 
280  startTVariableList["runnumber"].data<unsigned int>() = runnumber;
281  startTVariableList["name"].data<std::string>() = std::string("CMS.LVL0:START_TIME_T");
282  startTQuery->setCondition("RUNNUMBER =:runnumber AND NAME =:name", startTVariableList);
283  startTQuery->addToOutputList("TIME");
284  coral::ICursor& startTCursor = startTQuery->execute();
285 
286  while (startTCursor.next()) {
287  const coral::AttributeList& row = startTCursor.currentRow();
288  result.startT = row["TIME"].data<coral::TimeStamp>();
289  }
290  delete startTQuery;
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));
295 
296  stopTVariableList["runnumber"].data<unsigned int>() = runnumber;
297  stopTVariableList["name"].data<std::string>() = std::string("CMS.LVL0:STOP_TIME_T");
298  stopTQuery->setCondition("RUNNUMBER =:runnumber AND NAME =:name", stopTVariableList);
299  stopTQuery->addToOutputList("TIME");
300  coral::ICursor& stopTCursor = stopTQuery->execute();
301 
302  while (stopTCursor.next()) {
303  const coral::AttributeList& row = stopTCursor.currentRow();
304  result.stopT = row["TIME"].data<coral::TimeStamp>();
305  }
306  delete stopTQuery;
307  coral::IQuery* l1keyQuery = runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
308  coral::AttributeList l1keyOutput;
309  l1keyOutput.extend("l1key", typeid(std::string));
310  coral::AttributeList l1keyCondition;
311  l1keyCondition = coral::AttributeList();
312  l1keyCondition.extend("name", typeid(std::string));
313  l1keyCondition.extend("runnumber", typeid(unsigned int));
314  l1keyCondition["name"].data<std::string>() = std::string("CMS.TRG:TSC_KEY");
315  l1keyCondition["runnumber"].data<unsigned int>() = runnumber;
316  l1keyQuery->addToOutputList("STRING_VALUE");
317  l1keyQuery->setCondition("NAME=:name AND RUNNUMBER=:runnumber", l1keyCondition);
318  //l1keyQuery->limitReturnedRows(1);
319  l1keyQuery->defineOutput(l1keyOutput);
320  coral::ICursor& l1keyCursor = l1keyQuery->execute();
321  while (l1keyCursor.next()) {
322  const coral::AttributeList& row = l1keyCursor.currentRow();
323  result.l1key = row["l1key"].data<std::string>();
324  }
325  delete l1keyQuery;
326  } catch (const coral::Exception& er) {
327  runinfosession->transaction().rollback();
328  delete runinfosession;
329  delete svc;
330  throw er;
331  }
332  runinfosession->transaction().commit();
333  delete runinfosession;
334 
335  if (!csvsource.empty()) {
336  parseFillCSV(csvsource, result);
337  } else {
338  result.fillscheme = std::string("");
339  result.ncollidingbunches = 0;
340  }
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
344  << " : fillscheme " << result.fillscheme << " : ncollidingbunches : " << result.ncollidingbunches
345  << std::endl;
346 
347  //std::cout<<"connecting to dest "<<m_dest<<std::endl;
348  coral::ISessionProxy* destsession = svc->connect(m_dest, coral::Update);
349 
350  coral::ITypeConverter& desttpc = destsession->typeConverter();
351  desttpc.setCppTypeForSqlType("unsigned int", "NUMBER(10)");
352  try {
353  destsession->transaction().start(false);
354  coral::ISchema& destschema = destsession->nominalSchema();
355  coral::ITable& destruntable = destschema.tableHandle(LumiNames::cmsrunsummaryTableName());
356  coral::AttributeList runData;
357  destruntable.dataEditor().rowBuffer(runData);
358  runData["RUNNUM"].data<unsigned int>() = runnumber;
359  runData["FILLNUM"].data<unsigned int>() = str2int(result.fillnumber);
360  runData["SEQUENCE"].data<std::string>() = result.sequence;
361  runData["HLTKEY"].data<std::string>() = result.hltkey;
362  runData["STARTTIME"].data<coral::TimeStamp>() = result.startT;
363  runData["STOPTIME"].data<coral::TimeStamp>() = result.stopT;
364  runData["AMODETAG"].data<std::string>() = result.amodetag;
365  runData["EGEV"].data<unsigned int>() = (unsigned int)result.egev;
366  runData["L1KEY"].data<std::string>() = result.l1key;
367  runData["FILLSCHEME"].data<std::string>() = result.fillscheme;
368  runData["NCOLLIDINGBUNCHES"].data<unsigned int>() = result.ncollidingbunches;
369  destruntable.dataEditor().insertRow(runData);
370  } catch (const coral::Exception& er) {
371  std::cout << "database problem " << er.what() << std::endl;
372  destsession->transaction().rollback();
373  delete destsession;
374  delete svc;
375  throw er;
376  }
377  destsession->transaction().commit();
378  delete svc;
379  return 0;
380  }
381  const std::string CMSRunSummary2DB::dataType() const { return "CMSRUNSUMMARY"; }
382  const std::string CMSRunSummary2DB::sourceType() const { return "DB"; }
383  unsigned int CMSRunSummary2DB::str2int(const std::string& s) const {
384  std::istringstream myStream(s);
385  unsigned int i;
386  if (myStream >> i) {
387  return i;
388  } else {
389  throw lumi::Exception(std::string("str2int error"), "str2int", "CMSRunSummary2DB");
390  }
391  }
393 } // namespace lumi
396 #endif
muonTagProbeFilters_cff.matched
matched
Definition: muonTagProbeFilters_cff.py:62
lumi::CMSRunSummary2DB::sourceType
const std::string sourceType() const override
Definition: CMSRunSummary2DB.cc:382
lumi::DataPipe::m_source
std::string m_source
Definition: DataPipe.h:28
mps_fire.i
i
Definition: mps_fire.py:355
lumi::CMSRunSummary2DB::CMSRunSummary2DB
CMSRunSummary2DB(const std::string &dest)
Definition: CMSRunSummary2DB.cc:94
DataPipe.h
lumi::DataPipe::m_dest
std::string m_dest
Definition: DataPipe.h:27
idDealer.h
lumi::CMSRunSummary2DB::str2int
unsigned int str2int(const std::string &s) const
Definition: CMSRunSummary2DB.cc:383
gather_cfg.cout
cout
Definition: gather_cfg.py:144
lumi::DataPipe
Definition: DataPipe.h:8
lumi::CMSRunSummary2DB::cmsrunsum::startT
coral::TimeStamp startT
Definition: CMSRunSummary2DB.cc:59
GlobalPosition_Frontier_DevDB_cff.record
record
Definition: GlobalPosition_Frontier_DevDB_cff.py:10
lumi::CMSRunSummary2DB::cmsrunsum
Definition: CMSRunSummary2DB.cc:50
lumi::DBConfig::setAuthentication
void setAuthentication(const std::string &authPath)
Definition: DBConfig.cc:13
lumi::CMSRunSummary2DB::retrieveData
unsigned long long retrieveData(unsigned int runnumber) override
Definition: CMSRunSummary2DB.cc:111
LumiNames.h
lumi::LumiNames::cmsrunsummaryTableName
static const std::string cmsrunsummaryTableName()
Definition: LumiNames.cc:2
lumi::CMSRunSummary2DB::parseFillCSV
void parseFillCSV(const std::string &csvsource, cmsrunsum &result)
Definition: CMSRunSummary2DB.cc:68
DataPipeFactory.h
lumi::CMSRunSummary2DB::cmsrunsum::l1key
std::string l1key
Definition: CMSRunSummary2DB.cc:51
alignCSCRings.s
s
Definition: alignCSCRings.py:92
trigger::size_type
uint16_t size_type
Definition: TriggerTypeDefs.h:18
lumi::CMSRunSummary2DB
Definition: CMSRunSummary2DB.cc:40
DEFINE_EDM_PLUGIN
#define DEFINE_EDM_PLUGIN(factory, type, name)
Definition: PluginFactory.h:124
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
lumi::DataPipe::m_authpath
std::string m_authpath
Definition: DataPipe.h:29
lumi::CMSRunSummary2DB::cmsrunsum::stopT
coral::TimeStamp stopT
Definition: CMSRunSummary2DB.cc:60
edmplugin::PluginFactory
Definition: PluginFactory.h:34
cond::runnumber
Definition: Time.h:19
createfilelist.int
int
Definition: createfilelist.py:10
lumi::DBConfig
Definition: DBConfig.h:8
lumi::CMSRunSummary2DB::cmsrunsum::egev
int egev
Definition: CMSRunSummary2DB.cc:53
cc
lumi::nonCollisionException
Definition: Exception.h:16
lumi::CMSRunSummary2DB::cmsrunsum::amodetag
std::string amodetag
Definition: CMSRunSummary2DB.cc:52
lumi::CMSRunSummary2DB::dataType
const std::string dataType() const override
Definition: CMSRunSummary2DB.cc:381
lumi::CMSRunSummary2DB::isCollisionRun
bool isCollisionRun(const lumi::CMSRunSummary2DB::cmsrunsum &rundata)
Definition: CMSRunSummary2DB.cc:95
Exception
Definition: hltDiff.cc:246
Exception.h
lumi::CMSRunSummary2DB::cmsrunsum::ncollidingbunches
int ncollidingbunches
Definition: CMSRunSummary2DB.cc:58
DBConfig.h
mps_fire.result
result
Definition: mps_fire.py:303
mps_splice.line
line
Definition: mps_splice.py:76
lumi
Definition: LumiSectionData.h:20
lumi::CMSRunSummary2DB::~CMSRunSummary2DB
~CMSRunSummary2DB() override
Definition: CMSRunSummary2DB.cc:392
lumi::CMSRunSummary2DB::cmsrunsum::sequence
std::string sequence
Definition: CMSRunSummary2DB.cc:56
lumi::CMSRunSummary2DB::cmsrunsum::fillscheme
std::string fillscheme
Definition: CMSRunSummary2DB.cc:57
HIPAlignmentAlgorithm_cfi.isCollision
isCollision
Definition: HIPAlignmentAlgorithm_cfi.py:10
mps_fire.dest
dest
Definition: mps_fire.py:179
lumi::CMSRunSummary2DB::cmsrunsum::fillnumber
std::string fillnumber
Definition: CMSRunSummary2DB.cc:55
ConstantDef.h
lumi::CMSRunSummary2DB::cmsrunsum::hltkey
std::string hltkey
Definition: CMSRunSummary2DB.cc:54