CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_7_hltpatch1/src/RecoLuminosity/LumiProducer/plugins/HLTConf2DB.cc

Go to the documentation of this file.
00001 #ifndef RecoLuminosity_LumiProducer_HLTConf2DB_H 
00002 #define RecoLuminosity_LumiProducer_HLTConf2DB_H 
00003 
00004 #include "CoralBase/AttributeList.h"
00005 #include "CoralBase/Attribute.h"
00006 #include "CoralBase/AttributeSpecification.h"
00007 #include "CoralBase/Exception.h"
00008 #include "RelationalAccess/ConnectionService.h"
00009 #include "RelationalAccess/ISessionProxy.h"
00010 #include "RelationalAccess/ITransaction.h"
00011 #include "RelationalAccess/ITypeConverter.h"
00012 #include "RelationalAccess/IQuery.h"
00013 #include "RelationalAccess/ICursor.h"
00014 #include "RelationalAccess/ISchema.h"
00015 #include "RelationalAccess/IView.h"
00016 #include "RelationalAccess/ITable.h"
00017 #include "RelationalAccess/ITableDataEditor.h"
00018 #include "RelationalAccess/IBulkOperation.h"
00019 
00020 #include "RecoLuminosity/LumiProducer/interface/DataPipe.h"
00021 #include "RecoLuminosity/LumiProducer/interface/LumiNames.h"
00022 #include "RecoLuminosity/LumiProducer/interface/idDealer.h"
00023 #include "RecoLuminosity/LumiProducer/interface/Exception.h"
00024 #include "RecoLuminosity/LumiProducer/interface/DBConfig.h"
00025 #include "RecoLuminosity/LumiProducer/interface/ConstantDef.h"
00026 #include <iostream>
00027 #include <map>
00028 #include <vector>
00029 #include <string>
00030 namespace lumi{
00031   class HLTConf2DB : public DataPipe{
00032   public:
00033     explicit HLTConf2DB( const std::string& dest);
00034     virtual void retrieveData( unsigned int );
00035     virtual const std::string dataType() const;
00036     virtual const std::string sourceType() const;
00037     virtual ~HLTConf2DB();
00038   };//cl HLTConf2DB
00039   //
00040   //implementation
00041   //
00042   HLTConf2DB::HLTConf2DB( const std::string& dest):DataPipe(dest){}
00043   void HLTConf2DB::retrieveData( unsigned int runnumber ){
00044     std::string runinfoschema("CMS_RUNINFO");
00045     //std::string hltschema("CMS_HLT_V0");
00046     std::string hltschema("CMS_HLT");
00047     std::string runsessiontable("RUNSESSION_PARAMETER");
00048     //
00049     //must login as cms_hlt_r
00050     coral::ConnectionService* svc=new coral::ConnectionService;
00051     lumi::DBConfig dbconf(*svc);
00052     if(!m_authpath.empty()){
00053       dbconf.setAuthentication(m_authpath);
00054     }
00055     //std::cout<<"m_source "<<m_source<<std::endl;
00056     coral::ISessionProxy* srcsession=svc->connect(m_source, coral::ReadOnly);
00057     coral::ITypeConverter& tpc=srcsession->typeConverter();
00058     tpc.setCppTypeForSqlType("unsigned int","NUMBER(38)");
00059     //
00060     //select string_value from CMS_RUNINFO.runsession_parameter where name='CMS.LVL0:HLT_KEY_DESCRIPTION' and runnumber=xx;
00061     //
00062     std::string hltkey("");
00063     std::vector< std::pair<std::string,std::string> > hlt2l1map;
00064     hlt2l1map.reserve(200);
00065     try{
00066       srcsession->transaction().start(true);
00067       coral::ISchema& runinfoSchemaHandle=srcsession->schema(runinfoschema);
00068       if( !runinfoSchemaHandle.existsTable(runsessiontable) ){
00069         throw lumi::Exception("missing runsession table","retrieveData","HLTConf2DB");
00070       }
00071       coral::AttributeList rfBindVariableList;
00072       rfBindVariableList.extend("name",typeid(std::string));
00073       rfBindVariableList.extend("runnumber",typeid(unsigned int));
00074       rfBindVariableList["name"].data<std::string>()=std::string("CMS.LVL0:HLT_KEY_DESCRIPTION");
00075       rfBindVariableList["runnumber"].data<unsigned int>()=runnumber;
00076       coral::IQuery* kq=runinfoSchemaHandle.newQuery();
00077       coral::AttributeList runinfoOut;
00078       runinfoOut.extend("STRING_VALUE",typeid(std::string));
00079       kq->addToTableList(runsessiontable);
00080       kq->setCondition("NAME = :name AND RUNNUMBER = :runnumber",rfBindVariableList);
00081       kq->addToOutputList("STRING_VALUE");
00082       kq->defineOutput(runinfoOut);
00083       coral::ICursor& kcursor=kq->execute();
00084       unsigned int s=0;
00085       while( kcursor.next() ){
00086         const coral::AttributeList& row=kcursor.currentRow();
00087         hltkey=row["STRING_VALUE"].data<std::string>();
00088         ++s;
00089       }
00090       if( s==0||hltkey.empty() ){
00091         kcursor.close();
00092         delete kq;
00093         srcsession->transaction().rollback();
00094         throw lumi::Exception(std::string("requested run has no or invalid hltkey"),"retrieveData","TRG2DB");
00095       }
00096       if(s>1){
00097         kcursor.close();
00098         delete kq;
00099         srcsession->transaction().rollback();
00100         throw lumi::Exception(std::string("requested run has multile hltkey"),"retrieveData","TRG2DB");
00101       }
00102     }catch( const coral::Exception& er ){
00103       std::cout<<"source runinfo database problem "<<er.what()<<std::endl;
00104       srcsession->transaction().rollback();
00105       delete srcsession;
00106       delete svc;
00107       throw er;
00108     }
00109     coral::ISchema& confSchemaHandle=srcsession->nominalSchema();
00110     try{
00111       //srcsession->transaction().start(true);
00112       if( !confSchemaHandle.existsTable("PATHS") || !confSchemaHandle.existsTable("STRINGPARAMVALUES") || !confSchemaHandle.existsTable("PARAMETERS") || !confSchemaHandle.existsTable("SUPERIDPARAMETERASSOC") || !confSchemaHandle.existsTable("MODULES") || !confSchemaHandle.existsTable("MODULETEMPLATES") || !confSchemaHandle.existsTable("PATHMODULEASSOC") || !confSchemaHandle.existsTable("CONFIGURATIONPATHASSOC") || !confSchemaHandle.existsTable("CONFIGURATIONS") ){
00113         throw lumi::Exception("missing hlt conf tables" ,"retrieveData","HLTConf2DB");
00114       }
00115       coral::AttributeList q1BindVariableList;
00116       q1BindVariableList.extend("hltseed",typeid(std::string));
00117       q1BindVariableList.extend("l1seedexpr",typeid(std::string));
00118       q1BindVariableList.extend("hltkey",typeid(std::string));
00119       q1BindVariableList["hltseed"].data<std::string>()=std::string("HLTLevel1GTSeed");
00120       q1BindVariableList["l1seedexpr"].data<std::string>()=std::string("L1SeedsLogicalExpression");
00121       q1BindVariableList["hltkey"].data<std::string>()=hltkey;    
00122       coral::IQuery* q1=confSchemaHandle.newQuery();
00123       q1->addToOutputList("PATHS.NAME","hltpath");
00124       q1->addToOutputList("STRINGPARAMVALUES.VALUE","l1expression");
00125       
00126       q1->addToTableList("PATHS");    
00127       q1->addToTableList("STRINGPARAMVALUES");
00128       q1->addToTableList("PARAMETERS");
00129       q1->addToTableList("SUPERIDPARAMETERASSOC");
00130       q1->addToTableList("MODULES");
00131       q1->addToTableList("MODULETEMPLATES");
00132       q1->addToTableList("PATHMODULEASSOC");
00133       q1->addToTableList("CONFIGURATIONPATHASSOC");
00134       q1->addToTableList("CONFIGURATIONS");
00135       
00136       q1->setCondition("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 = :hltseed and PARAMETERS.NAME = :l1seedexpr and CONFIGURATIONS.CONFIGDESCRIPTOR = :hltkey",q1BindVariableList);
00137 
00141       coral::ICursor& cursor1=q1->execute();
00142       while( cursor1.next() ){
00143         const coral::AttributeList& row=cursor1.currentRow();
00144         std::string hltpath=row["hltpath"].data<std::string>();
00145         std::string l1expression=row["l1expression"].data<std::string>();
00146         hlt2l1map.push_back(std::make_pair(hltpath,l1expression));
00147       }
00148       cursor1.close();
00149       delete q1;
00150     }catch( const coral::Exception& er ){
00151       std::cout<<"database problem with source hlt confdb"<<er.what()<<std::endl;
00152       srcsession->transaction().rollback();
00153       delete srcsession;
00154       throw er;
00155     }
00156     srcsession->transaction().commit();
00157     delete srcsession;
00158     std::vector< std::pair<std::string,std::string> >::const_iterator mIt;
00159     std::vector< std::pair<std::string,std::string> >::const_iterator mBeg=hlt2l1map.begin();
00160     std::vector< std::pair<std::string,std::string> >::const_iterator mEnd=hlt2l1map.end();
00161     
00162     coral::ISessionProxy* destsession=svc->connect(m_dest, coral::Update);
00163     try{      
00164       //check if hltkey already exists
00165       destsession->transaction().start(true);
00166       bool hltkeyExists=false;
00167       coral::AttributeList kQueryBindList;
00168       kQueryBindList.extend("hltkey",typeid(std::string));
00169       coral::IQuery* kQuery=destsession->nominalSchema().tableHandle(LumiNames::trghltMapTableName()).newQuery();
00170       kQuery->setCondition("HLTKEY =:hltkey",kQueryBindList);
00171       kQueryBindList["hltkey"].data<std::string>()=hltkey;
00172       coral::ICursor& kResult=kQuery->execute();
00173       while( kResult.next() ){
00174         hltkeyExists=true;
00175       }
00176       if(hltkeyExists){
00177         std::cout<<"hltkey "<<hltkey<<" already registered , do nothing"<<std::endl;
00178         destsession->transaction().commit();
00179         delete kQuery;
00180         delete svc;
00181         return;
00182       }
00183       destsession->transaction().commit();
00184       destsession->transaction().start(false);
00185       coral::ISchema& destschema=destsession->nominalSchema();
00186       coral::ITable& hltconftable=destschema.tableHandle(LumiNames::trghltMapTableName());
00187       coral::AttributeList hltconfData;
00188       hltconfData.extend<std::string>("HLTKEY");
00189       hltconfData.extend<std::string>("HLTPATHNAME");
00190       hltconfData.extend<std::string>("L1SEED");
00191       coral::IBulkOperation* hltconfInserter=hltconftable.dataEditor().bulkInsert(hltconfData,200);
00192       hltconfData["HLTKEY"].data<std::string>()=hltkey;
00193       std::string& hltpathname=hltconfData["HLTPATHNAME"].data<std::string>();
00194       std::string& l1seedname=hltconfData["L1SEED"].data<std::string>();
00195       for(mIt=mBeg; mIt!=mEnd; ++mIt ){
00196         hltpathname=mIt->first;
00197         l1seedname=mIt->second;
00198         hltconfInserter->processNextIteration();
00199         //std::cout<<mIt->first<<" "<<mIt->second<<std::endl;      
00200       }
00201       hltconfInserter->flush();
00202       delete hltconfInserter;
00203       destsession->transaction().commit();
00204     }catch( const coral::Exception& er){
00205       std::cout<<"database problem "<<er.what()<<std::endl;
00206       destsession->transaction().rollback();
00207       delete destsession;
00208       delete svc;
00209       throw er;
00210     }
00211     delete destsession;
00212     delete svc;
00213   }
00214   const std::string HLTConf2DB::dataType() const{
00215     return "HLTConf";
00216   }
00217   const std::string HLTConf2DB::sourceType() const{
00218     return "DB";
00219   }
00220   HLTConf2DB::~HLTConf2DB(){}
00221 }//ns lumi
00222 #include "RecoLuminosity/LumiProducer/interface/DataPipeFactory.h"
00223 DEFINE_EDM_PLUGIN(lumi::DataPipeFactory,lumi::HLTConf2DB,"HLTConf2DB");
00224 #endif