[819] | 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 | // |
---|
| 27 | // $Id: G4EMDataSet.cc,v 1.12 2006/06/29 19:39:44 gunter Exp $ |
---|
| 28 | // GEANT4 tag $Name: geant4-09-01-patch-02 $ |
---|
| 29 | // |
---|
| 30 | // Author: Maria Grazia Pia (Maria.Grazia.Pia@cern.ch) |
---|
| 31 | // |
---|
| 32 | // History: |
---|
| 33 | // ----------- |
---|
| 34 | // 31 Jul 2001 MGP Created |
---|
| 35 | // |
---|
| 36 | // ------------------------------------------------------------------- |
---|
| 37 | |
---|
| 38 | #include "G4EMDataSet.hh" |
---|
| 39 | #include "G4VDataSetAlgorithm.hh" |
---|
| 40 | #include <fstream> |
---|
| 41 | #include <sstream> |
---|
| 42 | |
---|
| 43 | G4EMDataSet :: G4EMDataSet(G4int argZ, G4VDataSetAlgorithm * argAlgorithm, G4double argUnitEnergies, G4double argUnitData) |
---|
| 44 | : |
---|
| 45 | z(argZ), |
---|
| 46 | energies(0), |
---|
| 47 | data(0), |
---|
| 48 | algorithm(argAlgorithm), |
---|
| 49 | unitEnergies(argUnitEnergies), |
---|
| 50 | unitData(argUnitData) |
---|
| 51 | { |
---|
| 52 | if (algorithm == 0) |
---|
| 53 | G4Exception("G4EMDataSet::G4EMDataSet - interpolation == 0"); |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | |
---|
| 57 | |
---|
| 58 | G4EMDataSet :: G4EMDataSet(G4int argZ, G4DataVector * argEnergies, G4DataVector * argData, G4VDataSetAlgorithm * argAlgorithm, G4double argUnitEnergies, G4double argUnitData) |
---|
| 59 | : |
---|
| 60 | z(argZ), |
---|
| 61 | energies(argEnergies), |
---|
| 62 | data(argData), |
---|
| 63 | algorithm(argAlgorithm), |
---|
| 64 | unitEnergies(argUnitEnergies), |
---|
| 65 | unitData(argUnitData) |
---|
| 66 | { |
---|
| 67 | if (algorithm==0) |
---|
| 68 | G4Exception("G4EMDataSet::G4EMDataSet - interpolation == 0"); |
---|
| 69 | |
---|
| 70 | if ((energies==0) ^ (data==0)) |
---|
| 71 | G4Exception("G4EMDataSet::G4EMDataSet - different size for energies and data (zero case)"); |
---|
| 72 | |
---|
| 73 | if (energies==0) |
---|
| 74 | return; |
---|
| 75 | |
---|
| 76 | if (energies->size()!=data->size()) |
---|
| 77 | G4Exception("G4EMDataSet::G4EMDataSet - different size for energies and data"); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | |
---|
| 81 | |
---|
| 82 | G4EMDataSet :: ~G4EMDataSet() |
---|
| 83 | { |
---|
| 84 | delete algorithm; |
---|
| 85 | |
---|
| 86 | if (energies) |
---|
| 87 | delete energies; |
---|
| 88 | |
---|
| 89 | if (data) |
---|
| 90 | delete data; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | |
---|
| 94 | |
---|
| 95 | |
---|
| 96 | |
---|
| 97 | G4double G4EMDataSet :: FindValue(G4double argEnergy, G4int /* argComponentId */) const |
---|
| 98 | { |
---|
| 99 | if (!energies) |
---|
| 100 | G4Exception("G4EMDataSet::FindValue - energies == 0"); |
---|
| 101 | |
---|
| 102 | if (energies->empty()) |
---|
| 103 | return 0; |
---|
| 104 | |
---|
| 105 | if (argEnergy <= (*energies)[0]) |
---|
| 106 | return (*data)[0]; |
---|
| 107 | |
---|
| 108 | size_t i(energies->size()-1); |
---|
| 109 | |
---|
| 110 | if (argEnergy >= (*energies)[i]) |
---|
| 111 | return (*data)[i]; |
---|
| 112 | |
---|
| 113 | return algorithm->Calculate(argEnergy, FindLowerBound(argEnergy), *energies, *data); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | |
---|
| 117 | |
---|
| 118 | |
---|
| 119 | |
---|
| 120 | void G4EMDataSet :: PrintData(void) const |
---|
| 121 | { |
---|
| 122 | if (!energies) |
---|
| 123 | { |
---|
| 124 | G4cout << "Data not available." << G4endl; |
---|
| 125 | return; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | size_t size = energies->size(); |
---|
| 129 | |
---|
| 130 | for (size_t i(0); i<size; i++) |
---|
| 131 | G4cout << "Point: " << ((*energies)[i]/unitEnergies) |
---|
| 132 | << " - Data value : " << ((*data)[i]/unitData) << G4endl; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | |
---|
| 136 | |
---|
| 137 | |
---|
| 138 | |
---|
| 139 | void G4EMDataSet :: SetEnergiesData(G4DataVector * argEnergies, G4DataVector * argData, G4int /* argComponentId */) |
---|
| 140 | { |
---|
| 141 | if (energies) |
---|
| 142 | delete energies; |
---|
| 143 | energies=argEnergies; |
---|
| 144 | |
---|
| 145 | if (data) |
---|
| 146 | delete data; |
---|
| 147 | data=argData; |
---|
| 148 | |
---|
| 149 | if ((energies==0) ^ (data==0)) |
---|
| 150 | G4Exception("G4EMDataSet::SetEnergiesData - different size for energies and data (zero case)"); |
---|
| 151 | |
---|
| 152 | if (energies==0) |
---|
| 153 | return; |
---|
| 154 | |
---|
| 155 | if (energies->size()!=data->size()) |
---|
| 156 | G4Exception("G4EMDataSet::SetEnergiesData - different size for energies and data"); |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | |
---|
| 160 | |
---|
| 161 | |
---|
| 162 | |
---|
| 163 | G4bool G4EMDataSet :: LoadData(const G4String & argFileName) |
---|
| 164 | { |
---|
| 165 | // The file is organized into two columns: |
---|
| 166 | // 1st column is the energy |
---|
| 167 | // 2nd column is the corresponding value |
---|
| 168 | // The file terminates with the pattern: -1 -1 |
---|
| 169 | // -2 -2 |
---|
| 170 | |
---|
| 171 | G4String fullFileName(FullFileName(argFileName)); |
---|
| 172 | std::ifstream in(fullFileName); |
---|
| 173 | |
---|
| 174 | if (!in.is_open()) |
---|
| 175 | { |
---|
| 176 | G4String message("G4EMDataSet::LoadData - data file \""); |
---|
| 177 | message+=fullFileName; |
---|
| 178 | message+="\" not found"; |
---|
| 179 | G4Exception(message); |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | G4DataVector * argEnergies=new G4DataVector; |
---|
| 183 | G4DataVector * argData=new G4DataVector; |
---|
| 184 | |
---|
| 185 | G4double a; |
---|
| 186 | bool energyColumn(true); |
---|
| 187 | |
---|
| 188 | do |
---|
| 189 | { |
---|
| 190 | in >> a; |
---|
| 191 | |
---|
| 192 | if (a!=-1 && a!=-2) |
---|
| 193 | { |
---|
| 194 | if (energyColumn) |
---|
| 195 | argEnergies->push_back(a*unitEnergies); |
---|
| 196 | else |
---|
| 197 | argData->push_back(a*unitData); |
---|
| 198 | energyColumn=(!energyColumn); |
---|
| 199 | } |
---|
| 200 | } |
---|
| 201 | while (a != -2); |
---|
| 202 | |
---|
| 203 | SetEnergiesData(argEnergies, argData, 0); |
---|
| 204 | |
---|
| 205 | return true; |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | |
---|
| 209 | |
---|
| 210 | G4bool G4EMDataSet :: SaveData(const G4String & argFileName) const |
---|
| 211 | { |
---|
| 212 | // The file is organized into two columns: |
---|
| 213 | // 1st column is the energy |
---|
| 214 | // 2nd column is the corresponding value |
---|
| 215 | // The file terminates with the pattern: -1 -1 |
---|
| 216 | // -2 -2 |
---|
| 217 | |
---|
| 218 | G4String fullFileName(FullFileName(argFileName)); |
---|
| 219 | std::ofstream out(fullFileName); |
---|
| 220 | |
---|
| 221 | if (!out.is_open()) |
---|
| 222 | { |
---|
| 223 | G4String message("G4EMDataSet::SaveData - cannot open \""); |
---|
| 224 | message+=fullFileName; |
---|
| 225 | message+="\""; |
---|
| 226 | G4Exception(message); |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | out.precision(10); |
---|
| 230 | out.width(15); |
---|
| 231 | out.setf(std::ofstream::left); |
---|
| 232 | |
---|
| 233 | if (energies!=0 && data!=0) |
---|
| 234 | { |
---|
| 235 | G4DataVector::const_iterator i(energies->begin()); |
---|
| 236 | G4DataVector::const_iterator endI(energies->end()); |
---|
| 237 | G4DataVector::const_iterator j(data->begin()); |
---|
| 238 | |
---|
| 239 | while (i!=endI) |
---|
| 240 | { |
---|
| 241 | out.precision(10); |
---|
| 242 | out.width(15); |
---|
| 243 | out.setf(std::ofstream::left); |
---|
| 244 | out << ((*i)/unitEnergies) << ' '; |
---|
| 245 | |
---|
| 246 | out.precision(10); |
---|
| 247 | out.width(15); |
---|
| 248 | out.setf(std::ofstream::left); |
---|
| 249 | out << ((*j)/unitData) << std::endl; |
---|
| 250 | |
---|
| 251 | i++; |
---|
| 252 | j++; |
---|
| 253 | } |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | out.precision(10); |
---|
| 257 | out.width(15); |
---|
| 258 | out.setf(std::ofstream::left); |
---|
| 259 | out << -1.f << ' '; |
---|
| 260 | |
---|
| 261 | out.precision(10); |
---|
| 262 | out.width(15); |
---|
| 263 | out.setf(std::ofstream::left); |
---|
| 264 | out << -1.f << std::endl; |
---|
| 265 | |
---|
| 266 | out.precision(10); |
---|
| 267 | out.width(15); |
---|
| 268 | out.setf(std::ofstream::left); |
---|
| 269 | out << -2.f << ' '; |
---|
| 270 | |
---|
| 271 | out.precision(10); |
---|
| 272 | out.width(15); |
---|
| 273 | out.setf(std::ofstream::left); |
---|
| 274 | out << -2.f << std::endl; |
---|
| 275 | |
---|
| 276 | return true; |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | |
---|
| 280 | |
---|
| 281 | |
---|
| 282 | |
---|
| 283 | size_t G4EMDataSet :: FindLowerBound(G4double argEnergy) const |
---|
| 284 | { |
---|
| 285 | size_t lowerBound(0); |
---|
| 286 | size_t upperBound(energies->size() - 1); |
---|
| 287 | |
---|
| 288 | while (lowerBound <= upperBound) |
---|
| 289 | { |
---|
| 290 | size_t midBin((lowerBound + upperBound)/2); |
---|
| 291 | |
---|
| 292 | if (argEnergy < (*energies)[midBin]) |
---|
| 293 | upperBound = midBin-1; |
---|
| 294 | else |
---|
| 295 | lowerBound = midBin+1; |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | return upperBound; |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | |
---|
| 302 | |
---|
| 303 | |
---|
| 304 | |
---|
| 305 | G4String G4EMDataSet :: FullFileName(const G4String & argFileName) const |
---|
| 306 | { |
---|
| 307 | char* path = getenv("G4LEDATA"); |
---|
| 308 | if (!path) |
---|
| 309 | G4Exception("G4EMDataSet::FullFileName - G4LEDATA environment variable not set"); |
---|
| 310 | |
---|
| 311 | std::ostringstream fullFileName; |
---|
| 312 | |
---|
| 313 | fullFileName << path << '/' << argFileName << z << ".dat"; |
---|
| 314 | |
---|
| 315 | return G4String(fullFileName.str().c_str()); |
---|
| 316 | } |
---|