Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:13:50

0001 // -*- C++ -*-
0002 //
0003 // Package:    LHE2HepMCConverter
0004 // Class:      LHE2HepMCConverter
0005 //
0006 /**\class LHE2HepMCConverter LHE2HepMCConverter.cc GeneratorInterface/LHE2HepMCConverter/src/LHE2HepMCConverter.cc
0007 
0008  Description: [one line class summary]
0009 
0010  Implementation:
0011      [Notes on implementation]
0012 */
0013 //
0014 // Original Author:  Piergiulio Lenzi,40 1-B01,+41227671638,
0015 //         Created:  Wed Aug 31 19:02:24 CEST 2011
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/one/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/ParameterSet/interface/ParameterSet.h"
0031 #include "FWCore/Utilities/interface/InputTag.h"
0032 #include "DataFormats/Common/interface/Handle.h"
0033 
0034 #include "SimDataFormats/GeneratorProducts/interface/LHEEventProduct.h"
0035 #include "SimDataFormats/GeneratorProducts/interface/LHERunInfoProduct.h"
0036 #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
0037 
0038 //
0039 // class declaration
0040 //
0041 
0042 class LHE2HepMCConverter : public edm::one::EDProducer<edm::one::WatchRuns> {
0043 public:
0044   explicit LHE2HepMCConverter(const edm::ParameterSet&);
0045   ~LHE2HepMCConverter() override = default;
0046 
0047   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0048 
0049 protected:
0050   //      lhef::LHERunInfo *lheRunInfo() { return lheRunInfo_.get(); }
0051 
0052 private:
0053   void produce(edm::Event&, const edm::EventSetup&) override;
0054   void beginRun(edm::Run const&, edm::EventSetup const&) override;
0055   void endRun(edm::Run const&, edm::EventSetup const&) override {}
0056 
0057   // ----------member data ---------------------------
0058   const LHERunInfoProduct* _lheRunSrc;
0059   const edm::InputTag _lheEventSrcTag;
0060   const edm::InputTag _lheRunSrcTag;
0061   const edm::EDGetTokenT<LHEEventProduct> eventProductToken_;
0062   const edm::EDGetTokenT<LHERunInfoProduct> runInfoProductToken_;
0063   //      std::shared_ptr<lhef::LHERunInfo> lheRunInfo_;
0064 };
0065 
0066 //
0067 // constants, enums and typedefs
0068 //
0069 
0070 //
0071 // static data member definitions
0072 //
0073 
0074 //
0075 // constructors and destructor
0076 //
0077 LHE2HepMCConverter::LHE2HepMCConverter(const edm::ParameterSet& iConfig)
0078     : _lheRunSrc(nullptr),
0079       _lheEventSrcTag(iConfig.getParameter<edm::InputTag>("LHEEventProduct")),
0080       _lheRunSrcTag(iConfig.getParameter<edm::InputTag>("LHERunInfoProduct")),
0081       eventProductToken_(consumes<LHEEventProduct>(_lheEventSrcTag)),
0082       runInfoProductToken_(consumes<LHERunInfoProduct, edm::InRun>(_lheRunSrcTag)) {
0083   //register your products
0084   produces<edm::HepMCProduct>("unsmeared");
0085 }
0086 
0087 //
0088 // member functions
0089 //
0090 
0091 // ------------ method called to produce the data  ------------
0092 void LHE2HepMCConverter::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0093   using namespace edm;
0094   const edm::Handle<LHEEventProduct>& lheEventSrc = iEvent.getHandle(eventProductToken_);
0095 
0096   HepMC::GenEvent* evt = new HepMC::GenEvent();
0097   HepMC::GenVertex* v = new HepMC::GenVertex();
0098   evt->add_vertex(v);
0099   if (_lheRunSrc) {
0100     HepMC::FourVector beam1(0, 0, _lheRunSrc->heprup().EBMUP.first, _lheRunSrc->heprup().EBMUP.first);
0101     HepMC::GenParticle* gp1 = new HepMC::GenParticle(beam1, _lheRunSrc->heprup().IDBMUP.first, 4);
0102     v->add_particle_in(gp1);
0103     HepMC::FourVector beam2(0, 0, _lheRunSrc->heprup().EBMUP.second, _lheRunSrc->heprup().EBMUP.second);
0104     HepMC::GenParticle* gp2 = new HepMC::GenParticle(beam2, _lheRunSrc->heprup().IDBMUP.second, 4);
0105     v->add_particle_in(gp2);
0106     evt->set_beam_particles(gp1, gp2);
0107   } else {
0108     LogWarning("LHE2HepMCConverter") << "Could not retrieve the LHERunInfoProduct for this event. You'll miss the beam "
0109                                         "particles in your HepMC product.";
0110   }
0111 
0112   for (int i = 0; i < lheEventSrc->hepeup().NUP; ++i) {
0113     if (lheEventSrc->hepeup().ISTUP[i] != 1) {
0114       //cout << reader->hepeup.ISTUP[i] << ", " << reader->hepeup.IDUP[i] << endl;
0115       continue;
0116     }
0117     HepMC::FourVector p(lheEventSrc->hepeup().PUP[i][0],
0118                         lheEventSrc->hepeup().PUP[i][1],
0119                         lheEventSrc->hepeup().PUP[i][2],
0120                         lheEventSrc->hepeup().PUP[i][3]);
0121     HepMC::GenParticle* gp = new HepMC::GenParticle(p, lheEventSrc->hepeup().IDUP[i], 1);
0122     gp->set_generated_mass(lheEventSrc->hepeup().PUP[i][4]);
0123     v->add_particle_out(gp);
0124   }
0125 
0126   std::unique_ptr<HepMCProduct> pOut(new HepMCProduct(evt));
0127   iEvent.put(std::move(pOut), "unsmeared");
0128 }
0129 
0130 // ------------ method called when starting to processes a run  ------------
0131 void LHE2HepMCConverter::beginRun(edm::Run const& iRun, edm::EventSetup const&) {
0132   edm::Handle<LHERunInfoProduct> lheRunSrcHandle = iRun.getHandle(runInfoProductToken_);
0133   if (lheRunSrcHandle.isValid()) {
0134     _lheRunSrc = lheRunSrcHandle.product();
0135   } else {
0136     if (_lheRunSrcTag.label() != "source") {
0137       iRun.getByLabel("source", lheRunSrcHandle);
0138       if (lheRunSrcHandle.isValid()) {
0139         _lheRunSrc = lheRunSrcHandle.product();
0140         edm::LogInfo("LHE2HepMCConverter") << "Taking LHERunInfoproduct from source";
0141       } else
0142         edm::LogWarning("LHE2HepMCConverter") << "No LHERunInfoProduct from source";
0143     }
0144   }
0145 }
0146 
0147 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0148 void LHE2HepMCConverter::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0149   //The following says we do not know what parameters are allowed so do no validation
0150   // Please change this to state exactly what you do use, even if it is no parameters
0151   edm::ParameterSetDescription desc;
0152   desc.setUnknown();
0153   descriptions.addDefault(desc);
0154 }
0155 
0156 //define this as a plug-in
0157 DEFINE_FWK_MODULE(LHE2HepMCConverter);