CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch13/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.19 2011/02/14 16:16:41 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/LumiSummary.h"
00031 #include "DataFormats/Luminosity/interface/LumiDetails.h"
00032 #include "FWCore/ServiceRegistry/interface/Service.h"
00033 #include "FWCore/Framework/interface/EventSetup.h"
00034 
00035 #include "CoralBase/Exception.h"
00036 #include "CoralBase/AttributeList.h"
00037 #include "CoralBase/Attribute.h"
00038 #include "CoralBase/AttributeSpecification.h"
00039 #include "CoralBase/Exception.h"
00040 #include "CoralBase/Blob.h"
00041 #include "RelationalAccess/ISessionProxy.h"
00042 #include "RelationalAccess/ITransaction.h"
00043 #include "RelationalAccess/AccessMode.h"
00044 #include "RelationalAccess/ITypeConverter.h"
00045 #include "RelationalAccess/IQuery.h"
00046 #include "RelationalAccess/ICursor.h"
00047 #include "RelationalAccess/ISchema.h"
00048 #include "RelationalAccess/ITable.h"
00049 
00050 #include "RecoLuminosity/LumiProducer/interface/DBService.h"
00051 #include "RecoLuminosity/LumiProducer/interface/LumiNames.h"
00052 #include "RecoLuminosity/LumiProducer/interface/Exception.h"
00053 #include "RecoLuminosity/LumiProducer/interface/ConstantDef.h"
00054 #include <sstream>
00055 #include <string>
00056 #include <memory>
00057 #include <algorithm>
00058 #include <vector>
00059 #include <cstring>
00060 
00061 #include <xercesc/dom/DOM.hpp>
00062 #include <xercesc/parsers/XercesDOMParser.hpp>
00063 #include <xercesc/util/PlatformUtils.hpp>
00064 #include <xercesc/util/XMLString.hpp>
00065 
00066 #include "boost/filesystem/path.hpp"
00067 #include "boost/filesystem/operations.hpp"
00068 namespace edm {
00069   class EventSetup;
00070 }
00071 
00072 //
00073 // class declaration
00074 //
00075 class LumiProducer : public edm::EDProducer {
00076 
00077 public:
00078 
00079   struct HLTData{
00080     unsigned int pathnum;
00081     unsigned int prescale;
00082     unsigned int l1passcount;
00083     unsigned int acceptcount;
00084   };
00085   struct L1Data{
00086     unsigned int bitnum;
00087     unsigned int prescale;
00088     unsigned int ratecount;
00089   };
00090   struct PerRunData{
00091     std::map< unsigned int,std::string > TRGBitNames;
00092     std::map< unsigned int,std::string > HLTPathNames;
00093   };
00094   struct PerLSData{
00095     float lumivalue;
00096     float lumierror;
00097     short lumiquality;
00098     unsigned long long deadcount;
00099     unsigned int numorbit;
00100     unsigned int startorbit;
00101     std::vector< HLTData > hltdata;
00102     std::vector< L1Data > l1data;
00103     std::vector< std::pair<std::string, std::vector<float> > > bunchlumivalue;
00104     std::vector< std::pair<std::string, std::vector<float> > > bunchlumierror;
00105     std::vector< std::pair<std::string, std::vector<short> > > bunchlumiquality;
00106     std::vector<float> beam1intensity;
00107     std::vector<float> beam2intensity;
00108   };
00109 
00110   explicit LumiProducer(const edm::ParameterSet&);
00111 
00112   ~LumiProducer();
00113   
00114 private:
00115   
00116   virtual void produce(edm::Event&, const edm::EventSetup&);
00117 
00118   virtual void beginRun(edm::Run&, edm::EventSetup const &);
00119 
00120   virtual void beginLuminosityBlock(edm::LuminosityBlock & iLBlock,
00121                                     edm::EventSetup const& iSetup);
00122   virtual void endLuminosityBlock(edm::LuminosityBlock& lumiBlock, 
00123                                   edm::EventSetup const& c);
00124   //void fillDefaultLumi(edm::LuminosityBlock & iLBlock);
00125   bool fillLumi(edm::LuminosityBlock & iLBlock);
00126   void fillRunCache(unsigned int runnumber);
00127   void fillLSCache(unsigned int luminum);
00128   void writeProductsForEntry(edm::LuminosityBlock & iLBlock,unsigned int runnumber,unsigned int luminum);
00129   const std::string servletTranslation(const std::string& servlet) const;
00130   std::string x2s(const XMLCh* input)const;
00131   XMLCh* s2x(const std::string& input)const;
00132   std::string toParentString(const xercesc::DOMNode &nodeToConvert)const;
00133 
00134   std::string m_connectStr;
00135   std::string m_lumiversion;
00136   std::string m_siteconfpath;
00137   unsigned int m_cachedrun;
00138   PerRunData  m_runcache;
00139   std::map< unsigned int,PerLSData > m_lscache;
00140   bool m_isNullRun;
00141   unsigned int m_cachesize;
00142 };
00143 
00144 //
00145 // constructors and destructor
00146 //
00147 
00148 std::string 
00149 LumiProducer::x2s(const XMLCh *toTranscode)const{
00150   std::string tmp(xercesc::XMLString::transcode(toTranscode));
00151   return tmp;
00152 }
00153 
00154 XMLCh*  
00155 LumiProducer::s2x( const std::string& temp )const{
00156   XMLCh* buff = xercesc::XMLString::transcode(temp.c_str());    
00157   return  buff;
00158 }
00159 
00160 std::string
00161 LumiProducer::toParentString(const xercesc::DOMNode &nodeToConvert)const{
00162   std::ostringstream oss;
00163   xercesc::DOMNodeList *childList = nodeToConvert.getChildNodes();
00164 
00165   unsigned int numNodes = childList->getLength ();
00166   for (unsigned int i = 0; i < numNodes; ++i){
00167     xercesc::DOMNode *childNode = childList->item(i);
00168     if (childNode->getNodeType() != xercesc::DOMNode::ELEMENT_NODE)
00169       continue;
00170     xercesc::DOMElement *child = static_cast < xercesc::DOMElement *> (childNode);
00171     xercesc::DOMNamedNodeMap *attributes = child->getAttributes();
00172     unsigned int numAttributes = attributes->getLength ();
00173     for (unsigned int j = 0; j < numAttributes; ++j){
00174       xercesc::DOMNode *attributeNode = attributes->item(j);
00175       if (attributeNode->getNodeType() != xercesc::DOMNode::ATTRIBUTE_NODE)
00176         continue;
00177       xercesc::DOMAttr *attribute = static_cast <xercesc::DOMAttr *> (attributeNode);
00178       
00179       oss << "(" << x2s(child->getTagName()) << 
00180         x2s(attribute->getName()) << "=" << 
00181         x2s(attribute->getValue()) << ")";
00182     }
00183   }
00184   return oss.str();
00185 }
00186 
00187 const std::string
00188 LumiProducer::servletTranslation(const std::string& servlet) const{
00189   std::string frontierConnect;
00190   std::string realconnect;
00191   xercesc::XMLPlatformUtils::Initialize();  
00192   std::auto_ptr< xercesc::XercesDOMParser > parser(new xercesc::XercesDOMParser);
00193   try{
00194     parser->setValidationScheme(xercesc::XercesDOMParser::Val_Auto);
00195     parser->setDoNamespaces(false);
00196     parser->parse(m_siteconfpath.c_str());
00197     xercesc::DOMDocument* doc=parser->getDocument();
00198     if(!doc){
00199       return "";
00200     }
00201    
00202     xercesc::DOMNodeList *frontierConnectList=doc->getElementsByTagName(s2x("frontier-connect"));
00203     if (frontierConnectList->getLength()>0){
00204       xercesc::DOMElement *frontierConnectElement=static_cast < xercesc::DOMElement *> (frontierConnectList->item (0));
00205       frontierConnect = toParentString(*frontierConnectElement);
00206     }
00207     // Replace the last component of every "serverurl=" piece (up to the
00208     //   next close-paren) with the servlet
00209     std::string::size_type nextparen = 0;
00210     std::string::size_type serverurl, lastslash;
00211     std::string complexstr = "";
00212     while ((serverurl=frontierConnect.find("(serverurl=", nextparen)) != std::string::npos){
00213       realconnect.append(frontierConnect, nextparen, serverurl - nextparen);
00214       nextparen=frontierConnect.find(')', serverurl);
00215       lastslash=frontierConnect.rfind('/', nextparen);
00216       realconnect.append(frontierConnect,serverurl,lastslash-serverurl+1);
00217       realconnect.append(servlet);
00218     }
00219     realconnect.append(frontierConnect, nextparen,frontierConnect.length()-nextparen);
00220   }catch(xercesc::DOMException &e){
00221   }
00222   return realconnect;
00223 }
00224 
00225 LumiProducer::
00226 LumiProducer::LumiProducer(const edm::ParameterSet& iConfig):m_cachedrun(0),m_isNullRun(false),m_cachesize(0)
00227 {
00228   // register your products
00229   produces<LumiSummary, edm::InLumi>();
00230   produces<LumiDetails, edm::InLumi>();
00231   // set up cache
00232   std::string connectStr=iConfig.getParameter<std::string>("connect");
00233   m_cachesize=iConfig.getUntrackedParameter<unsigned int>("ncacheEntries",5);
00234   m_lumiversion=iConfig.getUntrackedParameter<std::string>("lumiversion");
00235   const std::string fproto("frontier://");
00236   //test if need frontier servlet site-local translation  
00237   if(connectStr.substr(0,fproto.length())==fproto){
00238     std::string::size_type startservlet=fproto.length();
00239     std::string::size_type endservlet=connectStr.find("(",startservlet);
00240     if(endservlet==std::string::npos){
00241       endservlet=connectStr.rfind('/',connectStr.length());
00242     }
00243     std::string servlet=connectStr.substr(startservlet,endservlet-startservlet);
00244     if( (servlet !="")&& (servlet.find_first_of(":/)[]")==std::string::npos)){
00245       if(servlet=="cms_conditions_data") servlet="";
00246       
00247       std::string siteconfpath=iConfig.getUntrackedParameter<std::string>("siteconfpath","");
00248       if(siteconfpath.length()==0){
00249         std::string url=(boost::filesystem::path("SITECONF")/boost::filesystem::path("local")/boost::filesystem::path("JobConfig")/boost::filesystem::path("site-local-config.xml")).string();
00250         char * tmp = getenv ("CMS_PATH");
00251         if(tmp){
00252           m_siteconfpath = (boost::filesystem::path(tmp)/boost::filesystem::path(url)).string();
00253         }
00254       }else{
00255         if(!boost::filesystem::exists(boost::filesystem::path(siteconfpath))){
00256           throw cms::Exception("Non existing path ")<<siteconfpath;
00257         }
00258         m_siteconfpath = (boost::filesystem::path(siteconfpath)/boost::filesystem::path("site-local-config.xml")).string();
00259       }
00260       //std::cout<<"servlet : "<<servlet<<std::endl;
00261       m_connectStr=fproto+servletTranslation(servlet)+connectStr.substr(endservlet);
00262     }else{
00263       m_connectStr=connectStr;
00264     }
00265   }else{
00266     m_connectStr=connectStr;
00267   }
00268   //std::cout<<"connect string "<< m_connectStr<<std::endl;
00269 }
00270 
00271 LumiProducer::~LumiProducer(){ 
00272 }
00273 //
00274 // member functions
00275 //
00276 void LumiProducer::produce(edm::Event& e, const edm::EventSetup& iSetup){ 
00277 }
00278 void LumiProducer::beginRun(edm::Run& run,edm::EventSetup const &iSetup){
00279   unsigned int runnumber=run.run();
00280   m_cachedrun=runnumber;
00281   fillRunCache(runnumber);
00282 }
00283 void LumiProducer::beginLuminosityBlock(edm::LuminosityBlock &iLBlock, edm::EventSetup const &iSetup){  
00284 }
00285 void LumiProducer::endLuminosityBlock(edm::LuminosityBlock & iLBlock, edm::EventSetup const& iSetup){
00286   unsigned int runnumber=iLBlock.run();
00287   unsigned int luminum=iLBlock.luminosityBlock();
00288   //if is null run, fill empty values and return
00289   if(m_isNullRun){
00290     std::auto_ptr<LumiSummary> pOut1;
00291     std::auto_ptr<LumiDetails> pOut2;
00292     LumiSummary* pIn1=new LumiSummary;
00293     LumiDetails* pIn2=new LumiDetails;
00294     pOut1.reset(pIn1);
00295     iLBlock.put(pOut1);
00296     pOut2.reset(pIn2);
00297     iLBlock.put(pOut2);
00298     return;
00299   }
00300   if(m_lscache.find(luminum)==m_lscache.end()){
00301     //if runnumber is cached but LS is not, this is the first LS, fill LS cache to full capacity
00302     fillLSCache(luminum);
00303   }
00304   //here the presence of ls is guaranteed
00305   writeProductsForEntry(iLBlock,runnumber,luminum); 
00306 }
00307 void 
00308 LumiProducer::fillRunCache(unsigned int runnumber){
00309   //queries once per run
00310   edm::Service<lumi::service::DBService> mydbservice;
00311   if( !mydbservice.isAvailable() ){
00312     throw cms::Exception("Non existing service lumi::service::DBService");
00313   }
00314   //std::cout<<"in fillRunCache "<<runnumber<<std::endl;
00315   coral::ISessionProxy* session=mydbservice->connectReadOnly(m_connectStr);
00316   try{
00317     session->transaction().start(true);
00318     coral::ISchema& schema=session->nominalSchema();
00319     //
00320     //select bitnum,bitname from trg where runnum=:runnum and cmslsnum=:1 order by bitnum;
00321     //
00322     //std::cout<<"got schema handle "<<std::endl;
00323     m_cachedrun=runnumber;
00324     coral::AttributeList trgBindVariables;
00325     trgBindVariables.extend("runnum",typeid(unsigned int));
00326     trgBindVariables.extend("cmslsnum",typeid(unsigned int));
00327     trgBindVariables["runnum"].data<unsigned int>()=runnumber;
00328     trgBindVariables["cmslsnum"].data<unsigned int>()=1;
00329     coral::AttributeList trgOutput;
00330     trgOutput.extend("bitnum",typeid(unsigned int));
00331     trgOutput.extend("bitname",typeid(std::string));
00332     coral::IQuery* trgQuery=schema.newQuery();
00333     trgQuery->addToTableList(lumi::LumiNames::trgTableName());
00334     trgQuery->addToOutputList("BITNUM");
00335     trgQuery->addToOutputList("BITNAME");
00336     trgQuery->setCondition("RUNNUM=:runnum AND CMSLSNUM=:cmslsnum",trgBindVariables);
00337     trgQuery->defineOutput(trgOutput);
00338     coral::ICursor& trgcursor=trgQuery->execute();
00339     unsigned int rowcounter=0;
00340     while( trgcursor.next() ){
00341       const coral::AttributeList& row=trgcursor.currentRow();
00342       m_runcache.TRGBitNames.insert(std::make_pair(row["bitnum"].data<unsigned int>(),row["bitname"].data<std::string>()));
00343       ++rowcounter;
00344     }
00345     delete trgQuery;
00346     if (rowcounter==0){
00347       m_isNullRun=true;
00348       session->transaction().commit();
00349       mydbservice->disconnect(session);
00350       return;
00351     }
00352     //
00353     //select pathname from from hlt where  runnum=:runnum and cmslsnum=:1 order by pathname;
00354     //
00355     coral::AttributeList hltBindVariables;
00356     hltBindVariables.extend("runnum",typeid(unsigned int));
00357     hltBindVariables.extend("cmslsnum",typeid(unsigned int));
00358     hltBindVariables["runnum"].data<unsigned int>()=runnumber;
00359     hltBindVariables["cmslsnum"].data<unsigned int>()=1;
00360     coral::AttributeList hltOutput;
00361     hltOutput.extend("pathname",typeid(std::string));
00362     coral::IQuery* hltQuery=schema.newQuery();
00363     hltQuery->addToTableList(lumi::LumiNames::hltTableName());
00364     hltQuery->addToOutputList("PATHNAME");
00365     hltQuery->setCondition("RUNNUM=:runnum AND CMSLSNUM=:cmslsnum",hltBindVariables);
00366     hltQuery->addToOrderList("PATHNAME");
00367     hltQuery->defineOutput(hltOutput);
00368     coral::ICursor& hltcursor=hltQuery->execute();
00369     rowcounter=0;
00370     unsigned int pathcount=0;
00371     while( hltcursor.next() ){
00372       const coral::AttributeList& row=hltcursor.currentRow();
00373       m_runcache.HLTPathNames.insert(std::make_pair(pathcount,row["pathname"].data<std::string>()));
00374       ++pathcount;
00375       ++rowcounter;
00376     }
00377     delete hltQuery;   
00378     if (rowcounter==0){
00379       m_isNullRun=true;
00380       session->transaction().commit();
00381       mydbservice->disconnect(session);
00382       return;
00383     }
00384     session->transaction().commit();
00385   }catch(const coral::Exception& er){
00386     session->transaction().rollback();
00387     mydbservice->disconnect(session);
00388     throw cms::Exception("DatabaseError ")<<er.what();
00389   }
00390   mydbservice->disconnect(session);
00391 }
00392 void
00393 LumiProducer::fillLSCache(unsigned int luminum){
00394   //std::cout<<"in fillLSCache "<<luminum<<std::endl;
00395   //initialize cache
00396   if(m_isNullRun) return;
00397   m_lscache.clear();
00398   for(unsigned int n=luminum;n<luminum+m_cachesize;++n){
00399     PerLSData l;
00400     l.hltdata.reserve(250);
00401     l.l1data.reserve(192);
00402     l.bunchlumivalue.reserve(5);
00403     l.bunchlumierror.reserve(5);
00404     l.bunchlumiquality.reserve(5);
00405     l.beam1intensity.resize(3564,0.0);
00406     l.beam2intensity.resize(3564,0.0);
00407     m_lscache.insert(std::make_pair(n,l));
00408   }
00409   //queries once per cache refill
00410   //
00411   //select cmslsnum,instlumi,instlumierror,lumiquality,startorbit,numorbit,bxindex,beam1intensity,beam2intensity from lumisummary where cmslsnum>=:lsmin and cmslsnum<:lsmax and runnum=:runnumber ;
00412   //
00413   edm::Service<lumi::service::DBService> mydbservice;
00414   if( !mydbservice.isAvailable() ){
00415     throw cms::Exception("Non existing service lumi::service::DBService");
00416   }
00417   coral::ISessionProxy* session=mydbservice->connectReadOnly(m_connectStr);
00418   try{
00419     session->transaction().start(true);
00420     coral::ISchema& schema=session->nominalSchema();
00421     coral::AttributeList lumisummaryBindVariables;
00422     lumisummaryBindVariables.extend("runnum",typeid(unsigned int));
00423     lumisummaryBindVariables.extend("lsmin",typeid(unsigned int));
00424     lumisummaryBindVariables.extend("lsmax",typeid(unsigned int));
00425   
00426     lumisummaryBindVariables["runnum"].data<unsigned int>()=m_cachedrun;
00427     lumisummaryBindVariables["lsmin"].data<unsigned int>()=luminum;
00428     lumisummaryBindVariables["lsmax"].data<unsigned int>()=luminum+m_cachesize;
00429     coral::AttributeList lumisummaryOutput;
00430     lumisummaryOutput.extend("cmslsnum",typeid(unsigned int));
00431     lumisummaryOutput.extend("instlumi",typeid(float));
00432     lumisummaryOutput.extend("instlumierror",typeid(float));
00433     lumisummaryOutput.extend("instlumiquality",typeid(short));
00434     lumisummaryOutput.extend("startorbit",typeid(unsigned int));
00435     lumisummaryOutput.extend("numorbit",typeid(unsigned int));
00436     lumisummaryOutput.extend("bxindexBlob",typeid(coral::Blob));
00437     lumisummaryOutput.extend("beam1intensityBlob",typeid(coral::Blob));
00438     lumisummaryOutput.extend("beam2intensityBlob",typeid(coral::Blob));
00439     
00440     coral::IQuery* lumisummaryQuery=schema.newQuery();
00441     lumisummaryQuery->addToTableList(lumi::LumiNames::lumisummaryTableName());
00442     lumisummaryQuery->addToOutputList("CMSLSNUM","cmslsnum");
00443     lumisummaryQuery->addToOutputList("INSTLUMI","instlumi");
00444     lumisummaryQuery->addToOutputList("INSTLUMIERROR","instlumierror");
00445     lumisummaryQuery->addToOutputList("INSTLUMIQUALITY","instlumiquality");
00446     lumisummaryQuery->addToOutputList("STARTORBIT","startorbit");
00447     lumisummaryQuery->addToOutputList("NUMORBIT","numorbit");
00448     lumisummaryQuery->addToOutputList("CMSBXINDEXBLOB","bxindexBlob");
00449     lumisummaryQuery->addToOutputList("BEAMINTENSITYBLOB_1","beam1intensityBlob");
00450     lumisummaryQuery->addToOutputList("BEAMINTENSITYBLOB_2","beam2intensityBlob");
00451     lumisummaryQuery->setCondition("RUNNUM=:runnum AND CMSLSNUM>=:lsmin AND CMSLSNUM<:lsmax",lumisummaryBindVariables);
00452     lumisummaryQuery->defineOutput(lumisummaryOutput);
00453     coral::ICursor& lumisummarycursor=lumisummaryQuery->execute();
00454     unsigned int rowcounter=0;
00455     while( lumisummarycursor.next() ){
00456       const coral::AttributeList& row=lumisummarycursor.currentRow();
00457       unsigned int cmslsnum=row["cmslsnum"].data<unsigned int>();
00458       //std::cout<<"cmslsnum "<<cmslsnum<<std::endl;
00459       PerLSData& lsdata=m_lscache[cmslsnum];
00460       lsdata.lumivalue=row["instlumi"].data<float>();
00461       lsdata.lumierror=row["instlumierror"].data<float>();
00462       lsdata.lumiquality=row["instlumiquality"].data<short>();
00463       lsdata.startorbit=row["startorbit"].data<unsigned int>();
00464       lsdata.numorbit=row["numorbit"].data<unsigned int>();
00465       
00466       if(!row["bxindexBlob"].isNull()){
00467         const coral::Blob& bxindexBlob=row["bxindexBlob"].data<coral::Blob>();
00468         const void* bxindex_StartAddress=bxindexBlob.startingAddress();
00469         short* bxindex=(short*)::malloc(bxindexBlob.size());
00470         const coral::Blob& beam1intensityBlob=row["beam1intensityBlob"].data<coral::Blob>();
00471         const void* beam1intensityBlob_StartAddress=beam1intensityBlob.startingAddress();
00472         float* beam1intensity=(float*)::malloc(beam1intensityBlob.size());
00473         const coral::Blob& beam2intensityBlob=row["beam2intensityBlob"].data<coral::Blob>();
00474         const void* beam2intensityBlob_StartAddress=beam2intensityBlob.startingAddress();
00475         float* beam2intensity=(float*)::malloc(beam2intensityBlob.size());
00476         std::memmove(bxindex,bxindex_StartAddress,bxindexBlob.size());
00477         std::memmove(beam1intensity,beam1intensityBlob_StartAddress,beam1intensityBlob.size());
00478         std::memmove(beam2intensity,beam2intensityBlob_StartAddress,beam2intensityBlob.size());
00479         //std::cout<<"lsnum,pos,bxidx,beam1intensity,beam2intensity "<<std::endl;
00480         for(unsigned int i=0;i<bxindexBlob.size()/sizeof(short);++i){
00481           unsigned int idx=bxindex[i];
00482           lsdata.beam1intensity.at(idx)=beam1intensity[i];
00483           lsdata.beam2intensity.at(idx)=beam2intensity[i];
00484           //std::cout<<cmslsnum<<","<<i<<","<<idx<<","<<beam1intensity[i]<<","<<beam2intensity[i]<<std::endl;
00485         }
00486         ::free(bxindex);
00487         ::free(beam1intensity);
00488         ::free(beam2intensity);
00489       }
00490       ++rowcounter;
00491     }
00492     if (rowcounter==0){
00493       m_isNullRun=true;
00494       return;
00495     }
00496     delete lumisummaryQuery;
00497     
00498     //
00499     //select lumisummary.cmslsnum,lumidetail.bxlumivalue,lumidetail.bxlumierror,lumidetail.bxlumiquality,lumidetail.algoname from lumisummary,lumidetail where lumisummary.lumisummary_id=lumidetail.lumisummary_id and lumisummary.runnum=:runnum and lumisummary.cmslsnum>=:luminum and lumisummary.cmslsnum<:luminum+cachesize order by lumidetail.algoname,lumisummary.cmslsnum
00500     //
00501     coral::AttributeList lumidetailBindVariables;
00502     lumidetailBindVariables.extend("runnum",typeid(unsigned int));
00503     lumidetailBindVariables.extend("lsmin",typeid(unsigned int));
00504     lumidetailBindVariables.extend("lsmax",typeid(unsigned int));
00505     
00506     lumidetailBindVariables["runnum"].data<unsigned int>()=m_cachedrun;
00507     lumidetailBindVariables["lsmin"].data<unsigned int>()=luminum;
00508     lumidetailBindVariables["lsmax"].data<unsigned int>()=luminum+m_cachesize;
00509     coral::AttributeList lumidetailOutput;
00510     
00511     lumidetailOutput.extend("cmslsnum",typeid(unsigned int));
00512     lumidetailOutput.extend("bxlumivalue",typeid(coral::Blob));
00513     lumidetailOutput.extend("bxlumierror",typeid(coral::Blob));
00514     lumidetailOutput.extend("bxlumiquality",typeid(coral::Blob));
00515     lumidetailOutput.extend("algoname",typeid(std::string));
00516 
00517     coral::IQuery* lumidetailQuery=schema.newQuery();
00518     lumidetailQuery->addToTableList(lumi::LumiNames::lumisummaryTableName());
00519     lumidetailQuery->addToTableList(lumi::LumiNames::lumidetailTableName());
00520     lumidetailQuery->addToOutputList(lumi::LumiNames::lumisummaryTableName()+".CMSLSNUM","cmslsnum");
00521     lumidetailQuery->addToOutputList(lumi::LumiNames::lumidetailTableName()+".BXLUMIVALUE","bxlumivalue");
00522     lumidetailQuery->addToOutputList(lumi::LumiNames::lumidetailTableName()+".BXLUMIERROR","bxlumierror");
00523     lumidetailQuery->addToOutputList(lumi::LumiNames::lumidetailTableName()+".BXLUMIQUALITY","instlumiquality");
00524     lumidetailQuery->addToOutputList(lumi::LumiNames::lumidetailTableName()+".ALGONAME","algoname");
00525     lumidetailQuery->setCondition(lumi::LumiNames::lumisummaryTableName()+".LUMISUMMARY_ID="+lumi::LumiNames::lumidetailTableName()+".LUMISUMMARY_ID AND "+lumi::LumiNames::lumisummaryTableName()+".RUNNUM=:runnum AND "+lumi::LumiNames::lumisummaryTableName()+".CMSLSNUM>=:lsmin AND "+lumi::LumiNames::lumisummaryTableName()+".CMSLSNUM<:lsmax",lumidetailBindVariables);
00526     lumidetailQuery->addToOrderList(lumi::LumiNames::lumidetailTableName()+".ALGONAME");
00527     lumidetailQuery->addToOrderList(lumi::LumiNames::lumisummaryTableName()+".CMSLSNUM");
00528     lumidetailQuery->defineOutput(lumidetailOutput);
00529     coral::ICursor& lumidetailcursor=lumidetailQuery->execute();
00530     while( lumidetailcursor.next() ){
00531       const coral::AttributeList& row=lumidetailcursor.currentRow();
00532       unsigned int cmslsnum=row["cmslsnum"].data<unsigned int>();
00533       std::string algoname=row["algoname"].data<std::string>();
00534       //std::cout<<"cmslsnum "<<cmslsnum<<" "<<algoname<<std::endl;
00535       PerLSData& lsdata=m_lscache[cmslsnum];
00536       if( !row["bxlumivalue"].isNull() && !row["bxlumierror"].isNull() && !row["bxlumiquality"].isNull() ){
00537         const coral::Blob& bxlumivalueBlob=row["bxlumivalue"].data<coral::Blob>();
00538         const coral::Blob& bxlumierrorBlob=row["bxlumierror"].data<coral::Blob>();
00539         const coral::Blob& bxlumiqualityBlob=row["bxlumiquality"].data<coral::Blob>();
00540         const void* bxlumivalueBlob_StartAddress=bxlumivalueBlob.startingAddress();
00541         const void* bxlumierrorBlob_StartAddress=bxlumierrorBlob.startingAddress();
00542         const void* bxlumiqualityBlob_StartAddress=bxlumiqualityBlob.startingAddress();
00543         float* bxlumivalue=(float*)::malloc(bxlumivalueBlob.size());
00544         float* bxlumierror=(float*)::malloc(bxlumierrorBlob.size());
00545         short* bxlumiquality=(short*)::malloc(bxlumiqualityBlob.size());
00546         std::memmove(bxlumivalue,bxlumivalueBlob_StartAddress,bxlumivalueBlob.size());
00547         std::memmove(bxlumierror,bxlumierrorBlob_StartAddress,bxlumierrorBlob.size());
00548         std::memmove(bxlumiquality,bxlumiqualityBlob_StartAddress,bxlumiqualityBlob.size());
00549         std::vector<float> bxlumivalueVec(bxlumivalue,bxlumivalue+bxlumivalueBlob.size()/sizeof(float));
00550         ::free(bxlumivalue);
00551         lsdata.bunchlumivalue.push_back(std::make_pair(algoname,bxlumivalueVec));
00552         std::vector<float> bxlumierrorVec(bxlumierror,bxlumierror+bxlumierrorBlob.size()/sizeof(float));
00553         ::free(bxlumierror);
00554         lsdata.bunchlumierror.push_back(std::make_pair(algoname,bxlumierrorVec));
00555         std::vector<short> bxlumiqualityVec(bxlumiquality,bxlumiquality+bxlumiqualityBlob.size()/sizeof(short));
00556         lsdata.bunchlumiquality.push_back(std::make_pair(algoname,bxlumiqualityVec));
00557         ::free(bxlumiquality);
00558       }
00559     }
00560     delete lumidetailQuery;
00561     //
00562     //select cmslsnum,bitnum,deadtime,prescale,trgcount from trg where cmslsnum >=:luminum and cmslsnum<:luminum+cachesize AND runnum=:runnum order by cmslsnum,bitnum
00563     //
00564     coral::AttributeList trgBindVariables;
00565     trgBindVariables.extend("runnum",typeid(unsigned int));
00566     trgBindVariables.extend("lsmin",typeid(unsigned int));
00567     trgBindVariables.extend("lsmax",typeid(unsigned int));
00568     trgBindVariables["runnum"].data<unsigned int>()=m_cachedrun;
00569     trgBindVariables["lsmin"].data<unsigned int>()=luminum;
00570     trgBindVariables["lsmax"].data<unsigned int>()=luminum+m_cachesize;
00571     coral::AttributeList trgOutput;
00572     trgOutput.extend("cmslsnum",typeid(unsigned int));
00573     trgOutput.extend("bitnum",typeid(unsigned int));
00574     trgOutput.extend("deadtime",typeid(unsigned long long));
00575     trgOutput.extend("prescale",typeid(unsigned int));
00576     trgOutput.extend("trgcount",typeid(unsigned int));
00577     
00578     coral::IQuery* trgQuery=schema.newQuery();
00579     trgQuery->addToTableList(lumi::LumiNames::trgTableName());
00580     trgQuery->addToOutputList("CMSLSNUM","cmslsnum");
00581     trgQuery->addToOutputList("BITNUM","bitnum");
00582     trgQuery->addToOutputList("DEADTIME","deadtime");
00583     trgQuery->addToOutputList("PRESCALE","prescale");
00584     trgQuery->addToOutputList("TRGCOUNT","trgcount");
00585     trgQuery->setCondition("CMSLSNUM>=:lsmin AND CMSLSNUM<:lsmax AND RUNNUM=:runnum ",trgBindVariables);
00586     trgQuery->addToOrderList("CMSLSNUM");
00587     trgQuery->addToOrderList("BITNUM");
00588     trgQuery->defineOutput(trgOutput);
00589     coral::ICursor& trgcursor=trgQuery->execute();
00590     while( trgcursor.next() ){
00591       const coral::AttributeList& row=trgcursor.currentRow();
00592       unsigned int cmslsnum=row["cmslsnum"].data<unsigned int>();
00593       PerLSData& lsdata=m_lscache[cmslsnum];
00594       lsdata.deadcount=row["deadtime"].data<unsigned long long>();
00595       L1Data l1tmp;
00596       l1tmp.bitnum=row["bitnum"].data<unsigned int>();
00597       l1tmp.prescale=row["prescale"].data<unsigned int>();
00598       l1tmp.ratecount=row["trgcount"].data<unsigned int>();
00599       lsdata.l1data.push_back(l1tmp);
00600     }
00601     delete trgQuery;
00602     //
00603     //select cmslsnum,inputcount,acceptcount,prescale from hlt where cmslsnum >=:luminum and cmslsnum<=:luminum+cachesize and runnum=:runnumber order by cmslsum,pathname
00604     //
00605     coral::AttributeList hltBindVariables;
00606     hltBindVariables.extend("runnum",typeid(unsigned int));
00607     hltBindVariables.extend("lsmin",typeid(unsigned int));
00608     hltBindVariables.extend("lsmax",typeid(unsigned int));
00609     hltBindVariables["runnum"].data<unsigned int>()=m_cachedrun;
00610     hltBindVariables["lsmin"].data<unsigned int>()=luminum;
00611     hltBindVariables["lsmax"].data<unsigned int>()=luminum+m_cachesize;
00612     coral::AttributeList hltOutput;
00613     hltOutput.extend("cmslsnum",typeid(unsigned int));
00614     hltOutput.extend("inputcount",typeid(unsigned int));
00615     hltOutput.extend("acceptcount",typeid(unsigned int));
00616     hltOutput.extend("prescale",typeid(unsigned int));
00617     coral::IQuery* hltQuery=schema.newQuery();
00618     hltQuery->addToTableList(lumi::LumiNames::hltTableName());
00619     hltQuery->addToOutputList("CMSLSNUM","cmslsnum");
00620     hltQuery->addToOutputList("INPUTCOUNT","inputcount");
00621     hltQuery->addToOutputList("ACCEPTCOUNT","acceptcount");
00622     hltQuery->addToOutputList("PRESCALE","prescale");
00623     hltQuery->setCondition("CMSLSNUM>=:lsmin AND CMSLSNUM<:lsmax AND RUNNUM=:runnum",hltBindVariables);
00624     hltQuery->addToOrderList("CMSLSNUM");
00625     hltQuery->addToOrderList("PATHNAME");
00626     hltQuery->defineOutput(hltOutput);
00627     coral::ICursor& hltcursor=hltQuery->execute();
00628     unsigned int npaths=m_runcache.HLTPathNames.size();
00629     unsigned int pathcount=0;
00630     while( hltcursor.next() ){
00631       const coral::AttributeList& row=hltcursor.currentRow();   
00632       unsigned int cmslsnum=row["cmslsnum"].data<unsigned int>();
00633       PerLSData& lsdata=m_lscache[cmslsnum];
00634       HLTData hlttmp;
00635       hlttmp.pathnum=pathcount;
00636       hlttmp.prescale=row["prescale"].data<unsigned int>();
00637       hlttmp.l1passcount=row["inputcount"].data<unsigned int>();
00638       hlttmp.acceptcount=row["acceptcount"].data<unsigned int>();
00639       lsdata.hltdata.push_back(hlttmp);
00640       if(pathcount!=npaths){
00641         ++pathcount;
00642       }else{
00643         pathcount=0;
00644       }
00645     }
00646     delete hltQuery;
00647     session->transaction().commit();
00648   }catch(const coral::Exception& er){
00649     session->transaction().rollback();
00650     mydbservice->disconnect(session);
00651     throw cms::Exception("DatabaseError ")<<er.what();
00652   }
00653   mydbservice->disconnect(session);
00654 }
00655 void
00656 LumiProducer::writeProductsForEntry(edm::LuminosityBlock & iLBlock,unsigned int runnumber,unsigned int luminum){
00657   std::auto_ptr<LumiSummary> pOut1;
00658   std::auto_ptr<LumiDetails> pOut2;
00659   LumiSummary* pIn1=new LumiSummary;
00660   LumiDetails* pIn2=new LumiDetails;
00661   if(m_isNullRun){
00662     pIn1->setLumiVersion("-1");
00663     pIn2->setLumiVersion("-1");
00664     pOut1.reset(pIn1);
00665     iLBlock.put(pOut1);
00666     pOut2.reset(pIn2);
00667     iLBlock.put(pOut2);
00668     return;
00669   }
00670   PerLSData& lsdata=m_lscache[luminum];
00671   pIn1->setLumiData(lsdata.lumivalue,lsdata.lumierror,lsdata.lumiquality);
00672   pIn1->setDeadtime(lsdata.deadcount);
00673   pIn1->setlsnumber(luminum);
00674   pIn1->setOrbitData(lsdata.startorbit,lsdata.numorbit);
00675   std::vector<LumiSummary::L1> l1temp;
00676   for(std::vector< L1Data >::iterator it=lsdata.l1data.begin();it!=lsdata.l1data.end();++it){
00677     LumiSummary::L1 trgtmp;
00678     trgtmp.prescale=it->prescale;
00679     trgtmp.ratecount=it->ratecount;
00680     trgtmp.triggername=m_runcache.TRGBitNames[it->bitnum];
00681     l1temp.push_back(trgtmp);
00682   }
00683   std::vector<LumiSummary::HLT> hlttemp;
00684   for(std::vector< HLTData >::iterator it=lsdata.hltdata.begin();it!=lsdata.hltdata.end();++it){
00685     LumiSummary::HLT hlttmp;
00686     hlttmp.prescale=it->prescale;
00687     hlttmp.ratecount=it->acceptcount;
00688     hlttmp.inputcount=it->l1passcount;
00689     hlttmp.pathname=m_runcache.HLTPathNames[it->pathnum];
00690     hlttemp.push_back(hlttmp);
00691   }
00692   pIn1->swapL1Data(l1temp);
00693   pIn1->swapHLTData(hlttemp);
00694   pIn1->setLumiVersion(m_lumiversion);  
00695   pIn2->fillBeamIntensities(lsdata.beam1intensity,lsdata.beam2intensity);
00696   for(unsigned int i=0;i<lsdata.bunchlumivalue.size();++i){
00697     std::string algoname=lsdata.bunchlumivalue[i].first;
00698     if(algoname=="OCC1"){
00699       pIn2->fill(LumiDetails::kOCC1,lsdata.bunchlumivalue[i].second,lsdata.bunchlumierror[i].second,lsdata.bunchlumiquality[i].second);
00700     }else if(algoname=="OCC2"){      
00701       pIn2->fill(LumiDetails::kOCC2,lsdata.bunchlumivalue[i].second,lsdata.bunchlumierror[i].second,lsdata.bunchlumiquality[i].second);
00702     }else if(algoname=="ET"){
00703       pIn2->fill(LumiDetails::kET,lsdata.bunchlumivalue[i].second,lsdata.bunchlumierror[i].second,lsdata.bunchlumiquality[i].second);
00704     }else if(algoname=="PLT"){
00705       pIn2->fill(LumiDetails::kPLT,lsdata.bunchlumivalue[i].second,lsdata.bunchlumierror[i].second,lsdata.bunchlumiquality[i].second);
00706     }
00707   }
00708   pIn2->setLumiVersion(m_lumiversion);
00709   pOut1.reset(pIn1);
00710   iLBlock.put(pOut1);
00711   pOut2.reset(pIn2);
00712   iLBlock.put(pOut2);
00713 }
00714 #include "FWCore/Framework/interface/MakerMacros.h"
00715 DEFINE_FWK_MODULE(LumiProducer);