ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4LatticeReader.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4LatticeReader.cc
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 //
28 //
29 // NOTE: This reader class for logical lattices should be moved to
30 // materials/ after the 10.0 release (and this comment removed).
31 //
32 // 20131106 M.Kelsey -- Add const to getenv() to avoid compiler warning.
33 // 20131112 Throw exception if input file fails.
34 // 20131115 Check file input arguments for maps for validity before use;
35 // move ctor, dtor here; check stream pointer before closing.
36 
37 #include "G4LatticeReader.hh"
38 #include "G4ExceptionSeverity.hh"
39 #include "G4LatticeLogical.hh"
40 #include "G4SystemOfUnits.hh"
41 #include <fstream>
42 #include <limits>
43 #include <stdlib.h>
44 
45 
46 // Default path to lattice files, for use with filenames below
47 
49  std::getenv("G4LATTICEDATA") ? (const char*)std::getenv("G4LATTICEDATA") : "./CrystalMaps";
50 
51 
52 // Constructor and destructor
53 
55  : verboseLevel(vb), psLatfile(0), pLattice(0), fMapPath(""),
56  fToken(""), fValue(0.), fMap(""), fsPol(""), fPol(-1), fNX(0), fNY(0) {;}
57 
59  delete psLatfile; psLatfile = 0;
60 }
61 
62 
63 // Main drivers to read configuration from file or stream
64 
66  if (verboseLevel) G4cout << "G4LatticeReader " << filename << G4endl;
67 
68  if (!OpenFile(filename)) {
70  msg << "Unable to open " << filename;
71  G4Exception("G4LatticeReader::MakeLattice", "Lattice001",
72  FatalException, msg);
73  return 0;
74  }
75 
76  pLattice = new G4LatticeLogical; // Create lattice to be filled
77 
78  G4bool goodLattice = true;
79  while (!psLatfile->eof()) {
80  goodLattice &= ProcessToken();
81  }
82  CloseFile();
83 
84  if (!goodLattice) {
86  msg << "Error reading lattice from " << filename;
87  G4Exception("G4LatticeReader::MakeLattice", "Lattice002",
88  FatalException, msg);
89  delete pLattice;
90  pLattice = 0;
91  }
92 
93  return pLattice; // Lattice complete; return pointer with ownership
94 }
95 
96 
97 // Open local file or file found under data path
98 
100  if (verboseLevel)
101  G4cout << "G4LatticeReader::OpenFile " << filename << G4endl;
102 
103  G4String filepath = filename;
104  psLatfile = new std::ifstream(filepath);
105  if (!psLatfile->good()) { // Local file not found
106  filepath = fDataDir + "/" + filename;
107  psLatfile->open(filepath); // Try data directory
108  if (!psLatfile->good()) {
109  CloseFile();
110  return false;
111  }
112  if (verboseLevel>1) G4cout << " Found file " << filepath << G4endl;
113  }
114 
115  // Extract path from filename to use in finding .ssv map files
116  size_t lastdir = filepath.last('/');
117  if (lastdir == std::string::npos) fMapPath = "."; // No path at all
118  else fMapPath = filepath(0,lastdir);
119 
120  return true;
121 }
122 
123 // Close and delete input stream
124 
126  if (psLatfile) psLatfile->close();
127  delete psLatfile;
128  psLatfile = 0;
129 }
130 
131 
132 // Read next token from file, use it to store next data into lattice
133 
135  fToken = "";
136  *psLatfile >> fToken;
137  if (fToken.empty() || psLatfile->eof()) return true; // End of file reached
138 
139  if (verboseLevel>1) G4cout << " ProcessToken " << fToken << G4endl;
140 
141  fToken.toLower();
142  if (fToken.contains('#')) return SkipComments(); // Ignore rest of line
143  if (fToken == "vdir") return ProcessNMap(); // Direction vector map
144  if (fToken == "vg") return ProcessMap(); // Velocity magnitudes
145  if (fToken == "dyn") return ProcessConstants(); // Dynamical parameters
146  return ProcessValue(fToken); // Single numeric value
147 }
148 
149 // Eat remainder of line, assuming a '#' token was found
150 
153  return true; // Never fails
154 }
155 
156 // Read double value from file, store based on name string
157 
159  *psLatfile >> fValue;
160  if (verboseLevel>1) G4cout << " ProcessValue " << fValue << G4endl;
161 
162  G4bool good = true;
163  /***** NOTE: Individual Set functions not included in Release 10.0
164  if (name == "beta") pLattice->SetBeta(fValue);
165  else if (name == "gamma") pLattice->SetGamma(fValue);
166  else if (name == "lambda") pLattice->SetLambda(fValue);
167  else if (name == "mu") pLattice->SetMu(fValue);
168  else *****/
169  if (name == "scat") pLattice->SetScatteringConstant(fValue*s*s*s);
170  else if (name == "b") pLattice->SetScatteringConstant(fValue*s*s*s);
171  else if (name == "decay") pLattice->SetAnhDecConstant(fValue*s*s*s*s);
172  else if (name == "a") pLattice->SetAnhDecConstant(fValue*s*s*s*s);
173  else if (name == "ldos") pLattice->SetLDOS(fValue);
174  else if (name == "stdos") pLattice->SetSTDOS(fValue);
175  else if (name == "ftdos") pLattice->SetFTDOS(fValue);
176  else {
177  G4cerr << "G4LatticeReader: Unrecognized token " << name << G4endl;
178  good = false;
179  }
180 
181  return good;
182 }
183 
185  G4double beta=0., gamma=0., lambda=0., mu=0.;
186  *psLatfile >> beta >> gamma >> lambda >> mu;
187  if (verboseLevel>1)
188  G4cout << " ProcessConstants " << beta << " " << gamma
189  << " " << lambda << " " << mu << G4endl;
190 
191  pLattice->SetDynamicalConstants(beta, gamma, lambda, mu);
192  return psLatfile->good();
193 }
194 
195 // Read map filename, polarization, and binning dimensions
196 
198  *psLatfile >> fMap >> fsPol >> fNX >> fNY;
199  if (verboseLevel>1)
200  G4cout << " ReadMapInfo " << fMap << " " << fsPol
201  << " " << fNX << " " << fNY << G4endl;
202 
203  if (fNX < 0 || fNX >= G4LatticeLogical::MAXRES) {
204  G4cerr << "G4LatticeReader: Invalid map theta dimension " << fNX << G4endl;
205  return false;
206  }
207 
208  if (fNY < 0 || fNY >= G4LatticeLogical::MAXRES) {
209  G4cerr << "G4LatticeReader: Invalid map phi dimension " << fNY << G4endl;
210  return false;
211  }
212 
213  // Prepend path to data files to map filename
214  fMap = fMapPath + "/" + fMap;
215 
216  // Convert string code (L,ST,LT) to polarization index
217  fsPol.toLower();
218  fPol = ( (fsPol=="l") ? 0 : // Longitudinal
219  (fsPol=="st") ? 1 : // Slow-transverse
220  (fsPol=="ft") ? 2 : // Fast-transverse
221  -1 ); // Invalid code
222 
223  if (fPol<0 || fPol>2) {
224  G4cerr << "G4LatticeReader: Invalid polarization code " << fsPol << G4endl;
225  return false;
226  }
227 
228  return true;
229 }
230 
232  if (!ReadMapInfo()) { // Get specific parameters for map to load
233  G4cerr << "G4LatticeReader: Unable to process mapfile directive." << G4endl;
234  return false;
235  }
236 
237  return pLattice->LoadMap(fNX, fNY, fPol, fMap);
238 }
239 
241  if (!ReadMapInfo()) { // Get specific parameters for map to load
242  G4cerr << "G4LatticeReader: Unable to process mapfile directive." << G4endl;
243  return false;
244  }
245 
246  return pLattice->Load_NMap(fNX, fNY, fPol, fMap);
247 }