Geant4  10.03.p03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4DNACrossSectionDataSet.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 
28 // $Id: G4DNACrossSectionDataSet.cc 105160 2017-07-14 09:33:58Z gcosmo $
29 //
30 // Author: Riccardo Capra <capra@ge.infn.it>
31 // Code review by MGP October 2007: removed inheritance from concrete class
32 // more improvements needed
33 //
34 // History:
35 // -----------
36 // 30 Jun 2005 RC Created
37 // 14 Oct 2007 MGP Removed inheritance from concrete class G4ShellEMDataSet
38 //
39 // 15 Jul 2009 N.A.Karakatsanis
40 //
41 // - LoadNonLogData method was created to load only the non-logarithmic data from G4EMLOW
42 // dataset. It is essentially performing the data loading operations as in the past.
43 //
44 // - LoadData method was revised in order to calculate the logarithmic values of the data
45 // It retrieves the data values from the G4EMLOW data files but, then, calculates the
46 // respective log values and loads them to seperate data structures.
47 //
48 // - SetLogEnergiesData method was cretaed to set logarithmic values to G4 data vectors.
49 // The EM data sets, initialized this way, contain both non-log and log values.
50 // These initialized data sets can enhance the computing performance of data interpolation
51 // operations
52 //
53 //
54 // -------------------------------------------------------------------
55 
56 
58 #include "G4VDataSetAlgorithm.hh"
59 #include "G4EMDataSet.hh"
60 #include <vector>
61 #include <fstream>
62 #include <sstream>
63 
64 
66  G4double argUnitEnergies,
67  G4double argUnitData)
68  :
69  algorithm(argAlgorithm), unitEnergies(argUnitEnergies), unitData(argUnitData)
70 {
71  z = 0;
72 
73 }
74 
75 
77 {
78  CleanUpComponents();
79 
80  if (algorithm)
81  delete algorithm;
82 }
83 
85 {
86  CleanUpComponents();
87 
88  G4String fullFileName(FullFileName(argFileName));
89  std::ifstream in(fullFileName, std::ifstream::binary|std::ifstream::in);
90 
91  if (!in.is_open())
92  {
93  G4String message("Data file \"");
94  message+=fullFileName;
95  message+="\" not found";
96  G4Exception("G4DNACrossSectionDataSet::LoadData","em0003",
97  FatalException,message);
98  return false;
99  }
100 
101  std::vector<G4DataVector *> columns;
102  std::vector<G4DataVector *> log_columns;
103 
104  std::stringstream *stream(new std::stringstream);
105  char c;
106  G4bool comment(false);
107  G4bool space(true);
108  G4bool first(true);
109 
110  try
111  {
112  while (!in.eof())
113  {
114  in.get(c);
115 
116  switch (c)
117  {
118  case '\r':
119  case '\n':
120  if (!first)
121  {
122  unsigned long i(0);
123  G4double value;
124 
125  while (!stream->eof())
126  {
127  (*stream) >> value;
128 
129  while (i>=columns.size())
130  {
131  columns.push_back(new G4DataVector);
132  log_columns.push_back(new G4DataVector);
133  }
134 
135  columns[i]->push_back(value);
136 
137 // N. A. Karakatsanis
138 // A condition is applied to check if negative or zero values are present in the dataset.
139 // If yes, then a near-zero value is applied to allow the computation of the logarithmic value
140 // If a value is zero, this simplification is acceptable
141 // If a value is negative, then it is not acceptable and the data of the particular column of
142 // logarithmic values should not be used by interpolation methods.
143 //
144 // Therefore, G4LogLogInterpolation and G4LinLogLogInterpolation should not be used if negative values are present.
145 // Instead, G4LinInterpolation is safe in every case
146 // SemiLogInterpolation is safe only if the energy columns are non-negative
147 // G4LinLogInterpolation is safe only if the cross section data columns are non-negative
148 
149  if (value <=0.) value = 1e-300;
150  log_columns[i]->push_back(std::log10(value));
151 
152  i++;
153  }
154 
155  delete stream;
156  stream=new std::stringstream;
157  }
158 
159  first=true;
160  comment=false;
161  space=true;
162  break;
163 
164  case '#':
165  comment=true;
166  break;
167 
168  case '\t':
169  case ' ':
170  space = true;
171  break;
172 
173  default:
174  if (comment) { break; }
175  if (space && (!first)) { (*stream) << ' '; }
176 
177  first=false;
178  (*stream) << c;
179  space=false;
180  }
181  }
182  }
183  catch(const std::ios::failure &e)
184  {
185  // some implementations of STL could throw a "failture" exception
186  // when read wants read characters after end of file
187  }
188 
189  delete stream;
190 
191  std::vector<G4DataVector *>::size_type maxI(columns.size());
192 
193  if (maxI<2)
194  {
195  G4String message("Data file \"");
196  message+=fullFileName;
197  message+="\" should have at least two columns";
198  G4Exception("G4DNACrossSectionDataSet::LoadData","em0005",
199  FatalException,message);
200  return false;
201  }
202 
203  std::vector<G4DataVector*>::size_type i(1);
204  while (i<maxI)
205  {
206  G4DataVector::size_type maxJ(columns[i]->size());
207 
208  if (maxJ!=columns[0]->size())
209  {
210  G4String message("Data file \"");
211  message+=fullFileName;
212  message+="\" has lines with a different number of columns";
213  G4Exception("G4DNACrossSectionDataSet::LoadData","em0005",
214  FatalException,message);
215  return false;
216  }
217 
218  G4DataVector::size_type j(0);
219 
220  G4DataVector *argEnergies=new G4DataVector;
221  G4DataVector *argData=new G4DataVector;
222  G4DataVector *argLogEnergies=new G4DataVector;
223  G4DataVector *argLogData=new G4DataVector;
224 
225  while(j<maxJ)
226  {
227  argEnergies->push_back(columns[0]->operator[] (j)*GetUnitEnergies());
228  argData->push_back(columns[i]->operator[] (j)*GetUnitData());
229  argLogEnergies->push_back(log_columns[0]->operator[] (j) + std::log10(GetUnitEnergies()));
230  argLogData->push_back(log_columns[i]->operator[] (j) + std::log10(GetUnitData()));
231  j++;
232  }
233 
234  AddComponent(new G4EMDataSet(i-1, argEnergies, argData, argLogEnergies, argLogData, GetAlgorithm()->Clone(), GetUnitEnergies(), GetUnitData()));
235 
236  i++;
237  }
238 
239  i=maxI;
240  while (i>0)
241  {
242  i--;
243  delete columns[i];
244  delete log_columns[i];
245  }
246 
247  return true;
248 }
249 
250 
252 {
253  CleanUpComponents();
254 
255  G4String fullFileName(FullFileName(argFileName));
256  std::ifstream in(fullFileName, std::ifstream::binary|std::ifstream::in);
257 
258  if (!in.is_open())
259  {
260  G4String message("Data file \"");
261  message+=fullFileName;
262  message+="\" not found";
263  G4Exception("G4DNACrossSectionDataSet::LoadData","em0003",
264  FatalException,message);
265  return false;
266  }
267 
268  std::vector<G4DataVector *> columns;
269 
270  std::stringstream *stream(new std::stringstream);
271  char c;
272  G4bool comment(false);
273  G4bool space(true);
274  G4bool first(true);
275 
276  try
277  {
278  while (!in.eof())
279  {
280  in.get(c);
281 
282  switch (c)
283  {
284  case '\r':
285  case '\n':
286  if (!first)
287  {
288  unsigned long i(0);
289  G4double value;
290 
291  while (!stream->eof())
292  {
293  (*stream) >> value;
294 
295  while (i>=columns.size())
296  {
297  columns.push_back(new G4DataVector);
298  }
299 
300  columns[i]->push_back(value);
301 
302  i++;
303  }
304 
305  delete stream;
306  stream=new std::stringstream;
307  }
308 
309  first=true;
310  comment=false;
311  space=true;
312  break;
313 
314  case '#':
315  comment=true;
316  break;
317 
318  case '\t':
319  case ' ':
320  space = true;
321  break;
322 
323  default:
324  if (comment) { break; }
325  if (space && (!first)) { (*stream) << ' '; }
326 
327  first=false;
328  (*stream) << c;
329  space=false;
330  }
331  }
332  }
333  catch(const std::ios::failure &e)
334  {
335  // some implementations of STL could throw a "failture" exception
336  // when read wants read characters after end of file
337  }
338 
339  delete stream;
340 
341  std::vector<G4DataVector *>::size_type maxI(columns.size());
342 
343  if (maxI<2)
344  {
345  G4String message("Data file \"");
346  message+=fullFileName;
347  message+="\" should have at least two columns";
348  G4Exception("G4DNACrossSectionDataSet::LoadData","em0005",
349  FatalException,message);
350  return false;
351  }
352 
353  std::vector<G4DataVector*>::size_type i(1);
354  while (i<maxI)
355  {
356  G4DataVector::size_type maxJ(columns[i]->size());
357 
358  if (maxJ!=columns[0]->size())
359  {
360  G4String message("Data file \"");
361  message+=fullFileName;
362  message+="\" has lines with a different number of columns.";
363  G4Exception("G4DNACrossSectionDataSet::LoadData","em0005",
364  FatalException,message);
365  return false;
366  }
367 
368  G4DataVector::size_type j(0);
369 
370  G4DataVector *argEnergies=new G4DataVector;
371  G4DataVector *argData=new G4DataVector;
372 
373  while(j<maxJ)
374  {
375  argEnergies->push_back(columns[0]->operator[] (j)*GetUnitEnergies());
376  argData->push_back(columns[i]->operator[] (j)*GetUnitData());
377  j++;
378  }
379 
380  AddComponent(new G4EMDataSet(i-1, argEnergies, argData, GetAlgorithm()->Clone(), GetUnitEnergies(), GetUnitData()));
381 
382  i++;
383  }
384 
385  i=maxI;
386  while (i>0)
387  {
388  i--;
389  delete columns[i];
390  }
391 
392  return true;
393 }
394 
395 
397 {
398  const size_t n(NumberOfComponents());
399 
400  if (n==0)
401  {
402  G4Exception("G4DNACrossSectionDataSet::SaveData","em0005",
403  FatalException,"Expected at least one component");
404 
405  return false;
406  }
407 
408  G4String fullFileName(FullFileName(argFileName));
409  std::ofstream out(fullFileName);
410 
411  if (!out.is_open())
412  {
413  G4String message("Cannot open \"");
414  message+=fullFileName;
415  message+="\"";
416  G4Exception("G4DNACrossSectionDataSet::SaveData","em0005",
417  FatalException,message);
418  return false;
419  }
420 
421  G4DataVector::const_iterator iEnergies(GetComponent(0)->GetEnergies(0).begin());
422  G4DataVector::const_iterator iEnergiesEnd(GetComponent(0)->GetEnergies(0).end());
423  G4DataVector::const_iterator * iData(new G4DataVector::const_iterator[n]);
424 
425  size_t k(n);
426 
427  while (k>0)
428  {
429  k--;
430  iData[k]=GetComponent(k)->GetData(0).begin();
431  }
432 
433  while (iEnergies!=iEnergiesEnd)
434  {
435  out.precision(10);
436  out.width(15);
437  out.setf(std::ofstream::left);
438  out << ((*iEnergies)/GetUnitEnergies());
439 
440  k=0;
441 
442  while (k<n)
443  {
444  out << ' ';
445  out.precision(10);
446  out.width(15);
447  out.setf(std::ofstream::left);
448  out << ((*(iData[k]))/GetUnitData());
449 
450  iData[k]++;
451  k++;
452  }
453 
454  out << std::endl;
455 
456  iEnergies++;
457  }
458 
459  delete[] iData;
460 
461  return true;
462 }
463 
464 
465 G4String G4DNACrossSectionDataSet::FullFileName(const G4String& argFileName) const
466 {
467  char* path = getenv("G4LEDATA");
468  if (!path)
469  {
470  G4Exception("G4DNACrossSectionDataSet::FullFileName","em0006",
471  FatalException,"G4LEDATA environment variable not set.");
472 
473  return "";
474  }
475 
476  std::ostringstream fullFileName;
477 
478  fullFileName << path << "/" << argFileName << ".dat";
479 
480  return G4String(fullFileName.str().c_str());
481 }
482 
483 
484 G4double G4DNACrossSectionDataSet::FindValue(G4double argEnergy, G4int /* argComponentId */) const
485 {
486  // Returns the sum over the shells corresponding to e
487  G4double value = 0.;
488 
489  std::vector<G4VEMDataSet *>::const_iterator i(components.begin());
490  std::vector<G4VEMDataSet *>::const_iterator end(components.end());
491 
492  while (i!=end)
493  {
494  value+=(*i)->FindValue(argEnergy);
495  i++;
496  }
497 
498  return value;
499 }
500 
501 
503 {
504  const size_t n(NumberOfComponents());
505 
506  G4cout << "The data set has " << n << " components" << G4endl;
507  G4cout << G4endl;
508 
509  size_t i(0);
510 
511  while (i<n)
512  {
513  G4cout << "--- Component " << i << " ---" << G4endl;
514  GetComponent(i)->PrintData();
515  i++;
516  }
517 }
518 
519 
521  G4DataVector* argData,
522  G4int argComponentId)
523 {
524  G4VEMDataSet * component(components[argComponentId]);
525 
526  if (component)
527  {
528  component->SetEnergiesData(argEnergies, argData, 0);
529  return;
530  }
531 
532  std::ostringstream message;
533  message << "Component " << argComponentId << " not found";
534 
535  G4Exception("G4DNACrossSectionDataSet::SetEnergiesData","em0005",
536  FatalException,message.str().c_str());
537 
538 }
539 
540 
542  G4DataVector* argData,
543  G4DataVector* argLogEnergies,
544  G4DataVector* argLogData,
545  G4int argComponentId)
546 {
547  G4VEMDataSet * component(components[argComponentId]);
548 
549  if (component)
550  {
551  component->SetLogEnergiesData(argEnergies, argData, argLogEnergies, argLogData, 0);
552  return;
553  }
554 
555  std::ostringstream message;
556  message << "Component " << argComponentId << " not found";
557 
558  G4Exception("G4DNACrossSectionDataSet::SetLogEnergiesData","em0005",
559  FatalException,message.str().c_str());
560 
561 }
562 
563 
564 void G4DNACrossSectionDataSet::CleanUpComponents()
565 {
566  while (!components.empty())
567  {
568  if (components.back()) delete components.back();
569  components.pop_back();
570  }
571 }
572 
573 
virtual void SetEnergiesData(G4DataVector *x, G4DataVector *data, G4int component=0)=0
virtual void SetLogEnergiesData(G4DataVector *x, G4DataVector *values, G4DataVector *log_x, G4DataVector *log_values, G4int componentId)
virtual const G4VEMDataSet * GetComponent(G4int componentId) const
virtual const G4DataVector & GetData(G4int componentId) const =0
virtual G4bool LoadData(const G4String &argFileName)
virtual G4bool LoadNonLogData(const G4String &argFileName)
int G4int
Definition: G4Types.hh:78
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 G4double FindValue(G4double e, G4int componentId=0) const
virtual void SetLogEnergiesData(G4DataVector *x, G4DataVector *data, G4DataVector *Log_x, G4DataVector *Log_data, G4int component=0)=0
G4DNACrossSectionDataSet(G4VDataSetAlgorithm *algo, G4double xUnit=CLHEP::MeV, G4double dataUnit=CLHEP::barn)
virtual size_t NumberOfComponents(void) const
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
virtual void PrintData(void) const =0
virtual void SetEnergiesData(G4DataVector *x, G4DataVector *values, G4int componentId)
virtual const G4DataVector & GetEnergies(G4int componentId) const
virtual void PrintData(void) const
#define G4endl
Definition: G4ios.hh:61
double G4double
Definition: G4Types.hh:76
tuple c
Definition: test.py:13
virtual void AddComponent(G4VEMDataSet *dataSet)
virtual G4bool SaveData(const G4String &argFileName) const