Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-17 09:28:25

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 medical/electronScattering2/src/ElectronRun.cc
0028 /// \brief Implementation of the ElectronRun class
0029 
0030 #include "ElectronRun.hh"
0031 
0032 #include "G4MultiFunctionalDetector.hh"
0033 #include "G4SDManager.hh"
0034 #include "G4SystemOfUnits.hh"
0035 #include "G4VPrimitiveScorer.hh"
0036 
0037 #include <assert.h>
0038 
0039 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0040 
0041 ElectronRun::ElectronRun() : G4Run(), fMap()
0042 {
0043   fMap[0] = new G4THitsMap<G4double>("MyDetector", "cell flux");
0044   fMap[1] = new G4THitsMap<G4double>("MyDetector", "e cell flux");
0045   fMap[2] = new G4THitsMap<G4double>("MyDetector", "population");
0046   fMap[3] = new G4THitsMap<G4double>("MyDetector", "e population");
0047 }
0048 
0049 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0050 
0051 ElectronRun::~ElectronRun()
0052 {
0053   // Important to clean up the map
0054   std::map<G4int, G4THitsMap<G4double>*>::iterator iter = fMap.begin();
0055 
0056   while (iter != fMap.end()) {
0057     delete iter->second;
0058     iter++;
0059   }
0060 }
0061 
0062 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0063 
0064 void ElectronRun::RecordEvent(const G4Event* anEvent)
0065 {
0066   // Get the hits collection
0067   G4HCofThisEvent* eventHitCollection = anEvent->GetHCofThisEvent();
0068 
0069   if (!eventHitCollection) return;
0070 
0071   // Update our private fMap
0072   std::map<G4int, G4THitsMap<G4double>*>::iterator iter = fMap.begin();
0073 
0074   while (iter != fMap.end()) {
0075     G4int id = iter->first;
0076 
0077     // Get the hit collection corresponding to "id"
0078     G4THitsMap<G4double>* eventHitsMap =
0079       dynamic_cast<G4THitsMap<G4double>*>(eventHitCollection->GetHC(id));
0080 
0081     // Expect this to exist
0082     assert(0 != eventHitsMap);
0083 
0084     // Accumulate event data into our G4THitsMap<G4double> map
0085     *(iter->second) += *eventHitsMap;
0086 
0087     iter++;
0088   }
0089 
0090   G4Run::RecordEvent(anEvent);
0091 }
0092 
0093 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0094 
0095 void ElectronRun::DumpData(G4String& outputFileSpec) const
0096 {
0097   // Titles
0098   std::vector<G4String> title;
0099   title.push_back("Radius");
0100 
0101   // Output map - energy binning on x axis, theta on y
0102   std::map<G4int, std::vector<G4double>> output;
0103 
0104   G4int nThetaBins = 233;
0105 
0106   // Energy bins depends on the number of scorers
0107   G4int nEnergyBins = fMap.size();
0108 
0109   G4int i(0), j(0);
0110 
0111   // Initialise current to 0 in all bins
0112   for (i = 0; i < nThetaBins; i++) {
0113     for (j = 0; j < nEnergyBins; j++) {
0114       output[i].push_back(0);
0115     }
0116   }
0117 
0118   i = 0;
0119 
0120   // Fill the output map
0121   std::map<G4int, G4THitsMap<G4double>*>::const_iterator iter = fMap.begin();
0122 
0123   while (iter != fMap.end()) {
0124     G4THitsMap<G4double>* hitMap = iter->second;
0125 
0126     title.push_back(hitMap->GetName());
0127 
0128     std::map<G4int, G4double*>* myMap = hitMap->GetMap();
0129 
0130     for (j = 0; j < nThetaBins; j++) {
0131       G4double* current = (*myMap)[j];
0132       if (0 != current) output[j][i] = (*current);
0133     }
0134 
0135     i++;
0136     iter++;
0137   }
0138 
0139   Print(title, output, outputFileSpec);
0140 }
0141 
0142 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0143 
0144 void ElectronRun::Print(const std::vector<G4String>& title,
0145                         const std::map<G4int, std::vector<G4double>>& myMap,
0146                         G4String& outputFileSpec) const
0147 {
0148   // Print to G4cout and an output file
0149   std::ofstream outFile(outputFileSpec);
0150 
0151   // Print title vector
0152   std::vector<G4String>::const_iterator titleIter = title.begin();
0153 
0154   while (titleIter != title.end()) {
0155     G4cout << std::setw(8) << *titleIter << " ";
0156     titleIter++;
0157   }
0158 
0159   G4cout << G4endl;
0160 
0161   // Print current data
0162   std::map<G4int, std::vector<G4double>>::const_iterator iter = myMap.begin();
0163 
0164   while (iter != myMap.end()) {
0165     G4cout << std::setw(8) << std::setprecision(3) << iter->first << " ";
0166 
0167     std::vector<G4double>::const_iterator energyBinIter = iter->second.begin();
0168 
0169     // The built-in scorers do not automatically account for the area of the
0170     // cylinder replica rings. We must account for this now by multiplying our result
0171     // by the ratio of the area of the full cylinder end over the area of the actual
0172     // scoring ring.
0173     // In this ratio, PI divides out, as does the width of the scoring rings.
0174     // Left with just the number of rings squared divided by the ring index plus
0175     // 1 squared minus ring index squared.
0176     G4int ringNum = iter->first;
0177     G4double areaCorrection = 233. * 233. / ((ringNum + 1) * (ringNum + 1) - ringNum * ringNum);
0178     G4int counter = 0;
0179 
0180     while (energyBinIter != iter->second.end()) {
0181       G4double value = *energyBinIter;
0182       if (counter < 2) value = value * areaCorrection;
0183       G4cout << std::setw(10) << std::setprecision(5) << value * mm * mm << " ";
0184       outFile << value * mm * mm;
0185       if (counter < 3) outFile << ",";
0186       counter++;
0187       energyBinIter++;
0188     }
0189     outFile << G4endl;
0190     G4cout << G4endl;
0191     iter++;
0192   }
0193 }
0194 
0195 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0196 
0197 void ElectronRun::Merge(const G4Run* aRun)
0198 {
0199   // This method is called at the end of the run for each worker thread.
0200   // It accumulates the worker's results into global results.
0201   const ElectronRun* localRun = static_cast<const ElectronRun*>(aRun);
0202   const std::map<G4int, G4THitsMap<G4double>*>& localMap = localRun->fMap;
0203   std::map<G4int, G4THitsMap<G4double>*>::const_iterator iter = localMap.begin();
0204   for (; iter != localMap.end(); ++iter)
0205     (*(fMap[iter->first])) += (*(iter->second));
0206 
0207   // This call lets Geant4 maintain overall summary information.
0208   G4Run::Merge(aRun);
0209 }
0210 
0211 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......