CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/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       }
00082     }//end hlt path check
00083   }//end path loop over
00084 
00085   std::sort(activeFilters.begin(),activeFilters.end());
00086   
00087 }
00088  
00089 //this function will filter the inactive filternames
00090 //it assumes the list of active filters is sorted   
00091 //at some point this will be replaced with one line of fancy stl code but I want it to work now :)
00092 void trigTools::filterInactiveTriggers(std::vector<std::string>& namesToFilter,const std::vector<std::string>& activeFilters)
00093 {
00094   //tempory vector to store the filtered results
00095   std::vector<std::string> filteredNames;
00096   
00097   for(size_t inputFilterNr=0;inputFilterNr<namesToFilter.size();inputFilterNr++){
00098     if(std::binary_search(activeFilters.begin(),activeFilters.end(),namesToFilter[inputFilterNr])){
00099       filteredNames.push_back(namesToFilter[inputFilterNr]);
00100     }
00101   }
00102   
00103   namesToFilter.swap(filteredNames);
00104 }
00105 
00106 //input filters have format filter1:filter2, this checks both filters are active, rejects ones where both are not active
00107 void trigTools::filterInactiveTightLooseTriggers(std::vector<std::string>& namesToFilter,const std::vector<std::string>& activeFilters)
00108 {
00109   //tempory vector to store the filtered results
00110   std::vector<std::string> filteredNames;
00111   
00112   for(size_t inputFilterNr=0;inputFilterNr<namesToFilter.size();inputFilterNr++){
00113     std::vector<std::string> names;
00114     boost::split(names,namesToFilter[inputFilterNr],boost::is_any_of(std::string(":")));
00115     if(names.size()!=2) continue; //format incorrect, reject it
00116     if(std::binary_search(activeFilters.begin(),activeFilters.end(),names[0]) &&
00117        std::binary_search(activeFilters.begin(),activeFilters.end(),names[1])){ //both filters are valid
00118       filteredNames.push_back(namesToFilter[inputFilterNr]);
00119     }
00120   }
00121   
00122   namesToFilter.swap(filteredNames);
00123 }
00124 
00125 //a comparison functiod for std::pair<std::string,std::string> 
00126 //this probably (infact must) exist elsewhere
00127 class StringPairCompare {
00128 public: 
00129   bool operator()(const std::pair<std::string,std::string>&lhs,
00130                   const std::pair<std::string,std::string>& rhs)const{return keyLess(lhs.first,rhs.first);}
00131   bool operator()(const std::pair<std::string,std::string>&lhs,
00132                   const std::pair<std::string,std::string>::first_type& rhs)const{return keyLess(lhs.first,rhs);}
00133   bool operator()(const std::pair<std::string,std::string>::first_type &lhs,
00134                   const std::pair<std::string,std::string>& rhs)const{return keyLess(lhs,rhs.first);}
00135 private:
00136   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;}
00137 };
00138 
00139 void trigTools::translateFiltersToPathNames(const HLTConfigProvider& hltConfig,const std::vector<std::string>& filters,std::vector<std::string>& paths)
00140 {
00141   
00142   paths.clear();
00143   std::vector<std::pair<std::string,std::string> > filtersAndPaths;
00144 
00145   for(size_t pathNr=0;pathNr<hltConfig.size();pathNr++){
00146     const std::string& pathName = hltConfig.triggerName(pathNr);
00147     if(pathName.find("HLT_")==0){ //hlt path as they all start with HLT_XXXX
00148   
00149       std::string lastFilter;
00150       const std::vector<std::string>& pathFilters = hltConfig.moduleLabels(pathNr);
00151       if(!pathFilters.empty()){
00152         if(pathFilters.back()=="hltBoolEnd" && pathFilters.size()>=2){
00153           //2nd to last element is the last filter, useally the case as last is hltBool except for ES bits
00154           filtersAndPaths.push_back(std::make_pair(pathFilters[pathFilters.size()-2],pathName));
00155         }else filtersAndPaths.push_back(std::make_pair(pathFilters.back(),pathName));
00156       }
00157     }//end hlt path check
00158   }//end path loop over
00159 
00160   std::sort(filtersAndPaths.begin(),filtersAndPaths.end(),StringPairCompare());
00161   
00162   for(size_t filterNr=0;filterNr<filters.size();filterNr++){
00163     typedef std::vector<std::pair<std::string,std::string> >::const_iterator VecIt;
00164     std::pair<VecIt,VecIt> searchResult = std::equal_range(filtersAndPaths.begin(),filtersAndPaths.end(),filters[filterNr],StringPairCompare());
00165     if(searchResult.first!=searchResult.second) paths.push_back(searchResult.first->second);
00166     else paths.push_back(filters[filterNr]);//if cant find the path, just  write the filter
00167  
00168   }
00169 
00170 }
00171 
00172 std::string trigTools::getL1SeedFilterOfPath(const HLTConfigProvider& hltConfig,const std::string& path)
00173 {
00174   const std::vector<std::string>& modules = hltConfig.moduleLabels(path);
00175 
00176   for(size_t moduleNr=0;moduleNr<modules.size();moduleNr++){
00177     const std::string& moduleName=modules[moduleNr]; 
00178     if(moduleName.find("hltL1s")==0) return moduleName; //found l1 seed module
00179   }
00180   std::string dummy;
00181   return dummy;
00182 
00183 }
00184   
00185 //hunts for first instance of pattern EtX where X = a number of any length and returns X
00186 float  trigTools::getEtThresFromName(const std::string& trigName)
00187 {
00188   size_t etStrPos = trigName.find("Et");
00189   while(etStrPos!=std::string::npos && trigName.find_first_of("1234567890",etStrPos)!=etStrPos+2){
00190     etStrPos = trigName.find("Et",etStrPos+1);  
00191   }
00192   if(etStrPos!=std::string::npos && trigName.find_first_of("1234567890",etStrPos)==etStrPos+2){
00193     size_t endOfEtValStr = trigName.find_first_not_of("1234567890",etStrPos+2);  
00194 
00195     std::istringstream etValStr(trigName.substr(etStrPos+2,endOfEtValStr-etStrPos-2));
00196     float etVal;
00197     etValStr>> etVal;
00198     return etVal;
00199     
00200   }
00201   return 0;
00202 
00203 }