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"
41 virtual unsigned long long retrieveData(
unsigned int )
override;
52 typedef std::map< unsigned int, std::string , std::less<unsigned int> >
HltPathMap;
53 typedef std::vector< std::map<unsigned int,HLTV32DB::hltinfo,std::less<unsigned int> > >
HltResult;
55 unsigned int irunnumber,
58 HltResult::iterator hltBeg,
59 HltResult::iterator hltEnd,
60 unsigned int commitintv);
62 unsigned int irunnumber,
65 HltResult::iterator hltBeg,
66 HltResult::iterator hltEnd,
68 unsigned int commitintv);
79 std::string triggerpathtabname(
"HLT_SUPERVISOR_TRIGGERPATHS");
80 std::string maptabname(
"HLT_SUPERVISOR_SCALAR_MAP_V2");
82 coral::ConnectionService*
svc=
new coral::ConnectionService;
94 if(cutpos!=std::string::npos){
99 coral::ISessionProxy* srcsession=svc->connect(dbsource, coral::ReadOnly);
100 coral::ITypeConverter& tpc=srcsession->typeConverter();
101 tpc.setCppTypeForSqlType(
"unsigned int",
"NUMBER(11)");
102 srcsession->transaction().start(
true);
103 coral::ISchema& gtSchemaHandle=srcsession->schema(gtschema);
104 coral::ISchema& hltSchemaHandle=srcsession->schema(hltschema);
105 coral::ISchema& confdbSchemaHandle=srcsession->schema(confdbschema);
106 if( !hltSchemaHandle.existsTable(triggerpathtabname) || !hltSchemaHandle.existsTable(maptabname) ){
107 throw lumi::Exception(
"missing hlt tables" ,
"retrieveData",
"HLTV32DB");
111 std::vector< std::pair<unsigned int,unsigned int> > psindexmap;
112 coral::AttributeList psindexVariableList;
113 psindexVariableList.extend(
"runnumber",
typeid(
unsigned int));
114 psindexVariableList[
"runnumber"].data<
unsigned int>()=runnumber;
115 coral::IQuery* qPsindex=gtSchemaHandle.tableHandle(gttabname).newQuery();
116 coral::AttributeList psindexOutput;
117 psindexOutput.extend(
"PRESCALE_INDEX",
typeid(
unsigned int));
118 psindexOutput.extend(
"LUMI_SECTION",
typeid(
unsigned int));
119 qPsindex->addToOutputList(
"PRESCALE_INDEX");
120 qPsindex->addToOutputList(
"LUMI_SECTION");
121 qPsindex->setCondition(
"RUN_NUMBER=:runnumber",psindexVariableList);
122 qPsindex->defineOutput(psindexOutput);
123 coral::ICursor& psindexCursor=qPsindex->execute();
124 unsigned int lsmin=4294967295;
125 unsigned int lsmax=0;
126 while( psindexCursor.next() ){
127 if( !psindexCursor.currentRow()[
"PRESCALE_INDEX"].isNull() ){
128 unsigned int psindx=psindexCursor.currentRow()[
"PRESCALE_INDEX"].data<
unsigned int>();
129 unsigned int pslsnum=psindexCursor.currentRow()[
"LUMI_SECTION"].data<
unsigned int>();
136 psindexmap.push_back(std::make_pair(pslsnum,psindx));
140 if(psindexmap.size()==0){
141 srcsession->transaction().commit();
143 throw lumi::Exception(
"no psindex data found",
"retrieveData",
"HLTV32DB");
149 coral::AttributeList bindVariableList;
150 bindVariableList.extend(
"runnumber",
typeid(
unsigned int));
151 bindVariableList[
"runnumber"].data<
unsigned int>()=runnumber;
152 coral::IQuery*
q1=hltSchemaHandle.tableHandle(triggerpathtabname).newQuery();
153 coral::AttributeList hltpathid;
154 hltpathid.extend(
"hltpathid",
typeid(
unsigned int));
155 q1->addToOutputList(
"distinct(PATHID)",
"hltpathid");
156 q1->setCondition(
"RUNNUMBER=:runnumber",bindVariableList);
157 q1->defineOutput(hltpathid);
158 coral::ICursor&
c=q1->execute();
162 unsigned int hid=c.currentRow()[
"hltpathid"].data<
unsigned int>();
163 hltpathmap.insert(std::make_pair(hid,
""));
168 HltPathMap::iterator mpit;
169 HltPathMap::iterator mpitBeg=hltpathmap.begin();
170 HltPathMap::iterator mpitEnd=hltpathmap.end();
171 for( mpit=mpitBeg;mpit!=mpitEnd;++mpit){
172 coral::IQuery* mq=confdbSchemaHandle.newQuery();
173 coral::AttributeList mqbindVariableList;
174 mqbindVariableList.extend(
"pathid",
typeid(
unsigned int));
175 mqbindVariableList[
"pathid"].data<
unsigned int>()=mpit->first;
176 mq->addToTableList(confdbpathtabname);
177 mq->addToOutputList(
"NAME",
"hltpathname");
178 mq->setCondition(
"PATHID=:pathid",mqbindVariableList);
179 coral::ICursor& mqcursor=mq->execute();
180 while( mqcursor.next() ){
189 unsigned int nls=lsmax-lsmin+1;
191 hltresult.reserve(nls);
193 for(
unsigned int i=lsmin;
i<=lsmax;++
i){
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();++it ){
216 unsigned int lsnum=it->first;
217 unsigned int psindex=it->second;
218 coral::AttributeList hltdataVariableList;
219 hltdataVariableList.extend(
"runnumber",
typeid(
unsigned int));
220 hltdataVariableList.extend(
"lsnum",
typeid(
unsigned int));
221 hltdataVariableList.extend(
"psindex",
typeid(
unsigned int));
222 hltdataVariableList[
"runnumber"].data<
unsigned int>()=runnumber;
223 hltdataVariableList[
"lsnum"].data<
unsigned int>()=lsnum;
224 hltdataVariableList[
"psindex"].data<
unsigned int>()=psindex;
225 coral::IQuery* qHltData=hltSchemaHandle.newQuery();
226 qHltData->addToTableList(triggerpathtabname,
"t");
227 qHltData->addToTableList(maptabname,
"m");
228 coral::AttributeList hltdataOutput;
229 hltdataOutput.extend(
"L1PASS",
typeid(
unsigned int));
230 hltdataOutput.extend(
"PACCEPT",
typeid(
unsigned int));
231 hltdataOutput.extend(
"PATHID",
typeid(
unsigned int));
232 hltdataOutput.extend(
"PSVALUE",
typeid(
unsigned int));
234 qHltData->addToOutputList(
"t.L1PASS",
"l1pass");
235 qHltData->addToOutputList(
"t.PACCEPT",
"paccept");
236 qHltData->addToOutputList(
"t.PATHID",
"pathid");
237 qHltData->addToOutputList(
"m.PSVALUE",
"psvalue");
238 qHltData->setCondition(
"m.PATHID=t.PATHID and m.RUNNUMBER=t.RUNNUMBER and m.RUNNUMBER=:runnumber AND m.PSINDEX=:psindex AND t.LSNUMBER=:lsnum",hltdataVariableList);
239 qHltData->defineOutput(hltdataOutput);
240 coral::ICursor& hltdataCursor=qHltData->execute();
241 while( hltdataCursor.next() ){
242 const coral::AttributeList& row=hltdataCursor.currentRow();
244 lscountfromzero=
true;
245 if(lscountfromzero) {
246 std::cout<<
"hlt ls count from 0 , we skip/dodge/parry it!"<<std::endl;
249 unsigned int pathid=row[
"PATHID"].data<
unsigned int>();
250 std::map<unsigned int,hltinfo>& allpathinfo=hltresult.at(lsnum-1);
251 hltinfo& pathcontent=allpathinfo[pathid];
252 pathcontent.
hltinput=row[
"L1PASS"].data<
unsigned int>();
253 pathcontent.
hltaccept=row[
"PACCEPT"].data<
unsigned int>();
254 pathcontent.
prescale=row[
"PSVALUE"].data<
unsigned int>();
259 srcsession->transaction().commit();
264 unsigned int npath=hltpathmap.size();
266 coral::ITypeConverter& lumitpc=destsession->typeConverter();
267 lumitpc.setCppTypeForSqlType(
"unsigned int",
"NUMBER(7)");
268 lumitpc.setCppTypeForSqlType(
"unsigned int",
"NUMBER(10)");
269 lumitpc.setCppTypeForSqlType(
"unsigned long long",
"NUMBER(20)");
279 unsigned int totalcmsls=hltresult.size();
280 std::cout<<
"inserting totalhltls "<<totalcmsls<<
" total path "<<npath<<std::endl;
283 unsigned long long hltdataid=0;
285 if(
m_mode==
"loadoldschema"){
286 std::cout<<
"writing hlt data to old hlt table"<<std::endl;
290 std::cout<<
"writing hlt data to new lshlt table"<<std::endl;
311 std::cout<<
"database problem "<<er.what()<<std::endl;
312 destsession->transaction().rollback();
321 unsigned int irunnumber,
324 HltResult::iterator hltItBeg,
325 HltResult::iterator hltItEnd,
326 unsigned int commitintv){
327 std::map< unsigned long long, std::vector<unsigned long long> > idallocationtable;
328 unsigned int hltlscount=0;
329 unsigned int totalcmsls=std::distance(hltItBeg,hltItEnd);
330 std::cout<<
"\t allocating total ids "<<totalcmsls*npath<<std::endl;
331 lumisession->transaction().start(
false);
334 for(HltResult::iterator hltIt=hltItBeg;hltIt!=hltItEnd;++hltIt,++hltlscount){
335 std::vector<unsigned long long> pathvec;
336 pathvec.reserve(npath);
337 for(
unsigned int i=0;
i<npath;++
i,++hltID){
338 pathvec.push_back(hltID);
340 idallocationtable.insert(std::make_pair(hltlscount,pathvec));
342 std::cout<<
"\t all ids allocated"<<std::endl;
344 coral::AttributeList hltData;
345 hltData.extend(
"HLT_ID",
typeid(
unsigned long long));
346 hltData.extend(
"RUNNUM",
typeid(
unsigned int));
347 hltData.extend(
"CMSLSNUM",
typeid(
unsigned int));
349 hltData.extend(
"INPUTCOUNT",
typeid(
unsigned int));
350 hltData.extend(
"ACCEPTCOUNT",
typeid(
unsigned int));
351 hltData.extend(
"PRESCALE",
typeid(
unsigned int));
354 unsigned long long& hlt_id=hltData[
"HLT_ID"].data<
unsigned long long>();
355 unsigned int& hltrunnum=hltData[
"RUNNUM"].data<
unsigned int>();
356 unsigned int&
cmslsnum=hltData[
"CMSLSNUM"].data<
unsigned int>();
358 unsigned int& inputcount=hltData[
"INPUTCOUNT"].data<
unsigned int>();
359 unsigned int& acceptcount=hltData[
"ACCEPTCOUNT"].data<
unsigned int>();
360 unsigned int& prescale=hltData[
"PRESCALE"].data<
unsigned int>();
362 coral::IBulkOperation* hltInserter=0;
363 unsigned int comittedls=0;
364 for(HltResult::iterator hltIt=hltItBeg;hltIt!=hltItEnd;++hltIt,++hltlscount){
365 std::map<unsigned int,HLTV32DB::hltinfo>::const_iterator pathIt;
366 std::map<unsigned int,HLTV32DB::hltinfo>::const_iterator pathBeg=hltIt->begin();
367 std::map<unsigned int,HLTV32DB::hltinfo>::const_iterator pathEnd=hltIt->end();
368 if(!lumisession->transaction().isActive()){
369 lumisession->transaction().start(
false);
371 hltInserter=hlttable.dataEditor().bulkInsert(hltData,npath);
375 hltInserter=hlttable.dataEditor().bulkInsert(hltData,npath);
378 unsigned int hltpathcount=0;
379 for(pathIt=pathBeg;pathIt!=pathEnd;++pathIt,++hltpathcount){
380 hlt_id = idallocationtable[hltlscount].at(hltpathcount);
381 hltrunnum = irunnumber;
382 cmslsnum = pathIt->second.cmsluminr;
383 pathname = pathIt->second.pathname;
384 inputcount = pathIt->second.hltinput;
385 acceptcount = pathIt->second.hltaccept;
386 prescale = pathIt->second.prescale;
387 hltInserter->processNextIteration();
389 hltInserter->flush();
391 if(comittedls==commitintv){
392 std::cout<<
"\t committing in LS chunck "<<comittedls<<std::endl;
393 delete hltInserter; hltInserter=0;
394 lumisession->transaction().commit();
397 }
else if( hltlscount==(totalcmsls-1) ){
398 std::cout<<
"\t committing at the end"<<std::endl;
399 delete hltInserter; hltInserter=0;
400 lumisession->transaction().commit();
407 unsigned int irunnumber,
410 HltResult::iterator hltItBeg,
411 HltResult::iterator hltItEnd,
413 unsigned int commitintv){
414 HltResult::iterator hltIt;
415 unsigned int totalcmsls=std::distance(hltItBeg,hltItEnd);
416 std::cout<<
"inserting totalcmsls "<<totalcmsls<<std::endl;
417 coral::AttributeList lshltData;
418 lshltData.extend(
"DATA_ID",
typeid(
unsigned long long));
419 lshltData.extend(
"RUNNUM",
typeid(
unsigned int));
420 lshltData.extend(
"CMSLSNUM",
typeid(
unsigned int));
421 lshltData.extend(
"PRESCALEBLOB",
typeid(coral::Blob));
422 lshltData.extend(
"HLTCOUNTBLOB",
typeid(coral::Blob));
423 lshltData.extend(
"HLTACCEPTBLOB",
typeid(coral::Blob));
424 unsigned long long& data_id=lshltData[
"DATA_ID"].data<
unsigned long long>();
425 unsigned int& hltrunnum=lshltData[
"RUNNUM"].data<
unsigned int>();
426 unsigned int&
cmslsnum=lshltData[
"CMSLSNUM"].data<
unsigned int>();
427 coral::Blob& prescaleblob=lshltData[
"PRESCALEBLOB"].data<coral::Blob>();
428 coral::Blob& hltcountblob=lshltData[
"HLTCOUNTBLOB"].data<coral::Blob>();
429 coral::Blob& hltacceptblob=lshltData[
"HLTACCEPTBLOB"].data<coral::Blob>();
431 unsigned long long branch_id=3;
435 std::stringstream op;
438 lumisession->transaction().start(
false);
442 hltrundata.
npath=npath;
444 HltPathMap::iterator hltpathmapIt;
445 HltPathMap::iterator hltpathmapItBeg=hltpathmap.begin();
446 HltPathMap::iterator hltpathmapItEnd=hltpathmap.end();
447 for(hltpathmapIt=hltpathmapItBeg;hltpathmapIt!=hltpathmapItEnd;++hltpathmapIt){
448 if(hltpathmapIt!=hltpathmapItBeg){
451 pathnames+=hltpathmapIt->second;
453 std::cout<<
"\tpathnames "<<pathnames<<std::endl;
465 std::cout<<
"inserting hltrundata"<<std::endl;
467 std::cout<<
"inserting lshlt data"<<std::endl;
469 unsigned int hltlscount=0;
470 coral::IBulkOperation* hltInserter=0;
471 unsigned int comittedls=0;
472 for(HltResult::iterator hltIt=hltItBeg;hltIt!=hltItEnd;++hltIt,++hltlscount){
473 unsigned int cmslscount=hltlscount+1;
474 std::map<unsigned int,HLTV32DB::hltinfo,std::less<unsigned int> >::const_iterator pathIt;
475 std::map<unsigned int,HLTV32DB::hltinfo,std::less<unsigned int> >::const_iterator pathBeg=hltIt->begin();
476 std::map<unsigned int,HLTV32DB::hltinfo,std::less<unsigned int> >::const_iterator pathEnd=hltIt->end();
477 if(!lumisession->transaction().isActive()){
478 lumisession->transaction().start(
false);
480 hltInserter=hlttable.dataEditor().bulkInsert(lshltData,npath);
484 hltInserter=hlttable.dataEditor().bulkInsert(lshltData,npath);
488 hltrunnum = irunnumber;
489 cmslsnum = cmslscount;
490 std::vector<unsigned int>
prescales; prescales.reserve(npath);
491 std::vector<unsigned int> hltcounts; hltcounts.reserve(npath);
492 std::vector<unsigned int> hltaccepts; hltaccepts.reserve(npath);
494 for(pathIt=pathBeg;pathIt!=pathEnd;++pathIt){
495 unsigned int hltcount=pathIt->second.hltinput;
497 hltcounts.push_back(hltcount);
498 unsigned int hltaccept=pathIt->second.hltaccept;
500 hltaccepts.push_back(hltaccept);
501 unsigned int prescale=pathIt->second.prescale;
503 prescales.push_back(prescale);
505 prescaleblob.resize(
sizeof(
unsigned int)*npath);
506 void* prescaleblob_StartAddress = prescaleblob.startingAddress();
507 std::memmove(prescaleblob_StartAddress,&prescales[0],
sizeof(
unsigned int)*npath);
508 hltcountblob.resize(
sizeof(
unsigned int)*npath);
509 void* hltcountblob_StartAddress = hltcountblob.startingAddress();
510 std::memmove(hltcountblob_StartAddress,&hltcounts[0],
sizeof(
unsigned int)*npath);
511 hltacceptblob.resize(
sizeof(
unsigned int)*npath);
512 void* hltacceptblob_StartAddress = hltacceptblob.startingAddress();
513 std::memmove(hltacceptblob_StartAddress,&hltaccepts[0],
sizeof(
unsigned int)*npath);
515 hltInserter->processNextIteration();
516 hltInserter->flush();
518 if(comittedls==commitintv){
519 std::cout<<
"\t committing in LS chunck "<<comittedls<<std::endl;
520 delete hltInserter; hltInserter=0;
521 lumisession->transaction().commit();
524 }
else if( hltlscount==(totalcmsls-1) ){
525 std::cout<<
"\t committing at the end"<<std::endl;
526 delete hltInserter; hltInserter=0;
527 lumisession->transaction().commit();
void addEntry(coral::ISchema &schema, const std::string &datatableName, const Entry &entry, unsigned long long branch_id, const std::string &branchname)
static const std::string lshltTableName()
virtual const std::string sourceType() const override
std::vector< std::map< unsigned int, HLTV32DB::hltinfo, std::less< unsigned int > > > HltResult
std::map< unsigned int, std::string, std::less< unsigned int > > HltPathMap
static const std::string hltdataTableName()
void writeHltData(coral::ISessionProxy *lumisession, unsigned int irunnumber, const std::string &source, unsigned int npath, HltResult::iterator hltBeg, HltResult::iterator hltEnd, unsigned int commitintv)
unsigned long long data_id
void setAuthentication(const std::string &authPath)
unsigned long long getEntryInBranchByName(coral::ISchema &schema, const std::string &datatableName, const std::string &entryname, const std::string &branchname)
unsigned long long revision_id
static const unsigned int COMMITINTERVAL
unsigned long long entry_id
static const unsigned int COMMITLSINTERVAL
void bookNewEntry(coral::ISchema &schema, const std::string &datatableName, Entry &entry)
void insertHltRunData(coral::ISchema &schema, const HltEntry &hltentry)
static const std::string hltTableName()
unsigned long long generateNextIDForTable(const std::string &tableName, unsigned int interval=1)
virtual const std::string dataType() const override
unsigned long long writeHltDataToSchema2(coral::ISessionProxy *lumisession, unsigned int irunnumber, const std::string &source, unsigned int npath, HltResult::iterator hltBeg, HltResult::iterator hltEnd, HltPathMap &hltpathmap, unsigned int commitintv)
#define DEFINE_EDM_PLUGIN(factory, type, name)
void addRevision(coral::ISchema &schema, const std::string &datatableName, const Entry &revision, unsigned long long branch_id, std::string &branchname)
virtual unsigned long long retrieveData(unsigned int) override
HLTV32DB(const std::string &dest)
static std::string const source
void bookNewRevision(coral::ISchema &schema, const std::string &datatableName, Entry &revision)