Geant4 Cross Reference

Cross-Referencing   Geant4
Geant4/examples/advanced/microbeam/src/EMField.cc

Version: [ ReleaseNotes ] [ 1.0 ] [ 1.1 ] [ 2.0 ] [ 3.0 ] [ 3.1 ] [ 3.2 ] [ 4.0 ] [ 4.0.p1 ] [ 4.0.p2 ] [ 4.1 ] [ 4.1.p1 ] [ 5.0 ] [ 5.0.p1 ] [ 5.1 ] [ 5.1.p1 ] [ 5.2 ] [ 5.2.p1 ] [ 5.2.p2 ] [ 6.0 ] [ 6.0.p1 ] [ 6.1 ] [ 6.2 ] [ 6.2.p1 ] [ 6.2.p2 ] [ 7.0 ] [ 7.0.p1 ] [ 7.1 ] [ 7.1.p1 ] [ 8.0 ] [ 8.0.p1 ] [ 8.1 ] [ 8.1.p1 ] [ 8.1.p2 ] [ 8.2 ] [ 8.2.p1 ] [ 8.3 ] [ 8.3.p1 ] [ 8.3.p2 ] [ 9.0 ] [ 9.0.p1 ] [ 9.0.p2 ] [ 9.1 ] [ 9.1.p1 ] [ 9.1.p2 ] [ 9.1.p3 ] [ 9.2 ] [ 9.2.p1 ] [ 9.2.p2 ] [ 9.2.p3 ] [ 9.2.p4 ] [ 9.3 ] [ 9.3.p1 ] [ 9.3.p2 ] [ 9.4 ] [ 9.4.p1 ] [ 9.4.p2 ] [ 9.4.p3 ] [ 9.4.p4 ] [ 9.5 ] [ 9.5.p1 ] [ 9.5.p2 ] [ 9.6 ] [ 9.6.p1 ] [ 9.6.p2 ] [ 9.6.p3 ] [ 9.6.p4 ] [ 10.0 ] [ 10.0.p1 ] [ 10.0.p2 ] [ 10.0.p3 ] [ 10.0.p4 ] [ 10.1 ] [ 10.1.p1 ] [ 10.1.p2 ] [ 10.1.p3 ] [ 10.2 ] [ 10.2.p1 ] [ 10.2.p2 ] [ 10.2.p3 ] [ 10.3 ] [ 10.3.p1 ] [ 10.3.p2 ] [ 10.3.p3 ] [ 10.4 ] [ 10.4.p1 ] [ 10.4.p2 ] [ 10.4.p3 ] [ 10.5 ] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

Diff markup

Differences between /examples/advanced/microbeam/src/EMField.cc (Version 11.3.0) and /examples/advanced/microbeam/src/EMField.cc (Version 10.3)


  1 //                                                  1 //
  2 // *******************************************      2 // ********************************************************************
  3 // * License and Disclaimer                         3 // * License and Disclaimer                                           *
  4 // *                                                4 // *                                                                  *
  5 // * The  Geant4 software  is  copyright of th      5 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
  6 // * the Geant4 Collaboration.  It is provided      6 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
  7 // * conditions of the Geant4 Software License      7 // * conditions of the Geant4 Software License,  included in the file *
  8 // * LICENSE and available at  http://cern.ch/      8 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
  9 // * include a list of copyright holders.           9 // * include a list of copyright holders.                             *
 10 // *                                               10 // *                                                                  *
 11 // * Neither the authors of this software syst     11 // * Neither the authors of this software system, nor their employing *
 12 // * institutes,nor the agencies providing fin     12 // * institutes,nor the agencies providing financial support for this *
 13 // * work  make  any representation or  warran     13 // * work  make  any representation or  warranty, express or implied, *
 14 // * regarding  this  software system or assum     14 // * regarding  this  software system or assume any liability for its *
 15 // * use.  Please see the license in the file      15 // * use.  Please see the license in the file  LICENSE  and URL above *
 16 // * for the full disclaimer and the limitatio     16 // * for the full disclaimer and the limitation of liability.         *
 17 // *                                               17 // *                                                                  *
 18 // * This  code  implementation is the result      18 // * This  code  implementation is the result of  the  scientific and *
 19 // * technical work of the GEANT4 collaboratio     19 // * technical work of the GEANT4 collaboration.                      *
 20 // * By using,  copying,  modifying or  distri     20 // * By using,  copying,  modifying or  distributing the software (or *
 21 // * any work based  on the software)  you  ag     21 // * any work based  on the software)  you  agree  to acknowledge its *
 22 // * use  in  resulting  scientific  publicati     22 // * use  in  resulting  scientific  publications,  and indicate your *
 23 // * acceptance of all terms of the Geant4 Sof     23 // * acceptance of all terms of the Geant4 Software license.          *
 24 // *******************************************     24 // ********************************************************************
 25 //                                                 25 //
 26 // This example is provided by the Geant4-DNA      26 // This example is provided by the Geant4-DNA collaboration
 27 // Any report or published results obtained us     27 // Any report or published results obtained using the Geant4-DNA software 
 28 // shall cite the following Geant4-DNA collabo     28 // shall cite the following Geant4-DNA collaboration publication:
 29 // Med. Phys. 37 (2010) 4692-4708                  29 // Med. Phys. 37 (2010) 4692-4708
 30 // The Geant4-DNA web site is available at htt     30 // The Geant4-DNA web site is available at http://geant4-dna.org
 31 //                                                 31 // 
 32 // If you use this example, please cite the fo     32 // If you use this example, please cite the following publication:
 33 // Rad. Prot. Dos. 133 (2009) 2-11                 33 // Rad. Prot. Dos. 133 (2009) 2-11
 34 //                                             << 
 35 // Based on purging magnet advanced example.   << 
 36 //                                             << 
 37                                                    34 
 38 #include "EMField.hh"                              35 #include "EMField.hh"
 39 #include "G4Exp.hh"                                36 #include "G4Exp.hh"
 40 #include "G4SystemOfUnits.hh"                      37 #include "G4SystemOfUnits.hh"
 41                                                    38 
 42 EMField::EMField()                                 39 EMField::EMField() 
 43 {}                                             <<  40 {    
                                                   >>  41 }
 44                                                    42 
 45 void EMField::GetFieldValue(const double point     43 void EMField::GetFieldValue(const double point[4], double *Bfield ) const
 46 {                                                  44 { 
 47   // Magnetic field                                45   // Magnetic field
 48   Bfield[0] = 0;                                   46   Bfield[0] = 0;
 49   Bfield[1] = 0;                                   47   Bfield[1] = 0;
 50   Bfield[2] = 0;                                   48   Bfield[2] = 0;
 51                                                    49   
 52   // Electric field                                50   // Electric field
 53   Bfield[3] = 0;                                   51   Bfield[3] = 0;
 54   Bfield[4] = 0;                                   52   Bfield[4] = 0;
 55   Bfield[5] = 0;                                   53   Bfield[5] = 0;
 56                                                    54 
 57   G4double Bx = 0;                                 55   G4double Bx = 0;
 58   G4double By = 0;                                 56   G4double By = 0;
 59   G4double Bz = 0;                                 57   G4double Bz = 0;
 60                                                    58    
 61   G4double x = point[0];                           59   G4double x = point[0];
 62   G4double y = point[1];                           60   G4double y = point[1];
 63   G4double z = point[2];                           61   G4double z = point[2];
 64                                                    62 
 65 // ***********************                         63 // ***********************
 66 // AIFIRA SWITCHING MAGNET                         64 // AIFIRA SWITCHING MAGNET
 67 // ***********************                         65 // ***********************
 68                                                    66   
 69   // MAGNETIC FIELD VALUE FOR 3 MeV ALPHAS         67   // MAGNETIC FIELD VALUE FOR 3 MeV ALPHAS
 70   G4double switchingField = 0.0589768635 * tes <<  68   //  G4double switchingField = 0.0589768635 * tesla ;
                                                   >>  69   G4double switchingField =   0.0590201 * tesla ;
 71                                                    70   
 72   // BEAM START                                    71   // BEAM START
 73   G4double beamStart = -10*m;                      72   G4double beamStart = -10*m;
 74                                                    73 
 75   // RADIUS                                        74   // RADIUS
 76   G4double Rp = 0.698*m;                           75   G4double Rp = 0.698*m;
 77                                                    76 
 78   // ENTRANCE POSITION AFTER ANALYSIS MAGNET       77   // ENTRANCE POSITION AFTER ANALYSIS MAGNET
 79   G4double zS = 975*mm;                            78   G4double zS = 975*mm;
 80                                                    79   
 81   // POLE GAP                                      80   // POLE GAP
 82   G4double D = 31.8*mm;                            81   G4double D = 31.8*mm;
 83                                                    82   
 84   // FRINGING FIELD                                83   // FRINGING FIELD
 85                                                    84 
 86   G4double fieldBoundary, wc0, wc1, wc2, wc3,      85   G4double fieldBoundary, wc0, wc1, wc2, wc3, limitMinEntrance, limitMaxEntrance, limitMinExit, limitMaxExit;
 87                                                    86 
 88   limitMinEntrance = beamStart+zS-4*D;             87   limitMinEntrance = beamStart+zS-4*D;
 89   limitMaxEntrance = beamStart+zS+4*D;             88   limitMaxEntrance = beamStart+zS+4*D;
 90   limitMinExit =Rp-4*D;                            89   limitMinExit =Rp-4*D;
 91   limitMaxExit =Rp+4*D;                            90   limitMaxExit =Rp+4*D;  
 92                                                    91     
 93   wc0 = 0.3835;                                    92   wc0 = 0.3835;
 94   wc1 = 2.388;                                     93   wc1 = 2.388;
 95   wc2 = -0.8171;                                   94   wc2 = -0.8171;
 96   wc3 = 0.200;                                     95   wc3 = 0.200;
 97                                                    96 
 98   fieldBoundary=0.62;                              97   fieldBoundary=0.62;
 99                                                    98 
100   G4double ws, largeS, h, dhdlargeS, dhds, dla     99   G4double ws, largeS, h, dhdlargeS, dhds, dlargeSds, dsdz, dsdx, zs0, Rs0, xcenter, zcenter;
101                                                   100   
102 // - ENTRANCE OF SWITCHING MAGNET                 101 // - ENTRANCE OF SWITCHING MAGNET
103                                                   102 
104 if ( (z >= limitMinEntrance) && (z < limitMaxE    103 if ( (z >= limitMinEntrance) && (z < limitMaxEntrance) ) 
105 {                                                 104 {
106   zs0 = fieldBoundary*D;                          105   zs0 = fieldBoundary*D;
107   ws = (-z+beamStart+zS-zs0)/D;                   106   ws = (-z+beamStart+zS-zs0)/D;
108   dsdz = -1/D;                                    107   dsdz = -1/D;
109   dsdx = 0;                                       108   dsdx = 0;
110                                                   109 
111   largeS = wc0 + wc1*ws + wc2*ws*ws + wc3*ws*w    110   largeS = wc0 + wc1*ws + wc2*ws*ws + wc3*ws*ws*ws;
112   h = 1./(1.+G4Exp(largeS));                      111   h = 1./(1.+G4Exp(largeS));
113   dhdlargeS = -G4Exp(largeS)*h*h;                 112   dhdlargeS = -G4Exp(largeS)*h*h;  
114   dlargeSds = wc1+ 2*wc2*ws + 3*wc3*ws*ws;        113   dlargeSds = wc1+ 2*wc2*ws + 3*wc3*ws*ws;
115   dhds = dhdlargeS * dlargeSds;                   114   dhds = dhdlargeS * dlargeSds;
116                                                   115       
117   By = switchingField * h ;                       116   By = switchingField * h ;
118   Bx = y*switchingField*dhds*dsdx;                117   Bx = y*switchingField*dhds*dsdx;
119   Bz = y*switchingField*dhds*dsdz;                118   Bz = y*switchingField*dhds*dsdz;
120                                                   119 
121 }                                                 120 }
122                                                   121 
123 // - HEART OF SWITCHING MAGNET                    122 // - HEART OF SWITCHING MAGNET    
124                                                   123     
125  if (                                             124  if ( 
126           (z >= limitMaxEntrance)              << 125   (z >= limitMaxEntrance)  
127      &&   (( x*x + (z -(beamStart+zS))*(z -(be << 126    &&  (( x*x + (z -(beamStart+zS))*(z -(beamStart+zS)) < limitMinExit*limitMinExit)) 
128     )                                             127     )   
129 {                                                 128 {
130    Bx=0;                                          129    Bx=0; 
131    By = switchingField;                           130    By = switchingField; 
132    Bz=0;                                          131    Bz=0;
133 }                                                 132 }                         
134                                                   133   
135 // - EXIT OF SWITCHING MAGNET                     134 // - EXIT OF SWITCHING MAGNET
136                                                   135 
137 if (                                              136 if ( 
138         (z >= limitMaxEntrance)                << 137   (z >= limitMaxEntrance)  
139      && (( x*x + (z -(beamStart+zS))*(z -(beam << 138    && (( x*x + (z -(beamStart+zS))*(z -(beamStart+zS))) >= limitMinExit*limitMinExit) 
140      && (( x*x + (z -(beamStart+zS))*(z -(beam << 139    &&   (( x*x + (z -(beamStart+zS))*(z -(beamStart+zS))) < limitMaxExit*limitMaxExit)
141                                                   140 
142    )                                           << 141     )   
143 {                                                 142 {
144                                                   143 
145   xcenter = 0;                                    144   xcenter = 0;
146   zcenter =  beamStart+zS;                        145   zcenter =  beamStart+zS;
147                                                   146   
148   Rs0 = Rp + D*fieldBoundary;                     147   Rs0 = Rp + D*fieldBoundary;
149   ws = (std::sqrt((z-zcenter)*(z-zcenter)+(x-x    148   ws = (std::sqrt((z-zcenter)*(z-zcenter)+(x-xcenter)*(x-xcenter)) - Rs0)/D;
150                                                   149     
151   dsdz = (1/D)*(z-zcenter)/std::sqrt((z-zcente    150   dsdz = (1/D)*(z-zcenter)/std::sqrt((z-zcenter)*(z-zcenter)+(x-xcenter)*(x-xcenter));
152   dsdx = (1/D)*(x-xcenter)/std::sqrt((z-zcente    151   dsdx = (1/D)*(x-xcenter)/std::sqrt((z-zcenter)*(z-zcenter)+(x-xcenter)*(x-xcenter));
153                                                   152 
154   largeS = wc0 + wc1*ws + wc2*ws*ws + wc3*ws*w    153   largeS = wc0 + wc1*ws + wc2*ws*ws + wc3*ws*ws*ws;
155   h = 1./(1.+G4Exp(largeS));                      154   h = 1./(1.+G4Exp(largeS));
156   dhdlargeS = -G4Exp(largeS)*h*h;                 155   dhdlargeS = -G4Exp(largeS)*h*h;  
157   dlargeSds = wc1+ 2*wc2*ws + 3*wc3*ws*ws;        156   dlargeSds = wc1+ 2*wc2*ws + 3*wc3*ws*ws;
158   dhds = dhdlargeS * dlargeSds;                   157   dhds = dhdlargeS * dlargeSds;
159                                                   158       
160   By = switchingField * h ;                       159   By = switchingField * h ;
161   Bx = y*switchingField*dhds*dsdx;                160   Bx = y*switchingField*dhds*dsdx;
162   Bz = y*switchingField*dhds*dsdz;                161   Bz = y*switchingField*dhds*dsdz;
163                                                   162 
164 }                                                 163 }
165                                                   164 
166 // **************************                     165 // **************************
167 // MICROBEAM LINE QUADRUPOLES                     166 // MICROBEAM LINE QUADRUPOLES
168 // **************************                     167 // **************************
169                                                   168  
170   // MICROBEAM LINE ANGLE                         169   // MICROBEAM LINE ANGLE
171   G4double lineAngle = -10*deg;                   170   G4double lineAngle = -10*deg;
172                                                   171   
173   // X POSITION OF FIRST QUADRUPOLE               172   // X POSITION OF FIRST QUADRUPOLE
174   G4double lineX = -1295.59*mm;                   173   G4double lineX = -1295.59*mm;
175                                                   174 
176   // Z POSITION OF FIRST QUADRUPOLE               175   // Z POSITION OF FIRST QUADRUPOLE
177   G4double lineZ = -1327*mm;                      176   G4double lineZ = -1327*mm;
178                                                   177 
179   // Adjust magnetic zone absolute position       178   // Adjust magnetic zone absolute position
180   lineX = lineX + 5.24*micrometer*std::cos(-li    179   lineX = lineX + 5.24*micrometer*std::cos(-lineAngle); // 5.24 = 1.3 + 3.94 micrometer (cf. DetectorConstruction)
181   lineZ = lineZ + 5.24*micrometer*std::sin(-li    180   lineZ = lineZ + 5.24*micrometer*std::sin(-lineAngle);
182                                                   181        
183   // QUADRUPOLE HALF LENGTH                       182   // QUADRUPOLE HALF LENGTH
184   G4double quadHalfLength = 75*mm;                183   G4double quadHalfLength = 75*mm;
185                                                   184   
186   // QUADRUPOLE SPACING                           185   // QUADRUPOLE SPACING
187   G4double quadSpacing = 40*mm;                   186   G4double quadSpacing = 40*mm;
188                                                   187   
189   // QUADRUPOLE CENTER COORDINATES                188   // QUADRUPOLE CENTER COORDINATES
190   G4double xoprime, zoprime;                      189   G4double xoprime, zoprime;
191                                                   190   
192 if (z>=-1400*mm && z <-200*mm)                    191 if (z>=-1400*mm && z <-200*mm)
193 {                                                 192 {
194   Bx=0; By=0; Bz=0;                               193   Bx=0; By=0; Bz=0;
195                                                   194   
196   // FRINGING FILED CONSTANTS                     195   // FRINGING FILED CONSTANTS
197   G4double c0[4], c1[4], c2[4], z1[4], z2[4],     196   G4double c0[4], c1[4], c2[4], z1[4], z2[4], a0[4], gradient[4];
198                                                   197   
199   // QUADRUPOLE 1                                 198   // QUADRUPOLE 1
200   c0[0] = -5.;                                    199   c0[0] = -5.;
201   c1[0] = 2.5;                                    200   c1[0] = 2.5;
202   c2[0] = -0.1;                                   201   c2[0] = -0.1;
203   z1[0] = 60*mm;                                  202   z1[0] = 60*mm;
204   z2[0] = 130*mm;                                 203   z2[0] = 130*mm;
205   a0[0] = 10*mm;                                  204   a0[0] = 10*mm;
206   gradient[0] = 3.406526 *tesla/m;                205   gradient[0] = 3.406526 *tesla/m;
207                                                   206 
208   // QUADRUPOLE 2                                 207   // QUADRUPOLE 2
209   c0[1] = -5.;                                    208   c0[1] = -5.;
210   c1[1] = 2.5;                                    209   c1[1] = 2.5;
211   c2[1] = -0.1;                                   210   c2[1] = -0.1;
212   z1[1] = 60*mm;                                  211   z1[1] = 60*mm;
213   z2[1] = 130*mm;                                 212   z2[1] = 130*mm;
214   a0[1] = 10*mm;                                  213   a0[1] = 10*mm;
215   gradient[1] = -8.505263 *tesla/m;               214   gradient[1] = -8.505263 *tesla/m;
216                                                   215 
217   // QUADRUPOLE 3                                 216   // QUADRUPOLE 3
218   c0[2] = -5.;                                    217   c0[2] = -5.;
219   c1[2] = 2.5;                                    218   c1[2] = 2.5;
220   c2[2] = -0.1;                                   219   c2[2] = -0.1;
221   z1[2] = 60*mm;                                  220   z1[2] = 60*mm;
222   z2[2] = 130*mm;                                 221   z2[2] = 130*mm;
223   a0[2] = 10*mm;                                  222   a0[2] = 10*mm;
224   gradient[2] = 8.505263 *tesla/m;                223   gradient[2] = 8.505263 *tesla/m;
225                                                   224 
226   // QUADRUPOLE 4                                 225   // QUADRUPOLE 4
227   c0[3] = -5.;                                    226   c0[3] = -5.;
228   c1[3] = 2.5;                                    227   c1[3] = 2.5;
229   c2[3] = -0.1;                                   228   c2[3] = -0.1;
230   z1[3] = 60*mm;                                  229   z1[3] = 60*mm;
231   z2[3] = 130*mm;                                 230   z2[3] = 130*mm;
232   a0[3] = 10*mm;                                  231   a0[3] = 10*mm;
233   gradient[3] = -3.406526*tesla/m;                232   gradient[3] = -3.406526*tesla/m;
234                                                   233 
235   // FIELD CREATED BY A QUADRUPOLE IN ITS LOCA    234   // FIELD CREATED BY A QUADRUPOLE IN ITS LOCAL FRAME
236   G4double Bx_local,By_local,Bz_local;            235   G4double Bx_local,By_local,Bz_local;
237   Bx_local = 0; By_local = 0; Bz_local = 0;       236   Bx_local = 0; By_local = 0; Bz_local = 0;
238                                                   237   
239   // FIELD CREATED BY A QUADRUPOOLE IN WORLD F    238   // FIELD CREATED BY A QUADRUPOOLE IN WORLD FRAME
240   G4double Bx_quad,By_quad,Bz_quad;               239   G4double Bx_quad,By_quad,Bz_quad;
241   Bx_quad = 0; By_quad=0; Bz_quad=0;              240   Bx_quad = 0; By_quad=0; Bz_quad=0;
242                                                   241   
243   // QUADRUPOLE FRAME                             242   // QUADRUPOLE FRAME
244   G4double x_local,y_local,z_local;               243   G4double x_local,y_local,z_local;
245   x_local= 0; y_local=0; z_local=0;               244   x_local= 0; y_local=0; z_local=0;
246                                                   245 
247   G4double vars = 0;                              246   G4double vars = 0;
248   G4double G0, G1, G2, G3;                        247   G4double G0, G1, G2, G3;
249   G4double K1, K2, K3;                            248   G4double K1, K2, K3;
250   G4double P0, P1, P2,     cte;                   249   G4double P0, P1, P2,     cte;
251                                                   250 
252   K1=0;                                           251   K1=0;
253   K2=0;                                           252   K2=0;
254   K3=0;                                           253   K3=0;
255   P0=0;                                           254   P0=0;
256   P1=0;                                           255   P1=0;
257   P2=0;                                           256   P2=0;
258   G0=0;                                           257   G0=0;
259   G1=0;                                           258   G1=0;
260   G2=0;                                           259   G2=0;
261   G3=0;                                           260   G3=0;
262   cte=0;                                          261   cte=0;
263                                                   262 
264   G4bool largeScattering=false;                   263   G4bool largeScattering=false;
265                                                   264   
266   for (G4int i=0;i<4; i++)                        265   for (G4int i=0;i<4; i++) 
267   {                                               266   {
268                                                   267  
269    if (i==0)                                      268    if (i==0) 
270     { xoprime = lineX + quadHalfLength*std::si    269     { xoprime = lineX + quadHalfLength*std::sin(lineAngle);
271       zoprime = lineZ + quadHalfLength*std::co    270       zoprime = lineZ + quadHalfLength*std::cos(lineAngle);
272                                                   271 
273       x_local = (x - xoprime) * std::cos (line    272       x_local = (x - xoprime) * std::cos (lineAngle) - (z - zoprime) * std::sin (lineAngle); 
274       y_local = y;                                273       y_local = y; 
275       z_local = (z - zoprime) * std::cos (line    274       z_local = (z - zoprime) * std::cos (lineAngle) + (x - xoprime) * std::sin (lineAngle); 
276       if (std::sqrt(x_local*x_local+y_local*y_    275       if (std::sqrt(x_local*x_local+y_local*y_local)>a0[i]) largeScattering=true;
277                                                   276 
278     }                                             277     }
279                                                   278      
280    if (i==1)                                      279    if (i==1) 
281     { xoprime = lineX + (3*quadHalfLength+quad    280     { xoprime = lineX + (3*quadHalfLength+quadSpacing)*std::sin(lineAngle);
282       zoprime = lineZ + (3*quadHalfLength+quad    281       zoprime = lineZ + (3*quadHalfLength+quadSpacing)*std::cos(lineAngle);
283                                                   282 
284       x_local = (x - xoprime) * std::cos (line    283       x_local = (x - xoprime) * std::cos (lineAngle) - (z - zoprime) * std::sin (lineAngle); 
285       y_local = y;                                284       y_local = y; 
286       z_local = (z - zoprime) * std::cos (line    285       z_local = (z - zoprime) * std::cos (lineAngle) + (x - xoprime) * std::sin (lineAngle); 
287       if (std::sqrt(x_local*x_local+y_local*y_    286       if (std::sqrt(x_local*x_local+y_local*y_local)>a0[i]) largeScattering=true;
288     }                                             287     }
289                                                   288 
290    if (i==2)                                      289    if (i==2) 
291     { xoprime = lineX + (5*quadHalfLength+2*qu    290     { xoprime = lineX + (5*quadHalfLength+2*quadSpacing)*std::sin(lineAngle);
292       zoprime = lineZ + (5*quadHalfLength+2*qu    291       zoprime = lineZ + (5*quadHalfLength+2*quadSpacing)*std::cos(lineAngle);
293                                                   292 
294       x_local = (x - xoprime) * std::cos (line    293       x_local = (x - xoprime) * std::cos (lineAngle) - (z - zoprime) * std::sin (lineAngle); 
295       y_local = y;                                294       y_local = y; 
296       z_local = (z - zoprime) * std::cos (line    295       z_local = (z - zoprime) * std::cos (lineAngle) + (x - xoprime) * std::sin (lineAngle); 
297       if (std::sqrt(x_local*x_local+y_local*y_    296       if (std::sqrt(x_local*x_local+y_local*y_local)>a0[i]) largeScattering=true;
298     }                                             297     }
299                                                   298    
300    if (i==3)                                      299    if (i==3) 
301     { xoprime = lineX + (7*quadHalfLength+3*qu    300     { xoprime = lineX + (7*quadHalfLength+3*quadSpacing)*std::sin(lineAngle);
302       zoprime = lineZ + (7*quadHalfLength+3*qu    301       zoprime = lineZ + (7*quadHalfLength+3*quadSpacing)*std::cos(lineAngle);
303                                                   302 
304       x_local = (x - xoprime) * std::cos (line    303       x_local = (x - xoprime) * std::cos (lineAngle) - (z - zoprime) * std::sin (lineAngle); 
305       y_local = y;                                304       y_local = y; 
306       z_local = (z - zoprime) * std::cos (line    305       z_local = (z - zoprime) * std::cos (lineAngle) + (x - xoprime) * std::sin (lineAngle); 
307       if (std::sqrt(x_local*x_local+y_local*y_    306       if (std::sqrt(x_local*x_local+y_local*y_local)>a0[i]) largeScattering=true;
308     }                                             307     }
309                                                   308 
310                                                   309    
311    if ( z_local < -z2[i] )                     << 310     if ( z_local < -z2[i] )
312    {                                              311    {
313     G0=0;                                         312     G0=0;
314     G1=0;                                         313     G1=0;
315     G2=0;                                         314     G2=0;
316     G3=0;                                         315     G3=0;
317    }                                              316    }
318                                                   317    
319    if ( z_local > z2[i] )                         318    if ( z_local > z2[i] )
320    {                                              319    {
321     G0=0;                                         320     G0=0;
322     G1=0;                                         321     G1=0;
323     G2=0;                                         322     G2=0;
324     G3=0;                                         323     G3=0;
325    }                                              324    }
326                                                   325 
327    if ( (z_local>=-z1[i]) & (z_local<=z1[i]) )    326    if ( (z_local>=-z1[i]) & (z_local<=z1[i]) ) 
328    {                                              327    {
329     G0=gradient[i];                               328     G0=gradient[i];
330     G1=0;                                         329     G1=0;
331     G2=0;                                         330     G2=0;
332     G3=0;                                         331     G3=0;
333    }                                              332    }
334                                                   333    
335    if ( ((z_local>=-z2[i]) & (z_local<-z1[i]))    334    if ( ((z_local>=-z2[i]) & (z_local<-z1[i])) ||  ((z_local>z1[i]) & (z_local<=z2[i])) ) 
336    {                                              335    {
337                                                   336 
338     vars = ( z_local - z1[i]) / a0[i] ;        << 337    vars = ( z_local - z1[i]) / a0[i] ;
339       if (z_local<-z1[i]) vars = ( - z_local - << 338      if (z_local<-z1[i]) vars = ( - z_local - z1[i]) / a0[i] ;
340                                                   339 
341                                                   340 
342     P0 = c0[i]+c1[i]*vars+c2[i]*vars*vars;     << 341    P0 = c0[i]+c1[i]*vars+c2[i]*vars*vars;
343                                                   342 
344     P1 = c1[i]/a0[i]+2*c2[i]*(z_local-z1[i])/a << 343    P1 = c1[i]/a0[i]+2*c2[i]*(z_local-z1[i])/a0[i]/a0[i];
345     if (z_local<-z1[i])  P1 = -c1[i]/a0[i]+2*c << 344    if (z_local<-z1[i])  P1 = -c1[i]/a0[i]+2*c2[i]*(z_local+z1[i])/a0[i]/a0[i];
346                                                   345 
347     P2 = 2*c2[i]/a0[i]/a0[i];                  << 346    P2 = 2*c2[i]/a0[i]/a0[i];
348                                                   347 
349     cte = 1 + G4Exp(c0[i]);                    << 348    cte = 1 + G4Exp(c0[i]);
350                                                   349 
351     K1 = -cte*P1*G4Exp(P0)/( (1+G4Exp(P0))*(1+ << 350    K1 = -cte*P1*G4Exp(P0)/( (1+G4Exp(P0))*(1+G4Exp(P0)) );
352                                                   351 
353     K2 = -cte*G4Exp(P0)*(                      << 352    K2 = -cte*G4Exp(P0)*(
354      P2/( (1+G4Exp(P0))*(1+G4Exp(P0)) )        << 353     P2/( (1+G4Exp(P0))*(1+G4Exp(P0)) )
355     +2*P1*K1/(1+G4Exp(P0))/cte                 << 354    +2*P1*K1/(1+G4Exp(P0))/cte
356     +P1*P1/(1+G4Exp(P0))/(1+G4Exp(P0))         << 355    +P1*P1/(1+G4Exp(P0))/(1+G4Exp(P0))
357     );                                         << 356    );
358                                                   357  
359     K3 = -cte*G4Exp(P0)*(                      << 358    K3 = -cte*G4Exp(P0)*(
360     (3*P2*P1+P1*P1*P1)/(1+G4Exp(P0))/(1+G4Exp( << 359    (3*P2*P1+P1*P1*P1)/(1+G4Exp(P0))/(1+G4Exp(P0))
361     +4*K1*(P1*P1+P2)/(1+G4Exp(P0))/cte         << 360    +4*K1*(P1*P1+P2)/(1+G4Exp(P0))/cte
362     +2*P1*(K1*K1/cte/cte+K2/(1+G4Exp(P0))/cte) << 361    +2*P1*(K1*K1/cte/cte+K2/(1+G4Exp(P0))/cte)
363      );                                        << 362     );
364                                                   363     
365     G0 = gradient[i]*cte/(1+G4Exp(P0));        << 364    G0 = gradient[i]*cte/(1+G4Exp(P0));
366     G1 = gradient[i]*K1;                       << 365    G1 = gradient[i]*K1;
367     G2 = gradient[i]*K2;                       << 366    G2 = gradient[i]*K2;
368     G3 = gradient[i]*K3;                       << 367    G3 = gradient[i]*K3;
369                                                   368 
370    }                                              369    }
371                                                   370     
372    // PROTECTION AGAINST LARGE SCATTERING         371    // PROTECTION AGAINST LARGE SCATTERING
373                                                   372 
374    if ( largeScattering )                         373    if ( largeScattering ) 
375    {                                              374    {
376     G0=0;                                         375     G0=0;
377     G1=0;                                         376     G1=0;
378     G2=0;                                         377     G2=0;
379     G3=0;                                         378     G3=0;
380    }                                              379    }
381                                                   380 
382    // MAGNETIC FIELD COMPUTATION FOR EACH QUAD    381    // MAGNETIC FIELD COMPUTATION FOR EACH QUADRUPOLE
383                                                   382    
384    Bx_local = y_local*(G0-(1./12)*(3*x_local*x    383    Bx_local = y_local*(G0-(1./12)*(3*x_local*x_local+y_local*y_local)*G2);
385    By_local = x_local*(G0-(1./12)*(3*y_local*y    384    By_local = x_local*(G0-(1./12)*(3*y_local*y_local+x_local*x_local)*G2);
386    Bz_local = x_local*y_local*(G1-(1./12)*(x_l    385    Bz_local = x_local*y_local*(G1-(1./12)*(x_local*x_local+y_local*y_local)*G3);
387                                                   386 
388    Bx_quad = Bz_local*std::sin(lineAngle)+Bx_l    387    Bx_quad = Bz_local*std::sin(lineAngle)+Bx_local*std::cos(lineAngle);
389    By_quad = By_local;                            388    By_quad = By_local;
390    Bz_quad = Bz_local*std::cos(lineAngle)-Bx_l    389    Bz_quad = Bz_local*std::cos(lineAngle)-Bx_local*std::sin(lineAngle);
391                                                   390 
392    // TOTAL MAGNETIC FIELD                        391    // TOTAL MAGNETIC FIELD
393                                                   392    
394    Bx = Bx + Bx_quad ;                            393    Bx = Bx + Bx_quad ;
395    By = By + By_quad ;                            394    By = By + By_quad ;
396    Bz = Bz + Bz_quad ;                            395    Bz = Bz + Bz_quad ;
397                                                   396 
398   } // LOOP ON QUADRUPOLES                        397   } // LOOP ON QUADRUPOLES
399                                                   398 
400                                                   399       
401 } // END OF QUADRUPLET                            400 } // END OF QUADRUPLET
402                                                   401 
403   Bfield[0] = Bx;                                 402   Bfield[0] = Bx;
404   Bfield[1] = By;                                 403   Bfield[1] = By;
405   Bfield[2] = Bz;                                 404   Bfield[2] = Bz;
406                                                   405 
407 // *****************************************      406 // *****************************************
408 // ELECTRIC FIELD CREATED BY SCANNING PLATES      407 // ELECTRIC FIELD CREATED BY SCANNING PLATES
409 // *****************************************      408 // *****************************************
410                                                   409 
411   Bfield[3] = 0;                                  410   Bfield[3] = 0;
412   Bfield[4] = 0;                                  411   Bfield[4] = 0;
413   Bfield[5] = 0;                                  412   Bfield[5] = 0;
414                                                   413 
415   // POSITION OF EXIT OF LAST QUAD WHERE THE S    414   // POSITION OF EXIT OF LAST QUAD WHERE THE SCANNING PLATES START
416                                                   415 
417   G4double electricPlateWidth1 = 5 * mm;          416   G4double electricPlateWidth1 = 5 * mm;
418   G4double electricPlateWidth2 = 5 * mm;          417   G4double electricPlateWidth2 = 5 * mm;
419   G4double electricPlateLength1 = 36 * mm;        418   G4double electricPlateLength1 = 36 * mm;
420   G4double electricPlateLength2 = 34 * mm;        419   G4double electricPlateLength2 = 34 * mm;
421   G4double electricPlateGap = 5 * mm;             420   G4double electricPlateGap = 5 * mm;
422   G4double electricPlateSpacing1 = 3 * mm;        421   G4double electricPlateSpacing1 = 3 * mm;
423   G4double electricPlateSpacing2 = 4 * mm;        422   G4double electricPlateSpacing2 = 4 * mm;
424                                                   423 
425   // APPLY VOLTAGE HERE IN VOLTS (no electrost    424   // APPLY VOLTAGE HERE IN VOLTS (no electrostatic deflection here)
426   G4double electricPlateVoltage1 = 0 * volt;      425   G4double electricPlateVoltage1 = 0 * volt;
427   G4double electricPlateVoltage2 = 0 * volt;      426   G4double electricPlateVoltage2 = 0 * volt;
428                                                   427 
429   G4double electricFieldPlate1 = electricPlate    428   G4double electricFieldPlate1 = electricPlateVoltage1 / electricPlateSpacing1 ;
430   G4double electricFieldPlate2 = electricPlate    429   G4double electricFieldPlate2 = electricPlateVoltage2 / electricPlateSpacing2 ;
431                                                   430 
432   G4double  beginFirstZoneX = lineX + (8*quadH    431   G4double  beginFirstZoneX = lineX + (8*quadHalfLength+3*quadSpacing)*std::sin(lineAngle);
433   G4double  beginFirstZoneZ = lineZ + (8*quadH    432   G4double  beginFirstZoneZ = lineZ + (8*quadHalfLength+3*quadSpacing)*std::cos(lineAngle);
434                                                   433 
435   G4double  beginSecondZoneX = lineX + (8*quad    434   G4double  beginSecondZoneX = lineX + (8*quadHalfLength+3*quadSpacing+electricPlateLength1+electricPlateGap)*std::sin(lineAngle);
436   G4double  beginSecondZoneZ = lineZ + (8*quad    435   G4double  beginSecondZoneZ = lineZ + (8*quadHalfLength+3*quadSpacing+electricPlateLength1+electricPlateGap)*std::cos(lineAngle);
437                                                   436 
438   G4double xA, zA, xB, zB, xC, zC, xD, zD;        437   G4double xA, zA, xB, zB, xC, zC, xD, zD;
439   G4double slope1, cte1, slope2, cte2, slope3,    438   G4double slope1, cte1, slope2, cte2, slope3, cte3, slope4, cte4;
440                                                   439  
441   // WARNING : lineAngle < 0                      440   // WARNING : lineAngle < 0
442                                                   441 
443   // FIRST PLATES                                 442   // FIRST PLATES
444                                                   443   
445   xA = beginFirstZoneX + std::cos(lineAngle)*e    444   xA = beginFirstZoneX + std::cos(lineAngle)*electricPlateSpacing1/2;
446   zA = beginFirstZoneZ - std::sin(lineAngle)*e    445   zA = beginFirstZoneZ - std::sin(lineAngle)*electricPlateSpacing1/2;
447                                                   446 
448   xB = xA + std::sin(lineAngle)*electricPlateL    447   xB = xA + std::sin(lineAngle)*electricPlateLength1; 
449   zB = zA + std::cos(lineAngle)*electricPlateL    448   zB = zA + std::cos(lineAngle)*electricPlateLength1;
450                                                   449   
451   xC = xB - std::cos(lineAngle)*electricPlateS    450   xC = xB - std::cos(lineAngle)*electricPlateSpacing1;
452   zC = zB + std::sin(lineAngle)*electricPlateS    451   zC = zB + std::sin(lineAngle)*electricPlateSpacing1;
453                                                   452 
454   xD = xC - std::sin(lineAngle)*electricPlateL    453   xD = xC - std::sin(lineAngle)*electricPlateLength1; 
455   zD = zC - std::cos(lineAngle)*electricPlateL    454   zD = zC - std::cos(lineAngle)*electricPlateLength1;
456                                                   455   
457   slope1 = (xB-xA)/(zB-zA);                       456   slope1 = (xB-xA)/(zB-zA);
458   cte1 = xA - slope1 * zA;                        457   cte1 = xA - slope1 * zA;
459                                                   458   
460   slope2 = (xC-xB)/(zC-zB);                       459   slope2 = (xC-xB)/(zC-zB);
461   cte2 = xB - slope2 * zB;                        460   cte2 = xB - slope2 * zB;
462                                                   461   
463   slope3 = (xD-xC)/(zD-zC);                       462   slope3 = (xD-xC)/(zD-zC);
464   cte3 = xC - slope3 * zC;                        463   cte3 = xC - slope3 * zC;
465                                                   464   
466   slope4 = (xA-xD)/(zA-zD);                       465   slope4 = (xA-xD)/(zA-zD);
467   cte4 = xD - slope4 * zD;                        466   cte4 = xD - slope4 * zD;
468                                                   467   
469                                                   468    
470   if                                              469   if 
471   (                                               470   (
472        x <= slope1 * z + cte1                  << 471      x <= slope1 * z + cte1
473     && x >= slope3 * z + cte3                  << 472   && x >= slope3 * z + cte3
474     && x <= slope4 * z + cte4                  << 473   && x <= slope4 * z + cte4
475     && x >= slope2 * z + cte2                  << 474   && x >= slope2 * z + cte2    
476     && std::abs(y)<=electricPlateWidth1/2      << 475   && std::abs(y)<=electricPlateWidth1/2
477   )                                               476   )  
478                                                   477 
479   {                                               478   {
480       Bfield[3] = electricFieldPlate1*std::cos    479       Bfield[3] = electricFieldPlate1*std::cos(lineAngle);
481       Bfield[4] = 0;                              480       Bfield[4] = 0;
482       Bfield[5] = -electricFieldPlate1*std::si    481       Bfield[5] = -electricFieldPlate1*std::sin(lineAngle);
483                                                   482  
484   }                                               483   }
485                                                   484       
486   // SECOND PLATES                                485   // SECOND PLATES
487                                                   486       
488   xA = beginSecondZoneX + std::cos(lineAngle)*    487   xA = beginSecondZoneX + std::cos(lineAngle)*electricPlateWidth2/2;
489   zA = beginSecondZoneZ - std::sin(lineAngle)*    488   zA = beginSecondZoneZ - std::sin(lineAngle)*electricPlateWidth2/2;
490                                                   489 
491   xB = xA + std::sin(lineAngle)*electricPlateL    490   xB = xA + std::sin(lineAngle)*electricPlateLength2; 
492   zB = zA + std::cos(lineAngle)*electricPlateL    491   zB = zA + std::cos(lineAngle)*electricPlateLength2;
493                                                   492   
494   xC = xB - std::cos(lineAngle)*electricPlateW    493   xC = xB - std::cos(lineAngle)*electricPlateWidth2;
495   zC = zB + std::sin(lineAngle)*electricPlateW    494   zC = zB + std::sin(lineAngle)*electricPlateWidth2;
496                                                   495 
497   xD = xC - std::sin(lineAngle)*electricPlateL    496   xD = xC - std::sin(lineAngle)*electricPlateLength2; 
498   zD = zC - std::cos(lineAngle)*electricPlateL    497   zD = zC - std::cos(lineAngle)*electricPlateLength2;
499                                                   498   
500   slope1 = (xB-xA)/(zB-zA);                       499   slope1 = (xB-xA)/(zB-zA);
501   cte1 = xA - slope1 * zA;                        500   cte1 = xA - slope1 * zA;
502                                                   501   
503   slope2 = (xC-xB)/(zC-zB);                       502   slope2 = (xC-xB)/(zC-zB);
504   cte2 = xB - slope2 * zB;                        503   cte2 = xB - slope2 * zB;
505                                                   504   
506   slope3 = (xD-xC)/(zD-zC);                       505   slope3 = (xD-xC)/(zD-zC);
507   cte3 = xC - slope3 * zC;                        506   cte3 = xC - slope3 * zC;
508                                                   507   
509   slope4 = (xA-xD)/(zA-zD);                       508   slope4 = (xA-xD)/(zA-zD);
510   cte4 = xD - slope4 * zD;                        509   cte4 = xD - slope4 * zD;
511                                                   510 
512   if                                              511   if 
513   (                                               512   (     
514        x <= slope1 * z + cte1                  << 513      x <= slope1 * z + cte1
515     && x >= slope3 * z + cte3                  << 514   && x >= slope3 * z + cte3
516     && x <= slope4 * z + cte4                  << 515   && x <= slope4 * z + cte4
517     && x >= slope2 * z + cte2                  << 516   && x >= slope2 * z + cte2    
518     && std::abs(y)<=electricPlateSpacing2/2    << 517   && std::abs(y)<=electricPlateSpacing2/2
519   )                                               518   )
520                                                   519 
521   {                                               520   {  
522       Bfield[3] = 0;                              521       Bfield[3] = 0;
523       Bfield[4] = electricFieldPlate2;            522       Bfield[4] = electricFieldPlate2;
524       Bfield[5] = 0;                              523       Bfield[5] = 0;
525   }                                               524   }
                                                   >> 525 
                                                   >> 526 //
526                                                   527 
527 }                                                 528 }
528                                                   529