EventSetupCacheIdentifierChecker

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
// -*- C++ -*-
//
// Package:    EventSetupCacheIdentifierChecker
// Class:      EventSetupCacheIdentifierChecker
//
/**\class EventSetupCacheIdentifierChecker EventSetupCacheIdentifierChecker.cc FWCore/Modules/src/EventSetupCacheIdentifierChecker.cc

 Description: [one line class summary]

 Implementation:
     [Notes on implementation]
*/
//
// Original Author:  Chris Jones
//         Created:  Wed May 30 14:42:16 CDT 2012
//
//

// system include files
#include <memory>
#include <map>
#include <vector>

// user include files
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/one/EDAnalyzer.h"

#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/MakerMacros.h"

#include "FWCore/ParameterSet/interface/ParameterSet.h"

#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/Framework/interface/EventSetupRecord.h"
#include "FWCore/Utilities/interface/Exception.h"

#include "FWCore/MessageLogger/interface/MessageLogger.h"
//
// class declaration
//

namespace edm {
  class EventSetupCacheIdentifierChecker
      : public edm::one::EDAnalyzer<edm::one::WatchRuns, edm::one::WatchLuminosityBlocks> {
  public:
    explicit EventSetupCacheIdentifierChecker(const edm::ParameterSet&);
    ~EventSetupCacheIdentifierChecker() override;

    static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);

  private:
    //virtual void beginJob() ;
    void analyze(const edm::Event&, const edm::EventSetup&) override;
    //virtual void endJob() ;

    void beginRun(edm::Run const&, edm::EventSetup const&) override;
    void endRun(edm::Run const&, edm::EventSetup const&) override {}
    void beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
    void endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override {}

    void check(edm::EventSetup const&);
    void initialize();
    // ----------member data ---------------------------
    ParameterSet m_pset;
    std::map<eventsetup::EventSetupRecordKey, std::vector<unsigned int> > m_recordKeysToExpectedCacheIdentifiers;
    unsigned int m_index;
  };
}  // namespace edm
//
// constants, enums and typedefs
//
using namespace edm;

//
// static data member definitions
//

//
// constructors and destructor
//
EventSetupCacheIdentifierChecker::EventSetupCacheIdentifierChecker(const edm::ParameterSet& iConfig)
    : m_pset(iConfig), m_index(0) {
  //now do what ever initialization is needed
}

EventSetupCacheIdentifierChecker::~EventSetupCacheIdentifierChecker() {
  // do anything here that needs to be done at desctruction time
  // (e.g. close files, deallocate resources etc.)
}

//
// member functions
//

// ------------ method called for each event  ------------
void EventSetupCacheIdentifierChecker::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
  check(iSetup);
}

// ------------ method called once each job just before starting event loop  ------------
//void
//EventSetupCacheIdentifierChecker::beginJob()
//{
//}

// ------------ method called once each job just after ending the event loop  ------------
//void
//EventSetupCacheIdentifierChecker::endJob()
//{
//}

// ------------ method called when starting to processes a run  ------------
void EventSetupCacheIdentifierChecker::beginRun(edm::Run const&, edm::EventSetup const& iSetup) { check(iSetup); }

// ------------ method called when ending the processing of a run  ------------
//void
//EventSetupCacheIdentifierChecker::endRun(edm::Run const&, edm::EventSetup const&)
//{
//}

// ------------ method called when starting to processes a luminosity block  ------------
void EventSetupCacheIdentifierChecker::beginLuminosityBlock(edm::LuminosityBlock const&,
                                                            edm::EventSetup const& iSetup) {
  check(iSetup);
}

// ------------ method called when ending the processing of a luminosity block  ------------
//void
//EventSetupCacheIdentifierChecker::endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&)
//{
//}

void EventSetupCacheIdentifierChecker::check(edm::EventSetup const& iSetup) {
  if (m_recordKeysToExpectedCacheIdentifiers.empty()) {
    initialize();
  }
  using namespace edm::eventsetup;

  for (auto it = m_recordKeysToExpectedCacheIdentifiers.begin(), itEnd = m_recordKeysToExpectedCacheIdentifiers.end();
       it != itEnd;
       ++it) {
    auto pRecord = iSetup.find(it->first);
    if (not pRecord) {
      edm::LogWarning("RecordNotInIOV") << "The EventSetup Record '" << it->first.name()
                                        << "' is not available for this IOV.";
    }
    if (it->second.size() <= m_index) {
      throw cms::Exception("TooFewCacheIDs")
          << "The vector of cacheIdentifiers for the record " << it->first.name() << " is too short";
    }
    if (pRecord && pRecord->cacheIdentifier() != it->second[m_index]) {
      throw cms::Exception("IncorrectCacheID")
          << "The Record " << it->first.name() << " was supposed to have cacheIdentifier: " << it->second[m_index]
          << " but instead has " << pRecord->cacheIdentifier();
    }
  }
  ++m_index;
}

void EventSetupCacheIdentifierChecker::initialize() {
  std::vector<std::string> recordNames{m_pset.getParameterNamesForType<std::vector<unsigned int> >(false)};

  for (auto const& name : recordNames) {
    eventsetup::EventSetupRecordKey recordKey(eventsetup::EventSetupRecordKey::TypeTag::findType(name));
    if (recordKey.type() == eventsetup::EventSetupRecordKey::TypeTag()) {
      //record not found
      edm::LogWarning("DataGetter") << "Record \"" << name << "\" does not exist " << std::endl;

      continue;
    }

    m_recordKeysToExpectedCacheIdentifiers.insert(
        std::make_pair(recordKey, m_pset.getUntrackedParameter<std::vector<unsigned int> >(name)));
  }
}

// ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
void EventSetupCacheIdentifierChecker::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
  //The following says we do not know what parameters are allowed so do no validation
  // Please change this to state exactly what you do use, even if it is no parameters
  edm::ParameterSetDescription desc;
  desc.addWildcardUntracked<std::vector<unsigned int> >("*")->setComment(
      "The label is the name of an EventSetup Record while the vector contains the expected cacheIdentifier values for "
      "each beginRun, beginLuminosityBlock and event transition");
  descriptions.addDefault(desc);
}

//define this as a plug-in
DEFINE_FWK_MODULE(EventSetupCacheIdentifierChecker);