00001
00002
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/Random/RandFlat.h"
00011 #include "CLHEP/Random/RandGaussQ.h"
00012 #include "CLHEP/Units/SystemOfUnits.h"
00013 #include "CLHEP/Units/PhysicalConstants.h"
00014
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.getUntrackedParameter<double>("BeamMeanX",0.0)*cm);
00024 meanY(p.getUntrackedParameter<double>("BeamMeanY",0.0)*cm);
00025 beamPos(p.getUntrackedParameter<double>("BeamPosition",0.0)*cm);
00026 sigmaX(p.getUntrackedParameter<double>("BeamSigmaX",0.0)*cm);
00027 sigmaY(p.getUntrackedParameter<double>("BeamSigmaY",0.0)*cm);
00028 double fMinEta = p.getUntrackedParameter<double>("MinEta",-5.5);
00029 double fMaxEta = p.getUntrackedParameter<double>("MaxEta",5.5);
00030 double fMinPhi = p.getUntrackedParameter<double>("MinPhi",-3.14159265358979323846);
00031 double fMaxPhi = p.getUntrackedParameter<double>("MaxPhi", 3.14159265358979323846);
00032 eta(0.5*(fMinEta+fMaxEta));
00033 phi(0.5*(fMinPhi+fMaxPhi));
00034 nBinx = p.getUntrackedParameter<int>("BinX",50);
00035 nBiny = p.getUntrackedParameter<int>("BinY",50);
00036 ffile = p.getUntrackedParameter<bool>("UseFile",false);
00037 fTimeOffset = p.getParameter<double>("TimeOffset")*ns*c_light;
00038
00039 if (ffile) {
00040 std::string file = p.getUntrackedParameter<std::string>("File","beam.profile");
00041 ifstream is(file.c_str(), std::ios::in);
00042 if (is) {
00043 double elem,sum=0;
00044 while (!is.eof()) {
00045 is >> elem;
00046 fdistn.push_back(elem);
00047 sum += elem;
00048 }
00049 if (((int)(fdistn.size())) >= nBinx*nBiny) {
00050 double last = 0;
00051 for (unsigned int i=0; i<fdistn.size(); i++) {
00052 fdistn[i] /= sum;
00053 fdistn[i] += last;
00054 last = fdistn[i];
00055 }
00056 setType(false);
00057 } else {
00058 ffile = false;
00059 }
00060 } else {
00061 ffile = false;
00062 }
00063 }
00064 if (!ffile) {
00065 setType(p.getUntrackedParameter<bool>("GaussianProfile",true));
00066 }
00067
00068 edm::LogInfo("BeamProfileVtxGenerator") << "BeamProfileVtxGenerator: with "
00069 << "beam along eta = " << fEta
00070 << " (Theta = " << fTheta/deg
00071 << ") phi = " << fPhi/deg
00072 << " centred at (" << fMeanX << ", "
00073 << fMeanY << ", " << fMeanZ << ") "
00074 << "and spread (" << fSigmaX << ", "
00075 << fSigmaY << ") of type Gaussian = "
00076 << fType << " use file " << ffile;
00077 if (ffile)
00078 edm::LogInfo("BeamProfileVtxGenerator") << "With " << nBinx << " bins "
00079 << " along X and " << nBiny
00080 << " bins along Y";
00081 }
00082
00083 BeamProfileVtxGenerator::~BeamProfileVtxGenerator() {
00084 delete fRandom;
00085 }
00086
00087
00088
00089 HepMC::FourVector* BeamProfileVtxGenerator::newVertex() {
00090 double aX, aY;
00091 if (ffile) {
00092 double r1 = (dynamic_cast<CLHEP::RandFlat*>(fRandom))->fire();
00093 int ixy = 0, ix, iy;
00094 for (unsigned int i=0; i<fdistn.size(); i++) {
00095 if (r1 > fdistn[i]) ixy = i+1;
00096 }
00097 if (ixy >= nBinx*nBiny) {
00098 ix = nBinx-1; iy = nBiny-1;
00099 } else {
00100 ix = ixy%nBinx; iy = (ixy-ix)/nBinx;
00101 }
00102 aX = 0.5*(2*ix-nBinx+2*(dynamic_cast<CLHEP::RandFlat*>(fRandom))->fire())*fSigmaX + fMeanX ;
00103 aY = 0.5*(2*iy-nBiny+2*(dynamic_cast<CLHEP::RandFlat*>(fRandom))->fire())*fSigmaY + fMeanY ;
00104 } else {
00105 if (fType) {
00106 aX = fSigmaX*(dynamic_cast<CLHEP::RandGaussQ*>(fRandom))->fire() +fMeanX;
00107 aY = fSigmaY*(dynamic_cast<CLHEP::RandGaussQ*>(fRandom))->fire() +fMeanY;
00108 } else {
00109 aX = (dynamic_cast<CLHEP::RandFlat*>(fRandom))->fire(-0.5*fSigmaX,0.5*fSigmaX) + fMeanX ;
00110 aY = (dynamic_cast<CLHEP::RandFlat*>(fRandom))->fire(-0.5*fSigmaY,0.5*fSigmaY) + fMeanY;
00111 }
00112 }
00113 double xp = -aX*cos(fTheta)*cos(fPhi) +aY*sin(fPhi) +fMeanZ*sin(fTheta)*cos(fPhi);
00114 double yp = -aX*cos(fTheta)*sin(fPhi) -aY*cos(fPhi) +fMeanZ*sin(fTheta)*sin(fPhi);
00115 double zp = aX*sin(fTheta) +fMeanZ*cos(fTheta);
00116
00117
00118
00119 if (fVertex == 0 ) fVertex = new HepMC::FourVector() ;
00120 fVertex->set(xp, yp, zp, fTimeOffset );
00121
00122
00123
00124 return fVertex;
00125 }
00126
00127 void BeamProfileVtxGenerator::sigmaX(double s) {
00128
00129 if (s>=0) {
00130 fSigmaX = s;
00131 } else {
00132 edm::LogWarning("BeamProfileVtxGenerator") << "Warning BeamProfileVtxGenerator:"
00133 << " Illegal resolution in X " << s
00134 << "- set to default value 0 cm";
00135 fSigmaX = 0;
00136 }
00137 }
00138
00139 void BeamProfileVtxGenerator::sigmaY(double s) {
00140
00141 if (s>=0) {
00142 fSigmaY = s;
00143 } else {
00144 edm::LogWarning("BeamProfileVtxGenerator") << "Warning BeamProfileVtxGenerator:"
00145 << " Illegal resolution in Y " << s
00146 << "- set to default value 0 cm";
00147 fSigmaY = 0;
00148 }
00149 }
00150
00151 void BeamProfileVtxGenerator::eta(double s) {
00152 fEta = s;
00153 fTheta = 2.0*atan(exp(-fEta));
00154 }
00155
00156 void BeamProfileVtxGenerator::setType(bool s) {
00157
00158 fType = s;
00159 delete fRandom;
00160
00161 if (fType == true)
00162 fRandom = new CLHEP::RandGaussQ(getEngine());
00163 else
00164 fRandom = new CLHEP::RandFlat(getEngine());
00165 }