CMS 3D CMS Logo

FlatEGunASCIIWriter.cc

Go to the documentation of this file.
00001 // ----------------------------------------------------------------------
00002 // FlatEGunASCIIWriter.cc
00003 // Author: Julia Yarba
00004 //
00005 // This code has been molded after examples in HepMC and HepPDT, and
00006 // after single particle gun example (private contacts with Lynn Garren)
00007 //
00008 // Plus, it uses the ParameterSet funtionalities for "user interface"  
00009 //
00010 // It creates a ParticleDataTable from PDG-2004 data file.
00011 // It then creates one or several HepMC particle(s) and adds it(them) 
00012 // to the HepMC::GenEvent.
00013 // For particle definition it uses flat random gun in a given eta, phi 
00014 // and energy ranges to calculate particle kinematics.
00015 // Vertex smearing is currently off, can be easily added later.
00016 // After all, it writes out the event into ASCII output file.
00017 //
00018 // ----------------------------------------------------------------------
00019 
00020 #include <iostream>
00021 #include <fstream>
00022 // header
00023 #include "IOMC/ParticleGuns/interface/FlatEGunASCIIWriter.h"
00024 
00025 // essentials !!!
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& /* not used so far, thus is "empty" */ ) 
00079 {
00080          
00081    // clean up GenEvent memory : also deletes all vtx/part in it
00082    // 
00083    if ( fEvt != NULL ) delete fEvt ;
00084    
00085    // here re-create fEvt (memory)
00086    fEvt = new HepMC::GenEvent() ;
00087 
00088    // now actualy, cook up the event from PDGTable and gun parameters
00089    //
00090    //HepMC::GenVertex* Vtx = new HepMC::GenVertex( CLHEP::HepLorentzVector(0.,0.,0.) );
00091    HepMC::GenVertex* Vtx = new HepMC::GenVertex( HepMC::FourVector(0.,0.,0.) );
00092 
00093    // loop over particles
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        //CLHEP::Hep3Vector p(px,py,pz) ;
00111        //HepMC::GenParticle* Part = 
00112        //    new HepMC::GenParticle(CLHEP::HepLorentzVector(p,energy),fPartIDs[ip],1);
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    // for testing purpose only
00123    // fEvt->print() ;     
00124 
00125    fOutStream->write_event( fEvt );
00126 
00127    fCurrentEvent++ ;
00128 
00129    return ;
00130    
00131 }
00132 

Generated on Tue Jun 9 17:39:07 2009 for CMSSW by  doxygen 1.5.4