00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "FWCore/Framework/interface/Event.h"
00025 #include "FWCore/Utilities/interface/Exception.h"
00026 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00027
00028 #include "FWCore/Framework/interface/EDProducer.h"
00029 #include "FWCore/Utilities/interface/InputTag.h"
00030 #include "FWCore/ServiceRegistry/interface/Service.h"
00031 #include "FWCore/Utilities/interface/RandomNumberGenerator.h"
00032 #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
00033
00034 #include "CLHEP/Random/RandGaussQ.h"
00035 #include "CLHEP/Units/GlobalSystemOfUnits.h"
00036 #include "CLHEP/Units/GlobalPhysicalConstants.h"
00037
00038 #include "HepMC/SimpleVector.h"
00039 #include "TMatrixD.h"
00040
00041 #include <iostream>
00042
00043 using namespace edm;
00044 using namespace std;
00045 using namespace CLHEP;
00046
00047 class RandGaussQ;
00048
00049
00050 class BetaBoostEvtVtxGenerator : public edm::EDProducer{
00051 public:
00052 BetaBoostEvtVtxGenerator(const edm::ParameterSet & p);
00053 virtual ~BetaBoostEvtVtxGenerator();
00054
00056
00057 virtual HepMC::FourVector* newVertex() ;
00058 virtual void produce( edm::Event&, const edm::EventSetup& );
00059 virtual TMatrixD* GetInvLorentzBoost();
00060
00061
00063 void sigmaZ(double s=1.0);
00064
00066 void X0(double m=0) { fX0=m; }
00068 void Y0(double m=0) { fY0=m; }
00070 void Z0(double m=0) { fZ0=m; }
00071
00073 void Phi(double m=0) { phi_=m; }
00075 void Alpha(double m=0) { alpha_=m; }
00076 void Beta(double m=0) { beta_=m; }
00077
00079 void betastar(double m=0) { fbetastar=m; }
00081 void emittance(double m=0) { femittance=m; }
00082
00084 double BetaFunction(double z, double z0);
00085 CLHEP::HepRandomEngine& getEngine();
00086
00087 private:
00089 BetaBoostEvtVtxGenerator(const BetaBoostEvtVtxGenerator &p);
00091 BetaBoostEvtVtxGenerator& operator = (const BetaBoostEvtVtxGenerator & rhs );
00092
00093 double alpha_, phi_;
00094
00095 double beta_;
00096 double fX0, fY0, fZ0;
00097 double fSigmaZ;
00098
00099 double fbetastar, femittance;
00100 double falpha;
00101
00102 HepMC::FourVector* fVertex ;
00103 TMatrixD *boost_;
00104 double fTimeOffset;
00105
00106 CLHEP::HepRandomEngine* fEngine;
00107 edm::InputTag sourceLabel;
00108
00109 CLHEP::RandGaussQ* fRandom ;
00110
00111 bool verbosity_;
00112
00113 };
00114
00115
00116 BetaBoostEvtVtxGenerator::BetaBoostEvtVtxGenerator(const edm::ParameterSet & p ):
00117 fVertex(0), boost_(0), fTimeOffset(0), fEngine(0),
00118 sourceLabel(p.getParameter<edm::InputTag>("src")),
00119 verbosity_(p.getUntrackedParameter<bool>("verbosity",false))
00120 {
00121
00122 edm::Service<edm::RandomNumberGenerator> rng;
00123
00124 if ( ! rng.isAvailable()) {
00125
00126 throw cms::Exception("Configuration")
00127 << "The BaseEvtVtxGenerator requires the RandomNumberGeneratorService\n"
00128 "which is not present in the configuration file. You must add the service\n"
00129 "in the configuration file or remove the modules that require it.";
00130 }
00131
00132 CLHEP::HepRandomEngine& engine = rng->getEngine();
00133 fEngine = &engine;
00134 fRandom = new CLHEP::RandGaussQ(getEngine());
00135
00136 fX0 = p.getParameter<double>("X0")*cm;
00137 fY0 = p.getParameter<double>("Y0")*cm;
00138 fZ0 = p.getParameter<double>("Z0")*cm;
00139 fSigmaZ = p.getParameter<double>("SigmaZ")*cm;
00140 alpha_ = p.getParameter<double>("Alpha")*radian;
00141 phi_ = p.getParameter<double>("Phi")*radian;
00142 fbetastar = p.getParameter<double>("BetaStar")*cm;
00143 femittance = p.getParameter<double>("Emittance")*cm;
00144 fTimeOffset = p.getParameter<double>("TimeOffset")*ns*c_light;
00145 beta_=p.getParameter<double>("Beta");
00146 if (fSigmaZ <= 0) {
00147 throw cms::Exception("Configuration")
00148 << "Error in BetaBoostEvtVtxGenerator: "
00149 << "Illegal resolution in Z (SigmaZ is negative)";
00150 }
00151
00152 produces<bool>();
00153
00154 }
00155
00156 BetaBoostEvtVtxGenerator::~BetaBoostEvtVtxGenerator()
00157 {
00158 delete fVertex ;
00159 if (boost_ != 0 ) delete boost_;
00160 delete fRandom;
00161 }
00162
00163 CLHEP::HepRandomEngine& BetaBoostEvtVtxGenerator::getEngine(){
00164 return *fEngine;
00165 }
00166
00167
00168
00169 HepMC::FourVector* BetaBoostEvtVtxGenerator::newVertex() {
00170
00171
00172 double X,Y,Z;
00173
00174 double tmp_sigz = fRandom->fire(0., fSigmaZ);
00175 Z = tmp_sigz + fZ0;
00176
00177 double tmp_sigx = BetaFunction(Z,fZ0);
00178
00179 tmp_sigx /= sqrt(2.0);
00180 X = fRandom->fire(0.,tmp_sigx) + fX0;
00181
00182 double tmp_sigy = BetaFunction(Z,fZ0);
00183
00184 tmp_sigy /= sqrt(2.0);
00185 Y = fRandom->fire(0.,tmp_sigy) + fY0;
00186
00187 double tmp_sigt = fRandom->fire(0., fSigmaZ);
00188 double T = tmp_sigt + fTimeOffset;
00189
00190 if ( fVertex == 0 ) fVertex = new HepMC::FourVector();
00191 fVertex->set(X,Y,Z,T);
00192
00193 return fVertex;
00194 }
00195
00196 double BetaBoostEvtVtxGenerator::BetaFunction(double z, double z0)
00197 {
00198 return sqrt(femittance*(fbetastar+(((z-z0)*(z-z0))/fbetastar)));
00199
00200 }
00201
00202
00203 void BetaBoostEvtVtxGenerator::sigmaZ(double s)
00204 {
00205 if (s>=0 ) {
00206 fSigmaZ=s;
00207 }
00208 else {
00209 throw cms::Exception("LogicError")
00210 << "Error in BetaBoostEvtVtxGenerator::sigmaZ: "
00211 << "Illegal resolution in Z (negative)";
00212 }
00213 }
00214
00215 TMatrixD* BetaBoostEvtVtxGenerator::GetInvLorentzBoost() {
00216
00217
00218
00219
00220
00221
00222
00223 TMatrixD tmpboost(4,4);
00224 TMatrixD tmpboostZ(4,4);
00225 TMatrixD tmpboostXYZ(4,4);
00226
00227
00228
00229
00230
00231
00232
00233 tmpboost(0,0) = 1./cos(phi_);
00234 tmpboost(0,1) = - cos(alpha_)*sin(phi_);
00235 tmpboost(0,2) = - tan(phi_)*sin(phi_);
00236 tmpboost(0,3) = - sin(alpha_)*sin(phi_);
00237 tmpboost(1,0) = - cos(alpha_)*tan(phi_);
00238 tmpboost(1,1) = 1.;
00239 tmpboost(1,2) = cos(alpha_)*tan(phi_);
00240 tmpboost(1,3) = 0.;
00241 tmpboost(2,0) = 0.;
00242 tmpboost(2,1) = - cos(alpha_)*sin(phi_);
00243 tmpboost(2,2) = cos(phi_);
00244 tmpboost(2,3) = - sin(alpha_)*sin(phi_);
00245 tmpboost(3,0) = - sin(alpha_)*tan(phi_);
00246 tmpboost(3,1) = 0.;
00247 tmpboost(3,2) = sin(alpha_)*tan(phi_);
00248 tmpboost(3,3) = 1.;
00249
00250 double gama=1.0/sqrt(1-beta_*beta_);
00251 tmpboostZ(0,0)=gama;
00252 tmpboostZ(0,1)=0.;
00253 tmpboostZ(0,2)=-1.0*beta_*gama;
00254 tmpboostZ(0,3)=0.;
00255 tmpboostZ(1,0)=0.;
00256 tmpboostZ(1,1) = 1.;
00257 tmpboostZ(1,2)=0.;
00258 tmpboostZ(1,3)=0.;
00259 tmpboostZ(2,0)=-1.0*beta_*gama;
00260 tmpboostZ(2,1) = 0.;
00261 tmpboostZ(2,2)=gama;
00262 tmpboostZ(2,3) = 0.;
00263 tmpboostZ(3,0)=0.;
00264 tmpboostZ(3,1)=0.;
00265 tmpboostZ(3,2)=0.;
00266 tmpboostZ(3,3) = 1.;
00267
00268 tmpboostXYZ=tmpboostZ*tmpboost;
00269 tmpboostXYZ.Invert();
00270
00271
00272
00273 boost_ = new TMatrixD(tmpboostXYZ);
00274 if ( verbosity_ ) { boost_->Print(); }
00275
00276 return boost_;
00277 }
00278
00279 void BetaBoostEvtVtxGenerator::produce( Event& evt, const EventSetup& )
00280 {
00281
00282
00283 Handle<HepMCProduct> HepMCEvt ;
00284 evt.getByLabel( sourceLabel, HepMCEvt ) ;
00285
00286
00287
00288 HepMCEvt->applyVtxGen( newVertex() ) ;
00289
00290
00291 HepMCEvt->boostToLab( GetInvLorentzBoost(), "vertex" );
00292 HepMCEvt->boostToLab( GetInvLorentzBoost(), "momentum" );
00293
00294
00295 auto_ptr<bool> NewProduct(new bool(true)) ;
00296 evt.put( NewProduct ) ;
00297 return ;
00298 }
00299
00300
00301
00302
00303 #include "FWCore/Framework/interface/MakerMacros.h"
00304 DEFINE_FWK_MODULE(BetaBoostEvtVtxGenerator);