/home/cern/BDSIM_new/src/BDSBunch.cc

00001 #include "BDSGlobalConstants.hh"
00002 #include "BDSBunch.hh"
00003 #include <iostream>
00004 #include "globals.hh"
00005 
00006 const int DEBUG = 0;
00007 
00008 using namespace std;
00009 
00010 extern G4bool verbose;      // run options
00011 extern G4bool verboseStep;
00012 extern G4bool verboseEvent;
00013 extern G4int verboseEventNumber;
00014 extern G4bool isBatch;
00015 
00016 extern G4int nptwiss;
00017 
00018 BDSBunch::BDSBunch()
00019 {
00020   X0 = 0;
00021   Y0 = 0;
00022   Z0 = 0;
00023   T0 = 0;
00024 
00025   Xp0 = 0;
00026   Yp0 = 0;
00027   Zp0 = 1;
00028 
00029   sigmaX = 0;
00030   sigmaY = 0;
00031   sigmaT = 0;
00032 
00033   sigmaXp = 0;
00034   sigmaYp = 0;
00035 
00036   energySpread = 0;
00037 
00038   betaX = 0;
00039   betaY = 0;
00040   alphaX = 0;
00041   alphaY = 0;
00042   emitX = 0;
00043   emitY = 0;
00044 
00045   partId = 0;
00046 
00047   ignoreLines = 0;
00048 
00049   distribType = -1;
00050   
00051   GaussGen=new RandGauss(*HepRandom::getTheEngine());
00052   FlatGen=new RandFlat(*HepRandom::getTheEngine());
00053   
00054 }
00055 
00056 BDSBunch::~BDSBunch()
00057 {
00058   delete GaussGen;
00059   delete FlatGen;
00060 }
00061 
00062 // set options from gmad
00063 
00064 G4double val;
00065 
00066 void BDSBunch::SetOptions(struct Options& opt)
00067 {
00068   map<const G4String, int, strCmp> distType;
00069   distType["gauss"]=_GAUSSIAN;
00070   distType["ring"]=_RING;
00071   distType["square"]=_SQUARE;
00072   distType["circle"]=_CIRCLE;
00073   distType["guineapig_bunch"]=_GUINEAPIG_BUNCH;
00074   distType["guineapig_pairs"]=_GUINEAPIG_PAIRS;
00075   distType["guineapig_slac"]=_GUINEAPIG_SLAC;
00076   distType["cain"]=_CAIN;
00077   distType["eshell"]=_ESHELL;
00078 
00079 #define _skip(nvalues) for(G4int i=0;i<nvalues;i++) InputBunchFile>>val;
00080 
00081   // twiss parameters - set always if present
00082   SetBetaX(opt.betx);
00083   SetBetaY(opt.bety);
00084   SetAlphaX(opt.alfx);
00085   SetAlphaY(opt.alfy);
00086   SetEmitX(opt.emitx);
00087   SetEmitY(opt.emity);
00088 
00089   ignoreLines = opt.nlinesIgnore;
00090 
00091   map<const G4String,int>::iterator iter;
00092   iter = distType.find(opt.distribType);
00093   if(iter!=distType.end()) 
00094     distribType = (*iter).second;
00095   if (DEBUG) G4cout<<"distrType -> "<<opt.distribType<<G4endl;
00096 
00097   //
00098   // global parameters
00099   //
00100   X0 = opt.X0;
00101   Y0 = opt.Y0;
00102   Z0 = opt.Z0;
00103   T0 = opt.T0;
00104   Xp0 = opt.Xp0;
00105   Yp0 = opt.Yp0;
00106   if (opt.Zp0 < 0)
00107     Zp0 = -sqrt(1.-Xp0*Xp0-Yp0*Yp0);
00108   else
00109     Zp0 = sqrt(1.-Xp0*Xp0-Yp0*Yp0);
00110 
00111 
00112   //
00113   // specific parameters which depend on distribution type
00114   //
00115   switch(distribType){
00116 
00117   case _GAUSSIAN:
00118     {
00119       SetSigmaX(opt.sigmaX); 
00120       SetSigmaY(opt.sigmaY);
00121       SetSigmaXp(opt.sigmaXp);
00122       SetSigmaYp(opt.sigmaYp);
00123       SetSigmaT(opt.sigmaT);
00124       energySpread = opt.sigmaE;
00125       break;
00126     } 
00127 
00128   case _RING:
00129     {
00130       rMin = opt.Rmin;
00131       rMax = opt.Rmax;
00132       energySpread = opt.sigmaE;
00133       break;
00134     } 
00135     
00136   case _ESHELL:
00137     {
00138       shellx = opt.shellX;
00139       shelly = opt.shellY;
00140       shellxp = opt.shellXp;
00141       shellyp = opt.shellYp;
00142       energySpread = opt.sigmaE;
00143       break;
00144     }
00145 
00146   case _GUINEAPIG_BUNCH:
00147     {
00148       inputfile = opt.distribFile;
00149       InputBunchFile.open(inputfile);
00150       if(!InputBunchFile.good()) 
00151         { G4cerr<<"Cannot open bunch file "<<inputfile<<G4endl; exit(1); }
00152       if(DEBUG) 
00153         G4cout<<"GUINEAPIG_BUNCH: skipping "<<opt.nlinesIgnore<<"  lines"<<G4endl;
00154       _skip(opt.nlinesIgnore * 6);
00155       break;
00156     } 
00157 
00158   case _GUINEAPIG_SLAC:
00159     {
00160       inputfile = opt.distribFile;
00161       InputBunchFile.open(inputfile);
00162       if(!InputBunchFile.good())  
00163         { G4cerr<<"Cannot open bunch file "<<inputfile<<G4endl; exit(1); }
00164       if(DEBUG) 
00165         G4cout<<"GUINEAPIG_SLAC: skipping "<<opt.nlinesIgnore<<"  lines"<<G4endl;
00166       _skip(opt.nlinesIgnore * 6);
00167       break;
00168     } 
00169 
00170   case _GUINEAPIG_PAIRS:
00171     {
00172       inputfile = opt.distribFile;
00173       InputBunchFile.open(inputfile);
00174       if(!InputBunchFile.good()) 
00175         { G4cerr<<"Cannot open bunch file "<<inputfile<<G4endl; exit(1); }
00176       if(DEBUG) 
00177         G4cout<<"GUINEAPIG_PAIRS: skipping "<<opt.nlinesIgnore<<"  lines"<<G4endl;
00178       _skip(opt.nlinesIgnore * 7);
00179       break;
00180     }
00181 
00182   case _CAIN:
00183     {
00184       inputfile = opt.distribFile;
00185       InputBunchFile.open(inputfile);
00186       if(!InputBunchFile.good()) 
00187         { G4cerr<<"Cannot open bunch file "<<inputfile<<G4endl; exit(1); }
00188       if(DEBUG) 
00189         G4cout<<"CAIN: skipping "<<opt.nlinesIgnore<<"  lines"<<G4endl;
00190       _skip(opt.nlinesIgnore * 14);
00191       break;
00192     } 
00193     //else
00194     //assuming the format is "field[unit]:field[unit]:..." - User Defined
00195   default:
00196     {
00197       distribType = _UDEF; 
00198 
00199       // construct the list of read attributes
00200       
00201       G4String unparsed_str = opt.distribType; 
00202       G4int pos = unparsed_str.find(":");
00203       
00204       struct Doublet sd;
00205       
00206       while(pos > 0)
00207         {
00208           pos = unparsed_str.find(":");
00209           G4String token = unparsed_str.substr(0,pos);
00210           unparsed_str = unparsed_str.substr(pos+1);
00211           if (DEBUG) G4cout<<"token ->"<<token<<G4endl;
00212           if (DEBUG) G4cout<<"unparsed_str ->"<<unparsed_str<<G4endl;
00213           if (DEBUG) G4cout<<"pos ->"<<pos<<G4endl;
00214           
00215           // see if the token has a meeting
00216           if( token.length() > 2) {
00217             if(token.substr(0,1)=="E") {
00218               if (DEBUG) G4cout<<"E!"<<G4endl;
00219               G4String rest = token.substr(1);
00220               if (DEBUG) G4cout<<"rest ->"<<rest<<G4endl;
00221               G4int pos1 = rest.find("[");
00222               G4int pos2 = rest.find("]");
00223               if(pos1 < 0 || pos2 < 0) {
00224                 G4cerr<<"unit format wrong!!!"<<G4endl;
00225               } else {
00226                 G4String fmt = rest.substr(pos1+1,pos2-1);
00227                 if (DEBUG) G4cout<<"fmt ->"<<fmt<<G4endl;
00228                 sd.name = "E"; 
00229 
00230                 if(fmt=="GeV") sd.unit=1;
00231                 if(fmt=="MeV") sd.unit=1.e-3;
00232                 if(fmt=="KeV") sd.unit=1.e-6;
00233                 if(fmt=="eV") sd.unit=1.e-9;
00234                 
00235                 fields.push_back(sd);
00236               }
00237             }
00238             if(token.substr(0,1)=="t") {
00239               if (DEBUG) G4cout<<"t!"<<G4endl;
00240               G4String rest = token.substr(1);
00241               if (DEBUG) G4cout<<"rest ->"<<rest<<G4endl;
00242               G4int pos1 = rest.find("[");
00243               G4int pos2 = rest.find("]");
00244               if(pos1 < 0 || pos2 < 0) {
00245                 G4cerr<<"unit format wrong!!!"<<G4endl;
00246               } else {
00247                 G4String fmt = rest.substr(pos1+1,pos2-1);
00248                 if (DEBUG) G4cout<<"fmt ->"<<fmt<<G4endl;
00249                 sd.name = "t"; 
00250 
00251                 if(fmt=="s") sd.unit=1;
00252                 if(fmt=="ms") sd.unit=1.e-3;
00253                 if(fmt=="mus") sd.unit=1.e-6;
00254                 if(fmt=="ns") sd.unit=1.e-9;
00255                 if(fmt=="mm/c") sd.unit=(mm/c_light)/s;
00256                 if(fmt=="nm/c") sd.unit=(nm/c_light)/s;
00257 
00258                 fields.push_back(sd);
00259 
00260               }
00261             }
00262             if( (token.substr(0,1)=="x") && (token.substr(1,1)!="p") ) {
00263               if (DEBUG) G4cout<<"x!"<<G4endl;
00264               G4String rest = token.substr(1);
00265               if (DEBUG) G4cout<<"rest ->"<<rest<<G4endl;
00266               G4int pos1 = rest.find("[");
00267               G4int pos2 = rest.find("]");
00268               if(pos1 < 0 || pos2 < 0) {
00269                 G4cerr<<"unit format wrong!!!"<<G4endl;
00270               } else {
00271                 G4String fmt = rest.substr(pos1+1,pos2-1);
00272                 if (DEBUG) G4cout<<"fmt ->"<<fmt<<G4endl;
00273                 sd.name="x";
00274                 
00275                 if(fmt=="m") sd.unit=1;
00276                 if(fmt=="cm") sd.unit=1.e-2;
00277                 if(fmt=="mm") sd.unit=1.e-3;
00278                 if(fmt=="mum") sd.unit=1.e-6;
00279                 if(fmt=="nm") sd.unit=1.e-9;
00280                 
00281                 fields.push_back(sd);
00282                 
00283               }
00284             }
00285             if(token.substr(0,1)=="y" && token.substr(1,1)!="p" ) {
00286               if (DEBUG) G4cout<<"y!"<<G4endl;
00287               G4String rest = token.substr(1);
00288               if (DEBUG) G4cout<<"rest ->"<<rest<<G4endl;
00289               G4int pos1 = rest.find("[");
00290               G4int pos2 = rest.find("]");
00291               if(pos1 < 0 || pos2 < 0) {
00292                 G4cerr<<"unit format wrong!!!"<<G4endl;
00293               } else {
00294                 G4String fmt = rest.substr(pos1+1,pos2-1);
00295                 if (DEBUG) G4cout<<"fmt ->"<<fmt<<G4endl;
00296                 sd.name="y";
00297                 
00298                 if(fmt=="m") sd.unit=1;
00299                 if(fmt=="cm") sd.unit=1.e-2;
00300                 if(fmt=="mm") sd.unit=1.e-3;
00301                 if(fmt=="mum") sd.unit=1.e-6;
00302                 if(fmt=="nm") sd.unit=1.e-9;
00303                 
00304                 fields.push_back(sd);
00305               }
00306             }
00307             if(token.substr(0,1)=="z" && token.substr(1,1)!="p" ) {
00308               if (DEBUG) G4cout<<"z!"<<G4endl;
00309               G4String rest = token.substr(1);
00310               if (DEBUG) G4cout<<"rest ->"<<rest<<G4endl;
00311               G4int pos1 = rest.find("[");
00312               G4int pos2 = rest.find("]");
00313               if(pos1 < 0 || pos2 < 0) {
00314                 G4cerr<<"unit format wrong!!!"<<G4endl;
00315               } else {
00316                 G4String fmt = rest.substr(pos1+1,pos2-1);
00317                 if (DEBUG) G4cout<<"fmt ->"<<fmt<<G4endl;
00318                 sd.name="z";
00319 
00320                 if(fmt=="m") sd.unit=1;
00321                 if(fmt=="cm") sd.unit=1.e-2;
00322                 if(fmt=="mm") sd.unit=1.e-3;
00323                 if(fmt=="mum") sd.unit=1.e-6;
00324                 if(fmt=="nm") sd.unit=1.e-9;
00325                 
00326                 fields.push_back(sd);
00327               }
00328             }
00329             if(token.substr(0,2)=="xp") {
00330               if (DEBUG) G4cout<<"xp!"<<G4endl;
00331               G4String rest = token.substr(2);
00332               if (DEBUG) G4cout<<"rest ->"<<rest<<G4endl;
00333               G4int pos1 = rest.find("[");
00334               G4int pos2 = rest.find("]");
00335               if(pos1 < 0 || pos2 < 0) {
00336                 G4cerr<<"unit format wrong!!!"<<G4endl;
00337               } else {
00338                 G4String fmt = rest.substr(pos1+1,pos2-1);
00339                 if (DEBUG) G4cout<<"fmt ->"<<fmt<<G4endl;
00340                 sd.name="xp";
00341                 
00342                 if(fmt=="rad") sd.unit=1;
00343                 if(fmt=="mrad") sd.unit=1.e-3;
00344                 
00345                 fields.push_back(sd);
00346                 
00347               }
00348             }
00349             if(token.substr(0,2)=="yp") {
00350               if (DEBUG) G4cout<<"yp!"<<G4endl;
00351               G4String rest = token.substr(2);
00352               if (DEBUG) G4cout<<"rest ->"<<rest<<G4endl;
00353               G4int pos1 = rest.find("[");
00354               G4int pos2 = rest.find("]");
00355               if(pos1 < 0 || pos2 < 0) {
00356                 G4cerr<<"unit format wrong!!!"<<G4endl;
00357               } else {
00358                 G4String fmt = rest.substr(pos1+1,pos2-1);
00359                 if (DEBUG) G4cout<<"fmt ->"<<fmt<<G4endl;
00360                 sd.name="yp";
00361                 
00362                 if(fmt=="rad") sd.unit=1;
00363                 if(fmt=="mrad") sd.unit=1.e-3;
00364                 
00365                 fields.push_back(sd);
00366               }
00367             }
00368             if(token.substr(0,2)=="zp") {
00369               if (DEBUG) G4cout<<"zp!"<<G4endl;
00370               G4String rest = token.substr(2);
00371               if (DEBUG) G4cout<<"rest ->"<<rest<<G4endl;
00372               G4int pos1 = rest.find("[");
00373               G4int pos2 = rest.find("]");
00374               if(pos1 < 0 || pos2 < 0) {
00375                 G4cerr<<"unit format wrong!!!"<<G4endl;
00376               } else {
00377                 G4String fmt = rest.substr(pos1+1,pos2-1);
00378                 if (DEBUG) G4cout<<"fmt ->"<<fmt<<G4endl;
00379                 sd.name="zp";
00380                 
00381                 if(fmt=="rad") sd.unit=1;
00382                 if(fmt=="mrad") sd.unit=1.e-3;
00383                 
00384                 fields.push_back(sd);
00385               }
00386             }
00387           }
00388           else
00389             if(token=="pt") {
00390               if (DEBUG) G4cout<<"pt!"<<G4endl;
00391               sd.name="pt";
00392               sd.unit=1;
00393               fields.push_back(sd);
00394             }
00395           else {
00396             G4cerr << "Cannot determine bunch data format" << G4endl; exit(1);
00397           }
00398           
00399         }
00400 
00401       inputfile = opt.distribFile;
00402       InputBunchFile.open(inputfile);
00403       if(!InputBunchFile.good()) {
00404         G4cerr<<"Cannot open bunch file "<<inputfile<<G4endl; 
00405         G4cerr<<"Reverting to gaussian bunch type" << G4endl;
00406         distribType = _GAUSSIAN;
00407       }
00408     }
00409   }
00410   return;  
00411 }
00412 
00413 // get initial bunch distribution parameters in Gaussian case 
00414 G4double BDSBunch::GetSigmaT() 
00415 {
00416   return sigmaT;
00417 } 
00418 
00419 G4double BDSBunch::GetSigmaX()
00420 {
00421 
00422   return sigmaX;
00423 }
00424  
00425 G4double BDSBunch::GetSigmaY()
00426 {
00427   return sigmaY;
00428 } 
00429 
00430 G4double BDSBunch::GetSigmaXp()
00431 {
00432   return sigmaXp;
00433 }
00434 
00435 G4double BDSBunch::GetSigmaYp()
00436 {
00437   return sigmaYp;
00438 }
00439 
00440 // generate primary coordinates
00441 
00442 G4double BDSBunch::GetNextX()
00443 {
00444   return sigmaX * GaussGen->shoot() * m;
00445 } 
00446 
00447 G4double BDSBunch::GetNextY()
00448 {
00449   return sigmaY * GaussGen->shoot() * m;
00450 }
00451 
00452 G4double BDSBunch::GetNextZ()
00453 {
00454   return 0;
00455 }
00456 
00457 
00458 
00459 G4double BDSBunch::GetNextXp()
00460 {
00461   return sigmaXp * GaussGen->shoot();
00462 }
00463 
00464 G4double BDSBunch::GetNextYp()
00465 {
00466   return sigmaYp * GaussGen->shoot();
00467 }
00468 
00469 G4double BDSBunch::GetNextT()
00470 {
00471   return - sigmaT* (1.-2.*GaussGen->shoot());
00472 }
00473 
00474 
00475 void BDSBunch::GetNextParticle(G4double& x0,G4double& y0,G4double& z0,
00476                                G4double& xp,G4double& yp,G4double& zp,
00477                                G4double& t, G4double& E)
00478 {
00479 
00480   if (DEBUG) G4cout<<"Twiss: "<<betaX<<" "<<betaY<<" "<<alphaX<<" "<<alphaY<<" "<<emitX<<" "<<emitY<<G4endl;
00481 
00482   if(verboseStep) G4cout<<"distribution type: "<<distribType<<G4endl;
00483 
00484   double r, phi;
00485   // Rescal must be at the top of GetNextParticle
00486   if(BDSGlobals->DoTwiss() && partId<nptwiss)
00487     {
00488       // temp numbers - to be replaced by parsed parameters
00489 
00490       
00491       G4double sigx = sqrt(betaX*emitX);
00492       G4double sigxp= sqrt(emitX / betaX);
00493       
00494       G4double sigy = sqrt(betaY*emitY);
00495       G4double sigyp= sqrt(emitY / betaY);
00496 
00497       G4double pi = 2.*asin(1.);
00498 
00499       partId++;
00500       E = BDSGlobals->GetBeamKineticEnergy();
00501       zp = 1;
00502       t=0;
00503       z0=0;
00504       G4double phase_factor = 1 / ( (nptwiss/2.) - 1.0 );
00505       if(partId<=nptwiss/2) //primary - xx' ellipse
00506         {
00507           x0 = sigx * cos(partId* 2 * pi*phase_factor);
00508           xp = -sigxp * ( alphaX * cos(partId * 2 * pi*phase_factor )
00509                           + sin(partId * 2 * pi*phase_factor ) );
00510           y0 = 0;
00511           yp = 0;
00512         }
00513       else if(partId<=nptwiss) //primary - yy' ellipse
00514         {
00515           x0 = 0;
00516           xp = 0;
00517           y0 = sigy * cos( (partId-nptwiss/2)*2*pi*phase_factor);
00518           yp = -sigyp * ( alphaY * cos( (partId-nptwiss/2) * 2 * pi*phase_factor)
00519                           + sin( (partId-nptwiss/2) * 2 * pi*phase_factor) );
00520         }
00521       //tmp - check units of above equations!!
00522       x0*=m;
00523       y0*=m;
00524       xp*=radian;
00525       yp*=radian;
00526       return;
00527     } //end doTwiss && partId<nptwiss
00528 
00529   switch(distribType){
00530   case _GAUSSIAN:
00531     {
00532       if(DEBUG) G4cout<<"GAUSSIAN: "<<G4endl
00533                       <<" X0= "<<X0<<" m"<<G4endl
00534                       <<" Y0= "<<Y0<<" m"<<G4endl
00535                       <<" Z0= "<<Z0<<" m"<<G4endl
00536                       <<" T0= "<<T0<<" s"<<G4endl
00537                       <<" Xp0= "<<Xp0<<G4endl
00538                       <<" Yp0= "<<Yp0<<G4endl
00539                       <<" Zp0= "<<Zp0<<G4endl
00540                       <<" sigmaX= "<<sigmaX<<" m"<<G4endl
00541                       <<" sigmaY= "<<sigmaY<<" m"<<G4endl
00542                       <<" sigmaXp= "<<sigmaXp<<G4endl
00543                       <<" sigmaYp= "<<sigmaYp<<G4endl
00544                       <<" sigmaT= "<<sigmaT<<"s"<<G4endl
00545                       <<" relative energy spread= "<<energySpread<<G4endl;
00546 
00547       x0 = (X0 + sigmaX * GaussGen->shoot()) * m;
00548       y0 = (Y0 + sigmaY * GaussGen->shoot()) * m;
00549       z0 = Z0 * m;
00550       xp = Xp0 + sigmaXp * GaussGen->shoot();
00551       yp = Yp0 + sigmaYp * GaussGen->shoot();
00552       if (Zp0<0)
00553         zp = -sqrt(1.-xp*xp -yp*yp);
00554       else
00555         zp = sqrt(1.-xp*xp -yp*yp);
00556       t = (T0 - sigmaT * (1.-2.*GaussGen->shoot())) * s;
00557       E = BDSGlobals->GetBeamKineticEnergy() * (1 + energySpread * GaussGen->shoot());
00558       break;
00559     }
00560   case _RING:
00561     {
00562       if(DEBUG) G4cout<<"RING: "<<G4endl
00563                       <<" X0= "<<X0<<" m"<<G4endl
00564                       <<" Y0= "<<Y0<<" m"<<G4endl
00565                       <<" Z0= "<<Z0<<" m"<<G4endl
00566                       <<" T0= "<<T0<<" s"<<G4endl
00567                       <<" Xp0= "<<Xp0<<G4endl
00568                       <<" Yp0= "<<Yp0<<G4endl
00569                       <<" Zp0= "<<Zp0<<G4endl
00570                       <<" rMin= "<<rMin<<" m"<<G4endl
00571                       <<" rMax= "<<rMax<<" m"<<G4endl
00572                       <<" relative energy spread= "<<energySpread<<G4endl;
00573 
00574       
00575       r = ( rMin + (rMax - rMin) *  rand() / RAND_MAX );
00576       phi = 2 * pi * rand() / RAND_MAX;
00577       
00578       x0 = ( X0 + r * sin(phi) ) * m;
00579       y0 = ( Y0 + r * cos(phi) ) * m;
00580       z0 = Z0 * m;
00581       xp = Xp0;
00582       yp = Yp0;
00583       if (Zp0<0)
00584         zp = -sqrt(1.-xp*xp -yp*yp);
00585       else
00586         zp = sqrt(1.-xp*xp -yp*yp);
00587       t = T0 * s;
00588       E = BDSGlobals->GetBeamKineticEnergy()
00589         * (1 + energySpread/2. * (1. -2. * FlatGen->shoot()));
00590       break;
00591     }
00592   case _ESHELL:
00593     {// generate elliptical shell - first generate on S1 and then transform into ellipse
00594       
00595       if(DEBUG) G4cout<<"SHELL: " 
00596                       <<" X0= "<<X0<<" m"<<G4endl
00597                       <<" Y0= "<<Y0<<" m"<<G4endl
00598                       <<" Z0= "<<Z0<<" m"<<G4endl
00599                       <<" T0= "<<T0<<" s"<<G4endl
00600                       <<" Xp0= "<<Xp0<<G4endl
00601                       <<" Yp0= "<<Yp0<<G4endl
00602                       <<" Zp0= "<<Zp0<<G4endl
00603                       <<" shellX= "<<shellx<<" m"<<G4endl
00604                       <<" shellY= "<<shelly<<" m"<<G4endl
00605                       <<" shellXp= "<<shellxp<<G4endl
00606                       <<" shellYp= "<<shellyp<<G4endl
00607                       <<" relative energy spread= "<<energySpread<<G4endl;
00608       
00609       phi = 2 * pi * rand() / RAND_MAX;
00610       
00611       x0 = (X0 + sin(phi) * shellx) * m;
00612       xp = Xp0 + cos(phi) * shellxp;
00613       
00614       phi = 2 * pi * rand() / RAND_MAX;
00615       
00616       y0 = (Y0 + sin(phi) * shelly) * m;
00617       yp = Yp0 + cos(phi) * shellyp;
00618       
00619       z0 = Z0 * m;
00620       if (Zp0<0)
00621         zp = -sqrt(1.-xp*xp -yp*yp);
00622       else
00623         zp = sqrt(1.-xp*xp -yp*yp);
00624 
00625       t = T0 * s;
00626       E = BDSGlobals->GetBeamKineticEnergy()
00627         * (1 + energySpread/2. * (1. -2. * FlatGen->shoot()));
00628       break;
00629     }
00630 
00631   case _GUINEAPIG_BUNCH:
00632     {
00633       #define  _READ(value) InputBunchFile>>value
00634       if(_READ(E))
00635         {
00636           _READ(x0);
00637           _READ(y0);
00638           _READ(z0);
00639           _READ(xp);
00640           _READ(yp);
00641           
00642           E*=GeV;
00643           x0*= micrometer;
00644           y0*= micrometer;
00645           z0*= micrometer;
00646           xp*=1.e-6*radian;
00647           yp*=1.e-6*radian;
00648           zp=sqrt(1.-xp*xp -yp*yp);  
00649           t=-z0/c_light;
00650           // use the Kinetic energy:
00651           E-=BDSGlobals->GetParticleDefinition()->GetPDGMass();
00652         }
00653       break;
00654     }
00655   case _GUINEAPIG_SLAC:
00656     {
00657       #define  _READ(value) InputBunchFile>>value
00658       if(_READ(E))
00659         {
00660           _READ(xp);
00661           _READ(yp);
00662           _READ(z0);
00663           _READ(x0);
00664           _READ(y0);
00665           
00666           E*=GeV;
00667           x0*= nanometer;
00668           y0*= nanometer;
00669           z0*= micrometer;
00670           xp*=radian;
00671           yp*=radian;
00672           zp=sqrt(1.-xp*xp -yp*yp);  
00673           t=-z0/c_light;
00674           // use the Kinetic energy:
00675           E-=BDSGlobals->GetParticleDefinition()->GetPDGMass();
00676         }
00677       else{
00678         InputBunchFile.clear();
00679         InputBunchFile.seekg(0);
00680         _skip(ignoreLines * 6);
00681         GetNextParticle(x0,y0,z0,xp,yp,zp,t,E);
00682       }
00683       break;
00684     }
00685   case _GUINEAPIG_PAIRS:
00686     {
00687       #define  _READ(value) InputBunchFile>>value
00688       if(_READ(E))
00689         {
00690           _READ(xp);
00691           _READ(yp);
00692           _READ(zp);
00693           _READ(x0);
00694           _READ(y0);
00695           _READ(z0);
00696           if(E>0) BDSGlobals->SetParticleDefinition(G4ParticleTable::
00697                                                     GetParticleTable()
00698                                                     ->FindParticle("e-"));
00699           if(E<0) BDSGlobals->SetParticleDefinition(G4ParticleTable::
00700                                                     GetParticleTable()
00701                                                     ->FindParticle("e+"));
00702           E=fabs(E)*GeV;
00703           x0*= nanometer;
00704           y0*= nanometer;
00705           z0*= nanometer;
00706           xp*=radian;
00707           yp*=radian;
00708           zp*=radian;
00709           // Using the sign of the pair file zp
00710           // but calculating zp more accurately
00711           if(zp<0) zp = -sqrt(1-(xp*xp+yp*yp));
00712           else zp = sqrt(1-(xp*xp+yp*yp));
00713           t=-z0/c_light;
00714           // use the Kinetic energy:
00715           E-=BDSGlobals->GetParticleDefinition()->GetPDGMass();
00716         }
00717       break;
00718     }
00719   case _CAIN:
00720     {// Note that for the CAIN input the following variables are read in but NOT used by BDSIM:
00721       //     generation, weight, spin_x, spin_y, spin_z
00722       
00723       G4int type;
00724       G4int gen;
00725       G4int pos;
00726       G4double weight;
00727       G4double part_mass;
00728       G4double px,py,pz;
00729       G4double sx;
00730       G4double sy;
00731       G4double sz;
00732       G4String dbl_var;
00733       G4String rep = 'E';      
00734       #define  _READ(value) InputBunchFile>>value
00735       
00736       if(_READ(type))
00737         {
00738           
00739           _READ(gen); // Read in but not currently used
00740           
00741           _READ(dbl_var);
00742           if(dbl_var.contains('D'))
00743             {
00744               pos = dbl_var.first('D');
00745               weight = atof( dbl_var.replace(pos,1,rep.data(),1) );
00746             }
00747           else weight = atof(dbl_var);
00748           // weight read in but not currently used
00749           
00750           _READ(dbl_var);
00751           if(dbl_var.contains('D'))
00752             {
00753               pos = dbl_var.first('D');
00754               t = atof( dbl_var.replace(pos,1,rep.data(),1) );
00755             }
00756           else t = atof(dbl_var);
00757           
00758           _READ(dbl_var);
00759           if(dbl_var.contains('D'))
00760             {
00761               pos = dbl_var.first('D');
00762               x0 = atof( dbl_var.replace(pos,1,rep.data(),1) );
00763             }
00764           else x0 = atof(dbl_var);
00765           
00766           _READ(dbl_var);
00767           if(dbl_var.contains('D'))
00768             {
00769               pos = dbl_var.first('D');
00770               y0 = atof( dbl_var.replace(pos,1,rep.data(),1) );
00771             }
00772           else y0 = atof(dbl_var);
00773           
00774           _READ(dbl_var);
00775           if(dbl_var.contains('D'))
00776             {
00777               pos = dbl_var.first('D');
00778               z0 = atof( dbl_var.replace(pos,1,rep.data(),1) );
00779             }
00780           else z0 = atof(dbl_var);
00781           
00782           _READ(dbl_var);
00783           if(dbl_var.contains('D'))
00784             {
00785               pos = dbl_var.first('D');
00786               E = atof( dbl_var.replace(pos,1,rep.data(),1) );
00787             }
00788           else E = atof(dbl_var);
00789           
00790           _READ(dbl_var);
00791           if(dbl_var.contains('D'))
00792             {
00793               pos = dbl_var.first('D');
00794               px = atof( dbl_var.replace(pos,1,rep.data(),1) );
00795             }
00796           else px = atof(dbl_var);
00797           
00798           _READ(dbl_var);
00799           if(dbl_var.contains('D'))
00800             {
00801               pos = dbl_var.first('D');
00802               py = atof( dbl_var.replace(pos,1,rep.data(),1) );
00803             }
00804           else py = atof(dbl_var);
00805           
00806           _READ(dbl_var);
00807           if(dbl_var.contains('D'))
00808             {
00809               pos = dbl_var.first('D');
00810               pz = atof( dbl_var.replace(pos,1,rep.data(),1) );
00811             }
00812           else pz = atof(dbl_var);
00813           
00814           // Spin Components read in - but not currently used
00815           _READ(sx);
00816           _READ(sy);
00817           _READ(sz);
00818           
00819           if(type==1) 
00820             BDSGlobals->SetParticleDefinition(G4ParticleTable::
00821                                               GetParticleTable()
00822                                               ->FindParticle("gamma"));
00823           else if(type==2) 
00824             BDSGlobals->SetParticleDefinition(G4ParticleTable::
00825                                               GetParticleTable()
00826                                               ->FindParticle("e-"));
00827           
00828           else if(type==3) 
00829             BDSGlobals->SetParticleDefinition(G4ParticleTable::
00830                                               GetParticleTable()
00831                                               ->FindParticle("e+"));
00832           
00833           t*= m/c_light;
00834           x0*= m;
00835           y0*= m;
00836           z0*= m;
00837           E*=eV;
00838           px*=eV/c_light;
00839           py*=eV/c_light;
00840           pz*=eV/c_light;
00841           
00842           part_mass = BDSGlobals->GetParticleDefinition()->GetPDGMass();
00843           // use the Kinetic energy:
00844           E-=part_mass;
00845           
00846           // calculate the momentum direction needed for BDSPrimaryGenerator
00847           xp = px*c_light / sqrt(E*E + 2*E*part_mass);
00848           yp = py*c_light / sqrt(E*E + 2*E*part_mass);
00849           zp = pz*c_light / sqrt(E*E + 2*E*part_mass);
00850 
00851           if (DEBUG) {
00852                   G4cout << "Bunch input was: " << G4endl;
00853                   G4cout << type << "\t"
00854                   << gen << "\t"
00855                   << weight << "\t"
00856                   << t/m << "\t"
00857                   << x0/m << "\t"
00858                   << y0/m << "\t"
00859                   << z0/m << "\t"
00860                   << E/eV << "\t"
00861                   << px/(eV/c_light) << "\t"
00862                   << py/(eV/c_light) << "\t"
00863                   << pz/(eV/c_light) << "\t"
00864                   << sx << "\t"
00865                   << sy << "\t"
00866                   << sz << "\t"
00867                   << xp << "\t"
00868                   << yp << "\t"
00869                   << zp << "\t"
00870                   << G4endl << G4endl;
00871           }
00872         }
00873       break;
00874     }
00875   case _UDEF:
00876     {
00877       E = x0 = y0 = z0 = xp = yp = zp = 0;
00878       bool zpdef = false; //keeps record whether zp has been read from file
00879       bool tdef = false; //keeps record whether t has been read from file
00880 
00881       #define  _READ(value) InputBunchFile>>value
00882       
00883       G4int type;
00884       
00885       list<struct Doublet>::iterator it;
00886       
00887       for(it=fields.begin();it!=fields.end();it++)
00888         {
00889           if (DEBUG) G4cout<<it->name<<"  ->  "<<it->unit<<G4endl;
00890           if(it->name=="E") { _READ(E); E *= ( GeV * it->unit ); }
00891           if(it->name=="t") { _READ(t); t *= ( s * it->unit ); tdef = true; }
00892           if(it->name=="x") { _READ(x0); x0 *= ( m * it->unit ); }
00893           if(it->name=="y") { _READ(y0); y0 *= ( m * it->unit ); }
00894           if(it->name=="z") { _READ(z0); z0 *= ( m * it->unit ); }
00895           if(it->name=="xp") { _READ(xp); xp *= ( radian * it->unit ); }
00896           if(it->name=="yp") { _READ(yp); yp *= ( radian * it->unit ); }
00897           if(it->name=="zp") { _READ(zp); zp *= ( radian * it->unit ); zpdef = true;}
00898           if(it->name=="pt") {
00899             _READ(type);
00900             if(InputBunchFile.good())
00901               BDSGlobals->SetParticleDefinition(G4ParticleTable::
00902                                                 GetParticleTable()
00903                                                 ->FindParticle(type));
00904           }
00905           
00906           // compute zp from xp and yp if it hasn't been read from file
00907           if (!zpdef) zp=sqrt(1.-xp*xp -yp*yp);
00908           // compute t from z0 if it hasn't been read from file
00909           if (!tdef) t=-z0/c_light;
00910           // use the Kinetic energy:
00911           E-=BDSGlobals->GetParticleDefinition()->GetPDGMass();
00912         }
00913       break;
00914     }
00915   default:
00916     {
00917       G4Exception("BDSBunch: Unknown distribution file type!");
00918     }
00919   }
00920 }
00921 
00922 
00923 G4double BDSBunch::GetEmitX()
00924 {
00925   return emitX;
00926 }
00927 
00928 G4double BDSBunch::GetEmitY()
00929 {
00930   return emitY;
00931 }
00932 
00933 G4double BDSBunch::GetAlphaX()
00934 {
00935   return alphaX;
00936 }
00937 
00938 G4double BDSBunch::GetAlphaY()
00939 {
00940   return alphaY;
00941 }
00942 
00943 G4double BDSBunch::GetBetaX()
00944 {
00945   return betaX;
00946 }
00947 
00948 G4double BDSBunch::GetBetaY()
00949 {
00950   return betaY;
00951 }
00952 
00953 
00954 
00955 // set initial bunch distribution parameters in Gaussian case 
00956 void BDSBunch::SetSigmaT(double val) 
00957 {
00958   sigmaT= val;
00959 } 
00960 
00961 void BDSBunch::SetSigmaX(double val)
00962 { 
00963   sigmaX = val;
00964 }
00965  
00966 void BDSBunch::SetSigmaY(double val)
00967 {
00968   sigmaY = val;
00969 } 
00970 
00971 void BDSBunch::SetSigmaXp(double val)
00972 {
00973   sigmaXp = val;
00974 }
00975 
00976 void BDSBunch::SetSigmaYp(double val)
00977 {
00978   sigmaYp = val;
00979 }
00980 
00981 void BDSBunch::SetX0(double val)
00982 {
00983   X0 = val;
00984 } 
00985 
00986 void BDSBunch::SetY0(double val)
00987 {
00988   Y0 = val;
00989 }
00990 
00991 void BDSBunch::SetXp0(double val)
00992 {
00993   Xp0 = val;
00994 }
00995 
00996 void BDSBunch::SetYp0(double val)
00997 {
00998   Yp0 = val;
00999 }
01000 
01001 void BDSBunch::SetEmitX(double val)
01002 {
01003   emitX = val;
01004 }
01005 
01006 void BDSBunch::SetEmitY(double val)
01007 {
01008   emitY = val;
01009 }
01010 
01011 void BDSBunch::SetAlphaX(double val)
01012 {
01013   alphaX = val;
01014 }
01015 
01016 void BDSBunch::SetAlphaY(double val)
01017 {
01018   alphaY = val;
01019 }
01020 
01021 void BDSBunch::SetBetaX(double val)
01022 {
01023   betaX = val;
01024 }
01025 
01026 void BDSBunch::SetBetaY(double val)
01027 {
01028   betaY = val;
01029 }

Generated on Wed Mar 5 17:25:22 2008 for BDSIM by  doxygen 1.5.3