Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:16:37

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
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     // Rotate all scoring meshes to the right position
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                //*storedScoreMap[hitMapName] += *(it->second);
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   //G4cout << "Enter ReadFile()...";
0172   const int SZ = 100;
0173   char buf[SZ];
0174   
0175   phiAngles.clear();    // If called for the second time
0176   thetaAngles.clear();  // we won't have 402 positions...
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       /* Skip the "Axx" at the beginning of the line */
0186       for (G4int c = 0; c < 4; c++) ifs.get();
0187       
0188       ifs >> phi >> theta;
0189       ifs.getline(buf, SZ); // Next line
0190       
0191       phiAngles.push_back( phi * degree );
0192       thetaAngles.push_back( theta * degree );
0193     }
0194   ifs.close();
0195   //G4cout << "... done " << G4endl;
0196 }