Geant4 Cross Reference |
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 /// \file runAndEvent/RE03/src/RE03UserScoreWriter.cc 27 /// \brief Implementation of the RE03UserScoreWriter class 28 // 29 // 30 31 #include "RE03UserScoreWriter.hh" 32 33 #include "G4MultiFunctionalDetector.hh" 34 #include "G4SDParticleFilter.hh" 35 #include "G4VPrimitiveScorer.hh" 36 #include "G4VScoringMesh.hh" 37 38 #include <fstream> 39 #include <map> 40 41 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 42 RE03UserScoreWriter::RE03UserScoreWriter() : G4VScoreWriter() 43 { 44 ; 45 } 46 47 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 48 RE03UserScoreWriter::~RE03UserScoreWriter() 49 { 50 ; 51 } 52 53 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 54 void RE03UserScoreWriter::DumpQuantityToFile(const G4String& psName, const G4String& fileName, 55 const G4String& option) 56 { 57 using MeshScoreMap = G4VScoringMesh::MeshScoreMap; 58 // 59 if (verboseLevel > 0) { 60 G4cout << "User-defined DumpQuantityToFile() method is invoked." << G4endl; 61 G4cout << " -- to obtain a projection of the quantity <" << psName << "> onto the x-y plane --" 62 << G4endl; 63 } 64 65 // change the option string into lowercase to the case-insensitive. 66 G4String opt = option; 67 std::transform(opt.begin(), opt.end(), opt.begin(), (int (*)(int))(tolower)); 68 69 // confirm the option 70 if (opt.size() == 0) opt = "csv"; 71 72 // open the file 73 std::ofstream ofile(fileName); 74 if (!ofile) { 75 G4cerr << "ERROR : DumpToFile : File open error -> " << fileName << G4endl; 76 return; 77 } 78 ofile << "# mesh name: " << fScoringMesh->GetWorldName() << G4endl; 79 80 // retrieve the map 81 MeshScoreMap scMap = fScoringMesh->GetScoreMap(); 82 83 MeshScoreMap::const_iterator msMapItr = scMap.find(psName); 84 if (msMapItr == scMap.end()) { 85 G4cerr << "ERROR : DumpToFile : Unknown quantity, \"" << psName << "\"." << G4endl; 86 return; 87 } 88 std::map<G4int, G4StatDouble*>* score = msMapItr->second->GetMap(); 89 ofile << "# primitive scorer name: " << msMapItr->first << G4endl; 90 91 // write header info 92 ofile << "# xy projection" << G4endl; 93 ofile << fNMeshSegments[0] << " " << fNMeshSegments[1] << " " << G4endl; 94 95 // declare xy array 96 std::vector<double> projy; 97 for (int y = 0; y < fNMeshSegments[1]; y++) 98 projy.push_back(0.); 99 std::vector<std::vector<double>> projxy; 100 for (int x = 0; x < fNMeshSegments[0]; x++) 101 projxy.push_back(projy); 102 // accumulate 103 ofile << std::setprecision(16); // for double value with 8 bytes 104 for (int x = 0; x < fNMeshSegments[0]; x++) { 105 for (int y = 0; y < fNMeshSegments[1]; y++) { 106 for (int z = 0; z < fNMeshSegments[2]; z++) { 107 G4int idx = GetIndex(x, y, z); 108 109 std::map<G4int, G4StatDouble*>::iterator value = score->find(idx); 110 if (value != score->end()) projxy[x][y] += value->second->sum_wx(); 111 112 } // z 113 } // y 114 } // x 115 116 // write quantity 117 ofile << std::setprecision(16); // for double value with 8 bytes 118 for (int x = 0; x < fNMeshSegments[0]; x++) { 119 for (int y = 0; y < fNMeshSegments[1]; y++) { 120 ofile << x << "," << y << ","; 121 ofile << projxy[x][y] << G4endl; 122 123 } // y 124 } // x 125 ofile << std::setprecision(6); 126 127 // close the file 128 ofile.close(); 129 } 130