Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer << 3 // * DISCLAIMER * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th << 5 // * The following disclaimer summarizes all the specific disclaimers * 6 // * the Geant4 Collaboration. It is provided << 6 // * of contributors to this software. The specific disclaimers,which * 7 // * conditions of the Geant4 Software License << 7 // * govern, are listed with their locations in: * 8 // * LICENSE and available at http://cern.ch/ << 8 // * http://cern.ch/geant4/license * 9 // * include a list of copyright holders. << 10 // * 9 // * * 11 // * Neither the authors of this software syst 10 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 11 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 12 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 13 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file << 14 // * use. * 16 // * for the full disclaimer and the limitatio << 17 // * 15 // * * 18 // * This code implementation is the result << 16 // * This code implementation is the intellectual property of the * 19 // * technical work of the GEANT4 collaboratio << 17 // * GEANT4 collaboration. * 20 // * By using, copying, modifying or distri << 18 // * By copying, distributing or modifying the Program (or any work * 21 // * any work based on the software) you ag << 19 // * based on the Program) you indicate your acceptance of this * 22 // * use in resulting scientific publicati << 20 // * statement, and all its terms. * 23 // * acceptance of all terms of the Geant4 Sof << 24 // ******************************************* 21 // ******************************************************************** 25 // 22 // 26 ////////////////////////////////////////////// 23 /////////////////////////////////////////////////////////////////////////////// 27 // File: CCalMagneticField.cc 24 // File: CCalMagneticField.cc 28 // Description: User Field class implementatio 25 // Description: User Field class implementation. 29 ////////////////////////////////////////////// 26 /////////////////////////////////////////////////////////////////////////////// 30 #include <fstream> << 31 << 32 #include "CCalMagneticField.hh" 27 #include "CCalMagneticField.hh" 33 #include "CCalutils.hh" 28 #include "CCalutils.hh" 34 #include "G4SystemOfUnits.hh" << 35 #include "G4FieldManager.hh" 29 #include "G4FieldManager.hh" >> 30 #include <fstream> 36 31 37 //#define ddebug 32 //#define ddebug 38 //#define debug 33 //#define debug 39 34 40 //Constructor and destructor: 35 //Constructor and destructor: 41 36 42 CCalMagneticField::CCalMagneticField(const G4S 37 CCalMagneticField::CCalMagneticField(const G4String &filename) : 43 fval(0), pos(0), slope(0), intercept(0) << 38 fval(0), pos(0), slope(0), intercept(0) { 44 { << 45 #ifdef debug << 46 fVerbosity = 1; << 47 #else << 48 fVerbosity = 0; << 49 #endif << 50 39 51 //Let's open the file 40 //Let's open the file 52 G4cout << " ==> Opening file " << filename < 41 G4cout << " ==> Opening file " << filename << " to read magnetic field..." 53 << G4endl; << 42 << G4endl; 54 G4String pathName = std::getenv("CCAL_GLOBAL << 43 G4String pathName = getenv("CCAL_GLOBALPATH"); 55 std::ifstream is; 44 std::ifstream is; 56 G4bool ok = openGeomFile(is, pathName, filen << 45 bool ok = openGeomFile(is, pathName, filename); 57 << 46 58 if (ok) { 47 if (ok) { 59 findDO(is, G4String("FLDM")); 48 findDO(is, G4String("FLDM")); 60 is >> fval >> npts >> xoff; 49 is >> fval >> npts >> xoff; 61 << 50 #ifdef debug 62 if (fVerbosity) << 51 G4cout << "Field value " << fval << " # points " << npts << " offset in x " 63 G4cout << "Field value " << fval << " # << 52 << xoff*mm << G4endl; 64 " offset in x " << 53 #endif 65 << xoff*mm << G4endl; << 66 54 67 if (npts > 0) { 55 if (npts > 0) { 68 pos = new G4double[npts]; 56 pos = new G4double[npts]; 69 slope = new G4double[npts]; 57 slope = new G4double[npts]; 70 intercept = new G4double[npts]; 58 intercept = new G4double[npts]; 71 59 72 for (G4int i = 0; i < npts; i++) { 60 for (G4int i = 0; i < npts; i++) { 73 is >> pos[i] >> slope[i] >> intercept[ << 61 is >> pos[i] >> slope[i] >> intercept[i]; 74 if (fVerbosity) << 62 #ifdef debug 75 G4cout << tab << "Position " << i << << 63 G4cout << tab << "Position " << i << " " << pos[i] << " Slope " 76 << slope[i] << " Intercept " << 64 << slope[i] << " Intercept " << intercept[i] << G4endl; >> 65 #endif 77 } 66 } 78 } 67 } 79 68 80 ////////////////////////////////////////// 69 /////////////////////////////////////////////////////////////// 81 // Close the file 70 // Close the file 82 G4cout << " ==> Closing file " << filename 71 G4cout << " ==> Closing file " << filename << G4endl; 83 is.close(); 72 is.close(); 84 } 73 } 85 } 74 } 86 75 87 76 88 CCalMagneticField::~CCalMagneticField() { 77 CCalMagneticField::~CCalMagneticField() { 89 if (pos) 78 if (pos) 90 delete[] pos; 79 delete[] pos; 91 if (slope) 80 if (slope) 92 delete[] slope; 81 delete[] slope; 93 if (intercept) 82 if (intercept) 94 delete[] intercept; 83 delete[] intercept; 95 } 84 } 96 85 97 86 98 // Member functions 87 // Member functions 99 88 100 void CCalMagneticField::MagneticField(const G4 << 89 void CCalMagneticField::MagneticField(const double x[3], double B[3]) const { 101 { << 90 102 G4int i=0; 91 G4int i=0; 103 for (i=0; i<2; i++) { 92 for (i=0; i<2; i++) { 104 B[i] = 0*kilogauss; 93 B[i] = 0*kilogauss; 105 } 94 } 106 95 107 G4double m1=0; << 96 G4double m=0, c=0; 108 G4double c1=0; << 109 G4double xnew = x[0]/mm + xoff; 97 G4double xnew = x[0]/mm + xoff; 110 if (npts > 0) { 98 if (npts > 0) { 111 for (i=0; i<npts; i++) { 99 for (i=0; i<npts; i++) { 112 if (xnew > pos[i]*mm) { 100 if (xnew > pos[i]*mm) { 113 m1 = slope[i]; << 101 m = slope[i]; 114 c1 = intercept[i]; << 102 c = intercept[i]; 115 } 103 } 116 } 104 } 117 } 105 } 118 G4double scor = c1 + m*xnew; << 106 G4double scor = c + m*xnew; 119 if (scor < 0.) scor = 0.; 107 if (scor < 0.) scor = 0.; 120 if (scor > 1.) scor = 1.0; 108 if (scor > 1.) scor = 1.0; 121 109 122 B[2] = scor*fval*kilogauss; 110 B[2] = scor*fval*kilogauss; 123 if (fVerbosity) << 111 #ifdef ddebug 124 { << 112 G4cout << "Field at x: " << x[0]/mm << "mm (" << xnew << ") = " << B[2]/tesla 125 << 113 << "T (m = " << m << ", c = " << c << ", scale = " << scor << ")" 126 G4cout << "Field at x: " << x[0]/mm << " << 114 << G4endl; 127 B[2]/tesla << 115 #endif 128 << "T (m = " << m1 << ", c = " << << 129 c1 << ", scale = " << scor << ")" << 130 << G4endl; << 131 } << 132 } 116 } 133 117 134 118 135 CLHEP::Hep3Vector CCalMagneticField:: << 119 Hep3Vector CCalMagneticField::MagneticField(const Hep3Vector point) const { 136 MagneticField(const CLHEP::Hep3Vector point) c << 137 120 138 G4double x[3],B[3]; 121 G4double x[3],B[3]; 139 CLHEP::Hep3Vector v; << 122 Hep3Vector v; 140 123 141 x[0] = point.x(); 124 x[0] = point.x(); 142 x[1] = point.y(); 125 x[1] = point.y(); 143 x[2] = point.z(); 126 x[2] = point.z(); 144 CCalMagneticField::MagneticField(x, B); 127 CCalMagneticField::MagneticField(x, B); 145 v.setX(B[0]); 128 v.setX(B[0]); 146 v.setY(B[1]); 129 v.setY(B[1]); 147 v.setZ(B[2]); 130 v.setZ(B[2]); 148 return v; 131 return v; 149 } 132 } 150 133 151 134 152 void CCalMagneticField::GetFieldValue(const G4 << 135 void CCalMagneticField::GetFieldValue(const double x[3], double* B) const { 153 CCalMagneticField::MagneticField(x, B); 136 CCalMagneticField::MagneticField(x, B); 154 } 137 } 155 138 156 139