CMS 3D CMS Logo

CloseByParticleGunProducer.cc
Go to the documentation of this file.
1 #include <ostream>
2 
4 
7 
9 
15 
16 #include "CLHEP/Random/RandFlat.h"
17 #include "CLHEP/Units/GlobalSystemOfUnits.h"
18 #include "CLHEP/Units/GlobalPhysicalConstants.h"
19 #include "CLHEP/Random/RandFlat.h"
20 
21 using namespace edm;
22 using namespace std;
23 
25  ParameterSet defpset;
26  ParameterSet pgun_params = pset.getParameter<ParameterSet>("PGunParameters");
27 
28  fEnMax = pgun_params.getParameter<double>("EnMax");
29  fEnMin = pgun_params.getParameter<double>("EnMin");
30  fRMax = pgun_params.getParameter<double>("RMax");
31  fRMin = pgun_params.getParameter<double>("RMin");
32  fZMax = pgun_params.getParameter<double>("ZMax");
33  fZMin = pgun_params.getParameter<double>("ZMin");
34  fDelta = pgun_params.getParameter<double>("Delta");
35  fPhiMin = pgun_params.getParameter<double>("MinPhi");
36  fPhiMax = pgun_params.getParameter<double>("MaxPhi");
37  fPointing = pgun_params.getParameter<bool>("Pointing");
38  fOverlapping = pgun_params.getParameter<bool>("Overlapping");
39  fRandomShoot = pgun_params.getParameter<bool>("RandomShoot");
40  fNParticles = pgun_params.getParameter<int>("NParticles");
41  fPartIDs = pgun_params.getParameter<vector<int> >("PartID");
42 
43  produces<HepMCProduct>("unsmeared");
44  produces<GenEventInfoProduct>();
45 }
46 
48  // no need to cleanup GenEvent memory - done in HepMCProduct
49 }
50 
53  CLHEP::HepRandomEngine* engine = &rng->getEngine(e.streamID());
54 
55  if (fVerbosity > 0) {
56  LogDebug("CloseByParticleGunProducer") << " CloseByParticleGunProducer : Begin New Event Generation" << endl;
57  }
58  fEvt = new HepMC::GenEvent();
59 
60  // loop over particles
61  //
62  int barcode = 1;
63  int numParticles = fRandomShoot ? CLHEP::RandFlat::shoot(engine, 1, fNParticles) : fNParticles;
64  std::vector<int> particles;
65 
66  for (int i = 0; i < numParticles; i++) {
67  int partIdx = CLHEP::RandFlat::shoot(engine, 0, fPartIDs.size());
68  particles.push_back(fPartIDs[partIdx]);
69  }
70 
71  double phi = CLHEP::RandFlat::shoot(engine, fPhiMin, fPhiMax);
72  double fR = CLHEP::RandFlat::shoot(engine, fRMin, fRMax);
73  double fZ = CLHEP::RandFlat::shoot(engine, fZMin, fZMax);
74  double tmpPhi = phi;
75  double tmpR = fR;
76 
77  for (unsigned int ip = 0; ip < particles.size(); ++ip) {
78  if (fOverlapping) {
79  fR = CLHEP::RandFlat::shoot(engine, tmpR - fDelta, tmpR + fDelta);
80  phi = CLHEP::RandFlat::shoot(engine, tmpPhi - fDelta / fR, tmpPhi + fDelta / fR);
81  } else
82  phi += fDelta / fR;
83 
84  double fEn = CLHEP::RandFlat::shoot(engine, fEnMin, fEnMax);
85  int PartID = particles[ip];
86  const HepPDT::ParticleData* PData = fPDGTable->particle(HepPDT::ParticleID(abs(PartID)));
87  double mass = PData->mass().value();
88  double mom2 = fEn * fEn - mass * mass;
89  double mom = 0.;
90  if (mom2 > 0.) {
91  mom = sqrt(mom2);
92  }
93  double px = 0.;
94  double py = 0.;
95  double pz = mom;
96  double energy = fEn;
97 
98  // Compute Vertex Position
99  double x = fR * cos(phi);
100  double y = fR * sin(phi);
101  constexpr double c = 2.99792458e+1; // cm/ns
102  double timeOffset = sqrt(x * x + y * y + fZ * fZ) / c * ns * c_light;
103  HepMC::GenVertex* Vtx = new HepMC::GenVertex(HepMC::FourVector(x * cm, y * cm, fZ * cm, timeOffset));
104 
105  HepMC::FourVector p(px, py, pz, energy);
106  // If we are requested to be pointing to (0,0,0), correct the momentum direction
107  if (fPointing) {
108  math::XYZVector direction(x, y, fZ);
109  math::XYZVector momentum = direction.unit() * mom;
110  p.setX(momentum.x());
111  p.setY(momentum.y());
112  p.setZ(momentum.z());
113  }
114  HepMC::GenParticle* Part = new HepMC::GenParticle(p, PartID, 1);
115  Part->suggest_barcode(barcode);
116  barcode++;
117 
118  Vtx->add_particle_out(Part);
119 
120  if (fVerbosity > 0) {
121  Vtx->print();
122  Part->print();
123  }
124  fEvt->add_vertex(Vtx);
125  }
126 
127  fEvt->set_event_number(e.id().event());
128  fEvt->set_signal_process_id(20);
129 
130  if (fVerbosity > 0) {
131  fEvt->print();
132  }
133 
134  unique_ptr<HepMCProduct> BProduct(new HepMCProduct());
135  BProduct->addHepMCData(fEvt);
136  e.put(std::move(BProduct), "unsmeared");
137 
138  unique_ptr<GenEventInfoProduct> genEventInfo(new GenEventInfoProduct(fEvt));
139  e.put(std::move(genEventInfo));
140 
141  if (fVerbosity > 0) {
142  LogDebug("CloseByParticleGunProducer") << " CloseByParticleGunProducer : Event Generation Done " << endl;
143  }
144 
145  particles.clear();
146 }
#define LogDebug(id)
T getParameter(std::string const &) const
EventNumber_t event() const
Definition: EventID.h:40
static unsigned int partIdx(const InputGenJetsParticleSelector::ParticleVector &p, const reco::Candidate *particle)
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:131
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
virtual CLHEP::HepRandomEngine & getEngine(StreamID const &)=0
Use this engine in event methods.
ESHandle< HepPDT::ParticleDataTable > fPDGTable
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
void produce(Event &e, const EventSetup &es) override
HepPDT::ParticleData ParticleData
XYZVectorD XYZVector
spatial vector with cartesian internal representation
Definition: Vector3D.h:31
edm::EventID id() const
Definition: EventBase.h:59
HLT enums.
StreamID streamID() const
Definition: Event.h:96
def move(src, dest)
Definition: eostools.py:511
#define constexpr