CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch13/src/RecoLuminosity/LumiProducer/plugins/CMSRunSummary2DB.cc

Go to the documentation of this file.
00001 #ifndef RecoLuminosity_LumiProducer_CMSRunSummary2DB_h 
00002 #define RecoLuminosity_LumiProducer_CMSRunSummary2DB_h 
00003 #include "CoralBase/AttributeList.h"
00004 #include "CoralBase/Attribute.h"
00005 #include "CoralBase/AttributeSpecification.h"
00006 #include "CoralBase/Exception.h"
00007 #include "CoralBase/TimeStamp.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 #include "RecoLuminosity/LumiProducer/interface/DataPipe.h"
00020 #include "RecoLuminosity/LumiProducer/interface/LumiNames.h"
00021 #include "RecoLuminosity/LumiProducer/interface/idDealer.h"
00022 #include "RecoLuminosity/LumiProducer/interface/Exception.h"
00023 #include "RecoLuminosity/LumiProducer/interface/DBConfig.h"
00024 #include "RecoLuminosity/LumiProducer/interface/ConstantDef.h"
00025 #include <iostream>
00026 #include <sstream>
00027 #include "RecoLuminosity/LumiProducer/interface/DataPipe.h"
00028 #include "RecoLuminosity/LumiProducer/interface/LumiNames.h"
00029 #include "RecoLuminosity/LumiProducer/interface/idDealer.h"
00030 #include "RecoLuminosity/LumiProducer/interface/Exception.h"
00031 #include "RecoLuminosity/LumiProducer/interface/DBConfig.h"
00032 #include "RecoLuminosity/LumiProducer/interface/ConstantDef.h"
00033 #include <string>
00034 namespace lumi{
00035   class CMSRunSummary2DB : public DataPipe{
00036   public:
00037     CMSRunSummary2DB( const std::string& dest);
00038     virtual void retrieveData( unsigned int runnumber );
00039     virtual const std::string dataType() const;
00040     virtual const std::string sourceType() const;
00041     unsigned int str2int(const std::string& s) const;
00042     virtual ~CMSRunSummary2DB();
00043   private:
00044     struct cmsrunsum{
00045       std::string sequence;
00046       std::string hltkey;
00047       std::string fillnumber; //convert to number when write into lumi
00048       coral::TimeStamp startT;
00049       coral::TimeStamp stopT;
00050     };
00051   };//cl CMSRunSummary2DB
00052   //
00053   //implementation
00054   //
00055   CMSRunSummary2DB::CMSRunSummary2DB(const std::string& dest):DataPipe(dest){}
00056   void CMSRunSummary2DB::retrieveData( unsigned int runnumber){
00066     cmsrunsum result;
00067     std::string runinfoschema("CMS_RUNINFO");
00068     std::string runsessionParamTable("RUNSESSION_PARAMETER");
00069     coral::ConnectionService* svc=new coral::ConnectionService;
00070     lumi::DBConfig dbconf(*svc);
00071     if(!m_authpath.empty()){
00072       dbconf.setAuthentication(m_authpath);
00073     }
00074  
00075     //std::cout<<"m_source "<<m_source<<std::endl;
00076     coral::ISessionProxy* runinfosession=svc->connect(m_source,coral::ReadOnly);
00077     try{
00078       coral::ITypeConverter& tpc=runinfosession->typeConverter();
00079       tpc.setCppTypeForSqlType("unsigned int","NUMBER(38)");
00080       runinfosession->transaction().start(true);
00081       coral::ISchema& runinfoschemaHandle=runinfosession->schema(runinfoschema);
00082       if(!runinfoschemaHandle.existsTable(runsessionParamTable)){
00083         throw lumi::Exception(std::string("non-existing table "+runsessionParamTable),"CMSRunSummary2DB","retrieveData");
00084       }
00085       coral::IQuery* seqQuery=runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
00086       coral::AttributeList seqBindVariableList;
00087       seqBindVariableList.extend("runnumber",typeid(unsigned int));
00088       seqBindVariableList.extend("name",typeid(std::string));
00089       
00090       seqBindVariableList["runnumber"].data<unsigned int>()=runnumber;
00091       seqBindVariableList["name"].data<std::string>()=std::string("CMS.LVL0:SEQ_NAME");
00092       seqQuery->setCondition("RUNNUMBER =:runnumber AND NAME =:name",seqBindVariableList);
00093       seqQuery->addToOutputList("STRING_VALUE");
00094       coral::ICursor& seqCursor=seqQuery->execute();
00095       
00096       while( seqCursor.next() ){
00097         const coral::AttributeList& row=seqCursor.currentRow();
00098         result.sequence=row["STRING_VALUE"].data<std::string>();
00099       }
00100       delete seqQuery;
00101       
00102       coral::IQuery* hltkeyQuery=runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
00103       coral::AttributeList hltkeyBindVariableList;
00104       hltkeyBindVariableList.extend("runnumber",typeid(unsigned int));
00105       hltkeyBindVariableList.extend("name",typeid(std::string));
00106 
00107       hltkeyBindVariableList["runnumber"].data<unsigned int>()=runnumber;
00108       hltkeyBindVariableList["name"].data<std::string>()=std::string("CMS.LVL0:HLT_KEY_DESCRIPTION");
00109       hltkeyQuery->setCondition("RUNNUMBER =:runnumber AND NAME =:name",hltkeyBindVariableList);
00110       hltkeyQuery->addToOutputList("STRING_VALUE");   
00111       coral::ICursor& hltkeyCursor=hltkeyQuery->execute();
00112       
00113     while( hltkeyCursor.next() ){
00114       const coral::AttributeList& row=hltkeyCursor.currentRow();
00115       result.hltkey=row["STRING_VALUE"].data<std::string>();
00116     }
00117     delete hltkeyQuery;
00118     
00119     coral::IQuery* fillQuery=runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
00120     coral::AttributeList fillBindVariableList;
00121     fillBindVariableList.extend("runnumber",typeid(unsigned int));
00122     fillBindVariableList.extend("name",typeid(std::string));
00123 
00124     fillBindVariableList["runnumber"].data<unsigned int>()=runnumber;
00125     fillBindVariableList["name"].data<std::string>()=std::string("CMS.SCAL:FILLN");
00126     fillQuery->setCondition("RUNNUMBER =:runnumber AND NAME =:name",fillBindVariableList);
00127     fillQuery->addToOutputList("STRING_VALUE"); 
00128     fillQuery->limitReturnedRows(1);
00129     coral::ICursor& fillCursor=fillQuery->execute();
00130     
00131     while( fillCursor.next() ){
00132       const coral::AttributeList& row=fillCursor.currentRow();
00133       result.fillnumber=row["STRING_VALUE"].data<std::string>();
00134     }
00135     delete fillQuery;
00136     if (result.fillnumber.empty()){
00137       throw nonCollisionException("retrieveData","CMSRunSummary2DB");
00138     }
00139     coral::IQuery* startTQuery=runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
00140     coral::AttributeList startTVariableList;
00141     startTVariableList.extend("runnumber",typeid(unsigned int));
00142     startTVariableList.extend("name",typeid(std::string));
00143 
00144     startTVariableList["runnumber"].data<unsigned int>()=runnumber;
00145     startTVariableList["name"].data<std::string>()=std::string("CMS.LVL0:START_TIME_T");
00146     startTQuery->setCondition("RUNNUMBER =:runnumber AND NAME =:name",startTVariableList);
00147     startTQuery->addToOutputList("TIME"); 
00148     coral::ICursor& startTCursor=startTQuery->execute();
00149     
00150     while( startTCursor.next() ){
00151       const coral::AttributeList& row=startTCursor.currentRow();
00152       result.startT=row["TIME"].data<coral::TimeStamp>();
00153     }
00154     delete startTQuery;
00155     coral::IQuery* stopTQuery=runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
00156     coral::AttributeList stopTVariableList;
00157     stopTVariableList.extend("runnumber",typeid(unsigned int));
00158     stopTVariableList.extend("name",typeid(std::string));
00159 
00160     stopTVariableList["runnumber"].data<unsigned int>()=runnumber;
00161     stopTVariableList["name"].data<std::string>()=std::string("CMS.LVL0:STOP_TIME_T");
00162     stopTQuery->setCondition("RUNNUMBER =:runnumber AND NAME =:name",stopTVariableList);
00163     stopTQuery->addToOutputList("TIME"); 
00164     coral::ICursor& stopTCursor=stopTQuery->execute();
00165     
00166     while( stopTCursor.next() ){
00167       const coral::AttributeList& row=stopTCursor.currentRow();
00168       result.stopT=row["TIME"].data<coral::TimeStamp>();
00169     }
00170     delete stopTQuery;
00171     }catch( const coral::Exception& er){
00172       runinfosession->transaction().rollback();
00173       delete runinfosession;
00174       delete svc;
00175       throw er;
00176     }
00177     runinfosession->transaction().commit();
00178     delete runinfosession;
00179     //std::cout<<"result for run "<<runnumber<<" : sequence : "<<result.sequence<<" : "<<result.hltkey<<" : hltkey : "<<result.hltkey<<" : fillnumber : "<<result.fillnumber<<std::endl; 
00180 
00181     //std::cout<<"connecting to dest "<<m_dest<<std::endl; 
00182     coral::ISessionProxy* destsession=svc->connect(m_dest,coral::Update);
00183     
00184     coral::ITypeConverter& desttpc=destsession->typeConverter();
00185     desttpc.setCppTypeForSqlType("unsigned int","NUMBER(10)");
00186     try{
00187       destsession->transaction().start(false);
00188       coral::ISchema& destschema=destsession->nominalSchema();
00189       coral::ITable& destruntable=destschema.tableHandle(LumiNames::cmsrunsummaryTableName());
00190       coral::AttributeList runData;
00191       destruntable.dataEditor().rowBuffer(runData);
00192       runData["RUNNUM"].data<unsigned int>()=runnumber;
00193       runData["FILLNUM"].data<unsigned int>()=str2int(result.fillnumber);
00194       runData["SEQUENCE"].data<std::string>()=result.sequence;
00195       runData["HLTKEY"].data<std::string>()=result.hltkey;
00196       runData["STARTTIME"].data<coral::TimeStamp>()=result.startT;
00197       runData["STOPTIME"].data<coral::TimeStamp>()=result.stopT;
00198       destruntable.dataEditor().insertRow(runData);
00199     }catch( const coral::Exception& er){
00200       std::cout<<"database problem "<<er.what()<<std::endl;
00201       destsession->transaction().rollback();
00202       delete destsession;
00203       delete svc;
00204       throw er;
00205     }
00206     destsession->transaction().commit();
00207     delete svc;
00208   }
00209   const std::string CMSRunSummary2DB::dataType() const{
00210     return "CMSRUNSUMMARY";
00211   }
00212   const std::string CMSRunSummary2DB::sourceType() const{
00213     return "DB";
00214   }
00215   unsigned int CMSRunSummary2DB::str2int(const std::string& s)  const{
00216     std::istringstream myStream(s);
00217     unsigned int i;
00218     if(myStream>>i){
00219       return i;
00220     }else{
00221       throw lumi::Exception(std::string("str2int error"),"str2int","CMSRunSummary2DB");
00222     }
00223   }
00224   CMSRunSummary2DB::~CMSRunSummary2DB(){}
00225 }//ns lumi
00226 #include "RecoLuminosity/LumiProducer/interface/DataPipeFactory.h"
00227 DEFINE_EDM_PLUGIN(lumi::DataPipeFactory,lumi::CMSRunSummary2DB,"CMSRunSummary2DB");
00228 #endif