Geant4  10.03.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4ShellEMDataSet.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 //
26 //
27 // $Id: G4ShellEMDataSet.cc 66241 2012-12-13 18:34:42Z gunter $
28 //
29 // Author: Maria Grazia Pia (Maria.Grazia.Pia@cern.ch)
30 //
31 // History:
32 // -----------
33 // 1 Aug 2001 MGP Created
34 //
35 // 09.10.01 V.Ivanchenko Add case z=0
36 //
37 // 9 Mar 2008 MGP Cleaned up unreadable code modified by former developer
38 // (Further clean-up needed)
39 //
40 // 15 Jul 2009 Nicolas A. Karakatsanis
41 //
42 // - LoadNonLogData method was created to load only the non-logarithmic data from G4EMLOW
43 // dataset. It is essentially performing the data loading operations as in the past.
44 //
45 // - LoadData method was revised in order to calculate the logarithmic values of the data
46 // It retrieves the data values from the G4EMLOW data files but, then, calculates the
47 // respective log values and loads them to seperate data structures.
48 //
49 // - SetLogEnergiesData method was cretaed to set logarithmic values to G4 data vectors.
50 // The EM data sets, initialized this way, contain both non-log and log values.
51 // These initialized data sets can enhance the computing performance of data interpolation
52 // operations
53 //
54 //
55 // -------------------------------------------------------------------
56 
57 #include "G4ShellEMDataSet.hh"
58 #include "G4EMDataSet.hh"
59 #include "G4VDataSetAlgorithm.hh"
60 #include <fstream>
61 #include <sstream>
62 
63 
65  G4double eUnit,
66  G4double dataUnit)
67  :
68  z(zeta),
69  algorithm(algo),
70  unitEnergies(eUnit),
71  unitData(dataUnit)
72 {
73  if (algorithm == 0) G4Exception("G4ShellEMDataSet::G4ShellEMDataSet()","em0007",FatalErrorInArgument, "Interpolation == 0");
74 }
75 
76 
78 {
80  if (algorithm) delete algorithm;
81 }
82 
83 
85 {
86  // Returns the sum over the shells corresponding to e
87  G4double value = 0.;
88 
89  std::vector<G4VEMDataSet *>::const_iterator i(components.begin());
90  std::vector<G4VEMDataSet *>::const_iterator end(components.end());
91 
92  while (i != end)
93  {
94  value += (*i)->FindValue(energy);
95  i++;
96  }
97 
98  return value;
99 }
100 
101 
103 {
104  const size_t n = NumberOfComponents();
105 
106  G4cout << "The data set has " << n << " components" << G4endl;
107  G4cout << G4endl;
108 
109  size_t i = 0;
110 
111  while (i < n)
112  {
113  G4cout << "--- Component " << i << " ---" << G4endl;
114  GetComponent(i)->PrintData();
115  i++;
116  }
117 }
118 
119 
121  G4DataVector* data,
122  G4int componentId)
123 {
124  G4VEMDataSet* component = components[componentId];
125 
126  if (component)
127  {
128  component->SetEnergiesData(energies, data, 0);
129  return;
130  }
131 
132  G4String msg = "component " + (G4String)componentId + " not found";
133 
134  G4Exception("G4ShellEMDataSet::SetEnergiesData()","em0008", FatalErrorInArgument ,msg);
135 }
136 
137 
140  G4DataVector* log_energies,
141  G4DataVector* log_data,
142  G4int componentId)
143 {
144  G4VEMDataSet* component = components[componentId];
145 
146  if (component)
147  {
148  component->SetLogEnergiesData(energies, data, log_energies, log_data, 0);
149  return;
150  }
151 
152  G4String msg = "component " + (G4String)componentId + " not found";
153 
154  G4Exception("G4ShellEMDataSet::SetLogEnergiesData()","em0008", FatalErrorInArgument ,msg);
155 
156 }
157 
158 
159 
161 {
163 
164  G4String fullFileName = FullFileName(file);
165  std::ifstream in(fullFileName);
166 
167  if (!in.is_open())
168  {
169  G4String message("Data file \"");
170  message += fullFileName;
171  message += "\" not found";
172  G4Exception("G4ShellEMDataSet::LoadData()", "em0003",FatalException, message);
173  return 0;
174  }
175 
176  G4DataVector* orig_shell_energies = 0;
177  G4DataVector* orig_shell_data = 0;
178  G4DataVector* log_shell_energies = 0;
179  G4DataVector* log_shell_data = 0;
180 
181  G4double a = 0.;
182  G4int shellIndex = 0;
183  G4int k = 0;
184  G4int nColumns = 2;
185 
186  do
187  {
188  in >> a;
189 
190  if (a==0.) a=1e-300;
191 
192  // The file is organized into four columns:
193  // 1st column contains the values of energy
194  // 2nd column contains the corresponding data value
195  // The file terminates with the pattern: -1 -1
196  // -2 -2
197  //
198  if (a == -1)
199  {
200  if ((k%nColumns == 0) && (orig_shell_energies != 0) )
201  {
202  AddComponent(new G4EMDataSet(shellIndex, orig_shell_energies, orig_shell_data, log_shell_energies, log_shell_data, algorithm->Clone(), unitEnergies, unitData));
203  orig_shell_energies = 0;
204  orig_shell_data = 0;
205  log_shell_energies = 0;
206  log_shell_data = 0;
207  }
208  }
209  else if (a != -2)
210  {
211  if (orig_shell_energies == 0)
212  {
213  orig_shell_energies = new G4DataVector;
214  orig_shell_data = new G4DataVector;
215  log_shell_energies = new G4DataVector;
216  log_shell_data = new G4DataVector;
217  }
218  if (k%nColumns == 0)
219  {
220  orig_shell_energies->push_back(a*unitEnergies);
221  log_shell_energies->push_back(std::log10(a) + std::log10(unitEnergies));
222  }
223  else if (k%nColumns == 1)
224  {
225  orig_shell_data->push_back(a*unitData);
226  log_shell_data->push_back(std::log10(a) + std::log10(unitData));
227  }
228  k++;
229  }
230  else k = 1;
231  }
232  while (a != -2); // End of file
233 
234 
235  delete orig_shell_energies;
236  delete orig_shell_data;
237  delete log_shell_energies;
238  delete log_shell_data;
239 
240  return true;
241 }
242 
243 
245 {
247 
248  G4String fullFileName = FullFileName(file);
249  std::ifstream in(fullFileName);
250 
251  if (!in.is_open())
252  {
253  G4String message("G4ShellEMDataSet::LoadData - data file \"");
254  message += fullFileName;
255  message += "\" not found";
256  G4Exception("G4ShellEMDataSet::LoadNonLogData()", "em0003",FatalException, message);
257  return 0;
258  }
259 
260  G4DataVector* orig_shell_energies = 0;
261  G4DataVector* orig_shell_data = 0;
262 
263  G4double a = 0.;
264  G4int shellIndex = 0;
265  G4int k = 0;
266  G4int nColumns = 2;
267 
268  do
269  {
270  in >> a;
271 
272  // The file is organized into four columns:
273  // 1st column contains the values of energy
274  // 2nd column contains the corresponding data value
275  // The file terminates with the pattern: -1 -1
276  // -2 -2
277  //
278  if (a == -1)
279  {
280  if ((k%nColumns == 0) && (orig_shell_energies != 0) )
281  {
282  AddComponent(new G4EMDataSet(shellIndex, orig_shell_energies, orig_shell_data, algorithm->Clone(), unitEnergies, unitData));
283  orig_shell_energies = 0;
284  orig_shell_data = 0;
285  }
286  }
287  else if (a != -2)
288  {
289  if (orig_shell_energies == 0)
290  {
291  orig_shell_energies = new G4DataVector;
292  orig_shell_data = new G4DataVector;
293  }
294  if (k%nColumns == 0)
295  {
296  orig_shell_energies->push_back(a*unitEnergies);
297  }
298  else if (k%nColumns == 1)
299  {
300  orig_shell_data->push_back(a*unitData);
301  }
302  k++;
303  }
304  else k = 1;
305  }
306  while (a != -2); // End of file
307 
308 
309  delete orig_shell_energies;
310  delete orig_shell_data;
311 
312  return true;
313 }
314 
315 
316 
318 {
319  G4String fullFileName = FullFileName(file);
320  std::ofstream out(fullFileName);
321 
322  if (!out.is_open())
323  {
324  G4String message("Cannot open \"");
325  message += fullFileName;
326  message += "\"";
327  G4Exception("G4EMDataSet::SaveData()","em0005",FatalException,message);
328  }
329 
330  const size_t n = NumberOfComponents();
331  size_t k = 0;
332 
333  while (k < n)
334  {
335  const G4VEMDataSet* component = GetComponent(k);
336 
337  if (component)
338  {
339  const G4DataVector& energies = component->GetEnergies(0);
340  const G4DataVector& data = component->GetData(0);
341 
342  G4DataVector::const_iterator i = energies.begin();
343  G4DataVector::const_iterator endI = energies.end();
344  G4DataVector::const_iterator j = data.begin();
345 
346  while (i != endI)
347  {
348  out.precision(10);
349  out.width(15);
350  out.setf(std::ofstream::left);
351  out << ((*i)/unitEnergies) << ' ';
352 
353  out.precision(10);
354  out.width(15);
355  out.setf(std::ofstream::left);
356  out << ((*j)/unitData) << std::endl;
357  i++;
358  j++;
359  }
360  }
361 
362  out.precision(10);
363  out.width(15);
364  out.setf(std::ofstream::left);
365  out << -1.f << ' ';
366 
367  out.precision(10);
368  out.width(15);
369  out.setf(std::ofstream::left);
370  out << -1.f << std::endl;
371 
372  k++;
373  }
374 
375  out.precision(10);
376  out.width(15);
377  out.setf(std::ofstream::left);
378  out << -2.f << ' ';
379 
380  out.precision(10);
381  out.width(15);
382  out.setf(std::ofstream::left);
383  out << -2.f << std::endl;
384 
385  return true;
386 }
387 
388 
390 {
391  while (!components.empty())
392  {
393  if (components.back()) delete components.back();
394  components.pop_back();
395  }
396 }
397 
398 
399 G4String G4ShellEMDataSet::FullFileName(const G4String& fileName) const
400 {
401  char* path = getenv("G4LEDATA");
402 
403  if (!path)
404  {
405  G4Exception("G4ShellEMDataSet::FullFileName()","em0006",JustWarning,"Please set G4LEDATA");
406  return "";
407  }
408 
409  std::ostringstream fullFileName;
410 
411  fullFileName << path << '/' << fileName << z << ".dat";
412 
413  return G4String(fullFileName.str().c_str());
414 }
virtual const G4DataVector & GetEnergies(G4int componentId) const =0
void CleanUpComponents(void)
virtual void SetEnergiesData(G4DataVector *x, G4DataVector *data, G4int component=0)=0
virtual size_t NumberOfComponents(void) const
virtual G4bool LoadData(const G4String &fileName)
std::vector< ExP01TrackerHit * > a
Definition: ExP01Classes.hh:33
virtual const G4DataVector & GetData(G4int componentId) const =0
G4ShellEMDataSet(G4int Z, G4VDataSetAlgorithm *algo, G4double eUnit=CLHEP::MeV, G4double dataUnit=CLHEP::barn)
virtual const G4VEMDataSet * GetComponent(G4int componentId) const
virtual void SetEnergiesData(G4DataVector *energies, G4DataVector *data, G4int componentId)
virtual void PrintData(void) const
virtual G4double FindValue(G4double energy, G4int componentId=0) const
int G4int
Definition: G4Types.hh:78
virtual G4VDataSetAlgorithm * Clone() const =0
const XML_Char const XML_Char * data
Definition: expat.h:268
virtual void SetLogEnergiesData(G4DataVector *energies, G4DataVector *data, G4DataVector *log_energies, G4DataVector *log_data, G4int componentId)
G4GLOB_DLL std::ostream G4cout
const XML_Char int const XML_Char * value
Definition: expat.h:331
bool G4bool
Definition: G4Types.hh:79
const G4int n
virtual void SetLogEnergiesData(G4DataVector *x, G4DataVector *data, G4DataVector *Log_x, G4DataVector *Log_data, G4int component=0)=0
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
virtual void PrintData(void) const =0
G4double energy(const ThreeVector &p, const G4double m)
virtual G4bool SaveData(const G4String &fileName) const
virtual ~G4ShellEMDataSet()
tuple z
Definition: test.py:28
#define G4endl
Definition: G4ios.hh:61
virtual G4bool LoadNonLogData(const G4String &fileName)
double G4double
Definition: G4Types.hh:76
virtual void AddComponent(G4VEMDataSet *dataSet)