ProvenanceCheckerOutputModule

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
// -*- C++ -*-
//
// Package:     Modules
// Class  :     ProvenanceCheckerOutputModule
//
// Implementation:
//     Checks the consistency of provenance stored in the framework
//
// Original Author:  Chris Jones
//         Created:  Thu Sep 11 19:24:13 EDT 2008
//

// system include files
#include "FWCore/Framework/interface/one/OutputModule.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/Framework/interface/EventForOutput.h"
#include "FWCore/Utilities/interface/Exception.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "DataFormats/Provenance/interface/ProductRegistry.h"
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
#include "FWCore/ServiceRegistry/interface/Service.h"

// user include files

namespace edm {

  class ModuleCallingContext;
  class ParameterSet;

  class ProvenanceCheckerOutputModule : public one::OutputModule<> {
  public:
    // We do not take ownership of passed stream.
    explicit ProvenanceCheckerOutputModule(ParameterSet const& pset);
    ~ProvenanceCheckerOutputModule() override;
    static void fillDescriptions(ConfigurationDescriptions& descriptions);

  private:
    void write(EventForOutput const& e) override;
    void writeLuminosityBlock(LuminosityBlockForOutput const&) override {}
    void writeRun(RunForOutput const&) override {}
  };

  //
  // constants, enums and typedefs
  //

  //
  // static data member definitions
  //

  //
  // constructors and destructor
  //
  ProvenanceCheckerOutputModule::ProvenanceCheckerOutputModule(ParameterSet const& pset)
      : one::OutputModuleBase(pset), one::OutputModule<>(pset) {}

  // ProvenanceCheckerOutputModule::ProvenanceCheckerOutputModule(ProvenanceCheckerOutputModule const& rhs)
  // {
  //    // do actual copying here;
  // }

  ProvenanceCheckerOutputModule::~ProvenanceCheckerOutputModule() {}

  //
  // assignment operators
  //
  // ProvenanceCheckerOutputModule const& ProvenanceCheckerOutputModule::operator=(ProvenanceCheckerOutputModule const& rhs)
  // {
  //   //An exception safe implementation is
  //   ProvenanceCheckerOutputModule temp(rhs);
  //   swap(rhs);
  //
  //   return *this;
  // }

  namespace {
    void markAncestors(EventForOutput const& e,
                       ProductProvenance const& iInfo,
                       std::map<BranchID, bool>& oMap,
                       std::set<BranchID>& oMapperMissing) {
      for (BranchID const id : iInfo.parentage().parents()) {
        //Don't look for parents if we've previously looked at the parents
        if (oMap.find(id) == oMap.end()) {
          //use side effect of calling operator[] which is if the item isn't there it will add it as 'false'
          oMap[id];
          ProductProvenance const* pInfo = e.getProvenance(id).productProvenance();
          if (pInfo) {
            markAncestors(e, *pInfo, oMap, oMapperMissing);
          } else {
            oMapperMissing.insert(id);
          }
        }
      }
    }
  }  // namespace

  void ProvenanceCheckerOutputModule::write(EventForOutput const& e) {
    //check ProductProvenance's parents to see if they are in the ProductProvenance list

    std::map<BranchID, bool> seenParentInPrincipal;
    std::set<BranchID> missingFromMapper;
    std::set<BranchID> missingProductProvenance;

    std::map<BranchID, const ProductDescription*> idToProductDescriptions;
    for (auto const& product : keptProducts()[InEvent]) {
      ProductDescription const* productDescription = product.first;
      BranchID branchID = productDescription->branchID();
      idToProductDescriptions[branchID] = productDescription;
      TypeID const& tid(productDescription->unwrappedTypeID());
      EDGetToken const& token = product.second;
      BasicHandle bh = e.getByToken(token, tid);
      bool cannotFindProductProvenance = false;
      if (!(bh.provenance() and bh.provenance()->productProvenance())) {
        missingProductProvenance.insert(branchID);
        cannotFindProductProvenance = true;
      }
      ProductProvenance const* pInfo = e.getProvenance(branchID).productProvenance();
      if (!pInfo) {
        missingFromMapper.insert(branchID);
        continue;
      }
      if (cannotFindProductProvenance) {
        continue;
      }
      markAncestors(e, *(bh.provenance()->productProvenance()), seenParentInPrincipal, missingFromMapper);
      seenParentInPrincipal[branchID] = true;
    }

    //Determine what BranchIDs are in the product registry
    std::set<BranchID> branchesInReg;
    for (auto const& product : e.productRegistry().productList()) {
      branchesInReg.insert(product.second.branchID());
      idToProductDescriptions[product.second.branchID()] = &product.second;
    }

    std::set<BranchID> missingFromReg;
    for (auto const& item : seenParentInPrincipal) {
      if (branchesInReg.find(item.first) == branchesInReg.end()) {
        missingFromReg.insert(item.first);
      }
    }

    if (!missingFromMapper.empty()) {
      LogError("ProvenanceChecker") << "Missing the following BranchIDs from ProductProvenanceRetriever\n";
      for (std::set<BranchID>::iterator it = missingFromMapper.begin(), itEnd = missingFromMapper.end(); it != itEnd;
           ++it) {
        LogProblem("ProvenanceChecker") << *it << " " << *(idToProductDescriptions[*it]);
      }
    }

    if (!missingProductProvenance.empty()) {
      LogError("ProvenanceChecker") << "The ProductResolvers for the following BranchIDs have no ProductProvenance\n";
      for (std::set<BranchID>::iterator it = missingProductProvenance.begin(), itEnd = missingProductProvenance.end();
           it != itEnd;
           ++it) {
        LogProblem("ProvenanceChecker") << *it << " " << *(idToProductDescriptions[*it]);
      }
    }

    if (!missingFromReg.empty()) {
      LogError("ProvenanceChecker") << "Missing the following BranchIDs from ProductRegistry\n";
      for (auto const& item : missingFromReg) {
        LogProblem("ProvenanceChecker") << item << " " << *(idToProductDescriptions[item]);
      }
    }

    if (!missingFromMapper.empty() || !missingProductProvenance.empty() || !missingFromReg.empty()) {
      throw cms::Exception("ProvenanceError")
          << (!missingFromMapper.empty() ? "Having missing ancestors from ProductProvenanceRetriever.\n" : "")
          << (!missingProductProvenance.empty() ? " Have missing ProductProvenance's from ProductResolver in Event.\n"
                                                : "")
          << (!missingFromReg.empty() ? " Have missing info from ProductRegistry.\n" : "");
    }

    //check consistency with all Intervals
    if (e.productRegistry().cacheIdentifier() != e.getRun().productRegistry().cacheIdentifier()) {
      throw cms::Exception("ProvenanceError")
          << "The registry cache id for Event ( " << e.productRegistry().cacheIdentifier()
          << " ) does not match the one for Run ( " << e.getRun().productRegistry().cacheIdentifier() << " )";
    }
    if (e.productRegistry().cacheIdentifier() != e.getLuminosityBlock().productRegistry().cacheIdentifier()) {
      throw cms::Exception("ProvenanceError")
          << "The registry cache id for Event ( " << e.productRegistry().cacheIdentifier()
          << " ) does not match the one for LuminosityBlock ( "
          << e.getLuminosityBlock().productRegistry().cacheIdentifier() << " )";
    }
  }

  //
  // const member functions
  //

  //
  // static member functions
  //
  void ProvenanceCheckerOutputModule::fillDescriptions(ConfigurationDescriptions& descriptions) {
    ParameterSetDescription desc;
    one::OutputModule<>::fillDescription(desc);
    descriptions.add("provenanceChecker", desc);
  }
}  // namespace edm
using edm::ProvenanceCheckerOutputModule;
DEFINE_FWK_MODULE(ProvenanceCheckerOutputModule);