CMS 3D CMS Logo

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

Go to the documentation of this file.
00001 #ifndef RecoLuminosity_LumiProducer_LumiCalculator_h
00002 #define RecoLuminosity_LumiProducer_LumiCalculator_h
00003 #include "FWCore/Framework/interface/EDAnalyzer.h"
00004 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00005 #include "FWCore/Framework/interface/Event.h"
00006 #include "FWCore/Framework/interface/Run.h"
00007 #include "FWCore/Framework/interface/LuminosityBlock.h"
00008 #include "DataFormats/Common/interface/Handle.h"
00009 #include "DataFormats/Luminosity/interface/LumiSummaryRunHeader.h"
00010 #include "DataFormats/Luminosity/interface/LumiSummary.h"
00011 #include "FWCore/Framework/interface/MakerMacros.h"
00012 #include "HLTrigger/HLTcore/interface/HLTConfigProvider.h"
00013 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00014 #include <boost/regex.hpp>
00015 #include <iostream>
00016 #include <map>
00017 struct hltPerPathInfo{
00018   hltPerPathInfo():prescale(0){}
00019   unsigned int prescale;
00020 };
00021 struct l1PerBitInfo{
00022   l1PerBitInfo():prescale(0){}
00023   unsigned int prescale;
00024 };
00025 struct MyPerLumiInfo{
00026   unsigned int lsnum;
00027   float livefraction;
00028   float intglumi;
00029   unsigned long long deadcount;
00030 };
00031 
00032 class LumiCalculator : public edm::EDAnalyzer{
00033 public:
00034   
00035   explicit LumiCalculator(edm::ParameterSet const& pset);
00036   virtual ~LumiCalculator();
00037 
00038 private:  
00039   virtual void beginJob();
00040   virtual void beginRun(const edm::Run& run, const edm::EventSetup& c);
00041   virtual void analyze(edm::Event const& e, edm::EventSetup const& c);
00042   virtual void endLuminosityBlock(edm::LuminosityBlock const& lumiBlock, 
00043                                   edm::EventSetup const& c);
00044   virtual void endRun(edm::Run const&, edm::EventSetup const&);
00045   virtual void endJob();
00046   std::vector<std::string> splitpathstr(const std::string& strValue,const std::string separator);
00047   HLTConfigProvider hltConfig_;
00048   std::multimap<std::string,std::string> trgpathMmap_;//key:hltpath,value:l1bit
00049   std::map<std::string,hltPerPathInfo> hltmap_;
00050   std::map<std::string,l1PerBitInfo> l1map_;//
00051   std::vector<MyPerLumiInfo> perrunlumiinfo_;
00052 private:
00053   edm::LogInfo* log_;
00054   bool showTrgInfo_;
00055   unsigned int currentlumi_;
00056 };//end class
00057 
00058 // -----------------------------------------------------------------
00059 
00060 LumiCalculator::LumiCalculator(edm::ParameterSet const& pset):log_( new edm::LogInfo("LumiReport")),currentlumi_(0){
00061   showTrgInfo_=pset.getUntrackedParameter<bool>("showTriggerInfo",false);
00062 }
00063 
00064 // -----------------------------------------------------------------
00065 
00066 LumiCalculator::~LumiCalculator(){
00067   delete log_; log_=0; 
00068 }
00069 
00070 // -----------------------------------------------------------------
00071 
00072 void LumiCalculator::analyze(edm::Event const& e,edm::EventSetup const&){
00073   
00074 }
00075 
00076 // -----------------------------------------------------------------
00077 
00078 void LumiCalculator::beginJob(){
00079   
00080 }
00081 
00082 // -----------------------------------------------------------------
00083 
00084 void LumiCalculator::beginRun(const edm::Run& run, const edm::EventSetup& c){
00085   //std::cout<<"I'm in run number "<<run.run()<<std::endl;
00086   //if(!hltConfig_.init("HLT")){
00087   // throw cms::Exception("HLT process cannot be initialized");
00088   //}
00089   bool changed(true);
00090   const std::string processname("HLT");
00091   if(!hltConfig_.init(run,c,processname,changed)){
00092     throw cms::Exception("HLT process cannot be initialized");
00093   }
00094   perrunlumiinfo_.clear();
00095   trgpathMmap_.clear();
00096   hltmap_.clear();
00097   l1map_.clear();
00098   //hltConfig_.dump("processName");
00099   //hltConfig_.dump("TableName");
00100   //hltConfig_.dump("Triggers");
00101   //hltConfig_.dump("Modules");  
00102   if(showTrgInfo_){
00103     *log_<<"======Trigger Configuration Overview======\n";
00104     *log_<<"Run "<<run.run()<<" Trigger Table : "<<hltConfig_.tableName()<<"\n";
00105   }
00106   unsigned int totaltrg=hltConfig_.size();
00107   for (unsigned int t=0;t<totaltrg;++t){
00108     std::string hltname(hltConfig_.triggerName(t));
00109     std::vector<std::string> numpathmodules=hltConfig_.moduleLabels(hltname);
00110     if(showTrgInfo_){
00111       *log_<<t<<" HLT path\t"<<hltname<<"\n";
00112     }
00113     hltPerPathInfo hlt;
00114     hlt.prescale=1;
00115     hltmap_.insert(std::make_pair(hltname,hlt));
00116     std::vector<std::string>::iterator hltpathBeg=numpathmodules.begin();
00117     std::vector<std::string>::iterator hltpathEnd=numpathmodules.end();
00118     unsigned int mycounter=0;
00119     for(std::vector<std::string>::iterator numpathmodule = hltpathBeg;
00120         numpathmodule!=hltpathEnd; ++numpathmodule ) {
00121       if (hltConfig_.moduleType(*numpathmodule) != "HLTLevel1GTSeed"){
00122         continue;
00123       }
00124       ++mycounter;
00125      
00126       edm::ParameterSet l1GTPSet=hltConfig_.modulePSet(*numpathmodule);
00127       std::string l1pathname=l1GTPSet.getParameter<std::string>("L1SeedsLogicalExpression");
00128       //*log_<<"numpathmodule "<< *numpathmodule <<" : l1pathname "<<l1pathname<<"\n";
00129       if(mycounter>1){
00130         if(showTrgInfo_){
00131           *log_<<"\tskip and erase previous seeds : multiple L1SeedsLogicalExpressions per hlt path\n";
00132         }
00133         //erase all previously calculated seeds for this path
00134         trgpathMmap_.erase(hltname);
00135         continue;
00136       }
00137       if(l1pathname.find("(")!=std::string::npos){
00138         if(showTrgInfo_){
00139           *log_<<"  L1SeedsLogicalExpression(Complex)\t"<<l1pathname<<"\n";
00140           *log_<<"\tskip:contain complex logic\n";
00141         }
00142         continue;
00143       }else if(l1pathname.find("OR")!=std::string::npos){
00144         if(showTrgInfo_){
00145           *log_<<"  L1SeedsLogicalExpression(ORed)\t"<<l1pathname<<"\n";
00146         }
00147         std::vector<std::string> seeds=splitpathstr(l1pathname," OR ");
00148         if(seeds.size()>2){
00149           if(showTrgInfo_){
00150             *log_<<"\tskip:contain >1 OR\n";
00151           }
00152           continue;
00153         }else{
00154           for(std::vector<std::string>::iterator i=seeds.begin();i!=seeds.end();++i){
00155             if(i->size()!=0 && showTrgInfo_)  *log_<<"\t\tseed: "<<*i<<"\n";
00156             if(i==seeds.begin()){//for now we take the first one from OR
00157                 trgpathMmap_.insert(std::make_pair(hltname,*i));
00158             }
00159           }
00160         }
00161       }else if (l1pathname.find("AND")!=std::string::npos){
00162         if(showTrgInfo_){
00163           *log_<<"  L1SeedsLogicalExpression(ANDed)\t"<< l1pathname<< "\n";
00164         }
00165         std::vector<std::string> seeds=splitpathstr(l1pathname," AND ");
00166         if(seeds.size()>2){
00167           if(showTrgInfo_){
00168             *log_<<"\tskip:contain >1 AND\n";
00169           }
00170           continue;
00171         }else{
00172           for(std::vector<std::string>::iterator i=seeds.begin();
00173               i!=seeds.end();++i){
00174             if(i->size()!=0 && showTrgInfo_) *log_<<"\t\tseed: "<<*i<<"\n";
00175             if(i==seeds.begin()){//for now we take the first one 
00176               trgpathMmap_.insert(std::make_pair(hltname,*i));
00177             }
00178           }
00179         }
00180       }else{
00181         if(showTrgInfo_){
00182           *log_<<"  L1SeedsLogicalExpression(ONE)\t"<< l1pathname<<"\n";
00183         }
00184         if(splitpathstr(l1pathname," NOT ").size()>1){
00185           if(showTrgInfo_){
00186             *log_<<"\tskip:contain NOT\n";
00187           }
00188           continue;
00189         }
00190         trgpathMmap_.insert(std::make_pair(hltname,l1pathname));
00191       } 
00192     }
00193   }
00194   if(showTrgInfo_){
00195     *log_<<"================\n";
00196   }
00197 }
00198 
00199 // -----------------------------------------------------------------
00200 void LumiCalculator::endLuminosityBlock(edm::LuminosityBlock const& lumiBlock, 
00201                                         edm::EventSetup const& c){
00205   //std::cout<<"I'm in lumi block "<<lumiBlock.id()<<std::endl;
00206   
00207   edm::Handle<LumiSummary> lumiSummary;
00208   lumiBlock.getByLabel("lumiProducer", lumiSummary);
00209 
00210   edm::Handle<LumiSummaryRunHeader> lumiSummaryRH;
00211   lumiBlock.getRun().getByLabel("lumiProducer", lumiSummaryRH);
00212 
00213   MyPerLumiInfo l;
00214   l.lsnum=lumiBlock.id().luminosityBlock();
00215 
00216   //
00217   //collect lumi. 
00218   //
00219   l.deadcount=lumiSummary->deadcount();
00220   l.intglumi=lumiSummary->avgInsDelLumi()*93.244;
00221   l.livefraction=lumiSummary->liveFrac();
00222   
00223   *log_<<"====== Lumi Section "<<lumiBlock.id().luminosityBlock()<<" ======\n";
00224   *log_<<"\t Luminosity "<<l.intglumi<<"\n";
00225   *log_<<"\t Dead count "<<l.deadcount<<"\n";
00226   *log_<<"\t Deadtime corrected Luminosity "<<l.intglumi*l.livefraction<<"\n";
00227 
00228   //
00229   //print correlated hlt-l1 info, only if you ask
00230   //
00231   if(showTrgInfo_){
00232     std::map<std::string,hltPerPathInfo>::iterator hltit;
00233     std::map<std::string,hltPerPathInfo>::iterator hltitBeg=hltmap_.begin();
00234     std::map<std::string,hltPerPathInfo>::iterator hltitEnd=hltmap_.end();
00235     
00236     typedef std::pair< std::multimap<std::string,std::string>::iterator,std::multimap<std::string,std::string>::iterator > TRGMAPIT;
00237     unsigned int c=0;
00238     for(hltit=hltitBeg;hltit!=hltitEnd;++hltit){
00239       std::string hltname=hltit->first;
00240       *log_<<c<<" HLT path  "<<hltname<<" , prescale : "<<hltit->second.prescale<<"\n";
00241       TRGMAPIT ppp;
00242       ppp=trgpathMmap_.equal_range(hltname);
00243       if(ppp.first==ppp.second){
00244         *log_<<"    no L1\n";
00245       }
00246       for(std::multimap<std::string,std::string>::iterator mit=ppp.first; mit!=ppp.second; ++mit){
00247         std::string l1name=mit->second;
00248         *log_<<"    L1 name : "<<l1name;
00249         LumiSummary::L1 l1result = lumiSummary->l1info(lumiSummaryRH->getL1Index(l1name));
00250         *log_<<" prescale : "<<l1result.prescale<<"\n";
00251         *log_<<"\n";
00252       }
00253       ++c;
00254     }
00255   }
00256   //
00257   //accumulate hlt counts. Absent for now
00258   //
00264   //
00265   //accumulate l1 counts
00266   //
00267   size_t n=lumiSummary->nTriggerLine();
00268   for(size_t i=0;i<n;++i){
00269     std::string l1bitname = lumiSummaryRH->getL1Name(lumiSummary->l1info(i).triggernameidx);
00270     l1PerBitInfo t;
00271     if(currentlumi_==0){
00272       t.prescale=lumiSummary->l1info(i).prescale;
00273       l1map_.insert(std::make_pair(l1bitname,t));
00274     }
00275   }
00276   
00277   perrunlumiinfo_.push_back(l);
00278 
00279   ++currentlumi_;
00280 }
00281  
00282 // -----------------------------------------------------------------
00283 void LumiCalculator::endRun(edm::Run const& run, edm::EventSetup const& c){
00300   //std::cout<<"valid trigger lines "<<trgpathMmap_.size()<<std::endl;
00301   //std::cout<<"total lumi lines "<<perrunlumiinfo_.size()<<std::endl;
00302   std::vector<MyPerLumiInfo>::const_iterator lumiIt;
00303   std::vector<MyPerLumiInfo>::const_iterator lumiItBeg=perrunlumiinfo_.begin();
00304   std::vector<MyPerLumiInfo>::const_iterator lumiItEnd=perrunlumiinfo_.end(); 
00305   float recorded=0.0;
00306   
00307   *log_<<"================ Run Summary "<<run.run()<<"================\n";
00308   for(lumiIt=lumiItBeg;lumiIt!=lumiItEnd;++lumiIt){//loop over LS
00309     recorded += lumiIt->intglumi*lumiIt->livefraction;  
00310   }
00311   *log_<<"  CMS Recorded Lumi (e+27cm^-2) : "<<recorded<<"\n";
00312   *log_<<"  Effective Lumi (e+27cm^-2) per trigger path: "<<"\n\n";
00313   std::multimap<std::string,std::string>::iterator it;
00314   std::multimap<std::string,std::string>::iterator itBeg=trgpathMmap_.begin();
00315   std::multimap<std::string,std::string>::iterator itEnd=trgpathMmap_.end();
00316   unsigned int cc=0;
00317   for(it=itBeg;it!=itEnd;++it){
00318     *log_<<"  "<<cc<<"  "<<it->first<<" - "<<it->second<<" : ";
00319     ++cc;
00320     std::map<std::string,hltPerPathInfo>::const_iterator hltIt=hltmap_.find(it->first);
00321     if( hltIt==hltmap_.end() ){
00322       std::cout<<"HLT path "<<it->first<<" not found"<<std::endl;
00323       *log_<<"\n";
00324       continue;
00325     }
00326     std::map<std::string,l1PerBitInfo>::const_iterator l1It=l1map_.find(it->second);
00327     if( l1It==l1map_.end() ){
00328       std::cout<<"L1 bit "<<it->second<<" not found"<<std::endl;
00329       *log_<<"\n";
00330       continue;
00331     }
00332     unsigned int hltprescale=hltIt->second.prescale;
00333     unsigned int l1prescale=l1It->second.prescale;
00334     if( hltprescale!=0 && l1prescale!=0 ){
00335       float effectiveLumi=recorded/(hltprescale*l1prescale);
00336       *log_<<effectiveLumi<<"\n";
00337     }else{
00338       *log_<<"0 prescale exception\n";
00339       continue;
00340     }
00341     *log_<<"\n"; 
00342   }
00343 }
00344 
00345 
00346 // -----------------------------------------------------------------
00347 void LumiCalculator::endJob(){
00348 }
00349 
00350 std::vector<std::string>
00351 LumiCalculator::splitpathstr(const std::string& strValue,const std::string separator){
00352   std::vector<std::string> vecstrResult;
00353   boost::regex re(separator);
00354   boost::sregex_token_iterator p(strValue.begin(),strValue.end(),re,-1);
00355   boost::sregex_token_iterator end;
00356   while(p!=end){
00357     vecstrResult.push_back(*p++);
00358   }
00359   return vecstrResult;
00360 }
00361 
00362 DEFINE_FWK_MODULE(LumiCalculator);
00363 #endif