CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/GeneratorInterface/LHEInterface/plugins/LHE2HepMCConverter.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:    LHE2HepMCConverter
00004 // Class:      LHE2HepMCConverter
00005 // 
00013 //
00014 // Original Author:  Piergiulio Lenzi,40 1-B01,+41227671638,
00015 //         Created:  Wed Aug 31 19:02:24 CEST 2011
00016 // $Id: LHE2HepMCConverter.cc,v 1.2 2013/03/01 21:45:29 wdd Exp $
00017 //
00018 //
00019 
00020 
00021 // system include files
00022 #include <memory>
00023 
00024 #include <boost/shared_ptr.hpp>
00025 
00026 // user include files
00027 #include "FWCore/Framework/interface/Frameworkfwd.h"
00028 #include "FWCore/Framework/interface/EDProducer.h"
00029 
00030 #include "FWCore/Framework/interface/Event.h"
00031 #include "FWCore/Framework/interface/Run.h"
00032 #include "FWCore/Framework/interface/MakerMacros.h"
00033 
00034 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00035 #include "FWCore/Utilities/interface/InputTag.h"
00036 #include "DataFormats/Common/interface/Handle.h"
00037 
00038 #include "SimDataFormats/GeneratorProducts/interface/LHEEventProduct.h"
00039 #include "SimDataFormats/GeneratorProducts/interface/LHERunInfoProduct.h"
00040 #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
00041 
00042 //
00043 // class declaration
00044 //
00045 
00046 class LHE2HepMCConverter : public edm::EDProducer {
00047    public:
00048       explicit LHE2HepMCConverter(const edm::ParameterSet&);
00049       ~LHE2HepMCConverter();
00050 
00051       static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
00052 
00053    protected:
00054 
00055   //      lhef::LHERunInfo *lheRunInfo() { return lheRunInfo_.get(); }
00056 
00057    private:
00058 
00059       virtual void produce(edm::Event&, const edm::EventSetup&) override;
00060       virtual void beginRun(edm::Run const&, edm::EventSetup const&) override;
00061 
00062       // ----------member data ---------------------------
00063       edm::InputTag _lheEventSrcTag;
00064       edm::InputTag _lheRunSrcTag;
00065       const LHERunInfoProduct* _lheRunSrc;
00066 
00067   //      boost::shared_ptr<lhef::LHERunInfo> lheRunInfo_;
00068 
00069 };
00070 
00071 //
00072 // constants, enums and typedefs
00073 //
00074 
00075 
00076 //
00077 // static data member definitions
00078 //
00079 
00080 //
00081 // constructors and destructor
00082 //
00083 LHE2HepMCConverter::LHE2HepMCConverter(const edm::ParameterSet& iConfig):
00084 _lheRunSrc(0)
00085 {
00086    //register your products
00087    produces<edm::HepMCProduct>();
00088 
00089    _lheEventSrcTag = iConfig.getParameter<edm::InputTag>("LHEEventProduct");
00090    _lheRunSrcTag   = iConfig.getParameter<edm::InputTag>("LHERunInfoProduct");
00091 
00092 }
00093 
00094 
00095 LHE2HepMCConverter::~LHE2HepMCConverter()
00096 {
00097  
00098    // do anything here that needs to be done at desctruction time
00099    // (e.g. close files, deallocate resources etc.)
00100 
00101 }
00102 
00103 
00104 //
00105 // member functions
00106 //
00107 
00108 // ------------ method called to produce the data  ------------
00109 void
00110 LHE2HepMCConverter::produce(edm::Event& iEvent, const edm::EventSetup& iSetup)
00111 {
00112    using namespace edm;
00113    Handle<LHEEventProduct> lheEventSrc;
00114    iEvent.getByLabel(_lheEventSrcTag, lheEventSrc);
00115 
00116    HepMC::GenEvent *evt = new HepMC::GenEvent();
00117    HepMC::GenVertex* v = new HepMC::GenVertex();
00118    evt->add_vertex(v);
00119    if (_lheRunSrc){
00120      HepMC::FourVector beam1(0, 0, _lheRunSrc->heprup().EBMUP.first, _lheRunSrc->heprup().EBMUP.first);
00121      HepMC::GenParticle* gp1 = new HepMC::GenParticle(beam1, _lheRunSrc->heprup().IDBMUP.first, 4);
00122      v->add_particle_in(gp1);
00123      HepMC::FourVector beam2(0, 0, _lheRunSrc->heprup().EBMUP.second, _lheRunSrc->heprup().EBMUP.second);
00124      HepMC::GenParticle* gp2 = new HepMC::GenParticle(beam2, _lheRunSrc->heprup().IDBMUP.second, 4);
00125      v->add_particle_in(gp2);
00126      evt->set_beam_particles(gp1, gp2);
00127    } else {
00128      LogWarning("LHE2HepMCConverter") << "Could not retrieve the LHERunInfoProduct for this event. You'll miss the beam particles in your HepMC product." ; 
00129    }
00130 
00131 
00132    for (int i = 0; i < lheEventSrc->hepeup().NUP; ++i) {
00133       if (lheEventSrc->hepeup().ISTUP[i] != 1) {
00134        //cout << reader->hepeup.ISTUP[i] << ", " << reader->hepeup.IDUP[i] << endl;
00135        continue;
00136       }
00137       HepMC::FourVector p(lheEventSrc->hepeup().PUP[i][0], lheEventSrc->hepeup().PUP[i][1],
00138                           lheEventSrc->hepeup().PUP[i][2], lheEventSrc->hepeup().PUP[i][3]);
00139       HepMC::GenParticle* gp = new HepMC::GenParticle(p, lheEventSrc->hepeup().IDUP[i], 1);
00140       gp->set_generated_mass(lheEventSrc->hepeup().PUP[i][4]);
00141       v->add_particle_out(gp);
00142    } 
00143 
00144    std::auto_ptr<HepMCProduct> pOut(new HepMCProduct(evt));
00145    iEvent.put(pOut);
00146 
00147 }
00148 
00149 // ------------ method called when starting to processes a run  ------------
00150 void
00151 LHE2HepMCConverter::beginRun(edm::Run const& iRun, edm::EventSetup const&)
00152 {
00153   
00154   edm::Handle<LHERunInfoProduct> lheRunSrcHandle;
00155   iRun.getByLabel(_lheRunSrcTag, lheRunSrcHandle);
00156   if (lheRunSrcHandle.isValid()) {
00157     _lheRunSrc = lheRunSrcHandle.product();
00158   }
00159   else {
00160     if (_lheRunSrcTag.label() != "source" ) {
00161       iRun.getByLabel("source", lheRunSrcHandle);
00162       if (lheRunSrcHandle.isValid()) {
00163         _lheRunSrc = lheRunSrcHandle.product();
00164         edm::LogInfo("LHE2HepMCConverter") << "Taking LHERunInfoproduct from source";
00165       }
00166       else
00167         edm::LogWarning("LHE2HepMCConverter") << "No LHERunInfoProduct from source"; 
00168     }
00169 
00170   }
00171 
00172 }
00173 
00174 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
00175 void
00176 LHE2HepMCConverter::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
00177   //The following says we do not know what parameters are allowed so do no validation
00178   // Please change this to state exactly what you do use, even if it is no parameters
00179   edm::ParameterSetDescription desc;
00180   desc.setUnknown();
00181   descriptions.addDefault(desc);
00182 }
00183 
00184 //define this as a plug-in
00185 DEFINE_FWK_MODULE(LHE2HepMCConverter);