CMS 3D CMS Logo

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 
29 
34 
35 #include "CLHEP/Units/GlobalSystemOfUnits.h"
36 #include "CLHEP/Units/GlobalPhysicalConstants.h"
37 //#include "CLHEP/Vector/ThreeVector.h"
38 #include "HepMC/SimpleVector.h"
39 #include "TMatrixD.h"
40 
41 #include <iostream>
42 
43 using namespace edm;
44 using namespace std;
45 using namespace CLHEP;
46 
47 class RandGaussQ;
48 class FourVector ;
49 
51 public:
53  ~MixBoostEvtVtxGenerator() override;
54 
56  void produce( edm::Event&, const edm::EventSetup& ) override;
57  virtual TMatrixD* GetInvLorentzBoost();
58  virtual HepMC::FourVector* getVertex(edm::Event&);
59  virtual HepMC::FourVector* getRecVertex(edm::Event&);
60 
62  void sigmaZ(double s=1.0);
63 
65  void X0(double m=0) { fX0=m; }
67  void Y0(double m=0) { fY0=m; }
69  void Z0(double m=0) { fZ0=m; }
70 
72  void Phi(double m=0) { phi_=m; }
74  void Alpha(double m=0) { alpha_=m; }
75  void Beta(double m=0) { beta_=m; }
76 
78  void betastar(double m=0) { fbetastar=m; }
80  void emittance(double m=0) { femittance=m; }
81 
83  double BetaFunction(double z, double z0);
84 
85 private:
89  MixBoostEvtVtxGenerator& operator = (const MixBoostEvtVtxGenerator & rhs ) = delete;
90 
91  double alpha_, phi_;
92  //TMatrixD boost_;
93  double beta_;
94  double fX0, fY0, fZ0;
95  double fSigmaZ;
96  //double fdxdz, fdydz;
97  double fbetastar, femittance;
98  double falpha;
99 
100  HepMC::FourVector* fVertex ;
101  TMatrixD *boost_;
102  double fTimeOffset;
103 
108  std::vector<double> vtxOffset;
109 
110 };
111 
113  fVertex(nullptr), boost_(nullptr), fTimeOffset(0),
114  vtxLabel(mayConsume<reco::VertexCollection>(pset.getParameter<edm::InputTag>("vtxLabel"))),
115  signalLabel(consumes<HepMCProduct>(pset.getParameter<edm::InputTag>("signalLabel"))),
116  mixLabel(consumes<CrossingFrame<HepMCProduct> >(pset.getParameter<edm::InputTag>("mixLabel"))),
117  useRecVertex(pset.exists("useRecVertex")?pset.getParameter<bool>("useRecVertex"):false)
118 {
119  beta_ = pset.getParameter<double>("Beta");
120  alpha_ = 0;
121  phi_ = 0;
122  if(pset.exists("Alpha")){
123  alpha_ = pset.getParameter<double>("Alpha")*radian;
124  phi_ = pset.getParameter<double>("Phi")*radian;
125  }
126 
127  vtxOffset.resize(3);
128  if(pset.exists("vtxOffset")) vtxOffset=pset.getParameter< std::vector<double> >("vtxOffset");
129 
130  produces<edm::HepMCProduct>();
131 
132 }
133 
135 {
136  if (fVertex != nullptr) delete fVertex ;
137  if (boost_ != nullptr ) delete boost_;
138 }
139 
140 
141 double MixBoostEvtVtxGenerator::BetaFunction(double z, double z0)
142 {
143  return sqrt(femittance*(fbetastar+(((z-z0)*(z-z0))/fbetastar)));
144 
145 }
146 
147 
149 {
150  if (s>=0 ) {
151  fSigmaZ=s;
152  }
153  else {
154  throw cms::Exception("LogicError")
155  << "Error in MixBoostEvtVtxGenerator::sigmaZ: "
156  << "Illegal resolution in Z (negative)";
157  }
158 }
159 
161 
162  //alpha_ = 0;
163  //phi_ = 142.e-6;
164 // if (boost_ != 0 ) return boost_;
165 
166  //boost_.ResizeTo(4,4);
167  //boost_ = new TMatrixD(4,4);
168  TMatrixD tmpboost(4,4);
169  TMatrixD tmpboostZ(4,4);
170  TMatrixD tmpboostXYZ(4,4);
171 
172  //if ( (alpha_ == 0) && (phi_==0) ) { boost_->Zero(); return boost_; }
173 
174  // Lorentz boost to frame where the collision is head-on
175  // phi is the half crossing angle in the plane ZS
176  // alpha is the angle to the S axis from the X axis in the XY plane
177 
178  tmpboost(0,0) = 1./cos(phi_);
179  tmpboost(0,1) = - cos(alpha_)*sin(phi_);
180  tmpboost(0,2) = - tan(phi_)*sin(phi_);
181  tmpboost(0,3) = - sin(alpha_)*sin(phi_);
182  tmpboost(1,0) = - cos(alpha_)*tan(phi_);
183  tmpboost(1,1) = 1.;
184  tmpboost(1,2) = cos(alpha_)*tan(phi_);
185  tmpboost(1,3) = 0.;
186  tmpboost(2,0) = 0.;
187  tmpboost(2,1) = - cos(alpha_)*sin(phi_);
188  tmpboost(2,2) = cos(phi_);
189  tmpboost(2,3) = - sin(alpha_)*sin(phi_);
190  tmpboost(3,0) = - sin(alpha_)*tan(phi_);
191  tmpboost(3,1) = 0.;
192  tmpboost(3,2) = sin(alpha_)*tan(phi_);
193  tmpboost(3,3) = 1.;
194  //cout<<"beta "<<beta_;
195  double gama=1.0/sqrt(1-beta_*beta_);
196  tmpboostZ(0,0)=gama;
197  tmpboostZ(0,1)=0.;
198  tmpboostZ(0,2)=-1.0*beta_*gama;
199  tmpboostZ(0,3)=0.;
200  tmpboostZ(1,0)=0.;
201  tmpboostZ(1,1) = 1.;
202  tmpboostZ(1,2)=0.;
203  tmpboostZ(1,3)=0.;
204  tmpboostZ(2,0)=-1.0*beta_*gama;
205  tmpboostZ(2,1) = 0.;
206  tmpboostZ(2,2)=gama;
207  tmpboostZ(2,3) = 0.;
208  tmpboostZ(3,0)=0.;
209  tmpboostZ(3,1)=0.;
210  tmpboostZ(3,2)=0.;
211  tmpboostZ(3,3) = 1.;
212 
213  tmpboostXYZ=tmpboostZ*tmpboost;
214  tmpboostXYZ.Invert();
215 
216  //cout<<"Boosting with beta : "<<beta_<<endl;
217 
218  boost_ = new TMatrixD(tmpboostXYZ);
219  boost_->Print();
220 
221  return boost_;
222 }
223 
224 HepMC::FourVector* MixBoostEvtVtxGenerator::getVertex( Event& evt){
225 
226  const HepMC::GenEvent* inev = nullptr;
227 
229  evt.getByToken(mixLabel,cf);
231 
232  const HepMCProduct& bkg = mix.getObject(1);
233  if(!(bkg.isVtxGenApplied())){
234  throw cms::Exception("MatchVtx")<<"Input background does not have smeared vertex!"<<endl;
235  }else{
236  inev = bkg.GetEvent();
237  }
238 
239  HepMC::GenVertex* genvtx = inev->signal_process_vertex();
240  if(!genvtx){
241  cout<<"No Signal Process Vertex!"<<endl;
242  HepMC::GenEvent::particle_const_iterator pt=inev->particles_begin();
243  HepMC::GenEvent::particle_const_iterator ptend=inev->particles_end();
244  while(!genvtx || ( genvtx->particles_in_size() == 1 && pt != ptend ) ){
245  if(!genvtx) cout<<"No Gen Vertex!"<<endl;
246  if(pt == ptend) cout<<"End reached!"<<endl;
247  genvtx = (*pt)->production_vertex();
248  ++pt;
249  }
250  }
251  double aX,aY,aZ,aT;
252 
253  aX = genvtx->position().x();
254  aY = genvtx->position().y();
255  aZ = genvtx->position().z();
256  aT = genvtx->position().t();
257 
258  if(!fVertex) fVertex = new HepMC::FourVector();
259  fVertex->set(aX,aY,aZ,aT);
260 
261  return fVertex;
262 
263 }
264 
265 
267 
268 
270  evt.getByToken(vtxLabel,input);
271 
272  double aX,aY,aZ;
273 
274  aX = input->begin()->position().x() + vtxOffset[0];
275  aY = input->begin()->position().y() + vtxOffset[1];
276  aZ = input->begin()->position().z() + vtxOffset[2];
277 
278  if(!fVertex) fVertex = new HepMC::FourVector();
279  fVertex->set(10.0*aX,10.0*aY,10.0*aZ,0.0); // HepMC positions in mm (RECO in cm)
280 
281  return fVertex;
282 
283 }
284 
285 
287 {
288  Handle<HepMCProduct> HepUnsmearedMCEvt;
289  evt.getByToken(signalLabel, HepUnsmearedMCEvt);
290 
291  // Copy the HepMC::GenEvent
292  HepMC::GenEvent* genevt = new HepMC::GenEvent(*HepUnsmearedMCEvt->GetEvent());
293  std::unique_ptr<edm::HepMCProduct> HepMCEvt(new edm::HepMCProduct(genevt));
294  // generate new vertex & apply the shift
295  //
296 
297  HepMCEvt->boostToLab( GetInvLorentzBoost(), "vertex" );
298  HepMCEvt->boostToLab( GetInvLorentzBoost(), "momentum" );
299 
300  HepMCEvt->applyVtxGen( useRecVertex ? getRecVertex(evt) : getVertex(evt) ) ;
301 
302  evt.put(std::move(HepMCEvt));
303  return ;
304 }
305 
T getParameter(std::string const &) const
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:137
void Z0(double m=0)
set mean in Z in cm
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:579
edm::EDGetTokenT< HepMCProduct > signalLabel
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
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
#define nullptr
static std::string const input
Definition: EdmProvDump.cc:44
return((rh^lh)&mask)
void sigmaZ(double s=1.0)
set resolution in Z in cm
edm::EDGetTokenT< reco::VertexCollection > vtxLabel
T sqrt(T t)
Definition: SSEVec.h:18
void produce(edm::Event &, const edm::EventSetup &) override
return a new event vertex
edm::EDGetTokenT< CrossingFrame< HepMCProduct > > mixLabel
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
virtual TMatrixD * GetInvLorentzBoost()
Tan< T >::type tan(const T &t)
Definition: Tan.h:22
MixBoostEvtVtxGenerator(const edm::ParameterSet &p)
void Alpha(double m=0)
angle between crossing plane and horizontal plane
const HepMC::GenEvent * GetEvent() const
Definition: HepMCProduct.h:38
void Phi(double m=0)
set half crossing angle
T const * product() const
Definition: Handle.h:81
void betastar(double m=0)
set beta_star
void Y0(double m=0)
set mean in Y in cm
fixed size matrix
HLT enums.
virtual HepMC::FourVector * getRecVertex(edm::Event &)
virtual HepMC::FourVector * getVertex(edm::Event &)
def move(src, dest)
Definition: eostools.py:510
double BetaFunction(double z, double z0)
beta function