source: trunk/examples/advanced/composite_calorimeter/src/CCalMagneticField.cc @ 1304

Last change on this file since 1304 was 807, checked in by garnier, 16 years ago

update

File size: 4.3 KB
Line 
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//
26///////////////////////////////////////////////////////////////////////////////
27// File: CCalMagneticField.cc
28// Description: User Field class implementation.
29///////////////////////////////////////////////////////////////////////////////
30#include "CCalMagneticField.hh"
31#include "CCalutils.hh"
32#include "G4FieldManager.hh"
33#include <fstream>
34
35//#define ddebug
36//#define debug
37
38//Constructor and destructor:
39
40CCalMagneticField::CCalMagneticField(const G4String &filename) :
41  fval(0), pos(0), slope(0), intercept(0) {
42
43  //Let's open the file
44  G4cout << " ==> Opening file " << filename << " to read magnetic field..."
45       << G4endl;
46  G4String pathName = getenv("CCAL_GLOBALPATH");
47  std::ifstream is;
48  bool ok = openGeomFile(is, pathName, filename);
49
50  if (ok) {
51    findDO(is, G4String("FLDM"));
52    is >> fval >> npts >> xoff;
53#ifdef debug
54    G4cout << "Field value " << fval << " # points " << npts << " offset in x "
55         << xoff*mm << G4endl;
56#endif
57
58    if (npts > 0) {
59      pos       = new G4double[npts];
60      slope     = new G4double[npts];
61      intercept = new G4double[npts];
62
63      for (G4int i = 0; i < npts; i++) {
64        is >> pos[i] >> slope[i] >> intercept[i];
65#ifdef debug
66        G4cout << tab << "Position " << i << " " << pos[i] << " Slope "
67             << slope[i] << " Intercept " << intercept[i] << G4endl;
68#endif
69      }
70    }
71
72    ///////////////////////////////////////////////////////////////
73    // Close the file
74    G4cout << " ==> Closing file " << filename << G4endl;
75    is.close();
76  }
77}
78
79
80CCalMagneticField::~CCalMagneticField() {
81  if (pos)
82    delete[] pos;
83  if (slope)
84    delete[] slope;
85  if (intercept)
86    delete[] intercept;
87}
88
89
90// Member functions
91
92void CCalMagneticField::MagneticField(const double x[3], double B[3]) const {
93
94  G4int i=0;
95  for (i=0; i<2; i++) {
96    B[i]   = 0*kilogauss;
97  }
98
99  G4double m=0, c=0;
100  G4double xnew = x[0]/mm + xoff;
101  if (npts > 0) {
102    for (i=0; i<npts; i++) {
103      if (xnew > pos[i]*mm) {
104        m = slope[i];
105        c = intercept[i];
106      }
107    }
108  }
109  G4double scor = c + m*xnew;
110  if (scor < 0.) scor = 0.;
111  if (scor > 1.) scor = 1.0;
112
113  B[2] = scor*fval*kilogauss;
114#ifdef ddebug
115  G4cout << "Field at x: " << x[0]/mm << "mm (" << xnew << ") = " << B[2]/tesla
116         << "T (m = " << m << ", c = " << c << ", scale = " << scor << ")"
117         << G4endl;
118#endif
119}
120
121
122CLHEP::Hep3Vector CCalMagneticField::
123MagneticField(const CLHEP::Hep3Vector point) const {
124
125  G4double x[3],B[3];
126  CLHEP::Hep3Vector v;
127 
128  x[0] = point.x();
129  x[1] = point.y();
130  x[2] = point.z();
131  CCalMagneticField::MagneticField(x, B);
132  v.setX(B[0]);   
133  v.setY(B[1]);   
134  v.setZ(B[2]);   
135  return v;
136}
137
138
139void CCalMagneticField::GetFieldValue(const double x[3], double* B) const {
140  CCalMagneticField::MagneticField(x, B);
141}
142
Note: See TracBrowser for help on using the repository browser.