Geant4  10.00.p02
CCalMagneticField.cc
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
27 // File: CCalMagneticField.cc
28 // Description: User Field class implementation.
30 #include <fstream>
31 
32 #include "CCalMagneticField.hh"
33 #include "CCalutils.hh"
34 #include "G4SystemOfUnits.hh"
35 #include "G4FieldManager.hh"
36 
37 //#define ddebug
38 //#define debug
39 
40 //Constructor and destructor:
41 
43  fval(0), pos(0), slope(0), intercept(0) {
44 
45  //Let's open the file
46  G4cout << " ==> Opening file " << filename << " to read magnetic field..."
47  << G4endl;
48  G4String pathName = getenv("CCAL_GLOBALPATH");
49  std::ifstream is;
50  bool ok = openGeomFile(is, pathName, filename);
51 
52  if (ok) {
53  findDO(is, G4String("FLDM"));
54  is >> fval >> npts >> xoff;
55 #ifdef debug
56  G4cout << "Field value " << fval << " # points " << npts << " offset in x "
57  << xoff*mm << G4endl;
58 #endif
59 
60  if (npts > 0) {
61  pos = new G4double[npts];
62  slope = new G4double[npts];
63  intercept = new G4double[npts];
64 
65  for (G4int i = 0; i < npts; i++) {
66  is >> pos[i] >> slope[i] >> intercept[i];
67 #ifdef debug
68  G4cout << tab << "Position " << i << " " << pos[i] << " Slope "
69  << slope[i] << " Intercept " << intercept[i] << G4endl;
70 #endif
71  }
72  }
73 
75  // Close the file
76  G4cout << " ==> Closing file " << filename << G4endl;
77  is.close();
78  }
79 }
80 
81 
83  if (pos)
84  delete[] pos;
85  if (slope)
86  delete[] slope;
87  if (intercept)
88  delete[] intercept;
89 }
90 
91 
92 // Member functions
93 
94 void CCalMagneticField::MagneticField(const double x[3], double B[3]) const {
95 
96  G4int i=0;
97  for (i=0; i<2; i++) {
98  B[i] = 0*kilogauss;
99  }
100 
101  G4double m1=0, c1=0;
102  G4double xnew = x[0]/mm + xoff;
103  if (npts > 0) {
104  for (i=0; i<npts; i++) {
105  if (xnew > pos[i]*mm) {
106  m1 = slope[i];
107  c1 = intercept[i];
108  }
109  }
110  }
111  G4double scor = c1 + m*xnew;
112  if (scor < 0.) scor = 0.;
113  if (scor > 1.) scor = 1.0;
114 
115  B[2] = scor*fval*kilogauss;
116 #ifdef ddebug
117  G4cout << "Field at x: " << x[0]/mm << "mm (" << xnew << ") = " << B[2]/tesla
118  << "T (m = " << m1 << ", c = " << c1 << ", scale = " << scor << ")"
119  << G4endl;
120 #endif
121 }
122 
123 
124 CLHEP::Hep3Vector CCalMagneticField::
125 MagneticField(const CLHEP::Hep3Vector point) const {
126 
127  G4double x[3],B[3];
128  CLHEP::Hep3Vector v;
129 
130  x[0] = point.x();
131  x[1] = point.y();
132  x[2] = point.z();
134  v.setX(B[0]);
135  v.setY(B[1]);
136  v.setZ(B[2]);
137  return v;
138 }
139 
140 
141 void CCalMagneticField::GetFieldValue(const double x[3], double* B) const {
143 }
144 
bool openGeomFile(std::ifstream &is, const G4String &pathname, const G4String &filename)
Definition: CCalutils.cc:116
virtual void GetFieldValue(const double Point[3], double *Bfield) const
std::ifstream & findDO(std::ifstream &, const G4String &)
Definition: CCalutils.cc:72
int G4int
Definition: G4Types.hh:78
G4GLOB_DLL std::ostream G4cout
std::ostream & tab(std::ostream &)
Definition: CCalutils.cc:89
static const G4double c1
void MagneticField(const double Point[3], double Bfield[3]) const
#define G4endl
Definition: G4ios.hh:61
static const double m
Definition: G4SIunits.hh:110
static const double tesla
Definition: G4SIunits.hh:247
double G4double
Definition: G4Types.hh:76
static const double kilogauss
Definition: G4SIunits.hh:250
CCalMagneticField(const G4String &name)
static const double mm
Definition: G4SIunits.hh:102
static const G4double pos