[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 | |
---|
| 28 | // $Id: G4DNACrossSectionDataSet.cc,v 1.7 2007/11/09 18:06:26 pia Exp $ |
---|
| 29 | // GEANT4 tag $Name: $ |
---|
| 30 | // |
---|
| 31 | // Author: Riccardo Capra <capra@ge.infn.it> |
---|
| 32 | // Code review by MGP October 2007: removed inheritance from concrete class |
---|
| 33 | // more improvements needed |
---|
| 34 | // |
---|
| 35 | // History: |
---|
| 36 | // ----------- |
---|
| 37 | // 30 Jun 2005 RC Created |
---|
| 38 | // 14 Oct 2007 MGP Removed inheritance from concrete class G4ShellEMDataSet |
---|
| 39 | |
---|
| 40 | |
---|
| 41 | #include "G4DNACrossSectionDataSet.hh" |
---|
| 42 | #include "G4VDataSetAlgorithm.hh" |
---|
| 43 | #include "G4EMDataSet.hh" |
---|
| 44 | #include <vector> |
---|
| 45 | #include <fstream> |
---|
| 46 | #include <sstream> |
---|
| 47 | |
---|
| 48 | |
---|
| 49 | G4DNACrossSectionDataSet::G4DNACrossSectionDataSet(G4VDataSetAlgorithm* argAlgorithm, |
---|
| 50 | G4double argUnitEnergies, |
---|
| 51 | G4double argUnitData) |
---|
| 52 | : |
---|
| 53 | algorithm(argAlgorithm), unitEnergies(argUnitEnergies), unitData(argUnitData) |
---|
| 54 | { |
---|
| 55 | z = 0; |
---|
| 56 | |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | |
---|
| 60 | G4DNACrossSectionDataSet::~G4DNACrossSectionDataSet() |
---|
| 61 | { |
---|
| 62 | CleanUpComponents(); |
---|
| 63 | |
---|
| 64 | if (algorithm) |
---|
| 65 | delete algorithm; |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | G4bool G4DNACrossSectionDataSet::LoadData(const G4String & argFileName) |
---|
| 70 | { |
---|
| 71 | CleanUpComponents(); |
---|
| 72 | |
---|
| 73 | G4String fullFileName(FullFileName(argFileName)); |
---|
| 74 | std::ifstream in(fullFileName, std::ifstream::binary|std::ifstream::in); |
---|
| 75 | |
---|
| 76 | if (!in.is_open()) |
---|
| 77 | { |
---|
| 78 | G4String message("G4DNACrossSectionDataSet::LoadData - data file \""); |
---|
| 79 | message+=fullFileName; |
---|
| 80 | message+="\" not found"; |
---|
| 81 | G4Exception(message); |
---|
| 82 | return false; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | std::vector<G4DataVector *> columns; |
---|
| 86 | std::stringstream *stream(new std::stringstream); |
---|
| 87 | char c; |
---|
| 88 | G4bool comment(false); |
---|
| 89 | G4bool space(true); |
---|
| 90 | G4bool first(true); |
---|
| 91 | |
---|
| 92 | try |
---|
| 93 | { |
---|
| 94 | while (!in.eof()) |
---|
| 95 | { |
---|
| 96 | in.get(c); |
---|
| 97 | |
---|
| 98 | switch (c) |
---|
| 99 | { |
---|
| 100 | case '\r': |
---|
| 101 | case '\n': |
---|
| 102 | if (!first) |
---|
| 103 | { |
---|
| 104 | unsigned long i(0); |
---|
| 105 | G4double value; |
---|
| 106 | |
---|
| 107 | while (!stream->eof()) |
---|
| 108 | { |
---|
| 109 | (*stream) >> value; |
---|
| 110 | |
---|
| 111 | while (i>=columns.size()) |
---|
| 112 | columns.push_back(new G4DataVector); |
---|
| 113 | |
---|
| 114 | columns[i]->push_back(value); |
---|
| 115 | |
---|
| 116 | i++; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | delete stream; |
---|
| 120 | stream=new std::stringstream; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | first=true; |
---|
| 124 | comment=false; |
---|
| 125 | space=true; |
---|
| 126 | break; |
---|
| 127 | |
---|
| 128 | case '#': |
---|
| 129 | comment=true; |
---|
| 130 | break; |
---|
| 131 | |
---|
| 132 | case '\t': |
---|
| 133 | c=' '; |
---|
| 134 | case ' ': |
---|
| 135 | if (space) |
---|
| 136 | break; |
---|
| 137 | default: |
---|
| 138 | if (comment) |
---|
| 139 | break; |
---|
| 140 | |
---|
| 141 | if (c==' ') |
---|
| 142 | space=true; |
---|
| 143 | else |
---|
| 144 | { |
---|
| 145 | if (space && (!first)) |
---|
| 146 | (*stream) << ' '; |
---|
| 147 | |
---|
| 148 | first=false; |
---|
| 149 | (*stream) << c; |
---|
| 150 | space=false; |
---|
| 151 | } |
---|
| 152 | } |
---|
| 153 | } |
---|
| 154 | } |
---|
| 155 | catch(const std::ios::failure &e) |
---|
| 156 | { |
---|
| 157 | // some implementations of STL could throw a "failture" exception |
---|
| 158 | // when read wants read characters after end of file |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | delete stream; |
---|
| 162 | |
---|
| 163 | std::vector<G4DataVector *>::size_type maxI(columns.size()); |
---|
| 164 | |
---|
| 165 | if (maxI<2) |
---|
| 166 | { |
---|
| 167 | G4String message("G4DNACrossSectionDataSet::LoadData - data file \""); |
---|
| 168 | message+=fullFileName; |
---|
| 169 | message+="\" should have at least two columns"; |
---|
| 170 | G4Exception(message); |
---|
| 171 | return false; |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | std::vector<G4DataVector*>::size_type i(1); |
---|
| 175 | while (i<maxI) |
---|
| 176 | { |
---|
| 177 | G4DataVector::size_type maxJ(columns[i]->size()); |
---|
| 178 | |
---|
| 179 | if (maxJ!=columns[0]->size()) |
---|
| 180 | { |
---|
| 181 | G4String message("G4DNACrossSectionDataSet::LoadData - data file \""); |
---|
| 182 | message+=fullFileName; |
---|
| 183 | message+="\" has lines with a different number of columns"; |
---|
| 184 | G4Exception(message); |
---|
| 185 | return false; |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | G4DataVector::size_type j(0); |
---|
| 189 | G4DataVector *argEnergies=new G4DataVector; |
---|
| 190 | G4DataVector *argData=new G4DataVector; |
---|
| 191 | |
---|
| 192 | while(j<maxJ) |
---|
| 193 | { |
---|
| 194 | argEnergies->push_back(columns[0]->operator[] (j)*GetUnitEnergies()); |
---|
| 195 | argData->push_back(columns[i]->operator[] (j)*GetUnitData()); |
---|
| 196 | j++; |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | AddComponent(new G4EMDataSet(i-1, argEnergies, argData, GetAlgorithm()->Clone(), GetUnitEnergies(), GetUnitData())); |
---|
| 200 | |
---|
| 201 | i++; |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | i=maxI; |
---|
| 205 | while (i>0) |
---|
| 206 | { |
---|
| 207 | i--; |
---|
| 208 | delete columns[i]; |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | return true; |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | |
---|
| 215 | |
---|
| 216 | G4bool G4DNACrossSectionDataSet::SaveData(const G4String & argFileName) const |
---|
| 217 | { |
---|
| 218 | const size_t n(NumberOfComponents()); |
---|
| 219 | |
---|
| 220 | if (n==0) |
---|
| 221 | { |
---|
| 222 | G4Exception("G4EMDataSet::SaveData - expected at least one component"); |
---|
| 223 | return false; |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | G4String fullFileName(FullFileName(argFileName)); |
---|
| 227 | std::ofstream out(fullFileName); |
---|
| 228 | |
---|
| 229 | if (!out.is_open()) |
---|
| 230 | { |
---|
| 231 | G4String message("G4EMDataSet::SaveData - cannot open \""); |
---|
| 232 | message+=fullFileName; |
---|
| 233 | message+="\""; |
---|
| 234 | G4Exception(message); |
---|
| 235 | return false; |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | G4DataVector::const_iterator iEnergies(GetComponent(0)->GetEnergies(0).begin()); |
---|
| 239 | G4DataVector::const_iterator iEnergiesEnd(GetComponent(0)->GetEnergies(0).end()); |
---|
| 240 | G4DataVector::const_iterator * iData(new G4DataVector::const_iterator[n]); |
---|
| 241 | |
---|
| 242 | size_t k(n); |
---|
| 243 | |
---|
| 244 | while (k>0) |
---|
| 245 | { |
---|
| 246 | k--; |
---|
| 247 | iData[k]=GetComponent(k)->GetData(0).begin(); |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | while (iEnergies!=iEnergiesEnd) |
---|
| 251 | { |
---|
| 252 | out.precision(10); |
---|
| 253 | out.width(15); |
---|
| 254 | out.setf(std::ofstream::left); |
---|
| 255 | out << ((*iEnergies)/GetUnitEnergies()); |
---|
| 256 | |
---|
| 257 | k=0; |
---|
| 258 | |
---|
| 259 | while (k<n) |
---|
| 260 | { |
---|
| 261 | out << ' '; |
---|
| 262 | out.precision(10); |
---|
| 263 | out.width(15); |
---|
| 264 | out.setf(std::ofstream::left); |
---|
| 265 | out << ((*(iData[k]))/GetUnitData()); |
---|
| 266 | |
---|
| 267 | iData[k]++; |
---|
| 268 | k++; |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | out << std::endl; |
---|
| 272 | |
---|
| 273 | iEnergies++; |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | delete[] iData; |
---|
| 277 | |
---|
| 278 | return true; |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | |
---|
| 282 | G4String G4DNACrossSectionDataSet::FullFileName(const G4String& argFileName) const |
---|
| 283 | { |
---|
| 284 | char* path = getenv("G4LEDATA"); |
---|
| 285 | if (!path) |
---|
| 286 | G4Exception("G4DNACrossSectionDataSet::FullFileName - G4LEDATA environment variable not set"); |
---|
| 287 | |
---|
| 288 | std::ostringstream fullFileName; |
---|
| 289 | |
---|
| 290 | fullFileName << path << '/' << argFileName << ".dat"; |
---|
| 291 | |
---|
| 292 | return G4String(fullFileName.str().c_str()); |
---|
| 293 | } |
---|
| 294 | |
---|
| 295 | |
---|
| 296 | G4double G4DNACrossSectionDataSet::FindValue(G4double argEnergy, G4int /* argComponentId */) const |
---|
| 297 | { |
---|
| 298 | // Returns the sum over the shells corresponding to e |
---|
| 299 | G4double value = 0.; |
---|
| 300 | |
---|
| 301 | std::vector<G4VEMDataSet *>::const_iterator i(components.begin()); |
---|
| 302 | std::vector<G4VEMDataSet *>::const_iterator end(components.end()); |
---|
| 303 | |
---|
| 304 | while (i!=end) |
---|
| 305 | { |
---|
| 306 | value+=(*i)->FindValue(argEnergy); |
---|
| 307 | i++; |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | return value; |
---|
| 311 | } |
---|
| 312 | |
---|
| 313 | |
---|
| 314 | void G4DNACrossSectionDataSet::PrintData(void) const |
---|
| 315 | { |
---|
| 316 | const size_t n(NumberOfComponents()); |
---|
| 317 | |
---|
| 318 | G4cout << "The data set has " << n << " components" << G4endl; |
---|
| 319 | G4cout << G4endl; |
---|
| 320 | |
---|
| 321 | size_t i(0); |
---|
| 322 | |
---|
| 323 | while (i<n) |
---|
| 324 | { |
---|
| 325 | G4cout << "--- Component " << i << " ---" << G4endl; |
---|
| 326 | GetComponent(i)->PrintData(); |
---|
| 327 | i++; |
---|
| 328 | } |
---|
| 329 | } |
---|
| 330 | |
---|
| 331 | |
---|
| 332 | void G4DNACrossSectionDataSet::SetEnergiesData(G4DataVector* argEnergies, |
---|
| 333 | G4DataVector* argData, |
---|
| 334 | G4int argComponentId) |
---|
| 335 | { |
---|
| 336 | G4VEMDataSet * component(components[argComponentId]); |
---|
| 337 | |
---|
| 338 | if (component) |
---|
| 339 | { |
---|
| 340 | component->SetEnergiesData(argEnergies, argData, 0); |
---|
| 341 | return; |
---|
| 342 | } |
---|
| 343 | |
---|
| 344 | std::ostringstream message; |
---|
| 345 | message << "G4DNACrossSectionDataSet::SetEnergiesData - component " << argComponentId << " not found"; |
---|
| 346 | |
---|
| 347 | G4Exception(message.str().c_str()); |
---|
| 348 | } |
---|
| 349 | |
---|
| 350 | |
---|
| 351 | void G4DNACrossSectionDataSet::CleanUpComponents() |
---|
| 352 | { |
---|
| 353 | while (!components.empty()) |
---|
| 354 | { |
---|
| 355 | if (components.back()) delete components.back(); |
---|
| 356 | components.pop_back(); |
---|
| 357 | } |
---|
| 358 | } |
---|
| 359 | |
---|
| 360 | |
---|