CMS 3D CMS Logo

HLLHCEvtVtxGenerator.cc
Go to the documentation of this file.
1 // $Id: HLLHCEvtVtxGenerator_Fix.cc, v 1.0 2015/03/15 10:32:11 Exp $
2 
5 
10 
11 #include <CLHEP/Random/RandFlat.h>
12 #include <CLHEP/Units/SystemOfUnits.h>
13 #include <CLHEP/Units/GlobalPhysicalConstants.h>
14 #include "HepMC/SimpleVector.h"
15 
16 using namespace std;
17 
18 namespace {
19  constexpr double pmass = 0.9382720813e9; // eV
20  constexpr double gamma34 = 1.22541670246517764513; // Gamma(3/4)
21  constexpr double gamma14 = 3.62560990822190831193; // Gamma(1/4)
22  constexpr double gamma54 = 0.90640247705547798267; // Gamma(5/4)
23  constexpr double sqrt2 = 1.41421356237;
24  constexpr double sqrt2to5 = 5.65685424949;
25  constexpr double two_pi = 2.0 * M_PI;
26 } // namespace
27 
30  desc.add<double>("MeanXIncm", 0.0);
31  desc.add<double>("MeanYIncm", 0.0);
32  desc.add<double>("MeanZIncm", 0.0);
33  desc.add<double>("TimeOffsetInns", 0.0);
34  desc.add<double>("EprotonInGeV", 7000.0);
35  desc.add<double>("CrossingAngleInurad", 510.0);
36  desc.add<double>("CrabbingAngleCrossingInurad", 380.0);
37  desc.add<double>("CrabbingAngleSeparationInurad", 0.0);
38  desc.add<double>("CrabFrequencyInMHz", 400.0);
39  desc.add<bool>("RF800", false);
40  desc.add<double>("BetaCrossingPlaneInm", 0.20);
41  desc.add<double>("BetaSeparationPlaneInm", 0.20);
42  desc.add<double>("HorizontalEmittance", 2.5e-06);
43  desc.add<double>("VerticalEmittance", 2.05e-06);
44  desc.add<double>("BunchLengthInm", 0.09);
45  desc.add<edm::InputTag>("src");
46  desc.add<bool>("readDB");
47  descriptions.add("HLLHCEvtVtxGenerator", desc);
48 }
49 
51  readDB_ = p.getParameter<bool>("readDB");
52  if (!readDB_) {
53  // Read configurable parameters
54  fMeanX = p.getParameter<double>("MeanXIncm") * CLHEP::cm;
55  fMeanY = p.getParameter<double>("MeanYIncm") * CLHEP::cm;
56  fMeanZ = p.getParameter<double>("MeanZIncm") * CLHEP::cm;
57  fTimeOffset_c_light = p.getParameter<double>("TimeOffsetInns") * CLHEP::ns * CLHEP::c_light;
58  fEProton = p.getParameter<double>("EprotonInGeV") * 1e9;
59  fCrossingAngle = p.getParameter<double>("CrossingAngleInurad") * 1e-6;
60  fCrabFrequency = p.getParameter<double>("CrabFrequencyInMHz") * 1e6;
61  fRF800 = p.getParameter<bool>("RF800");
62  fBetaCrossingPlane = p.getParameter<double>("BetaCrossingPlaneInm");
63  fBetaSeparationPlane = p.getParameter<double>("BetaSeparationPlaneInm");
64  fHorizontalEmittance = p.getParameter<double>("HorizontalEmittance");
65  fVerticalEmittance = p.getParameter<double>("VerticalEmittance");
66  fBunchLength = p.getParameter<double>("BunchLengthInm");
67  fCrabbingAngleCrossing = p.getParameter<double>("CrabbingAngleCrossingInurad") * 1e-6;
68  fCrabbingAngleSeparation = p.getParameter<double>("CrabbingAngleSeparationInurad") * 1e-6;
69  // Set parameters inferred from configurables
70  gamma = fEProton / pmass;
71  beta = std::sqrt((1.0 - 1.0 / gamma) * ((1.0 + 1.0 / gamma)));
72  betagamma = beta * gamma;
75  epss = epsx;
78  }
79  if (readDB_) {
80  // NOTE: this is currently watching LS transitions, while it should watch Run transitions,
81  // even though in reality there is no Run Dependent MC (yet) in CMS
82  beamToken_ =
83  esConsumes<SimBeamSpotHLLHCObjects, SimBeamSpotHLLHCObjectsRcd, edm::Transition::BeginLuminosityBlock>();
84  }
85 }
86 
88  update(iEventSetup);
89 }
90 
92  if (readDB_ && parameterWatcher_.check(iEventSetup)) {
93  edm::ESHandle<SimBeamSpotHLLHCObjects> beamhandle = iEventSetup.getHandle(beamToken_);
94  // Read configurable parameters
95  fMeanX = beamhandle->meanX() * CLHEP::cm;
96  fMeanY = beamhandle->meanY() * CLHEP::cm;
97  fMeanZ = beamhandle->meanZ() * CLHEP::cm;
98  fEProton = beamhandle->eProton() * 1e9;
99  fCrossingAngle = beamhandle->crossingAngle() * 1e-6;
100  fCrabFrequency = beamhandle->crabFrequency() * 1e6;
101  fRF800 = beamhandle->rf800();
102  fBetaCrossingPlane = beamhandle->betaCrossingPlane();
103  fBetaSeparationPlane = beamhandle->betaSeparationPlane();
104  fHorizontalEmittance = beamhandle->horizontalEmittance();
105  fVerticalEmittance = beamhandle->verticalEmittance();
106  fBunchLength = beamhandle->bunchLenght();
107  fCrabbingAngleCrossing = beamhandle->crabbingAngleCrossing() * 1e-6;
108  fCrabbingAngleSeparation = beamhandle->crabbingAngleSeparation() * 1e-6;
109  fTimeOffset_c_light = beamhandle->timeOffset() * CLHEP::ns * CLHEP::c_light;
110  // Set parameters inferred from configurables
111  gamma = fEProton / pmass;
112  beta = std::sqrt((1.0 - 1.0 / gamma) * ((1.0 + 1.0 / gamma)));
113  betagamma = beta * gamma;
114  oncc = fCrabbingAngleCrossing / fCrossingAngle;
115  epsx = fHorizontalEmittance / (betagamma);
116  epss = epsx;
117  sigx = std::sqrt(epsx * fBetaCrossingPlane);
118  phiCR = oncc * fCrossingAngle;
119  }
120 }
121 
122 HepMC::FourVector HLLHCEvtVtxGenerator::newVertex(CLHEP::HepRandomEngine* engine) const {
123  double imax = intensity(0., 0., 0., 0.);
124 
125  double x(0.), y(0.), z(0.), t(0.), i(0.);
126 
127  int count = 0;
128 
129  auto shoot = [&]() { return CLHEP::RandFlat::shoot(engine); };
130 
131  do {
132  z = (shoot() - 0.5) * 6.0 * fBunchLength;
133  t = (shoot() - 0.5) * 6.0 * fBunchLength;
134  x = (shoot() - 0.5) * 12.0 * sigma(0.0, fHorizontalEmittance, fBetaCrossingPlane, betagamma);
135  y = (shoot() - 0.5) * 12.0 * sigma(0.0, fVerticalEmittance, fBetaSeparationPlane, betagamma);
136 
137  i = intensity(x, y, z, t);
138 
139  if (i > imax)
140  edm::LogError("Too large intensity") << "i>imax : " << i << " > " << imax << endl;
141  ++count;
142  } while ((i < imax * shoot()) && count < 10000);
143 
144  if (count > 9999)
145  edm::LogError("Too many tries ") << " count : " << count << endl;
146 
147  //---convert to mm
148  x *= 1000.0;
149  y *= 1000.0;
150  z *= 1000.0;
151  t *= 1000.0;
152 
153  x += fMeanX;
154  y += fMeanY;
155  z += fMeanZ;
156  t += fTimeOffset_c_light;
157 
158  return HepMC::FourVector(x, y, z, t);
159 }
160 
161 double HLLHCEvtVtxGenerator::sigma(double z, double epsilon, double beta, double betagamma) const {
162  double sigma = std::sqrt(epsilon * (beta + z * z / beta) / betagamma);
163  return sigma;
164 }
165 
166 double HLLHCEvtVtxGenerator::intensity(double x, double y, double z, double t) const {
167  //---c in m/s --- remember t is already in meters
168  constexpr double c = 2.99792458e+8; // m/s
169 
170  const double sigmay = sigma(z, fVerticalEmittance, fBetaSeparationPlane, betagamma);
171 
172  const double alphay_mod = fCrabbingAngleSeparation * std::cos(fCrabFrequency * (z - t) / c);
173 
174  const double cay = std::cos(alphay_mod);
175  const double say = std::sin(alphay_mod);
176 
177  const double dy = -(z - t) * say - y * cay;
178 
179  const double xzt_density = integrandCC(x, z, t);
180 
181  const double norm = two_pi * sigmay;
182 
183  return (std::exp(-dy * dy / (sigmay * sigmay)) * xzt_density / norm);
184 }
185 
186 double HLLHCEvtVtxGenerator::integrandCC(double x, double z, double ct) const {
187  constexpr double local_c_light = 2.99792458e8;
188 
189  const double k = fCrabFrequency / local_c_light * two_pi;
190  const double k2 = k * k;
191  const double cos = std::cos(fCrossingAngle / 2.0);
192  const double sin = std::sin(fCrossingAngle / 2.0);
193  const double cos2 = cos * cos;
194  const double sin2 = sin * sin;
195 
196  const double sigx2 = sigx * sigx;
197  const double sigmax2 = sigx2 * (1 + z * z / (fBetaCrossingPlane * fBetaCrossingPlane));
198 
199  const double sigs2 = fBunchLength * fBunchLength;
200 
201  constexpr double factorRMSgauss4 =
202  1. / sqrt2 / gamma34 * gamma14; // # Factor to take rms sigma as input of the supergaussian
203  constexpr double NormFactorGauss4 = sqrt2to5 * gamma54 * gamma54;
204 
205  const double sinCR = std::sin(phiCR / 2.0);
206  const double sinCR2 = sinCR * sinCR;
207 
208  double result = -1.0;
209 
210  if (!fRF800) {
211  const double norm = 2.0 / (two_pi * sigs2);
212  const double cosks = std::cos(k * z);
213  const double sinkct = std::sin(k * ct);
214  result = norm *
215  std::exp(-ct * ct / sigs2 - z * z * cos2 / sigs2 -
216  1.0 / (4 * k2 * sigmax2) *
217  (
218  //-4*cosks*cosks * sinkct*sinkct * sinCR2 // comes from integral over x
219  -8 * z * k * std::sin(k * z) * std::cos(k * ct) * sin * sinCR + 2 * sinCR2 -
220  std::cos(2 * k * (z - ct)) * sinCR2 - std::cos(2 * k * (z + ct)) * sinCR2 +
221  4 * k2 * z * z * sin2) -
222  x * x * (cos2 / sigmax2 + sin2 / sigs2) // contribution from x integrand
223  + x * ct * sin / sigs2 // contribution from x integrand
224  + 2 * x * cos * cosks * sinkct * sinCR / k / sigmax2 // contribution from x integrand
225  //+(2*ct/k)*np.cos(k*s)*np.sin(k*ct) *(sin*sinCR)/(sigs2*cos) # small term
226  //+ct**2*(sin2/sigs4)/(cos2/sigmax2) # small term
227  ) /
228  (1.0 + (z * z) / (fBetaCrossingPlane * fBetaCrossingPlane)) /
229  std::sqrt(1.0 + (z * z) / (fBetaSeparationPlane * fBetaSeparationPlane));
230 
231  } else {
232  const double norm = 2.0 / (NormFactorGauss4 * sigs2 * factorRMSgauss4);
233  const double sigs4 = sigs2 * sigs2 * factorRMSgauss4 * factorRMSgauss4;
234  const double cosks = std::cos(k * z);
235  const double sinct = std::sin(k * ct);
236  result =
237  norm *
238  std::exp(-ct * ct * ct * ct / sigs4 - z * z * z * z * cos2 * cos2 / sigs4 - 6 * ct * ct * z * z * cos2 / sigs4 -
239  sin2 / (4 * k2 * sigmax2) *
240  (2 + 4 * k2 * z * z - std::cos(2 * k * (z - ct)) - std::cos(2 * k * (z + ct)) -
241  8 * k * CLHEP::s * std::cos(k * ct) * std::sin(k * z) - 4 * cosks * cosks * sinct * sinct)) /
242  std::sqrt((1 + z * z / (fBetaCrossingPlane * fBetaCrossingPlane)) /
243  (1 + z * z / (fBetaSeparationPlane * fBetaSeparationPlane)));
244  }
245 
246  return result;
247 }
HepMC::FourVector newVertex(CLHEP::HepRandomEngine *) const override
return a new event vertex
HLLHCEvtVtxGenerator(const edm::ParameterSet &p)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
double sigma(double z, double epsilon, double beta, double betagamma) const
Log< level::Error, false > LogError
double eProton() const
get EProton, fCrabFrequency, RF800
double meanX() const
get meanX, meanY, meanZ position
T sqrt(T t)
Definition: SSEVec.h:23
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
double crossingAngle() const
set Crossing and Crabbing angles
double intensity(double x, double y, double z, double t) const
edm::ESGetToken< SimBeamSpotHLLHCObjects, SimBeamSpotHLLHCObjectsRcd > beamToken_
ESHandle< T > getHandle(const ESGetToken< T, R > &iToken) const
Definition: EventSetup.h:130
#define M_PI
void update(const edm::EventSetup &iEventSetup)
void add(std::string const &label, ParameterSetDescription const &psetDescription)
double betaCrossingPlane() const
get BetaStar and Emittance
void beginLuminosityBlock(edm::LuminosityBlock const &, edm::EventSetup const &) override
static constexpr float local_c_light
Definition: pTFrom2Stubs.cc:9
#define update(a, b)
double bunchLenght() const
get BunchLength and TimeOffset
float x
double integrandCC(double x, double z, double t) const