Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:30:15

0001 #ifndef SimMuon_GEMDigitizer_ME0DigiPreRecoProducer_h
0002 #define SimMuon_GEMDigitizer_ME0DigiPreRecoProducer_h
0003 
0004 #include "FWCore/Framework/interface/stream/EDProducer.h"
0005 #include "FWCore/Framework/interface/MakerMacros.h"
0006 #include "FWCore/Framework/interface/ESHandle.h"
0007 #include "FWCore/Framework/interface/Event.h"
0008 #include "FWCore/Framework/interface/EventSetup.h"
0009 #include "FWCore/Framework/interface/ConsumesCollector.h"
0010 #include "FWCore/Utilities/interface/RandomNumberGenerator.h"
0011 #include "FWCore/Utilities/interface/Exception.h"
0012 #include "FWCore/Utilities/interface/ESGetToken.h"
0013 #include "FWCore/ServiceRegistry/interface/Service.h"
0014 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0015 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0016 
0017 #include "DataFormats/Common/interface/Handle.h"
0018 #include "SimDataFormats/CrossingFrame/interface/CrossingFrame.h"
0019 #include "SimDataFormats/TrackingHit/interface/PSimHitContainer.h"
0020 #include "SimDataFormats/CrossingFrame/interface/MixCollection.h"
0021 
0022 #include "SimMuon/GEMDigitizer/interface/ME0DigiPreRecoModelFactory.h"
0023 #include "SimMuon/GEMDigitizer/interface/ME0DigiPreRecoModel.h"
0024 
0025 #include "Geometry/Records/interface/MuonGeometryRecord.h"
0026 #include "Geometry/GEMGeometry/interface/ME0Geometry.h"
0027 
0028 #include <sstream>
0029 #include <string>
0030 #include <map>
0031 #include <vector>
0032 
0033 namespace CLHEP {
0034   class HepRandomEngine;
0035 }
0036 
0037 class ME0DigiPreRecoProducer : public edm::stream::EDProducer<> {
0038 public:
0039   explicit ME0DigiPreRecoProducer(const edm::ParameterSet& ps);
0040 
0041   ~ME0DigiPreRecoProducer() override;
0042 
0043   void beginRun(const edm::Run&, const edm::EventSetup&) override;
0044 
0045   void produce(edm::Event&, const edm::EventSetup&) override;
0046 
0047 private:
0048   //Name of Collection used for create the XF
0049   edm::EDGetTokenT<CrossingFrame<PSimHit> > cf_token;
0050   edm::ESGetToken<ME0Geometry, MuonGeometryRecord> geom_token_;
0051 
0052   std::string digiPreRecoModelString_;
0053   std::unique_ptr<ME0DigiPreRecoModel> me0DigiPreRecoModel_;
0054 };
0055 
0056 ME0DigiPreRecoProducer::ME0DigiPreRecoProducer(const edm::ParameterSet& ps)
0057     : digiPreRecoModelString_(ps.getParameter<std::string>("digiPreRecoModelString")),
0058       me0DigiPreRecoModel_{ME0DigiPreRecoModelFactory::get()->create("ME0" + digiPreRecoModelString_ + "Model", ps)} {
0059   produces<ME0DigiPreRecoCollection>();
0060 
0061   edm::Service<edm::RandomNumberGenerator> rng;
0062   if (!rng.isAvailable()) {
0063     throw cms::Exception("Configuration")
0064         << "ME0DigiPreRecoProducer::ME0PreRecoDigiProducer() - RandomNumberGeneratorService is not present in "
0065            "configuration file.\n"
0066         << "Add the service in the configuration file or remove the modules that require it.";
0067   }
0068   LogDebug("ME0DigiPreRecoProducer") << "Using ME0" + digiPreRecoModelString_ + "Model";
0069 
0070   std::string mix_(ps.getParameter<std::string>("mixLabel"));
0071   std::string collection_(ps.getParameter<std::string>("inputCollection"));
0072 
0073   cf_token = consumes<CrossingFrame<PSimHit> >(edm::InputTag(mix_, collection_));
0074   geom_token_ = esConsumes<ME0Geometry, MuonGeometryRecord, edm::Transition::BeginRun>();
0075 }
0076 
0077 ME0DigiPreRecoProducer::~ME0DigiPreRecoProducer() = default;
0078 
0079 void ME0DigiPreRecoProducer::beginRun(const edm::Run&, const edm::EventSetup& eventSetup) {
0080   // set geometry
0081   edm::ESHandle<ME0Geometry> hGeom = eventSetup.getHandle(geom_token_);
0082   me0DigiPreRecoModel_->setGeometry(&*hGeom);
0083   me0DigiPreRecoModel_->setup();
0084 }
0085 
0086 void ME0DigiPreRecoProducer::produce(edm::Event& e, const edm::EventSetup& eventSetup) {
0087   edm::Service<edm::RandomNumberGenerator> rng;
0088   CLHEP::HepRandomEngine* engine = &rng->getEngine(e.streamID());
0089 
0090   edm::Handle<CrossingFrame<PSimHit> > cf;
0091   e.getByToken(cf_token, cf);
0092 
0093   MixCollection<PSimHit> hits{cf.product()};
0094 
0095   // Create empty output
0096   auto digis = std::make_unique<ME0DigiPreRecoCollection>();
0097 
0098   // arrange the hits by eta partition
0099   std::map<uint32_t, edm::PSimHitContainer> hitMap;
0100   for (const auto& hit : hits) {
0101     hitMap[hit.detUnitId()].push_back(hit);
0102   }
0103 
0104   // simulate signal and noise for each eta partition
0105   const auto& etaPartitions(me0DigiPreRecoModel_->getGeometry()->etaPartitions());
0106 
0107   for (const auto& roll : etaPartitions) {
0108     const ME0DetId detId(roll->id());
0109     const uint32_t rawId(detId.rawId());
0110     const auto& simHits(hitMap[rawId]);
0111 
0112     LogDebug("ME0DigiPreRecoProducer") << "ME0DigiPreRecoProducer: found " << simHits.size()
0113                                        << " hit(s) in eta partition" << rawId;
0114 
0115     me0DigiPreRecoModel_->simulateSignal(roll, simHits, engine);
0116     me0DigiPreRecoModel_->simulateNoise(roll, engine);
0117     me0DigiPreRecoModel_->fillDigis(rawId, *digis);
0118   }
0119 
0120   // store them in the event
0121   e.put(std::move(digis));
0122 }
0123 
0124 DEFINE_FWK_MODULE(ME0DigiPreRecoProducer);
0125 #endif