1 #ifndef RecoLuminosity_LumiProducer_TRGWBM2DB_h
2 #define RecoLuminosity_LumiProducer_TRGWBM2DB_h
3 #include "CoralBase/AttributeList.h"
4 #include "CoralBase/Attribute.h"
5 #include "CoralBase/AttributeSpecification.h"
6 #include "CoralBase/Exception.h"
7 #include "RelationalAccess/ConnectionService.h"
8 #include "RelationalAccess/ISessionProxy.h"
9 #include "RelationalAccess/ITransaction.h"
10 #include "RelationalAccess/ITypeConverter.h"
11 #include "RelationalAccess/IQuery.h"
12 #include "RelationalAccess/ICursor.h"
13 #include "RelationalAccess/ISchema.h"
14 #include "RelationalAccess/IView.h"
15 #include "RelationalAccess/ITable.h"
16 #include "RelationalAccess/ITableDataEditor.h"
17 #include "RelationalAccess/IBulkOperation.h"
61 coral::ConnectionService* svc =
new coral::ConnectionService;
67 coral::ISessionProxy* trgsession = svc->connect(
m_source, coral::ReadOnly);
68 coral::ITypeConverter& tpc = trgsession->typeConverter();
70 tpc.setCppTypeForSqlType(
"unsigned int",
"NUMBER(7)");
71 tpc.setCppTypeForSqlType(
"unsigned int",
"NUMBER(10)");
72 tpc.setCppTypeForSqlType(
"unsigned long long",
"NUMBER(20)");
74 coral::AttributeList bindVariableList;
75 bindVariableList.extend(
"runnumber",
typeid(
unsigned int));
76 bindVariableList[
"runnumber"].data<
unsigned int>() = runnumber;
78 std::string algoname(
"LEVEL1_TRIGGER_ALGO_CONDITIONS");
79 std::string techname(
"LEVEL1_TRIGGER_TECH_CONDITIONS");
85 std::string runprescalgoviewname(
"GT_RUN_PRESC_ALGO_VIEW");
86 std::string runpresctechviewname(
"GT_RUN_PRESC_TECH_VIEW");
102 algocount.reserve(400);
104 techcount.reserve(400);
106 deadtimeresult.reserve(400);
107 coral::ITransaction& transaction = trgsession->transaction();
108 transaction.start(
true);
132 coral::ISchema& wbmschemaHandle = trgsession->schema(wbmschema);
133 if (!wbmschemaHandle.existsTable(algoname)) {
136 if (!wbmschemaHandle.existsTable(techname)) {
139 if (!wbmschemaHandle.existsTable(deadname)) {
147 coral::IQuery* Queryalgo = wbmschemaHandle.newQuery();
148 Queryalgo->addToTableList(algoname);
149 coral::AttributeList qalgoOutput;
150 qalgoOutput.extend(
"counts",
typeid(
unsigned int));
151 qalgoOutput.extend(
"lsnr",
typeid(
unsigned int));
152 qalgoOutput.extend(
"algobit",
typeid(
unsigned int));
153 Queryalgo->addToOutputList(
"GTALGOCOUNTS",
"counts");
154 Queryalgo->addToOutputList(
"LUMISEGMENTNR",
"lsnr");
155 Queryalgo->addToOutputList(
"BIT",
"algobit");
156 Queryalgo->setCondition(
"RUNNUMBER =:runnumber", bindVariableList);
157 Queryalgo->addToOrderList(
"LUMISEGMENTNR");
158 Queryalgo->addToOrderList(
"BIT");
159 Queryalgo->defineOutput(qalgoOutput);
160 coral::ICursor&
c = Queryalgo->execute();
163 const coral::AttributeList& row = c.currentRow();
165 unsigned int lsnr = row[
"lsnr"].data<
unsigned int>();
166 unsigned int count = row[
"counts"].data<
unsigned int>();
167 mybitcount_algo.push_back(count);
168 unsigned int algobit = row[
"algobit"].data<
unsigned int>();
172 std::cout <<
"ALGO COUNT alert: found hole in LS range" << std::endl;
173 std::cout <<
" fill all algocount 0 for LS " << s << std::endl;
175 algocount.push_back(tmpzero);
178 algocount.push_back(mybitcount_algo);
179 mybitcount_algo.clear();
185 transaction.commit();
191 transaction.commit();
198 transaction.start(
true);
199 coral::IQuery* Querytech = wbmschemaHandle.newQuery();
200 Querytech->addToTableList(techname);
201 coral::AttributeList qtechOutput;
202 qtechOutput.extend(
"counts",
typeid(
unsigned int));
203 qtechOutput.extend(
"lsnr",
typeid(
unsigned int));
204 qtechOutput.extend(
"techbit",
typeid(
unsigned int));
205 Querytech->addToOutputList(
"GTTECHCOUNTS",
"counts");
206 Querytech->addToOutputList(
"LUMISEGMENTNR",
"lsnr");
207 Querytech->addToOutputList(
"BIT",
"techbit");
208 Querytech->setCondition(
"RUNNUMBER=:runnumber", bindVariableList);
209 Querytech->addToOrderList(
"LUMISEGMENTNR");
210 Querytech->addToOrderList(
"BIT");
211 Querytech->defineOutput(qtechOutput);
212 coral::ICursor& techcursor = Querytech->execute();
215 while (techcursor.next()) {
216 const coral::AttributeList& row = techcursor.currentRow();
218 unsigned int lsnr = row[
"lsnr"].data<
unsigned int>();
219 unsigned int count = row[
"counts"].data<
unsigned int>();
220 unsigned int techbit = row[
"techbit"].data<
unsigned int>();
221 mybitcount_tech.push_back(count);
225 std::cout <<
"TECH COUNT alert: found hole in LS range" << std::endl;
226 std::cout <<
" fill all techcount with 0 for LS " << s << std::endl;
228 techcount.push_back(tmpzero);
231 techcount.push_back(mybitcount_tech);
232 mybitcount_tech.clear();
238 transaction.commit();
244 transaction.commit();
250 transaction.start(
true);
251 coral::IQuery* Querydead = wbmschemaHandle.newQuery();
252 Querydead->addToTableList(deadname);
253 coral::AttributeList qdeadOutput;
254 qdeadOutput.extend(
"counts",
typeid(
unsigned int));
255 qdeadOutput.extend(
"lsnr",
typeid(
unsigned int));
256 Querydead->addToOutputList(
"DEADTIMEBEAMACTIVE",
"counts");
257 Querydead->addToOutputList(
"LUMISEGMENTNR",
"lsnr");
258 coral::AttributeList bindVariablesDead;
259 bindVariablesDead.extend(
"runnumber",
typeid(
int));
260 bindVariablesDead[
"runnumber"].data<
int>() = runnumber;
261 Querydead->setCondition(
"RUNNUMBER=:runnumber", bindVariablesDead);
262 Querydead->addToOrderList(
"LUMISEGMENTNR");
263 Querydead->defineOutput(qdeadOutput);
264 coral::ICursor& deadcursor = Querydead->execute();
266 while (deadcursor.next()) {
267 const coral::AttributeList& row = deadcursor.currentRow();
270 unsigned int lsnr = row[
"lsnr"].data<
unsigned int>();
272 std::cout <<
"DEADTIME alert: found hole in LS range" << std::endl;
273 std::cout <<
" fill deadtimebeamactive 0 for LS " << s << std::endl;
274 deadtimeresult.push_back(0);
277 unsigned int count = row[
"counts"].data<
unsigned int>();
278 deadtimeresult.push_back(count);
284 transaction.commit();
291 transaction.commit();
297 transaction.start(
true);
298 coral::ISchema& gtschemaHandle = trgsession->schema(gtschema);
299 if (!gtschemaHandle.existsView(runtechviewname)) {
302 if (!gtschemaHandle.existsView(runalgoviewname)) {
305 if (!gtschemaHandle.existsView(runprescalgoviewname)) {
308 if (!gtschemaHandle.existsView(runpresctechviewname)) {
314 std::map<unsigned int, std::string> triggernamemap;
315 coral::IQuery* QueryName = gtschemaHandle.newQuery();
316 QueryName->addToTableList(runalgoviewname);
317 coral::AttributeList qAlgoNameOutput;
318 qAlgoNameOutput.extend(
"algo_index",
typeid(
unsigned int));
319 qAlgoNameOutput.extend(
"alias",
typeid(
std::string));
320 QueryName->addToOutputList(
"algo_index");
321 QueryName->addToOutputList(
"alias");
322 QueryName->setCondition(
"runnumber =:runnumber", bindVariableList);
323 QueryName->addToOrderList(
"algo_index");
324 QueryName->defineOutput(qAlgoNameOutput);
325 coral::ICursor& algonamecursor = QueryName->execute();
326 while (algonamecursor.next()) {
327 const coral::AttributeList& row = algonamecursor.currentRow();
329 unsigned int algo_index = row[
"algo_index"].data<
unsigned int>();
331 triggernamemap.insert(std::make_pair(algo_index, algo_name));
335 transaction.commit();
340 transaction.start(
true);
341 std::map<unsigned int, std::string> techtriggernamemap;
342 coral::IQuery* QueryTechName = gtschemaHandle.newQuery();
343 QueryTechName->addToTableList(runtechviewname);
344 coral::AttributeList qTechNameOutput;
345 qTechNameOutput.extend(
"techtrig_index",
typeid(
unsigned int));
346 qTechNameOutput.extend(
"name",
typeid(
std::string));
347 QueryTechName->addToOutputList(
"techtrig_index");
348 QueryTechName->addToOutputList(
"name");
349 QueryTechName->setCondition(
"runnumber =:runnumber", bindVariableList);
350 QueryTechName->addToOrderList(
"techtrig_index");
351 QueryTechName->defineOutput(qTechNameOutput);
352 coral::ICursor& technamecursor = QueryTechName->execute();
353 while (technamecursor.next()) {
354 const coral::AttributeList& row = technamecursor.currentRow();
356 unsigned int tech_index = row[
"techtrig_index"].data<
unsigned int>();
358 techtriggernamemap.insert(std::make_pair(tech_index, tech_name));
360 delete QueryTechName;
361 transaction.commit();
365 transaction.start(
true);
366 coral::IQuery* QueryAlgoPresc = gtschemaHandle.newQuery();
367 QueryAlgoPresc->addToTableList(runprescalgoviewname);
368 coral::AttributeList qAlgoPrescOutput;
369 std::string algoprescBase(
"PRESCALE_FACTOR_ALGO_");
372 qAlgoPrescOutput.extend(algopresc,
typeid(
unsigned int));
376 QueryAlgoPresc->addToOutputList(algopresc);
378 coral::AttributeList PrescbindVariable;
379 PrescbindVariable.extend(
"runnumber",
typeid(
int));
380 PrescbindVariable.extend(
"prescaleindex",
typeid(
int));
381 PrescbindVariable[
"runnumber"].data<
int>() = runnumber;
382 PrescbindVariable[
"prescaleindex"].data<
int>() = 0;
383 QueryAlgoPresc->setCondition(
"runnr =:runnumber AND prescale_index =:prescaleindex", PrescbindVariable);
384 QueryAlgoPresc->defineOutput(qAlgoPrescOutput);
385 coral::ICursor& algopresccursor = QueryAlgoPresc->execute();
386 while (algopresccursor.next()) {
387 const coral::AttributeList& row = algopresccursor.currentRow();
389 for (
unsigned int bitidx = 0; bitidx < 128; ++bitidx) {
391 algoprescale.push_back(row[algopresc].data<unsigned int>());
394 delete QueryAlgoPresc;
395 transaction.commit();
400 transaction.start(
true);
401 coral::IQuery* QueryTechPresc = gtschemaHandle.newQuery();
402 QueryTechPresc->addToTableList(runpresctechviewname);
403 coral::AttributeList qTechPrescOutput;
407 qTechPrescOutput.extend(techpresc,
typeid(
unsigned int));
411 QueryTechPresc->addToOutputList(techpresc);
413 coral::AttributeList TechPrescbindVariable;
414 TechPrescbindVariable.extend(
"runnumber",
typeid(
int));
415 TechPrescbindVariable.extend(
"prescaleindex",
typeid(
int));
416 TechPrescbindVariable[
"runnumber"].data<
int>() = runnumber;
417 TechPrescbindVariable[
"prescaleindex"].data<
int>() = 0;
418 QueryTechPresc->setCondition(
"runnr =:runnumber AND prescale_index =:prescaleindex", TechPrescbindVariable);
419 QueryTechPresc->defineOutput(qTechPrescOutput);
420 coral::ICursor& techpresccursor = QueryTechPresc->execute();
421 while (techpresccursor.next()) {
422 const coral::AttributeList& row = techpresccursor.currentRow();
426 techprescale.push_back(row[techpresc].data<unsigned int>());
429 delete QueryTechPresc;
430 transaction.commit();
431 transaction.commit();
439 std::map<unsigned int, std::string>::iterator pos = triggernamemap.find(algoidx);
440 if (pos != triggernamemap.end()) {
441 algonames.push_back(pos->second);
443 algonames.push_back(
"False");
449 std::stringstream
ss;
452 technames.push_back(ss.str());
458 if (algonames.size() != lumi::N_TRGALGOBIT || technames.size() !=
lumi::N_TRGTECHBIT) {
459 throw lumi::Exception(
"wrong number of bits",
"retrieveData",
"TRGWBM2DB");
461 if (algoprescale.size() != lumi::N_TRGALGOBIT || techprescale.size() !=
lumi::N_TRGTECHBIT) {
462 throw lumi::Exception(
"wrong number of prescale",
"retrieveData",
"TRGWBM2DB");
464 if (deadtimeresult.size() != algocount.size() || deadtimeresult.size() != techcount.size()) {
465 throw lumi::Exception(
"inconsistent number of LS",
"retrieveData",
"TRGWBM2DB");
470 coral::ISessionProxy* lumisession = svc->connect(
m_dest, coral::Update);
471 coral::ITypeConverter& lumitpc = lumisession->typeConverter();
472 lumitpc.setCppTypeForSqlType(
"unsigned int",
"NUMBER(7)");
473 lumitpc.setCppTypeForSqlType(
"unsigned int",
"NUMBER(10)");
474 lumitpc.setCppTypeForSqlType(
"unsigned long long",
"NUMBER(20)");
476 TriggerDeadCountResult::const_iterator deadIt;
477 TriggerDeadCountResult::const_iterator deadBeg = deadtimeresult.begin();
478 TriggerDeadCountResult::const_iterator deadEnd = deadtimeresult.end();
480 unsigned int totalcmsls = deadtimeresult.size();
481 std::cout <<
"inserting totalcmsls " << totalcmsls << std::endl;
482 std::map<unsigned long long, std::vector<unsigned long long> > idallocationtable;
485 lumisession->transaction().start(
false);
489 lumisession->transaction().commit();
490 unsigned int trglscount = 0;
491 for (deadIt = deadBeg; deadIt != deadEnd; ++deadIt, ++trglscount) {
492 std::vector<unsigned long long> bitvec;
493 bitvec.reserve(lumi::N_TRGBIT);
494 BITCOUNT& algoinbits = algocount[trglscount];
495 BITCOUNT& techinbits = techcount[trglscount];
496 BITCOUNT::const_iterator algoBitIt;
497 BITCOUNT::const_iterator algoBitBeg = algoinbits.begin();
498 BITCOUNT::const_iterator algoBitEnd = algoinbits.end();
499 for (algoBitIt = algoBitBeg; algoBitIt != algoBitEnd; ++algoBitIt, ++trgID) {
500 bitvec.push_back(trgID);
502 BITCOUNT::const_iterator techBitIt;
503 BITCOUNT::const_iterator techBitBeg = techinbits.begin();
504 BITCOUNT::const_iterator techBitEnd = techinbits.end();
505 for (techBitIt = techBitBeg; techBitIt != techBitEnd; ++techBitIt, ++trgID) {
506 bitvec.push_back(trgID);
508 idallocationtable.insert(std::make_pair(trglscount, bitvec));
510 std::cout <<
"\t all ids allocated" << std::endl;
511 coral::AttributeList trgData;
512 trgData.extend<
unsigned long long>(
"TRG_ID");
513 trgData.extend<
unsigned int>(
"RUNNUM");
514 trgData.extend<
unsigned int>(
"CMSLSNUM");
515 trgData.extend<
unsigned int>(
"BITNUM");
517 trgData.extend<
unsigned int>(
"TRGCOUNT");
518 trgData.extend<
unsigned long long>(
"DEADTIME");
519 trgData.extend<
unsigned int>(
"PRESCALE");
521 unsigned long long& trg_id = trgData[
"TRG_ID"].data<
unsigned long long>();
522 unsigned int& trgrunnum = trgData[
"RUNNUM"].data<
unsigned int>();
523 unsigned int& cmslsnum = trgData[
"CMSLSNUM"].data<
unsigned int>();
524 unsigned int& bitnum = trgData[
"BITNUM"].data<
unsigned int>();
526 unsigned int&
count = trgData[
"TRGCOUNT"].data<
unsigned int>();
527 unsigned long long& deadtime = trgData[
"DEADTIME"].data<
unsigned long long>();
528 unsigned int& prescale = trgData[
"PRESCALE"].data<
unsigned int>();
531 coral::IBulkOperation* trgInserter =
nullptr;
532 unsigned int comittedls = 0;
533 for (deadIt = deadBeg; deadIt != deadEnd; ++deadIt, ++trglscount) {
534 unsigned int cmslscount = trglscount + 1;
535 BITCOUNT& algoinbits = algocount[trglscount];
536 BITCOUNT& techinbits = techcount[trglscount];
537 unsigned int trgbitcount = 0;
538 BITCOUNT::const_iterator algoBitIt;
539 BITCOUNT::const_iterator algoBitBeg = algoinbits.begin();
540 BITCOUNT::const_iterator algoBitEnd = algoinbits.end();
541 if (!lumisession->transaction().isActive()) {
542 lumisession->transaction().start(
false);
544 trgInserter = trgtable.dataEditor().bulkInsert(trgData, 2048);
546 for (algoBitIt = algoBitBeg; algoBitIt != algoBitEnd; ++algoBitIt, ++trgbitcount) {
547 trg_id = idallocationtable[trglscount].at(trgbitcount);
550 cmslsnum = cmslscount;
551 bitnum = trgbitcount;
552 bitname = algonames[trgbitcount];
554 prescale = algoprescale[trgbitcount];
567 trgInserter->processNextIteration();
570 BITCOUNT::const_iterator techBitIt;
571 BITCOUNT::const_iterator techBitBeg = techinbits.begin();
572 BITCOUNT::const_iterator techBitEnd = techinbits.end();
573 for (techBitIt = techBitBeg; techBitIt != techBitEnd; ++techBitIt, ++trgbitcount) {
574 trg_id = idallocationtable[trglscount].at(trgbitcount);
577 cmslsnum = cmslscount;
578 bitnum = trgbitcount;
582 trgInserter->processNextIteration();
595 trgInserter->flush();
598 std::cout <<
"\t committing in LS chunck " << comittedls << std::endl;
600 trgInserter =
nullptr;
601 lumisession->transaction().commit();
603 std::cout <<
"\t committed " << std::endl;
604 }
else if (trglscount == (totalcmsls - 1)) {
605 std::cout <<
"\t committing at the end" << std::endl;
607 trgInserter =
nullptr;
608 lumisession->transaction().commit();
613 lumisession->transaction().rollback();
628 std::stringstream
ss;
635 std::istringstream myStream(s);
std::vector< BITCOUNT > TriggerCountResult_Tech
static const std::string trgTableName()
const edm::EventSetup & c
const std::string sourceType() const override
unsigned int str2int(const std::string &s)
std::vector< BITCOUNT > TriggerCountResult_Algo
unsigned long long DEADCOUNT
const unsigned int N_TRGTECHBIT
std::string int2str(unsigned int t, unsigned int width)
void setAuthentication(const std::string &authPath)
std::vector< std::string > TriggerNameResult_Algo
std::vector< std::string > TriggerNameResult_Tech
TRGWBM2DB(const std::string &dest)
std::vector< DEADCOUNT > TriggerDeadCountResult
std::vector< unsigned int > PrescaleResult_Tech
unsigned long long generateNextIDForTable(const std::string &tableName, unsigned int interval=1)
unsigned long long retrieveData(unsigned int runnumber) override
static const unsigned int COMMITLSINTERVAL
const std::string dataType() const override
#define DEFINE_EDM_PLUGIN(factory, type, name)
const unsigned int N_TRGALGOBIT
std::vector< unsigned int > BITCOUNT
const unsigned int N_TRGBIT
std::vector< unsigned int > PrescaleResult_Algo