CMS 3D CMS Logo

BeamProfileVtxGenerator.cc
Go to the documentation of this file.
1 
2 
5 
8 
9 #include "CLHEP/Geometry/Transform3D.h"
10 #include "CLHEP/Random/RandFlat.h"
11 #include "CLHEP/Random/RandGaussQ.h"
12 #include "CLHEP/Units/GlobalSystemOfUnits.h"
13 #include "CLHEP/Units/GlobalPhysicalConstants.h"
14 #include "HepMC/SimpleVector.h"
15 
16 #include<fstream>
17 #include<string>
18 
21 
22  meanX(p.getParameter<double>("BeamMeanX")*cm);
23  meanY(p.getParameter<double>("BeamMeanY")*cm);
24  beamPos(p.getParameter<double>("BeamPosition")*cm);
25  sigmaX(p.getParameter<double>("BeamSigmaX")*cm);
26  sigmaY(p.getParameter<double>("BeamSigmaY")*cm);
27  double fMinEta = p.getParameter<double>("MinEta");
28  double fMaxEta = p.getParameter<double>("MaxEta");
29  double fMinPhi = p.getParameter<double>("MinPhi");
30  double fMaxPhi = p.getParameter<double>("MaxPhi");
31  eta(0.5*(fMinEta+fMaxEta));
32  phi(0.5*(fMinPhi+fMaxPhi));
33  psi(p.getParameter<double>("Psi"));
34  nBinx = p.getParameter<int>("BinX");
35  nBiny = p.getParameter<int>("BinY");
36  ffile = p.getParameter<bool>("UseFile");
37  fTimeOffset = p.getParameter<double>("TimeOffset")*ns*c_light;
38 
39  if (ffile) {
41  std::ifstream is(file.c_str(), std::ios::in);
42  if (is) {
43  double elem,sum=0;
44  while (!is.eof()) {
45  is >> elem;
46  fdistn.push_back(elem);
47  sum += elem;
48  }
49  if (((int)(fdistn.size())) >= nBinx*nBiny) {
50  double last = 0;
51  for (unsigned int i=0; i<fdistn.size(); i++) {
52  fdistn[i] /= sum;
53  fdistn[i] += last;
54  last = fdistn[i];
55  }
56  setType(false);
57  } else {
58  ffile = false;
59  }
60  } else {
61  ffile = false;
62  }
63  }
64  if (!ffile) {
65  setType(p.getParameter<bool>("GaussianProfile"));
66  }
67 
68  edm::LogInfo("VertexGenerator") << "BeamProfileVtxGenerator: with "
69  << "beam along eta = " << fEta
70  << " (Theta = " << fTheta/deg
71  << ") phi = " << fPhi/deg
72  << ") psi = " << fPsi/deg
73  << " centred at (" << fMeanX << ", "
74  << fMeanY << ", " << fMeanZ << ") "
75  << "and spread (" << fSigmaX << ", "
76  << fSigmaY << ") of type Gaussian = "
77  << fType << " use file " << ffile;
78  if (ffile)
79  edm::LogInfo("VertexGenerator") << "With " << nBinx << " bins "
80  << " along X and " << nBiny
81  << " bins along Y";
82 }
83 
85 }
86 
87 
88 //Hep3Vector * BeamProfileVtxGenerator::newVertex() {
89 HepMC::FourVector BeamProfileVtxGenerator::newVertex(CLHEP::HepRandomEngine* engine) const {
90  double aX, aY;
91  if (ffile) {
92  double r1 = engine->flat();
93  int ixy = 0, ix, iy;
94  for (unsigned int i=0; i<fdistn.size(); i++) {
95  if (r1 > fdistn[i]) ixy = i+1;
96  }
97  if (ixy >= nBinx*nBiny) {
98  ix = nBinx-1; iy = nBiny-1;
99  } else {
100  ix = ixy%nBinx; iy = (ixy-ix)/nBinx;
101  }
102  aX = 0.5*(2*ix-nBinx+2*engine->flat())*fSigmaX + fMeanX ;
103  aY = 0.5*(2*iy-nBiny+2*engine->flat())*fSigmaY + fMeanY ;
104  } else {
105  if (fType) {
106  aX = fSigmaX*CLHEP::RandGaussQ::shoot(engine) + fMeanX;
107  aY = fSigmaY*CLHEP::RandGaussQ::shoot(engine) + fMeanY;
108  } else {
109  aX = CLHEP::RandFlat::shoot(engine, -0.5*fSigmaX, 0.5*fSigmaX) + fMeanX ;
110  aY = CLHEP::RandFlat::shoot(engine, -0.5*fSigmaY, 0.5*fSigmaY) + fMeanY;
111  }
112  }
113 
114  double xp, yp, zp ;
115  if( 2.*M_PI < fabs(fPsi) )
116  {
117  xp = -aX*cos(fTheta)*cos(fPhi) +aY*sin(fPhi) +fMeanZ*sin(fTheta)*cos(fPhi);
118  yp = -aX*cos(fTheta)*sin(fPhi) -aY*cos(fPhi) +fMeanZ*sin(fTheta)*sin(fPhi);
119  zp = aX*sin(fTheta) +fMeanZ*cos(fTheta);
120  }
121  else // for endcap testbeam, use 3rd angle psi
122  {
123  const HepGeom::Vector3D<double> av ( aX, aY, fMeanZ ) ;
124 
125 /*
126  static const double kRadToDeg ( 180./M_PI ) ;
127  std::cout<<"theta = "<<std::setw(7) << std::setiosflags( std::ios::fixed ) << std::setprecision(5)
128  <<fTheta*kRadToDeg<<", phi="<<std::setw(7) << std::setiosflags( std::ios::fixed ) << std::setprecision(5)
129  << fPhi*kRadToDeg <<", PSI="<<std::setw(7) << std::setiosflags( std::ios::fixed ) << std::setprecision(5)
130  <<fPsi*kRadToDeg<<std::endl ;
131 */
132  const HepGeom::RotateZ3D R1 ( fPhi - M_PI ) ;
133  const HepGeom::Point3D<double> xUnit ( 0,1,0 ) ;
134  const HepGeom::Point3D<double> zUnit ( 0,0,1 ) ;
135  const HepGeom::Transform3D RXRZ ( HepGeom::Rotate3D( -fTheta, R1*xUnit )*R1 ) ;
136  const HepGeom::Transform3D TRF ( HepGeom::Rotate3D(fPsi,RXRZ*zUnit)*RXRZ ) ;
137 /*
138  std::cout<<"\n\n$$$$$$$$$$$Transform="
139  <<" thetaZ = "<<std::setw(7) << std::setiosflags( std::ios::fixed ) << std::setprecision(5)
140  <<TRF.getRotation().thetaZ()*kRadToDeg
141  <<", phiZ = "<<std::setw(7) << std::setiosflags( std::ios::fixed ) << std::setprecision(5)
142  <<TRF.getRotation().phiZ()*kRadToDeg
143  <<", thetaY = "<<std::setw(7) << std::setiosflags( std::ios::fixed ) << std::setprecision(5)
144  <<TRF.getRotation().thetaY()*kRadToDeg
145  <<", phiY = "<<std::setw(7) << std::setiosflags( std::ios::fixed ) << std::setprecision(5)
146  <<TRF.getRotation().phiY()*kRadToDeg
147  <<", thetaX = "<<std::setw(7) << std::setiosflags( std::ios::fixed ) << std::setprecision(5)
148  <<TRF.getRotation().thetaX()*kRadToDeg
149  <<", phiX = "<<std::setw(7) << std::setiosflags( std::ios::fixed ) << std::setprecision(5)
150  <<TRF.getRotation().phiX()*kRadToDeg
151  <<std::setw(7) << std::setiosflags( std::ios::fixed ) << std::setprecision(5)
152  <<TRF.getTranslation()<<std::endl ;
153 */
154  const HepGeom::Vector3D<double> pv ( TRF*av ) ;
155 
156  xp = pv.x() ;
157  yp = pv.y() ;
158  zp = pv.z() ;
159  }
160 
161  LogDebug("VertexGenerator") << "BeamProfileVtxGenerator: Vertex created "
162  << "at (" << xp << ", " << yp << ", "
163  << zp << ", " << fTimeOffset << ")";
164  return HepMC::FourVector(xp, yp, zp, fTimeOffset );;
165 }
166 
168 
169  if (s>=0) {
170  fSigmaX = s;
171  } else {
172  edm::LogWarning("VertexGenerator") << "Warning BeamProfileVtxGenerator:"
173  << " Illegal resolution in X " << s
174  << "- set to default value 0 cm";
175  fSigmaX = 0;
176  }
177 }
178 
180 
181  if (s>=0) {
182  fSigmaY = s;
183  } else {
184  edm::LogWarning("VertexGenerator") << "Warning BeamProfileVtxGenerator:"
185  << " Illegal resolution in Y " << s
186  << "- set to default value 0 cm";
187  fSigmaY = 0;
188  }
189 }
190 
192  fEta = s;
193  fTheta = 2.0*atan(exp(-fEta));
194 }
195 
197  fType = s;
198 }
#define LogDebug(id)
void meanY(double m=0)
set mean in Y in cm
T getParameter(std::string const &) const
HepMC::FourVector newVertex(CLHEP::HepRandomEngine *) const override
return a new event vertex
void setType(bool m=true)
set type
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
void meanX(double m=0)
set mean in X in cm
void psi(double m=999)
set psi in radian
std::vector< double > fdistn
void sigmaX(double s=1.0)
set resolution in X in cm
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
def pv(vc)
Definition: MetAnalyzer.py:7
void beamPos(double m=0)
set mean in Z in cm
void phi(double m=0)
set phi in radian
void eta(double m=0)
set eta
def elem(elemtype, innerHTML='', html_class='', kwargs)
Definition: HTMLExport.py:19
#define M_PI
BeamProfileVtxGenerator(const edm::ParameterSet &p)
void sigmaY(double s=1.0)
set resolution in Y in cm