File indexing completed on 2025-01-18 09:17:09
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036 #include "XrayFluoDataSet.hh"
0037 #include <fstream>
0038 #include "G4VDataSetAlgorithm.hh"
0039
0040 XrayFluoDataSet::XrayFluoDataSet(G4int ,
0041 G4DataVector* points,
0042 G4DataVector* values,
0043 const G4VDataSetAlgorithm* interpolation,
0044 G4double unitE, G4double unitData)
0045 :energies(points), data(values), algorithm(interpolation)
0046 {
0047 numberOfBins = energies->size();
0048 unit1 = unitE;
0049 unit2 = unitData;
0050 }
0051
0052
0053
0054 XrayFluoDataSet:: XrayFluoDataSet(G4int ,
0055 const G4String& dataFile,
0056 const G4VDataSetAlgorithm* interpolation,
0057 G4double unitE, G4double unitData)
0058 : algorithm(interpolation)
0059 {
0060 energies = new G4DataVector;
0061 data = new G4DataVector;
0062 unit1 = unitE;
0063 unit2 = unitData;
0064 LoadData(dataFile);
0065 numberOfBins = energies->size();
0066 }
0067
0068
0069
0070
0071 XrayFluoDataSet::~XrayFluoDataSet()
0072 {
0073 delete energies;
0074 delete data;
0075 }
0076
0077
0078
0079 G4double XrayFluoDataSet::FindValue(G4double e, G4int) const
0080 {
0081 G4double value;
0082 G4double e0 = (*energies)[0];
0083
0084 size_t bin = FindBinLocation(e);
0085 if (bin == numberOfBins)
0086 {
0087
0088 value = (*data)[bin];
0089 }
0090 else if (e <= e0)
0091 {
0092
0093 value = (*data)[0];
0094 }
0095 else
0096 {
0097 value = algorithm->Calculate(e,bin,*energies,*data);
0098 }
0099
0100 return value;
0101 }
0102
0103
0104
0105 G4int XrayFluoDataSet::FindBinLocation(G4double energy) const
0106 {
0107
0108 G4double e0 = (*energies)[0];
0109 if (energy < e0)
0110 {
0111
0112 energy = e0;
0113 }
0114
0115 size_t lowerBound = 0;
0116 size_t upperBound = numberOfBins - 1;
0117
0118
0119 while (lowerBound <= upperBound)
0120 {
0121 size_t midBin = (lowerBound + upperBound)/2;
0122 if ( energy < (*energies)[midBin] ) upperBound = midBin-1;
0123 else lowerBound = midBin+1;
0124 }
0125
0126 return upperBound;
0127 }
0128
0129
0130
0131 G4bool XrayFluoDataSet::LoadData(const G4String& fileName)
0132 {
0133
0134 G4String dirFile = "";
0135
0136 char* path;
0137
0138 path = std::getenv("XRAYDATA");
0139 if (!path)
0140 path = std::getenv("PWD");
0141
0142
0143
0144
0145
0146 G4String pathString(path);
0147 pathString += "\0";
0148 dirFile = pathString + "/" + fileName + ".dat";
0149
0150 std::ifstream file(dirFile);
0151 std::filebuf* lsdp = file.rdbuf();
0152
0153 if (! (lsdp->is_open()) )
0154 {
0155 G4ExceptionDescription execp;
0156 execp << "XrayFluoDataSet - data file: " + dirFile + " not found"<<G4endl;
0157 G4Exception("XrayFluoDataSet::LoadData()","example-xray_fluorescence01",
0158 FatalException, execp);
0159 }
0160 G4double a = 0;
0161 G4int k = 1;
0162
0163 do
0164 {
0165 file >> a;
0166 G4int nColumns = 2;
0167
0168
0169
0170
0171
0172 if (a == -1 || a == -2)
0173 {
0174
0175 }
0176 else
0177 {
0178 if (k%nColumns != 0)
0179 {
0180 G4double e = a * unit1;
0181 energies->push_back(e);
0182
0183 k++;
0184
0185 }
0186 else if (k%nColumns == 0)
0187 {
0188 G4double value = a * unit2;
0189 data->push_back(value);
0190
0191 k = 1;
0192 }
0193 }
0194
0195 } while (a != -2);
0196
0197 file.close();
0198 return true;
0199
0200 }
0201
0202
0203
0204 void XrayFluoDataSet::PrintData() const
0205 {
0206 size_t size = numberOfBins;
0207 for (size_t i=0; i<size; i++)
0208 {
0209 G4double e = (*energies)[i] / unit1;
0210 G4double sigma = (*data)[i] / unit2 ;
0211 G4cout << "Point: "
0212 << e
0213 << " - Data value : "
0214 << sigma
0215 << G4endl;
0216 }
0217 }
0218
0219
0220