CMS 3D CMS Logo

RandomMultiParticlePGunProducer.cc
Go to the documentation of this file.
1 #include <ostream>
2 
4 
7 
14 #include "CLHEP/Random/RandFlat.h"
15 
16 //#define DebugLog
17 using namespace edm;
18 
20  ParameterSet pgunParams = pset.getParameter<ParameterSet>("PGunParameters");
21  fProbParticle_ = pgunParams.getParameter<std::vector<double> >("ProbParts");
22  fProbP_ = pgunParams.getParameter<std::vector<double> >("ProbP");
23  fMinP_ = pgunParams.getParameter<double>("MinP");
24  fMaxP_ = pgunParams.getParameter<double>("MaxP");
25  if (fProbParticle_.size() != fPartIDs.size())
26  throw cms::Exception("Configuration") << "Not all probabilities given for all particle types "
27  << fProbParticle_.size() << ":" << fPartIDs.size() << " need them to match\n";
28 
29  produces<HepMCProduct>("unsmeared");
30  produces<GenEventInfoProduct>();
31  fBinsP_ = (int)(fProbP_.size());
32  fDeltaP_ = (fMaxP_ - fMinP_) / fBinsP_;
33 #ifdef DebugLog
34  edm::LogVerbatim("IOMC") << "Internal FlatRandomPGun is initialzed for " << fPartIDs.size() << " particles in "
35  << fBinsP_ << " bins within momentum range " << fMinP_ << ":" << fMaxP_;
36  for (unsigned int k = 0; k < fPartIDs.size(); ++k)
37  edm::LogVerbatim("IOMC") << " [" << k << "] " << fPartIDs[k] << ":" << fProbParticle_[k];
38  edm::LogVerbatim("IOMC") << "Momentum distribution is given by";
39  for (int k = 0; k < fBinsP_; ++k) {
40  double p = fMinP_ + k * fDeltaP_;
41  edm::LogVerbatim("IOMC") << " Bin[" << k << "] " << p << ":" << p + fDeltaP_ << " --> " << fProbP_[k];
42  }
43 #endif
44  for (unsigned int k = 1; k < fProbParticle_.size(); ++k)
45  fProbParticle_[k] += fProbParticle_[k - 1];
46  for (unsigned int k = 0; k < fProbParticle_.size(); ++k)
47  fProbParticle_[k] /= fProbParticle_[fProbParticle_.size() - 1];
48 #ifdef DebugLog
49  edm::LogVerbatim("IOMC") << "Corrected probabilities for particle type:";
50  for (unsigned int k = 0; k < fProbParticle_.size(); ++k)
51  edm::LogVerbatim("IOMC") << " [" << k << "]: " << fProbParticle_[k];
52 #endif
53  for (int k = 1; k < fBinsP_; ++k)
54  fProbP_[k] += fProbP_[k - 1];
55  for (int k = 0; k < fBinsP_; ++k)
56  fProbP_[k] /= fProbP_[fBinsP_ - 1];
57 #ifdef DebugLog
58  edm::LogVerbatim("IOMC") << "Corrected probabilities for momentum:";
59  for (int k = 0; k < fBinsP_; ++k) {
60  double p = fMinP_ + k * fDeltaP_;
61  edm::LogVerbatim("IOMC") << " Bin[" << k << "] " << p << ":" << p + fDeltaP_ << " --> " << fProbP_[k];
62  }
63 #endif
64 }
65 
68  CLHEP::HepRandomEngine* engine = &rng->getEngine(e.streamID());
69 
70 #ifdef DebugLog
71  if (fVerbosity > 0)
72  edm::LogVerbatim("IOMC") << "RandomMultiParticlePGunProducer: "
73  << "Begin New Event Generation";
74 #endif
75 
76  // event loop (well, another step in it...)
77  // no need to clean up GenEvent memory - done in HepMCProduct
78  // here re-create fEvt (memory)
79  //
80  fEvt = new HepMC::GenEvent();
81 
82  // now actualy, cook up the event from PDGTable and gun parameters
83  //
84 
85  // 1st, primary vertex
86  //
87  HepMC::GenVertex* Vtx = new HepMC::GenVertex(HepMC::FourVector(0., 0., 0.));
88 
89  // loop over particles
90  //
91  int barcode(0), PartID(fPartIDs[0]);
92  double r1 = CLHEP::RandFlat::shoot(engine, 0., 1.);
93  for (unsigned int ip = 0; ip < fPartIDs.size(); ip++) {
94  if (r1 <= fProbParticle_[ip]) {
95  PartID = fPartIDs[ip];
96  break;
97  }
98  }
99  double minP(fMinP_);
100  double r2 = CLHEP::RandFlat::shoot(engine, 0., 1.);
101  for (int ip = 0; ip < fBinsP_; ip++) {
102  if (r2 <= fProbP_[ip]) {
103  minP = fMinP_ + ip * fDeltaP_;
104  break;
105  }
106  }
107  double maxP = minP + fDeltaP_;
108 #ifdef DebugLog
109  if (fVerbosity > 0)
110  edm::LogVerbatim("IOMC") << "Random " << r1 << " PartID " << PartID << " and for p " << r2 << " range " << minP
111  << ":" << maxP;
112 #endif
113  double mom = CLHEP::RandFlat::shoot(engine, minP, maxP);
114  double eta = CLHEP::RandFlat::shoot(engine, fMinEta, fMaxEta);
115  double phi = CLHEP::RandFlat::shoot(engine, fMinPhi, fMaxPhi);
116  const HepPDT::ParticleData* PData = fPDGTable->particle(HepPDT::ParticleID(abs(PartID)));
117  double mass = PData->mass().value();
118  double energy = sqrt(mom * mom + mass * mass);
119  double theta = 2. * atan(exp(-eta));
120  double px = mom * sin(theta) * cos(phi);
121  double py = mom * sin(theta) * sin(phi);
122  double pz = mom * cos(theta);
123 
124  HepMC::FourVector p(px, py, pz, energy);
125  HepMC::GenParticle* Part = new HepMC::GenParticle(p, PartID, 1);
126  barcode++;
127  Part->suggest_barcode(barcode);
128  Vtx->add_particle_out(Part);
129 
130  if (fAddAntiParticle) {
131  HepMC::FourVector ap(-px, -py, -pz, energy);
132  int APartID = (PartID == 22 || PartID == 23) ? PartID : -PartID;
133  HepMC::GenParticle* APart = new HepMC::GenParticle(ap, APartID, 1);
134  barcode++;
135  APart->suggest_barcode(barcode);
136  Vtx->add_particle_out(APart);
137  }
138 
139  fEvt->add_vertex(Vtx);
140  fEvt->set_event_number(e.id().event());
141  fEvt->set_signal_process_id(20);
142 
143 #ifdef DebugLog
144  if (fVerbosity > 0)
145  fEvt->print();
146 #endif
147 
148  std::unique_ptr<HepMCProduct> BProduct(new HepMCProduct());
149  BProduct->addHepMCData(fEvt);
150  e.put(std::move(BProduct), "unsmeared");
151 
152  std::unique_ptr<GenEventInfoProduct> genEventInfo(new GenEventInfoProduct(fEvt));
153  e.put(std::move(genEventInfo));
154 #ifdef DebugLog
155  if (fVerbosity > 0)
156  edm::LogVerbatim("IOMC") << " RandomMultiParticlePGunProducer : Event Generation Done ";
157 #endif
158 }
T getParameter(std::string const &) const
EventNumber_t event() const
Definition: EventID.h:40
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
Geom::Theta< T > theta() const
virtual CLHEP::HepRandomEngine & getEngine(StreamID const &)=0
Use this engine in event methods.
void produce(Event &e, const EventSetup &es) override
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
std::vector< int > fPartIDs
HepPDT::ParticleData ParticleData
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