Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:02:24

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 {
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   void test_remove_whitespace() {
0019     std::string a("noblanks");
0020     std::string b("\t   no   blanks    \t");
0021 
0022     remove_whitespace(b);
0023     assert(a == b);
0024   }
0025 
0026   //--------------------------------------------------------
0027   // Given a path-spec (std::string of the form "a:b", where the ":b" is
0028   // optional), return a parsed_path_spec_t containing "a" and "b".
0029 
0030   typedef std::pair<std::string, std::string> parsed_path_spec_t;
0031   void parse_path_spec(std::string const& path_spec, parsed_path_spec_t& output) {
0032     std::string trimmed_path_spec(path_spec);
0033     remove_whitespace(trimmed_path_spec);
0034 
0035     std::string::size_type colon = trimmed_path_spec.find(':');
0036     if (colon == std::string::npos) {
0037       output.first = trimmed_path_spec;
0038     } else {
0039       output.first = trimmed_path_spec.substr(0, colon);
0040       output.second = trimmed_path_spec.substr(colon + 1, trimmed_path_spec.size());
0041     }
0042   }
0043 
0044   void test_parse_path_spec() {
0045     std::vector<std::string> paths;
0046     paths.push_back("a:p1");
0047     paths.push_back("b:p2");
0048     paths.push_back("  c");
0049     paths.push_back("ddd\t:p3");
0050     paths.push_back("eee:  p4  ");
0051 
0052     std::vector<parsed_path_spec_t> parsed(paths.size());
0053     for (size_t i = 0; i < paths.size(); ++i) {
0054       parse_path_spec(paths[i], parsed[i]);
0055     }
0056 
0057     assert(parsed[0].first == "a");
0058     assert(parsed[0].second == "p1");
0059     assert(parsed[1].first == "b");
0060     assert(parsed[1].second == "p2");
0061     assert(parsed[2].first == "c");
0062     assert(parsed[2].second.empty());
0063     assert(parsed[3].first == "ddd");
0064     assert(parsed[3].second == "p3");
0065     assert(parsed[4].first == "eee");
0066     assert(parsed[4].second == "p4");
0067   }
0068 }  // namespace
0069 
0070 namespace edm {
0071   namespace test {
0072     void run_all_output_module_tests() {
0073       test_remove_whitespace();
0074       test_parse_path_spec();
0075     }
0076   }  // namespace test
0077 
0078   namespace detail {
0079 
0080     bool configureEventSelector(edm::ParameterSet const& iPSet,
0081                                 std::string const& iProcessName,
0082                                 std::vector<std::string> const& iAllTriggerNames,
0083                                 edm::detail::TriggerResultsBasedEventSelector& oSelector,
0084                                 edm::ConsumesCollector&& iC) {
0085       // If selectevents is an emtpy ParameterSet, then we are to write
0086       // all events, or one which contains a vstrig 'SelectEvents' that
0087       // is empty, we are to write all events. We have no need for any
0088       // EventSelectors.
0089       if (iPSet.empty()) {
0090         oSelector.setupDefault();
0091         return true;
0092       }
0093 
0094       std::vector<std::string> path_specs = iPSet.getParameter<std::vector<std::string> >("SelectEvents");
0095 
0096       if (path_specs.empty()) {
0097         oSelector.setupDefault();
0098         return true;
0099       }
0100 
0101       // If we get here, we have the possibility of having to deal with
0102       // path_specs that look at more than one process.
0103       std::vector<parsed_path_spec_t> parsed_paths(path_specs.size());
0104       for (size_t i = 0; i < path_specs.size(); ++i) {
0105         parse_path_spec(path_specs[i], parsed_paths[i]);
0106       }
0107       oSelector.setup(parsed_paths, iAllTriggerNames, iProcessName, std::move(iC));
0108 
0109       return false;
0110     }
0111 
0112     // typedef detail::NamedEventSelector NES;
0113 
0114     TriggerResultsBasedEventSelector::TriggerResultsBasedEventSelector() : selectors_(), wantAllEvents_(false) {}
0115 
0116     void TriggerResultsBasedEventSelector::setupDefault() { wantAllEvents_ = true; }
0117 
0118     void TriggerResultsBasedEventSelector::setup(std::vector<parsed_path_spec_t> const& path_specs,
0119                                                  std::vector<std::string> const& triggernames,
0120                                                  std::string const& process_name,
0121                                                  ConsumesCollector&& iC) {
0122       // paths_for_process maps each PROCESS names to a sequence of
0123       // PATH names
0124       std::map<std::string, std::vector<std::string> > paths_for_process;
0125       for (auto const& path_spec : path_specs) {
0126         // Default to current process if none specified
0127         if (path_spec.second.empty()) {
0128           paths_for_process[process_name].push_back(path_spec.first);
0129         } else {
0130           paths_for_process[path_spec.second].push_back(path_spec.first);
0131         }
0132       }
0133       // Now go through all the PROCESS names, and create a
0134       // NamedEventSelector for each.
0135       for (auto const& path : paths_for_process) {
0136         // For the current process we know the trigger names
0137         // from the configuration file
0138         if (path.first == process_name) {
0139           selectors_.emplace_back(path.first, EventSelector(path.second, triggernames), std::move(iC));
0140         } else {
0141           // For previous processes we do not know the trigger
0142           // names yet.
0143           selectors_.emplace_back(path.first, EventSelector(path.second), std::move(iC));
0144         }
0145       }
0146     }
0147 
0148     bool TriggerResultsBasedEventSelector::wantEvent(EventForOutput const& ev) {
0149       if (wantAllEvents_) {
0150         return true;
0151       }
0152       for (auto& selector : selectors_) {
0153         Handle<TriggerResults> handle;
0154         ev.getByToken<TriggerResults>(selector.token(), handle);
0155         bool match = selector.match(*handle);
0156         if (match) {
0157           return true;
0158         }
0159       }
0160       return false;
0161     }
0162 
0163     ParameterSetID registerProperSelectionInfo(
0164         edm::ParameterSet const& iInitial,
0165         std::string const& iLabel,
0166         std::map<std::string, std::vector<std::pair<std::string, int> > > const& outputModulePathPositions,
0167         bool anyProductProduced) {
0168       ParameterSet selectEventsInfo;
0169       selectEventsInfo.copyForModify(iInitial);
0170       selectEventsInfo.addParameter<bool>("InProcessHistory", anyProductProduced);
0171       std::vector<std::string> endPaths;
0172       std::vector<int> endPathPositions;
0173 
0174       // The label will be empty if and only if this is a SubProcess
0175       // SubProcess's do not appear on any end path
0176       if (!iLabel.empty()) {
0177         std::map<std::string, std::vector<std::pair<std::string, int> > >::const_iterator iter =
0178             outputModulePathPositions.find(iLabel);
0179         assert(iter != outputModulePathPositions.end());
0180         for (auto const& item : iter->second) {
0181           endPaths.push_back(item.first);
0182           endPathPositions.push_back(item.second);
0183         }
0184       }
0185       selectEventsInfo.addParameter<std::vector<std::string> >("EndPaths", endPaths);
0186       selectEventsInfo.addParameter<std::vector<int> >("EndPathPositions", endPathPositions);
0187       if (!selectEventsInfo.exists("SelectEvents")) {
0188         selectEventsInfo.addParameter<std::vector<std::string> >("SelectEvents", std::vector<std::string>());
0189       }
0190       selectEventsInfo.registerIt();
0191 
0192       return selectEventsInfo.id();
0193     }
0194 
0195   }  // namespace detail
0196 }  // namespace edm