00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <iostream>
00021 #include <fstream>
00022
00023 #include "IOMC/ParticleGuns/interface/FlatEGunASCIIWriter.h"
00024
00025
00026 #include "FWCore/Framework/interface/Event.h"
00027 #include "FWCore/Framework/interface/MakerMacros.h"
00028 #include "FWCore/Utilities/interface/Exception.h"
00029
00030 #include "SimGeneral/HepPDTRecord/interface/ParticleDataTable.h"
00031
00032 #include "CLHEP/Random/RandFlat.h"
00033
00034 using namespace edm;
00035 using namespace std;
00036
00037 FlatEGunASCIIWriter::FlatEGunASCIIWriter( const ParameterSet& pset )
00038 : fEvt(0),
00039 fOutFileName( pset.getUntrackedParameter<string>("OutFileName","FlatEGunHepMC.dat") ),
00040 fCurrentEvent(0)
00041 {
00042
00043 ParameterSet pgun_params = pset.getParameter<ParameterSet>("PGunParameters") ;
00044 fPartIDs = pgun_params.getParameter< vector<int> >("PartID");
00045 fMinEta = pgun_params.getParameter<double>("MinEta");
00046 fMaxEta = pgun_params.getParameter<double>("MaxEta");
00047 fMinPhi = pgun_params.getParameter<double>("MinPhi");
00048 fMaxPhi = pgun_params.getParameter<double>("MaxPhi");
00049 fMinE = pgun_params.getParameter<double>("MinE");
00050 fMaxE = pgun_params.getParameter<double>("MaxE");
00051
00052 }
00053
00054 FlatEGunASCIIWriter::~FlatEGunASCIIWriter()
00055 {
00056
00057 if ( fEvt != NULL ) delete fEvt ;
00058 delete fOutStream;
00059
00060 }
00061
00062 void FlatEGunASCIIWriter::beginJob( const EventSetup& es)
00063 {
00064
00065 es.getData( fPDGTable );
00066
00067 fOutStream = new HepMC::IO_Ascii( fOutFileName.c_str() );
00068 if ( fOutStream->rdstate() == std::ios::failbit ) {
00069 throw cms::Exception("FileNotOpen", "FlatEGunASCIIWriter::beginJob()")
00070 << "File " << fOutFileName << " was not open.\n";
00071 }
00072
00073 return ;
00074
00075 }
00076
00077 void FlatEGunASCIIWriter::analyze( const Event& ,
00078 const EventSetup& )
00079 {
00080
00081
00082
00083 if ( fEvt != NULL ) delete fEvt ;
00084
00085
00086 fEvt = new HepMC::GenEvent() ;
00087
00088
00089
00090
00091 HepMC::GenVertex* Vtx = new HepMC::GenVertex( HepMC::FourVector(0.,0.,0.) );
00092
00093
00094
00095 for ( unsigned int ip=0; ip<fPartIDs.size(); ip++ )
00096 {
00097 double energy = RandFlat::shoot( fMinE, fMaxE ) ;
00098 double eta = RandFlat::shoot( fMinEta, fMaxEta ) ;
00099 double phi = RandFlat::shoot( fMinPhi, fMaxPhi ) ;
00100 const HepPDT::ParticleData*
00101 PData = fPDGTable->particle(HepPDT::ParticleID(abs(fPartIDs[ip]))) ;
00102 double mass = PData->mass().value() ;
00103 double mom2 = energy*energy - mass*mass ;
00104 double mom = 0. ;
00105 if ( mom2 > 0. ) mom = sqrt(mom2) ;
00106 double theta = 2.*atan(exp(-eta)) ;
00107 double px = mom*sin(theta)*cos(phi) ;
00108 double py = mom*sin(theta)*sin(phi) ;
00109 double pz = mom*cos(theta) ;
00110
00111
00112
00113 HepMC::FourVector p(px,py,pz,energy);
00114 HepMC::GenParticle* Part =
00115 new HepMC::GenParticle(p,fPartIDs[ip],1);
00116 Vtx->add_particle_out(Part);
00117 }
00118 fEvt->add_vertex( Vtx ) ;
00119 fEvt->set_event_number( fCurrentEvent+1 ) ;
00120 fEvt->set_signal_process_id(20) ;
00121
00122
00123
00124
00125 fOutStream->write_event( fEvt );
00126
00127 fCurrentEvent++ ;
00128
00129 return ;
00130
00131 }
00132