CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PrescaleService.cc
Go to the documentation of this file.
1 //
3 // PrescaleService
4 // ---------------
5 //
7 
8 
15 
16 #include <iostream>
17 #include <set>
18 #include <algorithm>
19 
20 
21 namespace edm {
22  namespace service {
23 
24  // constructor
26  : forceDefault_(iPS.getParameter<bool>("forceDefault"))
27  , lvl1Labels_(iPS.getParameter<std::vector<std::string> >("lvl1Labels"))
28  , lvl1Default_(findDefaultIndex(iPS.getParameter<std::string>("lvl1DefaultLabel"), lvl1Labels_))
29  , vpsetPrescales_(iPS.getParameterSetVector("prescaleTable"))
30  , prescaleTable_()
31  {
33 
34  // Sanity check
35  if (lvl1Default_ >= lvl1Labels_.size()) {
36  throw cms::Exception("InvalidLvl1Index")
37  <<"lvl1Default_ '" << lvl1Default_ << "' exceeds number of prescale columns " << lvl1Labels_.size() << "!";
38  }
39 
40  // Check and store Prescale Table
41  for (unsigned int iVPSet=0; iVPSet < vpsetPrescales_.size(); ++iVPSet) {
42  const ParameterSet& psetPrescales = vpsetPrescales_[iVPSet];
43  const std::string pathName = psetPrescales.getParameter<std::string>("pathName");
44  if (prescaleTable_.find(pathName)!=prescaleTable_.end()) {
45  throw cms::Exception("PrescaleServiceConfigError")
46  << " Path '" << pathName << "' found more than once!";
47  }
48  std::vector<unsigned int> prescales = psetPrescales.getParameter<std::vector<unsigned int> >("prescales");
49  if (prescales.size()!=lvl1Labels_.size()) {
50  throw cms::Exception("PrescaleServiceConfigError")
51  << " Path '" << pathName << "' has " << prescales.size() << " prescales, instead of expected " << lvl1Labels_.size() << "!";
52  }
54  }
55 
56  }
57 
58  // destructor
60  }
61 
62  // member functions
63 
65 
66  // Acesss to Process ParameterSet needed - can't be done in c'tor
67  const ParameterSet prcPS = getProcessParameterSet();
68 
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") module2path[moduleLabel]="";
86  }
87  // Check all modules on all paths
88  const std::vector<std::string> allPaths = prcPS.getParameter<std::vector<std::string> >("@paths");
89  for (unsigned int iP = 0; iP < allPaths.size(); ++iP) {
90  const std::string& pathName = allPaths[iP];
91  std::vector<std::string> modules = prcPS.getParameter<std::vector<std::string> >(pathName);
92  for (unsigned int iM = 0; iM < modules.size(); ++iM) {
93  const std::string& moduleLabel = modules[iM];
94  if (module2path.find(moduleLabel)!=module2path.end()) {
95  if (path2module.find(pathName)==path2module.end()) {
96  path2module[pathName]=moduleLabel;
97  } else {
98  throw cms::Exception("PrescaleServiceConfigError")
99  << "Path '" << pathName << "' with (>1) HLTPrescalers: " << path2module[pathName] << "+" << moduleLabel << "!";
100  }
101  if (module2path[moduleLabel]=="") {
102  module2path[moduleLabel]=pathName;
103  } else {
104  throw cms::Exception("PrescaleServiceConfigError")
105  << " HLTPrescaler '" << moduleLabel << "' on (>1) Paths: " << module2path[moduleLabel] << "+" << pathName << "!";
106  }
107  }
108  }
109  }
110  // Check all HLTPrescaler instances are on a path
111  for (std::map<std::string,std::string>::const_iterator it = module2path.begin(); it!=module2path.end(); ++it) {
112  if (it->second=="") {
113  throw cms::Exception("PrescaleServiceConfigError")
114  << " HLTPrescaler '" << it->first << "' not found on any path!";
115  }
116  }
117 
118  // Check paths stored Prescale Table: each path is actually in the process config
119  for (std::map<std::string, std::vector<unsigned int> >::const_iterator it = prescaleTable_.begin(); it!=prescaleTable_.end(); ++it) {
120  if (path2module.find(it->first)==path2module.end()) {
121  throw cms::Exception("PrescaleServiceConfigError")
122  << " Path '"<< it->first << "' is unknown or does not contain any HLTPrescaler!";
123  }
124  }
125 
126  }
127 
128  // const method
129  unsigned int PrescaleService::getPrescale(std::string const& prescaledPath) const
130  {
131  return getPrescale(lvl1Default_, prescaledPath);
132  }
133 
134  // const method
135  unsigned int PrescaleService::getPrescale(unsigned int lvl1Index, std::string const& prescaledPath) const
136  {
137  if (forceDefault_) lvl1Index = lvl1Default_;
138 
139  if (lvl1Index >= lvl1Labels_.size()) {
140  throw cms::Exception("InvalidLvl1Index")
141  << "lvl1Index '" << lvl1Index << "' exceeds number of prescale columns " << lvl1Labels_.size() << "!";
142  }
143  PrescaleTable_t::const_iterator it = prescaleTable_.find(prescaledPath);
144  return (it == prescaleTable_.end()) ? 1 : it->second[lvl1Index];
145  }
146 
147  // static method
148  unsigned int PrescaleService::findDefaultIndex(std::string const & label, std::vector<std::string> const & labels) {
149  for (unsigned int i = 0; i < labels.size(); ++i) {
150  if (labels[i] == label) {
151  return i;
152  }
153  }
154  return labels.size();
155  }
156 
157  // static method
160 
161  std::vector<std::string> defaultVector;
162  defaultVector.push_back(std::string("default"));
163  desc.add<std::vector<std::string> >("lvl1Labels", defaultVector);
164 
165  // This default vector<ParameterSet> will be used when
166  // the configuration does not include this parameter and
167  // it also gets written into the generated cfi file.
168  std::vector<edm::ParameterSet> defaultVPSet;
169  edm::ParameterSet pset0;
170  pset0.addParameter<std::string>("pathName", std::string("HLTPath"));
171  std::vector<unsigned> defaultVectorU;
172  defaultVectorU.push_back(1u);
173  pset0.addParameter<std::vector<unsigned> >("prescales", defaultVectorU);
174  defaultVPSet.push_back(pset0);
175 
177  validator.add<std::string>("pathName");
178  validator.add<std::vector<unsigned int> >("prescales");
179 
180  desc.addVPSet("prescaleTable", validator, defaultVPSet);
181 
182  desc.add<std::string>("lvl1DefaultLabel", std::string("default"));
183  desc.add<bool> ("forceDefault", false);
184 
185  descriptions.add("PrescaleService", desc);
186  }
187 
188  } // namespace service
189 } // namespace edm
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
const unsigned int lvl1Default_
ParameterDescriptionBase * addVPSet(U const &iLabel, ParameterSetDescription const &validator, std::vector< ParameterSet > const &defaults)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
void addParameter(std::string const &name, T const &value)
Definition: ParameterSet.h:142
ParameterDescriptionBase * add(U const &iLabel, T const &value)
PrescaleService(ParameterSet const &, ActivityRegistry &)
ParameterSet const & getProcessParameterSet()
Definition: Registry.cc:82
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)
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