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 
43  bool simulateVertex_;
45 
46  struct SmearingParameters {
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 
ConfigurationDescriptions.h
CTPPSBeamParameters
Definition: CTPPSBeamParameters.h:22
CTPPSBeamParameters::getVtxStddevY
double getVtxStddevY() const
Definition: CTPPSBeamParameters.cc:83
BeamDivergenceVtxGenerator
Definition: BeamDivergenceVtxGenerator.cc:28
edm::RandomNumberGenerator::getEngine
virtual CLHEP::HepRandomEngine & getEngine(StreamID const &)=0
Use this engine in event methods.
CTPPSBeamParameters.h
electrons_cff.bool
bool
Definition: electrons_cff.py:366
CTPPSBeamParametersRcd.h
ESHandle.h
BeamDivergenceVtxGenerator::SmearingParameters::bd_x_45
double bd_x_45
Definition: BeamDivergenceVtxGenerator.cc:49
reco::GenParticle
Definition: GenParticle.h:21
convertSQLitetoXML_cfg.output
output
Definition: convertSQLitetoXML_cfg.py:72
edm::EDGetTokenT< edm::HepMCProduct >
RandomNumberGenerator.h
BeamDivergenceVtxGenerator::SmearingParameters::bd_y_56
double bd_y_56
Definition: BeamDivergenceVtxGenerator.cc:49
AlCaHLTBitMon_ParallelJobs.p
p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
pos
Definition: PixelAliasList.h:18
HLT_FULL_cff.InputTag
InputTag
Definition: HLT_FULL_cff.py:89281
edm::ParameterSetDescription
Definition: ParameterSetDescription.h:52
edm::EDGetTokenT::isUninitialized
constexpr bool isUninitialized() const noexcept
Definition: EDGetToken.h:99
EDProducer.h
Validation_hcalonly_cfi.sign
sign
Definition: Validation_hcalonly_cfi.py:32
edm::Handle< edm::HepMCProduct >
edm::Service::isAvailable
bool isAvailable() const
Definition: Service.h:40
ESGetToken.h
HepMC::GenEvent
Definition: hepmc_rootio.cc:9
GenParticle.h
CTPPSBeamParameters::getBeamDivergenceY45
double getBeamDivergenceY45() const
Definition: CTPPSBeamParameters.cc:64
edm::InputTag::label
std::string const & label() const
Definition: InputTag.h:36
BeamDivergenceVtxGenerator::SmearingParameters::vtx_t
double vtx_t
Definition: BeamDivergenceVtxGenerator.cc:48
MakerMacros.h
CTPPSBeamParameters::getVtxOffsetZ45
double getVtxOffsetZ45() const
Definition: CTPPSBeamParameters.cc:75
part
part
Definition: HCALResponse.h:20
BeamDivergenceVtxGenerator::SmearingParameters
Definition: BeamDivergenceVtxGenerator.cc:47
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
CTPPSBeamParameters::getBeamDivergenceY56
double getBeamDivergenceY56() const
Definition: CTPPSBeamParameters.cc:66
edm::ConfigurationDescriptions::add
void add(std::string const &label, ParameterSetDescription const &psetDescription)
Definition: ConfigurationDescriptions.cc:57
CTPPSBeamParameters::getBeamDivergenceX45
double getBeamDivergenceX45() const
Definition: CTPPSBeamParameters.cc:63
Service.h
BeamDivergenceVtxGenerator::beamParametersToken_
edm::ESGetToken< CTPPSBeamParameters, CTPPSBeamParametersRcd > beamParametersToken_
Definition: BeamDivergenceVtxGenerator.cc:41
mathSSE::sqrt
T sqrt(T t)
Definition: SSEVec.h:19
BeamDivergenceVtxGenerator::smearMomentum
static HepMC::FourVector smearMomentum(const T &mom, const SmearingParameters &sp)
Definition: BeamDivergenceVtxGenerator.cc:159
edm::ESHandle< CTPPSBeamParameters >
BeamDivergenceVtxGenerator::sourceToken_
edm::EDGetTokenT< edm::HepMCProduct > sourceToken_
Definition: BeamDivergenceVtxGenerator.cc:40
ParameterSetDescription.h
BeamDivergenceVtxGenerator::SmearingParameters::bd_y_45
double bd_y_45
Definition: BeamDivergenceVtxGenerator.cc:49
BeamDivergenceVtxGenerator::SmearingParameters::bd_x_56
double bd_x_56
Definition: BeamDivergenceVtxGenerator.cc:49
edm::ConfigurationDescriptions
Definition: ConfigurationDescriptions.h:28
CTPPSBeamParameters::getBeamDivergenceX56
double getBeamDivergenceX56() const
Definition: CTPPSBeamParameters.cc:65
runTauDisplay.gp
gp
Definition: runTauDisplay.py:431
CTPPSBeamParameters::getVtxOffsetY45
double getVtxOffsetY45() const
Definition: CTPPSBeamParameters.cc:74
edm::ParameterSet
Definition: ParameterSet.h:47
BeamDivergenceVtxGenerator::addSmearedGenParticle
void addSmearedGenParticle(const reco::GenParticle &gp, const SmearingParameters &sp, HepMC::GenEvent *genEvt)
Definition: BeamDivergenceVtxGenerator.cc:203
BeamDivergenceVtxGenerator::SmearingParameters::vtx_y
double vtx_y
Definition: BeamDivergenceVtxGenerator.cc:48
Event.h
edm::Service< edm::RandomNumberGenerator >
iEvent
int iEvent
Definition: GenABIO.cc:224
edm::EventSetup::getHandle
ESHandle< T > getHandle(const ESGetToken< T, R > &iToken) const
Definition: EventSetup.h:155
edm::stream::EDProducer
Definition: EDProducer.h:38
edm::EventSetup
Definition: EventSetup.h:58
edm::HepMCProduct::GetEvent
const HepMC::GenEvent * GetEvent() const
Definition: HepMCProduct.h:37
BeamDivergenceVtxGenerator::applySmearingHepMC
void applySmearingHepMC(const SmearingParameters &sp, HepMC::GenEvent *genEvt)
Definition: BeamDivergenceVtxGenerator.cc:184
CTPPSBeamParameters::getVtxOffsetX45
double getVtxOffsetX45() const
Definition: CTPPSBeamParameters.cc:73
edm::ESGetToken< CTPPSBeamParameters, CTPPSBeamParametersRcd >
CTPPSBeamParametersRcd
Definition: CTPPSBeamParametersRcd.h:14
BeamDivergenceVtxGenerator::BeamDivergenceVtxGenerator
BeamDivergenceVtxGenerator(const edm::ParameterSet &)
Definition: BeamDivergenceVtxGenerator.cc:60
BeamDivergenceVtxGenerator::~BeamDivergenceVtxGenerator
~BeamDivergenceVtxGenerator() override=default
submitPVResolutionJobs.desc
string desc
Definition: submitPVResolutionJobs.py:251
GenParticle.GenParticle
GenParticle
Definition: GenParticle.py:18
eostools.move
def move(src, dest)
Definition: eostools.py:511
extraflags_cff.vtx
vtx
Definition: extraflags_cff.py:18
CTPPSBeamParameters::getVtxStddevZ
double getVtxStddevZ() const
Definition: CTPPSBeamParameters.cc:84
T
long double T
Definition: Basic3DVectorLD.h:48
EventSetup.h
CTPPSBeamParameters::getVtxOffsetT45
double getVtxOffsetT45() const
Definition: CTPPSBeamParameters.cc:76
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
BeamDivergenceVtxGenerator::SmearingParameters::vtx_x
double vtx_x
Definition: BeamDivergenceVtxGenerator.cc:48
BeamDivergenceVtxGenerator::SmearingParameters::vtx_z
double vtx_z
Definition: BeamDivergenceVtxGenerator.cc:48
cms::Exception
Definition: Exception.h:70
BeamDivergenceVtxGenerator::genParticleTokens_
std::vector< edm::EDGetTokenT< reco::GenParticleCollection > > genParticleTokens_
Definition: BeamDivergenceVtxGenerator.cc:42
edm::HepMCProduct
Definition: HepMCProduct.h:21
ParameterSet.h
HepMCProduct.h
CTPPSBeamParameters::getVtxStddevT
double getVtxStddevT() const
Definition: CTPPSBeamParameters.cc:85
CTPPSBeamParameters::getVtxStddevX
double getVtxStddevX() const
Definition: CTPPSBeamParameters.cc:82
DeDxTools::esConsumes
ESGetTokenH3DDVariant esConsumes(std::string const &Reccord, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
edm::Event
Definition: Event.h:73
BeamDivergenceVtxGenerator::simulateVertex_
bool simulateVertex_
Definition: BeamDivergenceVtxGenerator.cc:44
edm::InputTag
Definition: InputTag.h:15
BeamDivergenceVtxGenerator::simulateBeamDivergence_
bool simulateBeamDivergence_
Definition: BeamDivergenceVtxGenerator.cc:45
TtGenEvtProducer_cfi.genEvt
genEvt
Definition: TtGenEvtProducer_cfi.py:7
BeamDivergenceVtxGenerator::fillDescriptions
static void fillDescriptions(edm::ConfigurationDescriptions &)
Definition: BeamDivergenceVtxGenerator.cc:84
BeamDivergenceVtxGenerator::produce
void produce(edm::Event &, const edm::EventSetup &) override
Definition: BeamDivergenceVtxGenerator.cc:101
reco::vertex_iterator
VertexRefVector::iterator vertex_iterator
iterator over a vector of references to Vertex objects in the same collection
Definition: VertexFwd.h:19
unpackBuffers-CaloStage2.token
token
Definition: unpackBuffers-CaloStage2.py:316