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