Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 /// \file electromagnetic/TestEm7/src/RunActio << 26 // $Id: RunAction.cc,v 1.25 2010-09-17 18:45:43 maire Exp $ 27 /// \brief Implementation of the RunAction cla << 27 // GEANT4 tag $Name: not supported by cvs2svn $ 28 // << 28 // 29 // << 30 //....oooOO0OOooo........oooOO0OOooo........oo 29 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 31 //....oooOO0OOooo........oooOO0OOooo........oo 30 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 32 31 33 #include "RunAction.hh" 32 #include "RunAction.hh" 34 << 35 #include "DetectorConstruction.hh" 33 #include "DetectorConstruction.hh" 36 #include "PhysicsList.hh" 34 #include "PhysicsList.hh" 37 #include "PrimaryGeneratorAction.hh" << 38 #include "StepMax.hh" 35 #include "StepMax.hh" >> 36 #include "HistoManager.hh" >> 37 #include "PrimaryGeneratorAction.hh" 39 38 40 #include "G4Run.hh" 39 #include "G4Run.hh" 41 #include "G4RunManager.hh" 40 #include "G4RunManager.hh" 42 #include "G4SystemOfUnits.hh" << 43 #include "G4UnitsTable.hh" 41 #include "G4UnitsTable.hh" 44 #include "G4ios.hh" 42 #include "G4ios.hh" >> 43 45 #include "Randomize.hh" 44 #include "Randomize.hh" 46 45 47 //....oooOO0OOooo........oooOO0OOooo........oo 46 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 48 47 49 RunAction::RunAction(DetectorConstruction* det << 48 RunAction::RunAction(DetectorConstruction* det, PhysicsList* phys, 50 : G4UserRunAction(), << 49 HistoManager* histo, PrimaryGeneratorAction* kin) 51 fAnalysisManager(0), << 50 :detector(det), physics(phys),histoManager(histo), kinematic(kin) 52 fDetector(det), << 51 { 53 fPhysics(phys), << 52 tallyEdep = new G4double[MaxTally]; 54 fKinematic(kin), << 55 fTallyEdep(new G4double[kMaxTally]), << 56 fProjRange(0.), << 57 fProjRange2(0.), << 58 fEdeptot(0.), << 59 fEniel(0.), << 60 fNbPrimarySteps(0), << 61 fRange(0) << 62 { << 63 // Book predefined histograms << 64 BookHisto(); << 65 } 53 } 66 54 67 //....oooOO0OOooo........oooOO0OOooo........oo 55 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 68 56 69 RunAction::~RunAction() 57 RunAction::~RunAction() 70 { 58 { 71 delete[] fTallyEdep; << 59 delete [] tallyEdep; 72 } 60 } 73 61 74 //....oooOO0OOooo........oooOO0OOooo........oo 62 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 75 63 76 void RunAction::BeginOfRunAction(const G4Run* 64 void RunAction::BeginOfRunAction(const G4Run* aRun) 77 { << 65 { 78 G4cout << "### Run " << aRun->GetRunID() << 66 G4cout << "### Run " << aRun->GetRunID() << " start." << G4endl; 79 << 67 80 if (!fAnalysisManager) { << 68 // save Rndm status 81 BookHisto(); << 69 G4RunManager::GetRunManager()->SetRandomNumberStore(true); 82 } << 83 << 84 CLHEP::HepRandom::showEngineStatus(); 70 CLHEP::HepRandom::showEngineStatus(); 85 << 71 86 // initialize projected range, tallies, Ebea << 72 //initialize projected range, tallies, Ebeam, and book histograms 87 // 73 // 88 fNbPrimarySteps = 0; << 74 nPrimarySteps = 0; 89 fRange = 0; << 75 nRange = 0; 90 fProjRange = fProjRange2 = 0.; << 76 projRange = projRange2 = 0.; 91 fEdeptot = fEniel = 0.; << 77 edeptot = eniel = 0.; 92 for (G4int j = 0; j < kMaxTally; ++j) { << 78 for (G4int j=0; j<MaxTally; j++) tallyEdep[j] = 0.; 93 fTallyEdep[j] = 0.; << 79 kinematic->ResetEbeamCumul(); 94 } << 80 95 fKinematic->ResetEbeamCumul(); << 81 // define "1" histogram binning 96 << 82 // histogram "1" is defined by the length of the target 97 if (fAnalysisManager->IsActive()) { << 83 // zoomed histograms are defined by UI command 98 fAnalysisManager->OpenFile(); << 84 G4double length = detector->GetAbsorSizeX(); 99 << 85 G4double stepMax = physics->GetStepMaxProcess()->GetMaxStep(); 100 // histogram "1" is defined by the length << 86 G4int nbmin = 100; 101 // zoomed histograms are defined by UI com << 87 G4int nbBins = (G4int)(0.5 + length/stepMax); 102 G4double length = fDetector->GetAbsorSizeX << 88 if (nbBins < nbmin) nbBins = nbmin; 103 G4double stepMax = fPhysics->GetStepMaxPro << 89 histoManager->SetHisto(1, nbBins, 0., length, "mm"); 104 G4int nbBins = 100; << 90 105 if (stepMax < DBL_MAX) { << 91 histoManager->book(); 106 G4int nb = (G4int)(0.5 + length / stepMa << 107 nbBins = std::min(std::max(nbBins, nb), << 108 } << 109 fAnalysisManager->SetH1(1, nbBins, 0., len << 110 } << 111 } 92 } 112 93 113 //....oooOO0OOooo........oooOO0OOooo........oo 94 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 114 95 115 void RunAction::EndOfRunAction(const G4Run* aR 96 void RunAction::EndOfRunAction(const G4Run* aRun) 116 { 97 { 117 G4int nbofEvents = aRun->GetNumberOfEvent(); << 98 G4int NbofEvents = aRun->GetNumberOfEvent(); 118 if (nbofEvents == 0) return; << 99 if (NbofEvents == 0) return; 119 100 120 // run conditions << 101 //run conditions 121 // << 102 // 122 const G4Material* material = fDetector->GetA << 103 G4Material* material = detector->GetAbsorMaterial(); 123 G4double density = material->GetDensity(); 104 G4double density = material->GetDensity(); 124 << 105 125 G4String particle = fKinematic->GetParticleG << 106 G4String particle = kinematic->GetParticleGun()->GetParticleDefinition() 126 G4double energy = fKinematic->GetParticleGun << 107 ->GetParticleName(); 127 G4cout << "\n The run consists of " << nbofE << 108 G4double energy = kinematic->GetParticleGun()->GetParticleEnergy(); 128 << G4BestUnit(energy, "Energy") << " << 109 G4cout << "\n The run consists of " << NbofEvents << " "<< particle << " of " 129 << G4BestUnit(fDetector->GetAbsorSize << 110 << G4BestUnit(energy,"Energy") << " through " 130 << " (density: " << G4BestUnit(densit << 111 << G4BestUnit(detector->GetAbsorSizeX(),"Length") << " of " 131 << 112 << material->GetName() << " (density: " 132 // compute projected range and straggling << 113 << G4BestUnit(density,"Volumic Mass") << ")" << G4endl; 133 // << 114 134 if (fRange > 0) { << 115 //compute projected range and straggling 135 fProjRange /= fRange; << 116 // 136 fProjRange2 /= fRange; << 117 if(nRange > 0) { 137 } << 118 projRange /= nRange; 138 G4double rms = fProjRange2 - fProjRange * fP << 119 projRange2 /= nRange; 139 if (rms > 0.) << 120 } 140 rms = std::sqrt(rms); << 121 G4double rms = projRange2 - projRange*projRange; 141 else << 122 if (rms>0.) rms = std::sqrt(rms); else rms = 0.; 142 rms = 0.; << 123 143 << 124 G4double nstep = G4double(nPrimarySteps)/G4double(NbofEvents); 144 G4double nstep = G4double(fNbPrimarySteps) / << 125 145 << 126 G4cout.precision(6); 146 G4cout.precision(6); << 127 G4cout << "\n Projected Range= "<< G4BestUnit(projRange,"Length") 147 G4cout << "\n Projected Range= " << G4BestUn << 128 << " rms= " << G4BestUnit( rms,"Length") 148 << " rms= " << G4BestUnit(rms, "Len << 129 << G4endl; 149 G4cout << " Mean number of primary steps = " << 130 G4cout << " Mean number of primary steps = "<< nstep << G4endl; 150 << 131 151 // compute energy deposition and niel << 132 //compute energy deposition and niel >> 133 // >> 134 edeptot /= NbofEvents; >> 135 G4cout << " Total energy deposit= "<< G4BestUnit(edeptot,"Energy") >> 136 << G4endl; >> 137 eniel /= NbofEvents; >> 138 G4cout << " niel energy deposit = "<< G4BestUnit(eniel,"Energy") >> 139 << G4endl; >> 140 >> 141 //print dose in tallies 152 // 142 // 153 fEdeptot /= nbofEvents; << 143 G4int tallyNumber = detector->GetTallyNumber(); 154 G4cout << " Total energy deposit= " << G4Bes << 155 fEniel /= nbofEvents; << 156 G4cout << " niel energy deposit = " << G4Bes << 157 << 158 // print dose in tallies << 159 // << 160 G4int tallyNumber = fDetector->GetTallyNumbe << 161 if (tallyNumber > 0) { 144 if (tallyNumber > 0) { 162 G4double Ebeam = fKinematic->GetEbeamCumul << 145 G4double Ebeam = kinematic->GetEbeamCumul(); 163 G4cout << "\n----------------------------- 146 G4cout << "\n---------------------------------------------------------\n"; 164 G4cout << " Cumulated Doses : \tEdep 147 G4cout << " Cumulated Doses : \tEdep \tEdep/Ebeam \tDose" << G4endl; 165 for (G4int j = 0; j < tallyNumber; ++j) { << 148 for (G4int j=1; j <= tallyNumber; j++) { 166 G4double Edep = fTallyEdep[j], ratio = 1 << 149 G4double Edep = tallyEdep[j], ratio = 100*Edep/Ebeam; 167 G4double tallyMass = fDetector->GetTally << 150 G4double tallyMass = detector->GetTallyMass(j); 168 G4double Dose = Edep / tallyMass; << 151 G4double Dose = Edep/tallyMass; 169 G4cout << " tally " << j << ": \t \t" << << 152 G4cout << " tally " << j << ": \t \t" 170 << " % \t" << G4BestUnit(Dose, "D << 153 << G4BestUnit(Edep,"Energy") << "\t" >> 154 << ratio << " % \t" >> 155 << G4BestUnit(Dose,"Dose") << G4endl; 171 } 156 } 172 G4cout << "\n----------------------------- 157 G4cout << "\n---------------------------------------------------------\n"; 173 G4cout << G4endl; << 158 G4cout << G4endl; 174 } << 175 << 176 if (fAnalysisManager->IsActive()) { << 177 // normalize histograms << 178 // << 179 for (G4int j = 1; j < 3; ++j) { << 180 G4double binWidth = fAnalysisManager->Ge << 181 G4double fac = (mm / MeV) / (nbofEvents << 182 fAnalysisManager->ScaleH1(j, fac); << 183 } << 184 fAnalysisManager->ScaleH1(3, 1. / nbofEven << 185 << 186 // save histograms << 187 fAnalysisManager->Write(); << 188 fAnalysisManager->CloseFile(); << 189 } 159 } 190 160 >> 161 // normalize histograms >> 162 for (G4int j=1; j<3; j++) { >> 163 G4double binWidth = histoManager->GetBinWidth(j); >> 164 G4double fac = (mm/MeV)/(NbofEvents * binWidth); >> 165 histoManager->Scale(j, fac); >> 166 } >> 167 histoManager->Scale(3, 1./NbofEvents); >> 168 >> 169 // save and clean histo >> 170 histoManager->save(); >> 171 191 // show Rndm status 172 // show Rndm status 192 // 173 // 193 CLHEP::HepRandom::showEngineStatus(); 174 CLHEP::HepRandom::showEngineStatus(); 194 } << 195 << 196 //....oooOO0OOooo........oooOO0OOooo........oo << 197 << 198 void RunAction::BookHisto() << 199 { << 200 // Create or get analysis manager << 201 // The choice of analysis technology is done << 202 // in HistoManager.hh << 203 fAnalysisManager = G4AnalysisManager::Instan << 204 fAnalysisManager->SetDefaultFileType("root") << 205 fAnalysisManager->SetFileName("testem7"); << 206 fAnalysisManager->SetVerboseLevel(1); << 207 fAnalysisManager->SetActivation(true); // e << 208 << 209 // Define histograms start values << 210 const G4int kMaxHisto = 4; << 211 const G4String id[] = {"h0", "h1", "h2", "h3 << 212 const G4String title[] = { << 213 "dummy", // 0 << 214 "Edep (MeV/mm) along absorber ", // 1 << 215 "Edep (MeV/mm) along absorber zoomed", // << 216 "projectile range" // 3 << 217 }; << 218 << 219 // Default values (to be reset via /analysis << 220 G4int nbins = 100; << 221 G4double vmin = 0.; << 222 G4double vmax = 100.; << 223 << 224 // Create all histograms as inactivated << 225 // as we have not yet set nbins, vmin, vmax << 226 for (G4int k = 0; k < kMaxHisto; ++k) { << 227 G4int ih = fAnalysisManager->CreateH1(id[k << 228 G4bool activ = false; << 229 if (k == 1) activ = true; << 230 fAnalysisManager->SetH1Activation(ih, acti << 231 } << 232 } 175 } 233 176 234 //....oooOO0OOooo........oooOO0OOooo........oo 177 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 235 178