CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_13_patch3/src/RecoLuminosity/LumiProducer/plugins/LumiProducer.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:    LumiProducer
00004 // Class:      LumiProducer
00005 // 
00016 //
00017 // Original Author:  Valerie Halyo
00018 //                   David Dagenhart
00019 //       
00020 //         Created:  Tue Jun 12 00:47:28 CEST 2007
00021 // $Id: LumiProducer.cc,v 1.26.2.2 2012/10/23 14:21:21 xiezhen Exp $
00022 
00023 #include "FWCore/Framework/interface/EDProducer.h"
00024 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00025 #include "FWCore/Framework/interface/Event.h"
00026 #include "FWCore/Framework/interface/LuminosityBlock.h"
00027 #include "FWCore/Framework/interface/Run.h"
00028 #include "DataFormats/Provenance/interface/BranchType.h"
00029 #include "DataFormats/Provenance/interface/LuminosityBlockID.h"
00030 #include "DataFormats/Luminosity/interface/LumiSummaryRunHeader.h"
00031 #include "DataFormats/Luminosity/interface/LumiSummary.h"
00032 #include "DataFormats/Luminosity/interface/LumiDetails.h"
00033 #include "FWCore/ServiceRegistry/interface/Service.h"
00034 #include "FWCore/Framework/interface/EventSetup.h"
00035 
00036 #include "CoralBase/Exception.h"
00037 #include "CoralBase/AttributeList.h"
00038 #include "CoralBase/Attribute.h"
00039 #include "CoralBase/AttributeSpecification.h"
00040 #include "CoralBase/Exception.h"
00041 #include "CoralBase/Blob.h"
00042 #include "RelationalAccess/ISessionProxy.h"
00043 #include "RelationalAccess/ITransaction.h"
00044 #include "RelationalAccess/AccessMode.h"
00045 #include "RelationalAccess/ITypeConverter.h"
00046 #include "RelationalAccess/IQuery.h"
00047 #include "RelationalAccess/ICursor.h"
00048 #include "RelationalAccess/ISchema.h"
00049 #include "RelationalAccess/ITable.h"
00050 
00051 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00052 
00053 #include "RecoLuminosity/LumiProducer/interface/DBService.h"
00054 #include "RecoLuminosity/LumiProducer/interface/LumiNames.h"
00055 #include "RecoLuminosity/LumiProducer/interface/Exception.h"
00056 #include "RecoLuminosity/LumiProducer/interface/ConstantDef.h"
00057 #include <iostream>
00058 #include <sstream>
00059 #include <string>
00060 #include <memory>
00061 #include <algorithm>
00062 #include <vector>
00063 #include <cstring>
00064 #include <iterator>
00065 #include <boost/foreach.hpp>
00066 #include <boost/tokenizer.hpp>
00067 #include <xercesc/dom/DOM.hpp>
00068 #include <xercesc/parsers/XercesDOMParser.hpp>
00069 #include <xercesc/util/PlatformUtils.hpp>
00070 #include <xercesc/util/XMLString.hpp>
00071 
00072 #include "boost/filesystem/path.hpp"
00073 #include "boost/filesystem/operations.hpp"
00074 
00075 namespace edm {
00076   class EventSetup;
00077 }
00078 
00079 //
00080 // class declaration
00081 //
00082 class LumiProducer : public edm::EDProducer {
00083 
00084 public:
00085 
00086   struct HLTData{
00087     std::string pathname;
00088     unsigned int prescale;
00089     unsigned int l1passcount;
00090     unsigned int acceptcount;
00091   };
00092   struct L1Data{
00093     std::string bitname;
00094     unsigned int prescale;
00095     unsigned int ratecount;
00096   };
00097   struct PerRunData{
00098     std::string bitzeroname;//norm bit name
00099     std::map<std::string, unsigned int> TRGBitNameToIndex;
00100     std::map<std::string, unsigned int> HLTPathNameToIndex;
00101     std::vector<std::string>            TRGBitNames;
00102     std::vector<std::string>            HLTPathNames;
00103   };
00104   struct PerLSData{
00105     float lumivalue;
00106     float lumierror;
00107     short lumiquality;
00108     unsigned long long deadcount;
00109     unsigned int numorbit;
00110     unsigned int startorbit;
00111     unsigned int bitzerocount;
00112     unsigned int bitzeroprescale;
00113     std::vector< HLTData > hltdata;
00114     std::vector< L1Data > l1data;
00115     std::vector< std::pair<std::string, std::vector<float> > > bunchlumivalue;
00116     std::vector< std::pair<std::string, std::vector<float> > > bunchlumierror;
00117     std::vector< std::pair<std::string, std::vector<short> > > bunchlumiquality;
00118     std::vector<float> beam1intensity;
00119     std::vector<float> beam2intensity;
00120   };
00121 
00122   explicit LumiProducer(const edm::ParameterSet&);
00123 
00124   ~LumiProducer();
00125   
00126 private:
00127   
00128   virtual void produce(edm::Event&, const edm::EventSetup&);
00129 
00130   virtual void beginRun(edm::Run&, edm::EventSetup const &);
00131 
00132   virtual void beginLuminosityBlock(edm::LuminosityBlock & iLBlock,
00133                                     edm::EventSetup const& iSetup);
00134   virtual void endLuminosityBlock(edm::LuminosityBlock& lumiBlock, 
00135                                   edm::EventSetup const& c);
00136 
00137   virtual void endRun(edm::Run&, edm::EventSetup const &);
00138 
00139   bool fillLumi(edm::LuminosityBlock & iLBlock);
00140   void fillRunCache(const coral::ISchema& schema,unsigned int runnumber);
00141   void fillLSCache(unsigned int luminum);
00142   void writeProductsForEntry(edm::LuminosityBlock & iLBlock,unsigned int runnumber,unsigned int luminum);
00143   const std::string servletTranslation(const std::string& servlet) const;
00144   std::string x2s(const XMLCh* input)const;
00145   XMLCh* s2x(const std::string& input)const;
00146   std::string toParentString(const xercesc::DOMNode &nodeToConvert)const;
00147   unsigned long long getLumiDataId(const coral::ISchema& schema,unsigned int runnumber);
00148   unsigned long long getTrgDataId(const coral::ISchema& schema,unsigned int runnumber);
00149   unsigned long long getHltDataId(const coral::ISchema& schema,unsigned int runnumber);
00150 
00151   std::string m_connectStr;
00152   std::string m_lumiversion;
00153   std::string m_siteconfpath;
00154   unsigned int m_cachedrun;
00155   unsigned long long m_cachedlumidataid;
00156   unsigned long long m_cachedtrgdataid;
00157   unsigned long long m_cachedhltdataid;
00158   PerRunData  m_runcache;
00159   std::map< unsigned int,PerLSData > m_lscache;
00160   bool m_isNullRun;
00161   unsigned int m_cachesize;
00162 };
00163 
00164 //
00165 // constructors and destructor
00166 //
00167 
00168 std::string 
00169 LumiProducer::x2s(const XMLCh *toTranscode)const{
00170   std::string tmp(xercesc::XMLString::transcode(toTranscode));
00171   return tmp;
00172 }
00173 
00174 XMLCh*  
00175 LumiProducer::s2x( const std::string& temp )const{
00176   XMLCh* buff = xercesc::XMLString::transcode(temp.c_str());    
00177   return  buff;
00178 }
00179 
00180 std::string
00181 LumiProducer::toParentString(const xercesc::DOMNode &nodeToConvert)const{
00182   std::ostringstream oss;
00183   xercesc::DOMNodeList *childList = nodeToConvert.getChildNodes();
00184 
00185   unsigned int numNodes = childList->getLength ();
00186   for (unsigned int i = 0; i < numNodes; ++i){
00187     xercesc::DOMNode *childNode = childList->item(i);
00188     if (childNode->getNodeType() != xercesc::DOMNode::ELEMENT_NODE)
00189       continue;
00190     xercesc::DOMElement *child = static_cast < xercesc::DOMElement *> (childNode);
00191     xercesc::DOMNamedNodeMap *attributes = child->getAttributes();
00192     unsigned int numAttributes = attributes->getLength ();
00193     for (unsigned int j = 0; j < numAttributes; ++j){
00194       xercesc::DOMNode *attributeNode = attributes->item(j);
00195       if (attributeNode->getNodeType() != xercesc::DOMNode::ATTRIBUTE_NODE)
00196         continue;
00197       xercesc::DOMAttr *attribute = static_cast <xercesc::DOMAttr *> (attributeNode);
00198       
00199       oss << "(" << x2s(child->getTagName()) << 
00200         x2s(attribute->getName()) << "=" << 
00201         x2s(attribute->getValue()) << ")";
00202     }
00203   }
00204   return oss.str();
00205 }
00206 
00207 const std::string
00208 LumiProducer::servletTranslation(const std::string& servlet) const{
00209   std::string frontierConnect;
00210   std::string realconnect;
00211   xercesc::XMLPlatformUtils::Initialize();  
00212   std::auto_ptr< xercesc::XercesDOMParser > parser(new xercesc::XercesDOMParser);
00213   try{
00214     parser->setValidationScheme(xercesc::XercesDOMParser::Val_Auto);
00215     parser->setDoNamespaces(false);
00216     parser->parse(m_siteconfpath.c_str());
00217     xercesc::DOMDocument* doc=parser->getDocument();
00218     if(!doc){
00219       return "";
00220     }
00221    
00222     xercesc::DOMNodeList *frontierConnectList=doc->getElementsByTagName(s2x("frontier-connect"));
00223     if (frontierConnectList->getLength()>0){
00224       xercesc::DOMElement *frontierConnectElement=static_cast < xercesc::DOMElement *> (frontierConnectList->item (0));
00225       frontierConnect = toParentString(*frontierConnectElement);
00226     }
00227     // Replace the last component of every "serverurl=" piece (up to the
00228     //   next close-paren) with the servlet
00229     std::string::size_type nextparen = 0;
00230     std::string::size_type serverurl, lastslash;
00231     std::string complexstr = "";
00232     while ((serverurl=frontierConnect.find("(serverurl=", nextparen)) != std::string::npos){
00233       realconnect.append(frontierConnect, nextparen, serverurl - nextparen);
00234       nextparen=frontierConnect.find(')', serverurl);
00235       lastslash=frontierConnect.rfind('/', nextparen);
00236       realconnect.append(frontierConnect,serverurl,lastslash-serverurl+1);
00237       realconnect.append(servlet);
00238     }
00239     realconnect.append(frontierConnect, nextparen,frontierConnect.length()-nextparen);
00240   }catch(xercesc::DOMException &e){
00241   }
00242   return realconnect;
00243 }
00244 
00245 LumiProducer::
00246 LumiProducer::LumiProducer(const edm::ParameterSet& iConfig):m_cachedrun(0),m_isNullRun(false),m_cachesize(0)
00247 {
00248   // register your products
00249   produces<LumiSummaryRunHeader, edm::InRun>();
00250   produces<LumiSummary, edm::InLumi>();
00251   produces<LumiDetails, edm::InLumi>();
00252   // set up cache
00253   std::string connectStr=iConfig.getParameter<std::string>("connect");
00254   m_cachesize=iConfig.getUntrackedParameter<unsigned int>("ncacheEntries",5);
00255   m_lumiversion="v2"; /*iConfig.getUntrackedParameter<std::string>("lumiversion");*/
00256   const std::string fproto("frontier://");
00257   //test if need frontier servlet site-local translation  
00258   if(connectStr.substr(0,fproto.length())==fproto){
00259     std::string::size_type startservlet=fproto.length();
00260     std::string::size_type endservlet=connectStr.find("(",startservlet);
00261     if(endservlet==std::string::npos){
00262       endservlet=connectStr.rfind('/',connectStr.length());
00263     }
00264     std::string servlet=connectStr.substr(startservlet,endservlet-startservlet);
00265     if( (servlet !="")&& (servlet.find_first_of(":/)[]")==std::string::npos)){
00266       if(servlet=="cms_conditions_data") servlet="";
00267       
00268       std::string siteconfpath=iConfig.getUntrackedParameter<std::string>("siteconfpath","");
00269       if(siteconfpath.length()==0){
00270         std::string url=(boost::filesystem::path("SITECONF")/boost::filesystem::path("local")/boost::filesystem::path("JobConfig")/boost::filesystem::path("site-local-config.xml")).string();
00271         char * tmp = getenv ("CMS_PATH");
00272         if(tmp){
00273           m_siteconfpath = (boost::filesystem::path(tmp)/boost::filesystem::path(url)).string();
00274         }
00275       }else{
00276         if(!boost::filesystem::exists(boost::filesystem::path(siteconfpath))){
00277           throw cms::Exception("Non existing path ")<<siteconfpath;
00278         }
00279         m_siteconfpath = (boost::filesystem::path(siteconfpath)/boost::filesystem::path("site-local-config.xml")).string();
00280       }
00281       //std::cout<<"servlet : "<<servlet<<std::endl;
00282       m_connectStr=fproto+servletTranslation(servlet)+connectStr.substr(endservlet);
00283     }else{
00284       m_connectStr=connectStr;
00285     }
00286   }else{
00287     m_connectStr=connectStr;
00288   }
00289   //std::cout<<"connect string "<< m_connectStr<<std::endl;
00290 }
00291 
00292 LumiProducer::~LumiProducer(){ 
00293 }
00294 
00295 //
00296 // member functions
00297 //
00298 void LumiProducer::produce(edm::Event& e, const edm::EventSetup& iSetup)
00299 { 
00300 }
00301 unsigned long long 
00302 LumiProducer::getLumiDataId(const coral::ISchema& schema,unsigned int runnumber){ 
00303   //
00304   //select max(data_id) from lumidata where runnum=:runnum
00305   //
00306   //std::count<<"entering getLumiDataId "<<std::endl;
00307   unsigned long long lumidataid=0;
00308   coral::AttributeList bindVariables;
00309   bindVariables.extend("runnum",typeid(unsigned int));
00310   bindVariables["runnum"].data<unsigned int>()=runnumber;
00311   coral::AttributeList lumiidOutput;
00312   lumiidOutput.extend("lumidataid",typeid(unsigned long long));
00313   coral::IQuery* lumiQuery=schema.newQuery();
00314   lumiQuery->addToTableList(lumi::LumiNames::lumidataTableName());
00315   lumiQuery->addToOutputList("MAX(DATA_ID)","lumidataid");
00316   lumiQuery->setCondition("RUNNUM=:runnum",bindVariables);
00317   lumiQuery->defineOutput(lumiidOutput);
00318   coral::ICursor& lumicursor=lumiQuery->execute();
00319   while( lumicursor.next() ){
00320     const coral::AttributeList& row=lumicursor.currentRow();
00321     if(!row["lumidataid"].isNull()){
00322       lumidataid=row["lumidataid"].data<unsigned long long>();
00323     }
00324   }
00325   delete lumiQuery;
00326   return lumidataid;
00327 }
00328 unsigned long long 
00329 LumiProducer::getTrgDataId(const coral::ISchema& schema,unsigned int runnumber){
00330   //
00331   //select max(data_id) from trgdata where runnum=:runnum
00332   //
00333   unsigned long long trgdataid=0;
00334   coral::AttributeList bindVariables;
00335   bindVariables.extend("runnum",typeid(unsigned int));
00336   bindVariables["runnum"].data<unsigned int>()=runnumber;
00337   coral::AttributeList trgidOutput;
00338   trgidOutput.extend("trgdataid",typeid(unsigned long long));
00339   coral::IQuery* trgQuery=schema.newQuery();
00340   trgQuery->addToTableList(lumi::LumiNames::trgdataTableName());
00341   trgQuery->addToOutputList("MAX(DATA_ID)","trgdataid");
00342   trgQuery->setCondition("RUNNUM=:runnum",bindVariables);
00343   trgQuery->defineOutput(trgidOutput);
00344   coral::ICursor& trgcursor=trgQuery->execute();
00345   while( trgcursor.next() ){
00346     const coral::AttributeList& row=trgcursor.currentRow();
00347     if(!row["trgdataid"].isNull()){
00348       trgdataid=row["trgdataid"].data<unsigned long long>();
00349     }
00350   }
00351   delete trgQuery;
00352   return trgdataid;
00353 }
00354 unsigned long long 
00355 LumiProducer::getHltDataId(const coral::ISchema& schema,unsigned int runnumber){
00356   //
00357   //select max(data_id) from hltdata where runnum=:runnum
00358   //
00359   unsigned long long hltdataid=0;
00360   coral::AttributeList bindVariables;
00361   bindVariables.extend("runnum",typeid(unsigned int));
00362   bindVariables["runnum"].data<unsigned int>()=runnumber;
00363   coral::AttributeList hltidOutput;
00364   hltidOutput.extend("hltdataid",typeid(unsigned long long));
00365   coral::IQuery* hltQuery=schema.newQuery();
00366   hltQuery->addToTableList(lumi::LumiNames::hltdataTableName());
00367   hltQuery->addToOutputList("MAX(DATA_ID)","hltdataid");
00368   hltQuery->setCondition("RUNNUM=:runnum",bindVariables);
00369   hltQuery->defineOutput(hltidOutput);
00370   coral::ICursor& hltcursor=hltQuery->execute();
00371   while( hltcursor.next() ){
00372     const coral::AttributeList& row=hltcursor.currentRow();
00373     if(!row["hltdataid"].isNull()){
00374       hltdataid=row["hltdataid"].data<unsigned long long>();
00375     }
00376   }
00377   delete hltQuery;
00378   return hltdataid;
00379 }
00380 void 
00381 LumiProducer::beginRun(edm::Run& run,edm::EventSetup const &iSetup)
00382 {
00383   unsigned int runnumber=run.run();
00384   if(m_cachedrun!=runnumber){
00385     //queries once per run
00386     m_cachedrun=runnumber;
00387     edm::Service<lumi::service::DBService> mydbservice;
00388     if( !mydbservice.isAvailable() ){
00389       throw cms::Exception("Non existing service lumi::service::DBService");
00390     }
00391     coral::ISessionProxy* session=mydbservice->connectReadOnly(m_connectStr);
00392     try{
00393       session->transaction().start(true);
00394       m_cachedlumidataid=getLumiDataId(session->nominalSchema(),runnumber);
00395       if(m_cachedlumidataid!=0){//if no lumi, do not bother other info
00396         m_cachedtrgdataid=getTrgDataId(session->nominalSchema(),runnumber);
00397         m_cachedhltdataid=getHltDataId(session->nominalSchema(),runnumber);
00398         fillRunCache(session->nominalSchema(),runnumber);
00399       }else{
00400         m_isNullRun=true;
00401       }
00402       session->transaction().commit();
00403     }catch(const coral::Exception& er){
00404       session->transaction().rollback();
00405       mydbservice->disconnect(session);
00406       throw cms::Exception("DatabaseError ")<<er.what();
00407     }
00408     mydbservice->disconnect(session);
00409   }
00410   //std::cout<<"end of beginRun "<<runnumber<<std::endl;
00411 }
00412 
00413 void LumiProducer::beginLuminosityBlock(edm::LuminosityBlock &iLBlock, edm::EventSetup const &iSetup)
00414 {
00415   unsigned int runnumber=iLBlock.run();
00416   unsigned int luminum=iLBlock.luminosityBlock();
00417   //std::cout<<"beg of beginLuminosityBlock "<<luminum<<std::endl;
00418   //if is null run, fill empty values and return
00419   if(m_isNullRun){
00420     std::auto_ptr<LumiSummary> pOut1;
00421     std::auto_ptr<LumiDetails> pOut2;
00422     LumiSummary* pIn1=new LumiSummary;
00423     LumiDetails* pIn2=new LumiDetails;
00424     pOut1.reset(pIn1);
00425     iLBlock.put(pOut1);
00426     pOut2.reset(pIn2);
00427     iLBlock.put(pOut2);
00428     return;
00429   }
00430   if(m_lscache.find(luminum)==m_lscache.end()){
00431     //if runnumber is cached but LS is not, this is the first LS, fill LS cache to full capacity
00432     fillLSCache(luminum);
00433   }
00434   //here the presence of ls is guaranteed
00435   writeProductsForEntry(iLBlock,runnumber,luminum); 
00436 }
00437 void 
00438 LumiProducer::endLuminosityBlock(edm::LuminosityBlock & iLBlock, edm::EventSetup const& iSetup)
00439 {
00440 }
00441 void 
00442 LumiProducer::endRun(edm::Run& run,edm::EventSetup const &iSetup)
00443 {
00444   std::auto_ptr<LumiSummaryRunHeader> lsrh(new LumiSummaryRunHeader());
00445   lsrh->swapL1Names(m_runcache.TRGBitNames);
00446   lsrh->swapHLTNames(m_runcache.HLTPathNames);
00447   run.put(lsrh);
00448   m_runcache.TRGBitNameToIndex.clear();
00449   m_runcache.HLTPathNameToIndex.clear();
00450 }
00451 void 
00452 LumiProducer::fillRunCache(const coral::ISchema& schema,unsigned int runnumber){
00453   if(m_cachedtrgdataid!=0){
00454     coral::AttributeList trgBindVariables;
00455     trgBindVariables.extend("trgdataid",typeid(unsigned long long));
00456     trgBindVariables["trgdataid"].data<unsigned long long>()=m_cachedtrgdataid;
00457     //std::cout<<"cached trgdataid "<<m_cachedtrgdataid<<std::endl;
00458     coral::AttributeList trgOutput;
00459     trgOutput.extend("bitzeroname",typeid(std::string));
00460     trgOutput.extend("bitnameclob",typeid(std::string));
00461     coral::IQuery* trgQuery=schema.newQuery();
00462     trgQuery->addToTableList(lumi::LumiNames::trgdataTableName());
00463     trgQuery->addToOutputList("BITZERONAME");
00464     trgQuery->addToOutputList("BITNAMECLOB");
00465     trgQuery->setCondition("DATA_ID=:trgdataid",trgBindVariables);
00466     trgQuery->defineOutput(trgOutput);
00467     coral::ICursor& trgcursor=trgQuery->execute();
00468     while( trgcursor.next() ){
00469       const coral::AttributeList& row=trgcursor.currentRow();
00470       m_runcache.bitzeroname=row["bitzeroname"].data<std::string>();
00471       //std::cout<<"bitzeroname "<<m_runcache.bitzeroname<<std::endl;
00472       std::string bitnames=row["bitnameclob"].data<std::string>();
00473       boost::char_separator<char> sep(",");
00474       boost::tokenizer<boost::char_separator<char> > tokens(bitnames,sep);
00475       for(boost::tokenizer<boost::char_separator<char> >::iterator tok_it=tokens.begin();tok_it!=tokens.end();++tok_it){
00476         m_runcache.TRGBitNames.push_back(*tok_it);
00477       }
00478       for(unsigned int i=0;i<m_runcache.TRGBitNames.size();++i){
00479         m_runcache.TRGBitNameToIndex.insert(std::make_pair(m_runcache.TRGBitNames.at(i),i) );
00480       }      
00481     }
00482     delete trgQuery;
00483   }
00484   if(m_cachedhltdataid!=0){
00485     //
00486     //select pathnameclob from hltdata where data_id=:hltdataid
00487     //
00488     coral::AttributeList hltBindVariables;
00489     hltBindVariables.extend("hltdataid",typeid(unsigned long long));
00490     hltBindVariables["hltdataid"].data<unsigned long long>()=m_cachedhltdataid;
00491     coral::AttributeList hltOutput;
00492     hltOutput.extend("PATHNAMECLOB",typeid(std::string));
00493     coral::IQuery* hltQuery=schema.newQuery();
00494     hltQuery->addToTableList(lumi::LumiNames::hltdataTableName());
00495     hltQuery->addToOutputList("PATHNAMECLOB");
00496     hltQuery->setCondition("DATA_ID=:hltdataid",hltBindVariables);
00497     hltQuery->defineOutput(hltOutput);
00498     coral::ICursor& hltcursor=hltQuery->execute();
00499     while( hltcursor.next() ){
00500       const coral::AttributeList& row=hltcursor.currentRow();
00501       std::string pathnames=row["PATHNAMECLOB"].data<std::string>();
00502       boost::char_separator<char> sep(",");
00503       boost::tokenizer<boost::char_separator<char> > tokens(pathnames,sep);
00504       for(boost::tokenizer<boost::char_separator<char> >::iterator tok_it=tokens.begin();tok_it!=tokens.end();++tok_it){
00505         m_runcache.HLTPathNames.push_back(*tok_it);
00506       }
00507       for(unsigned int i=0;i<m_runcache.HLTPathNames.size();++i){
00508         m_runcache.HLTPathNameToIndex.insert(std::make_pair(m_runcache.HLTPathNames.at(i),i));
00509       }     
00510     }
00511     delete hltQuery;   
00512   }
00513 }
00514 void
00515 LumiProducer::fillLSCache(unsigned int luminum){
00516   //initialize cache
00517   if(m_isNullRun) return;
00518   m_lscache.clear();
00519   for(unsigned int n=luminum;n<luminum+m_cachesize;++n){
00520     PerLSData l;
00521     l.hltdata.reserve(250);
00522     l.l1data.reserve(192);
00523     l.bunchlumivalue.reserve(5);
00524     l.bunchlumierror.reserve(5);
00525     l.bunchlumiquality.reserve(5);
00526     l.beam1intensity.resize(3564,0.0);
00527     l.beam2intensity.resize(3564,0.0);
00528     m_lscache.insert(std::make_pair(n,l));
00529   }
00530   //queries once per cache refill
00531   //
00532   //select cmslsnum,instlumi,startorbit,numorbit,bxindex,beam1intensity,beam2intensity,bxlumivalue_occ1,bxlumivalue_occ2,bxlumivalue_et from lumisummaryv2 where cmslsnum>=:lsmin and cmslsnum<:lsmax and data_id=:lumidataid;
00533   //
00534   edm::Service<lumi::service::DBService> mydbservice;
00535   if( !mydbservice.isAvailable() ){
00536     throw cms::Exception("Non existing service lumi::service::DBService");
00537   }
00538   coral::ISessionProxy* session=mydbservice->connectReadOnly(m_connectStr);
00539   try{
00540     session->transaction().start(true);
00541     coral::ISchema& schema=session->nominalSchema();
00542     coral::AttributeList lumisummaryBindVariables;
00543     lumisummaryBindVariables.extend("lsmin",typeid(unsigned int));
00544     lumisummaryBindVariables.extend("lsmax",typeid(unsigned int));
00545     lumisummaryBindVariables.extend("lumidataid",typeid(unsigned long long));
00546     lumisummaryBindVariables["lumidataid"].data<unsigned long long>()=m_cachedlumidataid;
00547     lumisummaryBindVariables["lsmin"].data<unsigned int>()=luminum;
00548     lumisummaryBindVariables["lsmax"].data<unsigned int>()=luminum+m_cachesize;
00549     coral::AttributeList lumisummaryOutput;
00550     lumisummaryOutput.extend("CMSLSNUM",typeid(unsigned int));
00551     lumisummaryOutput.extend("INSTLUMI",typeid(float));
00552     lumisummaryOutput.extend("STARTORBIT",typeid(unsigned int));
00553     lumisummaryOutput.extend("NUMORBIT",typeid(unsigned int));
00554     lumisummaryOutput.extend("CMSBXINDEXBLOB",typeid(coral::Blob));
00555     lumisummaryOutput.extend("BEAMINTENSITYBLOB_1",typeid(coral::Blob));
00556     lumisummaryOutput.extend("BEAMINTENSITYBLOB_2",typeid(coral::Blob));
00557     lumisummaryOutput.extend("BXLUMIVALUE_OCC1",typeid(coral::Blob));
00558     lumisummaryOutput.extend("BXLUMIVALUE_OCC2",typeid(coral::Blob));
00559     lumisummaryOutput.extend("BXLUMIVALUE_ET",typeid(coral::Blob));
00560     coral::IQuery* lumisummaryQuery=schema.newQuery();
00561     lumisummaryQuery->addToTableList(lumi::LumiNames::lumisummaryv2TableName());
00562     lumisummaryQuery->addToOutputList("CMSLSNUM");
00563     lumisummaryQuery->addToOutputList("INSTLUMI");
00564     lumisummaryQuery->addToOutputList("STARTORBIT");
00565     lumisummaryQuery->addToOutputList("NUMORBIT");
00566     lumisummaryQuery->addToOutputList("CMSBXINDEXBLOB");
00567     lumisummaryQuery->addToOutputList("BEAMINTENSITYBLOB_1");
00568     lumisummaryQuery->addToOutputList("BEAMINTENSITYBLOB_2");
00569     lumisummaryQuery->addToOutputList("BXLUMIVALUE_OCC1");
00570     lumisummaryQuery->addToOutputList("BXLUMIVALUE_OCC2");
00571     lumisummaryQuery->addToOutputList("BXLUMIVALUE_ET");
00572     lumisummaryQuery->setCondition("CMSLSNUM>=:lsmin AND CMSLSNUM<:lsmax AND DATA_ID=:lumidataid",lumisummaryBindVariables);
00573     lumisummaryQuery->defineOutput(lumisummaryOutput);
00574     coral::ICursor& lumisummarycursor=lumisummaryQuery->execute();
00575     unsigned int rowcounter=0;
00576     while( lumisummarycursor.next() ){
00577       const coral::AttributeList& row=lumisummarycursor.currentRow();
00578       unsigned int cmslsnum=row["CMSLSNUM"].data<unsigned int>();
00579       //std::cout<<"cmslsnum "<<cmslsnum<<std::endl;
00580       PerLSData& lsdata=m_lscache[cmslsnum];
00581       lsdata.lumivalue=row["INSTLUMI"].data<float>();
00582       lsdata.lumierror=0.0;
00583       lsdata.lumiquality=0;
00584       lsdata.startorbit=row["STARTORBIT"].data<unsigned int>();
00585       lsdata.numorbit=row["NUMORBIT"].data<unsigned int>();
00586       
00587       if(!row["CMSBXINDEXBLOB"].isNull() && !row["BXLUMIVALUE_OCC1"].isNull() ){
00588         const coral::Blob& bxindexBlob=row["CMSBXINDEXBLOB"].data<coral::Blob>();
00589         const void* bxindex_StartAddress=bxindexBlob.startingAddress();
00590         short* bxindex=(short*)::malloc(bxindexBlob.size());
00591         const coral::Blob& beam1intensityBlob=row["BEAMINTENSITYBLOB_1"].data<coral::Blob>();
00592         const void* beam1intensityBlob_StartAddress=beam1intensityBlob.startingAddress();
00593         float* beam1intensity=(float*)::malloc(beam1intensityBlob.size());
00594         const coral::Blob& beam2intensityBlob=row["BEAMINTENSITYBLOB_2"].data<coral::Blob>();
00595         const void* beam2intensityBlob_StartAddress=beam2intensityBlob.startingAddress();
00596         float* beam2intensity=(float*)::malloc(beam2intensityBlob.size());
00597         std::memmove(bxindex,bxindex_StartAddress,bxindexBlob.size());
00598         std::memmove(beam1intensity,beam1intensityBlob_StartAddress,beam1intensityBlob.size());
00599         std::memmove(beam2intensity,beam2intensityBlob_StartAddress,beam2intensityBlob.size());
00600 
00601         unsigned int iMax = bxindexBlob.size()/sizeof(short);
00602         unsigned int lsb1Max = lsdata.beam1intensity.size();
00603         unsigned int lsb2Max = lsdata.beam2intensity.size();
00604         unsigned int ib1Max = beam1intensityBlob.size()/sizeof(float);
00605         unsigned int ib2Max = beam2intensityBlob.size()/sizeof(float);
00606         for(unsigned int i=0;i<iMax;++i){
00607           unsigned int idx=bxindex[i];
00608           if(ib1Max>i && lsb1Max>idx){
00609           lsdata.beam1intensity.at(idx)=beam1intensity[i];
00610           }
00611           if(ib2Max>i && lsb2Max>idx){
00612           lsdata.beam2intensity.at(idx)=beam2intensity[i];
00613         }
00614         }
00615         ::free(bxindex);
00616         ::free(beam1intensity);
00617         ::free(beam2intensity);
00618 
00619         const coral::Blob& bxlumivalBlob_occ1=row["BXLUMIVALUE_OCC1"].data<coral::Blob>();
00620         const void* bxlumival_occ1_StartAddress=bxlumivalBlob_occ1.startingAddress();
00621         float* bxlumival_occ1=(float*)::malloc(bxlumivalBlob_occ1.size());
00622         std::memmove(bxlumival_occ1,bxlumival_occ1_StartAddress,bxlumivalBlob_occ1.size());
00623         std::vector<float> bxlumivalVec_occ1(bxlumival_occ1,bxlumival_occ1+bxlumivalBlob_occ1.size()/sizeof(float));
00624         ::free(bxlumival_occ1);
00625         lsdata.bunchlumivalue.push_back(std::make_pair(std::string("OCC1"),bxlumivalVec_occ1));
00626         lsdata.bunchlumierror.push_back(std::make_pair(std::string("OCC1"),std::vector<float>(3564)));
00627         lsdata.bunchlumiquality.push_back(std::make_pair(std::string("OCC1"),std::vector<short>(3564)));
00628         const coral::Blob& bxlumivalBlob_occ2=row["BXLUMIVALUE_OCC2"].data<coral::Blob>();
00629         const void* bxlumival_occ2_StartAddress=bxlumivalBlob_occ2.startingAddress();
00630         float* bxlumival_occ2=(float*)::malloc(bxlumivalBlob_occ2.size());
00631         std::memmove(bxlumival_occ2,bxlumival_occ2_StartAddress,bxlumivalBlob_occ2.size());
00632         std::vector<float> bxlumivalVec_occ2(bxlumival_occ2,bxlumival_occ2+bxlumivalBlob_occ1.size()/sizeof(float));
00633         ::free(bxlumival_occ2);
00634         lsdata.bunchlumivalue.push_back(std::make_pair(std::string("OCC2"),bxlumivalVec_occ2));
00635         lsdata.bunchlumierror.push_back(std::make_pair(std::string("OCC2"),std::vector<float>(3564)));
00636         lsdata.bunchlumiquality.push_back(std::make_pair(std::string("OCC2"),std::vector<short>(3564)));
00637 
00638         const coral::Blob& bxlumivalBlob_et=row["BXLUMIVALUE_ET"].data<coral::Blob>();
00639         const void* bxlumival_et_StartAddress=bxlumivalBlob_et.startingAddress();
00640         float* bxlumival_et=(float*)::malloc(bxlumivalBlob_et.size());
00641         std::memmove(bxlumival_et,bxlumival_et_StartAddress,bxlumivalBlob_et.size());   
00642         std::vector<float> bxlumivalVec_et(bxlumival_et,bxlumival_et+bxlumivalBlob_et.size()/sizeof(float));
00643         ::free(bxlumival_et);
00644         lsdata.bunchlumivalue.push_back(std::make_pair(std::string("ET"),bxlumivalVec_et));
00645         lsdata.bunchlumierror.push_back(std::make_pair(std::string("ET"),std::vector<float>(3564)));
00646         lsdata.bunchlumiquality.push_back(std::make_pair(std::string("ET"),std::vector<short>(3564)));
00647       }
00648       ++rowcounter;
00649     }
00650     if (rowcounter==0){
00651       m_isNullRun=true;
00652       return;
00653     }
00654     delete lumisummaryQuery;
00655     
00656     //
00657     //select cmslsnum,deadtimecount,bitzerocount,bitzeroprescale,prescaleblob,trgcountblob from lstrg where cmslsnum >=:luminum and cmslsnum<:luminum+cachesize AND data_id=:trgdataid;
00658     //
00659     coral::AttributeList trgBindVariables;
00660     trgBindVariables.extend("lsmin",typeid(unsigned int));
00661     trgBindVariables.extend("lsmax",typeid(unsigned int));
00662     trgBindVariables.extend("trgdataid",typeid(unsigned long long));
00663     trgBindVariables["lsmin"].data<unsigned int>()=luminum;
00664     trgBindVariables["lsmax"].data<unsigned int>()=luminum+m_cachesize;
00665     trgBindVariables["trgdataid"].data<unsigned long long>()=m_cachedtrgdataid;
00666     coral::AttributeList trgOutput;
00667     trgOutput.extend("CMSLSNUM",typeid(unsigned int));
00668     trgOutput.extend("DEADTIMECOUNT",typeid(unsigned long long));
00669     trgOutput.extend("BITZEROCOUNT",typeid(unsigned int));
00670     trgOutput.extend("BITZEROPRESCALE",typeid(unsigned int));
00671     trgOutput.extend("PRESCALEBLOB",typeid(coral::Blob));
00672     trgOutput.extend("TRGCOUNTBLOB",typeid(coral::Blob));
00673 
00674     coral::IQuery* trgQuery=schema.newQuery();
00675     trgQuery->addToTableList(lumi::LumiNames::lstrgTableName());
00676     trgQuery->addToOutputList("CMSLSNUM");
00677     trgQuery->addToOutputList("DEADTIMECOUNT");
00678     trgQuery->addToOutputList("BITZEROCOUNT");
00679     trgQuery->addToOutputList("BITZEROPRESCALE");
00680     trgQuery->addToOutputList("PRESCALEBLOB");
00681     trgQuery->addToOutputList("TRGCOUNTBLOB");
00682     trgQuery->setCondition("CMSLSNUM>=:lsmin AND CMSLSNUM<:lsmax AND DATA_ID=:trgdataid",trgBindVariables);
00683     trgQuery->defineOutput(trgOutput);
00684     coral::ICursor& trgcursor=trgQuery->execute();
00685     while( trgcursor.next() ){
00686       const coral::AttributeList& row=trgcursor.currentRow();
00687       unsigned int cmslsnum=row["CMSLSNUM"].data<unsigned int>();
00688       PerLSData& lsdata=m_lscache[cmslsnum];
00689       lsdata.deadcount=row["DEADTIMECOUNT"].data<unsigned long long>();
00690       lsdata.bitzerocount=row["BITZEROCOUNT"].data<unsigned int>();
00691       lsdata.bitzeroprescale=row["BITZEROPRESCALE"].data<unsigned int>();
00692       if(!row["PRESCALEBLOB"].isNull()){
00693         const coral::Blob& prescaleblob=row["PRESCALEBLOB"].data<coral::Blob>();
00694         const void* prescaleblob_StartAddress=prescaleblob.startingAddress();
00695         unsigned int* prescales=(unsigned int*)::malloc(prescaleblob.size());
00696         std::memmove(prescales,prescaleblob_StartAddress,prescaleblob.size());
00697         const coral::Blob& trgcountblob=row["TRGCOUNTBLOB"].data<coral::Blob>();
00698         const void* trgcountblob_StartAddress=trgcountblob.startingAddress();
00699         unsigned int* trgcounts=(unsigned int*)::malloc(trgcountblob.size());
00700         std::memmove(trgcounts,trgcountblob_StartAddress,trgcountblob.size());
00701         for(unsigned int i=0;i<sizeof(trgcounts)/sizeof(unsigned int);++i){
00702           L1Data l1tmp;
00703           l1tmp.bitname=m_runcache.TRGBitNames[i];
00704           l1tmp.prescale=prescales[i];
00705           l1tmp.ratecount=trgcounts[i];
00706           lsdata.l1data.push_back(l1tmp);
00707         }
00708         ::free(prescales);
00709         ::free(trgcounts);
00710       }
00711     }
00712     delete trgQuery;
00713     //
00714     //select cmslsnum,hltcountblob,hltacceptblob,prescaleblob from hlt where cmslsnum >=:luminum and cmslsnum<=:luminum+cachesize and data_id=:hltdataid 
00715     //
00716     coral::AttributeList hltBindVariables;
00717     hltBindVariables.extend("lsmin",typeid(unsigned int));
00718     hltBindVariables.extend("lsmax",typeid(unsigned int));
00719     hltBindVariables.extend("hltdataid",typeid(unsigned long long));
00720     hltBindVariables["lsmin"].data<unsigned int>()=luminum;
00721     hltBindVariables["lsmax"].data<unsigned int>()=luminum+m_cachesize;
00722     hltBindVariables["hltdataid"].data<unsigned long long>()=m_cachedhltdataid;
00723     coral::AttributeList hltOutput;
00724     hltOutput.extend("CMSLSNUM",typeid(unsigned int));
00725     hltOutput.extend("HLTCOUNTBLOB",typeid(coral::Blob));
00726     hltOutput.extend("HLTACCEPTBLOB",typeid(coral::Blob));
00727     hltOutput.extend("PRESCALEBLOB",typeid(coral::Blob));
00728     coral::IQuery* hltQuery=schema.newQuery();
00729     hltQuery->addToTableList(lumi::LumiNames::lshltTableName());
00730     hltQuery->addToOutputList("CMSLSNUM");
00731     hltQuery->addToOutputList("HLTCOUNTBLOB");
00732     hltQuery->addToOutputList("HLTACCEPTBLOB");
00733     hltQuery->addToOutputList("PRESCALEBLOB");
00734     hltQuery->setCondition("CMSLSNUM>=:lsmin AND CMSLSNUM<:lsmax AND DATA_ID=:hltdataid",hltBindVariables);
00735     hltQuery->defineOutput(hltOutput);
00736     coral::ICursor& hltcursor=hltQuery->execute();
00737     while( hltcursor.next() ){
00738       const coral::AttributeList& row=hltcursor.currentRow();   
00739       unsigned int cmslsnum=row["CMSLSNUM"].data<unsigned int>();
00740       PerLSData& lsdata=m_lscache[cmslsnum];
00741       if(!row["PRESCALEBLOB"].isNull()){
00742         const coral::Blob& hltprescaleblob=row["PRESCALEBLOB"].data<coral::Blob>();
00743         const void* hltprescaleblob_StartAddress=hltprescaleblob.startingAddress();
00744         unsigned int* hltprescales=(unsigned int*)::malloc(hltprescaleblob.size());
00745         std::memmove(hltprescales,hltprescaleblob_StartAddress,hltprescaleblob.size());
00746         const coral::Blob& hltcountblob=row["HLTCOUNTBLOB"].data<coral::Blob>();
00747         const void* hltcountblob_StartAddress=hltcountblob.startingAddress();
00748         unsigned int* hltcounts=(unsigned int*)::malloc(hltcountblob.size());
00749         std::memmove(hltcounts,hltcountblob_StartAddress,hltcountblob.size());
00750         const coral::Blob& hltacceptblob=row["HLTACCEPTBLOB"].data<coral::Blob>();
00751         const void* hltacceptblob_StartAddress=hltacceptblob.startingAddress();
00752         unsigned int* hltaccepts=(unsigned int*)::malloc(hltacceptblob.size());
00753         std::memmove(hltaccepts,hltacceptblob_StartAddress,hltacceptblob.size());
00754         unsigned int nhltaccepts = sizeof(hltaccepts)/sizeof(unsigned int);
00755         if(nhltaccepts > 0 && m_runcache.HLTPathNames.size() == 0){
00756           edm::LogWarning("CorruptOrMissingHLTData")<<"Got "<<nhltaccepts
00757 <<" hltaccepts, but the run chache is empty. hltdata will  not be written";
00758             break;
00759         }
00760 
00761         for(unsigned int i=0;i<sizeof(hltaccepts)/sizeof(unsigned int);++i){
00762           HLTData hlttmp;
00763           hlttmp.pathname=m_runcache.HLTPathNames[i];
00764           hlttmp.prescale=hltprescales[i];
00765           hlttmp.l1passcount=hltcounts[i];
00766           hlttmp.acceptcount=hltaccepts[i];
00767           lsdata.hltdata.push_back(hlttmp);
00768         }
00769         ::free(hltprescales);
00770         ::free(hltcounts);
00771         ::free(hltaccepts);
00772       }
00773     }
00774     delete hltQuery;
00775     session->transaction().commit();
00776   }catch(const coral::Exception& er){
00777     session->transaction().rollback();
00778     mydbservice->disconnect(session);
00779     throw cms::Exception("DatabaseError ")<<er.what();
00780   }
00781   mydbservice->disconnect(session);
00782 }
00783 void
00784 LumiProducer::writeProductsForEntry(edm::LuminosityBlock & iLBlock,unsigned int runnumber,unsigned int luminum){
00785   //std::cout<<"writing runnumber,luminum "<<runnumber<<" "<<luminum<<std::endl;
00786   std::auto_ptr<LumiSummary> pOut1;
00787   std::auto_ptr<LumiDetails> pOut2;
00788   LumiSummary* pIn1=new LumiSummary;
00789   LumiDetails* pIn2=new LumiDetails;
00790   if(m_isNullRun){
00791     pIn1->setLumiVersion("-1");
00792     pIn2->setLumiVersion("-1");
00793     pOut1.reset(pIn1);
00794     iLBlock.put(pOut1);
00795     pOut2.reset(pIn2);
00796     iLBlock.put(pOut2);
00797     return;
00798   }
00799   PerLSData& lsdata=m_lscache[luminum];
00800   pIn1->setLumiData(lsdata.lumivalue,lsdata.lumierror,lsdata.lumiquality);
00801   pIn1->setDeadCount(lsdata.deadcount);
00802   if(!lsdata.l1data.empty()){
00803     //std::cout<<"bitzerocount "<<lsdata.bitzerocount<<std::endl;
00804     //std::cout<<"bitzeroprescale "<<lsdata.bitzeroprescale<<std::endl;
00805     //std::cout<<"product "<<lsdata.bitzerocount*lsdata.bitzeroprescale<<std::endl;
00806     pIn1->setBitZeroCount(lsdata.bitzerocount*lsdata.bitzeroprescale);
00807   }
00808   pIn1->setlsnumber(luminum);
00809   pIn1->setOrbitData(lsdata.startorbit,lsdata.numorbit);
00810   std::vector<LumiSummary::L1> l1temp;
00811   for(std::vector< L1Data >::iterator it=lsdata.l1data.begin();it!=lsdata.l1data.end();++it){
00812     LumiSummary::L1 trgtmp;
00813     trgtmp.triggernameidx=m_runcache.TRGBitNameToIndex[it->bitname];
00814     trgtmp.prescale=it->prescale;
00815     l1temp.push_back(trgtmp);
00816   }
00817   std::vector<LumiSummary::HLT> hlttemp;
00818   for(std::vector< HLTData >::iterator it=lsdata.hltdata.begin();it!=lsdata.hltdata.end();++it){
00819     LumiSummary::HLT hlttmp;
00820     hlttmp.pathnameidx=m_runcache.HLTPathNameToIndex[it->pathname];;
00821     hlttmp.prescale=it->prescale;
00822     hlttemp.push_back(hlttmp);
00823   }
00824   pIn1->swapL1Data(l1temp);
00825   pIn1->swapHLTData(hlttemp);
00826   pIn1->setLumiVersion(m_lumiversion);  
00827   pIn2->fillBeamIntensities(lsdata.beam1intensity,lsdata.beam2intensity);
00828   for(unsigned int i=0;i<lsdata.bunchlumivalue.size();++i){
00829     std::string algoname=lsdata.bunchlumivalue[i].first;
00830     if(algoname=="OCC1"){
00831       pIn2->fill(LumiDetails::kOCC1,lsdata.bunchlumivalue[i].second,lsdata.bunchlumierror[i].second,lsdata.bunchlumiquality[i].second);
00832     }else if(algoname=="OCC2"){      
00833       pIn2->fill(LumiDetails::kOCC2,lsdata.bunchlumivalue[i].second,lsdata.bunchlumierror[i].second,lsdata.bunchlumiquality[i].second);
00834     }else if(algoname=="ET"){
00835       pIn2->fill(LumiDetails::kET,lsdata.bunchlumivalue[i].second,lsdata.bunchlumierror[i].second,lsdata.bunchlumiquality[i].second);
00836     }else if(algoname=="PLT"){
00837       pIn2->fill(LumiDetails::kPLT,lsdata.bunchlumivalue[i].second,lsdata.bunchlumierror[i].second,lsdata.bunchlumiquality[i].second);
00838     }
00839   }
00840   pIn2->setLumiVersion(m_lumiversion);
00841   pOut1.reset(pIn1);
00842   iLBlock.put(pOut1);
00843   pOut2.reset(pIn2);
00844   iLBlock.put(pOut2);
00845 }
00846 #include "FWCore/Framework/interface/MakerMacros.h"
00847 DEFINE_FWK_MODULE(LumiProducer);