Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 // 26 // >> 27 // $Id: XrayFluoDAtaSet.cc >> 28 // GEANT4 tag $Name: xray_fluo-V03-02-00 27 // 29 // 28 // Author: Elena Guardincerri (Elena.Guardince 30 // Author: Elena Guardincerri (Elena.Guardincerri@ge.infn.it) 29 // 31 // 30 // History: 32 // History: 31 // ----------- 33 // ----------- 32 // 28 Nov 2001 Elena Guardincerri Created 34 // 28 Nov 2001 Elena Guardincerri Created 33 // 35 // 34 // ------------------------------------------- 36 // ------------------------------------------------------------------- 35 37 36 #include "XrayFluoDataSet.hh" 38 #include "XrayFluoDataSet.hh" 37 #include <fstream> 39 #include <fstream> 38 #include "G4VDataSetAlgorithm.hh" 40 #include "G4VDataSetAlgorithm.hh" 39 41 40 XrayFluoDataSet::XrayFluoDataSet(G4int /*Z*/, 42 XrayFluoDataSet::XrayFluoDataSet(G4int /*Z*/, 41 G4DataVector* points, 43 G4DataVector* points, 42 G4DataVector* values, 44 G4DataVector* values, 43 const G4VDataSetAlgorithm* interpolatio 45 const G4VDataSetAlgorithm* interpolation, 44 G4double unitE, G4double unitData) 46 G4double unitE, G4double unitData) 45 :energies(points), data(values), algorithm(i 47 :energies(points), data(values), algorithm(interpolation) 46 { 48 { 47 numberOfBins = energies->size(); 49 numberOfBins = energies->size(); 48 unit1 = unitE; 50 unit1 = unitE; 49 unit2 = unitData; 51 unit2 = unitData; 50 } 52 } 51 53 52 //....oooOO0OOooo........oooOO0OOooo........oo 54 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 53 55 54 XrayFluoDataSet:: XrayFluoDataSet(G4int /*Z*/, 56 XrayFluoDataSet:: XrayFluoDataSet(G4int /*Z*/, 55 const G4String& dataFile, 57 const G4String& dataFile, 56 const G4VDataSetAlgorithm* interpolati 58 const G4VDataSetAlgorithm* interpolation, 57 G4double unitE, G4double unitData) 59 G4double unitE, G4double unitData) 58 : algorithm(interpolation) 60 : algorithm(interpolation) 59 { 61 { 60 energies = new G4DataVector; 62 energies = new G4DataVector; 61 data = new G4DataVector; 63 data = new G4DataVector; 62 unit1 = unitE; 64 unit1 = unitE; 63 unit2 = unitData; 65 unit2 = unitData; 64 LoadData(dataFile); 66 LoadData(dataFile); 65 numberOfBins = energies->size(); 67 numberOfBins = energies->size(); 66 } 68 } 67 69 68 //....oooOO0OOooo........oooOO0OOooo........oo 70 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 69 71 70 // Destructor 72 // Destructor 71 XrayFluoDataSet::~XrayFluoDataSet() 73 XrayFluoDataSet::~XrayFluoDataSet() 72 { 74 { 73 delete energies; 75 delete energies; 74 delete data; 76 delete data; 75 } 77 } 76 78 77 //....oooOO0OOooo........oooOO0OOooo........oo 79 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 78 80 79 G4double XrayFluoDataSet::FindValue(G4double e 81 G4double XrayFluoDataSet::FindValue(G4double e, G4int) const 80 { 82 { 81 G4double value; 83 G4double value; 82 G4double e0 = (*energies)[0]; 84 G4double e0 = (*energies)[0]; 83 // Protections 85 // Protections 84 size_t bin = FindBinLocation(e); 86 size_t bin = FindBinLocation(e); 85 if (bin == numberOfBins) 87 if (bin == numberOfBins) 86 { 88 { 87 89 88 value = (*data)[bin]; 90 value = (*data)[bin]; 89 } 91 } 90 else if (e <= e0) 92 else if (e <= e0) 91 { 93 { 92 94 93 value = (*data)[0]; 95 value = (*data)[0]; 94 } 96 } 95 else 97 else 96 { 98 { 97 value = algorithm->Calculate(e,bin,*ener 99 value = algorithm->Calculate(e,bin,*energies,*data); 98 } 100 } 99 101 100 return value; 102 return value; 101 } 103 } 102 104 103 //....oooOO0OOooo........oooOO0OOooo........oo 105 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 104 106 105 G4int XrayFluoDataSet::FindBinLocation(G4doubl 107 G4int XrayFluoDataSet::FindBinLocation(G4double energy) const 106 { 108 { 107 // Protection against call outside allowed r 109 // Protection against call outside allowed range 108 G4double e0 = (*energies)[0]; 110 G4double e0 = (*energies)[0]; 109 if (energy < e0) 111 if (energy < e0) 110 { 112 { 111 113 112 energy = e0; 114 energy = e0; 113 } 115 } 114 116 115 size_t lowerBound = 0; 117 size_t lowerBound = 0; 116 size_t upperBound = numberOfBins - 1; 118 size_t upperBound = numberOfBins - 1; 117 119 118 // Binary search 120 // Binary search 119 while (lowerBound <= upperBound) 121 while (lowerBound <= upperBound) 120 { 122 { 121 size_t midBin = (lowerBound + upperBound 123 size_t midBin = (lowerBound + upperBound)/2; 122 if ( energy < (*energies)[midBin] ) uppe 124 if ( energy < (*energies)[midBin] ) upperBound = midBin-1; 123 else lowerBound = midBin+1; 125 else lowerBound = midBin+1; 124 } 126 } 125 127 126 return upperBound; 128 return upperBound; 127 } 129 } 128 130 129 //....oooOO0OOooo........oooOO0OOooo........oo 131 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 130 132 131 G4bool XrayFluoDataSet::LoadData(const G4Strin 133 G4bool XrayFluoDataSet::LoadData(const G4String& fileName) 132 { 134 { 133 // Build the complete string identifying the 135 // Build the complete string identifying the file with the data set 134 G4String dirFile = ""; 136 G4String dirFile = ""; 135 137 136 char* path; 138 char* path; 137 139 138 path = std::getenv("XRAYDATA"); << 140 path = getenv("XRAYDATA"); 139 if (!path) 141 if (!path) 140 path = std::getenv("PWD"); << 142 path = getenv("PWD"); 141 143 142 //G4cout << path << G4endl; 144 //G4cout << path << G4endl; 143 //G4cout << fileName << G4endl; 145 //G4cout << fileName << G4endl; 144 146 145 147 146 G4String pathString(path); 148 G4String pathString(path); 147 pathString += "\0"; 149 pathString += "\0"; 148 dirFile = pathString + "/" + fileName + ".da 150 dirFile = pathString + "/" + fileName + ".dat"; 149 151 150 std::ifstream file(dirFile); 152 std::ifstream file(dirFile); 151 std::filebuf* lsdp = file.rdbuf(); 153 std::filebuf* lsdp = file.rdbuf(); 152 154 153 if (! (lsdp->is_open()) ) 155 if (! (lsdp->is_open()) ) 154 { 156 { 155 G4ExceptionDescription execp; 157 G4ExceptionDescription execp; 156 execp << "XrayFluoDataSet - data file: " + 158 execp << "XrayFluoDataSet - data file: " + dirFile + " not found"<<G4endl; 157 G4Exception("XrayFluoDataSet::LoadData()", 159 G4Exception("XrayFluoDataSet::LoadData()","example-xray_fluorescence01", 158 FatalException, execp); 160 FatalException, execp); 159 } 161 } 160 G4double a = 0; 162 G4double a = 0; 161 G4int k = 1; 163 G4int k = 1; 162 164 163 do 165 do 164 { 166 { 165 file >> a; 167 file >> a; 166 G4int nColumns = 2; 168 G4int nColumns = 2; 167 // The file is organized into two column 169 // The file is organized into two columns: 168 // 1st column is the energy 170 // 1st column is the energy 169 // 2nd column is the corresponding value 171 // 2nd column is the corresponding value 170 // The file terminates with the pattern: 172 // The file terminates with the pattern: -1 -1 171 // 173 // -2 -2 172 if (a == -1 || a == -2) 174 if (a == -1 || a == -2) 173 { 175 { 174 176 175 } 177 } 176 else 178 else 177 { 179 { 178 if (k%nColumns != 0) 180 if (k%nColumns != 0) 179 { 181 { 180 G4double e = a * unit1; 182 G4double e = a * unit1; 181 energies->push_back(e); 183 energies->push_back(e); 182 184 183 k++; 185 k++; 184 186 185 } 187 } 186 else if (k%nColumns == 0) 188 else if (k%nColumns == 0) 187 { 189 { 188 G4double value = a * unit2; 190 G4double value = a * unit2; 189 data->push_back(value); 191 data->push_back(value); 190 192 191 k = 1; 193 k = 1; 192 } 194 } 193 } 195 } 194 196 195 } while (a != -2); // end of file 197 } while (a != -2); // end of file 196 198 197 file.close(); 199 file.close(); 198 return true; 200 return true; 199 201 200 } 202 } 201 203 202 //....oooOO0OOooo........oooOO0OOooo........oo 204 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 203 205 204 void XrayFluoDataSet::PrintData() const 206 void XrayFluoDataSet::PrintData() const 205 { 207 { 206 size_t size = numberOfBins; 208 size_t size = numberOfBins; 207 for (size_t i=0; i<size; i++) 209 for (size_t i=0; i<size; i++) 208 { 210 { 209 G4double e = (*energies)[i] / unit1; 211 G4double e = (*energies)[i] / unit1; 210 G4double sigma = (*data)[i] / unit2 ; 212 G4double sigma = (*data)[i] / unit2 ; 211 G4cout << "Point: " 213 G4cout << "Point: " 212 << e 214 << e 213 << " - Data value : " 215 << " - Data value : " 214 << sigma 216 << sigma 215 << G4endl; 217 << G4endl; 216 } 218 } 217 } 219 } 218 220 219 221 220 222 221 223