ParameterSetDescription

SetDescriptionEntry

Macros

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
#ifndef FWCore_ParameterSet_ParameterSetDescription_h
#define FWCore_ParameterSet_ParameterSetDescription_h
// -*- C++ -*-
//
// Package:     ParameterSet
// Class  :     ParameterSetDescription
//
/**\class ParameterSetDescription ParameterSetDescription.h FWCore/ParameterSet/interface/ParameterSetDescription.h

 Description: Used to describe the allowed values in a ParameterSet

 Usage:
    <usage>


 Implementation Details:

    Note that there are some comments in the file ParameterDescriptionNode.h
    that might be useful for someone attempting to understand the implementation
    details.  This class holds a container full of nodes.  One node can represent
    a description of a single parameter or some logical restriction on the
    combinations of parameters allowed in a ParameterSet.  Often these logical
    restrictions are implemented by the nodes themselves containing a tree
    structure of other nodes.
*/
//
// Original Author:  Chris Jones
//         Created:  Tue Jul 31 15:18:40 EDT 2007
//

#include "FWCore/Utilities/interface/value_ptr.h"
#include "FWCore/ParameterSet/interface/ParameterDescriptionNode.h"

#include <vector>
#include <set>
#include <string>
#include <memory>
#include <iosfwd>

namespace edm {

  class ParameterSet;
  class ParameterDescriptionBase;
  class ParameterWildcardBase;
  class ParameterDescriptionNode;
  template <typename T>
  class ParameterDescription;
  template <typename T>
  class ParameterDescriptionCases;
  class DocFormatHelper;

  class ParameterSetDescription {
  public:
    using Modifier = ParameterModifier;
    class SetDescriptionEntry {
    public:
      bool optional() const { return modifier_ == Modifier::kOptional; }
      bool obsolete() const { return modifier_ == Modifier::kObsolete; }
      Modifier modifier() const { return modifier_; }
      bool writeToCfi() const { return writeToCfi_; }
      edm::value_ptr<ParameterDescriptionNode> const& node() const { return node_; }

      void setModifier(Modifier value) { modifier_ = value; }
      void setWriteToCfi(bool value) { writeToCfi_ = value; }
      ParameterDescriptionNode* setNode(std::unique_ptr<ParameterDescriptionNode> node) {
        node_ = std::move(node);
        return node_.operator->();
      }

    private:
      Modifier modifier_;
      bool writeToCfi_;
      edm::value_ptr<ParameterDescriptionNode> node_;
    };

    typedef std::vector<SetDescriptionEntry> SetDescriptionEntries;
    typedef SetDescriptionEntries::const_iterator const_iterator;

    ParameterSetDescription();
    virtual ~ParameterSetDescription();

    std::string const& comment() const { return comment_; }
    void setComment(std::string const& value);
    void setComment(char const* value);

    ///allow any parameter label/value pairs
    void setAllowAnything();

    // This is set only for parameterizables which have not set their descriptions.
    // This should only be called to allow backwards compatibility.
    void setUnknown();

    // ***** In these next 8 functions named "add", T is the parameter type ******
    // Exceptions: For parameters of type ParameterSet, T should be a
    // ParameterSetDescription instead of a ParameterSet.  And do not
    // use these next 8 functions for parameters of type vector<ParameterSet>

    template <typename T, typename U>
    ParameterDescriptionBase* add(U const& iLabel, T const& value) {
      return add<T, U>(iLabel, value, true, Modifier::kNone, true);
    }

    template <typename T, typename U>
    ParameterDescriptionBase* addUntracked(U const& iLabel, T const& value) {
      return add<T, U>(iLabel, value, false, Modifier::kNone, true);
    }

    template <typename T, typename U>
    ParameterDescriptionBase* addOptional(U const& iLabel, T const& value) {
      return add<T, U>(iLabel, value, true, Modifier::kOptional, true);
    }

    template <typename T, typename U>
    ParameterDescriptionBase* addOptionalUntracked(U const& iLabel, T const& value) {
      return add<T, U>(iLabel, value, false, Modifier::kOptional, true);
    }

    // For the next 4 functions, there is no default so they will not get injected
    // during validation if missing and they will not get written into cfi files.

    template <typename T, typename U>
    ParameterDescriptionBase* add(U const& iLabel) {
      return add<T, U>(iLabel, true, Modifier::kNone, true);
    }

    template <typename T, typename U>
    ParameterDescriptionBase* addUntracked(U const& iLabel) {
      return add<T, U>(iLabel, false, Modifier::kNone, true);
    }

    template <typename T, typename U>
    ParameterDescriptionBase* addOptional(U const& iLabel) {
      return add<T, U>(iLabel, true, Modifier::kOptional, true);
    }

    template <typename T, typename U>
    ParameterDescriptionBase* addOptionalUntracked(U const& iLabel) {
      return add<T, U>(iLabel, false, Modifier::kOptional, true);
    }

    template <typename T, typename U>
    ParameterDescriptionBase* addObsolete(U const& iLabel) {
      if constexpr (std::is_same_v<T, edm::ParameterSetDescription>) {
        return add<T, U>(iLabel, T(), true, Modifier::kObsolete, true);
      } else {
        return add<T, U>(iLabel, true, Modifier::kObsolete, true);
      }
    }

    template <typename T, typename U>
    ParameterDescriptionBase* addObsoleteUntracked(U const& iLabel) {
      if constexpr (std::is_same_v<T, edm::ParameterSetDescription>) {
        return add<T, U>(iLabel, T(), false, Modifier::kObsolete, true);
      } else {
        return add<T, U>(iLabel, false, Modifier::kObsolete, true);
      }
    }

    // ***** Use these 8 functions for parameters of type vector<ParameterSet> *****
    // When a vector<ParameterSet> appears in a configuration, all of its
    // elements will be validated using the description in the argument named
    // "validator" below.  The argument named "defaults" is used when the
    // a vector<ParameterSet> is required to be in the configuration and
    // is absent.  Note that these default ParameterSet's will be validated
    // as if they had appeared in the configuration so they must be consistent
    // with the description and missing parameters that have defaults in
    // in the description will be inserted during validation.  These defaults
    // are also used when writing cfi files.

    template <typename U>
    ParameterDescriptionBase* addVPSet(U const& iLabel,
                                       ParameterSetDescription const& validator,
                                       std::vector<ParameterSet> const& defaults) {
      return addVPSet<U>(iLabel, validator, defaults, true, Modifier::kNone, true);
    }

    template <typename U>
    ParameterDescriptionBase* addVPSetUntracked(U const& iLabel,
                                                ParameterSetDescription const& validator,
                                                std::vector<ParameterSet> const& defaults) {
      return addVPSet<U>(iLabel, validator, defaults, false, Modifier::kNone, true);
    }

    template <typename U>
    ParameterDescriptionBase* addVPSetOptional(U const& iLabel,
                                               ParameterSetDescription const& validator,
                                               std::vector<ParameterSet> const& defaults) {
      return addVPSet<U>(iLabel, validator, defaults, true, Modifier::kOptional, true);
    }

    template <typename U>
    ParameterDescriptionBase* addVPSetOptionalUntracked(U const& iLabel,
                                                        ParameterSetDescription const& validator,
                                                        std::vector<ParameterSet> const& defaults) {
      return addVPSet<U>(iLabel, validator, defaults, false, Modifier::kOptional, true);
    }

    template <typename U>
    ParameterDescriptionBase* addVPSet(U const& iLabel, ParameterSetDescription const& validator) {
      return addVPSet<U>(iLabel, validator, true, Modifier::kNone, true);
    }

    template <typename U>
    ParameterDescriptionBase* addVPSetUntracked(U const& iLabel, ParameterSetDescription const& validator) {
      return addVPSet<U>(iLabel, validator, false, Modifier::kNone, true);
    }

    template <typename U>
    ParameterDescriptionBase* addVPSetOptional(U const& iLabel, ParameterSetDescription const& validator) {
      return addVPSet<U>(iLabel, validator, true, Modifier::kOptional, true);
    }

    template <typename U>
    ParameterDescriptionBase* addVPSetOptionalUntracked(U const& iLabel, ParameterSetDescription const& validator) {
      return addVPSet<U>(iLabel, validator, false, Modifier::kOptional, true);
    }

    template <typename U>
    ParameterDescriptionBase* addVPSetObsolete(U const& iLabel) {
      ParameterSetDescription validator;
      return addVPSet<U>(iLabel, validator, true, Modifier::kObsolete, true);
    }
    template <typename U>
    ParameterDescriptionBase* addVPSetObsoleteUntracked(U const& iLabel) {
      ParameterSetDescription validator;
      return addVPSet<U>(iLabel, validator, false, Modifier::kObsolete, true);
    }

    // ********* Wildcards *********

    template <typename T, typename U>
    ParameterWildcardBase* addWildcard(U const& pattern) {
      return addWildcard<T, U>(pattern, true);
    }

    template <typename T, typename U>
    ParameterWildcardBase* addWildcardUntracked(U const& pattern) {
      return addWildcard<T, U>(pattern, false);
    }

    // ********* Used to insert generic nodes of any type ************

    ParameterDescriptionNode* addNode(ParameterDescriptionNode const& node);
    ParameterDescriptionNode* addNode(std::unique_ptr<ParameterDescriptionNode> node);
    ParameterDescriptionNode* addOptionalNode(ParameterDescriptionNode const& node, bool writeToCfi);
    ParameterDescriptionNode* addOptionalNode(std::unique_ptr<ParameterDescriptionNode> node, bool writeToCfi);

    // ********* Switches ************
    // ifValue will only work with type T as a bool, int, or string.
    // T holds the value of the switch variable.
    // If you try using any other type, then it will not compile.
    template <typename T>
    ParameterDescriptionNode* ifValue(ParameterDescription<T> const& switchParameter,
                                      std::unique_ptr<ParameterDescriptionCases<T>> cases) {
      return ifValue<T>(switchParameter, std::move(cases), Modifier::kNone, true);
    }

    template <typename T>
    ParameterDescriptionNode* ifValueOptional(ParameterDescription<T> const& switchParameter,
                                              std::unique_ptr<ParameterDescriptionCases<T>> cases,
                                              bool writeToCfi) {
      return ifValue<T>(switchParameter, std::move(cases), Modifier::kOptional, writeToCfi);
    }

    // ********* if exists ************
    ParameterDescriptionNode* ifExists(ParameterDescriptionNode const& node1, ParameterDescriptionNode const& node2) {
      return ifExists(node1, node2, Modifier::kNone, true);
    }

    ParameterDescriptionNode* ifExistsOptional(ParameterDescriptionNode const& node1,
                                               ParameterDescriptionNode const& node2,
                                               bool writeToCfi) {
      return ifExists(node1, node2, Modifier::kOptional, writeToCfi);
    }

    // ********* for parameters that are a list of allowed labels *********
    template <typename T, typename U>
    ParameterDescriptionNode* labelsFrom(U const& iLabel) {
      return labelsFrom<T, U>(iLabel, true, Modifier::kNone, true);
    }

    template <typename T, typename U>
    ParameterDescriptionNode* labelsFromUntracked(U const& iLabel) {
      return labelsFrom<T, U>(iLabel, false, Modifier::kNone, true);
    }

    template <typename T, typename U>
    ParameterDescriptionNode* labelsFromOptional(U const& iLabel, bool writeToCfi) {
      return labelsFrom<T, U>(iLabel, true, Modifier::kOptional, writeToCfi);
    }

    template <typename T, typename U>
    ParameterDescriptionNode* labelsFromOptionalUntracked(U const& iLabel, bool writeToCfi) {
      return labelsFrom<T, U>(iLabel, false, Modifier::kOptional, writeToCfi);
    }

    // These next four functions only work when the template
    // parameters are:
    // T = ParameterSetDescription and V = ParameterSetDescription
    // or
    // T = vector<ParameterSet> and V = ParameterSetDescription
    // In either case U can be either a string or char*
    // Note the U and V can be determined from the arguments, but
    // T must be explicitly specified by the calling function.
    template <typename T, typename U, typename V>
    ParameterDescriptionNode* labelsFrom(U const& iLabel, V const& desc) {
      return labelsFrom<T, U, V>(iLabel, true, Modifier::kNone, true, desc);
    }

    template <typename T, typename U, typename V>
    ParameterDescriptionNode* labelsFromUntracked(U const& iLabel, V const& desc) {
      return labelsFrom<T, U, V>(iLabel, false, Modifier::kNone, true, desc);
    }

    template <typename T, typename U, typename V>
    ParameterDescriptionNode* labelsFromOptional(U const& iLabel, bool writeToCfi, V const& desc) {
      return labelsFrom<T, U, V>(iLabel, true, Modifier::kOptional, writeToCfi, desc);
    }

    template <typename T, typename U, typename V>
    ParameterDescriptionNode* labelsFromOptionalUntracked(U const& iLabel, bool writeToCfi, V const& desc) {
      return labelsFrom<T, U, V>(iLabel, false, Modifier::kOptional, writeToCfi, desc);
    }

    bool anythingAllowed() const { return anythingAllowed_; }
    bool isUnknown() const { return unknown_; }

    const_iterator begin() const { return entries_.begin(); }

    const_iterator end() const { return entries_.end(); }

    bool empty() const noexcept { return entries_.empty(); }
    // Better performance if space is reserved for the number of
    // top level parameters before any are added.
    void reserve(SetDescriptionEntries::size_type n) { entries_.reserve(n); }

    void validate(ParameterSet& pset) const;

    void writeCfi(std::ostream& os, bool startWithComma, int indentation, CfiOptions&) const;

    void print(std::ostream& os, DocFormatHelper& dfh) const;

    bool isLabelUnused(std::string const& label) const;

  private:
    template <typename T, typename U>
    ParameterDescriptionBase* add(U const& iLabel, T const& value, bool isTracked, Modifier modifier, bool writeToCfi);

    template <typename T, typename U>
    ParameterDescriptionBase* add(U const& iLabel, bool isTracked, Modifier modifier, bool writeToCfi);

    template <typename U>
    ParameterDescriptionBase* addVPSet(U const& iLabel,
                                       ParameterSetDescription const& validator,
                                       std::vector<ParameterSet> const& defaults,
                                       bool isTracked,
                                       Modifier modifier,
                                       bool writeToCfi);

    template <typename U>
    ParameterDescriptionBase* addVPSet(
        U const& iLabel, ParameterSetDescription const& validator, bool isTracked, Modifier modifier, bool writeToCfi);

    template <typename T, typename U>
    ParameterWildcardBase* addWildcard(U const& pattern, bool isTracked);

    ParameterDescriptionNode* addNode(std::unique_ptr<ParameterDescriptionNode> node,
                                      Modifier modifier,
                                      bool writeToCfi);

    template <typename T>
    ParameterDescriptionNode* ifValue(ParameterDescription<T> const& switchParameter,
                                      std::unique_ptr<ParameterDescriptionCases<T>> cases,
                                      Modifier modifier,
                                      bool writeToCfi);

    ParameterDescriptionNode* ifExists(ParameterDescriptionNode const& node1,
                                       ParameterDescriptionNode const& node2,
                                       Modifier modifier,
                                       bool writeToCfi);

    template <typename T, typename U>
    ParameterDescriptionNode* labelsFrom(U const& iLabel, bool isTracked, Modifier modifier, bool writeToCfi);

    template <typename T, typename U, typename V>
    ParameterDescriptionNode* labelsFrom(
        U const& iLabel, bool isTracked, Modifier modifier, bool writeToCfi, V const& desc);

    static void validateNode(SetDescriptionEntry const& entry,
                             ParameterSet& pset,
                             std::set<std::string>& validatedNames);

    static void throwIllegalParameters(std::vector<std::string> const& parameterNames,
                                       std::set<std::string> const& validatedNames);

    static void writeNode(SetDescriptionEntry const& entry,
                          std::ostream& os,
                          bool& startWithComma,
                          int indentation,
                          CfiOptions&,
                          bool& wroteSomething);

    static void printNode(SetDescriptionEntry const& entry, std::ostream& os, DocFormatHelper& dfh);

    void throwIfLabelsAlreadyUsed(std::set<std::string> const& nodeLabels);
    void throwIfWildcardCollision(std::set<ParameterTypes> const& nodeParameterTypes,
                                  std::set<ParameterTypes> const& nodeWildcardTypes);

    bool anythingAllowed_;
    bool unknown_;
    SetDescriptionEntries entries_;

    std::set<std::string> usedLabels_;
    std::set<ParameterTypes> typesUsedForParameters_;
    std::set<ParameterTypes> typesUsedForWildcards_;

    std::string comment_;
  };
}  // namespace edm

#include "FWCore/ParameterSet/interface/ParameterWildcard.h"
#include "FWCore/ParameterSet/interface/ParameterSwitch.h"
#include "FWCore/ParameterSet/interface/AllowedLabelsDescription.h"

namespace edm {

  template <typename T, typename U>
  ParameterDescriptionBase* ParameterSetDescription::add(
      U const& iLabel, T const& value, bool isTracked, Modifier modifier, bool writeToCfi) {
    std::unique_ptr<ParameterDescriptionNode> node =
        std::make_unique<ParameterDescription<T>>(iLabel, value, isTracked);
    ParameterDescriptionNode* pnode = addNode(std::move(node), modifier, writeToCfi);
    return static_cast<ParameterDescriptionBase*>(pnode);
  }

  template <typename T, typename U>
  ParameterDescriptionBase* ParameterSetDescription::add(U const& iLabel,
                                                         bool isTracked,
                                                         Modifier modifier,
                                                         bool writeToCfi) {
    std::unique_ptr<ParameterDescriptionNode> node = std::make_unique<ParameterDescription<T>>(iLabel, isTracked);
    ParameterDescriptionNode* pnode = addNode(std::move(node), modifier, writeToCfi);
    return static_cast<ParameterDescriptionBase*>(pnode);
  }

  template <typename U>
  ParameterDescriptionBase* ParameterSetDescription::addVPSet(U const& iLabel,
                                                              ParameterSetDescription const& validator,
                                                              std::vector<ParameterSet> const& defaults,
                                                              bool isTracked,
                                                              Modifier modifier,
                                                              bool writeToCfi) {
    std::unique_ptr<ParameterDescriptionNode> node =
        std::make_unique<ParameterDescription<std::vector<ParameterSet>>>(iLabel, validator, isTracked, defaults);
    ParameterDescriptionNode* pnode = addNode(std::move(node), modifier, writeToCfi);
    return static_cast<ParameterDescriptionBase*>(pnode);
  }

  template <typename U>
  ParameterDescriptionBase* ParameterSetDescription::addVPSet(
      U const& iLabel, ParameterSetDescription const& validator, bool isTracked, Modifier modifier, bool writeToCfi) {
    std::unique_ptr<ParameterDescriptionNode> node =
        std::make_unique<ParameterDescription<std::vector<ParameterSet>>>(iLabel, validator, isTracked);
    ParameterDescriptionNode* pnode = addNode(std::move(node), modifier, writeToCfi);
    return static_cast<ParameterDescriptionBase*>(pnode);
  }

  template <typename T, typename U>
  ParameterWildcardBase* ParameterSetDescription::addWildcard(U const& pattern, bool isTracked) {
    std::unique_ptr<ParameterDescriptionNode> node =
        std::make_unique<ParameterWildcard<T>>(pattern, RequireZeroOrMore, isTracked);
    ParameterDescriptionNode* pnode = addNode(std::move(node), Modifier::kOptional, true);
    return static_cast<ParameterWildcardBase*>(pnode);
  }

  template <typename T>
  ParameterDescriptionNode* ParameterSetDescription::ifValue(ParameterDescription<T> const& switchParameter,
                                                             std::unique_ptr<ParameterDescriptionCases<T>> cases,
                                                             Modifier modifier,
                                                             bool writeToCfi) {
    std::unique_ptr<ParameterDescriptionNode> pdswitch =
        std::make_unique<ParameterSwitch<T>>(switchParameter, std::move(cases));
    return addNode(std::move(pdswitch), modifier, writeToCfi);
  }

  template <typename T, typename U>
  ParameterDescriptionNode* ParameterSetDescription::labelsFrom(U const& iLabel,
                                                                bool isTracked,
                                                                Modifier modifier,
                                                                bool writeToCfi) {
    std::unique_ptr<ParameterDescriptionNode> pd = std::make_unique<AllowedLabelsDescription<T>>(iLabel, isTracked);
    return addNode(std::move(pd), modifier, writeToCfi);
  }

  template <typename T, typename U, typename V>
  ParameterDescriptionNode* ParameterSetDescription::labelsFrom(
      U const& iLabel, bool isTracked, Modifier modifier, bool writeToCfi, V const& desc) {
    std::unique_ptr<ParameterDescriptionNode> pd =
        std::make_unique<AllowedLabelsDescription<T>>(iLabel, desc, isTracked);
    return addNode(std::move(pd), modifier, writeToCfi);
  }
}  // namespace edm

#endif