GetProductCheckerOutputModule

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
// -*- C++ -*-
//
// Package:     Modules
// Class  :     GetProductCheckerOutputModule
//
// Implementation:
//     <Notes on implementation>
//
// Original Author:
//         Created:  Wed Oct  7 14:41:26 CDT 2009
//

// system include files
#include <string>
#include <sstream>

// user include files
#include "FWCore/Framework/interface/one/OutputModule.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/Framework/interface/EventForOutput.h"
#include "FWCore/Framework/interface/LuminosityBlockForOutput.h"
#include "FWCore/Framework/interface/RunForOutput.h"
#include "FWCore/Utilities/interface/Exception.h"
#include "FWCore/Utilities/interface/ProductKindOfType.h"
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"

namespace edm {
  class ModuleCallingContext;
  class ParameterSet;

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

  private:
    void write(EventForOutput const& e) override;
    void writeLuminosityBlock(LuminosityBlockForOutput const&) override;
    void writeRun(RunForOutput const&) override;
    const std::vector<std::string> crosscheck_;
    const bool verbose_;
  };

  //
  // constants, enums and typedefs
  //

  //
  // static data member definitions
  //

  //
  // constructors and destructor
  //
  GetProductCheckerOutputModule::GetProductCheckerOutputModule(ParameterSet const& iPSet)
      : one::OutputModuleBase(iPSet),
        one::OutputModule<>(iPSet),
        crosscheck_(iPSet.getUntrackedParameter<std::vector<std::string>>("crosscheck")),
        verbose_(iPSet.getUntrackedParameter<bool>("verbose")) {}

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

  GetProductCheckerOutputModule::~GetProductCheckerOutputModule() {}

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

  //
  // member functions
  //
  template <typename T>
  static void check(T const& p, std::string const& id, SelectedProducts const& iProducts, bool iVerbose) {
    for (auto const& product : iProducts) {
      ProductDescription const* productDescription = product.first;
      TypeID const& tid = productDescription->unwrappedTypeID();
      EDGetToken const& token = product.second;
      BasicHandle bh = p.getByToken(token, tid);
      if (iVerbose) {
        if (bh.isValid()) {
          edm::LogInfo("FoundProduct") << "found " << productDescription->moduleLabel() << " '"
                                       << productDescription->productInstanceName() << "' "
                                       << productDescription->processName();
        } else {
          edm::LogInfo("DidNotFindProduct")
              << "did not find " << productDescription->moduleLabel() << " '"
              << productDescription->productInstanceName() << "' " << productDescription->processName();
        }
      }
      if (nullptr != bh.provenance() &&
          bh.provenance()->productDescription().branchID() != productDescription->branchID()) {
        throw cms::Exception("BranchIDMissMatch")
            << "While processing " << id << " getByToken request for " << productDescription->moduleLabel() << " '"
            << productDescription->productInstanceName() << "' " << productDescription->processName()
            << "\n should have returned BranchID " << productDescription->branchID() << " but returned BranchID "
            << bh.provenance()->productDescription().branchID() << "\n";
      }
    }
  }
  namespace {
    std::string canonicalName(std::string const& iOriginal) {
      if (iOriginal.empty()) {
        return iOriginal;
      }
      if (iOriginal.back() == '.') {
        return iOriginal.substr(0, iOriginal.size() - 1);
      }
      return iOriginal;
    }
  }  // namespace
  void GetProductCheckerOutputModule::write(EventForOutput const& e) {
    std::ostringstream str;
    str << e.id();
    check(e, str.str(), keptProducts()[InEvent], verbose_);
    if (not crosscheck_.empty()) {
      std::set<std::string> expectedProducts(crosscheck_.begin(), crosscheck_.end());
      for (auto const& kp : keptProducts()[InEvent]) {
        auto bn = canonicalName(kp.first->branchName());
        auto found = expectedProducts.find(bn);
        if (found == expectedProducts.end()) {
          throw cms::Exception("CrosscheckFailed") << "unexpected kept product " << bn;
        }
        expectedProducts.erase(bn);
      }
      if (not expectedProducts.empty()) {
        cms::Exception e("CrosscheckFailed");
        e << "Did not find the expected products:\n";
        for (auto const& p : expectedProducts) {
          e << p << "\n";
        }
        throw e;
      }
    }
  }
  void GetProductCheckerOutputModule::writeLuminosityBlock(LuminosityBlockForOutput const& l) {
    std::ostringstream str;
    str << l.id();
    check(l, str.str(), keptProducts()[InLumi], verbose_);
  }
  void GetProductCheckerOutputModule::writeRun(RunForOutput const& r) {
    std::ostringstream str;
    str << r.id();
    check(r, str.str(), keptProducts()[InRun], verbose_);
  }

  //
  // const member functions
  //

  //
  // static member functions
  //

  void GetProductCheckerOutputModule::fillDescriptions(ConfigurationDescriptions& descriptions) {
    ParameterSetDescription desc;
    one::OutputModule<>::fillDescription(desc);
    desc.addUntracked<std::vector<std::string>>("crosscheck", {})
        ->setComment("Branch names that should be in the event. If empty no check done.");
    desc.addUntracked<bool>("verbose", false);
    descriptions.add("productChecker", desc);
  }
}  // namespace edm

using edm::GetProductCheckerOutputModule;
DEFINE_FWK_MODULE(GetProductCheckerOutputModule);