00001 #include "BDSBunchPtc.hh"
00002 #include "BDSDebug.hh"
00003 #include "BDSExecOptions.hh"
00004 #include <iostream>
00005
00006 #include <fstream>
00007 #if __cplusplus>199711 // test for c++11 features
00008 #include <regex>
00009 #endif
00010
00011 BDSBunchPtc::BDSBunchPtc() {
00012 #ifdef BDSDEBUG
00013 G4cout << __METHOD_NAME__ << G4endl;
00014 #endif
00015
00016
00017 fileName = "./inrays.madx";
00018
00019 iRay = 0;
00020 nRays = 0;
00021 }
00022
00023 BDSBunchPtc::~BDSBunchPtc() {
00024 #ifdef BDSDEBUG
00025 G4cout << __METHOD_NAME__ << G4endl;
00026 #endif
00027 for(std::vector<double*>::iterator i = ptcData.begin();i!=ptcData.end();++i) {
00028 delete[] *i;
00029 }
00030 }
00031
00032 void BDSBunchPtc::LoadPtcFile() {
00033 #ifdef BDSDEBUG
00034 G4cout << __METHOD_NAME__ << G4endl;
00035 #endif
00036
00037
00038 std::ifstream ifstr(fileName);
00039
00040 if (!ifstr)
00041 { G4cout << __METHOD_NAME__ << "\"" << fileName << "\" file doesn't exist - exiting as no input" << G4endl;
00042 exit(1);
00043 }
00044
00045 std::string line;
00046
00047 while(std::getline(ifstr,line)) {
00048
00049
00050 double x=0.0;
00051 double y=0.0;
00052 double px=0.0;
00053 double py=0.0;
00054 double t=0.0;
00055 double pt=0.0;
00056
00057 #if __cplusplus>199711
00058
00059
00060 std::regex rex("\\sx\\s*=\\s*([0-9eE.+-]+)");
00061 std::regex rey("\\sy\\s*=\\s*([0-9eE.+-]+)");
00062 std::regex repx("px\\s*=\\s*([0-9eE.+-]+)");
00063 std::regex repy("py\\s*=\\s*([0-9eE.+-]+)");
00064 std::regex ret("\\st\\s*=\\s*([0-9eE.+-]+)");
00065 std::regex rept("pt\\s*=\\s*([0-9eE.+-]+)");
00066
00067
00068 std::smatch smx;
00069 std::smatch smy;
00070 std::smatch smpx;
00071 std::smatch smpy;
00072 std::smatch smt;
00073 std::smatch smpt;
00074
00075
00076 std::regex_search(line,smx, rex);
00077 std::regex_search(line,smy, rey);
00078 std::regex_search(line,smpx,repx);
00079 std::regex_search(line,smpy,repy);
00080 std::regex_search(line,smt, ret);
00081 std::regex_search(line,smpt, rept);
00082
00083 if(smx.size() == 2) x = std::stod(smx[1]);
00084 if(smy.size() == 2) y = std::stod(smy[1]);
00085 if(smpx.size() == 2) px = std::stod(smpx[1]);
00086 if(smpy.size() == 2) py = std::stod(smpy[1]);
00087 if(smt.size() == 2) t = std::stod(smt[1]);
00088 if(smpt.size() == 2) pt = std::stod(smpt[1]);
00089 #else
00090 G4cout << __METHOD_NAME__ << " WARNING not using C++11 regex to parse file"
00091 << " - no particle coordinates read in - default all 0" << G4endl;
00092 #endif
00093
00094
00095 #ifdef BDSDEBUG
00096 G4cout << __METHOD_NAME__ << "read line " << line << G4endl;
00097 G4cout << __METHOD_NAME__ << "values " << x << " " << px << " " << y << " " << py << " " << t << " " << pt << G4endl;
00098 #endif
00099
00100 double *values = new double[6];
00101 values[0] = x;
00102 values[1] = px;
00103 values[2] = y;
00104 values[3] = py;
00105 values[4] = t;
00106 values[5] = pt;
00107
00108
00109 ptcData.push_back(values);
00110 }
00111
00112
00113 nRays = ptcData.size();
00114 BDSGlobalConstants::Instance()->SetNumberToGenerate(nRays);
00115
00116 return;
00117 }
00118
00119 void BDSBunchPtc::SetOptions(struct Options& opt) {
00120 #ifdef BDSDEBUG
00121 G4cout << __METHOD_NAME__ << " " << opt.distribFile << G4endl;
00122 #endif
00123
00124 BDSBunchInterface::SetOptions(opt);
00125 SetDistribFile((G4String)opt.distribFile);
00126 LoadPtcFile();
00127 }
00128
00129 void BDSBunchPtc::SetDistribFile(G4String distribFileName){
00130 G4String fullPATH = BDSExecOptions::Instance()->GetBDSIMPATH();
00131 fileName = fullPATH + distribFileName;
00132 }
00133
00134 void BDSBunchPtc::GetNextParticle(G4double& x0, G4double& y0, G4double& z0,
00135 G4double& xp, G4double& yp, G4double& zp,
00136 G4double& t , G4double& E, G4double& weight) {
00137 #ifdef BDSDEBUG
00138 G4cout << __METHOD_NAME__ << G4endl;
00139 #endif
00140 x0 = ptcData[iRay][0]*CLHEP::m+X0;
00141 y0 = ptcData[iRay][2]*CLHEP::m+Y0;
00142 z0 = ptcData[iRay][4]+Z0;
00143 xp = ptcData[iRay][1]*CLHEP::rad+Xp0;
00144 yp = ptcData[iRay][3]*CLHEP::rad+Yp0;
00145 t = (z0-Z0)/CLHEP::c_light+T0;
00146 E = BDSGlobalConstants::Instance()->GetParticleKineticEnergy() * (ptcData[iRay][5]+1.0);
00147 zp = CalculateZp(xp,yp,Zp0);
00148 weight = 1.0;
00149
00150 iRay++;
00151
00152
00153 if (iRay == nRays) iRay=0;
00154
00155 return;
00156 }
00157