Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-17 07:51:43

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 // -------------------------------------------------------------------
0028 
0029 #include "RunAction.hh"
0030 #include "G4Run.hh"
0031 #include "TrackingAction.hh"
0032 #include "G4ParticleDefinition.hh"
0033 #include "G4RunManager.hh"
0034 #include "G4AnalysisManager.hh"
0035 #include "G4Threading.hh"
0036 #include "PrimaryGeneratorAction.hh"
0037 #include "MicroElecRun.hh"
0038 #include "G4UnitsTable.hh"
0039 #include "g4csv_defs.hh"
0040 #include "G4SystemOfUnits.hh"
0041 
0042 void PrintNParticles(std::map<const G4ParticleDefinition*, int>& container);
0043 
0044 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0045 
0046 RunAction::RunAction()
0047 {
0048  fFileName = "microelectronics";
0049  SEYfileName = "SEY";
0050  SEYntupleName = "SEYntuple";
0051  fpTrackingAction = 0;
0052  fInitialized = 0;
0053  NbOfIncidentPart = 0;
0054  EnergyOfIncidentPart = 999;
0055  fDebug = false;
0056 }
0057 
0058 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0059 
0060 RunAction::~RunAction()
0061 {}
0062 
0063 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0064 G4Run* RunAction::GenerateRun()
0065 {
0066     fRun = new MicroElecRun();
0067     return fRun;
0068 }
0069 
0070 
0071 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0072 
0073 void RunAction::BeginOfRunAction(const G4Run* run)
0074 {  
0075   // In this example, we considered that the same class was
0076   // used for both master and worker threads.
0077   // However, in case the run action is long,
0078   // for better code review, this practice is not recommanded.
0079   //
0080     if (isMaster) {// WARNING : in sequential mode, isMaster == true    
0081         //PrimaryGeneratorAction* PGA = (PrimaryGeneratorAction*)G4RunManager::GetRunManager()->GetUserPrimaryGeneratorAction();
0082         //EnergyOfIncidentPart = PGA->GetEnergy();
0083         //EnergyOfIncidentPart = PGA->GetParticleGPS()->GetParticleEnergy();
0084         BeginMaster(run);
0085     }
0086   else 
0087     BeginWorker(run);
0088 }
0089 
0090 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0091 
0092 void RunAction::EndOfRunAction(const G4Run* run)
0093 {
0094 
0095     if (isMaster) {
0096         EndMaster(run);
0097         G4cout << "End of run action : " << EnergyOfIncidentPart << G4endl;
0098     }
0099     else {
0100     PrimaryGeneratorAction* PGA = (PrimaryGeneratorAction*)G4RunManager::GetRunManager()->GetUserPrimaryGeneratorAction();
0101     EnergyOfIncidentPart = PGA->GetParticleGPS()->GetCurrentSource()->GetEneDist()->GetMonoEnergy();
0102     EndWorker(run);
0103     }
0104 
0105 }
0106 
0107 
0108 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0109 
0110 void RunAction::BeginMaster(const G4Run* run)
0111 {
0112   bool sequential = (G4RunManager::GetRunManager()->GetRunManagerType() == G4RunManager::sequentialRM);
0113   
0114   if(fDebug)
0115     {
0116       G4cout << "°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°" << G4endl;
0117       if(!sequential)
0118     G4cout << "°°°°°°°°°°°°°°°° RunAction::BeginMaster" << G4endl;
0119       PrintRunInfo(run);
0120       G4cout << "°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°" << G4endl;
0121     }
0122   
0123   if(sequential)
0124     {
0125       if(!fInitialized) 
0126     InitializeWorker(run);
0127       // Note: fpTrackingAction could be used as a flag for initialization instead      
0128 
0129       //CreateHistogram();
0130     }
0131   // modif CI 1/3/2022
0132   else {
0133       if (run->GetRunID() == 0) { CreateSEYHistogram(); }
0134   }
0135 }
0136 
0137 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0138 
0139 void RunAction::BeginWorker(const G4Run* run)
0140 {
0141   if(fDebug)
0142     {
0143       G4cout << "°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°" << G4endl;
0144       G4cout << "°°°°°°°°°°°°°°°° RunAction::BeginWorker" << G4endl;
0145       PrintRunInfo(run);
0146       G4cout << "°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°" << G4endl;
0147     }
0148   if(!fInitialized) 
0149     InitializeWorker(run);
0150   // modif CI 4/3/2022
0151   //CreateHistogram();
0152 }
0153 
0154 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0155 
0156 void RunAction::EndMaster(const G4Run* run)
0157 {
0158   bool sequential = (G4RunManager::GetRunManager()->GetRunManagerType() 
0159              == G4RunManager::sequentialRM);
0160   if (sequential) {
0161       EndWorker(run);
0162   }
0163   else {
0164       
0165       
0166       
0167       if (run->GetRunID() == 0) {
0168           NbOfIncidentPart  = run->GetNumberOfEvent();
0169       }
0170       
0171 
0172       MicroElecRun* localRun = (MicroElecRun*)(run);
0173 
0174       EnergyOfIncidentPart = localRun->GetElecEneIncPart();
0175       G4double compteurTot = localRun->GetElecTotaScorer();
0176       G4double compteurPrimaire = localRun->GetElecPrimScorer();
0177       G4double compteurSec = localRun->GetElecSecoScorer();
0178       G4double compteurSup50 = localRun->GetElecSup50Scorer();
0179 
0180       G4cout << "ooo End Master ooo, Run : " << run->GetRunID() << " | Energy = " << EnergyOfIncidentPart / eV <<
0181           " eV | Number of inci Part. = " << NbOfIncidentPart << " | Tot = " << compteurTot <<
0182           " | Primaires = " << compteurPrimaire << " | Sec = " << compteurSec << G4endl <<
0183           " | Taux d'emission = " << compteurTot/ NbOfIncidentPart << G4endl;
0184 
0185       G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
0186       analysisManager->FillNtupleDColumn(0, EnergyOfIncidentPart/eV);
0187       analysisManager->FillNtupleDColumn(1, compteurTot/ NbOfIncidentPart);
0188       analysisManager->FillNtupleDColumn(2, compteurSec/ NbOfIncidentPart);
0189       analysisManager->FillNtupleDColumn(3, compteurPrimaire/ NbOfIncidentPart);
0190       analysisManager->FillNtupleDColumn(4, compteurSup50/ NbOfIncidentPart);
0191       analysisManager->AddNtupleRow();//*/
0192   }
0193 
0194 }
0195 
0196 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0197 
0198 void RunAction::EndWorker(const G4Run* run)
0199 {
0200   if(fDebug)
0201     {
0202       PrintRunInfo(run);
0203     }
0204   
0205 
0206   G4int nofEvents = run->GetNumberOfEvent();
0207   if ( nofEvents == 0 )
0208     {
0209       if(fDebug)
0210     {
0211       G4cout << "°°°°°°°°°°°°°°°° NO EVENTS TREATED IN THIS RUN ==> LEAVING RunAction::EndOfRunAction "<< G4endl;
0212     }
0213       return;
0214     }
0215   
0216  
0217 
0218   ///////////////
0219   // Complete cleanup
0220   //
0221   G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
0222   analysisManager->Clear();
0223 
0224   ///////////////
0225   // Printouts
0226   //
0227   std::map<const G4ParticleDefinition*, int>&
0228     particlesCreatedInWorld = fpTrackingAction->GetNParticlesCreatedInWorld();
0229   std::map<const G4ParticleDefinition*, int>&
0230       particlesCreatedInTarget = fpTrackingAction->GetNParticlesCreatedInTarget();
0231 
0232   G4cout << "__________ooo End Worker ooo begin_____________" << G4endl;
0233   G4cout << " Number and type of particles created outside region \"Target\" :";
0234   PrintNParticles(particlesCreatedInWorld);
0235   G4cout << "ooo End Worker ooo, Number and type of particles created in region \"Target\" :";
0236   PrintNParticles(particlesCreatedInTarget);
0237   G4cout << "__________ooo End Worker ooo end_____________" << G4endl;
0238  
0239 
0240 }
0241 
0242 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0243 
0244 void RunAction::InitializeWorker(const G4Run*)
0245 {
0246     if (fpTrackingAction == 0)
0247     {
0248         fpTrackingAction = (TrackingAction*) G4RunManager::GetRunManager()->GetUserTrackingAction();
0249 
0250         if(fpTrackingAction == 0 && isMaster == false)
0251         {
0252             G4ExceptionDescription exDescrption ;
0253             exDescrption << "fpTrackingAction is a null pointer. Has it been correctly initialized ?";
0254             G4Exception("RunAction::BeginOfRunAction","RunAction001",FatalException, exDescrption);
0255         }
0256     }
0257 
0258     fInitialized = true;
0259 }
0260 
0261 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0262 
0263 
0264 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0265 
0266 void RunAction::SetSEYFileName(G4String& name)
0267 {
0268     SEYfileName = name;
0269 }
0270 
0271 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0272 
0273 void RunAction::CreateSEYHistogram()
0274 {
0275     // Book histograms, ntuple
0276 
0277     // Create analysis manager
0278     // The choice of analysis technology is done via selection of a namespace
0279     // in Analysis.hh
0280 
0281     G4cout << "##### Create analysis manager " << "  " << this << G4endl;
0282     G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
0283     analysisManager->SetDefaultFileType("csv");
0284 
0285     G4cout << "Using " << analysisManager->GetType() << " analysis manager" << G4endl;
0286 
0287     // Create directories
0288 
0289     //analysisManager->SetHistoDirectoryName("histograms");
0290     //analysisManager->SetNtupleDirectoryName("ntuple");
0291     analysisManager->SetVerboseLevel(1);
0292 
0293     // Open an output file
0294 
0295     analysisManager->OpenFile(SEYfileName);
0296     // Creating ntuple
0297     //                                  File name ext.   tupple info
0298     analysisManager->CreateNtuple("data", "Sec. Ele. Emission");
0299     analysisManager->CreateNtupleDColumn("Initial energy (eV)");
0300     analysisManager->CreateNtupleDColumn("TEEY");
0301     analysisManager->CreateNtupleDColumn("SEEY");
0302     analysisManager->CreateNtupleDColumn("BEEY");
0303     analysisManager->CreateNtupleDColumn("EEY>50eV");
0304     analysisManager->FinishNtuple();
0305     
0306     
0307 }
0308 
0309 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0310 
0311 void RunAction::WriteSEYHistogram()
0312 {
0313     // print histogram statistics
0314     //
0315     G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
0316 
0317     // save histograms
0318     //
0319     analysisManager->Write();
0320     analysisManager->CloseFile();
0321 }
0322 
0323 
0324 
0325 
0326 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0327 
0328 void RunAction::PrintRunInfo(const G4Run* run)
0329 {
0330     G4cout << "°°°°°°°°°°°°°°°° Run is = " << run->GetRunID() << G4endl;
0331     G4cout << "°°°°°°°°°°°°°°°° Run type is = " << G4RunManager::GetRunManager()->GetRunManagerType() << G4endl;
0332     G4cout << "°°°°°°°°°°°°°°°° Event processed = " << run->GetNumberOfEventToBeProcessed() << G4endl;
0333     G4cout << "°°°°°°°°°°°°°°°° N° Event = " << run->GetNumberOfEvent() << G4endl;
0334 }
0335 
0336 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0337 void PrintNParticles(std::map<const G4ParticleDefinition*, int>& container)
0338 {
0339     std::map<const G4ParticleDefinition*, int>::iterator it;
0340     for(it = container.begin() ;
0341         it != container.end(); it ++)
0342     {
0343         G4cout << "N " << it->first->GetParticleName() << " : " << it->second << G4endl;
0344     }
0345 }
0346  
0347 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....