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 | // $Id: G4ShellEMDataSet.cc,v 1.16 2008/03/10 15:07:41 pia Exp $ |
---|
28 | // GEANT4 tag $Name: geant4-09-02-ref-02 $ |
---|
29 | // |
---|
30 | // Author: Maria Grazia Pia (Maria.Grazia.Pia@cern.ch) |
---|
31 | // |
---|
32 | // History: |
---|
33 | // ----------- |
---|
34 | // 1 Aug 2001 MGP Created |
---|
35 | // 09.10.01 V.Ivanchenko Add case z=0 |
---|
36 | // 9 Mar 2008 MGP Cleaned up unreadable code modified by former developer |
---|
37 | // (Further clean-up needed) |
---|
38 | // |
---|
39 | // ------------------------------------------------------------------- |
---|
40 | |
---|
41 | #include "G4ShellEMDataSet.hh" |
---|
42 | #include "G4EMDataSet.hh" |
---|
43 | #include "G4VDataSetAlgorithm.hh" |
---|
44 | #include <fstream> |
---|
45 | #include <sstream> |
---|
46 | |
---|
47 | |
---|
48 | G4ShellEMDataSet::G4ShellEMDataSet(G4int zeta, G4VDataSetAlgorithm* algo, |
---|
49 | G4double eUnit, |
---|
50 | G4double dataUnit) |
---|
51 | : |
---|
52 | z(zeta), |
---|
53 | algorithm(algo), |
---|
54 | unitEnergies(eUnit), |
---|
55 | unitData(dataUnit) |
---|
56 | { |
---|
57 | if (algorithm == 0) G4Exception("G4ShellEMDataSet::G4ShellEMDataSet - interpolation == 0"); |
---|
58 | } |
---|
59 | |
---|
60 | |
---|
61 | G4ShellEMDataSet::~G4ShellEMDataSet() |
---|
62 | { |
---|
63 | CleanUpComponents(); |
---|
64 | if (algorithm) delete algorithm; |
---|
65 | } |
---|
66 | |
---|
67 | |
---|
68 | G4double G4ShellEMDataSet::FindValue(G4double energy, G4int /* componentId */) const |
---|
69 | { |
---|
70 | // Returns the sum over the shells corresponding to e |
---|
71 | G4double value = 0.; |
---|
72 | |
---|
73 | std::vector<G4VEMDataSet *>::const_iterator i(components.begin()); |
---|
74 | std::vector<G4VEMDataSet *>::const_iterator end(components.end()); |
---|
75 | |
---|
76 | while (i != end) |
---|
77 | { |
---|
78 | value += (*i)->FindValue(energy); |
---|
79 | i++; |
---|
80 | } |
---|
81 | |
---|
82 | return value; |
---|
83 | } |
---|
84 | |
---|
85 | |
---|
86 | void G4ShellEMDataSet::PrintData(void) const |
---|
87 | { |
---|
88 | const size_t n = NumberOfComponents(); |
---|
89 | |
---|
90 | G4cout << "The data set has " << n << " components" << G4endl; |
---|
91 | G4cout << G4endl; |
---|
92 | |
---|
93 | size_t i = 0; |
---|
94 | |
---|
95 | while (i < n) |
---|
96 | { |
---|
97 | G4cout << "--- Component " << i << " ---" << G4endl; |
---|
98 | GetComponent(i)->PrintData(); |
---|
99 | i++; |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | |
---|
104 | void G4ShellEMDataSet::SetEnergiesData(G4DataVector* energies, |
---|
105 | G4DataVector* data, |
---|
106 | G4int componentId) |
---|
107 | { |
---|
108 | G4VEMDataSet* component = components[componentId]; |
---|
109 | |
---|
110 | if (component) |
---|
111 | { |
---|
112 | component->SetEnergiesData(energies, data, 0); |
---|
113 | return; |
---|
114 | } |
---|
115 | |
---|
116 | std::ostringstream message; |
---|
117 | message << "G4ShellEMDataSet::SetEnergiesData - component " << componentId << " not found"; |
---|
118 | |
---|
119 | G4Exception(message.str().c_str()); |
---|
120 | } |
---|
121 | |
---|
122 | |
---|
123 | G4bool G4ShellEMDataSet::LoadData(const G4String& file) |
---|
124 | { |
---|
125 | CleanUpComponents(); |
---|
126 | |
---|
127 | G4String fullFileName = FullFileName(file); |
---|
128 | std::ifstream in(fullFileName); |
---|
129 | |
---|
130 | if (!in.is_open()) |
---|
131 | { |
---|
132 | G4String message("G4ShellEMDataSet::LoadData - data file \""); |
---|
133 | message += fullFileName; |
---|
134 | message += "\" not found"; |
---|
135 | G4Exception(message); |
---|
136 | } |
---|
137 | |
---|
138 | G4DataVector* energies = 0; |
---|
139 | G4DataVector* data = 0; |
---|
140 | |
---|
141 | G4double a = 0.; |
---|
142 | G4int shellIndex = 0; |
---|
143 | bool energyColumn = true; |
---|
144 | |
---|
145 | do |
---|
146 | { |
---|
147 | in >> a; |
---|
148 | |
---|
149 | if (a == -1) |
---|
150 | { |
---|
151 | if (energyColumn && energies!=0) |
---|
152 | { |
---|
153 | AddComponent(new G4EMDataSet(shellIndex, energies, data, algorithm->Clone(), unitEnergies, unitData)); |
---|
154 | energies = 0; |
---|
155 | data = 0; |
---|
156 | } |
---|
157 | |
---|
158 | energyColumn = (!energyColumn); |
---|
159 | } |
---|
160 | else if (a != -2) |
---|
161 | { |
---|
162 | if (energies == 0) |
---|
163 | { |
---|
164 | energies = new G4DataVector; |
---|
165 | data = new G4DataVector; |
---|
166 | } |
---|
167 | |
---|
168 | if (energyColumn) |
---|
169 | energies->push_back(a * unitEnergies); |
---|
170 | else |
---|
171 | data->push_back(a * unitData); |
---|
172 | |
---|
173 | energyColumn = (!energyColumn); |
---|
174 | } |
---|
175 | } |
---|
176 | while (a != -2); |
---|
177 | |
---|
178 | return true; |
---|
179 | } |
---|
180 | |
---|
181 | |
---|
182 | G4bool G4ShellEMDataSet::SaveData(const G4String& file) const |
---|
183 | { |
---|
184 | G4String fullFileName = FullFileName(file); |
---|
185 | std::ofstream out(fullFileName); |
---|
186 | |
---|
187 | if (!out.is_open()) |
---|
188 | { |
---|
189 | G4String message("G4EMDataSet::SaveData - cannot open \""); |
---|
190 | message += fullFileName; |
---|
191 | message += "\""; |
---|
192 | G4Exception(message); |
---|
193 | } |
---|
194 | |
---|
195 | const size_t n = NumberOfComponents(); |
---|
196 | size_t k = 0; |
---|
197 | |
---|
198 | while (k < n) |
---|
199 | { |
---|
200 | const G4VEMDataSet* component = GetComponent(k); |
---|
201 | |
---|
202 | if (component) |
---|
203 | { |
---|
204 | const G4DataVector& energies = component->GetEnergies(0); |
---|
205 | const G4DataVector& data = component->GetData(0); |
---|
206 | |
---|
207 | G4DataVector::const_iterator i = energies.begin(); |
---|
208 | G4DataVector::const_iterator endI = energies.end(); |
---|
209 | G4DataVector::const_iterator j = data.begin(); |
---|
210 | |
---|
211 | while (i != endI) |
---|
212 | { |
---|
213 | out.precision(10); |
---|
214 | out.width(15); |
---|
215 | out.setf(std::ofstream::left); |
---|
216 | out << ((*i)/unitEnergies) << ' '; |
---|
217 | |
---|
218 | out.precision(10); |
---|
219 | out.width(15); |
---|
220 | out.setf(std::ofstream::left); |
---|
221 | out << ((*j)/unitData) << std::endl; |
---|
222 | i++; |
---|
223 | j++; |
---|
224 | } |
---|
225 | } |
---|
226 | |
---|
227 | out.precision(10); |
---|
228 | out.width(15); |
---|
229 | out.setf(std::ofstream::left); |
---|
230 | out << -1.f << ' '; |
---|
231 | |
---|
232 | out.precision(10); |
---|
233 | out.width(15); |
---|
234 | out.setf(std::ofstream::left); |
---|
235 | out << -1.f << std::endl; |
---|
236 | |
---|
237 | k++; |
---|
238 | } |
---|
239 | |
---|
240 | out.precision(10); |
---|
241 | out.width(15); |
---|
242 | out.setf(std::ofstream::left); |
---|
243 | out << -2.f << ' '; |
---|
244 | |
---|
245 | out.precision(10); |
---|
246 | out.width(15); |
---|
247 | out.setf(std::ofstream::left); |
---|
248 | out << -2.f << std::endl; |
---|
249 | |
---|
250 | return true; |
---|
251 | } |
---|
252 | |
---|
253 | |
---|
254 | void G4ShellEMDataSet::CleanUpComponents(void) |
---|
255 | { |
---|
256 | while (!components.empty()) |
---|
257 | { |
---|
258 | if (components.back()) delete components.back(); |
---|
259 | components.pop_back(); |
---|
260 | } |
---|
261 | } |
---|
262 | |
---|
263 | |
---|
264 | G4String G4ShellEMDataSet::FullFileName(const G4String& fileName) const |
---|
265 | { |
---|
266 | char* path = getenv("G4LEDATA"); |
---|
267 | if (!path) |
---|
268 | G4Exception("G4ShellEMDataSet::FullFileName - G4LEDATA environment variable not set"); |
---|
269 | |
---|
270 | std::ostringstream fullFileName; |
---|
271 | |
---|
272 | fullFileName << path << '/' << fileName << z << ".dat"; |
---|
273 | |
---|
274 | return G4String(fullFileName.str().c_str()); |
---|
275 | } |
---|