CMS 3D CMS Logo

PrescaleService.cc
Go to the documentation of this file.
1 //
3 // PrescaleService
4 // ---------------
5 //
7 
14 
15 #include <iostream>
16 #include <set>
17 #include <algorithm>
18 
19 namespace edm {
20  namespace service {
21 
22  // constructor
24  : forceDefault_(iPS.getParameter<bool>("forceDefault")),
25  lvl1Labels_(iPS.getParameter<std::vector<std::string> >("lvl1Labels")),
26  lvl1Default_(findDefaultIndex(iPS.getParameter<std::string>("lvl1DefaultLabel"), lvl1Labels_)),
27  vpsetPrescales_(iPS.getParameterSetVector("prescaleTable")),
28  prescaleTable_() {
31 
32  // Sanity check
33  if (lvl1Default_ >= lvl1Labels_.size()) {
34  throw cms::Exception("InvalidLvl1Index")
35  << "lvl1Default_ '" << lvl1Default_ << "' exceeds number of prescale columns " << lvl1Labels_.size() << "!";
36  }
37 
38  // Check and store Prescale Table
39  for (unsigned int iVPSet = 0; iVPSet < vpsetPrescales_.size(); ++iVPSet) {
40  const ParameterSet& psetPrescales = vpsetPrescales_[iVPSet];
41  const std::string pathName = psetPrescales.getParameter<std::string>("pathName");
42  if (prescaleTable_.find(pathName) != prescaleTable_.end()) {
43  throw cms::Exception("PrescaleServiceConfigError") << " Path '" << pathName << "' found more than once!";
44  }
45  std::vector<unsigned int> prescales = psetPrescales.getParameter<std::vector<unsigned int> >("prescales");
46  if (prescales.size() != lvl1Labels_.size()) {
47  throw cms::Exception("PrescaleServiceConfigError")
48  << " Path '" << pathName << "' has " << prescales.size() << " prescales, instead of expected "
49  << lvl1Labels_.size() << "!";
50  }
51  prescaleTable_[pathName] = prescales;
52  }
53  }
54 
55  // destructor
57 
58  // member functions
59 
62  processParameterSetID_ = processContext.parameterSetID();
63  }
64  }
65 
67  // Acess to Process ParameterSet needed - can't be done in c'tor
69  // Label of HLTPrescaler on each path, keyed on pathName
70  std::map<std::string, std::string> path2module;
71  // Name of path for each HLTPrescaler, keyed on moduleLabel
72  std::map<std::string, std::string> module2path;
73 
74  // Check process config:
75  // * each path contains at most one HLTPrescaler instance
76  // * each HLTPrescaler instance is part of at most one path
77  // * each HLTPrescaler instance is part of at least one ptah
78 
79  // Find all HLTPrescaler instances
80  const std::vector<std::string> allModules = prcPS.getParameter<std::vector<std::string> >("@all_modules");
81  for (unsigned int i = 0; i < allModules.size(); ++i) {
82  ParameterSet const& pset = prcPS.getParameterSet(allModules[i]);
83  const std::string moduleLabel = pset.getParameter<std::string>("@module_label");
84  const std::string moduleType = pset.getParameter<std::string>("@module_type");
85  if (moduleType == "HLTPrescaler")
86  module2path[moduleLabel] = "";
87  }
88  // Check all modules on all paths
89  const std::vector<std::string> allPaths = prcPS.getParameter<std::vector<std::string> >("@paths");
90  for (unsigned int iP = 0; iP < allPaths.size(); ++iP) {
91  const std::string& pathName = allPaths[iP];
92  std::vector<std::string> modules = prcPS.getParameter<std::vector<std::string> >(pathName);
93  for (unsigned int iM = 0; iM < modules.size(); ++iM) {
94  const std::string& moduleLabel = modules[iM];
95  if (module2path.find(moduleLabel) != module2path.end()) {
96  if (path2module.find(pathName) == path2module.end()) {
97  path2module[pathName] = moduleLabel;
98  } else {
99  throw cms::Exception("PrescaleServiceConfigError")
100  << "Path '" << pathName << "' with (>1) HLTPrescalers: " << path2module[pathName] << "+"
101  << moduleLabel << "!";
102  }
103  if (module2path[moduleLabel].empty()) {
104  module2path[moduleLabel] = pathName;
105  } else {
106  throw cms::Exception("PrescaleServiceConfigError")
107  << " HLTPrescaler '" << moduleLabel << "' on (>1) Paths: " << module2path[moduleLabel] << "+"
108  << pathName << "!";
109  }
110  }
111  }
112  }
113  // Check all HLTPrescaler instances are on a path
114  for (std::map<std::string, std::string>::const_iterator it = module2path.begin(); it != module2path.end(); ++it) {
115  if (it->second.empty()) {
116  throw cms::Exception("PrescaleServiceConfigError")
117  << " HLTPrescaler '" << it->first << "' not found on any path!";
118  }
119  }
120 
121  // Check paths stored Prescale Table: each path is actually in the process config
122  for (std::map<std::string, std::vector<unsigned int> >::const_iterator it = prescaleTable_.begin();
123  it != prescaleTable_.end();
124  ++it) {
125  if (path2module.find(it->first) == path2module.end()) {
126  throw cms::Exception("PrescaleServiceConfigError")
127  << " Path '" << it->first << "' is unknown or does not contain any HLTPrescaler!";
128  }
129  }
130  }
131 
132  // const method
133  unsigned int PrescaleService::getPrescale(std::string const& prescaledPath) const {
134  return getPrescale(lvl1Default_, prescaledPath);
135  }
136 
137  // const method
138  unsigned int PrescaleService::getPrescale(unsigned int lvl1Index, std::string const& prescaledPath) const {
139  if (forceDefault_)
140  lvl1Index = lvl1Default_;
141 
142  if (lvl1Index >= lvl1Labels_.size()) {
143  throw cms::Exception("InvalidLvl1Index")
144  << "lvl1Index '" << lvl1Index << "' exceeds number of prescale columns " << lvl1Labels_.size() << "!";
145  }
146  PrescaleTable_t::const_iterator it = prescaleTable_.find(prescaledPath);
147  return (it == prescaleTable_.end()) ? 1 : it->second[lvl1Index];
148  }
149 
150  // static method
151  unsigned int PrescaleService::findDefaultIndex(std::string const& label, std::vector<std::string> const& labels) {
152  for (unsigned int i = 0; i < labels.size(); ++i) {
153  if (labels[i] == label) {
154  return i;
155  }
156  }
157  return labels.size();
158  }
159 
160  // static method
163 
164  std::vector<std::string> defaultVector;
165  defaultVector.push_back(std::string("default"));
166  desc.add<std::vector<std::string> >("lvl1Labels", defaultVector);
167 
168  // This default vector<ParameterSet> will be used when
169  // the configuration does not include this parameter and
170  // it also gets written into the generated cfi file.
171  std::vector<edm::ParameterSet> defaultVPSet;
172  edm::ParameterSet pset0;
173  pset0.addParameter<std::string>("pathName", std::string("HLTPath"));
174  std::vector<unsigned> defaultVectorU;
175  defaultVectorU.push_back(1u);
176  pset0.addParameter<std::vector<unsigned> >("prescales", defaultVectorU);
177  defaultVPSet.push_back(pset0);
178 
180  validator.add<std::string>("pathName");
181  validator.add<std::vector<unsigned int> >("prescales");
182 
183  desc.addVPSet("prescaleTable", validator, defaultVPSet);
184 
185  desc.add<std::string>("lvl1DefaultLabel", std::string("default"));
186  desc.add<bool>("forceDefault", false);
187 
188  descriptions.add("PrescaleService", desc);
189  }
190 
191  } // namespace service
192 } // namespace edm
T getParameter(std::string const &) const
void preBeginJob(PathsAndConsumesOfModulesBase const &, ProcessContext const &)
const unsigned int lvl1Default_
ParameterDescriptionBase * addVPSet(U const &iLabel, ParameterSetDescription const &validator, std::vector< ParameterSet > const &defaults)
ParameterSet const & getParameterSet(ParameterSetID const &id)
ParameterSetID const & parameterSetID() const
char const * label
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
void addParameter(std::string const &name, T const &value)
Definition: ParameterSet.h:125
ParameterDescriptionBase * add(U const &iLabel, T const &value)
PrescaleService(ParameterSet const &, ActivityRegistry &)
ParameterSet const & getParameterSet(std::string const &) const
unsigned int getPrescale(std::string const &prescaledPath) const
const std::vector< ParameterSet > vpsetPrescales_
void add(std::string const &label, ParameterSetDescription const &psetDescription)
void watchPreBeginJob(PreBeginJob::slot_type const &iSlot)
convenience function for attaching to signal
HLT enums.
ParameterSetID processParameterSetID_
bool isValid() const
Definition: Hash.h:154
static unsigned int findDefaultIndex(std::string const &label, std::vector< std::string > const &labels)
void watchPostBeginJob(PostBeginJob::slot_type const &iSlot)
convenience function for attaching to signal