CMS 3D CMS Logo

MultiParticleInConeGunProducer.cc
Go to the documentation of this file.
1 /*
2  * \author Jean-Roch Vlimant
3  */
4 
5 #include <ostream>
6 
8 
11 
16 
17 #include "CLHEP/Random/RandFlat.h"
18 
19 using namespace edm;
20 using namespace std;
21 
23  ParameterSet defpset;
24  ParameterSet pgun_params = pset.getParameter<ParameterSet>("PGunParameters");
25 
26  fMinPt = pgun_params.getParameter<double>("MinPt");
27  fMaxPt = pgun_params.getParameter<double>("MaxPt");
28 
29  fInConeIds = pgun_params.getParameter<vector<int> >("InConeID");
30  fMinDeltaR = pgun_params.getParameter<double>("MinDeltaR");
31  fMaxDeltaR = pgun_params.getParameter<double>("MaxDeltaR");
32  fMinMomRatio = pgun_params.getParameter<double>("MinMomRatio");
33  fMaxMomRatio = pgun_params.getParameter<double>("MaxMomRatio");
34 
35  fInConeMinEta = pgun_params.getParameter<double>("InConeMinEta");
36  fInConeMaxEta = pgun_params.getParameter<double>("InConeMaxEta");
37  fInConeMinPhi = pgun_params.getParameter<double>("InConeMinPhi");
38  fInConeMaxPhi = pgun_params.getParameter<double>("InConeMaxPhi");
39  fInConeMaxTry = pgun_params.getParameter<unsigned int>("InConeMaxTry");
40 
41  produces<HepMCProduct>("unsmeared");
42  produces<GenEventInfoProduct>();
43 }
44 
46  // no need to cleanup GenEvent memory - done in HepMCProduct
47 }
48 
51  CLHEP::HepRandomEngine* engine = &rng->getEngine(e.streamID());
52 
53  if (fVerbosity > 0) {
54  cout << " MultiParticleInConeGunProducer : Begin New Event Generation" << endl;
55  }
56  // event loop (well, another step in it...)
57 
58  // no need to clean up GenEvent memory - done in HepMCProduct
59  //
60 
61  // here re-create fEvt (memory)
62  //
63  fEvt = new HepMC::GenEvent();
64 
65  // now actualy, cook up the event from PDGTable and gun parameters
66  //
67  // 1st, primary vertex
68  //
69  //HepMC::GenVertex* Vtx = new HepMC::GenVertex(CLHEP::HepLorentzVector(0.,0.,0.));
70  HepMC::GenVertex* Vtx = new HepMC::GenVertex(HepMC::FourVector(0., 0., 0.));
71 
72  // loop over particles
73  //
74  int barcode = 1;
75  for (unsigned int ip = 0; ip < fPartIDs.size(); ++ip) {
76  double pt = CLHEP::RandFlat::shoot(engine, fMinPt, fMaxPt);
77  double eta = CLHEP::RandFlat::shoot(engine, fMinEta, fMaxEta);
78  double phi = CLHEP::RandFlat::shoot(engine, fMinPhi, fMaxPhi);
79  int PartID = fPartIDs[ip];
80  const HepPDT::ParticleData* PData = fPDGTable->particle(HepPDT::ParticleID(abs(PartID)));
81  double mass = PData->mass().value();
82  double theta = 2. * atan(exp(-eta));
83  double mom = pt / sin(theta);
84  double px = pt * cos(phi);
85  double py = pt * sin(phi);
86  double pz = mom * cos(theta);
87  double energy2 = mom * mom + mass * mass;
88  double energy = sqrt(energy2);
89 
90  HepMC::FourVector p(px, py, pz, energy);
92  Part->suggest_barcode(barcode);
93  barcode++;
94  Vtx->add_particle_out(Part);
95 
96  if (fAddAntiParticle) {
97  }
98 
99  // now add the particles in the cone
100  for (unsigned iPic = 0; iPic != fInConeIds.size(); iPic++) {
101  unsigned int nTry = 0;
102  while (true) {
103  //shoot flat Deltar
104  double dR = CLHEP::RandFlat::shoot(engine, fMinDeltaR, fMaxDeltaR);
105  //shoot flat eta/phi mixing
106  double alpha = CLHEP::RandFlat::shoot(engine, -3.14159265358979323846, 3.14159265358979323846);
107  double dEta = dR * cos(alpha);
108  double dPhi = dR * sin(alpha);
109 
110  /*
111  //shoot Energy of associated particle
112  double energyIc = CLHEP::RandFlat::shoot(engine, fMinEInCone, fMaxEInCone);
113  if (mom2Ic>0){ momIC = sqrt(mom2Ic);}
114  */
115  // get kinematics
116  double etaIc = eta + dEta;
117  double phiIc = phi + dPhi;
118  //put it back in -Pi:Pi if necessary. multiple time might be necessary if dR > 3
119  const unsigned int maxL = 100;
120  unsigned int iL = 0;
121  while (iL++ < maxL) {
122  if (phiIc > 3.14159265358979323846)
123  phiIc -= 2 * 3.14159265358979323846;
124  else if (phiIc < -3.14159265358979323846)
125  phiIc += 2 * 3.14159265358979323846;
126 
127  if (abs(phiIc) < 3.14159265358979323846)
128  break;
129  }
130 
131  //allow to skip it if you did not run out of possible drawing
132  if (nTry++ <= fInConeMaxTry) {
133  //draw another one if this one is not in acceptance
134  if (etaIc < fInConeMinEta || etaIc > fInConeMaxEta)
135  continue;
136  if (phiIc < fInConeMinPhi || phiIc > fInConeMaxPhi)
137  continue;
138  } else {
139  if (fVerbosity > 0) {
140  cout << " MultiParticleInConeGunProducer : could not produce a particle " << fInConeIds[iPic] << " in cone "
141  << fMinDeltaR << " to " << fMaxDeltaR << " within the " << fInConeMaxTry << " allowed tries." << endl;
142  }
143  /* edm::LogError("MultiParticleInConeGunProducer")<< " MultiParticleInConeGunProducer : could not produce a particle "<<
144  fInConeIds[iPic]<<" in cone "<<
145  fMinDeltaR<<" to "<<fMaxDeltaR<<" within the "<<fInConeMaxTry<<" allowed tries.";*/
146  }
147  int PartIDIc = fInConeIds[iPic];
148  const HepPDT::ParticleData* PDataIc = fPDGTable->particle(HepPDT::ParticleID(abs(PartIDIc)));
149 
150  //shoot momentum ratio
151  double momR = CLHEP::RandFlat::shoot(engine, fMinMomRatio, fMaxMomRatio);
152  double massIc = PDataIc->mass().value();
153  double momIc = momR * mom;
154  double energyIc = sqrt(momIc * momIc + massIc * massIc);
155 
156  double thetaIc = 2. * atan(exp(-etaIc));
157  double pxIc = momIc * sin(thetaIc) * cos(phiIc);
158  double pyIc = momIc * sin(thetaIc) * sin(phiIc);
159  double pzIc = momIc * cos(thetaIc);
160 
161  HepMC::FourVector pIc(pxIc, pyIc, pzIc, energyIc);
162  HepMC::GenParticle* PartIc = new HepMC::GenParticle(pIc, PartIDIc, 1);
163  PartIc->suggest_barcode(barcode);
164  barcode++;
165  Vtx->add_particle_out(PartIc);
166  break;
167  } //try many times while not in acceptance
168  } //loop over the particle Ids in the cone
169  }
170 
171  fEvt->add_vertex(Vtx);
172  fEvt->set_event_number(e.id().event());
173  fEvt->set_signal_process_id(20);
174 
175  if (fVerbosity > 0) {
176  fEvt->print();
177  }
178 
179  unique_ptr<HepMCProduct> BProduct(new HepMCProduct());
180  BProduct->addHepMCData(fEvt);
181  e.put(std::move(BProduct), "unsmeared");
182 
183  unique_ptr<GenEventInfoProduct> genEventInfo(new GenEventInfoProduct(fEvt));
184  e.put(std::move(genEventInfo));
185 
186  if (fVerbosity > 0) {
187  cout << " MultiParticleInConeGunProducer : Event Generation Done " << endl;
188  }
189 }
float alpha
Definition: AMPTWrapper.h:105
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
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
void produce(Event &e, const EventSetup &es) override
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
std::vector< int > fPartIDs
HepPDT::ParticleData ParticleData
HLT enums.
Geom::Theta< T > theta() const
def move(src, dest)
Definition: eostools.py:511