CMS 3D CMS Logo

BeamMomentumGunProducer.cc
Go to the documentation of this file.
2 
5 
11 
12 #include "CLHEP/Random/RandFlat.h"
13 
14 #include "TFile.h"
15 
16 namespace CLHEP {
17  class HepRandomEngine;
18 }
19 
20 namespace edm {
23  parPDGId_(nullptr),
24  parX_(nullptr),
25  parY_(nullptr),
26  parZ_(nullptr),
27  parPx_(nullptr),
28  parPy_(nullptr),
29  parPz_(nullptr),
30  b_npar_(nullptr),
31  b_eventId_(nullptr),
32  b_parPDGId_(nullptr),
33  b_parX_(nullptr),
34  b_parY_(nullptr),
35  b_parZ_(nullptr),
36  b_parPx_(nullptr),
37  b_parPy_(nullptr),
38  b_parPz_(nullptr) {
39  edm::ParameterSet pgun_params = pset.getParameter<edm::ParameterSet>("PGunParameters");
40 
41  // doesn't seem necessary to check if pset is empty
42  xoff_ = pgun_params.getParameter<double>("XOffset");
43  yoff_ = pgun_params.getParameter<double>("YOffset");
44  zpos_ = pgun_params.getParameter<double>("ZPosition");
45  if (fVerbosity > 0)
46  edm::LogVerbatim("BeamMomentumGun")
47  << "Beam vertex offset (cm) " << xoff_ << ":" << yoff_ << " and z position " << zpos_;
48 
49  edm::FileInPath fp = pgun_params.getParameter<edm::FileInPath>("FileName");
50  std::string infileName = fp.fullPath();
51 
52  fFile_ = new TFile(infileName.c_str());
53  fFile_->GetObject("EventTree", fTree_);
54  nentries_ = fTree_->GetEntriesFast();
55  if (fVerbosity > 0)
56  edm::LogVerbatim("BeamMomentumGun") << "Total Events: " << nentries_ << " in " << infileName;
57 
58  // Set branch addresses and branch pointers
59  int npart = fTree_->SetBranchAddress("npar", &npar_, &b_npar_);
60  int event = fTree_->SetBranchAddress("eventId", &eventId_, &b_eventId_);
61  int pdgid = fTree_->SetBranchAddress("parPDGId", &parPDGId_, &b_parPDGId_);
62  int parxx = fTree_->SetBranchAddress("parX", &parX_, &b_parX_);
63  int paryy = fTree_->SetBranchAddress("parY", &parY_, &b_parY_);
64  int parzz = fTree_->SetBranchAddress("parZ", &parZ_, &b_parZ_);
65  int parpx = fTree_->SetBranchAddress("parPx", &parPx_, &b_parPx_);
66  int parpy = fTree_->SetBranchAddress("parPy", &parPy_, &b_parPy_);
67  int parpz = fTree_->SetBranchAddress("parPz", &parPz_, &b_parPz_);
68  if ((npart != 0) || (event != 0) || (pdgid != 0) || (parxx != 0) || (paryy != 0) || (parzz != 0) || (parpx != 0) ||
69  (parpy != 0) || (parpz != 0))
70  throw cms::Exception("GenException") << "Branch address wrong in i/p file\n";
71 
72  produces<HepMCProduct>("unsmeared");
73  produces<GenEventInfoProduct>();
74 
75  if (fVerbosity > 0)
76  edm::LogVerbatim("BeamMomentumGun") << "BeamMonetumGun is initialzed";
77  }
78 
80  if (fVerbosity > 0)
81  edm::LogVerbatim("BeamMomentumGun") << "BeamMomentumGunProducer : Begin New Event Generation";
82 
84  CLHEP::HepRandomEngine* engine = &rng->getEngine(e.streamID());
85 
86  // event loop (well, another step in it...)
87  // no need to clean up GenEvent memory - done in HepMCProduct
88  // here re-create fEvt (memory)
89  //
90  fEvt = new HepMC::GenEvent();
91 
92  // random entry generation for peaking event randomly from tree
93  long int rjentry = static_cast<long int>(CLHEP::RandFlat::shoot(engine, 0, nentries_ - 1));
94  fTree_->GetEntry(rjentry);
95  if (fVerbosity > 0)
96  edm::LogVerbatim("BeamMomentumGun") << "Entry " << rjentry << " : " << eventId_ << " : " << npar_;
97 
98  // loop over particles
99  int barcode = 1;
100  for (unsigned int ip = 0; ip < parPDGId_->size(); ip++) {
101  int partID = parPDGId_->at(ip);
102  const HepPDT::ParticleData* pData = fPDGTable->particle(HepPDT::ParticleID(std::abs(partID)));
103  double mass = pData->mass().value();
104  if (fVerbosity > 0)
105  edm::LogVerbatim("BeamMomentumGun") << "PDGId: " << partID << " mass: " << mass;
106  double xp = (xoff_ * cm2mm_ + (-1) * parY_->at(ip)); // 90 degree rotation applied
107  double yp = (yoff_ * cm2mm_ + parX_->at(ip)); // 90 degree rotation applied
108  double zp = zpos_ * cm2mm_;
109  HepMC::GenVertex* Vtx = new HepMC::GenVertex(HepMC::FourVector(xp, yp, zp));
110  double pxGeV = MeV2GeV_ * (-1) * parPy_->at(ip); // 90 degree rotation applied
111  double pyGeV = MeV2GeV_ * parPx_->at(ip); // 90 degree rotation applied
112  double pzGeV = MeV2GeV_ * parPz_->at(ip);
113  double theta = CLHEP::RandFlat::shoot(engine, fMinTheta, fMaxTheta);
114  double phi = CLHEP::RandFlat::shoot(engine, fMinPhi, fMaxPhi);
115  // rotation about Z axis
116  double px1 = pxGeV * cos(phi) - pyGeV * sin(phi);
117  double py1 = pxGeV * sin(phi) + pyGeV * cos(phi);
118  double pz1 = pzGeV;
119  // rotation about Y axis
120  double px = px1 * cos(theta) + pz1 * sin(theta);
121  double py = py1;
122  double pz = -px1 * sin(theta) + pz1 * cos(theta);
123  double energy = std::sqrt(px * px + py * py + pz * pz + mass * mass);
124 
125  if (fVerbosity > 0) {
126  edm::LogVerbatim("BeamMomentumGun") << "x:y:z [mm] " << xp << ":" << yp << ":" << zpos_;
127  edm::LogVerbatim("BeamMomentumGun") << "px:py:pz [GeV] " << px << ":" << py << ":" << pz;
128  }
129 
130  HepMC::FourVector p(px, py, pz, energy);
131  HepMC::GenParticle* part = new HepMC::GenParticle(p, partID, 1);
132  part->suggest_barcode(barcode);
133  barcode++;
134  Vtx->add_particle_out(part);
135 
136  if (fAddAntiParticle) {
137  HepMC::FourVector ap(-px, -py, -pz, energy);
138  int apartID = (partID == 22 || partID == 23) ? partID : -partID;
139  HepMC::GenParticle* apart = new HepMC::GenParticle(ap, apartID, 1);
140  apart->suggest_barcode(barcode);
141  if (fVerbosity > 0)
142  edm::LogVerbatim("BeamMomentumGun")
143  << "Add anti-particle " << apartID << ":" << -px << ":" << -py << ":" << -pz;
144  barcode++;
145  Vtx->add_particle_out(apart);
146  }
147 
148  fEvt->add_vertex(Vtx);
149  }
150 
151  fEvt->set_event_number(e.id().event());
152  fEvt->set_signal_process_id(20);
153 
154  if (fVerbosity > 0)
155  fEvt->print();
156 
157  std::unique_ptr<HepMCProduct> BProduct(new HepMCProduct());
158  BProduct->addHepMCData(fEvt);
159  e.put(std::move(BProduct), "unsmeared");
160 
161  std::unique_ptr<GenEventInfoProduct> genEventInfo(new GenEventInfoProduct(fEvt));
162  e.put(std::move(genEventInfo));
163 
164  if (fVerbosity > 0)
165  edm::LogVerbatim("BeamMomentumGun") << "BeamMomentumGunProducer : Event Generation Done";
166  }
167 } // namespace edm
Log< level::Info, true > LogVerbatim
static constexpr double MeV2GeV_
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
ESHandle< HepPDT::ParticleDataTable > fPDGTable
double npart
Definition: HydjetWrapper.h:46
virtual CLHEP::HepRandomEngine & getEngine(StreamID const &)=0
Use this engine in event methods.
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
part
Definition: HCALResponse.h:20
BeamMomentumGunProducer(const ParameterSet &)
HLT enums.
Geom::Theta< T > theta() const
def move(src, dest)
Definition: eostools.py:511
Definition: event.py:1