CMS 3D CMS Logo

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