CTPPSLHCInfoESSource

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
// Original Author:  Jan Kašpar

#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/Framework/interface/SourceFactory.h"
#include "FWCore/Framework/interface/ESHandle.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Framework/interface/ESProducer.h"
#include "FWCore/Framework/interface/EventSetupRecordIntervalFinder.h"
#include "FWCore/Framework/interface/ESProducts.h"

#include "CondFormats/RunInfo/interface/LHCInfo.h"
#include "CondFormats/DataRecord/interface/LHCInfoRcd.h"

//----------------------------------------------------------------------------------------------------

/**
 * \brief Provides LHCInfo data necessary for CTPPS reconstruction (and direct simulation).
 **/
class CTPPSLHCInfoESSource : public edm::ESProducer, public edm::EventSetupRecordIntervalFinder {
public:
  CTPPSLHCInfoESSource(const edm::ParameterSet &);
  edm::ESProducts<std::unique_ptr<LHCInfo>> produce(const LHCInfoRcd &);
  static void fillDescriptions(edm::ConfigurationDescriptions &);

private:
  void setIntervalFor(const edm::eventsetup::EventSetupRecordKey &,
                      const edm::IOVSyncValue &,
                      edm::ValidityInterval &) override;

  std::string m_label;

  edm::EventRange m_validityRange;
  double m_beamEnergy;
  double m_betaStar;
  double m_xangle;

  bool m_insideValidityRange;
};

//----------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------

CTPPSLHCInfoESSource::CTPPSLHCInfoESSource(const edm::ParameterSet &conf)
    : m_label(conf.getParameter<std::string>("label")),
      m_validityRange(conf.getParameter<edm::EventRange>("validityRange")),
      m_beamEnergy(conf.getParameter<double>("beamEnergy")),
      m_betaStar(conf.getParameter<double>("betaStar")),
      m_xangle(conf.getParameter<double>("xangle")),
      m_insideValidityRange(false) {
  setWhatProduced(this, m_label);
  findingRecord<LHCInfoRcd>();
}

//----------------------------------------------------------------------------------------------------

void CTPPSLHCInfoESSource::fillDescriptions(edm::ConfigurationDescriptions &descriptions) {
  edm::ParameterSetDescription desc;

  desc.add<std::string>("label", "")->setComment("label of the LHCInfo record");

  desc.add<edm::EventRange>("validityRange", edm::EventRange())->setComment("interval of validity");

  desc.add<double>("beamEnergy", 0.)->setComment("beam energy");
  desc.add<double>("betaStar", 0.)->setComment("beta*");
  desc.add<double>("xangle", 0.)->setComment("crossing angle");

  descriptions.add("ctppsLHCInfoESSource", desc);
}

//----------------------------------------------------------------------------------------------------

void CTPPSLHCInfoESSource::setIntervalFor(const edm::eventsetup::EventSetupRecordKey &key,
                                          const edm::IOVSyncValue &iosv,
                                          edm::ValidityInterval &oValidity) {
  if (edm::contains(m_validityRange, iosv.eventID())) {
    m_insideValidityRange = true;
    oValidity = edm::ValidityInterval(edm::IOVSyncValue(m_validityRange.startEventID()),
                                      edm::IOVSyncValue(m_validityRange.endEventID()));
  } else {
    m_insideValidityRange = false;

    if (iosv.eventID() < m_validityRange.startEventID()) {
      edm::RunNumber_t run = m_validityRange.startEventID().run();
      edm::LuminosityBlockNumber_t lb = m_validityRange.startEventID().luminosityBlock();
      edm::EventID endEvent =
          (lb > 1) ? edm::EventID(run, lb - 1, 0) : edm::EventID(run - 1, edm::EventID::maxLuminosityBlockNumber(), 0);

      oValidity = edm::ValidityInterval(edm::IOVSyncValue::beginOfTime(), edm::IOVSyncValue(endEvent));
    } else {
      edm::RunNumber_t run = m_validityRange.startEventID().run();
      edm::LuminosityBlockNumber_t lb = m_validityRange.startEventID().luminosityBlock();
      edm::EventID beginEvent = (lb < edm::EventID::maxLuminosityBlockNumber() - 1) ? edm::EventID(run, lb + 1, 0)
                                                                                    : edm::EventID(run + 1, 0, 0);

      oValidity = edm::ValidityInterval(edm::IOVSyncValue(beginEvent), edm::IOVSyncValue::endOfTime());
    }
  }
}

//----------------------------------------------------------------------------------------------------

edm::ESProducts<std::unique_ptr<LHCInfo>> CTPPSLHCInfoESSource::produce(const LHCInfoRcd &) {
  auto output = std::make_unique<LHCInfo>();

  if (m_insideValidityRange) {
    output->setEnergy(m_beamEnergy);
    output->setBetaStar(m_betaStar);
    output->setCrossingAngle(m_xangle);
  } else {
    output->setEnergy(0.);
    output->setBetaStar(0.);
    output->setCrossingAngle(0.);
  }

  return edm::es::products(std::move(output));
}

//----------------------------------------------------------------------------------------------------

DEFINE_FWK_EVENTSETUP_SOURCE(CTPPSLHCInfoESSource);