1 #ifndef RecoLuminosity_LumiProducer_HLTV32DB_H
2 #define RecoLuminosity_LumiProducer_HLTV32DB_H
3 #include "CoralBase/AttributeList.h"
4 #include "CoralBase/Attribute.h"
5 #include "CoralBase/AttributeSpecification.h"
6 #include "CoralBase/Blob.h"
7 #include "CoralBase/Exception.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"
51 typedef std::map<unsigned int, std::string, std::less<unsigned int> >
HltPathMap;
52 typedef std::vector<std::map<unsigned int, HLTV32DB::hltinfo, std::less<unsigned int> > >
HltResult;
54 unsigned int irunnumber,
57 HltResult::iterator hltBeg,
58 HltResult::iterator hltEnd,
59 unsigned int commitintv);
61 unsigned int irunnumber,
64 HltResult::iterator hltBeg,
65 HltResult::iterator hltEnd,
67 unsigned int commitintv);
78 std::string triggerpathtabname(
"HLT_SUPERVISOR_TRIGGERPATHS");
79 std::string maptabname(
"HLT_SUPERVISOR_SCALAR_MAP_V2");
81 coral::ConnectionService* svc =
new coral::ConnectionService;
93 if (cutpos != std::string::npos) {
94 dbsource =
m_source.substr(0, cutpos);
95 csvsource =
m_source.substr(cutpos + 1);
98 coral::ISessionProxy* srcsession = svc->connect(dbsource, coral::ReadOnly);
99 coral::ITypeConverter& tpc = srcsession->typeConverter();
100 tpc.setCppTypeForSqlType(
"unsigned int",
"NUMBER(11)");
101 srcsession->transaction().start(
true);
102 coral::ISchema& gtSchemaHandle = srcsession->schema(gtschema);
103 coral::ISchema& hltSchemaHandle = srcsession->schema(hltschema);
104 coral::ISchema& confdbSchemaHandle = srcsession->schema(confdbschema);
105 if (!hltSchemaHandle.existsTable(triggerpathtabname) || !hltSchemaHandle.existsTable(maptabname)) {
106 throw lumi::Exception(
"missing hlt tables",
"retrieveData",
"HLTV32DB");
110 std::vector<std::pair<unsigned int, unsigned int> > psindexmap;
111 coral::AttributeList psindexVariableList;
112 psindexVariableList.extend(
"runnumber",
typeid(
unsigned int));
113 psindexVariableList[
"runnumber"].data<
unsigned int>() =
runnumber;
114 coral::IQuery* qPsindex = gtSchemaHandle.tableHandle(gttabname).newQuery();
115 coral::AttributeList psindexOutput;
116 psindexOutput.extend(
"PRESCALE_INDEX",
typeid(
unsigned int));
117 psindexOutput.extend(
"LUMI_SECTION",
typeid(
unsigned int));
118 qPsindex->addToOutputList(
"PRESCALE_INDEX");
119 qPsindex->addToOutputList(
"LUMI_SECTION");
120 qPsindex->setCondition(
"RUN_NUMBER=:runnumber", psindexVariableList);
121 qPsindex->defineOutput(psindexOutput);
122 coral::ICursor& psindexCursor = qPsindex->execute();
123 unsigned int lsmin = 4294967295;
124 unsigned int lsmax = 0;
125 while (psindexCursor.next()) {
126 if (!psindexCursor.currentRow()[
"PRESCALE_INDEX"].isNull()) {
127 unsigned int psindx = psindexCursor.currentRow()[
"PRESCALE_INDEX"].data<
unsigned int>();
128 unsigned int pslsnum = psindexCursor.currentRow()[
"LUMI_SECTION"].data<
unsigned int>();
129 if (pslsnum >
lsmax) {
132 if (pslsnum <
lsmin) {
135 psindexmap.push_back(std::make_pair(pslsnum, psindx));
139 if (psindexmap.empty()) {
140 srcsession->transaction().commit();
142 throw lumi::Exception(
"no psindex data found",
"retrieveData",
"HLTV32DB");
148 coral::AttributeList bindVariableList;
149 bindVariableList.extend(
"runnumber",
typeid(
unsigned int));
150 bindVariableList[
"runnumber"].data<
unsigned int>() =
runnumber;
151 coral::IQuery*
q1 = hltSchemaHandle.tableHandle(triggerpathtabname).newQuery();
152 coral::AttributeList hltpathid;
153 hltpathid.extend(
"hltpathid",
typeid(
unsigned int));
154 q1->addToOutputList(
"distinct(PATHID)",
"hltpathid");
155 q1->setCondition(
"RUNNUMBER=:runnumber", bindVariableList);
156 q1->defineOutput(hltpathid);
157 coral::ICursor&
c =
q1->execute();
158 unsigned int npc = 0;
161 unsigned int hid =
c.currentRow()[
"hltpathid"].data<
unsigned int>();
162 hltpathmap.insert(std::make_pair(hid,
""));
167 HltPathMap::iterator mpit;
168 HltPathMap::iterator mpitBeg = hltpathmap.begin();
169 HltPathMap::iterator mpitEnd = hltpathmap.end();
170 for (mpit = mpitBeg; mpit != mpitEnd; ++mpit) {
171 coral::IQuery* mq = confdbSchemaHandle.newQuery();
172 coral::AttributeList mqbindVariableList;
173 mqbindVariableList.extend(
"pathid",
typeid(
unsigned int));
174 mqbindVariableList[
"pathid"].data<
unsigned int>() = mpit->first;
175 mq->addToTableList(confdbpathtabname);
176 mq->addToOutputList(
"NAME",
"hltpathname");
177 mq->setCondition(
"PATHID=:pathid", mqbindVariableList);
178 coral::ICursor& mqcursor = mq->execute();
179 while (mqcursor.next()) {
190 hltresult.reserve(nls);
195 std::map<unsigned int, HLTV32DB::hltinfo> allpaths;
196 HltPathMap::iterator aIt;
197 HltPathMap::iterator aItBeg = hltpathmap.begin();
198 HltPathMap::iterator aItEnd = hltpathmap.end();
199 for (aIt = aItBeg; aIt != aItEnd; ++aIt) {
206 allpaths.insert(std::make_pair(aIt->first, ct));
208 hltresult.push_back(allpaths);
211 bool lscountfromzero =
false;
214 for (
std::vector<std::pair<unsigned int, unsigned int> >::iterator it = psindexmap.begin(); it != psindexmap.end();
217 unsigned int lsnum = it->first;
218 unsigned int psindex = it->second;
219 coral::AttributeList hltdataVariableList;
220 hltdataVariableList.extend(
"runnumber",
typeid(
unsigned int));
221 hltdataVariableList.extend(
"lsnum",
typeid(
unsigned int));
222 hltdataVariableList.extend(
"psindex",
typeid(
unsigned int));
223 hltdataVariableList[
"runnumber"].data<
unsigned int>() =
runnumber;
224 hltdataVariableList[
"lsnum"].data<
unsigned int>() = lsnum;
225 hltdataVariableList[
"psindex"].data<
unsigned int>() = psindex;
226 coral::IQuery* qHltData = hltSchemaHandle.newQuery();
227 qHltData->addToTableList(triggerpathtabname,
"t");
228 qHltData->addToTableList(maptabname,
"m");
229 coral::AttributeList hltdataOutput;
230 hltdataOutput.extend(
"L1PASS",
typeid(
unsigned int));
231 hltdataOutput.extend(
"PACCEPT",
typeid(
unsigned int));
232 hltdataOutput.extend(
"PATHID",
typeid(
unsigned int));
233 hltdataOutput.extend(
"PSVALUE",
typeid(
unsigned int));
235 qHltData->addToOutputList(
"t.L1PASS",
"l1pass");
236 qHltData->addToOutputList(
"t.PACCEPT",
"paccept");
237 qHltData->addToOutputList(
"t.PATHID",
"pathid");
238 qHltData->addToOutputList(
"m.PSVALUE",
"psvalue");
239 qHltData->setCondition(
240 "m.PATHID=t.PATHID and m.RUNNUMBER=t.RUNNUMBER and m.RUNNUMBER=:runnumber AND m.PSINDEX=:psindex AND "
242 hltdataVariableList);
243 qHltData->defineOutput(hltdataOutput);
244 coral::ICursor& hltdataCursor = qHltData->execute();
245 while (hltdataCursor.next()) {
246 const coral::AttributeList& row = hltdataCursor.currentRow();
248 lscountfromzero =
true;
249 if (lscountfromzero) {
250 std::cout <<
"hlt ls count from 0 , we skip/dodge/parry it!" << std::endl;
253 unsigned int pathid = row[
"PATHID"].data<
unsigned int>();
254 std::map<unsigned int, hltinfo>& allpathinfo = hltresult.at(lsnum - 1);
255 hltinfo& pathcontent = allpathinfo[pathid];
256 pathcontent.
hltinput = row[
"L1PASS"].data<
unsigned int>();
257 pathcontent.
hltaccept = row[
"PACCEPT"].data<
unsigned int>();
258 pathcontent.
prescale = row[
"PSVALUE"].data<
unsigned int>();
263 srcsession->transaction().commit();
268 unsigned int npath = hltpathmap.size();
269 coral::ISessionProxy* destsession = svc->connect(
m_dest, coral::Update);
270 coral::ITypeConverter& lumitpc = destsession->typeConverter();
271 lumitpc.setCppTypeForSqlType(
"unsigned int",
"NUMBER(7)");
272 lumitpc.setCppTypeForSqlType(
"unsigned int",
"NUMBER(10)");
273 lumitpc.setCppTypeForSqlType(
"unsigned long long",
"NUMBER(20)");
283 unsigned int totalcmsls = hltresult.size();
284 std::cout <<
"inserting totalhltls " << totalcmsls <<
" total path " << npath << std::endl;
287 unsigned long long hltdataid = 0;
289 if (
m_mode ==
"loadoldschema") {
290 std::cout <<
"writing hlt data to old hlt table" << std::endl;
294 std::cout <<
"writing hlt data to new lshlt table" << std::endl;
316 std::cout <<
"database problem " << er.what() << std::endl;
317 destsession->transaction().rollback();
325 unsigned int irunnumber,
328 HltResult::iterator hltItBeg,
329 HltResult::iterator hltItEnd,
330 unsigned int commitintv) {
331 std::map<unsigned long long, std::vector<unsigned long long> > idallocationtable;
332 unsigned int hltlscount = 0;
334 std::cout <<
"\t allocating total ids " << totalcmsls * npath << std::endl;
335 lumisession->transaction().start(
false);
337 unsigned long long hltID =
339 for (HltResult::iterator hltIt = hltItBeg; hltIt != hltItEnd; ++hltIt, ++hltlscount) {
340 std::vector<unsigned long long> pathvec;
341 pathvec.reserve(npath);
342 for (
unsigned int i = 0;
i < npath; ++
i, ++hltID) {
343 pathvec.push_back(hltID);
345 idallocationtable.insert(std::make_pair(hltlscount, pathvec));
347 std::cout <<
"\t all ids allocated" << std::endl;
349 coral::AttributeList hltData;
350 hltData.extend(
"HLT_ID",
typeid(
unsigned long long));
351 hltData.extend(
"RUNNUM",
typeid(
unsigned int));
352 hltData.extend(
"CMSLSNUM",
typeid(
unsigned int));
354 hltData.extend(
"INPUTCOUNT",
typeid(
unsigned int));
355 hltData.extend(
"ACCEPTCOUNT",
typeid(
unsigned int));
356 hltData.extend(
"PRESCALE",
typeid(
unsigned int));
359 unsigned long long& hlt_id = hltData[
"HLT_ID"].data<
unsigned long long>();
360 unsigned int& hltrunnum = hltData[
"RUNNUM"].data<
unsigned int>();
361 unsigned int& cmslsnum = hltData[
"CMSLSNUM"].data<
unsigned int>();
363 unsigned int& inputcount = hltData[
"INPUTCOUNT"].data<
unsigned int>();
364 unsigned int& acceptcount = hltData[
"ACCEPTCOUNT"].data<
unsigned int>();
365 unsigned int&
prescale = hltData[
"PRESCALE"].data<
unsigned int>();
367 coral::IBulkOperation* hltInserter =
nullptr;
368 unsigned int comittedls = 0;
369 for (HltResult::iterator hltIt = hltItBeg; hltIt != hltItEnd; ++hltIt, ++hltlscount) {
370 std::map<unsigned int, HLTV32DB::hltinfo>::const_iterator pathIt;
371 std::map<unsigned int, HLTV32DB::hltinfo>::const_iterator pathBeg = hltIt->begin();
372 std::map<unsigned int, HLTV32DB::hltinfo>::const_iterator pathEnd = hltIt->end();
373 if (!lumisession->transaction().isActive()) {
374 lumisession->transaction().start(
false);
376 hltInserter = hlttable.dataEditor().bulkInsert(hltData, npath);
378 if (hltIt == hltItBeg) {
380 hltInserter = hlttable.dataEditor().bulkInsert(hltData, npath);
383 unsigned int hltpathcount = 0;
384 for (pathIt = pathBeg; pathIt != pathEnd; ++pathIt, ++hltpathcount) {
385 hlt_id = idallocationtable[hltlscount].at(hltpathcount);
386 hltrunnum = irunnumber;
387 cmslsnum = pathIt->second.cmsluminr;
389 inputcount = pathIt->second.hltinput;
390 acceptcount = pathIt->second.hltaccept;
392 hltInserter->processNextIteration();
394 hltInserter->flush();
396 if (comittedls == commitintv) {
397 std::cout <<
"\t committing in LS chunck " << comittedls << std::endl;
399 hltInserter =
nullptr;
400 lumisession->transaction().commit();
402 std::cout <<
"\t committed " << std::endl;
403 }
else if (hltlscount == (totalcmsls - 1)) {
404 std::cout <<
"\t committing at the end" << std::endl;
406 hltInserter =
nullptr;
407 lumisession->transaction().commit();
413 unsigned int irunnumber,
416 HltResult::iterator hltItBeg,
417 HltResult::iterator hltItEnd,
419 unsigned int commitintv) {
421 std::cout <<
"inserting totalcmsls " << totalcmsls << std::endl;
422 coral::AttributeList lshltData;
423 lshltData.extend(
"DATA_ID",
typeid(
unsigned long long));
424 lshltData.extend(
"RUNNUM",
typeid(
unsigned int));
425 lshltData.extend(
"CMSLSNUM",
typeid(
unsigned int));
426 lshltData.extend(
"PRESCALEBLOB",
typeid(coral::Blob));
427 lshltData.extend(
"HLTCOUNTBLOB",
typeid(coral::Blob));
428 lshltData.extend(
"HLTACCEPTBLOB",
typeid(coral::Blob));
429 unsigned long long& data_id = lshltData[
"DATA_ID"].data<
unsigned long long>();
430 unsigned int& hltrunnum = lshltData[
"RUNNUM"].data<
unsigned int>();
431 unsigned int& cmslsnum = lshltData[
"CMSLSNUM"].data<
unsigned int>();
432 coral::Blob& prescaleblob = lshltData[
"PRESCALEBLOB"].data<coral::Blob>();
433 coral::Blob& hltcountblob = lshltData[
"HLTCOUNTBLOB"].data<coral::Blob>();
434 coral::Blob& hltacceptblob = lshltData[
"HLTACCEPTBLOB"].data<coral::Blob>();
436 unsigned long long branch_id = 3;
440 std::stringstream op;
443 lumisession->transaction().start(
false);
447 hltrundata.
npath = npath;
449 HltPathMap::iterator hltpathmapIt;
450 HltPathMap::iterator hltpathmapItBeg = hltpathmap.begin();
451 HltPathMap::iterator hltpathmapItEnd = hltpathmap.end();
452 for (hltpathmapIt = hltpathmapItBeg; hltpathmapIt != hltpathmapItEnd; ++hltpathmapIt) {
453 if (hltpathmapIt != hltpathmapItBeg) {
456 pathnames += hltpathmapIt->second;
458 std::cout <<
"\tpathnames " << pathnames << std::endl;
465 <<
" data_id " << hltrundata.
data_id << std::endl;
471 <<
" data_id " << hltrundata.
data_id << std::endl;
475 std::cout <<
"inserting hltrundata" << std::endl;
477 std::cout <<
"inserting lshlt data" << std::endl;
479 unsigned int hltlscount = 0;
480 coral::IBulkOperation* hltInserter =
nullptr;
481 unsigned int comittedls = 0;
482 for (HltResult::iterator hltIt = hltItBeg; hltIt != hltItEnd; ++hltIt, ++hltlscount) {
483 unsigned int cmslscount = hltlscount + 1;
484 std::map<unsigned int, HLTV32DB::hltinfo, std::less<unsigned int> >::const_iterator pathIt;
485 std::map<unsigned int, HLTV32DB::hltinfo, std::less<unsigned int> >::const_iterator pathBeg = hltIt->begin();
486 std::map<unsigned int, HLTV32DB::hltinfo, std::less<unsigned int> >::const_iterator pathEnd = hltIt->end();
487 if (!lumisession->transaction().isActive()) {
488 lumisession->transaction().start(
false);
490 hltInserter = hlttable.dataEditor().bulkInsert(lshltData, npath);
492 if (hltIt == hltItBeg) {
494 hltInserter = hlttable.dataEditor().bulkInsert(lshltData, npath);
498 hltrunnum = irunnumber;
499 cmslsnum = cmslscount;
502 std::vector<unsigned int> hltcounts;
503 hltcounts.reserve(npath);
504 std::vector<unsigned int> hltaccepts;
505 hltaccepts.reserve(npath);
507 for (pathIt = pathBeg; pathIt != pathEnd; ++pathIt) {
508 unsigned int hltcount = pathIt->second.hltinput;
510 hltcounts.push_back(hltcount);
511 unsigned int hltaccept = pathIt->second.hltaccept;
513 hltaccepts.push_back(hltaccept);
514 unsigned int prescale = pathIt->second.prescale;
518 prescaleblob.resize(
sizeof(
unsigned int) * npath);
519 void* prescaleblob_StartAddress = prescaleblob.startingAddress();
520 std::memmove(prescaleblob_StartAddress, &
prescales[0],
sizeof(
unsigned int) * npath);
521 hltcountblob.resize(
sizeof(
unsigned int) * npath);
522 void* hltcountblob_StartAddress = hltcountblob.startingAddress();
523 std::memmove(hltcountblob_StartAddress, &hltcounts[0],
sizeof(
unsigned int) * npath);
524 hltacceptblob.resize(
sizeof(
unsigned int) * npath);
525 void* hltacceptblob_StartAddress = hltacceptblob.startingAddress();
526 std::memmove(hltacceptblob_StartAddress, &hltaccepts[0],
sizeof(
unsigned int) * npath);
528 hltInserter->processNextIteration();
529 hltInserter->flush();
531 if (comittedls == commitintv) {
532 std::cout <<
"\t committing in LS chunck " << comittedls << std::endl;
534 hltInserter =
nullptr;
535 lumisession->transaction().commit();
537 std::cout <<
"\t committed " << std::endl;
538 }
else if (hltlscount == (totalcmsls - 1)) {
539 std::cout <<
"\t committing at the end" << std::endl;
541 hltInserter =
nullptr;
542 lumisession->transaction().commit();