CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/DQMOffline/Trigger/src/EgHLTOfflineSource.cc

Go to the documentation of this file.
00001 #include "DQMOffline/Trigger/interface/EgHLTOfflineSource.h"
00002 
00003 #include "DQMOffline/Trigger/interface/EgHLTEleHLTFilterMon.h"
00004 #include "DQMOffline/Trigger/interface/EgHLTPhoHLTFilterMon.h"
00005 
00006 #include "DQMOffline/Trigger/interface/EgHLTDebugFuncs.h"
00007 #include "DQMOffline/Trigger/interface/EgHLTDQMCut.h"
00008 #include "DQMOffline/Trigger/interface/EgHLTMonElemFuncs.h"
00009 #include "DQMOffline/Trigger/interface/EgHLTTrigTools.h"
00010 
00011 #include "DQMServices/Core/interface/MonitorElement.h"
00012 
00013 #include "FWCore/Framework/interface/Run.h"
00014 #include "HLTrigger/HLTcore/interface/HLTConfigProvider.h"
00015 
00016 #include <boost/algorithm/string.hpp>
00017 
00018 //#include "DQMOffline/Trigger/interface/EgHLTCutCodes.h"
00019 #include "DataFormats/TrackReco/interface/Track.h"
00020 #include "DataFormats/EgammaCandidates/interface/ElectronFwd.h"
00021 #include "DataFormats/HLTReco/interface/TriggerFilterObjectWithRefs.h"
00022 using namespace egHLT;
00023 
00024 EgHLTOfflineSource::EgHLTOfflineSource(const edm::ParameterSet& iConfig):
00025   nrEventsProcessed_(0),isSetup_(false)
00026 {
00027   dbe_ = edm::Service<DQMStore>().operator->(); //only one chance to get this, if we every have another shot, remember to check isSetup is okay
00028   if (!dbe_) {
00029     //our one and only error message ever logged
00030     edm::LogInfo("EgHLTOfflineSource") << "unable to get DQMStore service?";
00031   }
00032   if(iConfig.getUntrackedParameter<bool>("DQMStore", false)) {
00033     dbe_->setVerbose(0);
00034   }
00035 
00036  
00037   binData_.setup(iConfig.getParameter<edm::ParameterSet>("binData"));
00038   cutMasks_.setup(iConfig.getParameter<edm::ParameterSet>("cutMasks"));
00039   eleHLTFilterNames_ = iConfig.getParameter<std::vector<std::string> >("eleHLTFilterNames");
00040   phoHLTFilterNames_ = iConfig.getParameter<std::vector<std::string> >("phoHLTFilterNames");
00041   eleTightLooseTrigNames_ = iConfig.getParameter<std::vector<std::string> >("eleTightLooseTrigNames");
00042   diEleTightLooseTrigNames_ = iConfig.getParameter<std::vector<std::string> >("diEleTightLooseTrigNames"); 
00043   phoTightLooseTrigNames_ = iConfig.getParameter<std::vector<std::string> >("phoTightLooseTrigNames");
00044   diPhoTightLooseTrigNames_ = iConfig.getParameter<std::vector<std::string> >("diPhoTightLooseTrigNames"); 
00045 
00046   filterInactiveTriggers_ =iConfig.getParameter<bool>("filterInactiveTriggers");
00047   hltTag_ = iConfig.getParameter<std::string>("hltTag");
00048  
00049   
00050   dirName_=iConfig.getParameter<std::string>("DQMDirName");//"HLT/EgHLTOfflineSource_" + iConfig.getParameter<std::string>("@module_label");
00051 
00052  
00053   offEvtHelper_.setup(iConfig);
00054 
00055 }
00056 
00057 
00058 EgHLTOfflineSource::~EgHLTOfflineSource()
00059 { 
00060   // LogDebug("EgHLTOfflineSource") << "destructor called";
00061   for(size_t i=0;i<eleFilterMonHists_.size();i++){
00062     delete eleFilterMonHists_[i];
00063   } 
00064   for(size_t i=0;i<phoFilterMonHists_.size();i++){
00065     delete phoFilterMonHists_[i];
00066   }
00067   for(size_t i=0;i<eleMonElems_.size();i++){
00068     delete eleMonElems_[i];
00069   } 
00070   for(size_t i=0;i<phoMonElems_.size();i++){
00071     delete phoMonElems_[i];
00072   }
00073 }
00074 
00075 void EgHLTOfflineSource::beginJob()
00076 {
00077   if(dbe_) {
00078     dbe_->setCurrentFolder(dirName_);
00079     //the one monitor element the source fills directly
00080     dqmErrsMonElem_ =dbe_->book1D("dqmErrors","EgHLTOfflineSource Errors",101,-0.5,100.5);
00081     nrEventsProcessedMonElem_ = dbe_->bookInt("nrEventsProcessed");
00082   }
00083 }
00084 
00085 void EgHLTOfflineSource::endJob() 
00086 {
00087   //  LogDebug("EgHLTOfflineSource") << "ending job";
00088 }
00089 
00090 //due to changes in HLTConfigProvider to be called at beginRun rather then beginJob, I moved everything from beginJob here and ensure that it is only called once
00091 //if the HLTConfig changes during the job, the results are "un predicable" but in practice should be fine
00092 //the HLTConfig is used for working out which triggers are active, working out which filternames correspond to paths and L1 seeds
00093 //assuming those dont change for E/g it *should* be fine 
00094 void EgHLTOfflineSource::beginRun(const edm::Run& run, const edm::EventSetup& c)
00095 {
00096   if(dbe_ && !isSetup_){
00097 
00098     HLTConfigProvider hltConfig;
00099     bool changed=false;
00100     hltConfig.init(run,c,hltTag_,changed);
00101     if(filterInactiveTriggers_) filterTriggers(hltConfig);
00102 
00103     std::vector<std::string> hltFiltersUsed;
00104     getHLTFilterNamesUsed(hltFiltersUsed);
00105     TrigCodes::setCodes(hltFiltersUsed);
00106    
00107     offEvtHelper_.setupTriggers(hltConfig,hltFiltersUsed);
00108 
00109     //now book ME's
00110     dbe_->setCurrentFolder(dirName_);
00111     //each trigger path with generate object distributions and efficiencies (BUT not trigger efficiencies...)
00112     for(size_t i=0;i<eleHLTFilterNames_.size();i++) addEleTrigPath(eleHLTFilterNames_[i]);
00113     for(size_t i=0;i<phoHLTFilterNames_.size();i++) addPhoTrigPath(phoHLTFilterNames_[i]);
00114     
00115     //efficiencies of one trigger path relative to another
00116     MonElemFuncs::initTightLooseTrigHists(eleMonElems_,eleTightLooseTrigNames_,binData_,"gsfEle");
00118     MonElemFuncs::initTightLooseTrigHistsTrigCuts(eleMonElems_,eleTightLooseTrigNames_,binData_);
00119       
00120    
00121     MonElemFuncs::initTightLooseTrigHists(phoMonElems_,phoTightLooseTrigNames_,binData_,"pho");
00122     //  new EgHLTDQMVarCut<OffPho>(cutMasks_.stdPho,&OffPho::cutCode)); 
00123     MonElemFuncs::initTightLooseTrigHistsTrigCuts(phoMonElems_,phoTightLooseTrigNames_,binData_);
00124       
00125     //di-object triggers
00126     MonElemFuncs::initTightLooseTrigHists(eleMonElems_,diEleTightLooseTrigNames_,binData_,"gsfEle");
00127     //  new EgDiEleCut(cutMasks_.stdEle,&OffEle::cutCode));
00128     MonElemFuncs::initTightLooseTrigHists(phoMonElems_,diPhoTightLooseTrigNames_,binData_,"pho");
00129     //                          new EgDiPhoCut(cutMasks_.stdPho,&OffPho::cutCode));
00130     
00131     MonElemFuncs::initTightLooseDiObjTrigHistsTrigCuts(eleMonElems_,diEleTightLooseTrigNames_,binData_);
00132     MonElemFuncs::initTightLooseDiObjTrigHistsTrigCuts(phoMonElems_,diPhoTightLooseTrigNames_,binData_);
00133 
00134     
00135     //tag and probe trigger efficiencies
00136     //this is to do measure the trigger efficiency with respect to a fully selected offline electron
00137     //using a tag and probe technique (note: this will be different to the trigger efficiency normally calculated) 
00138     MonElemFuncs::initTrigTagProbeHists(eleMonElems_,eleHLTFilterNames_,cutMasks_.trigTPEle,binData_);
00139     
00140     //tag and probe not yet implimented for photons (attemping to see if it makes sense first)
00141     // MonElemFuncs::initTrigTagProbeHists(phoMonElems,phoHLTFilterNames_);
00142 
00143     isSetup_=true;
00144     
00145   }
00146 }
00147 
00148 
00149 void EgHLTOfflineSource::endRun(const edm::Run& run, const edm::EventSetup& c)
00150 {
00151   //LogDebug("EgHLTOfflineSource") << "endRun, run " << run.id();
00152 }
00153 
00154 
00155 void EgHLTOfflineSource::analyze(const edm::Event& iEvent,const edm::EventSetup& iSetup)
00156 { 
00157   //debugging info, commented out for prod
00158   //  int nrProducts = debug::listAllProducts<edm::ValueMap<double> >(iEvent,"EgHLTOfflineSource");
00159   //edm::LogInfo("EgHLTOfflineSource")<<" HERE ";
00160   // std::cout <<"here"<<std::endl;
00161   if(!dbe_) return;
00162    
00163  
00164  
00165 
00166 
00167   const double weight=1.; //we have the ability to weight but its disabled for now
00168   nrEventsProcessed_++;
00169   nrEventsProcessedMonElem_->Fill(nrEventsProcessed_);
00170   int errCode = offEvtHelper_.makeOffEvt(iEvent,iSetup,offEvt_);
00171   if(errCode!=0){
00172     dqmErrsMonElem_->Fill(errCode);
00173     return;
00174   }
00175 
00176 
00177   for(size_t pathNr=0;pathNr<eleFilterMonHists_.size();pathNr++){
00178     eleFilterMonHists_[pathNr]->fill(offEvt_,weight);
00179   } 
00180   for(size_t pathNr=0;pathNr<phoFilterMonHists_.size();pathNr++){
00181     phoFilterMonHists_[pathNr]->fill(offEvt_,weight);
00182   }
00183 
00184   for(size_t monElemNr=0;monElemNr<eleMonElems_.size();monElemNr++){
00185     const std::vector<OffEle>& eles = offEvt_.eles();
00186     for(size_t eleNr=0;eleNr<eles.size();eleNr++){
00187       eleMonElems_[monElemNr]->fill(eles[eleNr],offEvt_,weight);
00188     }
00189   }
00190 
00191   for(size_t monElemNr=0;monElemNr<phoMonElems_.size();monElemNr++){
00192     const std::vector<OffPho>& phos = offEvt_.phos();
00193     for(size_t phoNr=0;phoNr<phos.size();phoNr++){
00194       phoMonElems_[monElemNr]->fill(phos[phoNr],offEvt_,weight);
00195     }
00196   }
00197 }
00198 
00199 
00200 void EgHLTOfflineSource::addEleTrigPath(const std::string& name)
00201 {
00202   EleHLTFilterMon* filterMon = new EleHLTFilterMon(name,TrigCodes::getCode(name.c_str()),binData_,cutMasks_);  
00203   eleFilterMonHists_.push_back(filterMon);
00204   std::sort(eleFilterMonHists_.begin(),eleFilterMonHists_.end(),EleHLTFilterMon::ptrLess<EleHLTFilterMon>()); //takes a minor efficiency hit at initalisation to ensure that the vector is always sorted
00205 }
00206 
00207 void EgHLTOfflineSource::addPhoTrigPath(const std::string& name)
00208 {
00209   PhoHLTFilterMon* filterMon = new PhoHLTFilterMon(name,TrigCodes::getCode(name.c_str()),binData_,cutMasks_);
00210   phoFilterMonHists_.push_back(filterMon);
00211   std::sort(phoFilterMonHists_.begin(),phoFilterMonHists_.end(),PhoHLTFilterMon::ptrLess<PhoHLTFilterMon>()); //takes a minor efficiency hit at initalisation to ensure that the vector is always sorted
00212 }
00213 
00214 //this function puts every filter name used in a std::vector
00215 //due to the design, to ensure we get every filter, filters will be inserted multiple times
00216 //eg electron filters will contain photon triggers which are also in the photon filters
00217 //but only want one copy in the vector
00218 //this function is intended to be called once per job so some inefficiency can can be tolerated
00219 //therefore we will use a std::set to ensure ensure that each filtername is only inserted once
00220 //and then convert to a std::vector
00221 void EgHLTOfflineSource::getHLTFilterNamesUsed(std::vector<std::string>& filterNames)const
00222 { 
00223   std::set<std::string> filterNameSet;
00224   for(size_t i=0;i<eleHLTFilterNames_.size();i++) filterNameSet.insert(eleHLTFilterNames_[i]);
00225   for(size_t i=0;i<phoHLTFilterNames_.size();i++) filterNameSet.insert(phoHLTFilterNames_[i]);
00226   //here we are little more complicated as entries are of the form "tightTrig:looseTrig" 
00227   //so we need to split them first
00228   for(size_t tightLooseNr=0;tightLooseNr<eleTightLooseTrigNames_.size();tightLooseNr++){
00229     std::vector<std::string> trigNames;
00230     boost::split(trigNames,eleTightLooseTrigNames_[tightLooseNr],boost::is_any_of(std::string(":")));
00231     if(trigNames.size()!=2) continue; //format incorrect
00232     filterNameSet.insert(trigNames[0]);
00233     filterNameSet.insert(trigNames[1]);
00234   }
00235   for(size_t tightLooseNr=0;tightLooseNr<diEleTightLooseTrigNames_.size();tightLooseNr++){
00236     std::vector<std::string> trigNames;
00237     boost::split(trigNames,diEleTightLooseTrigNames_[tightLooseNr],boost::is_any_of(std::string(":")));
00238     if(trigNames.size()!=2) continue; //format incorrect
00239     filterNameSet.insert(trigNames[0]);
00240     filterNameSet.insert(trigNames[1]);
00241   }
00242   for(size_t tightLooseNr=0;tightLooseNr<phoTightLooseTrigNames_.size();tightLooseNr++){
00243     std::vector<std::string> trigNames;
00244     boost::split(trigNames,phoTightLooseTrigNames_[tightLooseNr],boost::is_any_of(std::string(":")));
00245     if(trigNames.size()!=2) continue; //format incorrect
00246     filterNameSet.insert(trigNames[0]);
00247     filterNameSet.insert(trigNames[1]);
00248   } 
00249   for(size_t tightLooseNr=0;tightLooseNr<diPhoTightLooseTrigNames_.size();tightLooseNr++){
00250     std::vector<std::string> trigNames;
00251     boost::split(trigNames,diPhoTightLooseTrigNames_[tightLooseNr],boost::is_any_of(std::string(":")));
00252     if(trigNames.size()!=2) continue; //format incorrect
00253     filterNameSet.insert(trigNames[0]);
00254     filterNameSet.insert(trigNames[1]);
00255   }
00256   //right all the triggers are inserted once and only once in the set, convert to vector
00257   //very lazy, create a new vector so can use the constructor and then use swap to transfer
00258   std::vector<std::string>(filterNameSet.begin(),filterNameSet.end()).swap(filterNames);
00259 }
00260 
00261 void EgHLTOfflineSource::filterTriggers(const HLTConfigProvider& hltConfig)
00262 {
00263   
00264   std::vector<std::string> activeFilters;
00265   
00266   trigTools::getActiveFilters(hltConfig,activeFilters);
00267   
00268   trigTools::filterInactiveTriggers(eleHLTFilterNames_,activeFilters);
00269   trigTools::filterInactiveTriggers(phoHLTFilterNames_,activeFilters);
00270   trigTools::filterInactiveTightLooseTriggers(eleTightLooseTrigNames_,activeFilters);
00271   trigTools::filterInactiveTightLooseTriggers(diEleTightLooseTrigNames_,activeFilters);
00272   trigTools::filterInactiveTightLooseTriggers(phoTightLooseTrigNames_,activeFilters);
00273   trigTools::filterInactiveTightLooseTriggers(diPhoTightLooseTrigNames_,activeFilters);                             
00274 }