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
GenEventInfoProduct
Definition: GenEventInfoProduct.h:17
edm::RandomNumberGenerator::getEngine
virtual CLHEP::HepRandomEngine & getEngine(StreamID const &)=0
Use this engine in event methods.
edm::BeamMomentumGunProducer::b_parZ_
TBranch * b_parZ_
Definition: BeamMomentumGunProducer.h:37
MessageLogger.h
multPhiCorr_741_25nsDY_cfi.py
py
Definition: multPhiCorr_741_25nsDY_cfi.py:12
edm::BeamMomentumGunProducer::MeV2GeV_
static constexpr double MeV2GeV_
Definition: BeamMomentumGunProducer.h:41
edm
HLT enums.
Definition: AlignableModifier.h:19
RandomNumberGenerator.h
AlCaHLTBitMon_ParallelJobs.p
p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
ZgammaFilter_cfi.HepMCProduct
HepMCProduct
Definition: ZgammaFilter_cfi.py:9
personalPlayback.fp
fp
Definition: personalPlayback.py:523
edm::BeamMomentumGunProducer::parPx_
std::vector< float > * parPx_
Definition: BeamMomentumGunProducer.h:33
edm::BeamMomentumGunProducer::produce
void produce(Event &e, const EventSetup &es) override
Definition: BeamMomentumGunProducer.cc:79
edm::FlatBaseThetaGunProducer::fEvt
HepMC::GenEvent * fEvt
Definition: FlatBaseThetaGunProducer.h:43
npart
double npart
Definition: HydjetWrapper.h:46
edm::BeamMomentumGunProducer::parPDGId_
std::vector< int > * parPDGId_
Definition: BeamMomentumGunProducer.h:31
HepMC::GenEvent
Definition: hepmc_rootio.cc:9
ParticleData
HepPDT::ParticleData ParticleData
Definition: ParticleDataTable.h:9
edm::BeamMomentumGunProducer::nentries_
long int nentries_
Definition: BeamMomentumGunProducer.h:27
edm::BeamMomentumGunProducer::cm2mm_
static constexpr double cm2mm_
Definition: BeamMomentumGunProducer.h:40
funct::sin
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
edm::BeamMomentumGunProducer::BeamMomentumGunProducer
BeamMomentumGunProducer(const ParameterSet &)
Definition: BeamMomentumGunProducer.cc:21
edm::FileInPath
Definition: FileInPath.h:64
edm::FlatBaseThetaGunProducer::fMinPhi
double fMinPhi
Definition: FlatBaseThetaGunProducer.h:39
part
part
Definition: HCALResponse.h:20
edm::BeamMomentumGunProducer::fTree_
TTree * fTree_
Definition: BeamMomentumGunProducer.h:26
funct::cos
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
edm::BeamMomentumGunProducer::parPy_
std::vector< float > * parPy_
Definition: BeamMomentumGunProducer.h:33
Service.h
mathSSE::sqrt
T sqrt(T t)
Definition: SSEVec.h:19
theta
Geom::Theta< T > theta() const
Definition: Basic3DVectorLD.h:150
HCALHighEnergyHPDFilter_cfi.energy
energy
Definition: HCALHighEnergyHPDFilter_cfi.py:5
CLHEP
Definition: CocoaGlobals.h:27
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
edm::FlatBaseThetaGunProducer::fMaxTheta
double fMaxTheta
Definition: FlatBaseThetaGunProducer.h:38
edm::FlatBaseThetaGunProducer::fMaxPhi
double fMaxPhi
Definition: FlatBaseThetaGunProducer.h:40
edm::BeamMomentumGunProducer::parX_
std::vector< float > * parX_
Definition: BeamMomentumGunProducer.h:32
edm::ParameterSet
Definition: ParameterSet.h:47
edm::BeamMomentumGunProducer::xoff_
double xoff_
Definition: BeamMomentumGunProducer.h:24
GenEventInfoProduct.h
Event.h
edm::BeamMomentumGunProducer::b_parX_
TBranch * b_parX_
Definition: BeamMomentumGunProducer.h:37
edm::BeamMomentumGunProducer::parPz_
std::vector< float > * parPz_
Definition: BeamMomentumGunProducer.h:33
edm::Service< edm::RandomNumberGenerator >
edm::FlatBaseThetaGunProducer::fMinTheta
double fMinTheta
Definition: FlatBaseThetaGunProducer.h:37
edm::EventSetup
Definition: EventSetup.h:58
edm::BeamMomentumGunProducer::b_parPz_
TBranch * b_parPz_
Definition: BeamMomentumGunProducer.h:38
edm::BeamMomentumGunProducer::b_parPx_
TBranch * b_parPx_
Definition: BeamMomentumGunProducer.h:38
edm::BeamMomentumGunProducer::b_eventId_
TBranch * b_eventId_
Definition: BeamMomentumGunProducer.h:36
edm::FlatBaseThetaGunProducer::fPDGTable
ESHandle< HepPDT::ParticleDataTable > fPDGTable
Definition: FlatBaseThetaGunProducer.h:47
edm::BeamMomentumGunProducer::zpos_
double zpos_
Definition: BeamMomentumGunProducer.h:24
BeamMomentumGunProducer.h
edm::FlatBaseThetaGunProducer
Definition: FlatBaseThetaGunProducer.h:21
multPhiCorr_741_25nsDY_cfi.px
px
Definition: multPhiCorr_741_25nsDY_cfi.py:10
GenParticle.GenParticle
GenParticle
Definition: GenParticle.py:18
eostools.move
def move(src, dest)
Definition: eostools.py:511
genParticles2HepMC_cfi.genEventInfo
genEventInfo
Definition: genParticles2HepMC_cfi.py:6
edm::LogVerbatim
Log< level::Info, true > LogVerbatim
Definition: MessageLogger.h:128
Exception
Definition: hltDiff.cc:245
edm::BeamMomentumGunProducer::npar_
int npar_
Definition: BeamMomentumGunProducer.h:30
EgHLTOffHistBins_cfi.mass
mass
Definition: EgHLTOffHistBins_cfi.py:34
edm::BeamMomentumGunProducer::fFile_
TFile * fFile_
Definition: BeamMomentumGunProducer.h:25
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
edm::BeamMomentumGunProducer::yoff_
double yoff_
Definition: BeamMomentumGunProducer.h:24
edm::BeamMomentumGunProducer::b_parPy_
TBranch * b_parPy_
Definition: BeamMomentumGunProducer.h:38
edm::BeamMomentumGunProducer::parZ_
std::vector< float > * parZ_
Definition: BeamMomentumGunProducer.h:32
edm::BeamMomentumGunProducer::b_parPDGId_
TBranch * b_parPDGId_
Definition: BeamMomentumGunProducer.h:36
edm::BeamMomentumGunProducer::eventId_
int eventId_
Definition: BeamMomentumGunProducer.h:30
funct::abs
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
ParameterSet.h
edm::FlatBaseThetaGunProducer::fVerbosity
int fVerbosity
Definition: FlatBaseThetaGunProducer.h:49
edm::BeamMomentumGunProducer::b_parY_
TBranch * b_parY_
Definition: BeamMomentumGunProducer.h:37
HepMCProduct.h
event
Definition: event.py:1
edm::Event
Definition: Event.h:73
EgammaValidation_cff.pdgid
pdgid
Definition: EgammaValidation_cff.py:30
LHEGenericFilter_cfi.ParticleID
ParticleID
Definition: LHEGenericFilter_cfi.py:6
edm::BeamMomentumGunProducer::parY_
std::vector< float > * parY_
Definition: BeamMomentumGunProducer.h:32
validateGeometry_cfg.infileName
infileName
Definition: validateGeometry_cfg.py:22
muonDTDigis_cfi.pset
pset
Definition: muonDTDigis_cfi.py:27
edm::BeamMomentumGunProducer::b_npar_
TBranch * b_npar_
Definition: BeamMomentumGunProducer.h:36
edm::FlatBaseThetaGunProducer::fAddAntiParticle
bool fAddAntiParticle
Definition: FlatBaseThetaGunProducer.h:51
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37