CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
FlatEGunASCIIWriter.cc
Go to the documentation of this file.
1 // ----------------------------------------------------------------------
2 // FlatEGunASCIIWriter.cc
3 // Author: Julia Yarba
4 //
5 // This code has been molded after examples in HepMC and HepPDT, and
6 // after single particle gun example (private contacts with Lynn Garren)
7 //
8 // Plus, it uses the ParameterSet funtionalities for "user interface"
9 //
10 // It creates a ParticleDataTable from PDG-2004 data file.
11 // It then creates one or several HepMC particle(s) and adds it(them)
12 // to the HepMC::GenEvent.
13 // For particle definition it uses flat random gun in a given eta, phi
14 // and energy ranges to calculate particle kinematics.
15 // Vertex smearing is currently off, can be easily added later.
16 // After all, it writes out the event into ASCII output file.
17 //
18 // ----------------------------------------------------------------------
19 
20 #include <iostream>
21 #include <fstream>
22 // header
24 
25 // essentials !!!
29 
31 
32 #include "CLHEP/Random/RandFlat.h"
33 
34 using namespace edm;
35 using namespace std;
36 
38  : fEvt(0),
39  fOutFileName( pset.getUntrackedParameter<string>("OutFileName","FlatEGunHepMC.dat") ),
40  fCurrentEvent(0)
41 {
42 
43  ParameterSet pgun_params = pset.getParameter<ParameterSet>("PGunParameters") ;
44  fPartIDs = pgun_params.getParameter< vector<int> >("PartID");
45  fMinEta = pgun_params.getParameter<double>("MinEta");
46  fMaxEta = pgun_params.getParameter<double>("MaxEta");
47  fMinPhi = pgun_params.getParameter<double>("MinPhi");
48  fMaxPhi = pgun_params.getParameter<double>("MaxPhi");
49  fMinE = pgun_params.getParameter<double>("MinE");
50  fMaxE = pgun_params.getParameter<double>("MaxE");
51 
52 }
53 
55 {
56 
57  if ( fEvt != NULL ) delete fEvt ;
58  if( fOutStream) delete fOutStream;
59 
60 }
61 
63 {
64 
65  es.getData( fPDGTable );
66 
67 }
68 
70 {
71  fOutStream = new HepMC::IO_GenEvent( fOutFileName.c_str() );
72  if ( fOutStream->rdstate() == std::ios::failbit ) {
73  throw cms::Exception("FileNotOpen", "FlatEGunASCIIWriter::beginJob()")
74  << "File " << fOutFileName << " was not open.\n";
75  }
76 
77  return ;
78 
79 }
80 
82  const EventSetup& /* not used so far, thus is "empty" */ )
83 {
84 
85  // clean up GenEvent memory : also deletes all vtx/part in it
86  //
87  if ( fEvt != NULL ) delete fEvt ;
88 
89  // here re-create fEvt (memory)
90  fEvt = new HepMC::GenEvent() ;
91 
92  // now actualy, cook up the event from PDGTable and gun parameters
93  //
94  //HepMC::GenVertex* Vtx = new HepMC::GenVertex( CLHEP::HepLorentzVector(0.,0.,0.) );
95  HepMC::GenVertex* Vtx = new HepMC::GenVertex( HepMC::FourVector(0.,0.,0.) );
96 
97  // loop over particles
98  //
99  for ( unsigned int ip=0; ip<fPartIDs.size(); ip++ )
100  {
101  double energy = CLHEP::RandFlat::shoot( fMinE, fMaxE ) ;
102  double eta = CLHEP::RandFlat::shoot( fMinEta, fMaxEta ) ;
103  double phi = CLHEP::RandFlat::shoot( fMinPhi, fMaxPhi ) ;
104  const HepPDT::ParticleData*
105  PData = fPDGTable->particle(HepPDT::ParticleID(abs(fPartIDs[ip]))) ;
106  double mass = PData->mass().value() ;
107  double mom2 = energy*energy - mass*mass ;
108  double mom = 0. ;
109  if ( mom2 > 0. ) mom = sqrt(mom2) ;
110  double theta = 2.*atan(exp(-eta)) ;
111  double px = mom*sin(theta)*cos(phi) ;
112  double py = mom*sin(theta)*sin(phi) ;
113  double pz = mom*cos(theta) ;
114  //CLHEP::Hep3Vector p(px,py,pz) ;
115  //HepMC::GenParticle* Part =
116  // new HepMC::GenParticle(CLHEP::HepLorentzVector(p,energy),fPartIDs[ip],1);
117  HepMC::FourVector p(px,py,pz,energy);
118  HepMC::GenParticle* Part =
119  new HepMC::GenParticle(p,fPartIDs[ip],1);
120  Vtx->add_particle_out(Part);
121  }
122  fEvt->add_vertex( Vtx ) ;
123  fEvt->set_event_number( fCurrentEvent+1 ) ;
124  fEvt->set_signal_process_id(20) ;
125 
126  // for testing purpose only
127  // fEvt->print() ;
128 
129  fOutStream->write_event( fEvt );
130 
131  fCurrentEvent++ ;
132 
133  return ;
134 
135 }
136 
T getParameter(std::string const &) const
virtual void beginRun(const edm::Run &, const EventSetup &) override
virtual void analyze(const edm::Event &, const edm::EventSetup &) override
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
Geom::Theta< T > theta() const
#define NULL
Definition: scimark2.h:8
std::vector< int > fPartIDs
T eta() const
void getData(T &iHolder) const
Definition: EventSetup.h:67
return((rh^lh)&mask)
ESHandle< HepPDT::ParticleDataTable > fPDGTable
T sqrt(T t)
Definition: SSEVec.h:48
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
HepPDT::ParticleData ParticleData
FlatEGunASCIIWriter(const edm::ParameterSet &)
HepMC::IO_GenEvent * fOutStream
Definition: Run.h:41
Definition: DDAxes.h:10