CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/DQM/HLTEvF/plugins/HLTMonSimpleBTag.cc

Go to the documentation of this file.
00001 // $Id: HLTMonSimpleBTag.cc,v 1.3 2011/03/25 16:10:43 fblekman Exp $
00002 // See header file for information. 
00003 #include "FWCore/Framework/interface/EDAnalyzer.h"
00004 #include "DataFormats/Common/interface/Handle.h"
00005 #include "FWCore/Framework/interface/Run.h"
00006 #include "FWCore/Framework/interface/MakerMacros.h"
00007 
00008 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00009 #include "DQM/HLTEvF/interface/HLTMonSimpleBTag.h"
00010 
00011 #include "DataFormats/HLTReco/interface/TriggerObject.h"
00012 #include "DataFormats/HLTReco/interface/TriggerEvent.h"
00013 
00014 #include "DQMServices/Core/interface/MonitorElement.h"
00015 
00016 #include "DataFormats/Math/interface/deltaR.h"
00017 #include "DQMServices/Core/interface/DQMNet.h"
00018 
00019 
00020 using namespace edm;
00021 
00022 HLTMonSimpleBTag::HLTMonSimpleBTag(const edm::ParameterSet& iConfig):
00023   resetMe_(true),  currentRun_(-99)
00024 {
00025   LogDebug("HLTMonSimpleBTag") << "constructor...." ;
00026 
00027   dbe_ = Service < DQMStore > ().operator->();
00028   if ( ! dbe_ ) {
00029     LogWarning("Status") << "unable to get DQMStore service?";
00030   }
00031   if (iConfig.getUntrackedParameter < bool > ("DQMStore", false)) {
00032     dbe_->setVerbose(0);
00033   }
00034   
00035   
00036   dirname_="HLT/HLTMonSimpleBTag" ;
00037   
00038   if (dbe_ != 0 ) {
00039     LogDebug("Status") << "Setting current directory to " << dirname_;
00040     dbe_->setCurrentFolder(dirname_);
00041   }
00042   
00043   
00044   // plotting paramters
00045   ptMin_ = iConfig.getUntrackedParameter<double>("ptMin",0.);
00046   ptMax_ = iConfig.getUntrackedParameter<double>("ptMax",200.);
00047   nBins_ = iConfig.getUntrackedParameter<unsigned int>("Nbins",50);
00048   dRTrigObjMatch_ = iConfig.getUntrackedParameter<double>("dRMatch",0.3);
00049   refresheff_ = iConfig.getUntrackedParameter<unsigned int>("Nrefresh",10);
00050  
00051  
00052 
00053   // this is the list of paths to look at.
00054   std::vector<edm::ParameterSet> filters = 
00055     iConfig.getParameter<std::vector<edm::ParameterSet> >("filters");
00056   for(std::vector<edm::ParameterSet>::iterator 
00057         filterconf = filters.begin() ; filterconf != filters.end(); 
00058       filterconf++) {
00059     std::string me  = filterconf->getParameter<std::string>("name");
00060     std::string denom = filterconf->getParameter<std::string>("refname");
00061     // only fill if both triggers are not there yet.
00062     if(hltPaths_.find(me)==hltPaths_.end())
00063       hltPaths_.push_back(PathInfo(me,ptMin_, ptMax_));
00064     if(hltPaths_.find(denom)==hltPaths_.end())
00065       hltPaths_.push_back(PathInfo(denom, ptMin_, ptMax_));
00066     
00067     std::string effname = makeEffName(me,denom);
00068     std::string numername=makeEffNumeratorName(me,denom);
00069     std::pair<std::string,std::string> trigpair(me,denom);
00070     if(find(triggerMap_.begin(),triggerMap_.end(),trigpair)==triggerMap_.end())
00071       triggerMap_.push_back(trigpair);
00072     if(hltEfficiencies_.find(effname)==hltEfficiencies_.end())
00073       hltEfficiencies_.push_back(PathInfo(effname,ptMin_,ptMax_));
00074     if(hltEfficiencies_.find(numername)==hltEfficiencies_.end())
00075       hltEfficiencies_.push_back(PathInfo(numername,ptMin_,ptMax_));
00076 
00077   }
00078   triggerSummaryLabel_ = 
00079     iConfig.getParameter<edm::InputTag>("triggerSummaryLabel");
00080 }
00081 
00082 
00083 HLTMonSimpleBTag::~HLTMonSimpleBTag()
00084 {
00085  
00086    // do anything here that needs to be done at desctruction time
00087    // (e.g. close files, deallocate resources etc.)
00088 
00089 }
00090 
00091 
00092 //
00093 // member functions
00094 //
00095 
00096 // ------------ method called to for each event  ------------
00097 void
00098 HLTMonSimpleBTag::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
00099 {
00100   using namespace edm;
00101   using namespace trigger;
00102   ++nev_;
00103   LogDebug("Status")<< "analyze" ;
00104   
00105   edm::Handle<TriggerEvent> triggerObj;
00106   iEvent.getByLabel(triggerSummaryLabel_,triggerObj); 
00107   if(!triggerObj.isValid()) { 
00108     edm::LogInfo("Status") << "Summary HLT object (TriggerEvent) not found, "
00109       "skipping event"; 
00110     return;
00111   }
00112   
00113   const trigger::TriggerObjectCollection & toc(triggerObj->getObjects());
00114 
00115   for ( size_t ia = 0; ia < triggerObj->sizeFilters(); ++ ia) {
00116     std::string fullname = triggerObj->filterTag(ia).encode();
00117     // the name can have in it the module label as well as the process and
00118     // other labels - strip 'em
00119     std::string name;
00120     size_t p = fullname.find_first_of(':');
00121     if ( p != std::string::npos) {
00122       name = fullname.substr(0, p);
00123     }
00124     else {
00125       name = fullname;
00126     }
00127     
00128     LogDebug("Parameter")  << "filter " << ia << ", full name = " << fullname
00129                           << ", p = " << p 
00130                           << ", abbreviated = " << name ;
00131     //      std::cout << std::endl;
00132     // check that trigger is in 'watch list'
00133     PathInfoCollection::iterator pic = hltPaths_.find(name);
00134     if(pic==hltPaths_.end())
00135       continue;
00136     
00137     // find keys 
00138     
00139     const trigger::Keys & k = triggerObj->filterKeys(ia);
00140     for (trigger::Keys::const_iterator ki = k.begin(); ki !=k.end(); ++ki ) {
00141       LogDebug("Parameters")  << name << "(" << ki-k.begin() << "): pt, eta, phi = " 
00142                              << toc[*ki].pt() << ", "
00143                              << toc[*ki].eta() << ", "
00144                               << toc[*ki].phi()  <<","
00145                               << toc[*ki].id();
00146       pic->getEtHisto()->Fill(toc[*ki].pt());
00147       pic->getEtaHisto()->Fill(toc[*ki].eta());
00148       pic->getPhiHisto()->Fill(toc[*ki].phi());
00149       pic->getEtaVsPhiHisto()->Fill(toc[*ki].eta(), toc[*ki].phi());
00150     }
00151 
00152     // check which trigger this trigger is reference to, fill those histograms separately:
00153     for(std::vector<std::pair<std::string, std::string> >::iterator matchIter = triggerMap_.begin(); matchIter!=triggerMap_.end(); ++matchIter){
00154       // only do anything if you actually have a match
00155       if(matchIter->second!=name)
00156         continue;
00157       LogDebug("HLTMonSimpleBTag") << "found match! " << " " << matchIter->first << " " << matchIter->second ;
00158       // now go find this one in the trigger collection... this is somewhat painful :(
00159       for(size_t ib = 0; ib < triggerObj->sizeFilters(); ++ ib) {
00160         if(ib==ia)
00161           continue;
00162         std::string fullname_b = triggerObj->filterTag(ib).encode();
00163         // the name can have in it the module label as well as the process and
00164         // other labels - strip 'em
00165         std::string name_b;
00166         size_t p_b = fullname_b.find_first_of(':');
00167         if ( p_b != std::string::npos) {
00168           name_b = fullname_b.substr(0, p_b);
00169         }
00170         else {
00171           name_b = fullname_b;
00172         }
00173         // this is where the matching to the trigger array happens
00174         if(name_b!=matchIter->first)
00175           continue;
00176         
00177         // ok, now we have two matching triggers with indices ia and ib in the trigger index. Get the keys for ib.
00178         
00179         // find the correct monitoring element:
00180         std::string numeratorname = makeEffNumeratorName(matchIter->first,matchIter->second);
00181         PathInfoCollection::iterator pic_b = hltEfficiencies_.find(numeratorname);
00182         if(pic_b==hltEfficiencies_.end())
00183           continue;
00184 
00185         const trigger::Keys & k_b = triggerObj->filterKeys(ib);
00186 
00187         const trigger::Keys & k = triggerObj->filterKeys(ia);
00188         for (trigger::Keys::const_iterator ki = k.begin(); ki !=k.end(); ++ki ) {
00189           for (trigger::Keys::const_iterator ki_b = k_b.begin(); ki_b !=k_b.end(); ++ki_b ) {
00190             // do cone match...
00191             if(reco::deltaR(toc[*ki].eta(),toc[*ki_b].eta(),toc[*ki].eta(),toc[*ki_b].eta())>dRTrigObjMatch_)
00192               continue;
00193             
00194             LogDebug("Parameters") << "matched pt, eta, phi = " 
00195                                    << toc[*ki].pt() << ", "
00196                                    << toc[*ki].eta() << ", "
00197                                    << toc[*ki].phi() << " to " 
00198                                    << toc[*ki_b].pt() << ", "
00199                                    << toc[*ki_b].eta() << ", "
00200                                    << toc[*ki_b].phi() << " (using the first to fill histo to avoid division problems...)";
00201             // as these are going to be divided it is important to fill numerator and denominator with the same pT spectrum. So this is why these are filled with ki objects instead of ki_b objects...
00202             pic_b->getEtHisto()->Fill(toc[*ki].pt());
00203             pic_b->getEtaHisto()->Fill(toc[*ki].eta());
00204             pic_b->getPhiHisto()->Fill(toc[*ki].phi());
00205             pic_b->getEtaVsPhiHisto()->Fill(toc[*ki].eta(), toc[*ki].phi());
00206             // for now just take the first match there are two keys within the same cone...
00207             break;
00208           }
00209         }
00210       }
00211     }
00212   }
00213   // and calculate efficiencies, only if the number of events is the refresh rate:
00214   if(nev_%refresheff_==0 && nev_!=1){
00215     calcEff();
00216   }
00217 }
00218 
00219 
00220 // -- method called once each job just before starting event loop  --------
00221 void 
00222 HLTMonSimpleBTag::beginJob()
00223 {
00224   nev_ = 0;
00225   DQMStore *dbe = 0;
00226   dbe = Service<DQMStore>().operator->();
00227   
00228   if (dbe) {
00229     dbe->setCurrentFolder(dirname_);
00230     dbe->rmdir(dirname_);
00231   }
00232   
00233   
00234   if (dbe) {
00235     dbe->setCurrentFolder(dirname_);
00236 
00237     for(PathInfoCollection::iterator v = hltPaths_.begin();
00238         v!= hltPaths_.end(); ++v ) {
00239       MonitorElement *et, *eta, *phi, *etavsphi=0;
00240       std::string histoname(v->getName()+"_et");
00241       std::string title(v->getName()+" E_t");
00242       et =  dbe->book1D(histoname.c_str(),
00243                         title.c_str(),nBins_,
00244                         v->getPtMin(),
00245                         v->getPtMax());
00246       
00247       histoname = v->getName()+"_eta";
00248       title = v->getName()+" #eta";
00249       eta =  dbe->book1D(histoname.c_str(),
00250                          title.c_str(),nBins_/2,-2.7,2.7);
00251       
00252       histoname = v->getName()+"_phi";
00253       title = v->getName()+" #phi";
00254       phi =  dbe->book1D(histoname.c_str(),
00255                          histoname.c_str(),nBins_/2,-3.14,3.14);
00256       
00257       
00258       histoname = v->getName()+"_etaphi";
00259       title = v->getName()+" #eta vs #phi";
00260       etavsphi =  dbe->book2D(histoname.c_str(),
00261                               title.c_str(),
00262                               nBins_/2,-2.7,2.7,
00263                               nBins_/2,-3.14, 3.14);
00264       
00265       v->setHistos( et, eta, phi, etavsphi);
00266     }
00267 
00268     for(PathInfoCollection::iterator v = hltEfficiencies_.begin();
00269         v!= hltEfficiencies_.end(); ++v ) {
00270       MonitorElement *et, *eta, *phi, *etavsphi=0;
00271       std::string histoname(v->getName()+"_et");
00272       std::string title(v->getName()+" E_t");
00273       et =  dbe->book1D(histoname.c_str(),
00274                         title.c_str(),nBins_,
00275                         v->getPtMin(),
00276                         v->getPtMax());
00277       
00278       histoname = v->getName()+"_eta";
00279       title = v->getName()+" #eta";
00280       eta =  dbe->book1D(histoname.c_str(),
00281                          title.c_str(),nBins_/2,-2.7,2.7);
00282       
00283       histoname = v->getName()+"_phi";
00284       title = v->getName()+" #phi";
00285       phi =  dbe->book1D(histoname.c_str(),
00286                          histoname.c_str(),nBins_/2,-3.14,3.14);
00287       
00288       
00289       histoname = v->getName()+"_etaphi";
00290       title = v->getName()+" #eta vs #phi";
00291       etavsphi =  dbe->book2D(histoname.c_str(),
00292                               title.c_str(),
00293                               nBins_/2,-2.7,2.7,
00294                               nBins_/2,-3.14, 3.14);
00295       
00296       v->setHistos( et, eta, phi, etavsphi);
00297     }
00298   }
00299 }
00300 
00301 // - method called once each job just after ending the event loop  ------------
00302 void 
00303 HLTMonSimpleBTag::endJob() 
00304 {
00305    LogInfo("Status") << "endJob: analyzed " << nev_ << " events";
00306    return;
00307 }
00308 
00309 
00310 // BeginRun
00311 void HLTMonSimpleBTag::beginRun(const edm::Run& run, const edm::EventSetup& c)
00312 {
00313   LogDebug("Status") << "beginRun, run " << run.id();
00314 }
00315 
00317 void HLTMonSimpleBTag::endRun(const edm::Run& run, const edm::EventSetup& c)
00318 {
00319   LogDebug("Status") << "final re-calculation efficiencies!" ;
00320   calcEff();
00321   LogDebug("Status") << "endRun, run " << run.id();
00322   
00323 }
00324 
00325 
00327 
00328 void 
00329 HLTMonSimpleBTag::calcEff(void){
00330  
00331   for( std::vector<std::pair<std::string,std::string> >::iterator iter = triggerMap_.begin(); iter!=triggerMap_.end(); iter++){
00332     if(hltPaths_.find(iter->first)==hltPaths_.end())
00333       continue;
00334     if(hltPaths_.find(iter->second)==hltPaths_.end())
00335       continue;
00336     
00337     std::string effname = makeEffName(iter->first,iter->second);
00338     std::string numeratorname = makeEffNumeratorName(iter->first,iter->second);
00339     LogDebug("HLTMonBTagSlim::calcEff") << "calculating efficiencies for histogram with effname=" << effname << " using also histogram " << numeratorname ;
00340     PathInfoCollection::iterator effHists = hltEfficiencies_.find(effname);
00341     if(effHists==hltEfficiencies_.end())
00342       continue;
00343     PathInfoCollection::iterator numerHists = hltEfficiencies_.find(numeratorname);
00344     if(numerHists==hltEfficiencies_.end())
00345       continue;
00346     LogDebug("HLTMonBTagSlim::calcEff") << "found histo with name " << effname << " and " << numeratorname << "!" ;
00347     // do the hists all separately:
00348 
00349     doEffCalc(effHists->getEtHisto(),numerHists->getEtHisto(),hltPaths_.find(iter->second)->getEtHisto());
00350     doEffCalc(effHists->getEtaHisto(),numerHists->getEtaHisto(),hltPaths_.find(iter->second)->getEtaHisto());
00351     doEffCalc(effHists->getPhiHisto(),numerHists->getPhiHisto(),hltPaths_.find(iter->second)->getPhiHisto());
00352     doEffCalc(effHists->getEtaVsPhiHisto(),numerHists->getEtaVsPhiHisto(),hltPaths_.find(iter->second)->getEtaVsPhiHisto());
00353 
00354 
00355   }
00356   LogDebug("HLTMonBTagSlim::calcEff") << "done with efficiencies!" ;
00357 }
00358 
00359 // function to actually do the efficiency calculation per-bin
00360 void 
00361 HLTMonSimpleBTag::doEffCalc(MonitorElement *eff, MonitorElement *num, MonitorElement *denom){
00362   double x,y,errx,erry;
00363 
00364   // presuming error propagation is non-quadratic (binominal):
00365   // so: err!= Sqrt(errx^2/x^2+erry^2/y^2) but instead
00366   // 
00367   // err=sqrt(errx^2/x^2+erry^2/y^2)* x/y*(1-x/y)
00368   bool is1d=false;
00369   bool is2d=false;
00370   if(num->kind()==DQMNet::DQM_PROP_TYPE_TH1F || num->kind()==DQMNet::DQM_PROP_TYPE_TH1S || num->kind()== DQMNet::DQM_PROP_TYPE_TH1D)
00371     is1d=true;
00372   else if(num->kind()==DQMNet::DQM_PROP_TYPE_TH2F || num->kind()==DQMNet::DQM_PROP_TYPE_TH2S || num->kind()== DQMNet::DQM_PROP_TYPE_TH2D)
00373     is2d=true;
00374   
00375 
00376   for(int ibin=0; ibin<=eff->getNbinsX() && ibin<=num->getNbinsX(); ibin++){
00377     if(is1d){ // 1D histograms!
00378       x=num->getBinContent(ibin);
00379       errx=num->getBinError(ibin);
00380       y=denom->getBinContent(ibin);
00381       erry=denom->getBinError(ibin);
00382       if(fabs(y)<0.00001 || fabs(x)<0.0001){// very stringent non-zero!
00383         eff->setBinContent(ibin,0);
00384         eff->setBinError(ibin,0);
00385         continue;
00386       }
00387 
00388       LogDebug("HLTMonSimpleBTag::calcEff()") << eff->getName() << " (" << ibin << ") " <<  x << " " << y;
00389       eff->setBinContent(ibin,x/y);
00390       eff->setBinError(ibin,sqrt((errx*errx)/(x*x)+(erry*erry)/(y*y))* (x/y)*(1-(x/y)));
00391     }
00392     else if(is2d){ // 2D histograms!
00393       for(int jbin=0; jbin<=num->getNbinsY(); jbin++){
00394         x=num->getBinContent(ibin,jbin);
00395         errx=num->getBinError(ibin,jbin);
00396         y=denom->getBinContent(ibin,jbin);
00397         erry=denom->getBinError(ibin,jbin);
00398         if(fabs(y)<0.0001 || fabs(x)<0.0001){ // very stringent non-zero!
00399           eff->setBinContent(ibin,jbin,0.);
00400           eff->setBinError(ibin,jbin,0.);
00401           continue;
00402         }
00403         LogDebug("HLTMonSimpleBTag::calcEff()") << eff->getName() << " (" << ibin<< "," << jbin << ") " <<  x << " " << y ;
00404           
00405         eff->setBinContent(ibin,jbin,x/y);
00406         eff->setBinError(ibin,jbin,sqrt((errx*errx)/(x*x)+(erry*erry)/(y*y))* (x/y)*(1-(x/y)));
00407       }
00408     }
00409   }
00410 }
00411