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
#include "DataFormats/Provenance/interface/ProductDescription.h"

#include "FWCore/Utilities/interface/EDMException.h"
#include "FWCore/Utilities/interface/Exception.h"
#include "FWCore/Utilities/interface/FriendlyName.h"
#include "FWCore/Reflection/interface/FunctionWithDict.h"
#include "FWCore/Reflection/interface/TypeWithDict.h"
#include "FWCore/Utilities/interface/WrappedClassName.h"

#include "TDictAttributeMap.h"

#include <cassert>
#include <ostream>
#include <sstream>

class TClass;

namespace edm {
  BranchDescription::Transients::Transients()
      : branchName_(),
        wrappedName_(),
        wrappedType_(),
        unwrappedType_(),
        produced_(false),
        onDemand_(false),
        isTransform_(false),
        dropped_(false),
        transient_(false),
        availableOnlyAtEndTransition_(false),
        isMergeable_(false) {}

  void BranchDescription::Transients::reset() { *this = BranchDescription::Transients(); }

  BranchDescription::BranchDescription()
      : branchType_(InEvent),
        moduleLabel_(),
        processName_(),
        branchID_(),
        fullClassName_(),
        friendlyClassName_(),
        productInstanceName_(),
        branchAliases_(),
        aliasForBranchID_(),
        transient_() {
    // do not call init here! It will result in an exception throw.
  }

  BranchDescription::BranchDescription(BranchType const& branchType,
                                       std::string const& moduleLabel,
                                       std::string const& processName,
                                       std::string const& className,
                                       std::string const& friendlyClassName,
                                       std::string const& productInstanceName,
                                       TypeWithDict const& theTypeWithDict,
                                       bool produced,
                                       bool availableOnlyAtEndTransition,
                                       std::set<std::string> const& aliases)
      : branchType_(branchType),
        moduleLabel_(moduleLabel),
        processName_(processName),
        branchID_(),
        fullClassName_(className),
        friendlyClassName_(friendlyClassName),
        productInstanceName_(productInstanceName),
        branchAliases_(aliases),
        transient_() {
    setDropped(false);
    setProduced(produced);
    setOnDemand(false);
    transient_.availableOnlyAtEndTransition_ = availableOnlyAtEndTransition;
    setUnwrappedType(theTypeWithDict);
    init();
  }

  BranchDescription::BranchDescription(BranchDescription const& aliasForBranch,
                                       std::string const& moduleLabelAlias,
                                       std::string const& productInstanceAlias)
      : branchType_(aliasForBranch.branchType()),
        moduleLabel_(moduleLabelAlias),
        processName_(aliasForBranch.processName()),
        branchID_(),
        fullClassName_(aliasForBranch.className()),
        friendlyClassName_(aliasForBranch.friendlyClassName()),
        productInstanceName_(productInstanceAlias),
        branchAliases_(aliasForBranch.branchAliases()),
        aliasForBranchID_(aliasForBranch.branchID()),
        transient_() {
    setDropped(false);
    setProduced(aliasForBranch.produced());
    setOnDemand(false);  // will be re-set externally to the aliasForBranch.onDemand() after that one has been set
    transient_.availableOnlyAtEndTransition_ = aliasForBranch.availableOnlyAtEndTransition();
    setUnwrappedType(aliasForBranch.unwrappedType());
    init();
  }

  void BranchDescription::initBranchName() {
    if (!branchName().empty()) {
      return;  // already called
    }
    throwIfInvalid_();

    char const underscore('_');
    char const period('.');

    if (friendlyClassName_.find(underscore) != std::string::npos) {
      throw cms::Exception("IllegalCharacter")
          << "Class name '" << friendlyClassName()
          << "' contains an underscore ('_'), which is illegal in the name of a product.\n";
    }

    // Module labels of non-persistent products are allowed to contain
    // underscores. For module labels of persistent products, the module
    // label is checked for underscores in the function initFromDictionary
    // after we determine whether the product is persistent or not.

    if (productInstanceName_.find(underscore) != std::string::npos) {
      throw cms::Exception("IllegalCharacter")
          << "Product instance name '" << productInstanceName()
          << "' contains an underscore ('_'), which is illegal in a product instance name.\n";
    }

    if (processName_.find(underscore) != std::string::npos) {
      throw cms::Exception("IllegalCharacter")
          << "Process name '" << processName()
          << "' contains an underscore ('_'), which is illegal in a process name.\n";
    }

    std::string& brName = transient_.branchName_;
    brName.reserve(friendlyClassName().size() + moduleLabel().size() + productInstanceName().size() +
                   processName().size() + 4);
    brName += friendlyClassName();
    brName += underscore;
    brName += moduleLabel();
    brName += underscore;
    brName += productInstanceName();
    brName += underscore;
    brName += processName();
    brName += period;

    if (!branchID_.isValid()) {
      branchID_.setID(brName);
    }
  }

  void BranchDescription::initFromDictionary() {
    if (bool(wrappedType())) {
      return;  // already initialized;
    }

    throwIfInvalid_();

    try {
      setWrappedName(wrappedClassName(fullClassName()));
      // unwrapped type.
      setUnwrappedType(TypeWithDict::byName(fullClassName()));
      if (!bool(unwrappedType())) {
        setTransient(false);
        return;
      }
    } catch (edm::Exception& caughtException) {
      caughtException.addContext(std::string{"While initializing meta data for branch: "} + branchName());
      throw;
    }

    edm::TypeWithDict wrType(TypeWithDict::byName(wrappedName()));
    try {
      setWrappedType(wrType);
      if (!bool(wrappedType())) {
        return;
      }
    } catch (edm::Exception& caughtException) {
      caughtException.addContext(std::string{"While initializing meta data for branch: "} + branchName());
      throw;
    }

    setTransient(false);
    TDictAttributeMap* wp = wrappedType().getClass()->GetAttributeMap();
    if (wp && wp->HasKey("persistent") && !strcmp(wp->GetPropertyAsString("persistent"), "false")) {
      // Set transient if persistent == "false".
      setTransient(true);
      return;
    } else {
      // Module labels of persistent products cannot contain underscores,
      // but for non-persistent products it is allowed because path names
      // are used as module labels for path status products and there
      // are many path names that include underscores.
      char const underscore('_');
      if (moduleLabel_.find(underscore) != std::string::npos) {
        throw cms::Exception("IllegalCharacter")
            << "Module label '" << moduleLabel()
            << "' contains an underscore ('_'), which is illegal in a module label.\n";
      }
    }
  }

  void BranchDescription::merge(BranchDescription const& other) {
    branchAliases_.insert(other.branchAliases().begin(), other.branchAliases().end());
  }

  void BranchDescription::setSwitchAliasForBranch(BranchDescription const& aliasForBranch) {
    if (branchType_ != aliasForBranch.branchType()) {
      throw Exception(errors::LogicError) << "BranchDescription::setSwitchAliasForBranch: branchType (" << branchType_
                                          << ") differs from aliasForBranch (" << aliasForBranch.branchType()
                                          << ").\nPlease report this error to the FWCore developers";
    }
    if (produced() != aliasForBranch.produced()) {
      throw Exception(errors::LogicError) << "BranchDescription::setSwitchAliasForBranch: produced differs from "
                                             "aliasForBranch.\nPlease report this error to the FWCore developers";
    }
    if (unwrappedTypeID().typeInfo() != aliasForBranch.unwrappedType().typeInfo()) {
      throw Exception(errors::LogicError)
          << "BranchDescription::setSwitchAliasForBranch: unwrapped type info (" << unwrappedTypeID().name()
          << ") differs from aliasForBranch (" << aliasForBranch.unwrappedType().typeInfo().name()
          << ").\nPlease report this error to the FWCore developers";
    }

    branchAliases_ = aliasForBranch.branchAliases();
    transient_.switchAliasForBranchID_ = aliasForBranch.originalBranchID();
    transient_.availableOnlyAtEndTransition_ = aliasForBranch.availableOnlyAtEndTransition();
  }

  void BranchDescription::write(std::ostream& os) const {
    os << "Branch Type = " << branchType() << std::endl;
    os << "Process Name = " << processName() << std::endl;
    os << "ModuleLabel = " << moduleLabel() << std::endl;
    os << "Branch ID = " << branchID() << '\n';
    os << "Class Name = " << fullClassName() << '\n';
    os << "Friendly Class Name = " << friendlyClassName() << '\n';
    os << "Product Instance Name = " << productInstanceName() << std::endl;
  }

  void throwExceptionWithText(char const* txt) {
    Exception e(errors::LogicError);
    e << "Problem using an incomplete BranchDescription\n"
      << txt << "\nPlease report this error to the FWCore developers";
    throw e;
  }

  void BranchDescription::throwIfInvalid_() const {
    if (branchType_ >= NumBranchTypes)
      throwExceptionWithText("Illegal BranchType detected");

    if (moduleLabel_.empty())
      throwExceptionWithText("Module label is not allowed to be empty");

    if (processName_.empty())
      throwExceptionWithText("Process name is not allowed to be empty");

    if (fullClassName_.empty())
      throwExceptionWithText("Full class name is not allowed to be empty");

    if (friendlyClassName_.empty())
      throwExceptionWithText("Friendly class name is not allowed to be empty");
  }

  void BranchDescription::updateFriendlyClassName() {
    friendlyClassName_ = friendlyname::friendlyName(fullClassName());
    clearBranchName();
    initBranchName();
  }

  bool operator<(BranchDescription const& a, BranchDescription const& b) {
    if (a.processName() < b.processName())
      return true;
    if (b.processName() < a.processName())
      return false;
    if (a.fullClassName() < b.fullClassName())
      return true;
    if (b.fullClassName() < a.fullClassName())
      return false;
    if (a.friendlyClassName() < b.friendlyClassName())
      return true;
    if (b.friendlyClassName() < a.friendlyClassName())
      return false;
    if (a.productInstanceName() < b.productInstanceName())
      return true;
    if (b.productInstanceName() < a.productInstanceName())
      return false;
    if (a.moduleLabel() < b.moduleLabel())
      return true;
    if (b.moduleLabel() < a.moduleLabel())
      return false;
    if (a.branchType() < b.branchType())
      return true;
    if (b.branchType() < a.branchType())
      return false;
    if (a.branchID() < b.branchID())
      return true;
    if (b.branchID() < a.branchID())
      return false;
    if (a.branchAliases() < b.branchAliases())
      return true;
    if (b.branchAliases() < a.branchAliases())
      return false;
    if (a.present() < b.present())
      return true;
    if (b.present() < a.present())
      return false;
    return false;
  }

  bool combinable(BranchDescription const& a, BranchDescription const& b) {
    return (a.branchType() == b.branchType()) && (a.processName() == b.processName()) &&
           (a.fullClassName() == b.fullClassName()) && (a.friendlyClassName() == b.friendlyClassName()) &&
           (a.productInstanceName() == b.productInstanceName()) && (a.moduleLabel() == b.moduleLabel()) &&
           (a.branchID() == b.branchID());
  }

  bool operator==(BranchDescription const& a, BranchDescription const& b) {
    return combinable(a, b) && (a.dropped() == b.dropped()) && (a.branchAliases() == b.branchAliases());
  }

  std::string match(BranchDescription const& a, BranchDescription const& b, std::string const& fileName) {
    std::ostringstream differences;
    if (a.branchName() != b.branchName()) {
      differences << "Branch name '" << b.branchName() << "' does not match '" << a.branchName() << "'.\n";
      // Need not compare components of branch name individually.
      // (a.friendlyClassName() != b.friendlyClassName())
      // (a.moduleLabel() != b.moduleLabel())
      // (a.productInstanceName() != b.productInstanceName())
      // (a.processName() != b.processName())
    }
    if (a.branchType() != b.branchType()) {
      differences << "Branch '" << b.branchName() << "' is a(n) '" << b.branchType() << "' branch\n";
      differences << "    in file '" << fileName << "', but a(n) '" << a.branchType()
                  << "' branch in previous files.\n";
    }
    if (a.branchID() != b.branchID()) {
      differences << "Branch '" << b.branchName() << "' has a branch ID of '" << b.branchID() << "'\n";
      differences << "    in file '" << fileName << "', but '" << a.branchID() << "' in previous files.\n";
    }
    if (a.fullClassName() != b.fullClassName()) {
      differences << "Products on branch '" << b.branchName() << "' have type '" << b.fullClassName() << "'\n";
      differences << "    in file '" << fileName << "', but '" << a.fullClassName() << "' in previous files.\n";
    }
    if (!b.dropped() && a.dropped()) {
      differences << "Branch '" << a.branchName() << "' was dropped in the first input file but is present in '"
                  << fileName << "'.\n";
    }
    return differences.str();
  }
}  // namespace edm