CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_2_9_HLT1_bphpatch4/src/DQMOffline/Trigger/src/EgHLTTrigTools.cc

Go to the documentation of this file.
00001 #include "DQMOffline/Trigger/interface/EgHLTTrigTools.h"
00002 
00003 #include "FWCore/ParameterSet/interface/Registry.h"
00004 
00005 #include "HLTrigger/HLTcore/interface/HLTConfigProvider.h"
00006 
00007 #include <boost/algorithm/string.hpp>
00008 using namespace egHLT;
00009 
00010 TrigCodes::TrigBitSet trigTools::getFiltersPassed(const std::vector<std::pair<std::string,int> >& filters,const trigger::TriggerEvent* trigEvt,const std::string& hltTag)
00011 {
00012   TrigCodes::TrigBitSet evtTrigs;
00013   for(size_t filterNrInVec=0;filterNrInVec<filters.size();filterNrInVec++){
00014     size_t filterNrInEvt = trigEvt->filterIndex(edm::InputTag(filters[filterNrInVec].first,"",hltTag).encode());
00015     const TrigCodes::TrigBitSet filterCode = TrigCodes::getCode(filters[filterNrInVec].first.c_str());
00016     if(filterNrInEvt<trigEvt->sizeFilters()){ //filter found in event, however this only means that something passed the previous filter
00017       const trigger::Keys& trigKeys = trigEvt->filterKeys(filterNrInEvt);
00018       if(static_cast<int>(trigKeys.size())>=filters[filterNrInVec].second){
00019         evtTrigs |=filterCode; //filter was passed
00020       }
00021     }//end check if filter is present
00022   }//end loop over all filters
00023 
00024 
00025   return evtTrigs;
00026 
00027 }
00028 
00029 
00030 
00031 
00032 //this function runs over all parameter sets for every module that has ever run on an event in this job
00033 //it looks for the specified filter module
00034 //and returns the minimum number of objects required to pass the filter, -1 if its not found
00035 //which is either the ncandcut or MinN parameter in the filter config
00036 //assumption: nobody will ever change MinN or ncandcut without changing the filter name
00037 //as this just picks the first module name and if 2 different versions of HLT were run with the filter having
00038 //a different min obj required in the two versions, this may give the wrong answer
00039 int trigTools::getMinNrObjsRequiredByFilter(const std::string& filterName)
00040 {
00041  
00042   //will return out of for loop once its found it to save time
00043   const edm::pset::Registry* psetRegistry = edm::pset::Registry::instance();
00044   if(psetRegistry==NULL) return -1;
00045   for(edm::pset::Registry::const_iterator psetIt=psetRegistry->begin();psetIt!=psetRegistry->end();++psetIt){ //loop over every pset for every module ever run
00046     const std::map<std::string,edm::Entry>& mapOfPara  = psetIt->second.tbl(); //contains the parameter name and value for all the parameters of the pset
00047     const std::map<std::string,edm::Entry>::const_iterator itToModLabel = mapOfPara.find("@module_label"); 
00048     if(itToModLabel!=mapOfPara.end()){
00049       if(itToModLabel->second.getString()==filterName){ //moduleName is the filter name, we have found filter, we will now return something
00050         std::map<std::string,edm::Entry>::const_iterator itToCandCut = mapOfPara.find("ncandcut");
00051         if(itToCandCut!=mapOfPara.end() && itToCandCut->second.typeCode()=='I') return itToCandCut->second.getInt32();
00052         else{ //checks if MinN exists and is int32, if not return -1
00053           itToCandCut = mapOfPara.find("MinN");
00054           if(itToCandCut!=mapOfPara.end() && itToCandCut->second.typeCode()=='I') return itToCandCut->second.getInt32();
00055           else return -1;
00056         }
00057       }
00058       
00059     }
00060   }
00061   return -1;
00062 }
00063  
00064 
00065 //this looks into the HLT config and fills a sorted vector with the last filter of all HLT triggers
00066 //it assumes this filter is either the last (in the case of ES filters) or second to last in the sequence
00067 void trigTools::getActiveFilters(const HLTConfigProvider& hltConfig,std::vector<std::string>& activeFilters)
00068 {
00069   activeFilters.clear();
00070 
00071   for(size_t pathNr=0;pathNr<hltConfig.size();pathNr++){
00072     const std::string& pathName = hltConfig.triggerName(pathNr);
00073     if(pathName.find("HLT_")==0){ //hlt path as they all start with HLT_XXXX
00074   
00075       std::string lastFilter;
00076       const std::vector<std::string>& filters = hltConfig.moduleLabels(pathNr);
00077       if(!filters.empty()){
00078         if(filters.back()=="hltBoolEnd" && filters.size()>=2){
00079           activeFilters.push_back(filters[filters.size()-2]); //2nd to last element is the last filter, useally the case as last is hltBool except for ES bits
00080         }else activeFilters.push_back(filters.back());
00081         //std::cout<<filters[filters.size()-2]<<std::endl;
00082       }
00083     }//end hlt path check
00084   }//end path loop over
00085 
00086   std::sort(activeFilters.begin(),activeFilters.end());
00087   
00088 }
00089 //----Morse test--------------
00090 //want to grab filters based on name, hopefully be able to apply offline cuts
00091 //this is first test to find photon30caloidl
00092 //based on names as well
00093 /*void trigTools::getPhoton30(const HLTConfigProvider& hltConfig,std::vector<std::string>& activeFilters)
00094   {
00095   
00096   activeFilters.clear();
00097   
00098   for(size_t pathNr=0;pathNr<hltConfig.size();pathNr++){
00099   const std::string& pathName = hltConfig.triggerName(pathNr);
00100   if(pathName.find("HLT_")==0){ //hlt path as they all start with HLT_XXXX
00101   if((pathName.find("Photon")==0 || pathName.find("Ele")==0 || pathName.find("EG")==0) && pathName.find("Photon30")==0 ){
00102   
00103   std::string lastFilter;
00104   const std::vector<std::string>& filters = hltConfig.moduleLabels(pathNr);
00105   if(!filters.empty()){
00106   if(filters.back()=="hltBoolEnd" && filters.size()>=2){
00107   activeFilters.push_back(filters[filters.size()-2]); //2nd to last element is the last filter, useally the case as last is hltBool except for ES bits
00108   //std::cout<<filters[filters.size()-2]<<std::endl;
00109   }else activeFilters.push_back(filters.back());
00110   }
00111   }
00112   }//end hlt path check
00113   }//end path loop over
00114   std::sort(activeFilters.begin(),activeFilters.end());
00115   }*/
00116 //----------------------------
00117 
00118 //this function will filter the inactive filternames
00119 //it assumes the list of active filters is sorted   
00120 //at some point this will be replaced with one line of fancy stl code but I want it to work now :)
00121 void trigTools::filterInactiveTriggers(std::vector<std::string>& namesToFilter,const std::vector<std::string>& activeFilters)
00122 {
00123   //tempory vector to store the filtered results
00124   std::vector<std::string> filteredNames;
00125   
00126   for(size_t inputFilterNr=0;inputFilterNr<namesToFilter.size();inputFilterNr++){
00127     if(std::binary_search(activeFilters.begin(),activeFilters.end(),namesToFilter[inputFilterNr])){
00128       filteredNames.push_back(namesToFilter[inputFilterNr]);
00129     }//std::cout<<filteredNames[inputFilterNr]<<std::endl;
00130   }
00131   
00132   namesToFilter.swap(filteredNames);
00133 }
00134 
00135 //input filters have format filter1:filter2, this checks both filters are active, rejects ones where both are not active
00136 void trigTools::filterInactiveTightLooseTriggers(std::vector<std::string>& namesToFilter,const std::vector<std::string>& activeFilters)
00137 {
00138   //tempory vector to store the filtered results
00139   std::vector<std::string> filteredNames;
00140   
00141   for(size_t inputFilterNr=0;inputFilterNr<namesToFilter.size();inputFilterNr++){
00142     std::vector<std::string> names;
00143     boost::split(names,namesToFilter[inputFilterNr],boost::is_any_of(std::string(":")));
00144     if(names.size()!=2) continue; //format incorrect, reject it
00145     if(std::binary_search(activeFilters.begin(),activeFilters.end(),names[0]) &&
00146        std::binary_search(activeFilters.begin(),activeFilters.end(),names[1])){ //both filters are valid
00147       filteredNames.push_back(namesToFilter[inputFilterNr]);
00148     }
00149   }
00150   
00151   namesToFilter.swap(filteredNames);
00152 }
00153 
00154 //a comparison functiod for std::pair<std::string,std::string> 
00155 //this probably (infact must) exist elsewhere
00156 class StringPairCompare {
00157 public: 
00158   bool operator()(const std::pair<std::string,std::string>&lhs,
00159                   const std::pair<std::string,std::string>& rhs)const{return keyLess(lhs.first,rhs.first);}
00160   bool operator()(const std::pair<std::string,std::string>&lhs,
00161                   const std::pair<std::string,std::string>::first_type& rhs)const{return keyLess(lhs.first,rhs);}
00162   bool operator()(const std::pair<std::string,std::string>::first_type &lhs,
00163                   const std::pair<std::string,std::string>& rhs)const{return keyLess(lhs,rhs.first);}
00164 private:
00165   bool keyLess(const std::pair<std::string,std::string>::first_type& k1,const std::pair<std::string,std::string>::first_type& k2)const{return k1<k2;}
00166 };
00167 
00168 void trigTools::translateFiltersToPathNames(const HLTConfigProvider& hltConfig,const std::vector<std::string>& filters,std::vector<std::string>& paths)
00169 {
00170   
00171   paths.clear();
00172   std::vector<std::pair<std::string,std::string> > filtersAndPaths;
00173 
00174   for(size_t pathNr=0;pathNr<hltConfig.size();pathNr++){
00175     const std::string& pathName = hltConfig.triggerName(pathNr);
00176     if(pathName.find("HLT_")==0){ //hlt path as they all start with HLT_XXXX
00177   
00178       std::string lastFilter;
00179       const std::vector<std::string>& pathFilters = hltConfig.moduleLabels(pathNr);
00180       if(!pathFilters.empty()){
00181         if(pathFilters.back()=="hltBoolEnd" && pathFilters.size()>=2){
00182           //2nd to last element is the last filter, useally the case as last is hltBool except for ES bits
00183           filtersAndPaths.push_back(std::make_pair(pathFilters[pathFilters.size()-2],pathName));
00184         }else filtersAndPaths.push_back(std::make_pair(pathFilters.back(),pathName));
00185       }
00186     }//end hlt path check
00187   }//end path loop over
00188 
00189   std::sort(filtersAndPaths.begin(),filtersAndPaths.end(),StringPairCompare());
00190   
00191   for(size_t filterNr=0;filterNr<filters.size();filterNr++){
00192     typedef std::vector<std::pair<std::string,std::string> >::const_iterator VecIt;
00193     std::pair<VecIt,VecIt> searchResult = std::equal_range(filtersAndPaths.begin(),filtersAndPaths.end(),filters[filterNr],StringPairCompare());
00194     if(searchResult.first!=searchResult.second) paths.push_back(searchResult.first->second);
00195     else paths.push_back(filters[filterNr]);//if cant find the path, just  write the filter
00196     //---Morse-----
00197     //std::cout<<filtersAndPaths[filterNr].first<<"  "<<filtersAndPaths[filterNr].second<<std::endl;
00198     //-------------
00199   }
00200 
00201 }
00202 
00203 std::string trigTools::getL1SeedFilterOfPath(const HLTConfigProvider& hltConfig,const std::string& path)
00204 {
00205   const std::vector<std::string>& modules = hltConfig.moduleLabels(path);
00206 
00207   for(size_t moduleNr=0;moduleNr<modules.size();moduleNr++){
00208     const std::string& moduleName=modules[moduleNr]; 
00209     if(moduleName.find("hltL1s")==0) return moduleName; //found l1 seed module
00210   }
00211   std::string dummy;
00212   return dummy;
00213 
00214 }
00215   
00216 //hunts for first instance of pattern EtX where X = a number of any length and returns X
00217 float  trigTools::getEtThresFromName(const std::string& trigName)
00218 {
00219   size_t etStrPos = trigName.find("Et");
00220   while(etStrPos!=std::string::npos && trigName.find_first_of("1234567890",etStrPos)!=etStrPos+2){
00221     etStrPos = trigName.find("Et",etStrPos+1);  
00222   }
00223   if(etStrPos!=std::string::npos && trigName.find_first_of("1234567890",etStrPos)==etStrPos+2){
00224     size_t endOfEtValStr = trigName.find_first_not_of("1234567890",etStrPos+2);  
00225 
00226     std::istringstream etValStr(trigName.substr(etStrPos+2,endOfEtValStr-etStrPos-2));
00227     float etVal;
00228     etValStr>> etVal;
00229     return etVal;
00230     
00231   }
00232   return 0;
00233 
00234 }