Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:19:55

0001 #ifndef RecoLuminosity_LumiProducer_HLTConf2DB_H
0002 #define RecoLuminosity_LumiProducer_HLTConf2DB_H
0003 
0004 #include "CoralBase/AttributeList.h"
0005 #include "CoralBase/Attribute.h"
0006 #include "CoralBase/AttributeSpecification.h"
0007 #include "CoralBase/Exception.h"
0008 #include "RelationalAccess/ConnectionService.h"
0009 #include "RelationalAccess/ISessionProxy.h"
0010 #include "RelationalAccess/ITransaction.h"
0011 #include "RelationalAccess/ITypeConverter.h"
0012 #include "RelationalAccess/IQuery.h"
0013 #include "RelationalAccess/ICursor.h"
0014 #include "RelationalAccess/ISchema.h"
0015 #include "RelationalAccess/IView.h"
0016 #include "RelationalAccess/ITable.h"
0017 #include "RelationalAccess/ITableDataEditor.h"
0018 #include "RelationalAccess/IBulkOperation.h"
0019 
0020 #include "RecoLuminosity/LumiProducer/interface/DataPipe.h"
0021 #include "RecoLuminosity/LumiProducer/interface/LumiNames.h"
0022 #include "RecoLuminosity/LumiProducer/interface/idDealer.h"
0023 #include "RecoLuminosity/LumiProducer/interface/Exception.h"
0024 #include "RecoLuminosity/LumiProducer/interface/DBConfig.h"
0025 #include "RecoLuminosity/LumiProducer/interface/ConstantDef.h"
0026 #include <iostream>
0027 #include <map>
0028 #include <vector>
0029 #include <string>
0030 namespace lumi {
0031   class HLTConf2DB : public DataPipe {
0032   public:
0033     explicit HLTConf2DB(const std::string& dest);
0034     unsigned long long retrieveData(unsigned int) override;
0035     const std::string dataType() const override;
0036     const std::string sourceType() const override;
0037     ~HLTConf2DB() override;
0038   };  //cl HLTConf2DB
0039   //
0040   //implementation
0041   //
0042   HLTConf2DB::HLTConf2DB(const std::string& dest) : DataPipe(dest) {}
0043   unsigned long long HLTConf2DB::retrieveData(unsigned int runnumber) {
0044     std::string runinfoschema("CMS_RUNINFO");
0045     //std::string hltschema("CMS_HLT_V0");
0046     std::string hltschema("CMS_HLT");
0047     std::string runsessiontable("RUNSESSION_PARAMETER");
0048     //
0049     //must login as cms_hlt_r
0050     coral::ConnectionService* svc = new coral::ConnectionService;
0051     lumi::DBConfig dbconf(*svc);
0052     if (!m_authpath.empty()) {
0053       dbconf.setAuthentication(m_authpath);
0054     }
0055     //std::cout<<"m_source "<<m_source<<std::endl;
0056     coral::ISessionProxy* srcsession = svc->connect(m_source, coral::ReadOnly);
0057     coral::ITypeConverter& tpc = srcsession->typeConverter();
0058     tpc.setCppTypeForSqlType("unsigned int", "NUMBER(38)");
0059     //
0060     //select string_value from CMS_RUNINFO.runsession_parameter where name='CMS.LVL0:HLT_KEY_DESCRIPTION' and runnumber=xx;
0061     //
0062     std::string hltkey("");
0063     std::vector<std::pair<std::string, std::string> > hlt2l1map;
0064     hlt2l1map.reserve(200);
0065     try {
0066       srcsession->transaction().start(true);
0067       coral::ISchema& runinfoSchemaHandle = srcsession->schema(runinfoschema);
0068       if (!runinfoSchemaHandle.existsTable(runsessiontable)) {
0069         throw lumi::Exception("missing runsession table", "retrieveData", "HLTConf2DB");
0070       }
0071       coral::AttributeList rfBindVariableList;
0072       rfBindVariableList.extend("name", typeid(std::string));
0073       rfBindVariableList.extend("runnumber", typeid(unsigned int));
0074       rfBindVariableList["name"].data<std::string>() = std::string("CMS.LVL0:HLT_KEY_DESCRIPTION");
0075       rfBindVariableList["runnumber"].data<unsigned int>() = runnumber;
0076       coral::IQuery* kq = runinfoSchemaHandle.newQuery();
0077       coral::AttributeList runinfoOut;
0078       runinfoOut.extend("STRING_VALUE", typeid(std::string));
0079       kq->addToTableList(runsessiontable);
0080       kq->setCondition("NAME = :name AND RUNNUMBER = :runnumber", rfBindVariableList);
0081       kq->addToOutputList("STRING_VALUE");
0082       kq->defineOutput(runinfoOut);
0083       coral::ICursor& kcursor = kq->execute();
0084       unsigned int s = 0;
0085       while (kcursor.next()) {
0086         const coral::AttributeList& row = kcursor.currentRow();
0087         hltkey = row["STRING_VALUE"].data<std::string>();
0088         ++s;
0089       }
0090       if (s == 0 || hltkey.empty()) {
0091         kcursor.close();
0092         delete kq;
0093         srcsession->transaction().rollback();
0094         throw lumi::Exception(std::string("requested run has no or invalid hltkey"), "retrieveData", "TRG2DB");
0095       }
0096       if (s > 1) {
0097         kcursor.close();
0098         delete kq;
0099         srcsession->transaction().rollback();
0100         throw lumi::Exception(std::string("requested run has multile hltkey"), "retrieveData", "TRG2DB");
0101       }
0102     } catch (const coral::Exception& er) {
0103       std::cout << "source runinfo database problem " << er.what() << std::endl;
0104       srcsession->transaction().rollback();
0105       delete srcsession;
0106       delete svc;
0107       throw er;
0108     }
0109     coral::ISchema& confSchemaHandle = srcsession->nominalSchema();
0110     try {
0111       //srcsession->transaction().start(true);
0112       if (!confSchemaHandle.existsTable("PATHS") || !confSchemaHandle.existsTable("STRINGPARAMVALUES") ||
0113           !confSchemaHandle.existsTable("PARAMETERS") || !confSchemaHandle.existsTable("SUPERIDPARAMETERASSOC") ||
0114           !confSchemaHandle.existsTable("MODULES") || !confSchemaHandle.existsTable("MODULETEMPLATES") ||
0115           !confSchemaHandle.existsTable("PATHMODULEASSOC") || !confSchemaHandle.existsTable("CONFIGURATIONPATHASSOC") ||
0116           !confSchemaHandle.existsTable("CONFIGURATIONS")) {
0117         throw lumi::Exception("missing hlt conf tables", "retrieveData", "HLTConf2DB");
0118       }
0119       coral::AttributeList q1BindVariableList;
0120       q1BindVariableList.extend("hltseed", typeid(std::string));
0121       q1BindVariableList.extend("l1seedexpr", typeid(std::string));
0122       q1BindVariableList.extend("hltkey", typeid(std::string));
0123       q1BindVariableList["hltseed"].data<std::string>() = std::string("HLTLevel1GTSeed");
0124       q1BindVariableList["l1seedexpr"].data<std::string>() = std::string("L1SeedsLogicalExpression");
0125       q1BindVariableList["hltkey"].data<std::string>() = hltkey;
0126       coral::IQuery* q1 = confSchemaHandle.newQuery();
0127       q1->addToOutputList("PATHS.NAME", "hltpath");
0128       q1->addToOutputList("STRINGPARAMVALUES.VALUE", "l1expression");
0129 
0130       q1->addToTableList("PATHS");
0131       q1->addToTableList("STRINGPARAMVALUES");
0132       q1->addToTableList("PARAMETERS");
0133       q1->addToTableList("SUPERIDPARAMETERASSOC");
0134       q1->addToTableList("MODULES");
0135       q1->addToTableList("MODULETEMPLATES");
0136       q1->addToTableList("PATHMODULEASSOC");
0137       q1->addToTableList("CONFIGURATIONPATHASSOC");
0138       q1->addToTableList("CONFIGURATIONS");
0139 
0140       q1->setCondition(
0141           "PARAMETERS.PARAMID=STRINGPARAMVALUES.PARAMID and SUPERIDPARAMETERASSOC.PARAMID=PARAMETERS.PARAMID and "
0142           "MODULES.SUPERID=SUPERIDPARAMETERASSOC.SUPERID and MODULETEMPLATES.SUPERID=MODULES.TEMPLATEID and "
0143           "PATHMODULEASSOC.MODULEID=MODULES.SUPERID and PATHS.PATHID=PATHMODULEASSOC.PATHID and "
0144           "CONFIGURATIONPATHASSOC.PATHID=PATHS.PATHID and CONFIGURATIONS.CONFIGID=CONFIGURATIONPATHASSOC.CONFIGID and "
0145           "MODULETEMPLATES.NAME = :hltseed and PARAMETERS.NAME = :l1seedexpr and CONFIGURATIONS.CONFIGDESCRIPTOR = "
0146           ":hltkey",
0147           q1BindVariableList);
0148 
0149       /**
0150      select paths.name,stringparamvalues.value from stringparamvalues,paths,parameters,superidparameterassoc,modules,moduletemplates,pathmoduleassoc,configurationpathassoc,configurations where parameters.paramid=stringparamvalues.paramid and  superidparameterassoc.paramid=parameters.paramid and modules.superid=superidparameterassoc.superid and moduletemplates.superid=modules.templateid and pathmoduleassoc.moduleid=modules.superid and paths.pathid=pathmoduleassoc.pathid and configurationpathassoc.pathid=paths.pathid and configurations.configid=configurationpathassoc.configid and moduletemplates.name='HLTLevel1GTSeed' and parameters.name='L1SeedsLogicalExpression' and configurations.configid=1905; 
0151       **/
0152       coral::ICursor& cursor1 = q1->execute();
0153       while (cursor1.next()) {
0154         const coral::AttributeList& row = cursor1.currentRow();
0155         std::string hltpath = row["hltpath"].data<std::string>();
0156         std::string l1expression = row["l1expression"].data<std::string>();
0157         hlt2l1map.push_back(std::make_pair(hltpath, l1expression));
0158       }
0159       cursor1.close();
0160       delete q1;
0161     } catch (const coral::Exception& er) {
0162       std::cout << "database problem with source hlt confdb" << er.what() << std::endl;
0163       srcsession->transaction().rollback();
0164       delete srcsession;
0165       throw er;
0166     }
0167     srcsession->transaction().commit();
0168     delete srcsession;
0169     std::vector<std::pair<std::string, std::string> >::const_iterator mIt;
0170     std::vector<std::pair<std::string, std::string> >::const_iterator mBeg = hlt2l1map.begin();
0171     std::vector<std::pair<std::string, std::string> >::const_iterator mEnd = hlt2l1map.end();
0172 
0173     coral::ISessionProxy* destsession = svc->connect(m_dest, coral::Update);
0174     try {
0175       //check if hltkey already exists
0176       destsession->transaction().start(true);
0177       bool hltkeyExists = false;
0178       coral::AttributeList kQueryBindList;
0179       kQueryBindList.extend("hltkey", typeid(std::string));
0180       coral::IQuery* kQuery = destsession->nominalSchema().tableHandle(LumiNames::trghltMapTableName()).newQuery();
0181       kQuery->setCondition("HLTKEY =:hltkey", kQueryBindList);
0182       kQueryBindList["hltkey"].data<std::string>() = hltkey;
0183       coral::ICursor& kResult = kQuery->execute();
0184       while (kResult.next()) {
0185         hltkeyExists = true;
0186       }
0187       if (hltkeyExists) {
0188         std::cout << "hltkey " << hltkey << " already registered , do nothing" << std::endl;
0189         destsession->transaction().commit();
0190         delete kQuery;
0191         delete svc;
0192         return 0;
0193       }
0194       destsession->transaction().commit();
0195       destsession->transaction().start(false);
0196       coral::ISchema& destschema = destsession->nominalSchema();
0197       coral::ITable& hltconftable = destschema.tableHandle(LumiNames::trghltMapTableName());
0198       coral::AttributeList hltconfData;
0199       hltconfData.extend<std::string>("HLTKEY");
0200       hltconfData.extend<std::string>("HLTPATHNAME");
0201       hltconfData.extend<std::string>("L1SEED");
0202       coral::IBulkOperation* hltconfInserter = hltconftable.dataEditor().bulkInsert(hltconfData, 200);
0203       hltconfData["HLTKEY"].data<std::string>() = hltkey;
0204       std::string& hltpathname = hltconfData["HLTPATHNAME"].data<std::string>();
0205       std::string& l1seedname = hltconfData["L1SEED"].data<std::string>();
0206       for (mIt = mBeg; mIt != mEnd; ++mIt) {
0207         hltpathname = mIt->first;
0208         l1seedname = mIt->second;
0209         hltconfInserter->processNextIteration();
0210         //std::cout<<mIt->first<<" "<<mIt->second<<std::endl;
0211       }
0212       hltconfInserter->flush();
0213       delete hltconfInserter;
0214       destsession->transaction().commit();
0215     } catch (const coral::Exception& er) {
0216       std::cout << "database problem " << er.what() << std::endl;
0217       destsession->transaction().rollback();
0218       delete destsession;
0219       delete svc;
0220       throw er;
0221     }
0222     delete destsession;
0223     delete svc;
0224     return 0;
0225   }
0226   const std::string HLTConf2DB::dataType() const { return "HLTConf"; }
0227   const std::string HLTConf2DB::sourceType() const { return "DB"; }
0228   HLTConf2DB::~HLTConf2DB() {}
0229 }  // namespace lumi
0230 #include "RecoLuminosity/LumiProducer/interface/DataPipeFactory.h"
0231 DEFINE_EDM_PLUGIN(lumi::DataPipeFactory, lumi::HLTConf2DB, "HLTConf2DB");
0232 #endif