test
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 
29 
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 
88 private:
92  MixBoostEvtVtxGenerator& operator = (const MixBoostEvtVtxGenerator & rhs );
93 
94  double alpha_, phi_;
95  //TMatrixD boost_;
96  double beta_;
97  double fX0, fY0, fZ0;
98  double fSigmaZ;
99  //double fdxdz, fdydz;
100  double fbetastar, femittance;
101  double falpha;
102 
103  HepMC::FourVector* fVertex ;
104  TMatrixD *boost_;
105  double fTimeOffset;
106 
107  CLHEP::RandGaussQ* fRandom ;
108 
113  std::vector<double> vtxOffset;
114 
115 };
116 
118  fVertex(0), boost_(0), fTimeOffset(0),
119  vtxLabel(mayConsume<reco::VertexCollection>(pset.getParameter<edm::InputTag>("vtxLabel"))),
120  signalLabel(consumes<HepMCProduct>(pset.getParameter<edm::InputTag>("signalLabel"))),
121  mixLabel(consumes<CrossingFrame<HepMCProduct> >(pset.getParameter<edm::InputTag>("mixLabel"))),
122  useRecVertex(pset.exists("useRecVertex")?pset.getParameter<bool>("useRecVertex"):false)
123 {
124 
125  vtxOffset.resize(3);
126  if(pset.exists("vtxOffset")) vtxOffset=pset.getParameter< std::vector<double> >("vtxOffset");
127 
128  produces<edm::HepMCProduct>();
129 
130 }
131 
133 {
134  delete fVertex ;
135  if (boost_ != 0 ) delete boost_;
136  delete fRandom;
137 }
138 
139 
140 //Hep3Vector* MixBoostEvtVtxGenerator::newVertex() {
142 
143 
144  double X,Y,Z;
145 
146  double tmp_sigz = fRandom->fire(0., fSigmaZ);
147  Z = tmp_sigz + fZ0;
148 
149  double tmp_sigx = BetaFunction(Z,fZ0);
150  // need sqrt(2) for beamspot width relative to single beam width
151  tmp_sigx /= sqrt(2.0);
152  X = fRandom->fire(0.,tmp_sigx) + fX0; // + Z*fdxdz ;
153 
154  double tmp_sigy = BetaFunction(Z,fZ0);
155  // need sqrt(2) for beamspot width relative to single beam width
156  tmp_sigy /= sqrt(2.0);
157  Y = fRandom->fire(0.,tmp_sigy) + fY0; // + Z*fdydz;
158 
159  double tmp_sigt = fRandom->fire(0., fSigmaZ);
160  double T = tmp_sigt + fTimeOffset;
161 
162  if ( fVertex == 0 ) fVertex = new HepMC::FourVector();
163  fVertex->set(X,Y,Z,T);
164 
165  return fVertex;
166 }
167 
168 double MixBoostEvtVtxGenerator::BetaFunction(double z, double z0)
169 {
170  return sqrt(femittance*(fbetastar+(((z-z0)*(z-z0))/fbetastar)));
171 
172 }
173 
174 
176 {
177  if (s>=0 ) {
178  fSigmaZ=s;
179  }
180  else {
181  throw cms::Exception("LogicError")
182  << "Error in MixBoostEvtVtxGenerator::sigmaZ: "
183  << "Illegal resolution in Z (negative)";
184  }
185 }
186 
188 
189  //alpha_ = 0;
190  //phi_ = 142.e-6;
191 // if (boost_ != 0 ) return boost_;
192 
193  //boost_.ResizeTo(4,4);
194  //boost_ = new TMatrixD(4,4);
195  TMatrixD tmpboost(4,4);
196  TMatrixD tmpboostZ(4,4);
197  TMatrixD tmpboostXYZ(4,4);
198 
199  //if ( (alpha_ == 0) && (phi_==0) ) { boost_->Zero(); return boost_; }
200 
201  // Lorentz boost to frame where the collision is head-on
202  // phi is the half crossing angle in the plane ZS
203  // alpha is the angle to the S axis from the X axis in the XY plane
204 
205  tmpboost(0,0) = 1./cos(phi_);
206  tmpboost(0,1) = - cos(alpha_)*sin(phi_);
207  tmpboost(0,2) = - tan(phi_)*sin(phi_);
208  tmpboost(0,3) = - sin(alpha_)*sin(phi_);
209  tmpboost(1,0) = - cos(alpha_)*tan(phi_);
210  tmpboost(1,1) = 1.;
211  tmpboost(1,2) = cos(alpha_)*tan(phi_);
212  tmpboost(1,3) = 0.;
213  tmpboost(2,0) = 0.;
214  tmpboost(2,1) = - cos(alpha_)*sin(phi_);
215  tmpboost(2,2) = cos(phi_);
216  tmpboost(2,3) = - sin(alpha_)*sin(phi_);
217  tmpboost(3,0) = - sin(alpha_)*tan(phi_);
218  tmpboost(3,1) = 0.;
219  tmpboost(3,2) = sin(alpha_)*tan(phi_);
220  tmpboost(3,3) = 1.;
221  //cout<<"beta "<<beta_;
222  double gama=1.0/sqrt(1-beta_*beta_);
223  tmpboostZ(0,0)=gama;
224  tmpboostZ(0,1)=0.;
225  tmpboostZ(0,2)=-1.0*beta_*gama;
226  tmpboostZ(0,3)=0.;
227  tmpboostZ(1,0)=0.;
228  tmpboostZ(1,1) = 1.;
229  tmpboostZ(1,2)=0.;
230  tmpboostZ(1,3)=0.;
231  tmpboostZ(2,0)=-1.0*beta_*gama;
232  tmpboostZ(2,1) = 0.;
233  tmpboostZ(2,2)=gama;
234  tmpboostZ(2,3) = 0.;
235  tmpboostZ(3,0)=0.;
236  tmpboostZ(3,1)=0.;
237  tmpboostZ(3,2)=0.;
238  tmpboostZ(3,3) = 1.;
239 
240  tmpboostXYZ=tmpboost*tmpboostZ;
241  tmpboost.Invert();
242 
243 
244 
245  boost_ = new TMatrixD(tmpboostXYZ);
246  boost_->Print();
247 
248  return boost_;
249 }
250 
251 HepMC::FourVector* MixBoostEvtVtxGenerator::getVertex( Event& evt){
252 
253  const HepMC::GenEvent* inev = 0;
254 
256  evt.getByToken(mixLabel,cf);
258 
259  const HepMCProduct& bkg = mix.getObject(1);
260  if(!(bkg.isVtxGenApplied())){
261  throw cms::Exception("MatchVtx")<<"Input background does not have smeared vertex!"<<endl;
262  }else{
263  inev = bkg.GetEvent();
264  }
265 
266  HepMC::GenVertex* genvtx = inev->signal_process_vertex();
267  if(!genvtx){
268  cout<<"No Signal Process Vertex!"<<endl;
269  HepMC::GenEvent::particle_const_iterator pt=inev->particles_begin();
270  HepMC::GenEvent::particle_const_iterator ptend=inev->particles_end();
271  while(!genvtx || ( genvtx->particles_in_size() == 1 && pt != ptend ) ){
272  if(!genvtx) cout<<"No Gen Vertex!"<<endl;
273  if(pt == ptend) cout<<"End reached!"<<endl;
274  genvtx = (*pt)->production_vertex();
275  ++pt;
276  }
277  }
278  double aX,aY,aZ,aT;
279 
280  aX = genvtx->position().x();
281  aY = genvtx->position().y();
282  aZ = genvtx->position().z();
283  aT = genvtx->position().t();
284 
285  if(!fVertex) fVertex = new HepMC::FourVector();
286  fVertex->set(aX,aY,aZ,aT);
287 
288  return fVertex;
289 
290 }
291 
292 
294 
295 
297  evt.getByToken(vtxLabel,input);
298 
299  double aX,aY,aZ;
300 
301  aX = input->begin()->position().x() + vtxOffset[0];
302  aY = input->begin()->position().y() + vtxOffset[1];
303  aZ = input->begin()->position().z() + vtxOffset[2];
304 
305  if(!fVertex) fVertex = new HepMC::FourVector();
306  fVertex->set(10.0*aX,10.0*aY,10.0*aZ,0.0); // HepMC positions in mm (RECO in cm)
307 
308  return fVertex;
309 
310 }
311 
312 
314 {
315  Handle<HepMCProduct> HepUnsmearedMCEvt;
316  evt.getByToken(signalLabel, HepUnsmearedMCEvt);
317 
318  // Copy the HepMC::GenEvent
319  HepMC::GenEvent* genevt = new HepMC::GenEvent(*HepUnsmearedMCEvt->GetEvent());
320  std::unique_ptr<edm::HepMCProduct> HepMCEvt(new edm::HepMCProduct(genevt));
321  // generate new vertex & apply the shift
322  //
323  HepMCEvt->applyVtxGen( useRecVertex ? getRecVertex(evt) : getVertex(evt) ) ;
324 
325  // HepMCEvt->boostToLab( GetInvLorentzBoost(), "vertex" );
326  // HepMCEvt->boostToLab( GetInvLorentzBoost(), "momentum" );
327 
328  evt.put(std::move(HepMCEvt));
329  return ;
330 }
331 
const double Z[kNumberCalorimeter]
T getParameter(std::string const &) const
void Z0(double m=0)
set mean in Z in cm
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:462
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
#define X(str)
Definition: MuonsGrabber.cc:48
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
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:121
edm::EDGetTokenT< reco::VertexCollection > vtxLabel
T sqrt(T t)
Definition: SSEVec.h:18
virtual void produce(edm::Event &, const edm::EventSetup &) override
edm::EDGetTokenT< CrossingFrame< HepMCProduct > > mixLabel
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
virtual HepMC::FourVector * newVertex()
return a new event vertex
def move
Definition: eostools.py:510
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
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
return(e1-e2)*(e1-e2)+dp *dp
tuple cout
Definition: gather_cfg.py:145
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