Geant4 Cross Reference |
1 // 1 2 // ******************************************* 3 // * License and Disclaimer 4 // * 5 // * The Geant4 software is copyright of th 6 // * the Geant4 Collaboration. It is provided 7 // * conditions of the Geant4 Software License 8 // * LICENSE and available at http://cern.ch/ 9 // * include a list of copyright holders. 10 // * 11 // * Neither the authors of this software syst 12 // * institutes,nor the agencies providing fin 13 // * work make any representation or warran 14 // * regarding this software system or assum 15 // * use. Please see the license in the file 16 // * for the full disclaimer and the limitatio 17 // * 18 // * This code implementation is the result 19 // * technical work of the GEANT4 collaboratio 20 // * By using, copying, modifying or distri 21 // * any work based on the software) you ag 22 // * use in resulting scientific publicati 23 // * acceptance of all terms of the Geant4 Sof 24 // ******************************************* 25 // 26 // 27 28 // 29 // Author: Riccardo Capra <capra@ge.infn.it> 30 // Code review by MGP October 2007: removed in 31 // more impro 32 // 33 // History: 34 // ----------- 35 // 30 Jun 2005 RC Created 36 // 14 Oct 2007 MGP Removed inheritan 37 // 38 // 15 Jul 2009 N.A.Karakatsanis 39 // 40 // - LoadNonLogData 41 // dataset. It is 42 // 43 // - LoadData method 44 // It retrieves th 45 // respective log 46 // 47 // - SetLogEnergiesD 48 // The EM data set 49 // These initializ 50 // operations 51 // 52 // 53 // ------------------------------------------- 54 55 56 #include "G4DNACrossSectionDataSet.hh" 57 #include "G4VDataSetAlgorithm.hh" 58 #include "G4EMDataSet.hh" 59 #include <vector> 60 #include <fstream> 61 #include <sstream> 62 63 64 G4DNACrossSectionDataSet::G4DNACrossSectionDat 65 G4double argUnitEnergies, 66 G4double argUnitData) 67 : 68 algorithm(argAlgorithm), unitEnergies(argUn 69 { 70 z = 0; 71 72 } 73 74 75 G4DNACrossSectionDataSet::~G4DNACrossSectionDa 76 { 77 CleanUpComponents(); 78 79 80 delete algorithm; 81 } 82 83 G4bool G4DNACrossSectionDataSet::LoadData(cons 84 { 85 CleanUpComponents(); 86 87 G4String fullFileName(FullFileName(argFileNa 88 std::ifstream in(fullFileName, std::ifstream 89 90 if (!in.is_open()) 91 { 92 G4String message("Data file \""); 93 message+=fullFileName; 94 message+="\" not found"; 95 G4Exception("G4DNACrossSectionDataSet::L 96 FatalException,message); 97 return false; 98 } 99 100 std::vector<G4DataVector *> columns; 101 std::vector<G4DataVector *> log_columns; 102 103 auto stream(new std::stringstream); 104 char c; 105 G4bool comment(false); 106 G4bool space(true); 107 G4bool first(true); 108 109 try 110 { 111 while (!in.eof()) 112 { 113 in.get(c); 114 115 switch (c) 116 { 117 case '\r': 118 case '\n': 119 if (!first) 120 { 121 unsigned long i(0); 122 G4double value; 123 124 while (!stream->eof()) 125 { 126 (*stream) >> value; 127 128 while (i>=columns.size()) 129 { 130 columns.push_back(new G4DataVector); 131 log_columns.push_back 132 } 133 134 columns[i]->push_back(value); 135 136 // N. A. Karakatsanis 137 // A condition is applied to check if negative 138 // If yes, then a near-zero value is applied t 139 // If a value is zero, this simplification is 140 // If a value is negative, then it is not acce 141 // logarithmic values should not be used by in 142 // 143 // Therefore, G4LogLogInterpolation and G4LinL 144 // Instead, G4LinInterpolation is safe in ever 145 // SemiLogInterpolation is safe only if the en 146 // G4LinLogInterpolation is safe only if the c 147 148 if (value <=0.) value = 149 log_columns[i]->push_bac 150 151 i++; 152 } 153 154 delete stream; 155 stream=new std::stringstream; 156 } 157 158 first=true; 159 comment=false; 160 space=true; 161 break; 162 163 case '#': 164 comment=true; 165 break; 166 167 case '\t': 168 case ' ': 169 space = true; 170 break; 171 172 default: 173 if (comment) { break; } 174 if (space && (!first)) { (*stream) << 175 176 first=false; 177 (*stream) << c; 178 space=false; 179 } 180 } 181 } 182 catch(const std::ios::failure &e) 183 { 184 // some implementations of STL could thr 185 // when read wants read characters after 186 } 187 188 delete stream; 189 190 std::size_t maxI(columns.size()); 191 192 if (maxI<2) 193 { 194 G4String message("Data file \""); 195 message+=fullFileName; 196 message+="\" should have at least two co 197 G4Exception("G4DNACrossSectionDataSet::L 198 FatalException,message); 199 return false; 200 } 201 202 std::size_t i(1); 203 while (i<maxI) 204 { 205 std::size_t maxJ(columns[i]->size()); 206 207 if (maxJ!=columns[0]->size()) 208 { 209 G4String message("Data file \""); 210 message+=fullFileName; 211 message+="\" has lines with a different nu 212 G4Exception("G4DNACrossSectionDataSet::Loa 213 FatalException,message); 214 return false; 215 } 216 217 std::size_t j(0); 218 219 auto argEnergies=new G4DataVector; 220 auto argData=new G4DataVector; 221 auto argLogEnergies=new G4DataVector; 222 auto argLogData=new G4DataVector; 223 224 while(j<maxJ) 225 { 226 argEnergies->push_back(columns[0]->operato 227 argData->push_back(columns[i]->operator[] 228 argLogEnergies->push_back(log_columns[0]-> 229 argLogData->push_back(log_columns[i]->oper 230 j++; 231 } 232 233 AddComponent(new G4EMDataSet(G4int(i-1), 234 235 i++; 236 } 237 238 i=maxI; 239 while (i>0) 240 { 241 i--; 242 delete columns[i]; 243 delete log_columns[i]; 244 } 245 246 return true; 247 } 248 249 250 G4bool G4DNACrossSectionDataSet::LoadNonLogDat 251 { 252 CleanUpComponents(); 253 254 G4String fullFileName(FullFileName(argFileNa 255 std::ifstream in(fullFileName, std::ifstream 256 257 if (!in.is_open()) 258 { 259 G4String message("Data file \""); 260 message+=fullFileName; 261 message+="\" not found"; 262 G4Exception("G4DNACrossSectionDataSet::L 263 FatalException,message); 264 return false; 265 } 266 267 std::vector<G4DataVector *> columns; 268 269 auto stream(new std::stringstream); 270 char c; 271 G4bool comment(false); 272 G4bool space(true); 273 G4bool first(true); 274 275 try 276 { 277 while (!in.eof()) 278 { 279 in.get(c); 280 281 switch (c) 282 { 283 case '\r': 284 case '\n': 285 if (!first) 286 { 287 unsigned long i(0); 288 G4double value; 289 290 while (!stream->eof()) 291 { 292 (*stream) >> value; 293 294 while (i>=columns.size()) 295 { 296 columns.push_back(new G4DataVector); 297 } 298 299 columns[i]->push_back(value); 300 301 i++; 302 } 303 304 delete stream; 305 stream=new std::stringstream; 306 } 307 308 first=true; 309 comment=false; 310 space=true; 311 break; 312 313 case '#': 314 comment=true; 315 break; 316 317 case '\t': 318 case ' ': 319 space = true; 320 break; 321 322 default: 323 if (comment) { break; } 324 if (space && (!first)) { (*stream) << 325 326 first=false; 327 (*stream) << c; 328 space=false; 329 } 330 } 331 } 332 catch(const std::ios::failure &e) 333 { 334 // some implementations of STL could thr 335 // when read wants read characters after 336 } 337 338 delete stream; 339 340 std::size_t maxI(columns.size()); 341 342 if (maxI<2) 343 { 344 G4String message("Data file \""); 345 message+=fullFileName; 346 message+="\" should have at least two co 347 G4Exception("G4DNACrossSectionDataSet::L 348 FatalException,message); 349 return false; 350 } 351 352 std::size_t i(1); 353 while (i<maxI) 354 { 355 std::size_t maxJ(columns[i]->size()); 356 357 if (maxJ!=columns[0]->size()) 358 { 359 G4String message("Data file \""); 360 message+=fullFileName; 361 message+="\" has lines with a different nu 362 G4Exception("G4DNACrossSectionDataSe 363 FatalException,message); 364 return false; 365 } 366 367 std::size_t j(0); 368 369 auto argEnergies=new G4DataVector; 370 auto argData=new G4DataVector; 371 372 while(j<maxJ) 373 { 374 argEnergies->push_back(columns[0]->operato 375 argData->push_back(columns[i]->operator[] 376 j++; 377 } 378 379 AddComponent(new G4EMDataSet(G4int(i-1), 380 381 i++; 382 } 383 384 i=maxI; 385 while (i>0) 386 { 387 i--; 388 delete columns[i]; 389 } 390 391 return true; 392 } 393 394 395 G4bool G4DNACrossSectionDataSet::SaveData(cons 396 { 397 const std::size_t n(NumberOfComponents()); 398 399 if (n==0) 400 { 401 G4Exception("G4DNACrossSectionDataSet::S 402 FatalException,"Expected 403 404 return false; 405 } 406 407 G4String fullFileName(FullFileName(argFileNa 408 std::ofstream out(fullFileName); 409 410 if (!out.is_open()) 411 { 412 G4String message("Cannot open \""); 413 message+=fullFileName; 414 message+="\""; 415 G4Exception("G4DNACrossSectionDataSet::S 416 FatalException,message); 417 return false; 418 } 419 420 auto iEnergies(GetComponent(0)->GetEnergies( 421 auto iEnergiesEnd(GetComponent(0)->GetEnergi 422 auto iData(new G4DataVector::const_iterator 423 424 std::size_t k(n); 425 426 while (k>0) 427 { 428 k--; 429 iData[k]=GetComponent((G4int)k)->GetData 430 } 431 432 while (iEnergies!=iEnergiesEnd) 433 { 434 out.precision(10); 435 out.width(15); 436 out.setf(std::ofstream::left); 437 out << ((*iEnergies)/GetUnitEnergies()); 438 439 k=0; 440 441 while (k<n) 442 { 443 out << ' '; 444 out.precision(10); 445 out.width(15); 446 out.setf(std::ofstream::left); 447 out << ((*(iData[k]))/GetUnitData()); 448 449 iData[k]++; 450 k++; 451 } 452 453 out << std::endl; 454 455 iEnergies++; 456 } 457 458 delete[] iData; 459 460 return true; 461 } 462 463 464 G4String G4DNACrossSectionDataSet::FullFileNam 465 { 466 const char* path = G4FindDataDir("G4LEDATA") 467 if (path == nullptr) 468 { 469 G4Exception("G4DNACrossSectionDataSet::F 470 FatalException,"G4LEDATA 471 472 return ""; 473 } 474 475 std::ostringstream fullFileName; 476 477 fullFileName << path << "/" << argFileName < 478 479 return G4String(fullFileName.str().c_str()); 480 } 481 482 483 G4double G4DNACrossSectionDataSet::FindValue(G 484 { 485 // Returns the sum over the shells correspon 486 G4double value = 0.; 487 488 auto i(components.begin()); 489 auto end(components.end()); 490 491 while (i!=end) 492 { 493 value+=(*i)->FindValue(argEnergy); 494 i++; 495 } 496 497 return value; 498 } 499 500 501 void G4DNACrossSectionDataSet::PrintData() con 502 { 503 const auto n = (G4int)NumberOfComponents(); 504 505 G4cout << "The data set has " << n << " comp 506 G4cout << G4endl; 507 508 G4int i(0); 509 510 while (i<n) 511 { 512 G4cout << "--- Component " << i << " --- 513 GetComponent(i)->PrintData(); 514 ++i; 515 } 516 } 517 518 519 void G4DNACrossSectionDataSet::SetEnergiesData 520 G4DataVector* argData, 521 G4int argComponentId) 522 { 523 G4VEMDataSet * component(components[argCompo 524 525 if (component != nullptr) 526 { 527 component->SetEnergiesData(argEnergies, 528 return; 529 } 530 531 std::ostringstream message; 532 message << "Component " << argComponentId << 533 534 G4Exception("G4DNACrossSectionDataSet::SetEn 535 FatalException,message.str(). 536 537 } 538 539 540 void G4DNACrossSectionDataSet::SetLogEnergiesD 541 G4DataVector* argData, 542 G4Dat 543 G4Dat 544 G4int argComponentId) 545 { 546 G4VEMDataSet * component(components[argCompo 547 548 if (component != nullptr) 549 { 550 component->SetLogEnergiesData(argEnergie 551 return; 552 } 553 554 std::ostringstream message; 555 message << "Component " << argComponentId << 556 557 G4Exception("G4DNACrossSectionDataSet::SetLo 558 FatalException,message.str(). 559 560 } 561 562 563 void G4DNACrossSectionDataSet::CleanUpComponen 564 { 565 while (!components.empty()) 566 { 567 if (components.back() != nullptr) delete 568 components.pop_back(); 569 } 570 } 571 572 573