CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
MixBoostEvtVtxGenerator.cc
Go to the documentation of this file.
1 
2 /*
3 ________________________________________________________________________
4 
5  MixBoostEvtVtxGenerator
6 
7  Smear vertex according to the Beta function on the transverse plane
8  and a Gaussian on the z axis. It allows the beam to have a crossing
9  angle (slopes dxdz and dydz).
10 
11  Based on GaussEvtVtxGenerator
12  implemented by Francisco Yumiceva (yumiceva@fnal.gov)
13 
14  FERMILAB
15  2006
16 ________________________________________________________________________
17 */
18 
19 //lingshan: add beta for z-axis boost
20 
21 //#include "IOMC/EventVertexGenerators/interface/BetafuncEvtVtxGenerator.h"
22 
26 
34 
35 #include "CLHEP/Random/RandGaussQ.h"
36 #include "CLHEP/Units/GlobalSystemOfUnits.h"
37 #include "CLHEP/Units/GlobalPhysicalConstants.h"
38 //#include "CLHEP/Vector/ThreeVector.h"
39 #include "HepMC/SimpleVector.h"
40 #include "TMatrixD.h"
41 
42 #include <iostream>
43 
44 using namespace edm;
45 using namespace std;
46 using namespace CLHEP;
47 
48 class RandGaussQ;
49 class FourVector ;
50 
52 public:
54  virtual ~MixBoostEvtVtxGenerator();
55 
57  //virtual CLHEP::Hep3Vector * newVertex();
58  virtual HepMC::FourVector* newVertex() ;
59  virtual void produce( edm::Event&, const edm::EventSetup& ) override;
60  virtual TMatrixD* GetInvLorentzBoost();
61  virtual HepMC::FourVector* getVertex(edm::Event&);
62  virtual HepMC::FourVector* getRecVertex(edm::Event&);
63 
65  void sigmaZ(double s=1.0);
66 
68  void X0(double m=0) { fX0=m; }
70  void Y0(double m=0) { fY0=m; }
72  void Z0(double m=0) { fZ0=m; }
73 
75  void Phi(double m=0) { phi_=m; }
77  void Alpha(double m=0) { alpha_=m; }
78  void Beta(double m=0) { beta_=m; }
79 
81  void betastar(double m=0) { fbetastar=m; }
83  void emittance(double m=0) { femittance=m; }
84 
86  double BetaFunction(double z, double z0);
87  CLHEP::HepRandomEngine& getEngine();
88 
89 private:
93  MixBoostEvtVtxGenerator& operator = (const MixBoostEvtVtxGenerator & rhs );
94 
95  double alpha_, phi_;
96  //TMatrixD boost_;
97  double beta_;
98  double fX0, fY0, fZ0;
99  double fSigmaZ;
100  //double fdxdz, fdydz;
101  double fbetastar, femittance;
102  double falpha;
103 
104  HepMC::FourVector* fVertex ;
105  TMatrixD *boost_;
106  double fTimeOffset;
107 
108  CLHEP::HepRandomEngine* fEngine;
110 
111  CLHEP::RandGaussQ* fRandom ;
112 
116  std::vector<double> vtxOffset;
117 
118 };
119 
120 
122  fVertex(0), boost_(0), fTimeOffset(0), fEngine(0),
123  signalLabel(pset.getParameter<edm::InputTag>("signalLabel")),
124  hiLabel(pset.getParameter<edm::InputTag>("heavyIonLabel")),
125  useRecVertex(pset.exists("useRecVertex")?pset.getParameter<bool>("useRecVertex"):false)
126 {
127 
128  vtxOffset.resize(3);
129  if(pset.exists("vtxOffset")) vtxOffset=pset.getParameter< std::vector<double> >("vtxOffset");
131 
132  if ( ! rng.isAvailable()) {
133 
134  throw cms::Exception("Configuration")
135  << "The BaseEvtVtxGenerator requires the RandomNumberGeneratorService\n"
136  "which is not present in the configuration file. You must add the service\n"
137  "in the configuration file or remove the modules that require it.";
138  }
139 
140  CLHEP::HepRandomEngine& engine = rng->getEngine();
141  fEngine = &engine;
142 
143  produces<bool>("matchedVertex");
144 
145 }
146 
148 {
149  delete fVertex ;
150  if (boost_ != 0 ) delete boost_;
151  delete fRandom;
152 }
153 
154 CLHEP::HepRandomEngine& MixBoostEvtVtxGenerator::getEngine(){
155  return *fEngine;
156 }
157 
158 
159 //Hep3Vector* MixBoostEvtVtxGenerator::newVertex() {
161 
162 
163  double X,Y,Z;
164 
165  double tmp_sigz = fRandom->fire(0., fSigmaZ);
166  Z = tmp_sigz + fZ0;
167 
168  double tmp_sigx = BetaFunction(Z,fZ0);
169  // need sqrt(2) for beamspot width relative to single beam width
170  tmp_sigx /= sqrt(2.0);
171  X = fRandom->fire(0.,tmp_sigx) + fX0; // + Z*fdxdz ;
172 
173  double tmp_sigy = BetaFunction(Z,fZ0);
174  // need sqrt(2) for beamspot width relative to single beam width
175  tmp_sigy /= sqrt(2.0);
176  Y = fRandom->fire(0.,tmp_sigy) + fY0; // + Z*fdydz;
177 
178  double tmp_sigt = fRandom->fire(0., fSigmaZ);
179  double T = tmp_sigt + fTimeOffset;
180 
181  if ( fVertex == 0 ) fVertex = new HepMC::FourVector();
182  fVertex->set(X,Y,Z,T);
183 
184  return fVertex;
185 }
186 
187 double MixBoostEvtVtxGenerator::BetaFunction(double z, double z0)
188 {
189  return sqrt(femittance*(fbetastar+(((z-z0)*(z-z0))/fbetastar)));
190 
191 }
192 
193 
195 {
196  if (s>=0 ) {
197  fSigmaZ=s;
198  }
199  else {
200  throw cms::Exception("LogicError")
201  << "Error in MixBoostEvtVtxGenerator::sigmaZ: "
202  << "Illegal resolution in Z (negative)";
203  }
204 }
205 
207 
208  //alpha_ = 0;
209  //phi_ = 142.e-6;
210 // if (boost_ != 0 ) return boost_;
211 
212  //boost_.ResizeTo(4,4);
213  //boost_ = new TMatrixD(4,4);
214  TMatrixD tmpboost(4,4);
215  TMatrixD tmpboostZ(4,4);
216  TMatrixD tmpboostXYZ(4,4);
217 
218  //if ( (alpha_ == 0) && (phi_==0) ) { boost_->Zero(); return boost_; }
219 
220  // Lorentz boost to frame where the collision is head-on
221  // phi is the half crossing angle in the plane ZS
222  // alpha is the angle to the S axis from the X axis in the XY plane
223 
224  tmpboost(0,0) = 1./cos(phi_);
225  tmpboost(0,1) = - cos(alpha_)*sin(phi_);
226  tmpboost(0,2) = - tan(phi_)*sin(phi_);
227  tmpboost(0,3) = - sin(alpha_)*sin(phi_);
228  tmpboost(1,0) = - cos(alpha_)*tan(phi_);
229  tmpboost(1,1) = 1.;
230  tmpboost(1,2) = cos(alpha_)*tan(phi_);
231  tmpboost(1,3) = 0.;
232  tmpboost(2,0) = 0.;
233  tmpboost(2,1) = - cos(alpha_)*sin(phi_);
234  tmpboost(2,2) = cos(phi_);
235  tmpboost(2,3) = - sin(alpha_)*sin(phi_);
236  tmpboost(3,0) = - sin(alpha_)*tan(phi_);
237  tmpboost(3,1) = 0.;
238  tmpboost(3,2) = sin(alpha_)*tan(phi_);
239  tmpboost(3,3) = 1.;
240  //cout<<"beta "<<beta_;
241  double gama=1.0/sqrt(1-beta_*beta_);
242  tmpboostZ(0,0)=gama;
243  tmpboostZ(0,1)=0.;
244  tmpboostZ(0,2)=-1.0*beta_*gama;
245  tmpboostZ(0,3)=0.;
246  tmpboostZ(1,0)=0.;
247  tmpboostZ(1,1) = 1.;
248  tmpboostZ(1,2)=0.;
249  tmpboostZ(1,3)=0.;
250  tmpboostZ(2,0)=-1.0*beta_*gama;
251  tmpboostZ(2,1) = 0.;
252  tmpboostZ(2,2)=gama;
253  tmpboostZ(2,3) = 0.;
254  tmpboostZ(3,0)=0.;
255  tmpboostZ(3,1)=0.;
256  tmpboostZ(3,2)=0.;
257  tmpboostZ(3,3) = 1.;
258 
259  tmpboostXYZ=tmpboost*tmpboostZ;
260  tmpboost.Invert();
261 
262 
263 
264  boost_ = new TMatrixD(tmpboostXYZ);
265  boost_->Print();
266 
267  return boost_;
268 }
269 
270 HepMC::FourVector* MixBoostEvtVtxGenerator::getVertex( Event& evt){
271 
273  evt.getByLabel(hiLabel,input);
274 
275  const HepMC::GenEvent* inev = input->GetEvent();
276  HepMC::GenVertex* genvtx = inev->signal_process_vertex();
277  if(!genvtx){
278  cout<<"No Signal Process Vertex!"<<endl;
279  HepMC::GenEvent::particle_const_iterator pt=inev->particles_begin();
280  HepMC::GenEvent::particle_const_iterator ptend=inev->particles_end();
281  while(!genvtx || ( genvtx->particles_in_size() == 1 && pt != ptend ) ){
282  if(!genvtx) cout<<"No Gen Vertex!"<<endl;
283  if(pt == ptend) cout<<"End reached!"<<endl;
284  genvtx = (*pt)->production_vertex();
285  ++pt;
286  }
287  }
288  double aX,aY,aZ,aT;
289 
290  aX = genvtx->position().x();
291  aY = genvtx->position().y();
292  aZ = genvtx->position().z();
293  aT = genvtx->position().t();
294 
295  if(!fVertex) fVertex = new HepMC::FourVector();
296  fVertex->set(aX,aY,aZ,aT);
297 
298  return fVertex;
299 
300 }
301 
302 
304 
306  evt.getByLabel(hiLabel,input);
307 
308  double aX,aY,aZ;
309 
310  aX = input->begin()->position().x() + vtxOffset[0];
311  aY = input->begin()->position().y() + vtxOffset[1];
312  aZ = input->begin()->position().z() + vtxOffset[2];
313 
314  if(!fVertex) fVertex = new HepMC::FourVector();
315  fVertex->set(10.0*aX,10.0*aY,10.0*aZ,0.0); // HepMC positions in mm (RECO in cm)
316 
317  return fVertex;
318 
319 }
320 
321 
323 {
324 
325 
326  Handle<HepMCProduct> HepMCEvt ;
327 
328  evt.getByLabel( signalLabel, HepMCEvt ) ;
329 
330  // generate new vertex & apply the shift
331  //
332 
333  HepMCEvt->applyVtxGen( useRecVertex ? getRecVertex(evt) : getVertex(evt) ) ;
334 
335  // HepMCEvt->boostToLab( GetInvLorentzBoost(), "vertex" );
336  // HepMCEvt->boostToLab( GetInvLorentzBoost(), "momentum" );
337 
338  // OK, create a (pseudo)product and put in into edm::Event
339  //
340  auto_ptr<bool> NewProduct(new bool(true)) ;
341  evt.put( NewProduct ,"matchedVertex") ;
342 
343  return ;
344 
345 }
346 
const double Z[kNumberCalorimeter]
T getParameter(std::string const &) const
CLHEP::HepRandomEngine * fEngine
void Z0(double m=0)
set mean in Z in cm
std::vector< double > vtxOffset
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
void emittance(double m=0)
emittance (no the normalized)
void X0(double m=0)
set mean in X in cm
bool exists(std::string const &parameterName) const
checks if a parameter exists
#define X(str)
Definition: MuonsGrabber.cc:48
float float float z
static std::string const input
Definition: EdmProvDump.cc:44
void sigmaZ(double s=1.0)
set resolution in Z in cm
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:116
T sqrt(T t)
Definition: SSEVec.h:48
CLHEP::HepRandomEngine & getEngine()
virtual void produce(edm::Event &, const edm::EventSetup &) override
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
virtual HepMC::FourVector * newVertex()
return a new event vertex
bool isAvailable() const
Definition: Service.h:46
virtual TMatrixD * GetInvLorentzBoost()
Tan< T >::type tan(const T &t)
Definition: Tan.h:22
MixBoostEvtVtxGenerator(const edm::ParameterSet &p)
virtual CLHEP::HepRandomEngine & getEngine() const =0
Use this to get the random number engine, this is the only function most users should call...
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:390
void Alpha(double m=0)
angle between crossing plane and horizontal plane
void Phi(double m=0)
set half crossing angle
void betastar(double m=0)
set beta_star
void Y0(double m=0)
set mean in Y in cm
return(e1-e2)*(e1-e2)+dp *dp
tuple cout
Definition: gather_cfg.py:121
virtual HepMC::FourVector * getRecVertex(edm::Event &)
volatile std::atomic< bool > shutdown_flag false
long double T
virtual HepMC::FourVector * getVertex(edm::Event &)
double BetaFunction(double z, double z0)
beta function