Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:12:16

0001 #include <algorithm>
0002 
0003 #include "FWCore/Framework/interface/TriggerResultsBasedEventSelector.h"
0004 #include "FWCore/Framework/interface/EventForOutput.h"
0005 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0006 #include "FWCore/Utilities/interface/Algorithms.h"
0007 
0008 static const edm::TypeID s_TrigResultsType(typeid(edm::TriggerResults));
0009 
0010 namespace trigger_results_based_event_selector_utils {
0011   //--------------------------------------------------------
0012   // Remove whitespace (spaces and tabs) from a std::string.
0013   void remove_whitespace(std::string& s) {
0014     s.erase(std::remove(s.begin(), s.end(), ' '), s.end());
0015     s.erase(std::remove(s.begin(), s.end(), '\t'), s.end());
0016   }
0017 
0018   //--------------------------------------------------------
0019   // Given a path-spec (std::string of the form "a:b", where the ":b" is
0020   // optional), return a parsed_path_spec_t containing "a" and "b".
0021 
0022   typedef std::pair<std::string, std::string> parsed_path_spec_t;
0023   void parse_path_spec(std::string const& path_spec, parsed_path_spec_t& output) {
0024     std::string trimmed_path_spec(path_spec);
0025     remove_whitespace(trimmed_path_spec);
0026 
0027     std::string::size_type colon = trimmed_path_spec.find(':');
0028     if (colon == std::string::npos) {
0029       output.first = trimmed_path_spec;
0030     } else {
0031       output.first = trimmed_path_spec.substr(0, colon);
0032       output.second = trimmed_path_spec.substr(colon + 1, trimmed_path_spec.size());
0033     }
0034   }
0035 }  // namespace trigger_results_based_event_selector_utils
0036 
0037 using namespace trigger_results_based_event_selector_utils;
0038 
0039 namespace edm {
0040 
0041   namespace detail {
0042 
0043     bool configureEventSelector(edm::ParameterSet const& iPSet,
0044                                 std::string const& iProcessName,
0045                                 std::vector<std::string> const& iAllTriggerNames,
0046                                 edm::detail::TriggerResultsBasedEventSelector& oSelector,
0047                                 edm::ConsumesCollector&& iC) {
0048       // If selectevents is an emtpy ParameterSet, then we are to write
0049       // all events, or one which contains a vstrig 'SelectEvents' that
0050       // is empty, we are to write all events. We have no need for any
0051       // EventSelectors.
0052       if (iPSet.empty()) {
0053         oSelector.setupDefault();
0054         return true;
0055       }
0056 
0057       std::vector<std::string> path_specs = iPSet.getParameter<std::vector<std::string> >("SelectEvents");
0058 
0059       if (path_specs.empty()) {
0060         oSelector.setupDefault();
0061         return true;
0062       }
0063 
0064       // If we get here, we have the possibility of having to deal with
0065       // path_specs that look at more than one process.
0066       std::vector<parsed_path_spec_t> parsed_paths(path_specs.size());
0067       for (size_t i = 0; i < path_specs.size(); ++i) {
0068         parse_path_spec(path_specs[i], parsed_paths[i]);
0069       }
0070       oSelector.setup(parsed_paths, iAllTriggerNames, iProcessName, std::move(iC));
0071 
0072       return false;
0073     }
0074 
0075     // typedef detail::NamedEventSelector NES;
0076 
0077     TriggerResultsBasedEventSelector::TriggerResultsBasedEventSelector() : selectors_(), wantAllEvents_(false) {}
0078 
0079     void TriggerResultsBasedEventSelector::setupDefault() { wantAllEvents_ = true; }
0080 
0081     void TriggerResultsBasedEventSelector::setup(std::vector<parsed_path_spec_t> const& path_specs,
0082                                                  std::vector<std::string> const& triggernames,
0083                                                  std::string const& process_name,
0084                                                  ConsumesCollector&& iC) {
0085       // paths_for_process maps each PROCESS names to a sequence of
0086       // PATH names
0087       std::map<std::string, std::vector<std::string> > paths_for_process;
0088       for (auto const& path_spec : path_specs) {
0089         // Default to current process if none specified
0090         if (path_spec.second.empty()) {
0091           paths_for_process[process_name].push_back(path_spec.first);
0092         } else {
0093           paths_for_process[path_spec.second].push_back(path_spec.first);
0094         }
0095       }
0096       // Now go through all the PROCESS names, and create a
0097       // NamedEventSelector for each.
0098       for (auto const& path : paths_for_process) {
0099         // For the current process we know the trigger names
0100         // from the configuration file
0101         if (path.first == process_name) {
0102           selectors_.emplace_back(path.first, EventSelector(path.second, triggernames), std::move(iC));
0103         } else {
0104           // For previous processes we do not know the trigger
0105           // names yet.
0106           selectors_.emplace_back(path.first, EventSelector(path.second), std::move(iC));
0107         }
0108       }
0109     }
0110 
0111     bool TriggerResultsBasedEventSelector::wantEvent(EventForOutput const& ev) {
0112       if (wantAllEvents_) {
0113         return true;
0114       }
0115       for (auto& selector : selectors_) {
0116         Handle<TriggerResults> handle;
0117         ev.getByToken<TriggerResults>(selector.token(), handle);
0118         bool match = selector.match(*handle);
0119         if (match) {
0120           return true;
0121         }
0122       }
0123       return false;
0124     }
0125 
0126     ParameterSetID registerProperSelectionInfo(
0127         edm::ParameterSet const& iInitial,
0128         std::string const& iLabel,
0129         std::map<std::string, std::vector<std::pair<std::string, int> > > const& outputModulePathPositions,
0130         bool anyProductProduced) {
0131       ParameterSet selectEventsInfo;
0132       selectEventsInfo.copyForModify(iInitial);
0133       selectEventsInfo.addParameter<bool>("InProcessHistory", anyProductProduced);
0134       std::vector<std::string> endPaths;
0135       std::vector<int> endPathPositions;
0136 
0137       // The label will be empty if and only if this is a SubProcess
0138       // SubProcess's do not appear on any end path
0139       if (!iLabel.empty()) {
0140         std::map<std::string, std::vector<std::pair<std::string, int> > >::const_iterator iter =
0141             outputModulePathPositions.find(iLabel);
0142         assert(iter != outputModulePathPositions.end());
0143         for (auto const& item : iter->second) {
0144           endPaths.push_back(item.first);
0145           endPathPositions.push_back(item.second);
0146         }
0147       }
0148       selectEventsInfo.addParameter<std::vector<std::string> >("EndPaths", endPaths);
0149       selectEventsInfo.addParameter<std::vector<int> >("EndPathPositions", endPathPositions);
0150       if (!selectEventsInfo.exists("SelectEvents")) {
0151         selectEventsInfo.addParameter<std::vector<std::string> >("SelectEvents", std::vector<std::string>());
0152       }
0153       selectEventsInfo.registerIt();
0154 
0155       return selectEventsInfo.id();
0156     }
0157 
0158   }  // namespace detail
0159 }  // namespace edm