ConfigurableAPVCyclePhaseProducer

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
// -*- C++ -*-
//
// Package:    SiStripTools
// Class:      ConfigurableAPVCyclePhaseProducer
//
/**\class ConfigurableAPVCyclePhaseProducer ConfigurableAPVCyclePhaseProducer.cc DPGAnalysis/SiStripTools/plugins/ConfigurableAPVCyclePhaseProducer.cc

 Description: EDproducer for APVCyclePhaseCollection which uses the configuration file to assign a phase to the run

 Implementation:
     <Notes on implementation>
*/
//
// Original Author:  Andrea Venturi
//         Created:  Mon Jan 12 09:05:45 CET 2009
//
//

// system include files
#include <memory>

// user include files
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/global/EDProducer.h"

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

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

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

#include <map>
#include <string>

#include "DPGAnalysis/SiStripTools/interface/APVCyclePhaseCollection.h"

//
// class decleration
//

class ConfigurableAPVCyclePhaseProducer : public edm::global::EDProducer<edm::RunCache<APVCyclePhaseCollection>> {
public:
  explicit ConfigurableAPVCyclePhaseProducer(const edm::ParameterSet&);
  ~ConfigurableAPVCyclePhaseProducer() override;

private:
  std::shared_ptr<APVCyclePhaseCollection> globalBeginRun(const edm::Run&, const edm::EventSetup&) const override;
  void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
  void globalEndRun(edm::Run const&, edm::EventSetup const&) const override {}

  // ----------member data ---------------------------

  const std::vector<std::string> _defpartnames;
  const std::vector<int> _defphases;

  std::map<int, std::vector<std::string>> _runpartnames;
  std::map<int, std::vector<int>> _runphases;
};

//
// constants, enums and typedefs
//

//
// static data member definitions
//

//
// constructors and destructor
//
ConfigurableAPVCyclePhaseProducer::ConfigurableAPVCyclePhaseProducer(const edm::ParameterSet& iConfig)
    : _defpartnames(iConfig.getParameter<std::vector<std::string>>("defaultPartitionNames")),
      _defphases(iConfig.getParameter<std::vector<int>>("defaultPhases")) {
  produces<APVCyclePhaseCollection, edm::InEvent>();

  //now do what ever other initialization is needed

  if (_defphases.size() < _defpartnames.size()) {
    // throw exception
    throw cms::Exception("InvalidAPVCyclePhases")
        << " Inconsistent default phases/partitions vector sizes: " << _defphases.size() << " " << _defpartnames.size();
  }

  std::vector<edm::ParameterSet> vps(iConfig.getParameter<std::vector<edm::ParameterSet>>("runPhases"));

  for (std::vector<edm::ParameterSet>::const_iterator ps = vps.begin(); ps != vps.end(); ps++) {
    _runphases[ps->getParameter<int>("runNumber")] = ps->getUntrackedParameter<std::vector<int>>("phases", _defphases);
    _runpartnames[ps->getParameter<int>("runNumber")] =
        ps->getUntrackedParameter<std::vector<std::string>>("partitions", _defpartnames);

    if (_runphases[ps->getParameter<int>("runNumber")].size() <
        _runpartnames[ps->getParameter<int>("runNumber")].size()) {
      // throw exception
      throw cms::Exception("InvalidAPVCyclePhases")
          << " Inconsistent run " << ps->getParameter<int>("runNumber")
          << " phases/partitions vector sizes: " << _runphases[ps->getParameter<int>("runNumber")].size() << " "
          << _runpartnames[ps->getParameter<int>("runNumber")].size();
    }
  }
}

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

//
// member functions
//

// ------------ method called to produce the data  ------------
std::shared_ptr<APVCyclePhaseCollection> ConfigurableAPVCyclePhaseProducer::globalBeginRun(
    const edm::Run& iRun, const edm::EventSetup& iSetup) const {
  using namespace edm;

  auto currapvphases = std::make_shared<APVCyclePhaseCollection>();

  // fill phase map

  const std::map<int, std::vector<std::string>>& _crunpartnames = _runpartnames;
  const std::map<int, std::vector<int>>& _crunphases = _runphases;

  std::map<int, std::vector<int>>::const_iterator trphases = _crunphases.find(iRun.run());
  std::map<int, std::vector<std::string>>::const_iterator trpartnames = _crunpartnames.find(iRun.run());

  std::vector<int> phases = _defphases;
  std::vector<std::string> partnames = _defpartnames;

  if (trphases != _crunphases.end()) {
    phases = trphases->second;
  }
  if (trpartnames != _crunpartnames.end()) {
    partnames = trpartnames->second;
  }

  if (phases.size() < partnames.size()) {
    // throw exception
    throw cms::Exception("InvalidAPVCyclePhases")
        << " Inconsistent phases/partitions vector sizes: " << phases.size() << " " << partnames.size();
  }

  for (unsigned int ipart = 0; ipart < partnames.size(); ++ipart) {
    if (phases[ipart] >= 0) {
      currapvphases->get()[partnames[ipart]] = phases[ipart];
    }
  }

  for (std::map<std::string, int>::const_iterator it = currapvphases->get().begin(); it != currapvphases->get().end();
       it++) {
    edm::LogInfo("APVCyclePhaseProducerDebug") << "partition " << it->first << " phase " << it->second;
  }
  return currapvphases;
}

void ConfigurableAPVCyclePhaseProducer::produce(edm::StreamID,
                                                edm::Event& iEvent,
                                                const edm::EventSetup& iSetup) const {
  using namespace edm;

  std::unique_ptr<APVCyclePhaseCollection> apvphases =
      std::make_unique<APVCyclePhaseCollection>(*runCache(iEvent.getRun().index()));

  iEvent.put(std::move(apvphases));
}

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