CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/src/RecoLuminosity/LumiProducer/plugins/HLT2DB.cc

Go to the documentation of this file.
00001 #ifndef RecoLuminosity_LumiProducer_HLT2DB_H 
00002 #define RecoLuminosity_LumiProducer_HLT2DB_H
00003 #include "CoralBase/AttributeList.h"
00004 #include "CoralBase/Attribute.h"
00005 #include "CoralBase/AttributeSpecification.h"
00006 #include "CoralBase/Exception.h"
00007 #include "RelationalAccess/ConnectionService.h"
00008 #include "RelationalAccess/ISessionProxy.h"
00009 #include "RelationalAccess/ITransaction.h"
00010 #include "RelationalAccess/ITypeConverter.h"
00011 #include "RelationalAccess/IQuery.h"
00012 #include "RelationalAccess/ICursor.h"
00013 #include "RelationalAccess/ISchema.h"
00014 #include "RelationalAccess/IView.h"
00015 #include "RelationalAccess/ITable.h"
00016 #include "RelationalAccess/ITableDataEditor.h"
00017 #include "RelationalAccess/IBulkOperation.h"
00018 
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 <map>
00027 #include <vector>
00028 #include <string>
00029 
00030 #include "RecoLuminosity/LumiProducer/interface/DataPipe.h"
00031 namespace lumi{
00032   class HLT2DB : public DataPipe{
00033     
00034   public:
00035     const static unsigned int COMMITLSINTERVAL=200; //commit interval in LS,totalrow=nls*(~200)
00036     explicit HLT2DB(const std::string& dest);
00037     virtual void retrieveData( unsigned int );
00038     virtual const std::string dataType() const;
00039     virtual const std::string sourceType() const;
00040     virtual ~HLT2DB();
00041 
00042     struct hltinfo{
00043       unsigned int cmsluminr;
00044       std::string pathname;
00045       unsigned int hltinput;
00046       unsigned int hltaccept;
00047       unsigned int prescale;
00048       unsigned int hltconfigid;
00049     };
00050   };//cl HLT2DB
00051   //
00052   //implementation
00053   //
00054   HLT2DB::HLT2DB(const std::string& dest):DataPipe(dest){}
00055   void HLT2DB::retrieveData( unsigned int runnumber){
00056     
00057     std::string hltschema("CMS_RUNINFO");
00058     std::string tabname("HLT_SUPERVISOR_LUMISECTIONS_V2");
00059     std::string maptabname("HLT_SUPERVISOR_SCALAR_MAP");
00060     
00061     coral::ConnectionService* svc=new coral::ConnectionService;
00062     lumi::DBConfig dbconf(*svc);
00063     if(!m_authpath.empty()){
00064       dbconf.setAuthentication(m_authpath);
00065     }
00071     //std::cout<<"m_source "<<m_source<<std::endl;
00072     coral::ISessionProxy* srcsession=svc->connect(m_source, coral::ReadOnly);
00073     coral::ITypeConverter& tpc=srcsession->typeConverter();
00074     tpc.setCppTypeForSqlType("unsigned int","NUMBER(11)");
00075     srcsession->transaction().start(true);
00076     coral::ISchema& hltSchemaHandle=srcsession->schema(hltschema);
00077     if( !hltSchemaHandle.existsTable(tabname) || !hltSchemaHandle.existsTable(maptabname) ){
00078       throw lumi::Exception("missing hlt tables" ,"retrieveData","HLT2DB");
00079     }
00080     std::vector< std::vector<HLT2DB::hltinfo> > hltresult;
00081     coral::AttributeList bindVariableList;
00082     bindVariableList.extend("runnumber",typeid(unsigned int));
00083     bindVariableList.extend("lsnumber",typeid(unsigned int));
00084     bindVariableList["runnumber"].data<unsigned int>()=runnumber;
00085     bindVariableList["lsnumber"].data<unsigned int>()=1;
00086     //coral::IQuery* q1=srcsession->nominalSchema().tableHandle(tabname).newQuery();
00087     //coral::AttributeList nls;
00088     //nls.extend("npath",typeid(unsigned int));
00089     //q1->addToOutputList("count(distinct PATHNAME)","npath");
00090     //q1->setCondition("RUNNR =:runnumber AND LSNUMBER =:lsnumber",bindVariableList);
00091     //q1->defineOutput(nls);
00092     //coral::ICursor& c=q1->execute();
00093     //if( !c.next() ){
00094     //c.close();
00095     // delete q1;
00096     //throw lumi::Exception("request run doen't exist","retrieveData","HLT2DB");
00097     //}else{
00098     //npath=c.currentRow()["npath"].data<unsigned int>();
00099     // c.close();
00100     //delete q1;
00101     //if(npath==0){
00102     //std::cout<<"request run is empty, do nothing"<<std::endl;
00103     //return;
00104     //}
00105     //}
00106     //std::cout<<"npath "<<npath<<std::endl;
00107     unsigned int npath=0;
00108     coral::IQuery* q2=srcsession->nominalSchema().newQuery();
00109     coral::AttributeList q2bindVariableList;
00110     q2bindVariableList.extend("runnumber",typeid(unsigned int));
00111     q2bindVariableList["runnumber"].data<unsigned int>()=runnumber;
00112     q2->addToTableList(tabname,"l");
00113     q2->addToTableList(maptabname,"m");
00114     q2->addToOutputList("l.LSNUMBER","lsnumber");
00115     q2->addToOutputList("l.PATHNAME","pathname");
00116     q2->addToOutputList("l.L1PASS","hltinput");
00117     q2->addToOutputList("l.PACCEPT","hltratecounter");
00118     q2->addToOutputList("m.PSVALUE","prescale");
00119     q2->addToOutputList("m.HLTKEY","hltconfigid");
00120     q2->setCondition("l.RUNNR=m.RUNNR and l.PSINDEX=m.PSINDEX and l.PATHNAME=m.PATHNAME and l.RUNNR =:runnumber",q2bindVariableList);   
00121     q2->addToOrderList("lsnumber");
00122     q2->setRowCacheSize(10692);
00123     coral::ICursor& cursor2=q2->execute();
00124     //unsigned int currentPath=0;
00125     unsigned int lastLumiSection=1;
00126     unsigned int currentLumiSection=0;
00127     // unsigned int counter=0;
00128     std::vector<hltinfo> allpaths;
00129     allpaths.reserve(200);
00130     while( cursor2.next() ){
00131       hltinfo pathcontent;
00132       const coral::AttributeList& row=cursor2.currentRow();
00133       currentLumiSection=row["lsnumber"].data<unsigned int>();
00134       pathcontent.cmsluminr=currentLumiSection;
00135       pathcontent.hltinput=row["hltinput"].data<unsigned int>();
00136       pathcontent.hltaccept=row["hltratecounter"].data<unsigned int>();
00137       pathcontent.pathname=row["pathname"].data<std::string>();
00138       pathcontent.prescale=row["prescale"].data<unsigned int>();
00139       pathcontent.hltconfigid=row["hltconfigid"].data<unsigned int>();
00140       if(currentLumiSection != lastLumiSection){
00141         hltresult.push_back(allpaths);
00142         //npath=allpaths.size();
00143         allpaths.clear();
00144       }
00145       lastLumiSection=currentLumiSection;
00146       allpaths.push_back(pathcontent);
00147       npath=allpaths.size();
00148     }
00149     hltresult.push_back(allpaths);
00150     cursor2.close();
00151     delete q2;
00152     srcsession->transaction().commit();
00153     delete srcsession;
00154     
00155     //
00156     // Write into DB
00157     //
00158     unsigned int totalcmsls=hltresult.size();
00159     std::cout<<"inserting totalhltls "<<totalcmsls<<std::endl;
00160     std::map< unsigned long long, std::vector<unsigned long long> > idallocationtable;
00161     coral::ISessionProxy* destsession=svc->connect(m_dest,coral::Update);
00162     coral::ITypeConverter& lumitpc=destsession->typeConverter();
00163     lumitpc.setCppTypeForSqlType("unsigned int","NUMBER(7)");
00164     lumitpc.setCppTypeForSqlType("unsigned int","NUMBER(10)");
00165     lumitpc.setCppTypeForSqlType("unsigned long long","NUMBER(20)");
00166 
00167     std::vector< std::vector<HLT2DB::hltinfo> >::const_iterator hltIt;
00168     std::vector< std::vector<HLT2DB::hltinfo> >::const_iterator hltBeg=hltresult.begin();
00169     std::vector< std::vector<HLT2DB::hltinfo> >::const_iterator hltEnd=hltresult.end();
00170     
00171     try{
00172        std::cout<<"\t allocating total ids "<<totalcmsls*npath<<std::endl; 
00173       destsession->transaction().start(false);
00174       lumi::idDealer idg(destsession->nominalSchema());
00175       unsigned long long hltID = idg.generateNextIDForTable(LumiNames::hltTableName(),totalcmsls*npath)-totalcmsls*npath;
00176       destsession->transaction().commit();
00177       unsigned int hltlscount=0;
00178       for(hltIt=hltBeg;hltIt!=hltEnd;++hltIt,++hltlscount){
00179         std::vector<unsigned long long> pathvec;
00180         pathvec.reserve(200);
00181         std::vector<HLT2DB::hltinfo>::const_iterator pathIt;
00182         std::vector<HLT2DB::hltinfo>::const_iterator pathBeg=hltIt->begin();
00183         std::vector<HLT2DB::hltinfo>::const_iterator pathEnd=hltIt->end();
00184         for(pathIt=pathBeg;pathIt!=pathEnd;++pathIt,++hltID){
00185           pathvec.push_back(hltID);
00186         }
00187         idallocationtable.insert(std::make_pair(hltlscount,pathvec));
00188       }
00189       std::cout<<"\t all ids allocated"<<std::endl; 
00190 
00191       coral::AttributeList hltData;
00192       hltData.extend("HLT_ID",typeid(unsigned long long));
00193       hltData.extend("RUNNUM",typeid(unsigned int));
00194       hltData.extend("CMSLSNUM",typeid(unsigned int));
00195       hltData.extend("PATHNAME",typeid(std::string));
00196       hltData.extend("INPUTCOUNT",typeid(unsigned int));
00197       hltData.extend("ACCEPTCOUNT",typeid(unsigned int));
00198       hltData.extend("PRESCALE",typeid(unsigned int));
00199       
00200       //loop over lumi LS
00201       unsigned long long& hlt_id=hltData["HLT_ID"].data<unsigned long long>();
00202       unsigned int& hltrunnum=hltData["RUNNUM"].data<unsigned int>();
00203       unsigned int& cmslsnum=hltData["CMSLSNUM"].data<unsigned int>();
00204       std::string& pathname=hltData["PATHNAME"].data<std::string>();
00205       unsigned int& inputcount=hltData["INPUTCOUNT"].data<unsigned int>();
00206       unsigned int& acceptcount=hltData["ACCEPTCOUNT"].data<unsigned int>();
00207       unsigned int& prescale=hltData["PRESCALE"].data<unsigned int>();
00208       hltlscount=0;
00209       coral::IBulkOperation* hltInserter=0; 
00210       unsigned int comittedls=0;
00211       for(hltIt=hltBeg;hltIt!=hltEnd;++hltIt,++hltlscount){
00212         std::vector<HLT2DB::hltinfo>::const_iterator pathIt;
00213         std::vector<HLT2DB::hltinfo>::const_iterator pathBeg=hltIt->begin();
00214         std::vector<HLT2DB::hltinfo>::const_iterator pathEnd=hltIt->end();
00215         if(!destsession->transaction().isActive()){ 
00216           destsession->transaction().start(false);
00217           coral::ITable& hlttable=destsession->nominalSchema().tableHandle(LumiNames::hltTableName());
00218           hltInserter=hlttable.dataEditor().bulkInsert(hltData,200);
00219         }
00220         unsigned int hltpathcount=0;
00221         for(pathIt=pathBeg;pathIt!=pathEnd;++pathIt,++hltpathcount){
00222           hlt_id = idallocationtable[hltlscount].at(hltpathcount);
00223           hltrunnum = runnumber;
00224           cmslsnum = pathIt->cmsluminr;
00225           pathname = pathIt->pathname;
00226           inputcount = pathIt->hltinput;
00227           acceptcount = pathIt->hltaccept;
00228           prescale = pathIt->prescale;
00229           hltInserter->processNextIteration();
00230         }
00231         hltInserter->flush();
00232         ++comittedls;
00233         if(comittedls==HLT2DB::COMMITLSINTERVAL){
00234           std::cout<<"\t committing in LS chunck "<<comittedls<<std::endl; 
00235           delete hltInserter; hltInserter=0;
00236           destsession->transaction().commit();
00237           comittedls=0;
00238           std::cout<<"\t committed "<<std::endl; 
00239         }else if( hltlscount==(totalcmsls-1) ){
00240           std::cout<<"\t committing at the end"<<std::endl; 
00241           delete hltInserter; hltInserter=0;
00242           destsession->transaction().commit();
00243           std::cout<<"\t done"<<std::endl; 
00244         }
00245       }
00246     }catch( const coral::Exception& er){
00247       std::cout<<"database problem "<<er.what()<<std::endl;
00248       destsession->transaction().rollback();
00249       delete destsession;
00250       delete svc;
00251       throw er;
00252     }
00253     delete destsession;
00254     delete svc;
00255   }
00256   const std::string HLT2DB::dataType() const{
00257     return "HLT";
00258   }
00259   const std::string HLT2DB::sourceType() const{
00260     return "DB";
00261   }
00262   HLT2DB::~HLT2DB(){}
00263 }//ns lumi
00264 #include "RecoLuminosity/LumiProducer/interface/DataPipeFactory.h"
00265 DEFINE_EDM_PLUGIN(lumi::DataPipeFactory,lumi::HLT2DB,"HLT2DB");
00266 #endif