CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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(nullptr),
39  fOutFileName(pset.getUntrackedParameter<string>("OutFileName", "FlatEGunHepMC.dat")),
40  fCurrentEvent(0) {
41  ParameterSet pgun_params = pset.getParameter<ParameterSet>("PGunParameters");
42  fPartIDs = pgun_params.getParameter<vector<int> >("PartID");
43  fMinEta = pgun_params.getParameter<double>("MinEta");
44  fMaxEta = pgun_params.getParameter<double>("MaxEta");
45  fMinPhi = pgun_params.getParameter<double>("MinPhi");
46  fMaxPhi = pgun_params.getParameter<double>("MaxPhi");
47  fMinE = pgun_params.getParameter<double>("MinE");
48  fMaxE = pgun_params.getParameter<double>("MaxE");
49 }
50 
52  if (fEvt != nullptr)
53  delete fEvt;
54  if (fOutStream)
55  delete fOutStream;
56 }
57 
59 
61  fOutStream = new HepMC::IO_GenEvent(fOutFileName);
62  if (fOutStream->rdstate() == std::ios::failbit) {
63  throw cms::Exception("FileNotOpen", "FlatEGunASCIIWriter::beginJob()")
64  << "File " << fOutFileName << " was not open.\n";
65  }
66 
67  return;
68 }
69 
70 void FlatEGunASCIIWriter::analyze(const Event&, const EventSetup& /* not used so far, thus is "empty" */) {
71  // clean up GenEvent memory : also deletes all vtx/part in it
72  //
73  if (fEvt != nullptr)
74  delete fEvt;
75 
76  // here re-create fEvt (memory)
77  fEvt = new HepMC::GenEvent();
78 
79  // now actualy, cook up the event from PDGTable and gun parameters
80  //
81  //HepMC::GenVertex* Vtx = new HepMC::GenVertex( CLHEP::HepLorentzVector(0.,0.,0.) );
82  HepMC::GenVertex* Vtx = new HepMC::GenVertex(HepMC::FourVector(0., 0., 0.));
83 
84  // loop over particles
85  //
86  for (unsigned int ip = 0; ip < fPartIDs.size(); ip++) {
87  double energy = CLHEP::RandFlat::shoot(fMinE, fMaxE);
88  double eta = CLHEP::RandFlat::shoot(fMinEta, fMaxEta);
89  double phi = CLHEP::RandFlat::shoot(fMinPhi, fMaxPhi);
90  const HepPDT::ParticleData* PData = fPDGTable->particle(HepPDT::ParticleID(abs(fPartIDs[ip])));
91  double mass = PData->mass().value();
92  double mom2 = energy * energy - mass * mass;
93  double mom = 0.;
94  if (mom2 > 0.)
95  mom = sqrt(mom2);
96  double theta = 2. * atan(exp(-eta));
97  double px = mom * sin(theta) * cos(phi);
98  double py = mom * sin(theta) * sin(phi);
99  double pz = mom * cos(theta);
100  //CLHEP::Hep3Vector p(px,py,pz) ;
101  //HepMC::GenParticle* Part =
102  // new HepMC::GenParticle(CLHEP::HepLorentzVector(p,energy),fPartIDs[ip],1);
103  HepMC::FourVector p(px, py, pz, energy);
104  HepMC::GenParticle* Part = new HepMC::GenParticle(p, fPartIDs[ip], 1);
105  Vtx->add_particle_out(Part);
106  }
107  fEvt->add_vertex(Vtx);
108  fEvt->set_event_number(fCurrentEvent + 1);
109  fEvt->set_signal_process_id(20);
110 
111  // for testing purpose only
112  // fEvt->print() ;
113 
114  fOutStream->write_event(fEvt);
115 
116  fCurrentEvent++;
117 
118  return;
119 }
void beginRun(const edm::Run &, const EventSetup &) override
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
Exp< T >::type exp(const T &t)
Definition: Exp.h:22
std::vector< int > fPartIDs
ESHandle< HepPDT::ParticleDataTable > fPDGTable
bool getData(T &iHolder) const
Definition: EventSetup.h:128
T sqrt(T t)
Definition: SSEVec.h:19
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
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
Definition: Run.h:45