Geant4-11
Public Member Functions | Private Member Functions | Private Attributes
G4DataSet Class Reference

#include <G4DataSet.hh>

Inheritance diagram for G4DataSet:
G4IDataSet

Public Member Functions

virtual void AddComponent (G4IDataSet *)
 
virtual G4double FindValue (G4double x, G4int componentId=0) const
 
 G4DataSet (G4int argZ, G4DataVector *xData, G4DataVector *data, G4IInterpolator *algo, G4double xUnit=CLHEP::MeV, G4double yUnit=CLHEP::barn, G4bool random=false)
 
 G4DataSet (G4int argZ, G4IInterpolator *algo, G4double xUnit=CLHEP::MeV, G4double yUnit=CLHEP::barn, G4bool random=false)
 
virtual const G4IDataSetGetComponent (G4int) const
 
virtual const G4DataVectorGetData (G4int) const
 
virtual const G4DataVectorGetEnergies (G4int) const
 
virtual G4bool LoadData (const G4String &fileName)
 
virtual size_t NumberOfComponents (void) const
 
virtual void PrintData (void) const
 
virtual G4double RandomSelect (G4int componentId=0) const
 
virtual G4bool SaveData (const G4String &fileName) const
 
virtual void SetEnergiesData (G4DataVector *xData, G4DataVector *data, G4int componentId)
 
virtual ~G4DataSet ()
 

Private Member Functions

virtual void BuildPdf ()
 
size_t FindLowerBound (G4double energy) const
 
size_t FindLowerBound (G4double x, G4DataVector *values) const
 
G4String FullFileName (const G4String &fileName) const
 
 G4DataSet ()
 
 G4DataSet (const G4DataSet &copy)
 
G4double IntegrationFunction (G4double x)
 
G4DataSetoperator= (const G4DataSet &right)
 

Private Attributes

G4IInterpolatoralgorithm
 
G4DataVectordata
 
G4DataVectorenergies
 
G4DataVectorpdf
 
G4bool randomSet
 
G4double unitData
 
G4double unitEnergies
 
G4int z
 

Detailed Description

Definition at line 53 of file G4DataSet.hh.

Constructor & Destructor Documentation

◆ G4DataSet() [1/4]

G4DataSet::G4DataSet ( G4int  argZ,
G4IInterpolator algo,
G4double  xUnit = CLHEP::MeV,
G4double  yUnit = CLHEP::barn,
G4bool  random = false 
)

Definition at line 46 of file G4DataSet.cc.

50 :
51 z(Z),
52 energies(0),
53 data(0),
54 algorithm(algo),
55 unitEnergies(xUnit),
56 unitData(yUnit),
57 pdf(0),
58 randomSet(random)
59{
60 if (algorithm == 0) G4Exception("G4DataSet::G4DataSet",
61 "pii00000101",
63 "Interpolation == 0");
64 if (randomSet) BuildPdf();
65}
@ FatalException
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:35
const G4int Z[17]
G4DataVector * energies
Definition: G4DataSet.hh:110
virtual void BuildPdf()
Definition: G4DataSet.cc:352
G4double unitEnergies
Definition: G4DataSet.hh:115
G4DataVector * data
Definition: G4DataSet.hh:111
G4bool randomSet
Definition: G4DataSet.hh:119
G4int z
Definition: G4DataSet.hh:108
G4DataVector * pdf
Definition: G4DataSet.hh:118
G4IInterpolator * algorithm
Definition: G4DataSet.hh:113
G4double unitData
Definition: G4DataSet.hh:116

References algorithm, BuildPdf(), FatalException, G4Exception(), and randomSet.

◆ G4DataSet() [2/4]

G4DataSet::G4DataSet ( G4int  argZ,
G4DataVector xData,
G4DataVector data,
G4IInterpolator algo,
G4double  xUnit = CLHEP::MeV,
G4double  yUnit = CLHEP::barn,
G4bool  random = false 
)

Definition at line 67 of file G4DataSet.cc.

73 :
74 z(argZ),
75 energies(dataX),
76 data(dataY),
77 algorithm(algo),
78 unitEnergies(xUnit),
79 unitData(yUnit),
80 pdf(0),
81 randomSet(random)
82{
83 if (algorithm == 0) G4Exception("G4DataSet::G4DataSet",
84 "pii00000110",
86 "Interpolation == 0");
87
88 if ((energies == 0) ^ (data == 0))
89 G4Exception("G4DataSet::G4DataSet",
90 "pii00000111-",
92 "different size for energies and data (zero case)");
93
94 if (energies == 0) return;
95
96 if (energies->size() != data->size())
97 G4Exception("G4DataSet::G4DataSet",
98 "pii00000112",
100 "different size for energies and data");
101
102 if (randomSet) BuildPdf();
103}

References algorithm, BuildPdf(), data, energies, FatalException, G4Exception(), and randomSet.

◆ ~G4DataSet()

G4DataSet::~G4DataSet ( )
virtual

Definition at line 105 of file G4DataSet.cc.

106{
107 delete algorithm;
108 if (energies) delete energies;
109 if (data) delete data;
110 if (pdf) delete pdf;
111}

References algorithm, data, energies, and pdf.

◆ G4DataSet() [3/4]

G4DataSet::G4DataSet ( )
private

Referenced by BuildPdf().

◆ G4DataSet() [4/4]

G4DataSet::G4DataSet ( const G4DataSet copy)
private

Member Function Documentation

◆ AddComponent()

virtual void G4DataSet::AddComponent ( G4IDataSet )
inlinevirtual

Implements G4IDataSet.

Definition at line 78 of file G4DataSet.hh.

78{}

◆ BuildPdf()

void G4DataSet::BuildPdf ( )
privatevirtual

Definition at line 352 of file G4DataSet.cc.

353{
354 pdf = new G4DataVector;
356
357 G4int nData = data->size();
358 pdf->push_back(0.);
359
360 // Integrate the data distribution
361 G4int i;
362 G4double totalSum = 0.;
363 for (i=1; i<nData; i++)
364 {
365 G4double xLow = (*energies)[i-1];
366 G4double xHigh = (*energies)[i];
367 G4double sum = integrator.Legendre96(this, &G4DataSet::IntegrationFunction, xLow, xHigh);
368 totalSum = totalSum + sum;
369 pdf->push_back(totalSum);
370 }
371
372 // Normalize to the last bin
373 G4double tot = 0.;
374 if (totalSum > 0.) tot = 1. / totalSum;
375 for (i=1; i<nData; i++)
376 {
377 (*pdf)[i] = (*pdf)[i] * tot;
378 }
379}
double G4double
Definition: G4Types.hh:83
int G4int
Definition: G4Types.hh:85
G4double IntegrationFunction(G4double x)
Definition: G4DataSet.cc:410

References data, G4DataSet(), IntegrationFunction(), and pdf.

Referenced by G4DataSet(), and LoadData().

◆ FindLowerBound() [1/2]

size_t G4DataSet::FindLowerBound ( G4double  energy) const
private

Definition at line 302 of file G4DataSet.cc.

303{
304 size_t lowerBound = 0;
305 size_t upperBound(energies->size() - 1);
306
307 while (lowerBound <= upperBound)
308 {
309 size_t midBin((lowerBound + upperBound) / 2);
310
311 if (x < (*energies)[midBin]) upperBound = midBin - 1;
312 else lowerBound = midBin + 1;
313 }
314
315 return upperBound;
316}

References energies.

Referenced by FindValue(), IntegrationFunction(), and RandomSelect().

◆ FindLowerBound() [2/2]

size_t G4DataSet::FindLowerBound ( G4double  x,
G4DataVector values 
) const
private

Definition at line 319 of file G4DataSet.cc.

320{
321 size_t lowerBound = 0;;
322 size_t upperBound(values->size() - 1);
323
324 while (lowerBound <= upperBound)
325 {
326 size_t midBin((lowerBound + upperBound) / 2);
327
328 if (x < (*values)[midBin]) upperBound = midBin - 1;
329 else lowerBound = midBin + 1;
330 }
331
332 return upperBound;
333}

◆ FindValue()

G4double G4DataSet::FindValue ( G4double  x,
G4int  componentId = 0 
) const
virtual

Implements G4IDataSet.

Definition at line 113 of file G4DataSet.cc.

114{
115 if (!energies) G4Exception("G4DataSet::FindValue",
116 "pii00000120",
118 "energies == 0");
119 if (energies->empty()) return 0;
120 if (energy <= (*energies)[0]) return (*data)[0];
121
122 size_t i = energies->size()-1;
123 if (energy >= (*energies)[i]) return (*data)[i];
124
126 return interpolated;
127}
size_t FindLowerBound(G4double energy) const
Definition: G4DataSet.cc:302
virtual G4double Calculate(G4double point, G4int bin, const G4DataVector &energies, const G4DataVector &data) const =0
G4double energy(const ThreeVector &p, const G4double m)

References algorithm, G4IInterpolator::Calculate(), data, energies, G4INCL::KinematicsUtils::energy(), FatalException, FindLowerBound(), and G4Exception().

◆ FullFileName()

G4String G4DataSet::FullFileName ( const G4String fileName) const
private

Definition at line 336 of file G4DataSet.cc.

337{
338 char* path = std::getenv("G4PIIDATA");
339 if (!path)
340 G4Exception("G4DataSet::FullFileName",
341 "pii00000160",
343 "G4PIIDATA environment variable not set");
344
345 std::ostringstream fullFileName;
346 fullFileName << path << '/' << name << z << ".dat";
347
348 return G4String(fullFileName.str().c_str());
349}
const char * name(G4int ptype)

References FatalException, G4Exception(), G4InuclParticleNames::name(), and z.

Referenced by LoadData(), and SaveData().

◆ GetComponent()

virtual const G4IDataSet * G4DataSet::GetComponent ( G4int  ) const
inlinevirtual

Implements G4IDataSet.

Definition at line 76 of file G4DataSet.hh.

76{ return 0; }

◆ GetData()

virtual const G4DataVector & G4DataSet::GetData ( G4int  ) const
inlinevirtual

Implements G4IDataSet.

Definition at line 83 of file G4DataSet.hh.

83{ return *data; }

References data.

◆ GetEnergies()

virtual const G4DataVector & G4DataSet::GetEnergies ( G4int  ) const
inlinevirtual

Implements G4IDataSet.

Definition at line 82 of file G4DataSet.hh.

82{ return *energies; }

References energies.

◆ IntegrationFunction()

G4double G4DataSet::IntegrationFunction ( G4double  x)
private

Definition at line 410 of file G4DataSet.cc.

411{
412 // This function is needed by G4Integrator to calculate the integral of the data distribution
413
414 G4double y = 0;
415
416 // Locate the random value in the X vector based on the PDF
417 size_t bin = FindLowerBound(x);
418
419 // Interpolate to calculate the X value:
420 // linear interpolation in the first bin (to avoid problem with 0),
421 // interpolation with associated algorithm in other bins
422
423 G4LinInterpolation linearAlgo;
424
425 if (bin == 0) y = linearAlgo.Calculate(x, bin, *energies, *data);
426 else y = algorithm->Calculate(x, bin, *energies, *data);
427
428 return y;
429}
G4double Calculate(G4double point, G4int bin, const G4DataVector &energies, const G4DataVector &data) const override

References algorithm, G4IInterpolator::Calculate(), G4LinInterpolation::Calculate(), data, energies, and FindLowerBound().

Referenced by BuildPdf().

◆ LoadData()

G4bool G4DataSet::LoadData ( const G4String fileName)
virtual

Implements G4IDataSet.

Definition at line 175 of file G4DataSet.cc.

176{
177 // The file is organized into two columns:
178 // 1st column is the energy
179 // 2nd column is the corresponding value
180 // The file terminates with the pattern: -1 -1
181 // -2 -2
182
183 G4String fullFileName(FullFileName(fileName));
184 std::ifstream in(fullFileName);
185
186 if (!in.is_open())
187 {
188
189 std::ostringstream message;
190 message << "G4DataSet::LoadData - data file " << fullFileName << " not found";
191
192 G4Exception("G4CompositeDataSet::LoadData",
193 "pii00000140",
195 message.str().c_str());
196 }
197
198 G4DataVector* argEnergies=new G4DataVector;
199 G4DataVector* argData=new G4DataVector;
200
201 G4double a;
202 bool energyColumn(true);
203
204 do
205 {
206 in >> a;
207
208 if (a!=-1 && a!=-2)
209 {
210 if (energyColumn)
211 {
212 // std::cout << fullFileName << ", a = " << a <<std::endl;
213 argEnergies->push_back(a*unitEnergies);
214 }
215 else
216 argData->push_back(a*unitData);
217 energyColumn=(!energyColumn);
218 }
219 }
220 while (a != -2);
221
222 SetEnergiesData(argEnergies, argData, 0);
223 if (randomSet) BuildPdf();
224
225 return true;
226}
virtual void SetEnergiesData(G4DataVector *xData, G4DataVector *data, G4int componentId)
Definition: G4DataSet.cc:150
G4String FullFileName(const G4String &fileName) const
Definition: G4DataSet.cc:336

References BuildPdf(), FatalException, FullFileName(), G4Exception(), randomSet, SetEnergiesData(), unitData, and unitEnergies.

◆ NumberOfComponents()

virtual size_t G4DataSet::NumberOfComponents ( void  ) const
inlinevirtual

Implements G4IDataSet.

Definition at line 80 of file G4DataSet.hh.

80{ return 0; }

◆ operator=()

G4DataSet & G4DataSet::operator= ( const G4DataSet right)
private

◆ PrintData()

void G4DataSet::PrintData ( void  ) const
virtual

Implements G4IDataSet.

Definition at line 130 of file G4DataSet.cc.

131{
132 if (!energies)
133 {
134 G4cout << "Data not available." << G4endl;
135 }
136 else
137 {
138 size_t size = energies->size();
139 for (size_t i(0); i<size; i++)
140 {
141 G4cout << "Point: " << ((*energies)[i]/unitEnergies)
142 << " - Data value: " << ((*data)[i]/unitData);
143 if (pdf != 0) G4cout << " - PDF : " << (*pdf)[i];
144 G4cout << G4endl;
145 }
146 }
147}
#define G4endl
Definition: G4ios.hh:57
G4GLOB_DLL std::ostream G4cout

References data, energies, G4cout, G4endl, pdf, unitData, and unitEnergies.

◆ RandomSelect()

G4double G4DataSet::RandomSelect ( G4int  componentId = 0) const
virtual

Implements G4IDataSet.

Definition at line 382 of file G4DataSet.cc.

383{
384 // Random select a X value according to the cumulative probability distribution
385 // derived from the data
386
387 if (!pdf) G4Exception("G4DataSet::RandomSelect",
388 "pii00000170",
390 "PDF has not been created for this data set");
391
392 G4double value = 0.;
394
395 // Locate the random value in the X vector based on the PDF
396 size_t bin = FindLowerBound(x,pdf);
397
398 // Interpolate the PDF to calculate the X value:
399 // linear interpolation in the first bin (to avoid problem with 0),
400 // interpolation with associated data set algorithm in other bins
401
402 G4LinInterpolation linearAlgo;
403 if (bin == 0) value = linearAlgo.Calculate(x, bin, *pdf, *energies);
404 else value = algorithm->Calculate(x, bin, *pdf, *energies);
405
406 // G4cout << x << " random bin "<< bin << " - " << value << G4endl;
407 return value;
408}
#define G4UniformRand()
Definition: Randomize.hh:52

References algorithm, G4IInterpolator::Calculate(), G4LinInterpolation::Calculate(), energies, FatalException, FindLowerBound(), G4Exception(), G4UniformRand, and pdf.

◆ SaveData()

G4bool G4DataSet::SaveData ( const G4String fileName) const
virtual

Implements G4IDataSet.

Definition at line 228 of file G4DataSet.cc.

229{
230 // The file is organized into two columns:
231 // 1st column is the energy
232 // 2nd column is the corresponding value
233 // The file terminates with the pattern: -1 -1
234 // -2 -2
235
236 G4String fullFileName(FullFileName(name));
237 std::ofstream out(fullFileName);
238
239 if (!out.is_open())
240 {
241
242 std::ostringstream message;
243 message << "G4DataSet:: SaveData - cannot open " << fullFileName;
244
245 G4Exception("G4CompositeDataSet::SaveData",
246 "pii00000150",
248 message.str().c_str());
249
250 }
251
252 out.precision(10);
253 out.width(15);
254 out.setf(std::ofstream::left);
255
256 if (energies!=0 && data!=0)
257 {
258 G4DataVector::const_iterator i(energies->begin());
259 G4DataVector::const_iterator endI(energies->end());
260 G4DataVector::const_iterator j(data->begin());
261
262 while (i!=endI)
263 {
264 out.precision(10);
265 out.width(15);
266 out.setf(std::ofstream::left);
267 out << ((*i)/unitEnergies) << ' ';
268
269 out.precision(10);
270 out.width(15);
271 out.setf(std::ofstream::left);
272 out << ((*j)/unitData) << std::endl;
273
274 i++;
275 j++;
276 }
277 }
278
279 out.precision(10);
280 out.width(15);
281 out.setf(std::ofstream::left);
282 out << -1.f << ' ';
283
284 out.precision(10);
285 out.width(15);
286 out.setf(std::ofstream::left);
287 out << -1.f << std::endl;
288
289 out.precision(10);
290 out.width(15);
291 out.setf(std::ofstream::left);
292 out << -2.f << ' ';
293
294 out.precision(10);
295 out.width(15);
296 out.setf(std::ofstream::left);
297 out << -2.f << std::endl;
298
299 return true;
300}

References data, energies, FatalException, FullFileName(), G4Exception(), G4InuclParticleNames::name(), unitData, and unitEnergies.

◆ SetEnergiesData()

void G4DataSet::SetEnergiesData ( G4DataVector xData,
G4DataVector data,
G4int  componentId 
)
virtual

Implements G4IDataSet.

Definition at line 150 of file G4DataSet.cc.

153{
154 if (energies) delete energies;
155 energies = dataX;
156
157 if (data) delete data;
158 data = dataY;
159
160 if ((energies == 0) ^ (data==0))
161 G4Exception("G4DataSet::SetEnergiesData",
162 "pii00000130",
164 "different size for energies and data (zero case)");
165
166 if (energies == 0) return;
167
168 if (energies->size() != data->size())
169 G4Exception("G4DataSet::SetEnergiesData",
170 "pii00000131",
172 "different size for energies and data");
173}

References data, energies, FatalException, and G4Exception().

Referenced by LoadData().

Field Documentation

◆ algorithm

G4IInterpolator* G4DataSet::algorithm
private

Definition at line 113 of file G4DataSet.hh.

Referenced by FindValue(), G4DataSet(), IntegrationFunction(), RandomSelect(), and ~G4DataSet().

◆ data

G4DataVector* G4DataSet::data
private

◆ energies

G4DataVector* G4DataSet::energies
private

◆ pdf

G4DataVector* G4DataSet::pdf
private

Definition at line 118 of file G4DataSet.hh.

Referenced by BuildPdf(), PrintData(), RandomSelect(), and ~G4DataSet().

◆ randomSet

G4bool G4DataSet::randomSet
private

Definition at line 119 of file G4DataSet.hh.

Referenced by G4DataSet(), and LoadData().

◆ unitData

G4double G4DataSet::unitData
private

Definition at line 116 of file G4DataSet.hh.

Referenced by LoadData(), PrintData(), and SaveData().

◆ unitEnergies

G4double G4DataSet::unitEnergies
private

Definition at line 115 of file G4DataSet.hh.

Referenced by LoadData(), PrintData(), and SaveData().

◆ z

G4int G4DataSet::z
private

The documentation for this class was generated from the following files: