Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-09-02 03:46:13

0001 // -*- C++ -*-
0002 //
0003 // Package:    SimPPS/PPSSimTrackProducer
0004 // Class:      PPSSimTrackProducer
0005 //
0006 /**\class PPSSimTrackProducer PPSSimTrackProducer.cc SimPPS/PPSSimTrackProducer/plugins/PPSSimTrackProducer.cc
0007 
0008  Description: [one line class summary]
0009 
0010  Implementation:
0011      [Notes on implementation]
0012 */
0013 //
0014 // Original Author:  Luiz Martins Mundim Filho
0015 //         Created:  Sun, 03 Dec 2017 00:25:54 GMT
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/stream/EDProducer.h"
0025 #include "FWCore/Framework/interface/Event.h"
0026 #include "FWCore/Framework/interface/MakerMacros.h"
0027 #include "FWCore/ServiceRegistry/interface/Service.h"
0028 #include "FWCore/Utilities/interface/EDGetToken.h"
0029 #include "FWCore/Utilities/interface/InputTag.h"
0030 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0031 
0032 #include "SimDataFormats/Forward/interface/LHCTransportLink.h"
0033 #include "SimDataFormats/Forward/interface/LHCTransportLinkContainer.h"
0034 
0035 #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
0036 #include "FWCore/Utilities/interface/RandomNumberGenerator.h"
0037 
0038 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0039 #include "FWCore/Utilities/interface/StreamID.h"
0040 #include "SimTransport/PPSProtonTransport/interface/ProtonTransport.h"
0041 #include "CLHEP/Random/RandomEngine.h"
0042 #include "TRandom3.h"
0043 
0044 //
0045 // class declaration
0046 //
0047 
0048 class PPSSimTrackProducer : public edm::stream::EDProducer<> {
0049 public:
0050   explicit PPSSimTrackProducer(const edm::ParameterSet&);
0051   ~PPSSimTrackProducer() override = default;
0052 
0053   //static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0054 
0055 private:
0056   void produce(edm::Event&, const edm::EventSetup&) override;
0057 
0058   // ----------member data ---------------------------
0059   bool m_verbosity;
0060   ProtonTransport theTransporter;
0061   edm::InputTag m_InTag;
0062   edm::EDGetTokenT<edm::HepMCProduct> m_InTagToken;
0063 
0064   std::string m_transportMethod;
0065   int m_eventsAnalysed;  //!< just to count events that have been analysed
0066 };
0067 
0068 //
0069 // constants, enums and typedefs
0070 //
0071 
0072 //
0073 // static data member definitions
0074 //
0075 
0076 //
0077 // constructors and destructor
0078 //
0079 PPSSimTrackProducer::PPSSimTrackProducer(const edm::ParameterSet& iConfig)
0080     : theTransporter(iConfig, consumesCollector()) {
0081   m_InTag = iConfig.getParameter<edm::InputTag>("HepMCProductLabel");
0082   m_InTagToken = consumes<edm::HepMCProduct>(m_InTag);
0083 
0084   m_verbosity = iConfig.getParameter<bool>("Verbosity");
0085   m_eventsAnalysed = 0;
0086   //m_transportMethod = iConfig.getParameter<std::string>("TransportMethod");
0087 
0088   produces<edm::HepMCProduct>();
0089   produces<edm::LHCTransportLinkContainer>();
0090 
0091   edm::Service<edm::RandomNumberGenerator> rng;
0092   if (!rng.isAvailable()) {
0093     throw cms::Exception("Configuration")
0094         << "LHCTransport (ProtonTransport) requires the RandomNumberGeneratorService\n"
0095            "which is not present in the configuration file.  You must add the service\n"
0096            "in the configuration file or remove the modules that require it.";
0097   }
0098 }
0099 
0100 //
0101 // member functions
0102 //
0103 
0104 // ------------ method called to produce the data  ------------
0105 void PPSSimTrackProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0106   using namespace edm;
0107   using namespace std;
0108   HepMC::GenEvent* evt;
0109   edm::Service<edm::RandomNumberGenerator> rng;
0110   CLHEP::HepRandomEngine* engine = &rng->getEngine(iEvent.streamID());
0111   if (engine->name() != "TRandom3") {
0112     throw cms::Exception("Configuration") << "The TRandom3 engine type must be used with ProtonTransport, Random "
0113                                              "Number Generator Service not correctly configured!";
0114   }
0115 
0116   m_eventsAnalysed++;
0117   Handle<HepMCProduct> HepMCEvt;
0118   iEvent.getByToken(m_InTagToken, HepMCEvt);
0119 
0120   if (!HepMCEvt.isValid()) {
0121     throw cms::Exception("InvalidReference") << "Invalid reference to HepMCProduct\n";
0122   }
0123 
0124   if (HepMCEvt.provenance()->moduleLabel() == "LHCTransport") {
0125     throw cms::Exception("LogicError") << "LHCTrasport HepMCProduce already exists\n";
0126   }
0127   edm::LogVerbatim("ProtonTransportEventProcessing") << "produce begin for event " << m_eventsAnalysed;
0128 
0129   evt = new HepMC::GenEvent(HepMCEvt->GetEvent()->signal_process_id(), HepMCEvt->GetEvent()->event_number());
0130 
0131   theTransporter.process(HepMCEvt->GetEvent(), iSetup, engine);
0132   theTransporter.addPartToHepMC(HepMCEvt->GetEvent(), evt);
0133 
0134   if (m_verbosity)
0135     evt->print();
0136 
0137   unique_ptr<HepMCProduct> newProduct(new edm::HepMCProduct());
0138   newProduct->addHepMCData(evt);
0139 
0140   iEvent.put(std::move(newProduct));
0141 
0142   unique_ptr<LHCTransportLinkContainer> NewCorrespondenceMap(new edm::LHCTransportLinkContainer());
0143   edm::LHCTransportLinkContainer thisLink(theTransporter.getCorrespondenceMap());
0144   (*NewCorrespondenceMap).swap(thisLink);
0145 
0146   if (m_verbosity) {
0147     for (unsigned int i = 0; i < (*NewCorrespondenceMap).size(); ++i)
0148       LogDebug("ProtonTransportEventProcessing")
0149           << "ProtonTransport correspondence table: " << (*NewCorrespondenceMap)[i];
0150   }
0151 
0152   iEvent.put(std::move(NewCorrespondenceMap));
0153   // There is no need to delete the pointer to the event, since it is deleted in HepMCProduct,
0154   // in fact, it MUST NOT be delete here, as a protection is missing in above package
0155   edm::LogVerbatim("ProtonTransportEventProcessing") << "produce end ";
0156 }
0157 
0158 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0159 /* to be done */
0160 //define this as a plug-in
0161 DEFINE_FWK_MODULE(PPSSimTrackProducer);