File indexing completed on 2025-04-22 06:27:21
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
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
0020
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 }
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
0049
0050
0051
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
0065
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
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
0086
0087 std::map<std::string, std::vector<std::string> > paths_for_process;
0088 for (auto const& path_spec : path_specs) {
0089
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
0097
0098 for (auto const& path : paths_for_process) {
0099
0100
0101 if (path.first == process_name) {
0102 selectors_.emplace_back(path.first, EventSelector(path.second, triggernames), std::move(iC));
0103 } else {
0104
0105
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 if (!iLabel.empty()) {
0138 std::map<std::string, std::vector<std::pair<std::string, int> > >::const_iterator iter =
0139 outputModulePathPositions.find(iLabel);
0140 assert(iter != outputModulePathPositions.end());
0141 for (auto const& item : iter->second) {
0142 endPaths.push_back(item.first);
0143 endPathPositions.push_back(item.second);
0144 }
0145 }
0146 selectEventsInfo.addParameter<std::vector<std::string> >("EndPaths", endPaths);
0147 selectEventsInfo.addParameter<std::vector<int> >("EndPathPositions", endPathPositions);
0148 if (!selectEventsInfo.exists("SelectEvents")) {
0149 selectEventsInfo.addParameter<std::vector<std::string> >("SelectEvents", std::vector<std::string>());
0150 }
0151 selectEventsInfo.registerIt();
0152
0153 return selectEventsInfo.id();
0154 }
0155
0156 }
0157 }