Import Geant4 9.2.0 source tree
This commit is contained in:
@@ -24,8 +24,8 @@
|
||||
// ********************************************************************
|
||||
//
|
||||
//
|
||||
// $Id: G4EMDataSet.cc,v 1.12 2006/06/29 19:39:44 gunter Exp $
|
||||
// GEANT4 tag $Name: geant4-09-01 $
|
||||
// $Id: G4EMDataSet.cc,v 1.18 2008/03/17 13:40:53 pia Exp $
|
||||
// GEANT4 tag $Name: geant4-09-02 $
|
||||
//
|
||||
// Author: Maria Grazia Pia (Maria.Grazia.Pia@cern.ch)
|
||||
//
|
||||
@@ -39,278 +39,353 @@
|
||||
#include "G4VDataSetAlgorithm.hh"
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include "G4Integrator.hh"
|
||||
#include "Randomize.hh"
|
||||
#include "G4LinInterpolation.hh"
|
||||
|
||||
G4EMDataSet :: G4EMDataSet(G4int argZ, G4VDataSetAlgorithm * argAlgorithm, G4double argUnitEnergies, G4double argUnitData)
|
||||
:
|
||||
z(argZ),
|
||||
energies(0),
|
||||
data(0),
|
||||
algorithm(argAlgorithm),
|
||||
unitEnergies(argUnitEnergies),
|
||||
unitData(argUnitData)
|
||||
|
||||
G4EMDataSet::G4EMDataSet(G4int Z,
|
||||
G4VDataSetAlgorithm* algo,
|
||||
G4double xUnit,
|
||||
G4double yUnit,
|
||||
G4bool random):
|
||||
z(Z),
|
||||
energies(0),
|
||||
data(0),
|
||||
algorithm(algo),
|
||||
unitEnergies(xUnit),
|
||||
unitData(yUnit),
|
||||
pdf(0),
|
||||
randomSet(random)
|
||||
{
|
||||
if (algorithm == 0)
|
||||
G4Exception("G4EMDataSet::G4EMDataSet - interpolation == 0");
|
||||
if (algorithm == 0) G4Exception("G4EMDataSet::G4EMDataSet - interpolation == 0");
|
||||
if (randomSet) BuildPdf();
|
||||
}
|
||||
|
||||
|
||||
|
||||
G4EMDataSet :: G4EMDataSet(G4int argZ, G4DataVector * argEnergies, G4DataVector * argData, G4VDataSetAlgorithm * argAlgorithm, G4double argUnitEnergies, G4double argUnitData)
|
||||
:
|
||||
z(argZ),
|
||||
energies(argEnergies),
|
||||
data(argData),
|
||||
algorithm(argAlgorithm),
|
||||
unitEnergies(argUnitEnergies),
|
||||
unitData(argUnitData)
|
||||
G4EMDataSet::G4EMDataSet(G4int argZ,
|
||||
G4DataVector* dataX,
|
||||
G4DataVector* dataY,
|
||||
G4VDataSetAlgorithm* algo,
|
||||
G4double xUnit,
|
||||
G4double yUnit,
|
||||
G4bool random):
|
||||
z(argZ),
|
||||
energies(dataX),
|
||||
data(dataY),
|
||||
algorithm(algo),
|
||||
unitEnergies(xUnit),
|
||||
unitData(yUnit),
|
||||
pdf(0),
|
||||
randomSet(random)
|
||||
{
|
||||
if (algorithm==0)
|
||||
G4Exception("G4EMDataSet::G4EMDataSet - interpolation == 0");
|
||||
if (algorithm == 0) G4Exception("G4EMDataSet::G4EMDataSet - interpolation == 0");
|
||||
|
||||
if ((energies==0) ^ (data==0))
|
||||
G4Exception("G4EMDataSet::G4EMDataSet - different size for energies and data (zero case)");
|
||||
if ((energies == 0) ^ (data == 0))
|
||||
G4Exception("G4EMDataSet::G4EMDataSet - different size for energies and data (zero case)");
|
||||
|
||||
if (energies==0)
|
||||
return;
|
||||
if (energies == 0) return;
|
||||
|
||||
if (energies->size()!=data->size())
|
||||
G4Exception("G4EMDataSet::G4EMDataSet - different size for energies and data");
|
||||
if (energies->size() != data->size())
|
||||
G4Exception("G4EMDataSet::G4EMDataSet - different size for energies and data");
|
||||
|
||||
if (randomSet) BuildPdf();
|
||||
}
|
||||
|
||||
|
||||
|
||||
G4EMDataSet :: ~G4EMDataSet()
|
||||
G4EMDataSet::~G4EMDataSet()
|
||||
{
|
||||
delete algorithm;
|
||||
|
||||
if (energies)
|
||||
delete energies;
|
||||
delete algorithm;
|
||||
if (energies) delete energies;
|
||||
if (data) delete data;
|
||||
if (pdf) delete pdf;
|
||||
}
|
||||
|
||||
if (data)
|
||||
delete data;
|
||||
G4double G4EMDataSet::FindValue(G4double energy, G4int /* componentId */) const
|
||||
{
|
||||
if (!energies) G4Exception("G4EMDataSet::FindValue - energies == 0");
|
||||
if (energies->empty()) return 0;
|
||||
if (energy <= (*energies)[0]) return (*data)[0];
|
||||
|
||||
size_t i = energies->size()-1;
|
||||
if (energy >= (*energies)[i]) return (*data)[i];
|
||||
|
||||
return algorithm->Calculate(energy, FindLowerBound(energy), *energies, *data);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
G4double G4EMDataSet :: FindValue(G4double argEnergy, G4int /* argComponentId */) const
|
||||
void G4EMDataSet::PrintData(void) const
|
||||
{
|
||||
if (!energies)
|
||||
G4Exception("G4EMDataSet::FindValue - energies == 0");
|
||||
|
||||
if (energies->empty())
|
||||
return 0;
|
||||
|
||||
if (argEnergy <= (*energies)[0])
|
||||
return (*data)[0];
|
||||
|
||||
size_t i(energies->size()-1);
|
||||
|
||||
if (argEnergy >= (*energies)[i])
|
||||
return (*data)[i];
|
||||
|
||||
return algorithm->Calculate(argEnergy, FindLowerBound(argEnergy), *energies, *data);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void G4EMDataSet :: PrintData(void) const
|
||||
{
|
||||
if (!energies)
|
||||
{
|
||||
G4cout << "Data not available." << G4endl;
|
||||
return;
|
||||
}
|
||||
|
||||
size_t size = energies->size();
|
||||
|
||||
for (size_t i(0); i<size; i++)
|
||||
G4cout << "Point: " << ((*energies)[i]/unitEnergies)
|
||||
<< " - Data value : " << ((*data)[i]/unitData) << G4endl;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void G4EMDataSet :: SetEnergiesData(G4DataVector * argEnergies, G4DataVector * argData, G4int /* argComponentId */)
|
||||
{
|
||||
if (energies)
|
||||
delete energies;
|
||||
energies=argEnergies;
|
||||
|
||||
if (data)
|
||||
delete data;
|
||||
data=argData;
|
||||
|
||||
if ((energies==0) ^ (data==0))
|
||||
G4Exception("G4EMDataSet::SetEnergiesData - different size for energies and data (zero case)");
|
||||
|
||||
if (energies==0)
|
||||
return;
|
||||
|
||||
if (energies->size()!=data->size())
|
||||
G4Exception("G4EMDataSet::SetEnergiesData - different size for energies and data");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
G4bool G4EMDataSet :: LoadData(const G4String & argFileName)
|
||||
{
|
||||
// The file is organized into two columns:
|
||||
// 1st column is the energy
|
||||
// 2nd column is the corresponding value
|
||||
// The file terminates with the pattern: -1 -1
|
||||
// -2 -2
|
||||
|
||||
G4String fullFileName(FullFileName(argFileName));
|
||||
std::ifstream in(fullFileName);
|
||||
|
||||
if (!in.is_open())
|
||||
{
|
||||
G4String message("G4EMDataSet::LoadData - data file \"");
|
||||
message+=fullFileName;
|
||||
message+="\" not found";
|
||||
G4Exception(message);
|
||||
}
|
||||
|
||||
G4DataVector * argEnergies=new G4DataVector;
|
||||
G4DataVector * argData=new G4DataVector;
|
||||
|
||||
G4double a;
|
||||
bool energyColumn(true);
|
||||
|
||||
do
|
||||
{
|
||||
in >> a;
|
||||
|
||||
if (a!=-1 && a!=-2)
|
||||
{
|
||||
if (energyColumn)
|
||||
argEnergies->push_back(a*unitEnergies);
|
||||
else
|
||||
argData->push_back(a*unitData);
|
||||
energyColumn=(!energyColumn);
|
||||
}
|
||||
}
|
||||
while (a != -2);
|
||||
|
||||
SetEnergiesData(argEnergies, argData, 0);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
G4bool G4EMDataSet :: SaveData(const G4String & argFileName) const
|
||||
{
|
||||
// The file is organized into two columns:
|
||||
// 1st column is the energy
|
||||
// 2nd column is the corresponding value
|
||||
// The file terminates with the pattern: -1 -1
|
||||
// -2 -2
|
||||
|
||||
G4String fullFileName(FullFileName(argFileName));
|
||||
std::ofstream out(fullFileName);
|
||||
|
||||
if (!out.is_open())
|
||||
{
|
||||
G4String message("G4EMDataSet::SaveData - cannot open \"");
|
||||
message+=fullFileName;
|
||||
message+="\"";
|
||||
G4Exception(message);
|
||||
}
|
||||
|
||||
out.precision(10);
|
||||
out.width(15);
|
||||
out.setf(std::ofstream::left);
|
||||
|
||||
if (energies!=0 && data!=0)
|
||||
{
|
||||
G4DataVector::const_iterator i(energies->begin());
|
||||
G4DataVector::const_iterator endI(energies->end());
|
||||
G4DataVector::const_iterator j(data->begin());
|
||||
|
||||
while (i!=endI)
|
||||
{
|
||||
out.precision(10);
|
||||
out.width(15);
|
||||
out.setf(std::ofstream::left);
|
||||
out << ((*i)/unitEnergies) << ' ';
|
||||
|
||||
out.precision(10);
|
||||
out.width(15);
|
||||
out.setf(std::ofstream::left);
|
||||
out << ((*j)/unitData) << std::endl;
|
||||
|
||||
i++;
|
||||
j++;
|
||||
}
|
||||
}
|
||||
|
||||
out.precision(10);
|
||||
out.width(15);
|
||||
out.setf(std::ofstream::left);
|
||||
out << -1.f << ' ';
|
||||
|
||||
out.precision(10);
|
||||
out.width(15);
|
||||
out.setf(std::ofstream::left);
|
||||
out << -1.f << std::endl;
|
||||
|
||||
out.precision(10);
|
||||
out.width(15);
|
||||
out.setf(std::ofstream::left);
|
||||
out << -2.f << ' ';
|
||||
|
||||
out.precision(10);
|
||||
out.width(15);
|
||||
out.setf(std::ofstream::left);
|
||||
out << -2.f << std::endl;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
size_t G4EMDataSet :: FindLowerBound(G4double argEnergy) const
|
||||
{
|
||||
size_t lowerBound(0);
|
||||
size_t upperBound(energies->size() - 1);
|
||||
|
||||
while (lowerBound <= upperBound)
|
||||
{
|
||||
size_t midBin((lowerBound + upperBound)/2);
|
||||
|
||||
if (argEnergy < (*energies)[midBin])
|
||||
upperBound = midBin-1;
|
||||
if (!energies)
|
||||
{
|
||||
G4cout << "Data not available." << G4endl;
|
||||
}
|
||||
else
|
||||
lowerBound = midBin+1;
|
||||
}
|
||||
|
||||
return upperBound;
|
||||
{
|
||||
size_t size = energies->size();
|
||||
for (size_t i(0); i<size; i++)
|
||||
{
|
||||
G4cout << "Point: " << ((*energies)[i]/unitEnergies)
|
||||
<< " - Data value: " << ((*data)[i]/unitData);
|
||||
if (pdf != 0) G4cout << " - PDF : " << (*pdf)[i];
|
||||
G4cout << G4endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
G4String G4EMDataSet :: FullFileName(const G4String & argFileName) const
|
||||
void G4EMDataSet::SetEnergiesData(G4DataVector* dataX,
|
||||
G4DataVector* dataY,
|
||||
G4int /* componentId */)
|
||||
{
|
||||
char* path = getenv("G4LEDATA");
|
||||
if (!path)
|
||||
G4Exception("G4EMDataSet::FullFileName - G4LEDATA environment variable not set");
|
||||
|
||||
std::ostringstream fullFileName;
|
||||
if (energies) delete energies;
|
||||
energies = dataX;
|
||||
|
||||
if (data) delete data;
|
||||
data = dataY;
|
||||
|
||||
fullFileName << path << '/' << argFileName << z << ".dat";
|
||||
|
||||
return G4String(fullFileName.str().c_str());
|
||||
if ((energies == 0) ^ (data==0))
|
||||
G4Exception("G4EMDataSet::SetEnergiesData - different size for energies and data (zero case)");
|
||||
|
||||
if (energies == 0) return;
|
||||
|
||||
if (energies->size() != data->size())
|
||||
G4Exception("G4EMDataSet::SetEnergiesData - different size for energies and data");
|
||||
}
|
||||
|
||||
G4bool G4EMDataSet::LoadData(const G4String& fileName)
|
||||
{
|
||||
// The file is organized into two columns:
|
||||
// 1st column is the energy
|
||||
// 2nd column is the corresponding value
|
||||
// The file terminates with the pattern: -1 -1
|
||||
// -2 -2
|
||||
|
||||
G4String fullFileName(FullFileName(fileName));
|
||||
std::ifstream in(fullFileName);
|
||||
|
||||
if (!in.is_open())
|
||||
{
|
||||
G4String message("G4EMDataSet::LoadData - data file \"");
|
||||
message += fullFileName;
|
||||
message += "\" not found";
|
||||
G4Exception(message);
|
||||
}
|
||||
|
||||
G4DataVector* argEnergies=new G4DataVector;
|
||||
G4DataVector* argData=new G4DataVector;
|
||||
|
||||
G4double a;
|
||||
bool energyColumn(true);
|
||||
|
||||
do
|
||||
{
|
||||
in >> a;
|
||||
|
||||
if (a!=-1 && a!=-2)
|
||||
{
|
||||
if (energyColumn)
|
||||
argEnergies->push_back(a*unitEnergies);
|
||||
else
|
||||
argData->push_back(a*unitData);
|
||||
energyColumn=(!energyColumn);
|
||||
}
|
||||
}
|
||||
while (a != -2);
|
||||
|
||||
SetEnergiesData(argEnergies, argData, 0);
|
||||
if (randomSet) BuildPdf();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
G4bool G4EMDataSet::SaveData(const G4String& name) const
|
||||
{
|
||||
// The file is organized into two columns:
|
||||
// 1st column is the energy
|
||||
// 2nd column is the corresponding value
|
||||
// The file terminates with the pattern: -1 -1
|
||||
// -2 -2
|
||||
|
||||
G4String fullFileName(FullFileName(name));
|
||||
std::ofstream out(fullFileName);
|
||||
|
||||
if (!out.is_open())
|
||||
{
|
||||
G4String message("G4EMDataSet::SaveData - cannot open \"");
|
||||
message+=fullFileName;
|
||||
message+="\"";
|
||||
G4Exception(message);
|
||||
}
|
||||
|
||||
out.precision(10);
|
||||
out.width(15);
|
||||
out.setf(std::ofstream::left);
|
||||
|
||||
if (energies!=0 && data!=0)
|
||||
{
|
||||
G4DataVector::const_iterator i(energies->begin());
|
||||
G4DataVector::const_iterator endI(energies->end());
|
||||
G4DataVector::const_iterator j(data->begin());
|
||||
|
||||
while (i!=endI)
|
||||
{
|
||||
out.precision(10);
|
||||
out.width(15);
|
||||
out.setf(std::ofstream::left);
|
||||
out << ((*i)/unitEnergies) << ' ';
|
||||
|
||||
out.precision(10);
|
||||
out.width(15);
|
||||
out.setf(std::ofstream::left);
|
||||
out << ((*j)/unitData) << std::endl;
|
||||
|
||||
i++;
|
||||
j++;
|
||||
}
|
||||
}
|
||||
|
||||
out.precision(10);
|
||||
out.width(15);
|
||||
out.setf(std::ofstream::left);
|
||||
out << -1.f << ' ';
|
||||
|
||||
out.precision(10);
|
||||
out.width(15);
|
||||
out.setf(std::ofstream::left);
|
||||
out << -1.f << std::endl;
|
||||
|
||||
out.precision(10);
|
||||
out.width(15);
|
||||
out.setf(std::ofstream::left);
|
||||
out << -2.f << ' ';
|
||||
|
||||
out.precision(10);
|
||||
out.width(15);
|
||||
out.setf(std::ofstream::left);
|
||||
out << -2.f << std::endl;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
size_t G4EMDataSet::FindLowerBound(G4double x) const
|
||||
{
|
||||
size_t lowerBound = 0;
|
||||
size_t upperBound(energies->size() - 1);
|
||||
|
||||
while (lowerBound <= upperBound)
|
||||
{
|
||||
size_t midBin((lowerBound + upperBound) / 2);
|
||||
|
||||
if (x < (*energies)[midBin]) upperBound = midBin - 1;
|
||||
else lowerBound = midBin + 1;
|
||||
}
|
||||
|
||||
return upperBound;
|
||||
}
|
||||
|
||||
|
||||
size_t G4EMDataSet::FindLowerBound(G4double x, G4DataVector* values) const
|
||||
{
|
||||
size_t lowerBound = 0;;
|
||||
size_t upperBound(values->size() - 1);
|
||||
|
||||
while (lowerBound <= upperBound)
|
||||
{
|
||||
size_t midBin((lowerBound + upperBound) / 2);
|
||||
|
||||
if (x < (*values)[midBin]) upperBound = midBin - 1;
|
||||
else lowerBound = midBin + 1;
|
||||
}
|
||||
|
||||
return upperBound;
|
||||
}
|
||||
|
||||
|
||||
G4String G4EMDataSet::FullFileName(const G4String& name) const
|
||||
{
|
||||
char* path = getenv("G4LEDATA");
|
||||
if (!path)
|
||||
G4Exception("G4EMDataSet::FullFileName - G4LEDATA environment variable not set");
|
||||
|
||||
std::ostringstream fullFileName;
|
||||
fullFileName << path << '/' << name << z << ".dat";
|
||||
|
||||
return G4String(fullFileName.str().c_str());
|
||||
}
|
||||
|
||||
|
||||
void G4EMDataSet::BuildPdf()
|
||||
{
|
||||
pdf = new G4DataVector;
|
||||
G4Integrator <G4EMDataSet, G4double(G4EMDataSet::*)(G4double)> integrator;
|
||||
|
||||
G4int nData = data->size();
|
||||
pdf->push_back(0.);
|
||||
|
||||
// Integrate the data distribution
|
||||
G4int i;
|
||||
G4double totalSum = 0.;
|
||||
for (i=1; i<nData; i++)
|
||||
{
|
||||
G4double xLow = (*energies)[i-1];
|
||||
G4double xHigh = (*energies)[i];
|
||||
G4double sum = integrator.Legendre96(this, &G4EMDataSet::IntegrationFunction, xLow, xHigh);
|
||||
totalSum = totalSum + sum;
|
||||
pdf->push_back(totalSum);
|
||||
}
|
||||
|
||||
// Normalize to the last bin
|
||||
G4double tot = 0.;
|
||||
if (totalSum > 0.) tot = 1. / totalSum;
|
||||
for (i=1; i<nData; i++)
|
||||
{
|
||||
(*pdf)[i] = (*pdf)[i] * tot;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
G4double G4EMDataSet::RandomSelect(G4int /* componentId */) const
|
||||
{
|
||||
// Random select a X value according to the cumulative probability distribution
|
||||
// derived from the data
|
||||
|
||||
if (!pdf) G4Exception("G4EMDataSet::RandomSelect - PDF has not been created for this data set");
|
||||
|
||||
G4double value = 0.;
|
||||
G4double x = G4UniformRand();
|
||||
|
||||
// Locate the random value in the X vector based on the PDF
|
||||
size_t bin = FindLowerBound(x,pdf);
|
||||
|
||||
// Interpolate the PDF to calculate the X value:
|
||||
// linear interpolation in the first bin (to avoid problem with 0),
|
||||
// interpolation with associated data set algorithm in other bins
|
||||
|
||||
G4LinInterpolation linearAlgo;
|
||||
if (bin == 0) value = linearAlgo.Calculate(x, bin, *pdf, *energies);
|
||||
else value = algorithm->Calculate(x, bin, *pdf, *energies);
|
||||
|
||||
// G4cout << x << " random bin "<< bin << " - " << value << G4endl;
|
||||
return value;
|
||||
}
|
||||
|
||||
G4double G4EMDataSet::IntegrationFunction(G4double x)
|
||||
{
|
||||
// This function is needed by G4Integrator to calculate the integral of the data distribution
|
||||
|
||||
G4double y = 0;
|
||||
|
||||
// Locate the random value in the X vector based on the PDF
|
||||
size_t bin = FindLowerBound(x);
|
||||
|
||||
// Interpolate to calculate the X value:
|
||||
// linear interpolation in the first bin (to avoid problem with 0),
|
||||
// interpolation with associated algorithm in other bins
|
||||
|
||||
G4LinInterpolation linearAlgo;
|
||||
|
||||
if (bin == 0) y = linearAlgo.Calculate(x, bin, *energies, *data);
|
||||
else y = algorithm->Calculate(x, bin, *energies, *data);
|
||||
|
||||
return y;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user