B1RunAction.cc 6.97 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
//
// ********************************************************************
// * License and Disclaimer                                           *
// *                                                                  *
// * The  Geant4 software  is  copyright of the Copyright Holders  of *
// * the Geant4 Collaboration.  It is provided  under  the terms  and *
// * conditions of the Geant4 Software License,  included in the file *
// * LICENSE and available at  http://cern.ch/geant4/license .  These *
// * include a list of copyright holders.                             *
// *                                                                  *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work  make  any representation or  warranty, express or implied, *
// * regarding  this  software system or assume any liability for its *
// * use.  Please see the license in the file  LICENSE  and URL above *
// * for the full disclaimer and the limitation of liability.         *
// *                                                                  *
// * This  code  implementation is the result of  the  scientific and *
// * technical work of the GEANT4 collaboration.                      *
// * By using,  copying,  modifying or  distributing the software (or *
// * any work based  on the software)  you  agree  to acknowledge its *
// * use  in  resulting  scientific  publications,  and indicate your *
// * acceptance of all terms of the Geant4 Software license.          *
// ********************************************************************
//
//
/// \file B1RunAction.cc
/// \brief Implementation of the B1RunAction class

#include "B1RunAction.hh"
#include "B1PrimaryGeneratorAction.hh"
#include "B1DetectorConstruction.hh"
// #include "B1Run.hh"
34 35
#include <stdio.h>      /* printf */
#include <math.h> 
36 37 38 39 40 41 42 43 44 45 46 47 48
#include "G4RunManager.hh"
#include "G4Run.hh"
#include "G4AccumulableManager.hh"
#include "G4LogicalVolumeStore.hh"
#include "G4LogicalVolume.hh"
#include "G4UnitsTable.hh"
#include "G4SystemOfUnits.hh"

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

B1RunAction::B1RunAction()
: G4UserRunAction(),
  fEdep1(0.),
49 50 51
  fEdep2(0.),
  fE_mup(0.),
  fE_mum(0.)
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
{ 
  // add new units for dose
  // 
  const G4double milligray = 1.e-3*gray;
  const G4double microgray = 1.e-6*gray;
  const G4double nanogray  = 1.e-9*gray;  
  const G4double picogray  = 1.e-12*gray;
   
  new G4UnitDefinition("milligray", "milliGy" , "Dose", milligray);
  new G4UnitDefinition("microgray", "microGy" , "Dose", microgray);
  new G4UnitDefinition("nanogray" , "nanoGy"  , "Dose", nanogray);
  new G4UnitDefinition("picogray" , "picoGy"  , "Dose", picogray); 

  // Register accumulable to the accumulable manager
  G4AccumulableManager* accumulableManager = G4AccumulableManager::Instance();
  accumulableManager->RegisterAccumulable(fEdep1);
68 69 70
  accumulableManager->RegisterAccumulable(fEdep2);
  accumulableManager->RegisterAccumulable(fE_mup);
  accumulableManager->RegisterAccumulable(fE_mum);
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

B1RunAction::~B1RunAction()
{}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

void B1RunAction::BeginOfRunAction(const G4Run*)
{ 
	
  // inform the runManager to save random number seed
  G4RunManager::GetRunManager()->SetRandomNumberStore(false);

  // reset accumulables to their initial values
  G4AccumulableManager* accumulableManager = G4AccumulableManager::Instance();
  accumulableManager->Reset();


}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

void B1RunAction::EndOfRunAction(const G4Run* run)
{
  G4int nofEvents = run->GetNumberOfEvent();
  if (nofEvents == 0) return;

  // Merge accumulables 
  G4AccumulableManager* accumulableManager = G4AccumulableManager::Instance();
  accumulableManager->Merge();

  // Compute dose = total energy deposit in a run and its variance
  //
  G4double edep  = fEdep1.GetValue() + fEdep2.GetValue();
  //G4double edep2 = fEdep2.GetValue();
108 109
  G4double mu_p_energy = fE_mup.GetValue();
  G4double mu_m_energy = fE_mum.GetValue();
110 111 112 113 114 115 116 117
  //G4double rms = edep2 - edep*edep/nofEvents;
 // if (rms > 0.) rms = std::sqrt(rms); else rms = 0.;  

  const B1DetectorConstruction* detectorConstruction
   = static_cast<const B1DetectorConstruction*>
     (G4RunManager::GetRunManager()->GetUserDetectorConstruction());
  //G4double mass = detectorConstruction->GetScoringVolume()->GetMass();
  G4double dose = edep/nofEvents;
118 119
  G4double mu_p_en = mu_p_energy/nofEvents;
  G4double mu_m_en = mu_m_energy/nofEvents;
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
  //G4double rmsDose = rms/mass;

  // Run conditions
  //  note: There is no primary generator action object for "master"
  //        run manager for multi-threaded mode.
  const B1PrimaryGeneratorAction* generatorAction
   = static_cast<const B1PrimaryGeneratorAction*>
     (G4RunManager::GetRunManager()->GetUserPrimaryGeneratorAction());
  G4String runCondition;
  if (generatorAction)
  {
    const G4ParticleGun* particleGun = generatorAction->GetParticleGun();
    runCondition += particleGun->GetParticleDefinition()->GetParticleName();
    runCondition += " of ";
    G4double particleEnergy = particleGun->GetParticleEnergy();
135 136 137 138
    //G4ParticleMomentum direction = particleGun->GetParticleMomentumDirection();
    G4double y = particleGun->GetParticleMomentumDirection().y();
    G4double z = particleGun->GetParticleMomentumDirection().z();
    
139
    runCondition += G4BestUnit(particleEnergy,"Energy");
140 141 142 143 144 145 146
   
   #define PI 3.14159265
    G4double angle = atan(y/z)*180/PI;

  std::ofstream mu_p_energy("energy_mu_p" + std::to_string(particleEnergy/GeV) + ".csv",std::ios_base::app);
 
  std::ofstream mu_m_energy("energy_mu_m" + std::to_string(particleEnergy/GeV) + ".csv",std::ios_base::app);
147
        
148 149 150 151 152 153 154 155 156 157
   mu_p_energy << mu_p_en/GeV << " " << angle << "\n";
   mu_m_energy << mu_m_en/GeV << " " << angle << "\n";

  mu_p_energy.close();
  mu_m_energy.close();
  }
 
  //write on files
 
  
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
  // Print
  //  
  if (IsMaster()) {
    G4cout
     << G4endl
     << "--------------------End of Global Run-----------------------";
  }
  else {
    G4cout
     << G4endl
     << "--------------------End of Local Run------------------------";
  }
  
  G4cout
     << G4endl
     << " The run consists of " << nofEvents << " "<< runCondition
     << G4endl
     << " Average Cumulated energy in volumes : " 
176
     << G4BestUnit(dose,"Energy") << G4endl
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
     << "------------------------------------------------------------"
     << G4endl
     << G4endl;
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

void B1RunAction::AddEdep1(G4double edep)
{
  fEdep1  += edep;
  
}

void B1RunAction::AddEdep2(G4double edep)
{
  fEdep2  += edep;
  
194 195 196 197 198 199 200 201 202 203
}
void B1RunAction::AddE_mup(G4double edep)
{
  fE_mup  += edep;
  
}
void B1RunAction::AddE_mum(G4double edep)
{
  fE_mum  += edep;
  
204 205 206
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......