CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_13_patch3/src/IOMC/EventVertexGenerators/src/BeamProfileVtxGenerator.cc

Go to the documentation of this file.
00001 
00002 // $Id: BeamProfileVtxGenerator.cc,v 1.12 2009/09/04 08:23:58 fabiocos Exp $
00003 
00004 #include "IOMC/EventVertexGenerators/interface/BeamProfileVtxGenerator.h"
00005 #include "FWCore/Utilities/interface/Exception.h"
00006 
00007 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00009 
00010 #include "CLHEP/Geometry/Transform3D.h"
00011 #include "CLHEP/Random/RandFlat.h"
00012 #include "CLHEP/Random/RandGaussQ.h"
00013 #include "CLHEP/Units/GlobalSystemOfUnits.h"
00014 #include "CLHEP/Units/GlobalPhysicalConstants.h"
00015 #include "HepMC/SimpleVector.h"
00016 
00017 #include<fstream>
00018 #include<string>
00019 
00020 BeamProfileVtxGenerator::BeamProfileVtxGenerator(const edm::ParameterSet & p) :
00021   BaseEvtVtxGenerator(p), fRandom(0) {
00022   
00023   meanX(p.getParameter<double>("BeamMeanX")*cm);
00024   meanY(p.getParameter<double>("BeamMeanY")*cm);
00025   beamPos(p.getParameter<double>("BeamPosition")*cm);
00026   sigmaX(p.getParameter<double>("BeamSigmaX")*cm);
00027   sigmaY(p.getParameter<double>("BeamSigmaY")*cm);
00028   double fMinEta = p.getParameter<double>("MinEta");
00029   double fMaxEta = p.getParameter<double>("MaxEta");
00030   double fMinPhi = p.getParameter<double>("MinPhi");
00031   double fMaxPhi = p.getParameter<double>("MaxPhi");
00032   eta(0.5*(fMinEta+fMaxEta));
00033   phi(0.5*(fMinPhi+fMaxPhi));
00034   psi(p.getParameter<double>("Psi"));
00035   nBinx = p.getParameter<int>("BinX");
00036   nBiny = p.getParameter<int>("BinY");
00037   ffile = p.getParameter<bool>("UseFile");
00038   fTimeOffset = p.getParameter<double>("TimeOffset")*ns*c_light;
00039   
00040   if (ffile) {
00041     std::string file = p.getParameter<std::string>("File");
00042     ifstream is(file.c_str(), std::ios::in);
00043     if (is) {
00044       double elem,sum=0;
00045       while (!is.eof()) {
00046         is >> elem;
00047         fdistn.push_back(elem);
00048         sum += elem;
00049       }
00050       if (((int)(fdistn.size())) >= nBinx*nBiny) {
00051         double last = 0;
00052         for (unsigned int i=0; i<fdistn.size(); i++) {
00053           fdistn[i] /= sum;
00054           fdistn[i] += last;
00055           last       = fdistn[i];
00056         }
00057         setType(false);
00058       } else {
00059         ffile = false;
00060       }
00061     } else {
00062       ffile = false;
00063     }
00064   } 
00065   if (!ffile) {
00066     setType(p.getParameter<bool>("GaussianProfile"));
00067   }
00068 
00069   edm::LogInfo("VertexGenerator") << "BeamProfileVtxGenerator: with "
00070                                   << "beam along eta = " << fEta 
00071                                   << " (Theta = " << fTheta/deg 
00072                                   << ") phi = " << fPhi/deg 
00073                                   << ") psi = " << fPsi/deg 
00074                                   << " centred at (" << fMeanX << ", " 
00075                                   << fMeanY << ", "  << fMeanZ << ") "
00076                                   << "and spread (" << fSigmaX << ", "
00077                                   << fSigmaY << ") of type Gaussian = "
00078                                   << fType << " use file " << ffile;
00079   if (ffile) 
00080     edm::LogInfo("VertexGenerator") << "With " << nBinx << " bins "
00081                                     << " along X and " << nBiny 
00082                                     << " bins along Y";
00083 }
00084 
00085 BeamProfileVtxGenerator::~BeamProfileVtxGenerator() {
00086   delete fRandom;
00087 }
00088 
00089 
00090 //Hep3Vector * BeamProfileVtxGenerator::newVertex() {
00091 HepMC::FourVector* BeamProfileVtxGenerator::newVertex() {
00092   double aX, aY;
00093   if (ffile) {
00094     double r1 = (dynamic_cast<CLHEP::RandFlat*>(fRandom))->fire();
00095     int ixy = 0, ix, iy;
00096     for (unsigned int i=0; i<fdistn.size(); i++) {
00097       if (r1 > fdistn[i]) ixy = i+1;
00098     }
00099     if (ixy >= nBinx*nBiny) {
00100       ix = nBinx-1; iy = nBiny-1;
00101     } else {
00102       ix = ixy%nBinx; iy = (ixy-ix)/nBinx;
00103     }
00104     aX = 0.5*(2*ix-nBinx+2*(dynamic_cast<CLHEP::RandFlat*>(fRandom))->fire())*fSigmaX + fMeanX ;
00105     aY = 0.5*(2*iy-nBiny+2*(dynamic_cast<CLHEP::RandFlat*>(fRandom))->fire())*fSigmaY + fMeanY ;
00106   } else {
00107     if (fType) {
00108       aX = fSigmaX*(dynamic_cast<CLHEP::RandGaussQ*>(fRandom))->fire() +fMeanX;
00109       aY = fSigmaY*(dynamic_cast<CLHEP::RandGaussQ*>(fRandom))->fire() +fMeanY;
00110     } else {
00111       aX = (dynamic_cast<CLHEP::RandFlat*>(fRandom))->fire(-0.5*fSigmaX,0.5*fSigmaX) + fMeanX ;
00112       aY = (dynamic_cast<CLHEP::RandFlat*>(fRandom))->fire(-0.5*fSigmaY,0.5*fSigmaY) + fMeanY;
00113     }
00114   }
00115 
00116   double xp, yp, zp ;
00117   if( 2.*M_PI < fabs(fPsi) )
00118   {
00119      xp = -aX*cos(fTheta)*cos(fPhi) +aY*sin(fPhi) +fMeanZ*sin(fTheta)*cos(fPhi);
00120      yp = -aX*cos(fTheta)*sin(fPhi) -aY*cos(fPhi) +fMeanZ*sin(fTheta)*sin(fPhi);
00121      zp =  aX*sin(fTheta)                         +fMeanZ*cos(fTheta);
00122   }
00123   else // for endcap testbeam, use 3rd angle psi
00124   {
00125      const HepGeom::Vector3D<double>  av ( aX, aY, fMeanZ ) ;
00126 
00127 /*
00128      static const double kRadToDeg ( 180./M_PI ) ;
00129      std::cout<<"theta = "<<std::setw(7) << std::setiosflags( std::ios::fixed ) << std::setprecision(5)
00130               <<fTheta*kRadToDeg<<", phi="<<std::setw(7) << std::setiosflags( std::ios::fixed ) << std::setprecision(5)
00131               << fPhi*kRadToDeg <<", PSI="<<std::setw(7) << std::setiosflags( std::ios::fixed ) << std::setprecision(5)
00132               <<fPsi*kRadToDeg<<std::endl ;
00133 */
00134      const HepGeom::RotateZ3D R1 ( fPhi - M_PI ) ;
00135      const HepGeom::Point3D<double>  xUnit ( 0,1,0 ) ;
00136      const HepGeom::Point3D<double>  zUnit ( 0,0,1 ) ;
00137      const HepGeom::Transform3D RXRZ ( HepGeom::Rotate3D( -fTheta, R1*xUnit )*R1 ) ;
00138      const HepGeom::Transform3D TRF ( HepGeom::Rotate3D(fPsi,RXRZ*zUnit)*RXRZ ) ;
00139 /*
00140      std::cout<<"\n\n$$$$$$$$$$$Transform="
00141               <<" thetaZ = "<<std::setw(7) << std::setiosflags( std::ios::fixed ) << std::setprecision(5)
00142               <<TRF.getRotation().thetaZ()*kRadToDeg
00143               <<", phiZ = "<<std::setw(7) << std::setiosflags( std::ios::fixed ) << std::setprecision(5)
00144               <<TRF.getRotation().phiZ()*kRadToDeg
00145               <<", thetaY = "<<std::setw(7) << std::setiosflags( std::ios::fixed ) << std::setprecision(5)
00146               <<TRF.getRotation().thetaY()*kRadToDeg
00147               <<", phiY = "<<std::setw(7) << std::setiosflags( std::ios::fixed ) << std::setprecision(5)
00148               <<TRF.getRotation().phiY()*kRadToDeg
00149               <<", thetaX = "<<std::setw(7) << std::setiosflags( std::ios::fixed ) << std::setprecision(5)
00150               <<TRF.getRotation().thetaX()*kRadToDeg
00151               <<", phiX = "<<std::setw(7) << std::setiosflags( std::ios::fixed ) << std::setprecision(5)
00152               <<TRF.getRotation().phiX()*kRadToDeg
00153               <<std::setw(7) << std::setiosflags( std::ios::fixed ) << std::setprecision(5)
00154               <<TRF.getTranslation()<<std::endl ;
00155 */
00156      const HepGeom::Vector3D<double>  pv ( TRF*av ) ;
00157 
00158      xp = pv.x() ;
00159      yp = pv.y() ;
00160      zp = pv.z() ;
00161   }
00162   //if (fVertex == 0) fVertex = new CLHEP::Hep3Vector;
00163   //fVertex->set(xp, yp, zp);
00164   if (fVertex == 0 ) fVertex = new HepMC::FourVector() ;
00165   fVertex->set(xp, yp, zp, fTimeOffset );
00166 
00167   LogDebug("VertexGenerator") << "BeamProfileVtxGenerator: Vertex created "
00168                               << "at (" << xp << ", " << yp << ", "
00169                               << zp << ", " << fTimeOffset << ")";
00170   return fVertex;
00171 }
00172 
00173 void BeamProfileVtxGenerator::sigmaX(double s) { 
00174 
00175   if (s>=0) {
00176     fSigmaX = s; 
00177   } else {
00178     edm::LogWarning("VertexGenerator") << "Warning BeamProfileVtxGenerator:"
00179                                        << " Illegal resolution in X " << s
00180                                        << "- set to default value 0 cm";
00181     fSigmaX = 0;
00182   }
00183 }
00184 
00185 void BeamProfileVtxGenerator::sigmaY(double s) { 
00186 
00187   if (s>=0) {
00188     fSigmaY = s; 
00189   } else {
00190     edm::LogWarning("VertexGenerator") << "Warning BeamProfileVtxGenerator:"
00191                                        << " Illegal resolution in Y " << s
00192                                        << "- set to default value 0 cm";
00193     fSigmaY = 0;
00194   }
00195 }
00196 
00197 void BeamProfileVtxGenerator::eta(double s) { 
00198   fEta   = s; 
00199   fTheta = 2.0*atan(exp(-fEta));
00200 }
00201 
00202 void BeamProfileVtxGenerator::setType(bool s) { 
00203 
00204   fType = s;
00205   delete fRandom;
00206   
00207   if (fType == true)
00208     fRandom = new CLHEP::RandGaussQ(getEngine());
00209   else
00210     fRandom = new CLHEP::RandFlat(getEngine());
00211 }