Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 // 26 // 27 /* 27 /* 28 // Code developed by: 28 // Code developed by: 29 // S.Guatelli, susanna@uow.edu.au 29 // S.Guatelli, susanna@uow.edu.au 30 // 30 // 31 Original code from geant4/examples/extended/ru 31 Original code from geant4/examples/extended/runAndEvent/RE03, by M. Asai 32 */ 32 */ 33 33 34 #include "BrachyUserScoreWriter.hh" 34 #include "BrachyUserScoreWriter.hh" 35 #include "G4AnalysisManager.hh" << 35 #include "BrachyAnalysis.hh" 36 #include "G4MultiFunctionalDetector.hh" 36 #include "G4MultiFunctionalDetector.hh" 37 #include "G4SDParticleFilter.hh" 37 #include "G4SDParticleFilter.hh" 38 #include "G4VPrimitiveScorer.hh" 38 #include "G4VPrimitiveScorer.hh" 39 #include "G4VScoringMesh.hh" 39 #include "G4VScoringMesh.hh" 40 #include "G4SystemOfUnits.hh" << 40 41 #include <map> 41 #include <map> 42 #include <fstream> 42 #include <fstream> >> 43 43 // The default output is 44 // The default output is 44 // voxelX, voxelY, voxelZ, edep 45 // voxelX, voxelY, voxelZ, edep 45 // The BrachyUserScoreWriter allows to change 46 // The BrachyUserScoreWriter allows to change the format of the output file. 46 // in the specific case: 47 // in the specific case: 47 // xx (mm) yy(mm) zz(mm) edep(keV) 48 // xx (mm) yy(mm) zz(mm) edep(keV) >> 49 48 // The same information is stored in a ntuple, 50 // The same information is stored in a ntuple, in the 49 // brachytherapy.root file 51 // brachytherapy.root file 50 52 51 BrachyUserScoreWriter::BrachyUserScoreWriter() << 53 BrachyUserScoreWriter::BrachyUserScoreWriter(): G4VScoreWriter() 52 G4VScoreWriter() << 54 {;} 53 { << 54 } << 55 55 56 BrachyUserScoreWriter::~BrachyUserScoreWriter( 56 BrachyUserScoreWriter::~BrachyUserScoreWriter() 57 {;} 57 {;} 58 58 59 void BrachyUserScoreWriter::DumpQuantityToFile << 59 void BrachyUserScoreWriter::DumpQuantityToFile(G4String & psName, G4String & fileName, G4String & option) 60 << 61 << 62 { 60 { 63 using MeshScoreMap = G4VScoringMesh::MeshScore << 61 if(verboseLevel > 0) { >> 62 G4cout << "BrachyUserScorer-defined DumpQuantityToFile() method is invoked." >> 63 << G4endl; } >> 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 -> " >> 76 << fileName << G4endl; >> 77 return; >> 78 } >> 79 ofile << "# mesh name: " << fScoringMesh->GetWorldName() << G4endl; 64 80 65 if(verboseLevel > 0) << 81 // retrieve the map 66 {G4cout << "BrachyUserScorer-defined DumpQua << 82 MeshScoreMap fSMap = fScoringMesh -> GetScoreMap(); 67 << G4endl; << 83 >> 84 MeshScoreMap::const_iterator msMapItr = fSMap.find(psName); >> 85 if(msMapItr == fSMap.end()) { >> 86 G4cerr << "ERROR : DumpToFile : Unknown quantity, \"" >> 87 << psName << "\"." << G4endl; >> 88 return; 68 } 89 } >> 90 std::map<G4int, G4double*> * score = msMapItr -> second-> GetMap(); >> 91 ofile << "# primitive scorer name: " << msMapItr -> first << G4endl; 69 92 70 // change the option string into lowercase to << 71 G4String opt = option; << 72 std::transform(opt.begin(), opt.end(), opt.beg << 73 93 74 // confirm the option << 94 G4AnalysisManager* analysisManager = G4AnalysisManager::Instance(); 75 if(opt.size() == 0) opt = "csv"; << 76 95 77 // open the file << 96 // 78 std::ofstream ofile(fileName); << 97 // Open a ROOT output file 79 << 98 // 80 if(!ofile) << 81 { << 82 G4cerr << "ERROR : DumpToFile : File open e << 83 return; << 84 } << 85 ofile << "# mesh name: " << fScoringMesh->Ge << 86 99 87 // retrieve the map << 100 analysisManager -> OpenFile("brachytherapy"); 88 MeshScoreMap fSMap = fScoringMesh -> GetScoreM << 101 89 << 102 // 90 auto msMapItr = fSMap.find(psName); << 103 // Creating ntuple 91 << 104 // 92 if(msMapItr == fSMap.end()) << 105 analysisManager -> CreateNtuple("EnergyDeposition", "Edep(keV) in the phantom"); 93 { << 106 analysisManager -> CreateNtupleDColumn("xx"); 94 G4cerr << "ERROR : DumpToFile : Unknown qua << 107 analysisManager -> CreateNtupleDColumn("yy"); 95 << "\"." << G4endl; << 108 analysisManager -> CreateNtupleDColumn("zz"); 96 return; << 109 analysisManager -> CreateNtupleDColumn("edep"); 97 } << 110 analysisManager -> FinishNtuple(); >> 111 >> 112 // >> 113 // Write quantity in the ASCII output file and in brachytherapy.root >> 114 // >> 115 ofile << std::setprecision(16); // for double value with 8 bytes >> 116 for(int x = 0; x < fNMeshSegments[0]; x++) { >> 117 for(int y = 0; y < fNMeshSegments[1]; y++) { >> 118 for(int z = 0; z < fNMeshSegments[2]; z++) { >> 119 >> 120 G4int numberOfVoxel = fNMeshSegments[0]; >> 121 >> 122 // If the voxel width is changed in the macro file, >> 123 // the voxel width variable must be updated >> 124 G4double voxelWidth = 1. *mm; >> 125 // >> 126 >> 127 G4double xx = ( - numberOfVoxel + 1+ 2*x )* voxelWidth/2; >> 128 G4double yy = ( - numberOfVoxel + 1+ 2*y )* voxelWidth/2; >> 129 G4double zz = ( - numberOfVoxel + 1+ 2*z )* voxelWidth/2; 98 130 99 auto score = msMapItr-> second-> GetMap(); << 100 << 101 ofile << "# primitive scorer name: " << msMapI << 102 // << 103 // Write quantity in the ASCII output file and << 104 // << 105 ofile << std::setprecision(16); // for double << 106 << 107 auto analysisManager = G4AnalysisManager::Inst << 108 << 109 G4bool fileOpen = analysisManager -> OpenFile( << 110 if (! fileOpen) { << 111 G4cerr << "\n---> The ROOT output file has << 112 << analysisManager->GetFileName() < << 113 } << 114 << 115 G4cout << "Using " << analysisManager -> GetTy << 116 analysisManager -> SetVerboseLevel(1); << 117 analysisManager -> SetActivation(true); << 118 << 119 // Create histograms << 120 G4int histo2= analysisManager-> CreateH2("h20" << 121 << 122 // Histo 0 with the energy spectrum will not b << 123 // in brachytherapy.root << 124 analysisManager->SetH1Activation(0, false); << 125 analysisManager->SetH2Activation(histo2, true) << 126 << 127 for(int x = 0; x < fNMeshSegments[0]; x++) { << 128 for(int y = 0; y < fNMeshSegments[1]; y++) << 129 for(int z = 0; z < fNMeshSegments[2]; z++ << 130 G4int numberOfVoxel_x = fNMeshSegments << 131 G4int numberOfVoxel_y = fNMeshSegments << 132 G4int numberOfVoxel_z =fNMeshSegments[ << 133 // If the voxel width is changed in th << 134 // the voxel width variable must be up << 135 G4double voxelWidth = 0.25 *CLHEP::mm; << 136 // << 137 G4double xx = ( - numberOfVoxel_x + 1+ << 138 G4double yy = ( - numberOfVoxel_y + 1+ << 139 G4double zz = ( - numberOfVoxel_z + 1+ << 140 G4int idx = GetIndex(x, y, z); 131 G4int idx = GetIndex(x, y, z); 141 std::map<G4int, G4StatDouble*>::iterat << 132 142 << 133 std::map<G4int, G4double*>::iterator value = score -> find(idx); 143 if (value != score -> end()) << 134 >> 135 if (value != score -> end()) 144 { 136 { 145 // Print in the ASCII output file the << 137 // Print in the ASCII output file the information 146 << 138 ofile << xx << " " << yy << " " << zz <<" " <<*(value->second)/keV << G4endl; 147 ofile << xx << " " << yy << " " << << 139 148 <<(value->second->sum_wx())/keV << 140 // Save the same information in the output analysis file 149 << 141 analysisManager = G4AnalysisManager::Instance(); 150 // Save the same information in the RO << 142 analysisManager -> FillNtupleDColumn(0, xx); 151 << 143 analysisManager -> FillNtupleDColumn(1, yy); 152 if(zz> -0.125 *CLHEP::mm && zz < 0.125/mm) << 144 analysisManager -> FillNtupleDColumn(2, zz); 153 analysisManager->FillH2(histo2, xx, y << 145 analysisManager -> FillNtupleDColumn(3, *(value->second)/keV); 154 }}}} << 146 analysisManager -> AddNtupleRow(); 155 << 147 } 156 ofile << std::setprecision(6); << 148 } 157 << 149 } 158 // Close the output ASCII file << 150 } 159 ofile.close(); << 151 ofile << std::setprecision(6); 160 << 152 161 // Close the output ROOT file << 153 // Close the output ASCII file 162 analysisManager -> Write(); << 154 ofile.close(); 163 analysisManager -> CloseFile(); << 155 >> 156 // Close the output brachytherapy.root >> 157 analysisManager -> Write(); >> 158 analysisManager -> CloseFile(); 164 } 159 } >> 160 165 161