Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-23 09:21:46

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 /// \file DicomRunAction.cc
0028 /// \brief Implementation of the DicomRunAction class
0029 //
0030 
0031 #include "DicomRunAction.hh"
0032 
0033 #include "DicomRun.hh"
0034 
0035 //-- In order to obtain detector information.
0036 #include "G4RunManager.hh"
0037 #include "G4SystemOfUnits.hh"
0038 #include "G4THitsMap.hh"
0039 #include "G4UnitsTable.hh"
0040 
0041 #include <fstream>
0042 #include <iomanip>
0043 
0044 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0045 /// Constructor
0046 DicomRunAction::DicomRunAction() : G4UserRunAction(), fDcmrun(0), fFieldValue(14)
0047 {
0048   // - Prepare data member for DicomRun.
0049   //   vector represents a list of MultiFunctionalDetector names.
0050   fSDName.push_back(G4String("phantomSD"));
0051 }
0052 
0053 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0054 /// Destructor.
0055 DicomRunAction::~DicomRunAction()
0056 {
0057   fSDName.clear();
0058 }
0059 
0060 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0061 G4Run* DicomRunAction::GenerateRun()
0062 {
0063   // Generate new RUN object, which is specially
0064   // dedicated for MultiFunctionalDetector scheme.
0065   //  Detail description can be found in DicomRun.hh/cc.
0066   // return new DicomRun(fSDName);
0067   return fDcmrun = new DicomRun(fSDName);
0068 }
0069 
0070 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0071 void DicomRunAction::BeginOfRunAction(const G4Run* aRun)
0072 {
0073   G4cout << "### Run " << aRun->GetRunID() << " start." << G4endl;
0074   // inform the runManager to save random number seed
0075   G4RunManager::GetRunManager()->SetRandomNumberStore(true);
0076   G4RunManager::GetRunManager()->SetRandomNumberStorePerEvent(false);
0077   G4RunManager::GetRunManager()->SetRandomNumberStoreDir(G4String("dicom-run-")
0078                                                          + std::to_string(aRun->GetRunID()));
0079 
0080   G4int progress = aRun->GetNumberOfEventToBeProcessed() / 100;
0081   progress = (progress < 1) ? 1 : progress;
0082   G4RunManager::GetRunManager()->SetPrintProgress(progress);
0083 }
0084 
0085 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0086 void DicomRunAction::EndOfRunAction(const G4Run* aRun)
0087 {
0088   G4int nofEvents = aRun->GetNumberOfEvent();
0089 
0090   static G4double local_total_dose = 0;
0091   G4double total_dose = 0;
0092 
0093   const DicomRun* reRun = static_cast<const DicomRun*>(aRun);
0094   //--- Dump all scored quantities involved in DicomRun.
0095   for (G4int i = 0; i < (G4int)fSDName.size(); i++) {
0096     //
0097     //---------------------------------------------
0098     // Dump accumulated quantities for this RUN.
0099     //  (Display only central region of x-y plane)
0100     //      0       ConcreteSD/DoseDeposit
0101     //---------------------------------------------
0102     G4THitsMap<G4double>* DoseDeposit = reRun->GetHitsMap(fSDName[i] + "/DoseDeposit");
0103 
0104     if (DoseDeposit && DoseDeposit->GetMap()->size() != 0) {
0105       std::map<G4int, G4double*>::iterator itr = DoseDeposit->GetMap()->begin();
0106       for (; itr != DoseDeposit->GetMap()->end(); itr++) {
0107         if (!IsMaster()) {
0108           local_total_dose += *(itr->second);
0109         }
0110         total_dose += *(itr->second);
0111       }
0112     }
0113   }
0114 
0115   if (IsMaster()) {
0116     G4cout << "--------------------End of Global Run-----------------------" << G4endl;
0117     G4cout << " The run was " << nofEvents << " events " << G4endl;
0118     G4cout << "LOCAL TOTAL DOSE : \t" << local_total_dose / gray << " Gy" << G4endl;
0119     G4cout << "      TOTAL DOSE : \t" << total_dose / gray << " Gy" << G4endl;
0120   }
0121   else {
0122     G4cout << "--------------------End of Local Run------------------------" << G4endl;
0123     G4cout << " The run was " << nofEvents << G4endl;
0124     G4cout << "LOCAL TOTAL DOSE : \t" << local_total_dose / gray << " Gy" << G4endl;
0125     G4cout << "      TOTAL DOSE : \t" << total_dose / gray << " Gy" << G4endl;
0126   }
0127 
0128   if (IsMaster()) {
0129     G4cout << " ###### EndOfRunAction ###### " << G4endl;
0130     //- DicomRun object.
0131     const DicomRun* re02Run = static_cast<const DicomRun*>(aRun);
0132     //--- Dump all scored quantities involved in DicomRun.
0133 
0134     for (G4int i = 0; i < (G4int)fSDName.size(); i++) {
0135       //
0136       //---------------------------------------------
0137       // Dump accumulated quantities for this RUN.
0138       //  (Display only central region of x-y plane)
0139       //      0       ConcreteSD/DoseDeposit
0140       //---------------------------------------------
0141       G4THitsMap<G4double>* DoseDeposit = re02Run->GetHitsMap(fSDName[i] + "/DoseDeposit");
0142 
0143       G4cout << "=============================================================" << G4endl;
0144       G4cout << " Number of event processed : " << aRun->GetNumberOfEvent() << G4endl;
0145       G4cout << "=============================================================" << G4endl;
0146 
0147       std::ofstream fileout;
0148       G4String fname = "dicom.out";
0149       fileout.open(fname);
0150       G4cout << " opened file " << fname << " for dose output" << G4endl;
0151 
0152       if (DoseDeposit && DoseDeposit->GetMap()->size() != 0) {
0153         std::ostream* myout = &G4cout;
0154         PrintHeader(myout);
0155         std::map<G4int, G4double*>::iterator itr = DoseDeposit->GetMap()->begin();
0156         for (; itr != DoseDeposit->GetMap()->end(); itr++) {
0157           fileout << itr->first << "     " << *(itr->second) / CLHEP::gray << G4endl;
0158           // G4cout << "    " << itr->first
0159           //        << "     " << std::setprecision(6)
0160           //        << *(itr->second)/CLHEP::gray << " Gy"
0161           //        << G4endl;
0162         }
0163         G4cout << "=============================================" << G4endl;
0164       }
0165       else {
0166         G4Exception("DicomRunAction", "000", JustWarning,
0167                     "DoseDeposit HitsMap is either a null pointer of the HitsMap was empty");
0168       }
0169       fileout.close();
0170       G4cout << " closed file " << fname << " for dose output" << G4endl;
0171     }
0172   }
0173 
0174   G4cout << "Finished : End of Run Action " << aRun->GetRunID() << G4endl;
0175 }
0176 
0177 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0178 void DicomRunAction::PrintHeader(std::ostream* out)
0179 {
0180   std::vector<G4String> vecScoreName;
0181   vecScoreName.push_back("DoseDeposit");
0182 
0183   // head line
0184   //
0185   std::string vname;
0186   *out << std::setw(10) << "Voxel"
0187        << " |";
0188   for (std::vector<G4String>::iterator it = vecScoreName.begin(); it != vecScoreName.end(); it++) {
0189     // vname = FillString((*it),
0190     //                        ' ',
0191     //                        FieldValue+1,
0192     //                        false);
0193     //     *out << vname << '|';
0194     *out << std::setw(fFieldValue) << (*it) << "  |";
0195   }
0196   *out << G4endl;
0197 }
0198 
0199 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0200 std::string DicomRunAction::FillString(const std::string& name, char c, G4int n, G4bool back)
0201 {
0202   std::string fname("");
0203   G4int k = G4int(n - name.size());
0204   if (k > 0) {
0205     if (back) {
0206       fname = name;
0207       fname += std::string(k, c);
0208     }
0209     else {
0210       fname = std::string(k, c);
0211       fname += name;
0212     }
0213   }
0214   else {
0215     fname = name;
0216   }
0217   return fname;
0218 }