File indexing completed on 2025-01-18 09:16:37
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028 #include "GammaKnifeController.hh"
0029 #include "G4UImanager.hh"
0030 #include "G4RunManager.hh"
0031 #include <fstream>
0032
0033 #include "G4SystemOfUnits.hh"
0034
0035 GammaKnifeController::GammaKnifeController( GammaKnifeDetectorConstruction *det ) : scoreMaps( 0 )
0036 {
0037 detector = det;
0038
0039 messenger = new GammaKnifeMessenger( this );
0040 }
0041
0042 GammaKnifeController::~GammaKnifeController()
0043 {
0044 delete messenger;
0045 }
0046
0047 void GammaKnifeController::BeamOn( G4int n_event )
0048 {
0049 PrepareHitsAccumulation();
0050 for (G4int i = 0; i < GAMMAKNIFE_SOURCES; i++)
0051 {
0052 RotateForward(i);
0053 G4RunManager::GetRunManager()->BeamOn(n_event);
0054
0055 if (i != (GAMMAKNIFE_SOURCES - 1))
0056 StoreHits();
0057
0058 RotateBack(i);
0059 }
0060 AccumulateAllHits();
0061 }
0062
0063 void GammaKnifeController::RotateForward( G4int position )
0064 {
0065
0066 G4ScoringManager* scm = G4ScoringManager::GetScoringManagerIfExist();
0067 if (scm)
0068 {
0069 for (size_t i = 0; i < scm->GetNumberOfMesh(); i++)
0070 {
0071 G4VScoringMesh * mesh = scm->GetMesh(i);
0072 mesh->RotateX( thetaAngles[position] );
0073 mesh->RotateZ( phiAngles[position] );
0074 }
0075 }
0076 }
0077
0078 void GammaKnifeController::RotateBack( G4int position )
0079 {
0080 G4ScoringManager* scm = G4ScoringManager::GetScoringManagerIfExist();
0081 if (scm)
0082 {
0083 for (size_t i = 0; i < scm->GetNumberOfMesh(); i++)
0084 {
0085 G4VScoringMesh * mesh = scm->GetMesh(i);
0086 mesh->RotateZ( - phiAngles[position] );
0087 mesh->RotateX( - thetaAngles[position] );
0088 }
0089 }
0090 }
0091
0092 void GammaKnifeController::PrepareHitsAccumulation()
0093 {
0094 G4ScoringManager* scm = G4ScoringManager::GetScoringManagerIfExist();
0095 if (scm)
0096 {
0097 size_t size = scm->GetNumberOfMesh();
0098 scoreMaps = new MeshScoreMap[size];
0099 for (size_t i = 0; i < size; i++)
0100 {
0101 G4VScoringMesh * mesh = scm->GetMesh(i);
0102
0103 MeshScoreMap scoreMap = mesh->GetScoreMap();
0104 MeshScoreMap& storedScoreMap = scoreMaps[i];
0105
0106 MeshScoreMap::iterator it = scoreMap.begin();
0107 for( ; it != scoreMap.end(); it++)
0108 {
0109 std::string hitMapName = it->first;
0110 G4THitsMap<G4StatDouble>* hitMapToStore
0111 = new G4THitsMap<G4StatDouble>("GammaKnifeController", hitMapName);
0112 storedScoreMap[ hitMapName ] = hitMapToStore;
0113 }
0114 }
0115 }
0116 }
0117
0118 void GammaKnifeController::StoreHits()
0119 {
0120 G4ScoringManager* scm = G4ScoringManager::GetScoringManagerIfExist();
0121 if (scm)
0122 {
0123 for (size_t i = 0; i < scm->GetNumberOfMesh(); i++)
0124 {
0125 G4VScoringMesh* mesh = scm->GetMesh(i);
0126
0127
0128 MeshScoreMap scoreMap = mesh->GetScoreMap();
0129 MeshScoreMap& storedScoreMap = scoreMaps[i];
0130
0131 MeshScoreMap::iterator it = scoreMap.begin();
0132 for( ; it != scoreMap.end(); it++)
0133 {
0134 std::string hitMapName = it->first;
0135
0136 auto storedMap = storedScoreMap[hitMapName]->GetMap();
0137 auto mapItr = it->second->GetMap()->begin();
0138 for(;mapItr!=it->second->GetMap()->end();mapItr++)
0139 {
0140 auto key = mapItr->first;
0141 auto val = mapItr->second;
0142 if(storedMap->find(key)==storedMap->end())
0143 { (*storedMap)[key] = new G4StatDouble(); }
0144 (*storedMap)[key]->add(val);
0145 }
0146 }
0147 }
0148 }
0149 }
0150
0151 void GammaKnifeController::AccumulateAllHits()
0152 {
0153 G4ScoringManager* scm = G4ScoringManager::GetScoringManagerIfExist();
0154 if (scm)
0155 {
0156 for (size_t i = 0; i < scm->GetNumberOfMesh(); i++)
0157 {
0158 G4VScoringMesh* mesh = scm->GetMesh(i);
0159 MeshScoreMap& storedScoreMap = scoreMaps[i];
0160 MeshScoreMap::iterator it = storedScoreMap.begin();
0161 for( ; it != storedScoreMap.end(); it++)
0162 {
0163 mesh->Accumulate( it->second );
0164 }
0165 }
0166 }
0167 }
0168
0169 void GammaKnifeController::ReadFile( std::string fileName )
0170 {
0171
0172 const int SZ = 100;
0173 char buf[SZ];
0174
0175 phiAngles.clear();
0176 thetaAngles.clear();
0177
0178 std::ifstream ifs;
0179 ifs.open( fileName.c_str() );
0180
0181 for (G4int i = 0; i < GAMMAKNIFE_SOURCES; i++)
0182 {
0183 G4double phi, theta;
0184
0185
0186 for (G4int c = 0; c < 4; c++) ifs.get();
0187
0188 ifs >> phi >> theta;
0189 ifs.getline(buf, SZ);
0190
0191 phiAngles.push_back( phi * degree );
0192 thetaAngles.push_back( theta * degree );
0193 }
0194 ifs.close();
0195
0196 }