Cache

RunLumiESAnalyzer

UnsafeCache

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
// -*- C++ -*-
//
// Package:    FWCore/Integration
// Class:      RunLumiESAnalyzer
//
/**\class edmtest::RunLumiESAnalyzer

 Description: Used in tests of the EventSetup system,
 particularly testing its support of Run and Lumi
 transitions.
*/
// Original Author:  W. David Dagenhart
//         Created:  18 April 2019

#include "DataFormats/TestObjects/interface/ToyProducts.h"
#include "FWCore/Framework/interface/global/EDAnalyzer.h"
#include "FWCore/Framework/interface/ESHandle.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/Framework/interface/Run.h"
#include "FWCore/Integration/interface/ESTestRecords.h"
#include "FWCore/Integration/interface/IOVTestInfo.h"
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
#include "FWCore/Utilities/interface/EDGetToken.h"
#include "FWCore/Utilities/interface/ESGetToken.h"
#include "FWCore/Utilities/interface/ESInputTag.h"
#include "FWCore/Utilities/interface/Exception.h"
#include "FWCore/Utilities/interface/InputTag.h"

#include <memory>

namespace {
  struct Cache {
    Cache() : value(0) {}
    //Using mutable since we want to update the value.
    mutable std::atomic<unsigned int> value;
  };

  struct UnsafeCache {
    UnsafeCache() : value(0) {}
    unsigned int value;
  };
}  //end anonymous namespace

namespace edmtest {

  class RunLumiESAnalyzer : public edm::global::EDAnalyzer<edm::StreamCache<UnsafeCache>,
                                                           edm::RunCache<Cache>,
                                                           edm::LuminosityBlockCache<Cache>> {
  public:
    explicit RunLumiESAnalyzer(edm::ParameterSet const&);

    std::unique_ptr<UnsafeCache> beginStream(edm::StreamID) const override;
    void streamBeginRun(edm::StreamID, edm::Run const&, edm::EventSetup const&) const override;
    void streamBeginLuminosityBlock(edm::StreamID, edm::LuminosityBlock const&, edm::EventSetup const&) const override;
    void streamEndLuminosityBlock(edm::StreamID, edm::LuminosityBlock const&, edm::EventSetup const&) const override;
    void streamEndRun(edm::StreamID, edm::Run const&, edm::EventSetup const&) const override;

    std::shared_ptr<Cache> globalBeginRun(edm::Run const&, edm::EventSetup const&) const override;
    void globalEndRun(edm::Run const& iRun, edm::EventSetup const&) const override;
    std::shared_ptr<Cache> globalBeginLuminosityBlock(edm::LuminosityBlock const&,
                                                      edm::EventSetup const&) const override;
    void globalEndLuminosityBlock(edm::LuminosityBlock const& iLB, edm::EventSetup const&) const override;

    void analyze(edm::StreamID, edm::Event const&, edm::EventSetup const&) const override;

    static void fillDescriptions(edm::ConfigurationDescriptions&);

  private:
    void checkIOVInfo(edm::EventSetup const& eventSetup,
                      unsigned int run,
                      unsigned int lumiNumber,
                      edm::ESHandle<IOVTestInfo> const& iovTestInfo,
                      const char* functionName) const;

    edm::ESGetToken<IOVTestInfo, ESTestRecordC> const esToken_;
    edm::ESGetToken<IOVTestInfo, ESTestRecordC> const esTokenNonEmptyLabel_;
    edm::ESGetToken<IOVTestInfo, ESTestRecordC> const tokenBeginRun_;
    edm::ESGetToken<IOVTestInfo, ESTestRecordC> const tokenBeginLumi_;
    edm::ESGetToken<IOVTestInfo, ESTestRecordC> const tokenEndLumi_;
    edm::ESGetToken<IOVTestInfo, ESTestRecordC> const tokenEndRun_;

    bool checkDataProductContents_;
    bool getIntProduct_;

    edm::EDGetTokenT<IntProduct> token_;
  };

  RunLumiESAnalyzer::RunLumiESAnalyzer(edm::ParameterSet const& pset)
      : esToken_{esConsumes(pset.getParameter<edm::ESInputTag>("esInputTag"))},
        esTokenNonEmptyLabel_{esConsumes(edm::ESInputTag("", "nonEmptyLabel"))},
        tokenBeginRun_{esConsumes<edm::Transition::BeginRun>(pset.getParameter<edm::ESInputTag>("esInputTag"))},
        tokenBeginLumi_{
            esConsumes<edm::Transition::BeginLuminosityBlock>(pset.getParameter<edm::ESInputTag>("esInputTag"))},
        tokenEndLumi_{
            esConsumes<edm::Transition::EndLuminosityBlock>(pset.getParameter<edm::ESInputTag>("esInputTag"))},
        tokenEndRun_{esConsumes<edm::Transition::EndRun>(pset.getParameter<edm::ESInputTag>("esInputTag"))},
        checkDataProductContents_(pset.getParameter<bool>("checkDataProductContents")),
        getIntProduct_(pset.getParameter<bool>("getIntProduct")) {
    if (getIntProduct_) {
      token_ = consumes<IntProduct>(edm::InputTag("intProducer", ""));
    }
  }

  std::unique_ptr<UnsafeCache> RunLumiESAnalyzer::beginStream(edm::StreamID iID) const {
    return std::make_unique<UnsafeCache>();
  }

  void RunLumiESAnalyzer::checkIOVInfo(edm::EventSetup const& eventSetup,
                                       unsigned int run,
                                       unsigned int lumiNumber,
                                       edm::ESHandle<IOVTestInfo> const& iovTestInfo,
                                       const char* functionName) const {
    ESTestRecordC recordC = eventSetup.get<ESTestRecordC>();
    edm::ValidityInterval iov = recordC.validityInterval();

    if (checkDataProductContents_) {
      if (iovTestInfo->iovStartRun_ != run || iovTestInfo->iovEndRun_ != run ||
          iovTestInfo->iovStartLumi_ != lumiNumber || iovTestInfo->iovEndLumi_ != lumiNumber) {
        throw cms::Exception("TestFailure")
            << functionName << ": values read from EventSetup do not agree with auxiliary";
      }
    }

    if (iov.first().eventID().run() != run || iov.last().eventID().run() != run ||
        iov.first().luminosityBlockNumber() != lumiNumber || iov.last().luminosityBlockNumber() != lumiNumber) {
      throw cms::Exception("TestFailure") << functionName << ": values from EventSetup IOV do not agree with auxiliary";
    }
  }

  void RunLumiESAnalyzer::streamBeginRun(edm::StreamID, edm::Run const& iRun, edm::EventSetup const& eventSetup) const {
    auto run = iRun.runAuxiliary().run();
    unsigned int lumiNumber = 0;
    edm::ESHandle<IOVTestInfo> iovTestInfo = eventSetup.getHandle(tokenBeginRun_);
    checkIOVInfo(eventSetup, run, lumiNumber, iovTestInfo, "RunLumiESAnalyzer::streamBeginRun");
  }

  void RunLumiESAnalyzer::streamBeginLuminosityBlock(edm::StreamID,
                                                     edm::LuminosityBlock const& iLumi,
                                                     edm::EventSetup const& eventSetup) const {
    auto run = iLumi.luminosityBlockAuxiliary().run();
    unsigned int lumiNumber = iLumi.luminosityBlockAuxiliary().luminosityBlock();
    edm::ESHandle<IOVTestInfo> iovTestInfo = eventSetup.getHandle(tokenBeginLumi_);
    checkIOVInfo(eventSetup, run, lumiNumber, iovTestInfo, "RunLumiESAnalyzer::streamBeginLuminosityBlock");
  }

  void RunLumiESAnalyzer::streamEndLuminosityBlock(edm::StreamID,
                                                   edm::LuminosityBlock const& iLumi,
                                                   edm::EventSetup const& eventSetup) const {
    auto run = iLumi.luminosityBlockAuxiliary().run();
    unsigned int lumiNumber = iLumi.luminosityBlockAuxiliary().luminosityBlock();
    edm::ESHandle<IOVTestInfo> iovTestInfo = eventSetup.getHandle(tokenEndLumi_);
    checkIOVInfo(eventSetup, run, lumiNumber, iovTestInfo, "RunLumiESAnalyzer::streamEndLuminosityBlock");
  }

  void RunLumiESAnalyzer::streamEndRun(edm::StreamID, edm::Run const& iRun, edm::EventSetup const& eventSetup) const {
    auto run = iRun.runAuxiliary().run();
    unsigned int lumiNumber = 4294967295;
    edm::ESHandle<IOVTestInfo> iovTestInfo = eventSetup.getHandle(tokenEndRun_);
    checkIOVInfo(eventSetup, run, lumiNumber, iovTestInfo, "RunLumiESAnalyzer::streamEndRun");
  }

  std::shared_ptr<Cache> RunLumiESAnalyzer::globalBeginRun(edm::Run const& iRun,
                                                           edm::EventSetup const& eventSetup) const {
    auto run = iRun.runAuxiliary().run();
    unsigned int lumiNumber = 0;
    edm::ESHandle<IOVTestInfo> iovTestInfo = eventSetup.getHandle(tokenBeginRun_);
    checkIOVInfo(eventSetup, run, lumiNumber, iovTestInfo, "RunLumiESAnalyzer::globalBeginRun");
    return std::make_shared<Cache>();
  }

  void RunLumiESAnalyzer::globalEndRun(edm::Run const& iRun, edm::EventSetup const& eventSetup) const {
    auto run = iRun.runAuxiliary().run();
    unsigned int lumiNumber = 4294967295;
    edm::ESHandle<IOVTestInfo> iovTestInfo = eventSetup.getHandle(tokenEndRun_);
    checkIOVInfo(eventSetup, run, lumiNumber, iovTestInfo, "RunLumiESAnalyzer::globalEndRun");
  }

  std::shared_ptr<Cache> RunLumiESAnalyzer::globalBeginLuminosityBlock(edm::LuminosityBlock const& iLumi,
                                                                       edm::EventSetup const& eventSetup) const {
    auto run = iLumi.luminosityBlockAuxiliary().run();
    unsigned int lumiNumber = iLumi.luminosityBlockAuxiliary().luminosityBlock();
    edm::ESHandle<IOVTestInfo> iovTestInfo = eventSetup.getHandle(tokenBeginLumi_);
    checkIOVInfo(eventSetup, run, lumiNumber, iovTestInfo, "RunLumiESAnalyzer::globalBeginLuminosityBlock");
    return std::make_shared<Cache>();
  }

  void RunLumiESAnalyzer::globalEndLuminosityBlock(edm::LuminosityBlock const& iLumi,
                                                   edm::EventSetup const& eventSetup) const {
    auto run = iLumi.luminosityBlockAuxiliary().run();
    unsigned int lumiNumber = iLumi.luminosityBlockAuxiliary().luminosityBlock();
    edm::ESHandle<IOVTestInfo> iovTestInfo = eventSetup.getHandle(tokenEndLumi_);
    checkIOVInfo(eventSetup, run, lumiNumber, iovTestInfo, "RunLumiESAnalyzer::globalEndLuminosityBlock");
  }

  void RunLumiESAnalyzer::analyze(edm::StreamID, edm::Event const& event, edm::EventSetup const& eventSetup) const {
    auto run = event.eventAuxiliary().run();
    auto lumiNumber = event.eventAuxiliary().luminosityBlock();
    edm::ESHandle<IOVTestInfo> iovTestInfo = eventSetup.getHandle(esToken_);
    checkIOVInfo(eventSetup, run, lumiNumber, iovTestInfo, "RunLumiESAnalyzer::analyzer");

    {
      edm::ESHandle<IOVTestInfo> iovTestInfo = eventSetup.getHandle(esTokenNonEmptyLabel_);
      checkIOVInfo(eventSetup, run, lumiNumber, iovTestInfo, "RunLumiESAnalyzer::analyzer");
    }
    if (getIntProduct_) {
      event.get(token_);
    }
  }

  void RunLumiESAnalyzer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
    edm::ParameterSetDescription desc;
    desc.add<edm::ESInputTag>("esInputTag", edm::ESInputTag());
    desc.add<bool>("checkDataProductContents", true);
    desc.add<bool>("getIntProduct", false);
    descriptions.addDefault(desc);
  }
}  // namespace edmtest
using namespace edmtest;
DEFINE_FWK_MODULE(RunLumiESAnalyzer);