GEMC  2.3
Geant4 Monte-Carlo Framework
multipoleField.cc
Go to the documentation of this file.
1 // gemc headers
2 #include "multipoleField.h"
3 
4 // CLHEP units
5 #include "CLHEP/Units/PhysicalConstants.h"
6 using namespace CLHEP;
7 
8 multipoleField::multipoleField(int Npole, G4double scale, G4double x,
9  G4double y, G4double z, G4double rot, string ROTaxis)
10 {
11  polenumber = Npole;
12  strength = scale;
13  origin[0] = x;
14  origin[1] = y;
15  origin[2] = z;
16  rotation = rot;
17  rotaxis = ROTaxis;
18 
19  if (rotaxis != "X" && rotaxis != "Y" && rotaxis != "Z")
20  {
21  cout << "!!! Error: multipole field has rot axis along X or Y or Z, while you have axis "
22  << rotaxis << endl; exit(-1);
23  }
24 
25 }
26 
28 
29 // ------------------------------------------------------------------------
30 
31 void multipoleField::GetFieldValue(const G4double pos[4], G4double *B) const
32 {
33 
34  G4ThreeVector x0(pos[0], pos[1], pos[2]);
35  G4ThreeVector x1(origin[0], origin[1], origin[2]);
36  G4ThreeVector x2;
37  G4ThreeVector x0_local;
38  if (rotaxis=="X")
39  {
40  x2 = G4ThreeVector(0*cm,0*cm,1*cm).rotateX(rotation)+x1;
41  x0_local = (x0 - x1).rotateX(-rotation);
42  }
43  else if (rotaxis=="Y")
44  {
45  x2 = G4ThreeVector(0*cm,0*cm,1*cm).rotateY(rotation)+x1;
46  x0_local = (x0 - x1).rotateY(-rotation);
47  }
48  else if (rotaxis=="Z")
49  {
50  x2 = G4ThreeVector(0*cm,0*cm,1*cm).rotateZ(rotation)+x1;
51  x0_local = (x0 - x1).rotateZ(-rotation);
52  }
53  else
54  {
55  cout << "!!! Error: multipole field has rot axis along X or Y or Z, while you have axis: "
56  << rotaxis << endl;
57  exit(-1);
58  }
59 
60  G4double r = (x2 - x1).cross(x1 - x0).mag() / (x2 - x1).mag(); //distance from x0 to line x1-x2
61  G4double phi = atan2(x0_local.y(), x0_local.x());
62 
63  G4ThreeVector B_local;
64  if (polenumber == 2)
65  {
66  B_local.setX(0);
67  B_local.setY(strength);
68  B_local.setZ(0);
69  }
70  else
71  {
72  int a = polenumber / 2 - 1;
73  B_local.setX(strength * pow(r/m, a) * sin(a * phi));
74  B_local.setY(strength * pow(r/m, a) * cos(a * phi));
75  B_local.setZ(0);
76  }
77 
78  G4ThreeVector B_lab=B_local;
79  if (rotaxis=="X") {B_lab.rotateX(rotation);}
80  else if (rotaxis=="Y") {B_lab.rotateY(rotation);}
81  else if (rotaxis=="Z") {B_lab.rotateZ(rotation);}
82  else {cout << "!!! Error: multipole field has rot axis along X or Y or Z, while you have axis "
83  << rotaxis << endl; exit(-1);
84  }
85 
86  B[0]=B_lab.x();
87  B[1]=B_lab.y();
88  B[2]=B_lab.z();
89 
90 }
virtual ~multipoleField()
multipoleField(int Npole, G4double scale, G4double x, G4double y, G4double z, G4double rot, string ROTaxis)
virtual void GetFieldValue(const G4double pos[4], G4double *MagField) const