Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-28 07:17:07

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 /// \file RunAction.cc
0027 /// \brief Implementation of the RunAction class
0028 
0029 #include "RunAction.hh"
0030 
0031 #include "DetectorConstruction.hh"
0032 #include "PhysicsList.hh"
0033 #include "PrimaryGeneratorAction.hh"
0034 #include "StepMax.hh"
0035 
0036 #include "G4Run.hh"
0037 #include "G4RunManager.hh"
0038 #include "G4SystemOfUnits.hh"
0039 #include "G4UnitsTable.hh"
0040 #include "G4ios.hh"
0041 #include "Randomize.hh"
0042 
0043 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0044 
0045 RunAction::RunAction(DetectorConstruction* det, PhysicsList* phys, PrimaryGeneratorAction* kin)
0046   : G4UserRunAction(),
0047     fAnalysisManager(0),
0048     fDetector(det),
0049     fPhysics(phys),
0050     fKinematic(kin),
0051     fTallyEdep(new G4double[kMaxTally]),
0052     fProjRange(0.),
0053     fProjRange2(0.),
0054     fEdeptot(0.),
0055     fEniel(0.),
0056     fNbPrimarySteps(0),
0057     fRange(0)
0058 {
0059   // Book predefined histograms
0060   BookHisto();
0061 }
0062 
0063 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0064 
0065 RunAction::~RunAction()
0066 {
0067   delete[] fTallyEdep;
0068 }
0069 
0070 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0071 
0072 void RunAction::BeginOfRunAction(const G4Run* aRun)
0073 {
0074   G4cout << "### Run " << aRun->GetRunID() << " start." << G4endl;
0075 
0076   if (!fAnalysisManager) {
0077     BookHisto();
0078   }
0079 
0080   CLHEP::HepRandom::showEngineStatus();
0081 
0082   // initialize projected range, tallies, Ebeam, and book histograms
0083   //
0084   fNbPrimarySteps = 0;
0085   fRange = 0;
0086   fProjRange = fProjRange2 = 0.;
0087   fEdeptot = fEniel = 0.;
0088   for (G4int j = 0; j < kMaxTally; ++j) {
0089     fTallyEdep[j] = 0.;
0090   }
0091   fKinematic->ResetEbeamCumul();
0092 
0093   if (fAnalysisManager->IsActive()) {
0094     fAnalysisManager->OpenFile();
0095 
0096     // histogram "1" is defined by the length of the target
0097     // zoomed histograms are defined by UI command
0098     G4double length = fDetector->GetAbsorSizeX();
0099     G4double stepMax = fPhysics->GetStepMaxProcess()->GetMaxStep();
0100     G4int nbBins = 100;
0101     if (stepMax < DBL_MAX) {
0102       G4int nb = (G4int)(0.5 + length / stepMax);
0103       nbBins = std::min(std::max(nbBins, nb), 10000);
0104     }
0105     fAnalysisManager->SetH1(1, nbBins, 0., length, "mm");
0106   }
0107 }
0108 
0109 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0110 
0111 void RunAction::EndOfRunAction(const G4Run* aRun)
0112 {
0113   G4int nbofEvents = aRun->GetNumberOfEvent();
0114   if (nbofEvents == 0) return;
0115 
0116   // run conditions
0117   //
0118   const G4Material* material = fDetector->GetAbsorMaterial();
0119   G4double density = material->GetDensity();
0120 
0121   G4String particle = fKinematic->GetParticleGun()->GetParticleDefinition()->GetParticleName();
0122   G4double energy = fKinematic->GetParticleGun()->GetParticleEnergy();
0123   G4cout << "\n The run consists of " << nbofEvents << " " << particle << " of "
0124          << G4BestUnit(energy, "Energy") << " through "
0125          << G4BestUnit(fDetector->GetAbsorSizeX(), "Length") << " of " << material->GetName()
0126          << " (density: " << G4BestUnit(density, "Volumic Mass") << ")" << G4endl;
0127 
0128   // compute projected range and straggling
0129   //
0130   if (fRange > 0) {
0131     fProjRange /= fRange;
0132     fProjRange2 /= fRange;
0133   }
0134   G4double rms = fProjRange2 - fProjRange * fProjRange;
0135   if (rms > 0.)
0136     rms = std::sqrt(rms);
0137   else
0138     rms = 0.;
0139 
0140   G4double nstep = G4double(fNbPrimarySteps) / G4double(nbofEvents);
0141 
0142   G4cout.precision(6);
0143   G4cout << "\n Projected Range= " << G4BestUnit(fProjRange, "Length")
0144          << "   rms= " << G4BestUnit(rms, "Length") << G4endl;
0145   G4cout << " Mean number of primary steps = " << nstep << G4endl;
0146 
0147   // compute energy deposition and niel
0148   //
0149   fEdeptot /= nbofEvents;
0150   G4cout << " Total energy deposit= " << G4BestUnit(fEdeptot, "Energy") << G4endl;
0151   fEniel /= nbofEvents;
0152   G4cout << " niel energy deposit = " << G4BestUnit(fEniel, "Energy") << G4endl;
0153 
0154   // print dose in tallies
0155   //
0156   G4int tallyNumber = fDetector->GetTallyNumber();
0157   if (tallyNumber > 0) {
0158     G4double Ebeam = fKinematic->GetEbeamCumul();
0159     G4cout << "\n---------------------------------------------------------\n";
0160     G4cout << " Cumulated Doses : \tEdep      \tEdep/Ebeam \tDose" << G4endl;
0161     for (G4int j = 0; j < tallyNumber; ++j) {
0162       G4double Edep = fTallyEdep[j], ratio = 100 * Edep / Ebeam;
0163       G4double tallyMass = fDetector->GetTallyMass(j);
0164       G4double Dose = Edep / tallyMass;
0165       G4cout << " tally " << j << ": \t \t" << G4BestUnit(Edep, "Energy") << "\t" << ratio
0166              << " % \t" << G4BestUnit(Dose, "Dose") << G4endl;
0167     }
0168     G4cout << "\n---------------------------------------------------------\n";
0169     G4cout << G4endl;
0170   }
0171 
0172   if (fAnalysisManager->IsActive()) {
0173     // normalize histograms
0174     //
0175     for (G4int j = 1; j < 3; ++j) {
0176       G4double binWidth = fAnalysisManager->GetH1Width(j);
0177       G4double fac = (mm / MeV) / (nbofEvents * binWidth);
0178       fAnalysisManager->ScaleH1(j, fac);
0179     }
0180     fAnalysisManager->ScaleH1(3, 1. / nbofEvents);
0181 
0182     // save histograms
0183     fAnalysisManager->Write();
0184     fAnalysisManager->CloseFile();
0185   }
0186 
0187   // show Rndm status
0188   //
0189   CLHEP::HepRandom::showEngineStatus();
0190 }
0191 
0192 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0193 
0194 void RunAction::BookHisto()
0195 {
0196   // Create or get analysis manager
0197   fAnalysisManager = G4AnalysisManager::Instance();
0198   fAnalysisManager->SetDefaultFileType("root");
0199   fAnalysisManager->SetFileName("testem7");
0200   fAnalysisManager->SetVerboseLevel(1);
0201   fAnalysisManager->SetActivation(true);  // enable inactivation of histograms
0202 
0203   // Define histograms start values
0204   const G4int kMaxHisto = 4;
0205   const G4String id[] = {"h0", "h1", "h2", "h3"};
0206   const G4String title[] = {
0207     "dummy",  // 0
0208     "Edep (MeV/mm) along absorber ",  // 1
0209     "Edep (MeV/mm) along absorber zoomed",  // 2
0210     "projectile range"  // 3
0211   };
0212 
0213   // Default values (to be reset via /analysis/h1/set command)
0214   G4int nbins = 100;
0215   G4double vmin = 0.;
0216   G4double vmax = 100.;
0217 
0218   // Create all histograms as inactivated
0219   // as we have not yet set nbins, vmin, vmax
0220   for (G4int k = 0; k < kMaxHisto; ++k) {
0221     G4int ih = fAnalysisManager->CreateH1(id[k], title[k], nbins, vmin, vmax);
0222     G4bool activ = false;
0223     if (k == 1) activ = true;
0224     fAnalysisManager->SetH1Activation(ih, activ);
0225   }
0226 }
0227 
0228 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......