CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ConfigurationDescriptions.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: ParameterSet
4 // Class : ConfigurationDescriptions
5 //
6 // Implementation:
7 // <Notes on implementation>
8 //
9 // Original Author: W. David Dagenhart
10 // Created: 17 December 2008
11 //
12 
17 
18 #include "boost/bind.hpp"
19 
20 #include <fstream>
21 #include <iostream>
22 #include <iomanip>
23 #include <sstream>
24 
25 namespace {
26  void matchLabel(std::pair<std::string, edm::ParameterSetDescription> const& thePair,
27  std::string const& moduleLabel,
28  edm::ParameterSetDescription const*& psetDesc) {
29  if (thePair.first == moduleLabel) {
30  psetDesc = &thePair.second;
31  }
32  }
33 }
34 
35 namespace edm {
36 
38  baseType_(baseType),
39  defaultDescDefined_(false)
40  { }
41 
43 
44  void
46  { comment_ = value; }
47 
48  void
50  { comment_ = value; }
51 
52  void
54  ParameterSetDescription const& psetDescription) {
55  std::string labelString(label);
56  add(labelString, psetDescription);
57  }
58 
59  void
61  ParameterSetDescription const& psetDescription) {
62 
63  if (baseType_ == std::string("Source")) {
64  if (label != std::string("source")) {
66  "ConfigurationDescriptions::add, when adding a ParameterSetDescription for a source the label must be \"source\"\n");
67  }
68  if (descriptions_.size() != 0U ||
69  defaultDescDefined_ == true) {
71  "ConfigurationDescriptions::add, for a source only 1 ParameterSetDescription may be added\n");
72  }
73  }
74  else if (baseType_ == std::string("Service")) {
75  if (descriptions_.size() != 0U ||
76  defaultDescDefined_ == true) {
78  "ConfigurationDescriptions::add, for a service only 1 ParameterSetDescription may be added\n");
79  }
80  }
81 
82  // To minimize the number of copies involved create an empty description first
83  // and push it into the vector. Then perform the copy.
84  std::pair<std::string, ParameterSetDescription> pairWithEmptyDescription;
85  descriptions_.push_back(pairWithEmptyDescription);
86  std::pair<std::string, ParameterSetDescription> & pair = descriptions_.back();
87 
88  pair.first = label;
89  pair.second = psetDescription;
90 
91  if (baseType_ == std::string("ESSource") || baseType_ == std::string("ESProducer")) {
92  std::string name("appendToDataLabel");
93  if (pair.second.isLabelUnused(name)) {
94  pair.second.add<std::string>(name, std::string(""));
95  }
96  }
97  }
98 
99  void
101 
102  if (baseType_ == std::string("Source") || baseType_ == std::string("Service")) {
103  if (descriptions_.size() != 0U ||
104  defaultDescDefined_ == true) {
106  "ConfigurationDescriptions::addDefault, for a source or service only 1 ParameterSetDescription may be added\n");
107  }
108  }
109 
110  defaultDescDefined_ = true;
111  defaultDesc_ = psetDescription;
112 
113  if (baseType_ == std::string("ESSource") || baseType_ == std::string("ESProducer")) {
114  std::string name("appendToDataLabel");
115  if (defaultDesc_.isLabelUnused(name)) {
116  defaultDesc_.add<std::string>(name, std::string(""));
117  }
118  }
119  }
120 
121  void
123  std::string const& moduleLabel) const {
124 
125  ParameterSetDescription const* psetDesc = 0;
126  for_all(descriptions_, boost::bind(&matchLabel,
127  _1,
128  boost::cref(moduleLabel),
129  boost::ref(psetDesc)));
130 
131  // If there is a matching label
132  if (psetDesc != 0) {
133  psetDesc->validate(pset);
134  }
135  // Is there an explicit description to be used for a non standard label
136  else if (defaultDescDefined_) {
137  defaultDesc_.validate(pset);
138  }
139  // Otherwise use the first one.
140  else if (descriptions_.size() > 0U) {
141  descriptions_[0].second.validate(pset);
142  }
143  // It is possible for no descriptions to be defined and no validation occurs
144  // for this module ever.
145  }
146 
147  void
148  ConfigurationDescriptions::writeCfis(std::string const& baseType,
149  std::string const& pluginName) const {
150 
152  _1,
153  boost::cref(baseType),
154  boost::cref(pluginName)));
155  }
156 
157 
158  void
159  ConfigurationDescriptions::writeCfiForLabel(std::pair<std::string, ParameterSetDescription> const& labelAndDesc,
160  std::string const& baseType,
161  std::string const& pluginName)
162  {
163  if (baseType == std::string("Service") && labelAndDesc.first != pluginName) {
165  "ConfigurationDescriptions::writeCfiForLabel\nFor a service the label and the plugin name must be the same.\n")
166  << "This error probably is caused by an incorrect label being passed\nto the ConfigurationDescriptions::add function earlier.\n"
167  << "plugin name = \"" << pluginName << "\" label name = \"" << labelAndDesc.first << "\"\n";
168  }
169 
170  std::string cfi_filename;
171  if (baseType == std::string("Source")) {
172  cfi_filename = pluginName + "_cfi.py";
173  }
174  else {
175  cfi_filename = labelAndDesc.first + "_cfi.py";
176  }
177  std::ofstream outFile(cfi_filename.c_str());
178 
179 
180  outFile << "import FWCore.ParameterSet.Config as cms\n\n";
181  outFile << labelAndDesc.first << " = cms." << baseType << "('" << pluginName << "'";
182 
183  bool startWithComma = true;
184  int indentation = 2;
185  labelAndDesc.second.writeCfi(outFile, startWithComma, indentation);
186 
187  outFile << ")\n";
188 
189  outFile.close();
190 
191  if (baseType == std::string("Source")) {
192  std::cout << pluginName << "\n";
193  }
194  else {
195  std::cout << labelAndDesc.first << "\n";
196  }
197  }
198 
199  void ConfigurationDescriptions::print(std::ostream & os,
200  std::string const& moduleLabel,
201  bool brief,
202  bool printOnlyLabels,
203  size_t lineWidth,
204  int indentation,
205  int iPlugin) const {
206  if (!brief) {
207  if (!comment().empty()) {
208  DocFormatHelper::wrapAndPrintText(os, comment(), indentation, lineWidth);
209  }
210  os << "\n";
211  }
212 
213  char oldFill = os.fill();
214  if (descriptions_.empty() && !defaultDescDefined_) {
215  indentation += DocFormatHelper::offsetModuleLabel();
216  os << std::setfill(' ') << std::setw(indentation) << "";
217  os << "There are no PSet descriptions defined for this plugin.\n";
218  os << std::setfill(' ') << std::setw(indentation) << "";
219  os << "PSets will not be validated and no cfi files will be generated.\n";
220  if (!brief) os << "\n";
221  os.fill(oldFill);
222  return;
223  }
224 
226  indentation += DocFormatHelper::offsetModuleLabel();
227  os << std::setfill(' ') << std::setw(indentation) << "";
228  os << "This plugin has not implemented the function which defines its\n";
229  os << std::setfill(' ') << std::setw(indentation) << "";
230  os << "configuration descriptions yet. No descriptions are available.\n";
231  os << std::setfill(' ') << std::setw(indentation) << "";
232  os << "Its PSets will not be validated, and no cfi files will be generated.\n";
233  if (!brief) os << "\n";
234  os.fill(oldFill);
235  return;
236  }
237 
238  if (!brief) {
239  std::stringstream ss;
240  if (defaultDescDefined_) {
241  if (descriptions_.empty()) {
242  ss << "This plugin has only one PSet description. "
243  << "This description is always used to validate configurations. "
244  << "Because this configuration has no label, no cfi files will be generated.";
245  }
246  else {
247  ss << "This plugin has " << (descriptions_.size() + 1U) << " PSet descriptions. "
248  << "The description used to validate a configuration is selected by "
249  << "matching the module labels. If none match, then the last description, "
250  << "which has no label, is selected. "
251  << "A cfi file will be generated for each configuration with a module label.";
252  }
253  }
254  else {
255  if (descriptions_.size() == 1U) {
256  ss << "This plugin has " << descriptions_.size() << " PSet description. "
257  << "This description is always used to validate configurations. "
258  << "The label below is used when generating the cfi file.";
259  }
260  else {
261  ss << "This plugin has " << descriptions_.size() << " PSet descriptions. "
262  << "The description used to validate a configuration is selected by "
263  << "matching the module labels. If none match the first description below is used. "
264  << "The module labels below are also used when generating the cfi files.";
265  }
266  }
267  DocFormatHelper::wrapAndPrintText(os, ss.str(), indentation, lineWidth);
268  os << "\n";
269  }
270 
271  indentation += DocFormatHelper::offsetModuleLabel();
272 
274  counter.iPlugin = iPlugin;
275  counter.iSelectedModule = 0;
276  counter.iModule = 0;
277 
279  this,
280  _1,
281  boost::ref(os),
282  boost::cref(moduleLabel),
283  brief,
284  printOnlyLabels,
285  lineWidth,
286  indentation,
287  boost::ref(counter)));
288 
289  if (defaultDescDefined_) {
290  printForLabel(os,
291  std::string("@default"),
292  defaultDesc_,
293  moduleLabel,
294  brief,
295  printOnlyLabels,
296  lineWidth,
297  indentation,
298  counter);
299  }
300  }
301 
302  void
303  ConfigurationDescriptions::printForLabel(std::pair<std::string, ParameterSetDescription> const& labelAndDesc,
304  std::ostream & os,
305  std::string const& moduleLabel,
306  bool brief,
307  bool printOnlyLabels,
308  size_t lineWidth,
309  int indentation,
310  DescriptionCounter & counter) const
311  {
312  printForLabel(os,
313  labelAndDesc.first,
314  labelAndDesc.second,
315  moduleLabel,
316  brief,
317  printOnlyLabels,
318  lineWidth,
319  indentation,
320  counter);
321  }
322 
323  void
325  std::string const& label,
327  std::string const& moduleLabel,
328  bool brief,
329  bool printOnlyLabels,
330  size_t lineWidth,
331  int indentation,
332  DescriptionCounter & counter) const
333  {
334  ++counter.iModule;
335  if (!moduleLabel.empty() && label != moduleLabel) return;
336  ++counter.iSelectedModule;
337 
338  std::stringstream ss;
339  ss << counter.iPlugin << "." << counter.iSelectedModule;
340  std::string section = ss.str();
341 
342  char oldFill = os.fill();
343  os << std::setfill(' ') << std::setw(indentation) << "";
344  os.fill(oldFill);
345  os << section << " ";
346  if (label == std::string("@default")) {
347  os << "description without a module label\n";
348  }
349  else {
350  if (!brief) {
351  if (baseType_ == std::string("Source") || baseType_ == std::string("Service")) {
352  os << "label: ";
353  }
354  else {
355  os << "module label: ";
356  }
357  }
358  os << label << "\n";
359  }
360 
361  if (!brief) {
362  if (!description.comment().empty()) {
363  DocFormatHelper::wrapAndPrintText(os, description.comment(), indentation, lineWidth - indentation);
364  }
365  os << "\n";
366  }
367  if (printOnlyLabels) return;
368 
369  DocFormatHelper dfh;
370  dfh.setBrief(brief);
371  dfh.setLineWidth(lineWidth);
373  dfh.setSection(section);
375 
376  description.print(os, dfh);
377  }
378 }
void writeCfis(std::string const &baseType, std::string const &pluginName) const
const std::string & label
Definition: MVAComputer.cc:186
void printForLabel(std::pair< std::string, ParameterSetDescription > const &labelAndDesc, std::ostream &os, std::string const &moduleLabel, bool brief, bool printOnlyLabels, size_t lineWidth, int indentationn, DescriptionCounter &counter) const
void validate(ParameterSet &pset) const
static void writeCfiForLabel(std::pair< std::string, ParameterSetDescription > const &labelAndDesc, std::string const &baseType, std::string const &pluginName)
static void wrapAndPrintText(std::ostream &os, std::string const &text, size_t indent, size_t suggestedWidth)
ConfigurationDescriptions(std::string const &baseType)
Func for_all(ForwardSequence &s, Func f)
wrapper for std::for_each
Definition: Algorithms.h:16
static int offsetModuleLabel()
void addDefault(ParameterSetDescription const &psetDescription)
void setBrief(bool value)
std::vector< std::pair< std::string, ParameterSetDescription > > descriptions_
void print(std::ostream &os, DocFormatHelper &dfh) const
bool isLabelUnused(std::string const &label) const
ParameterDescriptionBase * add(U const &iLabel, T const &value)
void setComment(std::string const &value)
tuple description
Definition: idDealer.py:66
void add(std::string const &label, ParameterSetDescription const &psetDescription)
void print(std::ostream &os, std::string const &moduleLabel, bool brief, bool printOnlyLabels, size_t lineWidth, int indentation, int iPlugin) const
tuple cout
Definition: gather_cfg.py:41
void validate(ParameterSet &pset, std::string const &moduleLabel) const
std::string const & comment() const
std::string const & comment() const
static int offsetTopLevelPSet()
void setSection(std::string const &value)
void setParent(DescriptionParent value)
void setIndentation(int value)
const std::string * moduleLabel() const
Definition: HLTadd.h:40
void setLineWidth(size_t value)