CMS 3D CMS Logo

FileRandomMultiParticlePGunProducer.cc
Go to the documentation of this file.
1 #include <ostream>
2 
4 
7 
14 #include "CLHEP/Random/RandFlat.h"
15 
16 using namespace edm;
17 
18 const unsigned int np = 6;
19 const unsigned int kfactor = 100;
20 
23  ParameterSet pgunParams = pset.getParameter<ParameterSet>("PGunParameters");
24  fMinP_ = pgunParams.getParameter<double>("MinP");
25  fMaxP_ = pgunParams.getParameter<double>("MaxP");
26  edm::FileInPath fp = pgunParams.getParameter<edm::FileInPath>("FileName");
27  std::string file = fp.fullPath();
28 
29  produces<HepMCProduct>("unsmeared");
30  produces<GenEventInfoProduct>();
31  edm::LogVerbatim("ParticleGun") << "FileRandomMultiParticlePGun is initialzed with i/p file " << file
32  << " and use momentum range " << fMinP_ << ":" << fMaxP_;
33 
34  if (fPartIDs.size() != np)
35  throw cms::Exception("ParticleGun") << "Invalid list of partices: " << fPartIDs.size() << " should be " << np
36  << "\n";
37 
38  std::ifstream is(file.c_str(), std::ios::in);
39  if (!is) {
40  throw cms::Exception("Configuration") << "Cannot find the file " << file << "\n";
41  } else {
42  double xl, xh;
43  is >> fPBin_ >> xl >> xh >> fEtaBin_ >> fEtaMin_ >> fEtaBinWidth_;
44  fP_.emplace_back(xl);
45  edm::LogVerbatim("ParticleGun") << "FileRandomMultiParticlePGun: p: " << fPBin_ << ":" << xl << ":" << xh
46  << " Eta: " << fEtaBin_ << ":" << fEtaMin_ << ":" << fEtaBinWidth_;
47  for (int ip = 0; ip < fPBin_; ++ip) {
48  for (int ie = 0; ie < fEtaBin_; ++ie) {
49  double totprob(0);
50  std::vector<double> prob(np, 0);
51  int je;
52  is >> xl >> xh >> je >> prob[0] >> prob[1] >> prob[2] >> prob[3] >> prob[4] >> prob[5];
53  if (ie == 0)
54  fP_.emplace_back(xh);
55  for (unsigned int k = 0; k < np; ++k) {
56  totprob += prob[k];
57  if (k > 0)
58  prob[k] += prob[k - 1];
59  }
60  for (unsigned int k = 0; k < np; ++k)
61  prob[k] /= totprob;
62  int indx = (ip + 1) * kfactor + ie;
63  fProbParticle_[indx] = prob;
64  if (fVerbosity > 0)
65  edm::LogVerbatim("ParticleGun")
66  << "FileRandomMultiParticlePGun [" << ip << "," << ie << ", " << indx << "] Probability " << prob[0]
67  << ", " << prob[1] << ", " << prob[2] << ", " << prob[3] << ", " << prob[4] << ", " << prob[5];
68  }
69  }
70  is.close();
71  }
72 }
73 
75 
78  CLHEP::HepRandomEngine* engine = &rng->getEngine(e.streamID());
79 
80  if (fVerbosity > 0)
81  edm::LogVerbatim("ParticleGun") << "FileRandomMultiParticlePGunProducer: Begin New Event Generation";
82 
83  // event loop (well, another step in it...)
84  // no need to clean up GenEvent memory - done in HepMCProduct
85  // here re-create fEvt (memory)
86  //
87  fEvt = new HepMC::GenEvent();
88 
89  // now actualy, cook up the event from PDGTable and gun parameters
90  //
91 
92  // 1st, primary vertex
93  //
94  HepMC::GenVertex* Vtx = new HepMC::GenVertex(HepMC::FourVector(0., 0., 0.));
95 
96  // Now p, eta, phi
97  double mom = CLHEP::RandFlat::shoot(engine, fMinP_, fMaxP_);
98  double eta = CLHEP::RandFlat::shoot(engine, fMinEta, fMaxEta);
99  double phi = CLHEP::RandFlat::shoot(engine, fMinPhi, fMaxPhi);
100  int ieta = static_cast<int>((eta - fEtaMin_) / fEtaBinWidth_);
101  auto ipp = std::lower_bound(fP_.begin(), fP_.end(), mom);
102  if (ipp == fP_.end())
103  --ipp;
104  int ip = static_cast<int>(ipp - fP_.begin());
105  int indx = ip * kfactor + ieta;
106  if (fVerbosity > 0)
107  edm::LogVerbatim("ParticleGun") << "FileRandomMultiParticlePGunProducer: p " << mom << " Eta " << eta << " Phi "
108  << phi << " Index " << indx;
109 
110  // Now particle id
111  //
112  int barcode(0), partID(fPartIDs[0]);
113  double r1 = CLHEP::RandFlat::shoot(engine, 0., 1.);
114  for (unsigned int ip = 0; ip < fPartIDs.size(); ip++) {
115  if (r1 <= fProbParticle_[indx][ip])
116  break;
117  partID = fPartIDs[ip];
118  }
119  if (fVerbosity > 0)
120  edm::LogVerbatim("ParticleGun") << "Random " << r1 << " PartID " << partID;
121  const HepPDT::ParticleData* PData = fPDGTable->particle(HepPDT::ParticleID(partID));
122  double mass = PData->mass().value();
123  double energy = sqrt(mom * mom + mass * mass);
124  double theta = 2. * atan(exp(-eta));
125  double px = mom * sin(theta) * cos(phi);
126  double py = mom * sin(theta) * sin(phi);
127  double pz = mom * cos(theta);
128 
129  HepMC::FourVector p(px, py, pz, energy);
130  HepMC::GenParticle* Part = new HepMC::GenParticle(p, partID, 1);
131  barcode++;
132  Part->suggest_barcode(barcode);
133  Vtx->add_particle_out(Part);
134 
135  fEvt->add_vertex(Vtx);
136  fEvt->set_event_number(e.id().event());
137  fEvt->set_signal_process_id(20);
138 
139  if (fVerbosity > 1)
140  fEvt->print();
141 
142  std::unique_ptr<HepMCProduct> BProduct(new HepMCProduct());
143  BProduct->addHepMCData(fEvt);
144  e.put(std::move(BProduct), "unsmeared");
145 
146  std::unique_ptr<GenEventInfoProduct> genEventInfo(new GenEventInfoProduct(fEvt));
147  e.put(std::move(genEventInfo));
148  if (fVerbosity > 0)
149  edm::LogVerbatim("ParticleGun") << "FileRandomMultiParticlePGunProducer : Event Generation Done";
150 }
Log< level::Info, true > LogVerbatim
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
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:23
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
std::vector< int > fPartIDs
HepPDT::ParticleData ParticleData
const unsigned int np
std::map< int, std::vector< double > > fProbParticle_
HLT enums.
const unsigned int kfactor
def move(src, dest)
Definition: eostools.py:511