CMS 3D CMS Logo

BeamDivergenceVtxGenerator.cc
Go to the documentation of this file.
1 /****************************************************************************
2  * Authors:
3  * Jan Kašpar
4  ****************************************************************************/
5 
17 
19 
21 
24 
25 #include <CLHEP/Random/RandGauss.h>
26 
27 //----------------------------------------------------------------------------------------------------
28 
30 public:
32  ~BeamDivergenceVtxGenerator() override = default;
33 
35 
36  void produce(edm::Event &, const edm::EventSetup &) override;
37 
38 private:
41  std::vector<edm::EDGetTokenT<reco::GenParticleCollection>> genParticleTokens_;
42 
45 
47  double vtx_x, vtx_y, vtx_z, vtx_t; // cm
48  double bd_x_45, bd_y_45, bd_x_56, bd_y_56; // rad
49  };
50 
51  template <typename T>
52  static HepMC::FourVector smearMomentum(const T &mom, const SmearingParameters &sp);
53 
55 
57 };
58 
59 //----------------------------------------------------------------------------------------------------
60 
62  : beamParametersToken_(esConsumes<CTPPSBeamParameters, CTPPSBeamParametersRcd>()),
63  simulateVertex_(iConfig.getParameter<bool>("simulateVertex")),
64  simulateBeamDivergence_(iConfig.getParameter<bool>("simulateBeamDivergence")) {
65  const edm::InputTag tagSrcHepMC = iConfig.getParameter<edm::InputTag>("src");
66  if (!tagSrcHepMC.label().empty())
67  sourceToken_ = consumes<edm::HepMCProduct>(tagSrcHepMC);
68 
69  for (const auto &it : iConfig.getParameter<std::vector<edm::InputTag>>("srcGenParticle"))
70  genParticleTokens_.push_back(consumes<reco::GenParticleCollection>(it));
71 
73  if (!rng.isAvailable())
74  throw cms::Exception("Configuration")
75  << "The BeamDivergenceVtxGenerator requires the RandomNumberGeneratorService\n"
76  "which is not present in the configuration file. \n"
77  "You must add the service\n"
78  "in the configuration file or remove the modules that require it.";
79 
80  produces<edm::HepMCProduct>();
81 }
82 
83 //----------------------------------------------------------------------------------------------------
84 
87 
88  desc.add<edm::InputTag>("src", edm::InputTag("generator", "unsmeared"))
89  ->setComment("input collection in HepMC format");
90 
91  desc.add<std::vector<edm::InputTag>>("srcGenParticle", std::vector<edm::InputTag>())
92  ->setComment("input collections in GenParticle format");
93 
94  desc.add<bool>("simulateBeamDivergence", true)->setComment("account for the beam angular divergence?");
95  desc.add<bool>("simulateVertex", true)->setComment("account for the vertex transverse smearing?");
96 
97  descriptions.add("beamDivergenceVtxGenerator", desc);
98 }
99 
100 //----------------------------------------------------------------------------------------------------
101 
103  // get random engine
105  CLHEP::HepRandomEngine *rnd = &(rng->getEngine(iEvent.streamID()));
106 
107  // get conditions
109 
110  // get HepMC input (if given)
113  genEvt = new HepMC::GenEvent();
114  } else {
115  edm::Handle<edm::HepMCProduct> hepUnsmearedMCEvt;
116  iEvent.getByToken(sourceToken_, hepUnsmearedMCEvt);
117 
118  genEvt = new HepMC::GenEvent(*hepUnsmearedMCEvt->GetEvent());
119  }
120 
121  // prepare output
122  std::unique_ptr<edm::HepMCProduct> output(new edm::HepMCProduct(genEvt));
123 
124  // generate smearing parameters
126 
127  if (simulateVertex_) {
128  // NB: the separtion between effective offsets in LHC sectors 45 and 56 cannot be applied, thus the values for 45 are used
129  sp.vtx_x = hBeamParameters->getVtxOffsetX45() + CLHEP::RandGauss::shoot(rnd) * hBeamParameters->getVtxStddevX();
130  sp.vtx_y = hBeamParameters->getVtxOffsetY45() + CLHEP::RandGauss::shoot(rnd) * hBeamParameters->getVtxStddevY();
131  sp.vtx_z = hBeamParameters->getVtxOffsetZ45() + CLHEP::RandGauss::shoot(rnd) * hBeamParameters->getVtxStddevZ();
132  sp.vtx_t = hBeamParameters->getVtxOffsetT45() + CLHEP::RandGauss::shoot(rnd) * hBeamParameters->getVtxStddevT();
133  }
134 
136  sp.bd_x_45 = CLHEP::RandGauss::shoot(rnd) * hBeamParameters->getBeamDivergenceX45();
137  sp.bd_x_56 = CLHEP::RandGauss::shoot(rnd) * hBeamParameters->getBeamDivergenceX56();
138  sp.bd_y_45 = CLHEP::RandGauss::shoot(rnd) * hBeamParameters->getBeamDivergenceY45();
139  sp.bd_y_56 = CLHEP::RandGauss::shoot(rnd) * hBeamParameters->getBeamDivergenceY56();
140  }
141 
142  // apply smearing
144 
145  for (const auto &token : genParticleTokens_) {
147  iEvent.getByToken(token, hGPCollection);
148 
149  for (const auto &gp : *hGPCollection)
151  }
152 
153  // save output
154  iEvent.put(std::move(output));
155 }
156 
157 //----------------------------------------------------------------------------------------------------
158 
159 template <typename T>
160 HepMC::FourVector BeamDivergenceVtxGenerator::smearMomentum(const T &mom, const SmearingParameters &sp) {
161  // TODO: this is an oversimplified implemetation
162  // the TOTEM smearing module should be taken as reference
163 
164  double th_x = mom.x() / mom.z();
165  double th_y = mom.y() / mom.z();
166 
167  if (mom.z() > 0.) {
168  th_x += sp.bd_x_45;
169  th_y += sp.bd_y_45;
170  } else {
171  th_x += sp.bd_x_56;
172  th_y += sp.bd_y_56;
173  }
174 
175  // calculate consistent p_z component
176  const double sign = (mom.z() > 0.) ? 1. : -1.;
177  const double p = sqrt(mom.x() * mom.x() + mom.y() * mom.y() + mom.z() * mom.z());
178  const double p_z = sign * p / sqrt(1. + th_x * th_x + th_y * th_y);
179 
180  return HepMC::FourVector(p_z * th_x, p_z * th_y, p_z, mom.e());
181 }
182 
183 //----------------------------------------------------------------------------------------------------
184 
186  if (simulateVertex_) {
187  for (HepMC::GenEvent::vertex_iterator vit = genEvt->vertices_begin(); vit != genEvt->vertices_end(); ++vit) {
188  const auto &pos = (*vit)->position();
189  (*vit)->set_position(HepMC::FourVector(pos.x() + sp.vtx_x * 1E1, // conversion: cm to mm
190  pos.y() + sp.vtx_y * 1E1,
191  pos.z() + sp.vtx_z * 1E1,
192  pos.t() + sp.vtx_t * 1E1));
193  }
194  }
195 
197  for (HepMC::GenEvent::particle_iterator part = genEvt->particles_begin(); part != genEvt->particles_end(); ++part)
198  (*part)->set_momentum(smearMomentum((*part)->momentum(), sp));
199  }
200 }
201 
202 //----------------------------------------------------------------------------------------------------
203 
205  const SmearingParameters &sp,
207  // add vertex of the particle
208  HepMC::GenVertex *vtx = new HepMC::GenVertex(HepMC::FourVector(
209  (gp.vx() + sp.vtx_x) * 1E1, // conversion: cm to mm
210  (gp.vy() + sp.vtx_y) * 1E1,
211  (gp.vz() + sp.vtx_z) * 1E1,
212  (/*gp.vt()*/ +sp.vtx_t) * 1E1)); // TODO: GenParticle doesn't seem to have time component of the vertex
213  genEvt->add_vertex(vtx);
214 
215  // add the particle itself
216  HepMC::GenParticle *particle = new HepMC::GenParticle(smearMomentum(gp.p4(), sp), gp.pdgId(), gp.status());
217  vtx->add_particle_out(particle);
218 }
219 
220 //----------------------------------------------------------------------------------------------------
221 
std::vector< edm::EDGetTokenT< reco::GenParticleCollection > > genParticleTokens_
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
static void fillDescriptions(edm::ConfigurationDescriptions &)
double getVtxStddevX() const
static HepMC::FourVector smearMomentum(const T &mom, const SmearingParameters &sp)
double getVtxStddevZ() const
constexpr bool isUninitialized() const noexcept
Definition: EDGetToken.h:104
edm::ESGetToken< CTPPSBeamParameters, CTPPSBeamParametersRcd > beamParametersToken_
std::string const & label() const
Definition: InputTag.h:36
double getVtxOffsetX45() const
virtual CLHEP::HepRandomEngine & getEngine(StreamID const &)=0
Use this engine in event methods.
double getVtxOffsetT45() const
double getVtxStddevY() const
double getBeamDivergenceX56() const
int iEvent
Definition: GenABIO.cc:224
double getVtxOffsetZ45() const
T sqrt(T t)
Definition: SSEVec.h:23
double getVtxStddevT() const
double getVtxOffsetY45() const
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
edm::EDGetTokenT< edm::HepMCProduct > sourceToken_
ESHandle< T > getHandle(const ESGetToken< T, R > &iToken) const
Definition: EventSetup.h:130
const HepMC::GenEvent * GetEvent() const
Definition: HepMCProduct.h:37
double getBeamDivergenceY56() const
void applySmearingHepMC(const SmearingParameters &sp, HepMC::GenEvent *genEvt)
part
Definition: HCALResponse.h:20
void addSmearedGenParticle(const reco::GenParticle &gp, const SmearingParameters &sp, HepMC::GenEvent *genEvt)
BeamDivergenceVtxGenerator(const edm::ParameterSet &)
void add(std::string const &label, ParameterSetDescription const &psetDescription)
~BeamDivergenceVtxGenerator() override=default
double getBeamDivergenceX45() const
bool isAvailable() const
Definition: Service.h:40
Definition: output.py:1
VertexRefVector::iterator vertex_iterator
iterator over a vector of references to Vertex objects in the same collection
Definition: VertexFwd.h:19
long double T
double getBeamDivergenceY45() const
def move(src, dest)
Definition: eostools.py:511
void produce(edm::Event &, const edm::EventSetup &) override