46 #include "G4THitsCollection.hh"
61 RMC01AnalysisManager::RMC01AnalysisManager()
62 :fAccumulated_edep(0.), fAccumulated_edep2(0.), fMean_edep(0.),
63 fError_mean_edep(0.), fRelative_error(0.), fElapsed_time(0.),
64 fPrecision_to_reach(0.),fStop_run_if_precision_reached(true),
65 fNb_evt_modulo_for_convergence_test(5000),
66 fEdep_rmatrix_vs_electron_prim_energy(0),
67 fElectron_current_rmatrix_vs_electron_prim_energy(0),
68 fGamma_current_rmatrix_vs_electron_prim_energy(0),
69 fEdep_rmatrix_vs_gamma_prim_energy(0),
70 fElectron_current_rmatrix_vs_gamma_prim_energy(0),
71 fGamma_current_rmatrix_vs_gamma_prim_energy(0),
72 fEdep_rmatrix_vs_proton_prim_energy(0),
73 fElectron_current_rmatrix_vs_proton_prim_energy(0),
74 fProton_current_rmatrix_vs_proton_prim_energy(0),
75 fGamma_current_rmatrix_vs_proton_prim_energy(0),
77 fPrimSpectrumType(
EXPO),
78 fAlpha_or_E0(.5*
MeV),fAmplitude_prim_spectrum (1.),
79 fEmin_prim_spectrum(1.*
keV),fEmax_prim_spectrum (20.*
MeV),
80 fAdjoint_sim_mode(true),fNb_evt_per_adj_evt(2)
121 fAccumulated_edep =0.;
122 fAccumulated_edep2 =0.;
128 if (fAdjoint_sim_mode){
131 fConvergenceFileOutput.open(
"ConvergenceOfAdjointSimulationResults.txt",
133 fConvergenceFileOutput<<
134 "Normalised Edep[MeV]\terror[MeV]\tcomputing_time[s]"<<std::endl;
137 fConvergenceFileOutput.open(
"ConvergenceOfForwardSimulationResults.txt",
139 fConvergenceFileOutput<<
140 "Edep per event [MeV]\terror[MeV]\tcomputing_time[s]"
143 fConvergenceFileOutput.setf(std::ios::scientific);
144 fConvergenceFileOutput.precision(6);
158 if (!fAdjoint_sim_mode){
159 G4cout<<
"Results of forward simulation!"<<std::endl;
160 G4cout<<
"edep per event [MeV] = "<<fMean_edep<<std::endl;
161 G4cout<<
"error[MeV] = "<<fError_mean_edep<<std::endl;
165 G4cout<<
"Results of reverse/adjoint simulation!"<<std::endl;
166 G4cout<<
"normalised edep [MeV] = "<<fMean_edep<<std::endl;
167 G4cout<<
"error[MeV] = "<<fError_mean_edep<<std::endl;
172 fConvergenceFileOutput.close();
185 if (fAdjoint_sim_mode) EndOfEventForAdjointSimulation(anEvent);
186 else EndOfEventForForwardSimulation(anEvent);
192 if (fAdjoint_sim_mode) {
193 G4double n_adj_evt= nb_event/fNb_evt_per_adj_evt;
195 if (n_adj_evt*fNb_evt_per_adj_evt == nb_event) {
196 nb_event =
static_cast<G4int>(n_adj_evt);
202 if (nb_event>100 && fStop_run_if_precision_reached &&
203 fPrecision_to_reach >fRelative_error) {
204 G4cout<<fPrecision_to_reach*100.<<
"% Precision reached!"<<std::endl;
207 fConvergenceFileOutput<<fMean_edep<<
'\t'<<fError_mean_edep
208 <<
'\t'<<fElapsed_time<<std::endl;
212 if (nb_event>0 && nb_event % fNb_evt_modulo_for_convergence_test == 0) {
216 fConvergenceFileOutput<<fMean_edep<<
'\t'<<fError_mean_edep<<
'\t'
217 <<fElapsed_time<<std::endl;
223 void RMC01AnalysisManager::EndOfEventForForwardSimulation(
249 for (i=0;i<edepCollection->
entries();i++)
250 totEdep+=(*edepCollection)[i]->GetValue()
251 *(*edepCollection)[i]->GetWeight();
254 fAccumulated_edep +=totEdep ;
255 fAccumulated_edep2 +=totEdep*totEdep;
259 G4double prim_ekin =std::sqrt(E0*E0+P*P)-E0;
260 fEdep_vs_prim_ekin->fill(prim_ekin,totEdep);
262 ComputeMeanEdepAndError(anEvent,fMean_edep,fError_mean_edep);
263 if (fError_mean_edep>0) fRelative_error= fError_mean_edep/fMean_edep;
268 for (i=0;i<electronCurrentCollection->
entries();i++) {
269 G4double ekin =(*electronCurrentCollection)[i]->GetValue();
271 fElectron_current->fill(ekin,weight);
274 for (i=0;i<protonCurrentCollection->
entries();i++) {
275 G4double ekin =(*protonCurrentCollection)[i]->GetValue();
276 G4double weight=(*protonCurrentCollection)[i]->GetWeight();
277 fProton_current->fill(ekin,weight);
280 for (i=0;i<gammaCurrentCollection->
entries();i++) {
281 G4double ekin =(*gammaCurrentCollection)[i]->GetValue();
282 G4double weight=(*gammaCurrentCollection)[i]->GetWeight();
283 fGamma_current->fill(ekin,weight);
290 void RMC01AnalysisManager::EndOfEventForAdjointSimulation(
318 G4int pdg_nb =theAdjointSimManager
328 if (pdg_nb== fPrimPDG_ID && prim_ekin>= fEmin_prim_spectrum
329 && prim_ekin<= fEmax_prim_spectrum)
331 adj_weight*PrimDiffAndDirFluxForAdjointSim(prim_ekin);
336 G4AnaH2* electron_current_rmatrix =0;
337 G4AnaH2* gamma_current_rmatrix =0;
338 G4AnaH2* proton_current_rmatrix =0;
341 edep_rmatrix = fEdep_rmatrix_vs_electron_prim_energy;
343 electron_current_rmatrix =
344 fElectron_current_rmatrix_vs_electron_prim_energy;
346 gamma_current_rmatrix = fGamma_current_rmatrix_vs_electron_prim_energy;
350 edep_rmatrix = fEdep_rmatrix_vs_gamma_prim_energy;
351 electron_current_rmatrix = fElectron_current_rmatrix_vs_gamma_prim_energy;
352 gamma_current_rmatrix = fGamma_current_rmatrix_vs_gamma_prim_energy;
356 edep_rmatrix = fEdep_rmatrix_vs_proton_prim_energy;
357 electron_current_rmatrix = fElectron_current_rmatrix_vs_proton_prim_energy;
358 gamma_current_rmatrix = fGamma_current_rmatrix_vs_proton_prim_energy;
359 proton_current_rmatrix = fProton_current_rmatrix_vs_proton_prim_energy;
366 for (i=0;i<edepCollection->
entries();i++)
367 totEdep+=(*edepCollection)[i]->GetValue()*
368 (*edepCollection)[i]->GetWeight();
370 G4bool new_mean_computed=
false;
372 if (normalised_weight>0.){
379 fAccumulated_edep +=
edep;
380 fAccumulated_edep2 +=edep*
edep;
382 ComputeMeanEdepAndError(anEvent,new_mean,new_error);
384 if ( new_error >0) new_relative_error = new_error/ new_mean;
385 if (fRelative_error <0.10 && new_relative_error>1.5*fRelative_error) {
386 G4cout<<
"Potential wrong adjoint weight!"<<std::endl;
387 G4cout<<
"The results of this event will not be registered!"
389 G4cout<<
"previous mean edep [MeV] "<< fMean_edep<<std::endl;
390 G4cout<<
"previous relative error "<< fRelative_error<<std::endl;
391 G4cout<<
"new rejected mean edep [MeV] "<< new_mean<<std::endl;
392 G4cout<<
"new rejected relative error "<< new_relative_error
394 fAccumulated_edep -=
edep;
395 fAccumulated_edep2 -=edep*
edep;
399 fMean_edep = new_mean;
400 fError_mean_edep = new_error;
401 fRelative_error =new_relative_error;
402 new_mean_computed=
true;
404 fEdep_vs_prim_ekin->fill(prim_ekin,edep);
410 edep_rmatrix->fill(prim_ekin,totEdep*adj_weight/
cm2);
412 if (!new_mean_computed){
413 ComputeMeanEdepAndError(anEvent,fMean_edep,fError_mean_edep);
414 if (fError_mean_edep>0) fRelative_error= fError_mean_edep/fMean_edep;
421 for (i=0;i<electronCurrentCollection->
entries();i++) {
422 G4double ekin =(*electronCurrentCollection)[i]->GetValue();
423 G4double weight=(*electronCurrentCollection)[i]->GetWeight();
424 fElectron_current->fill(ekin,weight*normalised_weight);
425 electron_current_rmatrix->fill(prim_ekin,ekin,weight*adj_weight/
cm2);
428 for (i=0;i<protonCurrentCollection->
entries();i++) {
429 G4double ekin =(*protonCurrentCollection)[i]->GetValue();
430 G4double weight=(*protonCurrentCollection)[i]->GetWeight();
431 fProton_current->fill(ekin,weight*normalised_weight);
432 proton_current_rmatrix->fill(prim_ekin,ekin,weight*adj_weight/
cm2);
435 for (i=0;i<gammaCurrentCollection->
entries();i++) {
436 G4double ekin =(*gammaCurrentCollection)[i]->GetValue();
437 G4double weight=(*gammaCurrentCollection)[i]->GetWeight();
438 fGamma_current->fill(ekin,weight*normalised_weight);
439 gamma_current_rmatrix->fill(prim_ekin,ekin,weight*adj_weight/
cm2);
445 G4double RMC01AnalysisManager::PrimDiffAndDirFluxForAdjointSim(
448 G4double flux=fAmplitude_prim_spectrum;
449 if ( fPrimSpectrumType ==
EXPO) flux*=std::exp(-prim_energy/fAlpha_or_E0);
450 else flux*=std::pow(prim_energy, -fAlpha_or_E0);
496 void RMC01AnalysisManager::ComputeMeanEdepAndError(
501 if (fAdjoint_sim_mode) {
502 nb_event /=fNb_evt_per_adj_evt;
508 mean = fAccumulated_edep/nb_event;
509 G4double mean_x2 =fAccumulated_edep2/nb_event;
510 error = factor*std::sqrt(mean_x2-mean*mean)/std::sqrt(
G4double(nb_event));
525 { fPrimSpectrumType =
EXPO;
526 if (particle_name ==
"e-" ) fPrimPDG_ID =
528 else if (particle_name ==
"gamma") fPrimPDG_ID =
530 else if (particle_name ==
"proton") fPrimPDG_ID =
533 G4cout<<
"The particle that you did select is not in the candidate "<<
534 "list for primary [e-, gamma, proton]!"<<
G4endl;
538 fAmplitude_prim_spectrum = omni_fluence/E0/
539 (std::exp(-Emin/E0)-std::exp(-Emax/E0))/4./
pi;
540 fEmin_prim_spectrum = Emin ;
541 fEmax_prim_spectrum = Emax;
549 { fPrimSpectrumType =
POWER;
550 if (particle_name ==
"e-" ) fPrimPDG_ID =
552 else if (particle_name ==
"gamma") fPrimPDG_ID =
554 else if (particle_name ==
"proton") fPrimPDG_ID =
557 G4cout<<
"The particle that you did select is not in the candidate"<<
558 " list for primary [e-, gamma, proton]!"<<
G4endl;
564 fAmplitude_prim_spectrum = omni_fluence/std::log(Emax/Emin)/4./
pi;
568 fAmplitude_prim_spectrum = omni_fluence/p/(std::pow(Emax,p)
569 -std::pow(Emin,p))/4./
pi;
572 fAlpha_or_E0 = alpha ;
573 fEmin_prim_spectrum = Emin ;
574 fEmax_prim_spectrum = Emax;
592 fFileName[0]=
"forward_sim";
593 if (fAdjoint_sim_mode) fFileName[0]=
"adjoint_sim";
598 fFileName[1] = fFileName[0] +
"." + extension;
603 G4cout <<
"\n---> RMC01AnalysisManager::book(): cannot open " << fFileName[1]
619 G4String(
"edep vs e- primary energy"),60,emin,emax,
621 fEdep_vs_prim_ekin = theHistoManager->
GetH1(idHisto);
627 fElectron_current = theHistoManager->
GetH1(idHisto);
632 fProton_current=theHistoManager->
GetH1(idHisto);
638 fGamma_current=theHistoManager->
GetH1(idHisto);
643 if (fAdjoint_sim_mode){
649 G4String(
"electron RM vs e- primary energy"),60,emin,emax,
651 fEdep_rmatrix_vs_electron_prim_energy = theHistoManager->
GetH1(idHisto);
655 CreateH2(
G4String(
"Electron_current_rmatrix_vs_electron_prim_energy"),
656 G4String(
"electron current RM vs e- primary energy"),
657 60,emin,emax,60,emin,emax,
660 fElectron_current_rmatrix_vs_electron_prim_energy =
661 theHistoManager->
GetH2(idHisto);
665 CreateH2(
G4String(
"Gamma_current_rmatrix_vs_electron_prim_energy"),
666 G4String(
"gamma current RM vs e- primary energy"),
667 60,emin,emax,60,emin,emax,
671 fGamma_current_rmatrix_vs_electron_prim_energy =
672 theHistoManager->
GetH2(idHisto);
679 G4String(
"electron RM vs gamma primary energy"),60,emin,emax,
681 fEdep_rmatrix_vs_gamma_prim_energy = theHistoManager->
GetH1(idHisto);
685 CreateH2(
G4String(
"Electron_current_rmatrix_vs_gamma_prim_energy"),
686 G4String(
"electron current RM vs gamma primary energy"),
687 60,emin,emax,60,emin,emax,
690 fElectron_current_rmatrix_vs_gamma_prim_energy =
691 theHistoManager->
GetH2(idHisto);
695 CreateH2(
G4String(
"Gamma_current_rmatrix_vs_gamma_prim_energy"),
696 G4String(
"gamma current RM vs gamma primary energy"),
697 60,emin,emax,60,emin,emax,
700 fGamma_current_rmatrix_vs_gamma_prim_energy =
701 theHistoManager->
GetH2(idHisto);
708 G4String(
"electron RM vs proton primary energy"),60,emin,emax,
710 fEdep_rmatrix_vs_proton_prim_energy = theHistoManager->
GetH1(idHisto);
714 CreateH2(
G4String(
"Electron_current_rmatrix_vs_proton_prim_energy"),
715 G4String(
"electron current RM vs proton primary energy"),
716 60,emin,emax,60,emin,emax,
719 fElectron_current_rmatrix_vs_proton_prim_energy =
720 theHistoManager->
GetH2(idHisto);
724 CreateH2(
G4String(
"Gamma_current_rmatrix_vs_proton_prim_energy"),
725 G4String(
"gamma current RM vs proton primary energy"),
726 60,emin,emax,60,emin,emax,
729 fGamma_current_rmatrix_vs_proton_prim_energy =
730 theHistoManager->
GetH2(idHisto);
734 CreateH2(
G4String(
"Proton_current_rmatrix_vs_proton_prim_energy"),
735 G4String(
"proton current RM vs proton primary energy"),
736 60,emin,emax,60,emin,emax,
739 fProton_current_rmatrix_vs_proton_prim_energy =
740 theHistoManager->
GetH2(idHisto);
743 G4cout <<
"\n----> Histogram Tree is opened in " << fFileName[1] <<
G4endl;
754 for (
int ind=1; ind<=theHistoManager->
GetNofH1s();ind++){
756 theHistoManager->
ScaleH1(ind,scaling_factor);
758 for (
int ind=1; ind<=theHistoManager->
GetNofH2s();ind++)
759 theHistoManager->
ScaleH2(ind,scaling_factor);
761 theHistoManager->
Write();
763 G4cout <<
"\n----> Histogram Tree is saved in " << fFileName[1] <<
G4endl;
765 delete G4AnalysisManager::Instance();
static G4AdjointSimManager * GetInstance()
G4bool SetHistoDirectoryName(const G4String &dirName)
virtual void AbortRun(G4bool softAbort=false)
void SetPrimaryPowerLawSpectrumForAdjointSim(const G4String &particle_name, G4double fluence, G4double alpha, G4double Emin, G4double Emax)
G4ThreeVector GetMomentum() const
G4bool SetFirstHistoId(G4int firstId)
G4VHitsCollection * GetHC(G4int i)
G4int GetCollectionID(G4String colName)
G4int CreateH1(const G4String &name, const G4String &title, G4int nbins, G4double xmin, G4double xmax, const G4String &unitName="none", const G4String &fcnName="none", const G4String &binSchemeName="linear")
tools::histo::h2d G4AnaH2
G4bool ScaleH1(G4int id, G4double factor)
G4bool ScaleH2(G4int id, G4double factor)
G4bool GetAdjointSimMode()
G4int GetFwdParticlePDGEncodingAtEndOfLastAdjointTrack()
G4int GetPDGEncoding() const
void SetPrimaryExpSpectrumForAdjointSim(const G4String &particle_name, G4double fluence, G4double E0, G4double Emin, G4double Emax)
void BeginOfEvent(const G4Event *)
G4ParticleDefinition * GetG4code() const
G4int GetNbEvtOfLastRun()
Definition of the RMC01SD class.
void save(G4double scaling_factor)
G4double GetEkinAtEndOfLastAdjointTrack()
G4PrimaryParticle * GetPrimary(G4int i=0) const
void SetH1Ascii(G4int id, G4bool ascii)
G4GLOB_DLL std::ostream G4cout
G4int GetNumberOfEvent() const
G4String GetFileType() const
static G4Proton * Proton()
void EndOfRun(const G4Run *)
G4PrimaryVertex * GetPrimaryVertex(G4int i=0) const
Definition of the RMC01AnalysisManager class.
static G4RunManager * GetRunManager()
tools::histo::h1d G4AnaH1
Definition of the RMC01AnalysisManagerMessenger class.
G4double GetPDGMass() const
G4double GetRealElapsed() const
static RMC01AnalysisManager * GetInstance()
static G4SDManager * GetSDMpointer()
void EndOfEvent(const G4Event *)
G4double GetWeightAtEndOfLastAdjointTrack()
void BeginOfRun(const G4Run *)
G4int GetNumberOfEventToBeProcessed() const
static G4Electron * Electron()
G4HCofThisEvent * GetHCofThisEvent() const
tools::histo::h1d * GetH1(G4int id, G4bool warn=true, G4bool onlyIfActive=true) const
tools::histo::h2d * GetH2(G4int id, G4bool warn=true, G4bool onlyIfActive=true) const