CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/IOMC/ParticleGuns/src/FlatRandomPtGunProducer.cc

Go to the documentation of this file.
00001 /*
00002  *  $Date: 2009/02/19 21:52:40 $
00003  *  $Revision: 1.4 $
00004  *  \author Julia Yarba
00005  */
00006 
00007 #include <ostream>
00008 
00009 #include "IOMC/ParticleGuns/interface/FlatRandomPtGunProducer.h"
00010 
00011 #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
00012 #include "SimDataFormats/GeneratorProducts/interface/GenEventInfoProduct.h"
00013 
00014 #include "FWCore/Framework/interface/Event.h"
00015 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00016 
00017 
00018 using namespace edm;
00019 using namespace std;
00020 
00021 FlatRandomPtGunProducer::FlatRandomPtGunProducer(const ParameterSet& pset) : 
00022    BaseFlatGunProducer(pset)
00023 {
00024 
00025 
00026    ParameterSet defpset ;
00027    ParameterSet pgun_params = 
00028       pset.getParameter<ParameterSet>("PGunParameters") ;
00029   
00030    fMinPt = pgun_params.getParameter<double>("MinPt");
00031    fMaxPt = pgun_params.getParameter<double>("MaxPt");
00032   
00033   produces<HepMCProduct>();
00034   produces<GenEventInfoProduct>();
00035 }
00036 
00037 FlatRandomPtGunProducer::~FlatRandomPtGunProducer()
00038 {
00039    // no need to cleanup GenEvent memory - done in HepMCProduct
00040 }
00041 
00042 void FlatRandomPtGunProducer::produce(Event &e, const EventSetup& es) 
00043 {
00044 
00045    if ( fVerbosity > 0 )
00046    {
00047       cout << " FlatRandomPtGunProducer : Begin New Event Generation" << endl ; 
00048    }
00049    // event loop (well, another step in it...)
00050           
00051    // no need to clean up GenEvent memory - done in HepMCProduct
00052    // 
00053    
00054    // here re-create fEvt (memory)
00055    //
00056    fEvt = new HepMC::GenEvent() ;
00057    
00058    // now actualy, cook up the event from PDGTable and gun parameters
00059    //
00060    // 1st, primary vertex
00061    //
00062    HepMC::GenVertex* Vtx = new HepMC::GenVertex(HepMC::FourVector(0.,0.,0.));
00063 
00064    // loop over particles
00065    //
00066    int barcode = 1 ;
00067    for (unsigned int ip=0; ip<fPartIDs.size(); ++ip)
00068    {
00069 
00070        double pt     = fRandomGenerator->fire(fMinPt, fMaxPt) ;
00071        double eta    = fRandomGenerator->fire(fMinEta, fMaxEta) ;
00072        double phi    = fRandomGenerator->fire(fMinPhi, fMaxPhi) ;
00073        int PartID = fPartIDs[ip] ;
00074        const HepPDT::ParticleData* 
00075           PData = fPDGTable->particle(HepPDT::ParticleID(abs(PartID))) ;
00076        double mass   = PData->mass().value() ;
00077        double theta  = 2.*atan(exp(-eta)) ;
00078        double mom    = pt/sin(theta) ;
00079        double px     = pt*cos(phi) ;
00080        double py     = pt*sin(phi) ;
00081        double pz     = mom*cos(theta) ;
00082        double energy2= mom*mom + mass*mass ;
00083        double energy = sqrt(energy2) ; 
00084        HepMC::FourVector p(px,py,pz,energy) ;
00085        HepMC::GenParticle* Part = 
00086            new HepMC::GenParticle(p,PartID,1);
00087        Part->suggest_barcode( barcode ) ;
00088        barcode++ ;
00089        Vtx->add_particle_out(Part);
00090 
00091        if ( fAddAntiParticle )
00092        {
00093           HepMC::FourVector ap(-px,-py,-pz,energy) ;
00094           int APartID = -PartID ;
00095           if ( PartID == 22 || PartID == 23 )
00096           {
00097              APartID = PartID ;
00098           }       
00099           HepMC::GenParticle* APart =
00100              new HepMC::GenParticle(ap,APartID,1);
00101           APart->suggest_barcode( barcode ) ;
00102           barcode++ ;
00103           Vtx->add_particle_out(APart) ;
00104        }
00105 
00106    }
00107 
00108    fEvt->add_vertex(Vtx) ;
00109    fEvt->set_event_number(e.id().event()) ;
00110    fEvt->set_signal_process_id(20) ; 
00111         
00112    if ( fVerbosity > 0 )
00113    {
00114       fEvt->print() ;  
00115    }
00116 
00117    auto_ptr<HepMCProduct> BProduct(new HepMCProduct()) ;
00118    BProduct->addHepMCData( fEvt );
00119    e.put(BProduct);
00120 
00121    auto_ptr<GenEventInfoProduct> genEventInfo(new GenEventInfoProduct(fEvt));
00122    e.put(genEventInfo);
00123     
00124    if ( fVerbosity > 0 )
00125    {
00126       // for testing purpose only
00127       // fEvt->print() ; // prints empty info after it's made into edm::Event
00128       cout << " FlatRandomPtGunProducer : Event Generation Done " << endl;
00129    }
00130 }
00131 //#include "FWCore/Framework/interface/MakerMacros.h"
00132 //DEFINE_FWK_MODULE(FlatRandomPtGunProducer);