Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-12-08 08:16:01

0001 // -*- C++ -*-
0002 //
0003 // Package:    SiStripTools
0004 // Class:      ConfigurableAPVCyclePhaseProducer
0005 //
0006 /**\class ConfigurableAPVCyclePhaseProducer ConfigurableAPVCyclePhaseProducer.cc DPGAnalysis/SiStripTools/plugins/ConfigurableAPVCyclePhaseProducer.cc
0007 
0008  Description: EDproducer for APVCyclePhaseCollection which uses the configuration file to assign a phase to the run
0009 
0010  Implementation:
0011      <Notes on implementation>
0012 */
0013 //
0014 // Original Author:  Andrea Venturi
0015 //         Created:  Mon Jan 12 09:05:45 CET 2009
0016 //
0017 //
0018 
0019 // system include files
0020 #include <memory>
0021 
0022 // user include files
0023 #include "FWCore/Framework/interface/Frameworkfwd.h"
0024 #include "FWCore/Framework/interface/global/EDProducer.h"
0025 
0026 #include "FWCore/Framework/interface/Event.h"
0027 #include "FWCore/Framework/interface/Run.h"
0028 #include "FWCore/Framework/interface/MakerMacros.h"
0029 
0030 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0031 
0032 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0033 #include "FWCore/Utilities/interface/Exception.h"
0034 
0035 #include <map>
0036 #include <string>
0037 
0038 #include "DPGAnalysis/SiStripTools/interface/APVCyclePhaseCollection.h"
0039 
0040 //
0041 // class decleration
0042 //
0043 
0044 class ConfigurableAPVCyclePhaseProducer : public edm::global::EDProducer<edm::RunCache<APVCyclePhaseCollection>> {
0045 public:
0046   explicit ConfigurableAPVCyclePhaseProducer(const edm::ParameterSet&);
0047   ~ConfigurableAPVCyclePhaseProducer() override;
0048 
0049 private:
0050   std::shared_ptr<APVCyclePhaseCollection> globalBeginRun(const edm::Run&, const edm::EventSetup&) const override;
0051   void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
0052   void globalEndRun(edm::Run const&, edm::EventSetup const&) const override {}
0053 
0054   // ----------member data ---------------------------
0055 
0056   const std::vector<std::string> _defpartnames;
0057   const std::vector<int> _defphases;
0058 
0059   std::map<int, std::vector<std::string>> _runpartnames;
0060   std::map<int, std::vector<int>> _runphases;
0061 };
0062 
0063 //
0064 // constants, enums and typedefs
0065 //
0066 
0067 //
0068 // static data member definitions
0069 //
0070 
0071 //
0072 // constructors and destructor
0073 //
0074 ConfigurableAPVCyclePhaseProducer::ConfigurableAPVCyclePhaseProducer(const edm::ParameterSet& iConfig)
0075     : _defpartnames(iConfig.getParameter<std::vector<std::string>>("defaultPartitionNames")),
0076       _defphases(iConfig.getParameter<std::vector<int>>("defaultPhases")) {
0077   produces<APVCyclePhaseCollection, edm::InEvent>();
0078 
0079   //now do what ever other initialization is needed
0080 
0081   if (_defphases.size() < _defpartnames.size()) {
0082     // throw exception
0083     throw cms::Exception("InvalidAPVCyclePhases")
0084         << " Inconsistent default phases/partitions vector sizes: " << _defphases.size() << " " << _defpartnames.size();
0085   }
0086 
0087   std::vector<edm::ParameterSet> vps(iConfig.getParameter<std::vector<edm::ParameterSet>>("runPhases"));
0088 
0089   for (std::vector<edm::ParameterSet>::const_iterator ps = vps.begin(); ps != vps.end(); ps++) {
0090     _runphases[ps->getParameter<int>("runNumber")] = ps->getUntrackedParameter<std::vector<int>>("phases", _defphases);
0091     _runpartnames[ps->getParameter<int>("runNumber")] =
0092         ps->getUntrackedParameter<std::vector<std::string>>("partitions", _defpartnames);
0093 
0094     if (_runphases[ps->getParameter<int>("runNumber")].size() <
0095         _runpartnames[ps->getParameter<int>("runNumber")].size()) {
0096       // throw exception
0097       throw cms::Exception("InvalidAPVCyclePhases")
0098           << " Inconsistent run " << ps->getParameter<int>("runNumber")
0099           << " phases/partitions vector sizes: " << _runphases[ps->getParameter<int>("runNumber")].size() << " "
0100           << _runpartnames[ps->getParameter<int>("runNumber")].size();
0101     }
0102   }
0103 }
0104 
0105 ConfigurableAPVCyclePhaseProducer::~ConfigurableAPVCyclePhaseProducer() {
0106   // do anything here that needs to be done at desctruction time
0107   // (e.g. close files, deallocate resources etc.)
0108 }
0109 
0110 //
0111 // member functions
0112 //
0113 
0114 // ------------ method called to produce the data  ------------
0115 std::shared_ptr<APVCyclePhaseCollection> ConfigurableAPVCyclePhaseProducer::globalBeginRun(
0116     const edm::Run& iRun, const edm::EventSetup& iSetup) const {
0117   using namespace edm;
0118 
0119   auto currapvphases = std::make_shared<APVCyclePhaseCollection>();
0120 
0121   // fill phase map
0122 
0123   const std::map<int, std::vector<std::string>>& _crunpartnames = _runpartnames;
0124   const std::map<int, std::vector<int>>& _crunphases = _runphases;
0125 
0126   std::map<int, std::vector<int>>::const_iterator trphases = _crunphases.find(iRun.run());
0127   std::map<int, std::vector<std::string>>::const_iterator trpartnames = _crunpartnames.find(iRun.run());
0128 
0129   std::vector<int> phases = _defphases;
0130   std::vector<std::string> partnames = _defpartnames;
0131 
0132   if (trphases != _crunphases.end()) {
0133     phases = trphases->second;
0134   }
0135   if (trpartnames != _crunpartnames.end()) {
0136     partnames = trpartnames->second;
0137   }
0138 
0139   if (phases.size() < partnames.size()) {
0140     // throw exception
0141     throw cms::Exception("InvalidAPVCyclePhases")
0142         << " Inconsistent phases/partitions vector sizes: " << phases.size() << " " << partnames.size();
0143   }
0144 
0145   for (unsigned int ipart = 0; ipart < partnames.size(); ++ipart) {
0146     if (phases[ipart] >= 0) {
0147       currapvphases->get()[partnames[ipart]] = phases[ipart];
0148     }
0149   }
0150 
0151   for (std::map<std::string, int>::const_iterator it = currapvphases->get().begin(); it != currapvphases->get().end();
0152        it++) {
0153     edm::LogInfo("APVCyclePhaseProducerDebug") << "partition " << it->first << " phase " << it->second;
0154   }
0155   return currapvphases;
0156 }
0157 
0158 void ConfigurableAPVCyclePhaseProducer::produce(edm::StreamID,
0159                                                 edm::Event& iEvent,
0160                                                 const edm::EventSetup& iSetup) const {
0161   using namespace edm;
0162 
0163   std::unique_ptr<APVCyclePhaseCollection> apvphases =
0164       std::make_unique<APVCyclePhaseCollection>(*runCache(iEvent.getRun().index()));
0165 
0166   iEvent.put(std::move(apvphases));
0167 }
0168 
0169 //define this as a plug-in
0170 DEFINE_FWK_MODULE(ConfigurableAPVCyclePhaseProducer);