File indexing completed on 2025-03-26 01:51:13
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include "FWCore/ParameterSet/interface/ParameterDescriptionBase.h"
0014
0015 #include "FWCore/ParameterSet/interface/DocFormatHelper.h"
0016 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0017 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0018 #include "FWCore/Utilities/interface/EDMException.h"
0019
0020 #include <iomanip>
0021 #include <iostream>
0022
0023 namespace edm {
0024
0025 ParameterDescriptionBase::ParameterDescriptionBase(
0026 std::string const& iLabel, ParameterTypes iType, bool isTracked, bool hasDefault, Comment const& iComment)
0027 : ParameterDescriptionNode(iComment),
0028 label_(iLabel),
0029 type_(iType),
0030 isTracked_(isTracked),
0031 hasDefault_(hasDefault) {
0032 if (label_.empty()) {
0033 throw Exception(errors::LogicError) << "Empty string used as a label for a parameter. This is\n"
0034 "not allowed.\n";
0035 }
0036 }
0037
0038 ParameterDescriptionBase::ParameterDescriptionBase(
0039 char const* iLabel, ParameterTypes iType, bool isTracked, bool hasDefault, Comment const& iComment)
0040 : ParameterDescriptionNode(iComment),
0041 label_(iLabel),
0042 type_(iType),
0043 isTracked_(isTracked),
0044 hasDefault_(hasDefault) {
0045 if (label_.empty()) {
0046 throw Exception(errors::LogicError) << "Empty string used as a label for a parameter. This is\n"
0047 "not allowed.\n";
0048 }
0049 }
0050
0051 ParameterDescriptionBase::~ParameterDescriptionBase() {}
0052
0053 void ParameterDescriptionBase::throwParameterWrongTrackiness() const {
0054 std::string tr("a tracked");
0055 std::string shouldBe("untracked");
0056 if (isTracked()) {
0057 tr = "an untracked";
0058 shouldBe = "tracked";
0059 }
0060
0061 throw Exception(errors::Configuration) << "In the configuration, parameter \"" << label()
0062 << "\" is defined "
0063 "as "
0064 << tr << " " << parameterTypeEnumToString(type()) << ".\n"
0065 << "It should be " << shouldBe << ".\n";
0066 }
0067
0068 void ParameterDescriptionBase::throwParameterWrongType() const {
0069 std::string tr("an untracked");
0070 if (isTracked())
0071 tr = "a tracked";
0072
0073 throw Exception(errors::Configuration) << "Parameter \"" << label()
0074 << "\" should be defined "
0075 "as "
0076 << tr << " " << parameterTypeEnumToString(type()) << ".\n"
0077 << "The type in the configuration is incorrect.\n";
0078 }
0079
0080 void ParameterDescriptionBase::throwMissingRequiredNoDefault() const {
0081 std::string tr("untracked");
0082 if (isTracked())
0083 tr = "tracked";
0084
0085 throw Exception(errors::Configuration)
0086 << "Missing required parameter. It should have label \"" << label() << "\" and have type \"" << tr << " "
0087 << parameterTypeEnumToString(type()) << "\".\n"
0088 << "The description has no default. The parameter must be defined "
0089 "in the configuration\n";
0090 }
0091
0092 void ParameterDescriptionBase::checkAndGetLabelsAndTypes_(std::set<std::string>& usedLabels,
0093 std::set<ParameterTypes>& parameterTypes,
0094 std::set<ParameterTypes>& ) const {
0095 usedLabels.insert(label());
0096 parameterTypes.insert(type());
0097 }
0098
0099 void ParameterDescriptionBase::validate_(ParameterSet& pset,
0100 std::set<std::string>& validatedLabels,
0101 Modifier modifier) const {
0102 bool exists = exists_(pset, isTracked());
0103
0104 if (exists) {
0105 validatedLabels.insert(label());
0106 } else if (exists_(pset, !isTracked())) {
0107 throwParameterWrongTrackiness();
0108 } else if (pset.exists(label())) {
0109 throwParameterWrongType();
0110 }
0111
0112 if (exists and modifier == Modifier::kObsolete) {
0113 edm::LogWarning("Configuration") << "ignoring obsolete parameter '" << label() << "'";
0114 return;
0115 }
0116 if (!exists && modifier == Modifier::kNone) {
0117 if (hasDefault()) {
0118 insertDefault_(pset);
0119 validatedLabels.insert(label());
0120 } else {
0121 throwMissingRequiredNoDefault();
0122 }
0123 }
0124 }
0125
0126 void ParameterDescriptionBase::writeCfi_(std::ostream& os,
0127 Modifier modifier,
0128 bool& startWithComma,
0129 int indentation,
0130 CfiOptions& options,
0131 bool& wroteSomething) const {
0132 if (label().empty() or label()[0] == '@') {
0133 return;
0134 }
0135
0136 auto check = cfi::needToSwitchToTyped(label(), options);
0137 if (check.first) {
0138 CfiOptions fullOp = cfi::Typed{};
0139 writeFullCfi(os, modifier, startWithComma, indentation, fullOp, wroteSomething);
0140 } else if (shouldWriteUntyped(options)) {
0141 if (modifier != Modifier::kObsolete) {
0142 writeLabelValueCfi(os, modifier == Modifier::kOptional, startWithComma, indentation, options, wroteSomething);
0143 }
0144 } else {
0145 writeFullCfi(os, modifier, startWithComma, indentation, options, wroteSomething);
0146 }
0147 }
0148
0149 void ParameterDescriptionBase::writeLabelValueCfi(std::ostream& os,
0150 bool optional,
0151 bool& startWithComma,
0152 int indentation,
0153 CfiOptions& options,
0154 bool& wroteSomething) const {
0155 constexpr std::string_view k_endList = "]";
0156 constexpr std::string_view k_endParenthesis = ")";
0157 constexpr std::string_view k_endBasicType = "";
0158 if (not hasDefault()) {
0159 return;
0160 }
0161 wroteSomething = true;
0162 if (startWithComma)
0163 os << ",";
0164 startWithComma = true;
0165 os << "\n";
0166 printSpaces(os, indentation);
0167
0168 os << label() << " = ";
0169 std::string_view endDelimiter = k_endBasicType;
0170 switch (type()) {
0171 case k_vdouble:
0172 case k_vint32:
0173 case k_vint64:
0174 case k_vstringRaw:
0175 case k_vuint32:
0176 case k_vuint64:
0177 case k_VESInputTag:
0178 case k_VEventID:
0179 case k_VEventRange:
0180 case k_VInputTag:
0181 case k_VLuminosityBlockID:
0182 case k_VLuminosityBlockRange:
0183 case k_VPSet:
0184 os << "[";
0185 endDelimiter = k_endList;
0186 break;
0187 case k_PSet:
0188 os << "dict(";
0189 endDelimiter = k_endParenthesis;
0190 break;
0191 case k_EventID:
0192 case k_EventRange:
0193 case k_ESInputTag:
0194 case k_InputTag:
0195 case k_LuminosityBlockID:
0196 case k_LuminosityBlockRange:
0197 os << "(";
0198 endDelimiter = k_endParenthesis;
0199 break;
0200 default:
0201 break;
0202 }
0203 writeCfi_(os, indentation, options);
0204 os << endDelimiter;
0205 return;
0206 }
0207
0208 void ParameterDescriptionBase::writeFullCfi(std::ostream& os,
0209 Modifier modifier,
0210 bool& startWithComma,
0211 int indentation,
0212 CfiOptions& options,
0213 bool& wroteSomething) const {
0214 wroteSomething = true;
0215 if (startWithComma)
0216 os << ",";
0217 startWithComma = true;
0218
0219 os << "\n";
0220 printSpaces(os, indentation);
0221
0222 os << label() << " = cms.";
0223
0224 if (modifier == Modifier::kObsolete or !hasDefault()) {
0225 if (modifier == Modifier::kOptional) {
0226 os << "optional.";
0227 } else if (modifier == Modifier::kObsolete) {
0228 os << "obsolete.";
0229 } else {
0230 os << "required.";
0231 }
0232 if (!isTracked())
0233 os << "untracked.";
0234 os << parameterTypeEnumToString(type());
0235 } else {
0236 if (!isTracked())
0237 os << "untracked.";
0238 os << parameterTypeEnumToString(type()) << "(";
0239 writeCfi_(os, indentation, options);
0240 os << ")";
0241 }
0242 }
0243 void ParameterDescriptionBase::print_(std::ostream& os,
0244 Modifier modifier,
0245 bool writeToCfi,
0246 DocFormatHelper& dfh) const {
0247 const bool optional = (modifier == Modifier::kOptional);
0248 const bool obsolete = (modifier == Modifier::kObsolete);
0249 if (dfh.pass() == 0) {
0250 dfh.setAtLeast1(label().size());
0251 if (isTracked()) {
0252 dfh.setAtLeast2(parameterTypeEnumToString(type()).size());
0253 } else {
0254 dfh.setAtLeast2(parameterTypeEnumToString(type()).size() + 10U);
0255 }
0256 if (optional) {
0257 dfh.setAtLeast3(8U);
0258 }
0259 } else {
0260 if (dfh.brief()) {
0261 std::ios::fmtflags oldFlags = os.flags();
0262
0263 dfh.indent(os);
0264 os << std::left << std::setw(dfh.column1()) << label() << " ";
0265
0266 if (isTracked()) {
0267 os << std::setw(dfh.column2()) << parameterTypeEnumToString(type());
0268 } else {
0269 std::stringstream ss;
0270 ss << "untracked ";
0271 ss << parameterTypeEnumToString(type());
0272 os << std::setw(dfh.column2()) << ss.str();
0273 }
0274 os << " ";
0275
0276 os << std::setw(dfh.column3());
0277 if (optional) {
0278 os << "optional";
0279 } else if (obsolete) {
0280 os << "obsolete";
0281 } else {
0282 os << "";
0283 }
0284 os << " ";
0285 os.flags(oldFlags);
0286 printDefault_(os, writeToCfi, dfh);
0287 } else {
0288
0289 dfh.indent(os);
0290 os << label() << "\n";
0291
0292 dfh.indent2(os);
0293 os << "type: ";
0294 if (!isTracked())
0295 os << "untracked ";
0296
0297 os << parameterTypeEnumToString(type()) << " ";
0298
0299 if (optional)
0300 os << "optional";
0301 if (obsolete)
0302 os << "obsolete";
0303 os << "\n";
0304
0305 dfh.indent2(os);
0306 printDefault_(os, writeToCfi, dfh);
0307
0308 if (!comment().empty()) {
0309 DocFormatHelper::wrapAndPrintText(os, comment(), dfh.startColumn2(), dfh.commentWidth());
0310 }
0311 os << "\n";
0312 }
0313 }
0314 }
0315
0316 void ParameterDescriptionBase::printDefault_(std::ostream& os, bool writeToCfi, DocFormatHelper& dfh) const {
0317 if (!dfh.brief())
0318 os << "default: ";
0319 if (writeToCfi && hasDefault()) {
0320 if (hasNestedContent()) {
0321 os << "see Section " << dfh.section() << "." << dfh.counter();
0322 } else {
0323 if (dfh.brief()) {
0324 writeDoc_(os, dfh.indentation());
0325 } else {
0326 writeDoc_(os, dfh.startColumn2());
0327 }
0328 }
0329 } else if (!writeToCfi) {
0330 os << "none (do not write to cfi)";
0331 } else {
0332 os << "none";
0333 }
0334 os << "\n";
0335 }
0336
0337 void ParameterDescriptionBase::printNestedContent_(std::ostream& os, bool , DocFormatHelper& dfh) const {
0338 int indentation = dfh.indentation();
0339 if (dfh.parent() != DocFormatHelper::TOP) {
0340 indentation -= DocFormatHelper::offsetSectionContent();
0341 }
0342
0343 printSpaces(os, indentation);
0344 os << "Section " << dfh.section() << "." << dfh.counter() << " " << label() << " default contents: ";
0345 writeDoc_(os, indentation + 2);
0346 os << "\n";
0347 if (!dfh.brief())
0348 os << "\n";
0349 }
0350
0351 bool ParameterDescriptionBase::partiallyExists_(ParameterSet const& pset) const { return exists(pset); }
0352
0353 int ParameterDescriptionBase::howManyXORSubNodesExist_(ParameterSet const& pset) const {
0354 return exists(pset) ? 1 : 0;
0355 }
0356 }