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"
42 virtual const std::string
dataType()
const;
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;
56 unsigned int irunnumber,
59 HltResult::iterator hltBeg,
60 HltResult::iterator hltEnd,
61 unsigned int commitintv);
63 unsigned int irunnumber,
66 HltResult::iterator hltBeg,
67 HltResult::iterator hltEnd,
69 unsigned int commitintv);
78 std::string confdbschema(
"CMS_HLT");
79 std::string hltschema(
"CMS_RUNINFO");
80 std::string confdbpathtabname(
"PATHS");
81 std::string triggerpathtabname(
"HLT_SUPERVISOR_TRIGGERPATHS");
82 std::string lstabname(
"HLT_SUPERVISOR_LUMISECTIONS_V3");
83 std::string maptabname(
"HLT_SUPERVISOR_SCALAR_MAP_V2");
85 coral::ConnectionService*
svc=
new coral::ConnectionService;
103 std::string csvsource(
"");
104 if(cutpos!=std::string::npos){
106 csvsource=
m_source.substr(cutpos+1);
109 coral::ISessionProxy* srcsession=svc->connect(dbsource, coral::ReadOnly);
110 coral::ITypeConverter& tpc=srcsession->typeConverter();
111 tpc.setCppTypeForSqlType(
"unsigned int",
"NUMBER(11)");
112 srcsession->transaction().start(
true);
113 coral::ISchema& hltSchemaHandle=srcsession->schema(hltschema);
114 coral::ISchema& confdbSchemaHandle=srcsession->schema(confdbschema);
115 if( !hltSchemaHandle.existsTable(triggerpathtabname) || !hltSchemaHandle.existsTable(lstabname) || !hltSchemaHandle.existsTable(maptabname) ){
116 throw lumi::Exception(
"missing hlt tables" ,
"retrieveData",
"HLTV32DB");
119 coral::AttributeList bindVariableList;
120 bindVariableList.extend(
"runnumber",
typeid(
unsigned int));
121 bindVariableList[
"runnumber"].data<
unsigned int>()=runnumber;
122 coral::IQuery*
q1=hltSchemaHandle.tableHandle(triggerpathtabname).newQuery();
123 coral::AttributeList hltpathid;
124 hltpathid.extend(
"hltpathid",
typeid(
unsigned int));
125 q1->addToOutputList(
"distinct PATHID",
"hltpathid");
126 q1->setCondition(
"RUNNUMBER =:runnumber",bindVariableList);
127 q1->defineOutput(hltpathid);
128 coral::ICursor&
c=q1->execute();
132 unsigned int hid=c.currentRow()[
"hltpathid"].data<
unsigned int>();
133 hltpathmap.insert(std::make_pair(hid,
""));
136 HltPathMap::iterator mpit;
137 HltPathMap::iterator mpitBeg=hltpathmap.begin();
138 HltPathMap::iterator mpitEnd=hltpathmap.end();
139 for( mpit=mpitBeg;mpit!=mpitEnd;++mpit){
140 coral::IQuery* mq=confdbSchemaHandle.newQuery();
141 coral::AttributeList mqbindVariableList;
142 mqbindVariableList.extend(
"pathid",
typeid(
unsigned int));
143 mqbindVariableList[
"pathid"].data<
unsigned int>()=mpit->first;
144 mq->addToTableList(confdbpathtabname);
145 mq->addToOutputList(
"NAME",
"hltpathname");
146 mq->setCondition(
"PATHID=:pathid",mqbindVariableList);
147 coral::ICursor& mqcursor=mq->execute();
148 while( mqcursor.next() ){
149 std::string
pathname=mqcursor.currentRow()[
"hltpathname"].data<std::string>();
157 unsigned int maxls=0;
158 unsigned int minls=0;
160 coral::IQuery* nq=hltSchemaHandle.tableHandle(lstabname).newQuery();
161 coral::AttributeList nqbindVariableList;
162 coral::AttributeList nqout;
163 nqout.extend(
"minls",
typeid(
unsigned int));
164 nqout.extend(
"maxls",
typeid(
unsigned int));
165 nqbindVariableList.extend(
"runnumber",
typeid(
unsigned int));
166 nqbindVariableList[
"runnumber"].data<
unsigned int>()=runnumber;
167 nq->addToOutputList(
"min(lsnumber)",
"minls");
168 nq->addToOutputList(
"max(lsnumber)",
"maxls");
169 nq->setCondition(
"RUNNUMBER =:runnumber",nqbindVariableList);
170 nq->defineOutput(nqout);
171 coral::ICursor& nqcursor=nq->execute();
172 while( nqcursor.next() ){
173 minls=nqcursor.currentRow()[
"minls"].data<
unsigned int>();
174 maxls=nqcursor.currentRow()[
"maxls"].data<
unsigned int>();
181 hltresult.reserve(nls);
183 for(
unsigned int i=minls;
i<=maxls;++
i){
185 std::map<unsigned int, HLTV32DB::hltinfo> allpaths;
186 HltPathMap::iterator aIt;
187 HltPathMap::iterator aItBeg=hltpathmap.begin();
188 HltPathMap::iterator aItEnd=hltpathmap.end();
189 for(aIt=aItBeg;aIt!=aItEnd;++aIt){
196 allpaths.insert(std::make_pair(aIt->first,ct));
198 hltresult.push_back(allpaths);
201 coral::IQuery* jq=hltSchemaHandle.newQuery();
202 coral::AttributeList jqbindVariableList;
203 jqbindVariableList.extend(
"runnumber",
typeid(
unsigned int));
204 jqbindVariableList[
"runnumber"].data<
unsigned int>()=runnumber;
205 coral::AttributeList jqoutput;
206 jqoutput.extend(
"lsnumber",
typeid(
unsigned int));
207 jqoutput.extend(
"pathid",
typeid(
unsigned int));
208 jqoutput.extend(
"l1pass",
typeid(
unsigned int));
209 jqoutput.extend(
"paccept",
typeid(
unsigned int));
210 jqoutput.extend(
"psvalue",
typeid(
unsigned int));
211 jq->addToTableList(triggerpathtabname,
"tr");
212 jq->addToTableList(lstabname,
"ls");
213 jq->addToTableList(maptabname,
"sm");
214 jq->addToOutputList(
"tr.LSNUMBER",
"lsnumber");
215 jq->addToOutputList(
"tr.PATHID",
"pathid");
216 jq->addToOutputList(
"tr.L1PASS",
"l1pass");
217 jq->addToOutputList(
"tr.PACCEPT",
"paccept");
218 jq->addToOutputList(
"sm.PSVALUE",
"psvalue");
219 jq->setCondition(
"tr.RUNNUMBER=ls.RUNNUMBER AND tr.LSNUMBER=ls.LSNUMBER and sm.RUNNUMBER=tr.RUNNUMBER and sm.PATHID=tr.PATHID and sm.PSINDEX=ls.PSINDEX and tr.RUNNUMBER=:runnumber",jqbindVariableList);
220 jq->defineOutput(jqoutput);
221 jq->addToOrderList(
"tr.LSNUMBER");
222 jq->setRowCacheSize(10692);
223 coral::ICursor& jqcursor=jq->execute();
224 bool lscountfromzero=
false;
225 while( jqcursor.next() ){
226 const coral::AttributeList& row=jqcursor.currentRow();
227 unsigned int currentLumiSection=row[
"lsnumber"].data<
unsigned int>();
228 if (currentLumiSection==0){
229 lscountfromzero=
true;
231 std::map<unsigned int,hltinfo>& allpathinfo=hltresult.at(currentLumiSection-1);
232 unsigned int pathid=row[
"pathid"].data<
unsigned int>();
234 hltinfo& pathcontent=allpathinfo[pathid];
235 pathcontent.
hltinput=row[
"l1pass"].data<
unsigned int>();
236 pathcontent.hltaccept=row[
"paccept"].data<
unsigned int>();
237 pathcontent.prescale=row[
"psvalue"].data<
unsigned int>();
240 if(lscountfromzero) {
241 std::cout<<
"hlt ls count from 0 , we skip/dodge/parry it!"<<std::endl;
244 srcsession->transaction().commit();
249 unsigned int npath=hltpathmap.size();
251 coral::ITypeConverter& lumitpc=destsession->typeConverter();
252 lumitpc.setCppTypeForSqlType(
"unsigned int",
"NUMBER(7)");
253 lumitpc.setCppTypeForSqlType(
"unsigned int",
"NUMBER(10)");
254 lumitpc.setCppTypeForSqlType(
"unsigned long long",
"NUMBER(20)");
264 unsigned int totalcmsls=hltresult.size();
265 std::cout<<
"inserting totalhltls "<<totalcmsls<<
" total path "<<npath<<std::endl;
269 if(
m_mode==
"loadoldschema"){
270 std::cout<<
"writing hlt data to old hlt table"<<std::endl;
274 std::cout<<
"writing hlt data to new lshlt table"<<std::endl;
280 std::cout<<
"database problem "<<er.what()<<std::endl;
281 destsession->transaction().rollback();
289 unsigned int irunnumber,
290 const std::string&
source,
292 HltResult::iterator hltItBeg,
293 HltResult::iterator hltItEnd,
294 unsigned int commitintv){
295 std::map< unsigned long long, std::vector<unsigned long long> > idallocationtable;
296 unsigned int hltlscount=0;
297 unsigned int totalcmsls=std::distance(hltItBeg,hltItEnd);
298 std::cout<<
"\t allocating total ids "<<totalcmsls*npath<<std::endl;
299 lumisession->transaction().start(
false);
302 for(HltResult::iterator hltIt=hltItBeg;hltIt!=hltItEnd;++hltIt,++hltlscount){
303 std::vector<unsigned long long> pathvec;
304 pathvec.reserve(npath);
305 for(
unsigned int i=0;
i<npath;++
i,++hltID){
306 pathvec.push_back(hltID);
308 idallocationtable.insert(std::make_pair(hltlscount,pathvec));
310 std::cout<<
"\t all ids allocated"<<std::endl;
312 coral::AttributeList hltData;
313 hltData.extend(
"HLT_ID",
typeid(
unsigned long long));
314 hltData.extend(
"RUNNUM",
typeid(
unsigned int));
315 hltData.extend(
"CMSLSNUM",
typeid(
unsigned int));
316 hltData.extend(
"PATHNAME",
typeid(std::string));
317 hltData.extend(
"INPUTCOUNT",
typeid(
unsigned int));
318 hltData.extend(
"ACCEPTCOUNT",
typeid(
unsigned int));
319 hltData.extend(
"PRESCALE",
typeid(
unsigned int));
322 unsigned long long& hlt_id=hltData[
"HLT_ID"].data<
unsigned long long>();
323 unsigned int& hltrunnum=hltData[
"RUNNUM"].data<
unsigned int>();
324 unsigned int&
cmslsnum=hltData[
"CMSLSNUM"].data<
unsigned int>();
325 std::string&
pathname=hltData[
"PATHNAME"].data<std::string>();
326 unsigned int& inputcount=hltData[
"INPUTCOUNT"].data<
unsigned int>();
327 unsigned int& acceptcount=hltData[
"ACCEPTCOUNT"].data<
unsigned int>();
328 unsigned int& prescale=hltData[
"PRESCALE"].data<
unsigned int>();
330 coral::IBulkOperation* hltInserter=0;
331 unsigned int comittedls=0;
332 for(HltResult::iterator hltIt=hltItBeg;hltIt!=hltItEnd;++hltIt,++hltlscount){
333 std::map<unsigned int,HLTV32DB::hltinfo>::const_iterator pathIt;
334 std::map<unsigned int,HLTV32DB::hltinfo>::const_iterator pathBeg=hltIt->begin();
335 std::map<unsigned int,HLTV32DB::hltinfo>::const_iterator pathEnd=hltIt->end();
336 if(!lumisession->transaction().isActive()){
337 lumisession->transaction().start(
false);
339 hltInserter=hlttable.dataEditor().bulkInsert(hltData,npath);
343 hltInserter=hlttable.dataEditor().bulkInsert(hltData,npath);
346 unsigned int hltpathcount=0;
347 for(pathIt=pathBeg;pathIt!=pathEnd;++pathIt,++hltpathcount){
348 hlt_id = idallocationtable[hltlscount].at(hltpathcount);
349 hltrunnum = irunnumber;
350 cmslsnum = pathIt->second.cmsluminr;
351 pathname = pathIt->second.pathname;
352 inputcount = pathIt->second.hltinput;
353 acceptcount = pathIt->second.hltaccept;
354 prescale = pathIt->second.prescale;
355 hltInserter->processNextIteration();
357 hltInserter->flush();
359 if(comittedls==commitintv){
360 std::cout<<
"\t committing in LS chunck "<<comittedls<<std::endl;
361 delete hltInserter; hltInserter=0;
362 lumisession->transaction().commit();
365 }
else if( hltlscount==(totalcmsls-1) ){
366 std::cout<<
"\t committing at the end"<<std::endl;
367 delete hltInserter; hltInserter=0;
368 lumisession->transaction().commit();
375 unsigned int irunnumber,
376 const std::string&
source,
378 HltResult::iterator hltItBeg,
379 HltResult::iterator hltItEnd,
381 unsigned int commitintv){
382 HltResult::iterator hltIt;
383 unsigned int totalcmsls=std::distance(hltItBeg,hltItEnd);
384 std::cout<<
"inserting totalcmsls "<<totalcmsls<<std::endl;
385 coral::AttributeList lshltData;
386 lshltData.extend(
"DATA_ID",
typeid(
unsigned long long));
387 lshltData.extend(
"RUNNUM",
typeid(
unsigned int));
388 lshltData.extend(
"CMSLSNUM",
typeid(
unsigned int));
389 lshltData.extend(
"PRESCALEBLOB",
typeid(coral::Blob));
390 lshltData.extend(
"HLTCOUNTBLOB",
typeid(coral::Blob));
391 lshltData.extend(
"HLTACCEPTBLOB",
typeid(coral::Blob));
392 unsigned long long& data_id=lshltData[
"DATA_ID"].data<
unsigned long long>();
393 unsigned int& hltrunnum=lshltData[
"RUNNUM"].data<
unsigned int>();
394 unsigned int&
cmslsnum=lshltData[
"CMSLSNUM"].data<
unsigned int>();
395 coral::Blob& prescaleblob=lshltData[
"PRESCALEBLOB"].data<coral::Blob>();
396 coral::Blob& hltcountblob=lshltData[
"HLTCOUNTBLOB"].data<coral::Blob>();
397 coral::Blob& hltacceptblob=lshltData[
"HLTACCEPTBLOB"].data<coral::Blob>();
399 unsigned long long branch_id=3;
400 std::string branch_name(
"DATA");
403 std::stringstream op;
405 std::string runnumberStr=op.str();
406 lumisession->transaction().start(
false);
410 hltrundata.
npath=npath;
412 HltPathMap::iterator hltpathmapIt;
413 HltPathMap::iterator hltpathmapItBeg=hltpathmap.begin();
414 HltPathMap::iterator hltpathmapItEnd=hltpathmap.end();
415 for(hltpathmapIt=hltpathmapItBeg;hltpathmapIt!=hltpathmapItEnd;++hltpathmapIt){
416 if(hltpathmapIt!=hltpathmapItBeg){
417 pathnames+=std::string(
",");
419 pathnames+=hltpathmapIt->second;
421 std::cout<<
"\tpathnames "<<pathnames<<std::endl;
433 std::cout<<
"inserting hltrundata"<<std::endl;
435 std::cout<<
"inserting lshlt data"<<std::endl;
437 unsigned int hltlscount=0;
438 coral::IBulkOperation* hltInserter=0;
439 unsigned int comittedls=0;
440 for(HltResult::iterator hltIt=hltItBeg;hltIt!=hltItEnd;++hltIt,++hltlscount){
441 unsigned int cmslscount=hltlscount+1;
442 std::map<unsigned int,HLTV32DB::hltinfo,std::less<unsigned int> >::const_iterator pathIt;
443 std::map<unsigned int,HLTV32DB::hltinfo,std::less<unsigned int> >::const_iterator pathBeg=hltIt->begin();
444 std::map<unsigned int,HLTV32DB::hltinfo,std::less<unsigned int> >::const_iterator pathEnd=hltIt->end();
445 if(!lumisession->transaction().isActive()){
446 lumisession->transaction().start(
false);
448 hltInserter=hlttable.dataEditor().bulkInsert(lshltData,npath);
452 hltInserter=hlttable.dataEditor().bulkInsert(lshltData,npath);
456 hltrunnum = irunnumber;
457 cmslsnum = cmslscount;
458 std::vector<unsigned int>
prescales; prescales.reserve(npath);
459 std::vector<unsigned int> hltcounts; hltcounts.reserve(npath);
460 std::vector<unsigned int> hltaccepts; hltaccepts.reserve(npath);
462 for(pathIt=pathBeg;pathIt!=pathEnd;++pathIt){
463 unsigned int hltcount=pathIt->second.hltinput;
465 hltcounts.push_back(hltcount);
466 unsigned int hltaccept=pathIt->second.hltaccept;
468 hltaccepts.push_back(hltaccept);
469 unsigned int prescale=pathIt->second.prescale;
471 prescales.push_back(prescale);
473 prescaleblob.resize(
sizeof(
unsigned int)*npath);
474 void* prescaleblob_StartAddress = prescaleblob.startingAddress();
475 std::memmove(prescaleblob_StartAddress,&prescales[0],
sizeof(
unsigned int)*npath);
476 hltcountblob.resize(
sizeof(
unsigned int)*npath);
477 void* hltcountblob_StartAddress = hltcountblob.startingAddress();
478 std::memmove(hltcountblob_StartAddress,&hltcounts[0],
sizeof(
unsigned int)*npath);
479 hltacceptblob.resize(
sizeof(
unsigned int)*npath);
480 void* hltacceptblob_StartAddress = hltacceptblob.startingAddress();
481 std::memmove(hltacceptblob_StartAddress,&hltaccepts[0],
sizeof(
unsigned int)*npath);
483 hltInserter->processNextIteration();
484 hltInserter->flush();
486 if(comittedls==commitintv){
487 std::cout<<
"\t committing in LS chunck "<<comittedls<<std::endl;
488 delete hltInserter; hltInserter=0;
489 lumisession->transaction().commit();
492 }
else if( hltlscount==(totalcmsls-1) ){
493 std::cout<<
"\t committing at the end"<<std::endl;
494 delete hltInserter; hltInserter=0;
495 lumisession->transaction().commit();
virtual void retrieveData(unsigned int)
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()
void 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)
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
virtual const std::string sourceType() const
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)
virtual const std::string dataType() const
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)
#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)
HLTV32DB(const std::string &dest)
void bookNewRevision(coral::ISchema &schema, const std::string &datatableName, Entry &revision)