[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 | // |
---|
[961] | 27 | // $Id: G4ShellEMDataSet.cc,v 1.16 2008/03/10 15:07:41 pia Exp $ |
---|
| 28 | // GEANT4 tag $Name: geant4-09-02-ref-02 $ |
---|
[819] | 29 | // |
---|
| 30 | // Author: Maria Grazia Pia (Maria.Grazia.Pia@cern.ch) |
---|
| 31 | // |
---|
| 32 | // History: |
---|
| 33 | // ----------- |
---|
[961] | 34 | // 1 Aug 2001 MGP Created |
---|
[819] | 35 | // 09.10.01 V.Ivanchenko Add case z=0 |
---|
[961] | 36 | // 9 Mar 2008 MGP Cleaned up unreadable code modified by former developer |
---|
| 37 | // (Further clean-up needed) |
---|
[819] | 38 | // |
---|
| 39 | // ------------------------------------------------------------------- |
---|
| 40 | |
---|
| 41 | #include "G4ShellEMDataSet.hh" |
---|
| 42 | #include "G4EMDataSet.hh" |
---|
| 43 | #include "G4VDataSetAlgorithm.hh" |
---|
| 44 | #include <fstream> |
---|
| 45 | #include <sstream> |
---|
| 46 | |
---|
| 47 | |
---|
[961] | 48 | G4ShellEMDataSet::G4ShellEMDataSet(G4int zeta, G4VDataSetAlgorithm* algo, |
---|
| 49 | G4double eUnit, |
---|
| 50 | G4double dataUnit) |
---|
[819] | 51 | : |
---|
[961] | 52 | z(zeta), |
---|
| 53 | algorithm(algo), |
---|
| 54 | unitEnergies(eUnit), |
---|
| 55 | unitData(dataUnit) |
---|
[819] | 56 | { |
---|
[961] | 57 | if (algorithm == 0) G4Exception("G4ShellEMDataSet::G4ShellEMDataSet - interpolation == 0"); |
---|
[819] | 58 | } |
---|
| 59 | |
---|
| 60 | |
---|
| 61 | G4ShellEMDataSet::~G4ShellEMDataSet() |
---|
| 62 | { |
---|
| 63 | CleanUpComponents(); |
---|
[961] | 64 | if (algorithm) delete algorithm; |
---|
[819] | 65 | } |
---|
| 66 | |
---|
| 67 | |
---|
[961] | 68 | G4double G4ShellEMDataSet::FindValue(G4double energy, G4int /* componentId */) const |
---|
[819] | 69 | { |
---|
| 70 | // Returns the sum over the shells corresponding to e |
---|
| 71 | G4double value = 0.; |
---|
| 72 | |
---|
| 73 | std::vector<G4VEMDataSet *>::const_iterator i(components.begin()); |
---|
| 74 | std::vector<G4VEMDataSet *>::const_iterator end(components.end()); |
---|
| 75 | |
---|
[961] | 76 | while (i != end) |
---|
[819] | 77 | { |
---|
[961] | 78 | value += (*i)->FindValue(energy); |
---|
[819] | 79 | i++; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | return value; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | |
---|
| 86 | void G4ShellEMDataSet::PrintData(void) const |
---|
| 87 | { |
---|
[961] | 88 | const size_t n = NumberOfComponents(); |
---|
[819] | 89 | |
---|
| 90 | G4cout << "The data set has " << n << " components" << G4endl; |
---|
| 91 | G4cout << G4endl; |
---|
| 92 | |
---|
[961] | 93 | size_t i = 0; |
---|
[819] | 94 | |
---|
[961] | 95 | while (i < n) |
---|
[819] | 96 | { |
---|
| 97 | G4cout << "--- Component " << i << " ---" << G4endl; |
---|
| 98 | GetComponent(i)->PrintData(); |
---|
| 99 | i++; |
---|
| 100 | } |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | |
---|
[961] | 104 | void G4ShellEMDataSet::SetEnergiesData(G4DataVector* energies, |
---|
| 105 | G4DataVector* data, |
---|
| 106 | G4int componentId) |
---|
[819] | 107 | { |
---|
[961] | 108 | G4VEMDataSet* component = components[componentId]; |
---|
[819] | 109 | |
---|
| 110 | if (component) |
---|
| 111 | { |
---|
[961] | 112 | component->SetEnergiesData(energies, data, 0); |
---|
[819] | 113 | return; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | std::ostringstream message; |
---|
[961] | 117 | message << "G4ShellEMDataSet::SetEnergiesData - component " << componentId << " not found"; |
---|
[819] | 118 | |
---|
| 119 | G4Exception(message.str().c_str()); |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | |
---|
[961] | 123 | G4bool G4ShellEMDataSet::LoadData(const G4String& file) |
---|
[819] | 124 | { |
---|
| 125 | CleanUpComponents(); |
---|
| 126 | |
---|
[961] | 127 | G4String fullFileName = FullFileName(file); |
---|
[819] | 128 | std::ifstream in(fullFileName); |
---|
| 129 | |
---|
| 130 | if (!in.is_open()) |
---|
| 131 | { |
---|
| 132 | G4String message("G4ShellEMDataSet::LoadData - data file \""); |
---|
[961] | 133 | message += fullFileName; |
---|
| 134 | message += "\" not found"; |
---|
[819] | 135 | G4Exception(message); |
---|
| 136 | } |
---|
| 137 | |
---|
[961] | 138 | G4DataVector* energies = 0; |
---|
| 139 | G4DataVector* data = 0; |
---|
[819] | 140 | |
---|
[961] | 141 | G4double a = 0.; |
---|
| 142 | G4int shellIndex = 0; |
---|
| 143 | bool energyColumn = true; |
---|
[819] | 144 | |
---|
| 145 | do |
---|
| 146 | { |
---|
| 147 | in >> a; |
---|
| 148 | |
---|
| 149 | if (a == -1) |
---|
| 150 | { |
---|
[961] | 151 | if (energyColumn && energies!=0) |
---|
[819] | 152 | { |
---|
[961] | 153 | AddComponent(new G4EMDataSet(shellIndex, energies, data, algorithm->Clone(), unitEnergies, unitData)); |
---|
| 154 | energies = 0; |
---|
| 155 | data = 0; |
---|
[819] | 156 | } |
---|
| 157 | |
---|
[961] | 158 | energyColumn = (!energyColumn); |
---|
[819] | 159 | } |
---|
[961] | 160 | else if (a != -2) |
---|
[819] | 161 | { |
---|
[961] | 162 | if (energies == 0) |
---|
[819] | 163 | { |
---|
[961] | 164 | energies = new G4DataVector; |
---|
| 165 | data = new G4DataVector; |
---|
[819] | 166 | } |
---|
| 167 | |
---|
| 168 | if (energyColumn) |
---|
[961] | 169 | energies->push_back(a * unitEnergies); |
---|
[819] | 170 | else |
---|
[961] | 171 | data->push_back(a * unitData); |
---|
[819] | 172 | |
---|
[961] | 173 | energyColumn = (!energyColumn); |
---|
[819] | 174 | } |
---|
| 175 | } |
---|
| 176 | while (a != -2); |
---|
| 177 | |
---|
| 178 | return true; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | |
---|
[961] | 182 | G4bool G4ShellEMDataSet::SaveData(const G4String& file) const |
---|
[819] | 183 | { |
---|
[961] | 184 | G4String fullFileName = FullFileName(file); |
---|
[819] | 185 | std::ofstream out(fullFileName); |
---|
| 186 | |
---|
| 187 | if (!out.is_open()) |
---|
| 188 | { |
---|
| 189 | G4String message("G4EMDataSet::SaveData - cannot open \""); |
---|
[961] | 190 | message += fullFileName; |
---|
| 191 | message += "\""; |
---|
[819] | 192 | G4Exception(message); |
---|
| 193 | } |
---|
| 194 | |
---|
[961] | 195 | const size_t n = NumberOfComponents(); |
---|
| 196 | size_t k = 0; |
---|
[819] | 197 | |
---|
[961] | 198 | while (k < n) |
---|
[819] | 199 | { |
---|
[961] | 200 | const G4VEMDataSet* component = GetComponent(k); |
---|
[819] | 201 | |
---|
| 202 | if (component) |
---|
| 203 | { |
---|
[961] | 204 | const G4DataVector& energies = component->GetEnergies(0); |
---|
| 205 | const G4DataVector& data = component->GetData(0); |
---|
[819] | 206 | |
---|
[961] | 207 | G4DataVector::const_iterator i = energies.begin(); |
---|
| 208 | G4DataVector::const_iterator endI = energies.end(); |
---|
| 209 | G4DataVector::const_iterator j = data.begin(); |
---|
[819] | 210 | |
---|
[961] | 211 | while (i != endI) |
---|
[819] | 212 | { |
---|
| 213 | out.precision(10); |
---|
| 214 | out.width(15); |
---|
| 215 | out.setf(std::ofstream::left); |
---|
| 216 | out << ((*i)/unitEnergies) << ' '; |
---|
| 217 | |
---|
| 218 | out.precision(10); |
---|
| 219 | out.width(15); |
---|
| 220 | out.setf(std::ofstream::left); |
---|
| 221 | out << ((*j)/unitData) << std::endl; |
---|
| 222 | i++; |
---|
| 223 | j++; |
---|
| 224 | } |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | out.precision(10); |
---|
| 228 | out.width(15); |
---|
| 229 | out.setf(std::ofstream::left); |
---|
| 230 | out << -1.f << ' '; |
---|
| 231 | |
---|
| 232 | out.precision(10); |
---|
| 233 | out.width(15); |
---|
| 234 | out.setf(std::ofstream::left); |
---|
| 235 | out << -1.f << std::endl; |
---|
| 236 | |
---|
| 237 | k++; |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | out.precision(10); |
---|
| 241 | out.width(15); |
---|
| 242 | out.setf(std::ofstream::left); |
---|
| 243 | out << -2.f << ' '; |
---|
| 244 | |
---|
| 245 | out.precision(10); |
---|
| 246 | out.width(15); |
---|
| 247 | out.setf(std::ofstream::left); |
---|
| 248 | out << -2.f << std::endl; |
---|
| 249 | |
---|
| 250 | return true; |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | |
---|
| 254 | void G4ShellEMDataSet::CleanUpComponents(void) |
---|
| 255 | { |
---|
| 256 | while (!components.empty()) |
---|
| 257 | { |
---|
[961] | 258 | if (components.back()) delete components.back(); |
---|
[819] | 259 | components.pop_back(); |
---|
| 260 | } |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | |
---|
[961] | 264 | G4String G4ShellEMDataSet::FullFileName(const G4String& fileName) const |
---|
[819] | 265 | { |
---|
| 266 | char* path = getenv("G4LEDATA"); |
---|
| 267 | if (!path) |
---|
| 268 | G4Exception("G4ShellEMDataSet::FullFileName - G4LEDATA environment variable not set"); |
---|
| 269 | |
---|
| 270 | std::ostringstream fullFileName; |
---|
| 271 | |
---|
[961] | 272 | fullFileName << path << '/' << fileName << z << ".dat"; |
---|
[819] | 273 | |
---|
| 274 | return G4String(fullFileName.str().c_str()); |
---|
| 275 | } |
---|