79 G4double fieldBoundary, wc0, wc1, wc2, wc3, limitMinEntrance, limitMaxEntrance, limitMinExit, limitMaxExit;
81 limitMinEntrance = beamStart+zS-4*D;
82 limitMaxEntrance = beamStart+zS+4*D;
93 G4double ws, largeS, h, dhdlargeS, dhds, dlargeSds, dsdz, dsdx, zs0, Rs0, xcenter, zcenter;
97 if ( (z >= limitMinEntrance) && (z < limitMaxEntrance) )
99 zs0 = fieldBoundary*D;
100 ws = (-z+beamStart+zS-zs0)/D;
104 largeS = wc0 + wc1*ws + wc2*ws*ws + wc3*ws*ws*ws;
105 h = 1./(1.+std::exp(largeS));
106 dhdlargeS = -std::exp(largeS)*h*h;
107 dlargeSds = wc1+ 2*wc2*ws + 3*wc3*ws*ws;
108 dhds = dhdlargeS * dlargeSds;
110 By = switchingField * h ;
111 Bx = y*switchingField*dhds*dsdx;
112 Bz = y*switchingField*dhds*dsdz;
119 (z >= limitMaxEntrance)
120 && (( x*x + (z -(beamStart+zS))*(z -(beamStart+zS)) < limitMinExit*limitMinExit))
131 (z >= limitMaxEntrance)
132 && (( x*x + (z -(beamStart+zS))*(z -(beamStart+zS))) >= limitMinExit*limitMinExit)
133 && (( x*x + (z -(beamStart+zS))*(z -(beamStart+zS))) < limitMaxExit*limitMaxExit)
139 zcenter = beamStart+zS;
141 Rs0 = Rp + D*fieldBoundary;
142 ws = (std::sqrt((z-zcenter)*(z-zcenter)+(x-xcenter)*(x-xcenter)) - Rs0)/D;
144 dsdz = (1/D)*(z-zcenter)/std::sqrt((z-zcenter)*(z-zcenter)+(x-xcenter)*(x-xcenter));
145 dsdx = (1/D)*(x-xcenter)/std::sqrt((z-zcenter)*(z-zcenter)+(x-xcenter)*(x-xcenter));
147 largeS = wc0 + wc1*ws + wc2*ws*ws + wc3*ws*ws*ws;
148 h = 1./(1.+std::exp(largeS));
149 dhdlargeS = -std::exp(largeS)*h*h;
150 dlargeSds = wc1+ 2*wc2*ws + 3*wc3*ws*ws;
151 dhds = dhdlargeS * dlargeSds;
153 By = switchingField * h ;
154 Bx = y*switchingField*dhds*dsdx;
155 Bz = y*switchingField*dhds*dsdz;
173 lineX = lineX + 5.24*
micrometer*std::cos(-lineAngle);
174 lineZ = lineZ + 5.24*
micrometer*std::sin(-lineAngle);
185 if (z>=-1400*
mm && z <-200*
mm)
190 G4double c0[4],
c1[4], c2[4], z1[4], z2[4], a0[4], gradient[4];
199 gradient[0] = 3.406526 *
tesla/
m;
208 gradient[1] = -8.505263 *
tesla/
m;
217 gradient[2] = 8.505263 *
tesla/
m;
226 gradient[3] = -3.406526*
tesla/
m;
229 G4double Bx_local,By_local,Bz_local;
230 Bx_local = 0; By_local = 0; Bz_local = 0;
234 Bx_quad = 0; By_quad=0; Bz_quad=0;
238 x_local= 0; y_local=0; z_local=0;
257 G4bool largeScattering=
false;
259 for (
G4int i=0;i<4; i++)
263 { xoprime = lineX + quadHalfLength*std::sin(lineAngle);
264 zoprime = lineZ + quadHalfLength*std::cos(lineAngle);
266 x_local = (x - xoprime) * std::cos (lineAngle) - (z - zoprime) * std::sin (lineAngle);
268 z_local = (z - zoprime) * std::cos (lineAngle) + (x - xoprime) * std::sin (lineAngle);
269 if (std::sqrt(x_local*x_local+y_local*y_local)>a0[i]) largeScattering=
true;
274 { xoprime = lineX + (3*quadHalfLength+quadSpacing)*std::sin(lineAngle);
275 zoprime = lineZ + (3*quadHalfLength+quadSpacing)*std::cos(lineAngle);
277 x_local = (x - xoprime) * std::cos (lineAngle) - (z - zoprime) * std::sin (lineAngle);
279 z_local = (z - zoprime) * std::cos (lineAngle) + (x - xoprime) * std::sin (lineAngle);
280 if (std::sqrt(x_local*x_local+y_local*y_local)>a0[i]) largeScattering=
true;
284 { xoprime = lineX + (5*quadHalfLength+2*quadSpacing)*std::sin(lineAngle);
285 zoprime = lineZ + (5*quadHalfLength+2*quadSpacing)*std::cos(lineAngle);
287 x_local = (x - xoprime) * std::cos (lineAngle) - (z - zoprime) * std::sin (lineAngle);
289 z_local = (z - zoprime) * std::cos (lineAngle) + (x - xoprime) * std::sin (lineAngle);
290 if (std::sqrt(x_local*x_local+y_local*y_local)>a0[i]) largeScattering=
true;
294 { xoprime = lineX + (7*quadHalfLength+3*quadSpacing)*std::sin(lineAngle);
295 zoprime = lineZ + (7*quadHalfLength+3*quadSpacing)*std::cos(lineAngle);
297 x_local = (x - xoprime) * std::cos (lineAngle) - (z - zoprime) * std::sin (lineAngle);
299 z_local = (z - zoprime) * std::cos (lineAngle) + (x - xoprime) * std::sin (lineAngle);
300 if (std::sqrt(x_local*x_local+y_local*y_local)>a0[i]) largeScattering=
true;
304 if ( z_local < -z2[i] )
312 if ( z_local > z2[i] )
320 if ( (z_local>=-z1[i]) & (z_local<=z1[i]) )
328 if ( ((z_local>=-z2[i]) & (z_local<-z1[i])) || ((z_local>z1[i]) & (z_local<=z2[i])) )
331 vars = ( z_local - z1[i]) / a0[i] ;
332 if (z_local<-z1[i]) vars = ( - z_local - z1[i]) / a0[i] ;
335 P0 = c0[i]+c1[i]*vars+c2[i]*vars*vars;
337 P1 = c1[i]/a0[i]+2*c2[i]*(z_local-z1[i])/a0[i]/a0[i];
338 if (z_local<-z1[i]) P1 = -c1[i]/a0[i]+2*c2[i]*(z_local+z1[i])/a0[i]/a0[i];
340 P2 = 2*c2[i]/a0[i]/a0[i];
342 cte = 1 + std::exp(c0[i]);
344 K1 = -cte*P1*std::exp(P0)/( (1+std::exp(P0))*(1+std::exp(P0)) );
346 K2 = -cte*std::exp(P0)*(
347 P2/( (1+std::exp(P0))*(1+std::exp(P0)) )
348 +2*P1*K1/(1+std::exp(P0))/cte
349 +P1*P1/(1+std::exp(P0))/(1+std::exp(P0))
352 K3 = -cte*std::exp(P0)*(
353 (3*P2*P1+P1*P1*P1)/(1+std::exp(P0))/(1+std::exp(P0))
354 +4*K1*(P1*P1+P2)/(1+std::exp(P0))/cte
355 +2*P1*(K1*K1/cte/cte+K2/(1+std::exp(P0))/cte)
358 G0 = gradient[i]*cte/(1+std::exp(P0));
367 if ( largeScattering )
377 Bx_local = y_local*(G0-(1./12)*(3*x_local*x_local+y_local*y_local)*G2);
378 By_local = x_local*(G0-(1./12)*(3*y_local*y_local+x_local*x_local)*G2);
379 Bz_local = x_local*y_local*(G1-(1./12)*(x_local*x_local+y_local*y_local)*G3);
381 Bx_quad = Bz_local*std::sin(lineAngle)+Bx_local*std::cos(lineAngle);
383 Bz_quad = Bz_local*std::cos(lineAngle)-Bx_local*std::sin(lineAngle);
422 G4double electricFieldPlate1 = electricPlateVoltage1 / electricPlateSpacing1 ;
423 G4double electricFieldPlate2 = electricPlateVoltage2 / electricPlateSpacing2 ;
425 G4double beginFirstZoneX = lineX + (8*quadHalfLength+3*quadSpacing)*std::sin(lineAngle);
426 G4double beginFirstZoneZ = lineZ + (8*quadHalfLength+3*quadSpacing)*std::cos(lineAngle);
428 G4double beginSecondZoneX = lineX + (8*quadHalfLength+3*quadSpacing+electricPlateLength1+electricPlateGap)*std::sin(lineAngle);
429 G4double beginSecondZoneZ = lineZ + (8*quadHalfLength+3*quadSpacing+electricPlateLength1+electricPlateGap)*std::cos(lineAngle);
431 G4double xA, zA, xB, zB, xC, zC, xD, zD;
432 G4double slope1, cte1, slope2, cte2, slope3, cte3, slope4, cte4;
438 xA = beginFirstZoneX + std::cos(lineAngle)*electricPlateSpacing1/2;
439 zA = beginFirstZoneZ - std::sin(lineAngle)*electricPlateSpacing1/2;
441 xB = xA + std::sin(lineAngle)*electricPlateLength1;
442 zB = zA + std::cos(lineAngle)*electricPlateLength1;
444 xC = xB - std::cos(lineAngle)*electricPlateSpacing1;
445 zC = zB + std::sin(lineAngle)*electricPlateSpacing1;
447 xD = xC - std::sin(lineAngle)*electricPlateLength1;
448 zD = zC - std::cos(lineAngle)*electricPlateLength1;
450 slope1 = (xB-xA)/(zB-zA);
451 cte1 = xA - slope1 * zA;
453 slope2 = (xC-xB)/(zC-zB);
454 cte2 = xB - slope2 * zB;
456 slope3 = (xD-xC)/(zD-zC);
457 cte3 = xC - slope3 * zC;
459 slope4 = (xA-xD)/(zA-zD);
460 cte4 = xD - slope4 * zD;
465 x <= slope1 * z + cte1
466 && x >= slope3 * z + cte3
467 && x <= slope4 * z + cte4
468 && x >= slope2 * z + cte2
469 && std::abs(y)<=electricPlateWidth1/2
473 Bfield[3] = electricFieldPlate1*std::cos(lineAngle);
475 Bfield[5] = -electricFieldPlate1*std::sin(lineAngle);
481 xA = beginSecondZoneX + std::cos(lineAngle)*electricPlateWidth2/2;
482 zA = beginSecondZoneZ - std::sin(lineAngle)*electricPlateWidth2/2;
484 xB = xA + std::sin(lineAngle)*electricPlateLength2;
485 zB = zA + std::cos(lineAngle)*electricPlateLength2;
487 xC = xB - std::cos(lineAngle)*electricPlateWidth2;
488 zC = zB + std::sin(lineAngle)*electricPlateWidth2;
490 xD = xC - std::sin(lineAngle)*electricPlateLength2;
491 zD = zC - std::cos(lineAngle)*electricPlateLength2;
493 slope1 = (xB-xA)/(zB-zA);
494 cte1 = xA - slope1 * zA;
496 slope2 = (xC-xB)/(zC-zB);
497 cte2 = xB - slope2 * zB;
499 slope3 = (xD-xC)/(zD-zC);
500 cte3 = xC - slope3 * zC;
502 slope4 = (xA-xD)/(zA-zD);
503 cte4 = xD - slope4 * zD;
507 x <= slope1 * z + cte1
508 && x >= slope3 * z + cte3
509 && x <= slope4 * z + cte4
510 && x >= slope2 * z + cte2
511 && std::abs(y)<=electricPlateSpacing2/2
516 Bfield[4] = electricFieldPlate2;