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 
92  void
94 
95  if (baseType_ == std::string("Source") || baseType_ == std::string("Service")) {
96  if (descriptions_.size() != 0U ||
97  defaultDescDefined_ == true) {
99  "ConfigurationDescriptions::addDefault, for a source or service only 1 ParameterSetDescription may be added\n");
100  }
101  }
102 
103  defaultDescDefined_ = true;
104  defaultDesc_ = psetDescription;
105  }
106 
107  void
109  std::string const& moduleLabel) const {
110 
111  ParameterSetDescription const* psetDesc = 0;
112  for_all(descriptions_, boost::bind(&matchLabel,
113  _1,
114  boost::cref(moduleLabel),
115  boost::ref(psetDesc)));
116 
117  // If there is a matching label
118  if (psetDesc != 0) {
119  psetDesc->validate(pset);
120  }
121  // Is there an explicit description to be used for a non standard label
122  else if (defaultDescDefined_) {
123  defaultDesc_.validate(pset);
124  }
125  // Otherwise use the first one.
126  else if (descriptions_.size() > 0U) {
127  descriptions_[0].second.validate(pset);
128  }
129  // It is possible for no descriptions to be defined and no validation occurs
130  // for this module ever.
131  }
132 
133  void
134  ConfigurationDescriptions::writeCfis(std::string const& baseType,
135  std::string const& pluginName) const {
136 
138  _1,
139  boost::cref(baseType),
140  boost::cref(pluginName)));
141  }
142 
143 
144  void
145  ConfigurationDescriptions::writeCfiForLabel(std::pair<std::string, ParameterSetDescription> const& labelAndDesc,
146  std::string const& baseType,
147  std::string const& pluginName)
148  {
149  if (baseType == std::string("Service") && labelAndDesc.first != pluginName) {
151  "ConfigurationDescriptions::writeCfiForLabel\nFor a service the label and the plugin name must be the same.\n")
152  << "This error probably is caused by an incorrect label being passed\nto the ConfigurationDescriptions::add function earlier.\n"
153  << "plugin name = \"" << pluginName << "\" label name = \"" << labelAndDesc.first << "\"\n";
154  }
155 
156  std::string cfi_filename;
157  if (baseType == std::string("Source")) {
158  cfi_filename = pluginName + "_cfi.py";
159  }
160  else {
161  cfi_filename = labelAndDesc.first + "_cfi.py";
162  }
163  std::ofstream outFile(cfi_filename.c_str());
164 
165 
166  outFile << "import FWCore.ParameterSet.Config as cms\n\n";
167  outFile << labelAndDesc.first << " = cms." << baseType << "('" << pluginName << "'";
168 
169  bool startWithComma = true;
170  int indentation = 2;
171  labelAndDesc.second.writeCfi(outFile, startWithComma, indentation);
172 
173  outFile << ")\n";
174 
175  outFile.close();
176 
177  if (baseType == std::string("Source")) {
178  std::cout << pluginName << "\n";
179  }
180  else {
181  std::cout << labelAndDesc.first << "\n";
182  }
183  }
184 
185  void ConfigurationDescriptions::print(std::ostream & os,
186  std::string const& moduleLabel,
187  bool brief,
188  bool printOnlyLabels,
189  size_t lineWidth,
190  int indentation,
191  int iPlugin) const {
192  if (!brief) {
193  if (!comment().empty()) {
194  DocFormatHelper::wrapAndPrintText(os, comment(), indentation, lineWidth);
195  }
196  os << "\n";
197  }
198 
199  char oldFill = os.fill();
200  if (descriptions_.empty() && !defaultDescDefined_) {
201  indentation += DocFormatHelper::offsetModuleLabel();
202  os << std::setfill(' ') << std::setw(indentation) << "";
203  os << "There are no PSet descriptions defined for this plugin.\n";
204  os << std::setfill(' ') << std::setw(indentation) << "";
205  os << "PSets will not be validated and no cfi files will be generated.\n";
206  if (!brief) os << "\n";
207  os.fill(oldFill);
208  return;
209  }
210 
212  indentation += DocFormatHelper::offsetModuleLabel();
213  os << std::setfill(' ') << std::setw(indentation) << "";
214  os << "This plugin has not implemented the function which defines its\n";
215  os << std::setfill(' ') << std::setw(indentation) << "";
216  os << "configuration descriptions yet. No descriptions are available.\n";
217  os << std::setfill(' ') << std::setw(indentation) << "";
218  os << "Its PSets will not be validated, and no cfi files will be generated.\n";
219  if (!brief) os << "\n";
220  os.fill(oldFill);
221  return;
222  }
223 
224  if (!brief) {
225  std::stringstream ss;
226  if (defaultDescDefined_) {
227  if (descriptions_.empty()) {
228  ss << "This plugin has only one PSet description. "
229  << "This description is always used to validate configurations. "
230  << "Because this configuration has no label, no cfi files will be generated.";
231  }
232  else {
233  ss << "This plugin has " << (descriptions_.size() + 1U) << " PSet descriptions. "
234  << "The description used to validate a configuration is selected by "
235  << "matching the module labels. If none match, then the last description, "
236  << "which has no label, is selected. "
237  << "A cfi file will be generated for each configuration with a module label.";
238  }
239  }
240  else {
241  if (descriptions_.size() == 1U) {
242  ss << "This plugin has " << descriptions_.size() << " PSet description. "
243  << "This description is always used to validate configurations. "
244  << "The label below is used when generating the cfi file.";
245  }
246  else {
247  ss << "This plugin has " << descriptions_.size() << " PSet descriptions. "
248  << "The description used to validate a configuration is selected by "
249  << "matching the module labels. If none match the first description below is used. "
250  << "The module labels below are also used when generating the cfi files.";
251  }
252  }
253  DocFormatHelper::wrapAndPrintText(os, ss.str(), indentation, lineWidth);
254  os << "\n";
255  }
256 
257  indentation += DocFormatHelper::offsetModuleLabel();
258 
260  counter.iPlugin = iPlugin;
261  counter.iSelectedModule = 0;
262  counter.iModule = 0;
263 
265  this,
266  _1,
267  boost::ref(os),
268  boost::cref(moduleLabel),
269  brief,
270  printOnlyLabels,
271  lineWidth,
272  indentation,
273  boost::ref(counter)));
274 
275  if (defaultDescDefined_) {
276  printForLabel(os,
277  std::string("@default"),
278  defaultDesc_,
279  moduleLabel,
280  brief,
281  printOnlyLabels,
282  lineWidth,
283  indentation,
284  counter);
285  }
286  }
287 
288  void
289  ConfigurationDescriptions::printForLabel(std::pair<std::string, ParameterSetDescription> const& labelAndDesc,
290  std::ostream & os,
291  std::string const& moduleLabel,
292  bool brief,
293  bool printOnlyLabels,
294  size_t lineWidth,
295  int indentation,
296  DescriptionCounter & counter) const
297  {
298  printForLabel(os,
299  labelAndDesc.first,
300  labelAndDesc.second,
301  moduleLabel,
302  brief,
303  printOnlyLabels,
304  lineWidth,
305  indentation,
306  counter);
307  }
308 
309  void
311  std::string const& label,
313  std::string const& moduleLabel,
314  bool brief,
315  bool printOnlyLabels,
316  size_t lineWidth,
317  int indentation,
318  DescriptionCounter & counter) const
319  {
320  ++counter.iModule;
321  if (!moduleLabel.empty() && label != moduleLabel) return;
322  ++counter.iSelectedModule;
323 
324  std::stringstream ss;
325  ss << counter.iPlugin << "." << counter.iSelectedModule;
326  std::string section = ss.str();
327 
328  char oldFill = os.fill();
329  os << std::setfill(' ') << std::setw(indentation) << "";
330  os.fill(oldFill);
331  os << section << " ";
332  if (label == std::string("@default")) {
333  os << "description without a module label\n";
334  }
335  else {
336  if (!brief) {
337  if (baseType_ == std::string("Source") || baseType_ == std::string("Service")) {
338  os << "label: ";
339  }
340  else {
341  os << "module label: ";
342  }
343  }
344  os << label << "\n";
345  }
346 
347  if (!brief) {
348  if (!description.comment().empty()) {
349  DocFormatHelper::wrapAndPrintText(os, description.comment(), indentation, lineWidth - indentation);
350  }
351  os << "\n";
352  }
353  if (printOnlyLabels) return;
354 
355  DocFormatHelper dfh;
356  dfh.setBrief(brief);
357  dfh.setLineWidth(lineWidth);
359  dfh.setSection(section);
361 
362  description.print(os, dfh);
363  }
364 }
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
tuple pset
Definition: CrabTask.py:85
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)