Geant4 Cross Reference |
1 // 2 // ******************************************************************** 3 // * License and Disclaimer * 4 // * * 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. * 10 // * * 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitation of liability. * 17 // * * 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************************************** 25 // 26 /// \file Run.cc 27 /// \brief Implementation of the Run class 28 // 29 // 30 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 31 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 32 33 #include "Run.hh" 34 35 #include "DetectorConstruction.hh" 36 #include "EventAction.hh" 37 #include "HistoManager.hh" 38 #include "PrimaryGeneratorAction.hh" 39 40 #include "G4Event.hh" 41 #include "G4Material.hh" 42 #include "G4SystemOfUnits.hh" 43 #include "G4UnitsTable.hh" 44 45 #include <iomanip> 46 47 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 48 49 Run::Run(DetectorConstruction* detector) : fDetector(detector) 50 { 51 fTotEdep[1] = fEleak[1] = fEtotal[1] = joule; 52 53 for (G4int i = 0; i < kMaxAbsor; ++i) { 54 fEdeposit[i] = 0.; 55 fEmin[i] = joule; 56 fEmax[i] = 0.; 57 } 58 } 59 60 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 61 62 void Run::SetPrimary(G4ParticleDefinition* particle, G4double energy) 63 { 64 fParticle = particle; 65 fEkin = energy; 66 } 67 68 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 69 70 void Run::CountProcesses(const G4VProcess* process) 71 { 72 if (process == nullptr) return; 73 G4String procName = process->GetProcessName(); 74 std::map<G4String, G4int>::iterator it = fProcCounter.find(procName); 75 if (it == fProcCounter.end()) { 76 fProcCounter[procName] = 1; 77 } 78 else { 79 fProcCounter[procName]++; 80 } 81 } 82 83 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 84 85 void Run::ParticleCount(G4int k, G4String name, G4double Ekin, G4double meanLife) 86 { 87 std::map<G4String, ParticleData>::iterator it = fParticleDataMap[k].find(name); 88 if (it == fParticleDataMap[k].end()) { 89 (fParticleDataMap[k])[name] = ParticleData(1, Ekin, Ekin, Ekin, meanLife); 90 } 91 else { 92 ParticleData& data = it->second; 93 data.fCount++; 94 data.fEmean += Ekin; 95 // update min max 96 G4double emin = data.fEmin; 97 if (Ekin < emin) data.fEmin = Ekin; 98 G4double emax = data.fEmax; 99 if (Ekin > emax) data.fEmax = Ekin; 100 data.fTmean = meanLife; 101 } 102 } 103 104 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 105 106 void Run::AddEdep(G4int i, G4double e) 107 { 108 if (e > 0.) { 109 fEdeposit[i] += e; 110 if (e < fEmin[i]) fEmin[i] = e; 111 if (e > fEmax[i]) fEmax[i] = e; 112 } 113 } 114 115 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 116 117 void Run::AddTotEdep(G4double e) 118 { 119 if (e > 0.) { 120 fTotEdep[0] += e; 121 if (e < fTotEdep[1]) fTotEdep[1] = e; 122 if (e > fTotEdep[2]) fTotEdep[2] = e; 123 } 124 } 125 126 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 127 128 void Run::AddEleak(G4double e) 129 { 130 if (e > 0.) { 131 fEleak[0] += e; 132 if (e < fEleak[1]) fEleak[1] = e; 133 if (e > fEleak[2]) fEleak[2] = e; 134 } 135 } 136 137 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 138 139 void Run::AddEtotal(G4double e) 140 { 141 if (e > 0.) { 142 fEtotal[0] += e; 143 if (e < fEtotal[1]) fEtotal[1] = e; 144 if (e > fEtotal[2]) fEtotal[2] = e; 145 } 146 } 147 148 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 149 150 void Run::AddTrackStatus(G4int i) 151 { 152 fStatus[i]++; 153 } 154 155 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 156 157 void Run::Merge(const G4Run* run) 158 { 159 const Run* localRun = static_cast<const Run*>(run); 160 161 // pass information about primary particle 162 fParticle = localRun->fParticle; 163 fEkin = localRun->fEkin; 164 165 // Edep in absorbers 166 // 167 G4int nbOfAbsor = fDetector->GetNbOfAbsor(); 168 for (G4int i = 1; i <= nbOfAbsor; ++i) { 169 fEdeposit[i] += localRun->fEdeposit[i]; 170 // min, max 171 G4double min, max; 172 min = localRun->fEmin[i]; 173 max = localRun->fEmax[i]; 174 if (fEmin[i] > min) fEmin[i] = min; 175 if (fEmax[i] < max) fEmax[i] = max; 176 } 177 178 for (G4int i = 0; i < 3; ++i) 179 fStatus[i] += localRun->fStatus[i]; 180 181 // total Edep 182 fTotEdep[0] += localRun->fTotEdep[0]; 183 G4double min, max; 184 min = localRun->fTotEdep[1]; 185 max = localRun->fTotEdep[2]; 186 if (fTotEdep[1] > min) fTotEdep[1] = min; 187 if (fTotEdep[2] < max) fTotEdep[2] = max; 188 189 // Eleak 190 fEleak[0] += localRun->fEleak[0]; 191 min = localRun->fEleak[1]; 192 max = localRun->fEleak[2]; 193 if (fEleak[1] > min) fEleak[1] = min; 194 if (fEleak[2] < max) fEleak[2] = max; 195 196 // Etotal 197 fEtotal[0] += localRun->fEtotal[0]; 198 min = localRun->fEtotal[1]; 199 max = localRun->fEtotal[2]; 200 if (fEtotal[1] > min) fEtotal[1] = min; 201 if (fEtotal[2] < max) fEtotal[2] = max; 202 203 // map: processes count 204 std::map<G4String, G4int>::const_iterator itp; 205 for (itp = localRun->fProcCounter.begin(); itp != localRun->fProcCounter.end(); ++itp) { 206 G4String procName = itp->first; 207 G4int localCount = itp->second; 208 if (fProcCounter.find(procName) == fProcCounter.end()) { 209 fProcCounter[procName] = localCount; 210 } 211 else { 212 fProcCounter[procName] += localCount; 213 } 214 } 215 216 // map: created particles in absorbers count 217 for (G4int k = 0; k <= nbOfAbsor; ++k) { 218 std::map<G4String, ParticleData>::const_iterator itc; 219 for (itc = localRun->fParticleDataMap[k].begin(); itc != localRun->fParticleDataMap[k].end(); 220 ++itc) 221 { 222 G4String name = itc->first; 223 const ParticleData& localData = itc->second; 224 if (fParticleDataMap[k].find(name) == fParticleDataMap[k].end()) { 225 (fParticleDataMap[k])[name] = ParticleData( 226 localData.fCount, localData.fEmean, localData.fEmin, localData.fEmax, localData.fTmean); 227 } 228 else { 229 ParticleData& data = (fParticleDataMap[k])[name]; 230 data.fCount += localData.fCount; 231 data.fEmean += localData.fEmean; 232 G4double emin = localData.fEmin; 233 if (emin < data.fEmin) data.fEmin = emin; 234 G4double emax = localData.fEmax; 235 if (emax > data.fEmax) data.fEmax = emax; 236 data.fTmean = localData.fTmean; 237 } 238 } 239 } 240 241 G4Run::Merge(run); 242 } 243 244 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 245 246 void Run::EndOfRun() 247 { 248 G4int prec = 5, wid = prec + 2; 249 G4int dfprec = G4cout.precision(prec); 250 251 // run conditions 252 // 253 G4String partName = fParticle->GetParticleName(); 254 G4int nbOfAbsor = fDetector->GetNbOfAbsor(); 255 256 G4cout << "\n ======================== run summary =====================\n"; 257 G4cout << "\n The run is " << numberOfEvent << " " << partName << " of " 258 << G4BestUnit(fEkin, "Energy") << " through " << nbOfAbsor << " absorbers: \n"; 259 for (G4int i = 1; i <= nbOfAbsor; i++) { 260 G4Material* material = fDetector->GetAbsorMaterial(i); 261 G4double thickness = fDetector->GetAbsorThickness(i); 262 G4double density = material->GetDensity(); 263 G4cout << std::setw(5) << i << std::setw(10) << G4BestUnit(thickness, "Length") << " of " 264 << material->GetName() << " (density: " << G4BestUnit(density, "Volumic Mass") << ")" 265 << G4endl; 266 } 267 268 if (numberOfEvent == 0) { 269 G4cout.precision(dfprec); 270 return; 271 } 272 273 G4cout.precision(3); 274 275 // frequency of processes 276 // 277 G4cout << "\n Process calls frequency :" << G4endl; 278 G4int index = 0; 279 std::map<G4String, G4int>::iterator it; 280 for (it = fProcCounter.begin(); it != fProcCounter.end(); it++) { 281 G4String procName = it->first; 282 G4int count = it->second; 283 G4String space = " "; 284 if (++index % 3 == 0) space = "\n"; 285 G4cout << " " << std::setw(20) << procName << "=" << std::setw(7) << count << space; 286 } 287 G4cout << G4endl; 288 289 // Edep in absorbers 290 // 291 for (G4int i = 1; i <= nbOfAbsor; i++) { 292 fEdeposit[i] /= numberOfEvent; 293 294 G4cout << "\n Edep in absorber " << i << " = " << G4BestUnit(fEdeposit[i], "Energy") << "\t(" 295 << G4BestUnit(fEmin[i], "Energy") << "-->" << G4BestUnit(fEmax[i], "Energy") << ")"; 296 } 297 G4cout << G4endl; 298 299 if (nbOfAbsor > 1) { 300 fTotEdep[0] /= numberOfEvent; 301 G4cout << "\n Edep in all absorb = " << G4BestUnit(fTotEdep[0], "Energy") << "\t(" 302 << G4BestUnit(fTotEdep[1], "Energy") << "-->" << G4BestUnit(fTotEdep[2], "Energy") << ")" 303 << G4endl; 304 } 305 306 // Eleak 307 // 308 fEleak[0] /= numberOfEvent; 309 G4cout << " Energy leakage = " << G4BestUnit(fEleak[0], "Energy") << "\t(" 310 << G4BestUnit(fEleak[1], "Energy") << "-->" << G4BestUnit(fEleak[2], "Energy") << ")" 311 << G4endl; 312 313 // Etotal 314 // 315 fEtotal[0] /= numberOfEvent; 316 G4cout << " Energy total = " << G4BestUnit(fEtotal[0], "Energy") << "\t(" 317 << G4BestUnit(fEtotal[1], "Energy") << "-->" << G4BestUnit(fEtotal[2], "Energy") << ")" 318 << G4endl; 319 320 // particles count in absorbers 321 // 322 for (G4int k = 1; k <= nbOfAbsor; k++) { 323 G4cout << "\n List of created particles in absorber " << k << ":" << G4endl; 324 325 std::map<G4String, ParticleData>::iterator itc; 326 for (itc = fParticleDataMap[k].begin(); itc != fParticleDataMap[k].end(); itc++) { 327 G4String name = itc->first; 328 ParticleData data = itc->second; 329 G4int count = data.fCount; 330 G4double eMean = data.fEmean / count; 331 G4double eMin = data.fEmin; 332 G4double eMax = data.fEmax; 333 G4double meanLife = data.fTmean; 334 335 G4cout << " " << std::setw(13) << name << ": " << std::setw(7) << count 336 << " Emean = " << std::setw(wid) << G4BestUnit(eMean, "Energy") << "\t( " 337 << G4BestUnit(eMin, "Energy") << " --> " << G4BestUnit(eMax, "Energy") << ")"; 338 if (meanLife >= 0.) 339 G4cout << "\tmean life = " << G4BestUnit(meanLife, "Time") << G4endl; 340 else 341 G4cout << "\tstable" << G4endl; 342 } 343 } 344 // particles emerging from absorbers 345 // 346 G4cout << "\n List of particles emerging from absorbers :" << G4endl; 347 348 std::map<G4String, ParticleData>::iterator itc; 349 for (itc = fParticleDataMap[0].begin(); itc != fParticleDataMap[0].end(); itc++) { 350 G4String name = itc->first; 351 ParticleData data = itc->second; 352 G4int count = data.fCount; 353 G4double eMean = data.fEmean / count; 354 G4double eMin = data.fEmin; 355 G4double eMax = data.fEmax; 356 /// G4double meanLife = data.fTmean; 357 358 G4cout << " " << std::setw(13) << name << ": " << std::setw(7) << count 359 << " Emean = " << std::setw(wid) << G4BestUnit(eMean, "Energy") << "\t( " 360 << G4BestUnit(eMin, "Energy") << " --> " << G4BestUnit(eMax, "Energy") << ")" << G4endl; 361 } 362 363 // transmission coefficients 364 // 365 G4double dNofEvents = double(numberOfEvent); 366 G4double absorbed = 100. * fStatus[0] / dNofEvents; 367 G4double transmit = 100. * fStatus[1] / dNofEvents; 368 G4double reflected = 100. * fStatus[2] / dNofEvents; 369 370 G4cout.precision(2); 371 G4cout << "\n Nb of events with primary absorbed = " << absorbed << " %," 372 << " transmit = " << transmit << " %," 373 << " reflected = " << reflected << " %" << G4endl; 374 375 // normalize histograms of longitudinal energy profile 376 // 377 G4AnalysisManager* analysisManager = G4AnalysisManager::Instance(); 378 G4int ih = 10; 379 G4double binWidth = analysisManager->GetH1Width(ih) * analysisManager->GetH1Unit(ih); 380 G4double fac = (1. / (numberOfEvent * binWidth)) * (mm / MeV); 381 analysisManager->ScaleH1(ih, fac); 382 383 // remove all contents in fProcCounter, fCount 384 fProcCounter.clear(); 385 for (G4int k = 0; k <= nbOfAbsor; k++) 386 fParticleDataMap[k].clear(); 387 388 // reset default formats 389 G4cout.precision(dfprec); 390 } 391 392 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 393