CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 pgun_params = pset.getParameter<ParameterSet>("PGunParameters");
26  fControlledByEta = pgun_params.getParameter<bool>("ControlledByEta");
27  fEnMax = pgun_params.getParameter<double>("EnMax");
28  fEnMin = pgun_params.getParameter<double>("EnMin");
29  fMaxEnSpread = pgun_params.getParameter<bool>("MaxEnSpread");
30  if (fControlledByEta) {
31  fEtaMax = pgun_params.getParameter<double>("MaxEta");
32  fEtaMin = pgun_params.getParameter<double>("MinEta");
33  if (fEtaMax <= fEtaMin)
34  LogError("CloseByParticleGunProducer") << " Please fix MinEta and MaxEta values in the configuration";
35  } else {
36  fRMax = pgun_params.getParameter<double>("RMax");
37  fRMin = pgun_params.getParameter<double>("RMin");
38  if (fRMax <= fRMin)
39  LogError("CloseByParticleGunProducer") << " Please fix RMin and RMax values in the configuration";
40  }
41  fZMax = pgun_params.getParameter<double>("ZMax");
42  fZMin = pgun_params.getParameter<double>("ZMin");
43  fDelta = pgun_params.getParameter<double>("Delta");
44  fPhiMin = pgun_params.getParameter<double>("MinPhi");
45  fPhiMax = pgun_params.getParameter<double>("MaxPhi");
46  fPointing = pgun_params.getParameter<bool>("Pointing");
47  fOverlapping = pgun_params.getParameter<bool>("Overlapping");
48  fRandomShoot = pgun_params.getParameter<bool>("RandomShoot");
49  fNParticles = pgun_params.getParameter<int>("NParticles");
50  fPartIDs = pgun_params.getParameter<vector<int> >("PartID");
51 
52  produces<HepMCProduct>("unsmeared");
53  produces<GenEventInfoProduct>();
54 }
55 
57  // no need to cleanup GenEvent memory - done in HepMCProduct
58 }
59 
62  CLHEP::HepRandomEngine* engine = &rng->getEngine(e.streamID());
63 
64  if (fVerbosity > 0) {
65  LogDebug("CloseByParticleGunProducer") << " CloseByParticleGunProducer : Begin New Event Generation" << endl;
66  }
67  fEvt = new HepMC::GenEvent();
68 
69  int barcode = 1;
70  unsigned int numParticles = fRandomShoot ? CLHEP::RandFlat::shoot(engine, 1, fNParticles) : fNParticles;
71 
72  double phi = CLHEP::RandFlat::shoot(engine, fPhiMin, fPhiMax);
73  double fZ = CLHEP::RandFlat::shoot(engine, fZMin, fZMax);
74  double fR;
75 
76  if (!fControlledByEta) {
77  fR = CLHEP::RandFlat::shoot(engine, fRMin, fRMax);
78  } else {
79  double fEta = CLHEP::RandFlat::shoot(engine, fEtaMin, fEtaMax);
80  fR = (fZ / sinh(fEta));
81  }
82 
83  double tmpPhi = phi;
84  double tmpR = fR;
85 
86  // Loop over particles
87  for (unsigned int ip = 0; ip < numParticles; ++ip) {
88  if (fOverlapping) {
89  fR = CLHEP::RandFlat::shoot(engine, tmpR - fDelta, tmpR + fDelta);
90  phi = CLHEP::RandFlat::shoot(engine, tmpPhi - fDelta / fR, tmpPhi + fDelta / fR);
91  } else
92  phi += fDelta / fR;
93 
94  double fEn;
95  if (numParticles > 1 && fMaxEnSpread)
96  fEn = fEnMin + ip * (fEnMax - fEnMin) / (numParticles - 1);
97  else
98  fEn = CLHEP::RandFlat::shoot(engine, fEnMin, fEnMax);
99 
100  int partIdx = CLHEP::RandFlat::shoot(engine, 0, fPartIDs.size());
101  int PartID = fPartIDs[partIdx];
102  const HepPDT::ParticleData* PData = fPDGTable->particle(HepPDT::ParticleID(abs(PartID)));
103  double mass = PData->mass().value();
104  double mom2 = fEn * fEn - mass * mass;
105  double mom = 0.;
106  if (mom2 > 0.) {
107  mom = sqrt(mom2);
108  }
109  double px = 0.;
110  double py = 0.;
111  double pz = mom;
112  double energy = fEn;
113 
114  // Compute Vertex Position
115  double x = fR * cos(phi);
116  double y = fR * sin(phi);
117  constexpr double c = 2.99792458e+1; // cm/ns
118  double timeOffset = sqrt(x * x + y * y + fZ * fZ) / c * ns * c_light;
119  HepMC::GenVertex* Vtx = new HepMC::GenVertex(HepMC::FourVector(x * cm, y * cm, fZ * cm, timeOffset));
120 
121  HepMC::FourVector p(px, py, pz, energy);
122  // If we are requested to be pointing to (0,0,0), correct the momentum direction
123  if (fPointing) {
124  math::XYZVector direction(x, y, fZ);
125  math::XYZVector momentum = direction.unit() * mom;
126  p.setX(momentum.x());
127  p.setY(momentum.y());
128  p.setZ(momentum.z());
129  }
130  HepMC::GenParticle* Part = new HepMC::GenParticle(p, PartID, 1);
131  Part->suggest_barcode(barcode);
132  barcode++;
133 
134  Vtx->add_particle_out(Part);
135 
136  if (fVerbosity > 0) {
137  Vtx->print();
138  Part->print();
139  }
140  fEvt->add_vertex(Vtx);
141  }
142 
143  fEvt->set_event_number(e.id().event());
144  fEvt->set_signal_process_id(20);
145 
146  if (fVerbosity > 0) {
147  fEvt->print();
148  }
149 
150  unique_ptr<HepMCProduct> BProduct(new HepMCProduct());
151  BProduct->addHepMCData(fEvt);
152  e.put(std::move(BProduct), "unsmeared");
153 
154  unique_ptr<GenEventInfoProduct> genEventInfo(new GenEventInfoProduct(fEvt));
155  e.put(std::move(genEventInfo));
156 
157  if (fVerbosity > 0) {
158  LogDebug("CloseByParticleGunProducer") << " CloseByParticleGunProducer : Event Generation Done " << endl;
159  }
160 }
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:133
const edm::EventSetup & c
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
Log< level::Error, false > LogError
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
def move
Definition: eostools.py:511
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
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
edm::EventID id() const
Definition: EventBase.h:59
StreamID streamID() const
Definition: Event.h:98
float x
#define LogDebug(id)