Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-23 09:20:23

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 
0037 void PrintNParticles(std::map<const G4ParticleDefinition*, int>& container);
0038 
0039 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0040 
0041 RunAction::RunAction()
0042 {
0043  fFileName = "microelectronics";
0044  fpTrackingAction = 0;
0045  fInitialized = 0;
0046  fDebug = false;
0047 }
0048 
0049 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0050 
0051 RunAction::~RunAction()
0052 {}
0053 
0054 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0055 
0056 void RunAction::BeginOfRunAction(const G4Run* run)
0057 {  
0058   // In this example, we considered that the same class was
0059   // used for both master and worker threads.
0060   // However, in case the run action is long,
0061   // for better code review, this practice is not recommanded.
0062   //
0063   
0064   if(isMaster) // WARNING : in sequential mode, isMaster == true    
0065     BeginMaster(run);    
0066   else 
0067     BeginWorker(run);
0068 }
0069 
0070 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0071 
0072 void RunAction::EndOfRunAction(const G4Run* run)
0073 {
0074   if(isMaster)   
0075     EndMaster(run);
0076   else  
0077     EndWorker(run);
0078 }
0079 
0080 
0081 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0082 
0083 void RunAction::BeginMaster(const G4Run* run)
0084 {
0085   bool sequential = (G4RunManager::GetRunManager()->GetRunManagerType() == G4RunManager::sequentialRM);
0086   
0087   if(fDebug)
0088     {
0089       G4cout << "°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°" << G4endl;
0090       if(!sequential)
0091     G4cout << "°°°°°°°°°°°°°°°° RunAction::BeginMaster" << G4endl;
0092       PrintRunInfo(run);
0093       G4cout << "°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°" << G4endl;
0094     }
0095   
0096   if(sequential)
0097     {
0098       if(!fInitialized) 
0099     InitializeWorker(run);
0100       // Note: fpTrackingAction could be used as a flag for initialization instead      
0101 
0102       CreateHistogram();
0103     }
0104 }
0105 
0106 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0107 
0108 void RunAction::BeginWorker(const G4Run* run)
0109 {
0110   if(fDebug)
0111     {
0112       G4cout << "°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°" << G4endl;
0113       G4cout << "°°°°°°°°°°°°°°°° RunAction::BeginWorker" << G4endl;
0114       PrintRunInfo(run);
0115       G4cout << "°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°" << G4endl;
0116     }
0117   if(!fInitialized) 
0118     InitializeWorker(run);
0119   
0120   CreateHistogram();
0121 }
0122 
0123 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0124 
0125 void RunAction::EndMaster(const G4Run* run)
0126 {
0127   bool sequential = (G4RunManager::GetRunManager()->GetRunManagerType() 
0128              == G4RunManager::sequentialRM);
0129   if(sequential)    
0130     EndWorker(run);    
0131 }
0132 
0133 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0134 
0135 void RunAction::EndWorker(const G4Run* run)
0136 {
0137   if(fDebug)
0138     {
0139       PrintRunInfo(run);
0140     }
0141   
0142   G4int nofEvents = run->GetNumberOfEvent();
0143   if ( nofEvents == 0 )
0144     {
0145       if(fDebug)
0146     {
0147       G4cout << "°°°°°°°°°°°°°°°° NO EVENTS TREATED IN THIS RUN ==> LEAVING RunAction::EndOfRunAction "<< G4endl;
0148     }
0149       return;
0150     }
0151   
0152   ///////////////
0153   // Write Histo
0154   //
0155   WriteHistogram();
0156 
0157   ///////////////
0158   // Complete cleanup
0159   //
0160   G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
0161   analysisManager->Clear();
0162 
0163   ///////////////
0164   // Printouts
0165   //
0166   std::map<const G4ParticleDefinition*, int>&
0167     particlesCreatedInWorld = fpTrackingAction->GetNParticlesCreatedInWorld();
0168   
0169   G4cout << "Number and type of particles created outside region \"Target\" :" << G4endl;
0170   
0171   PrintNParticles(particlesCreatedInWorld);
0172   
0173   G4cout << "_______________________" << G4endl;
0174   
0175   std::map<const G4ParticleDefinition*, int>&
0176     particlesCreatedInTarget = fpTrackingAction->GetNParticlesCreatedInTarget();
0177   
0178   G4cout << "Number and type of particles created in region \"Target\" :" << G4endl;
0179   
0180   PrintNParticles(particlesCreatedInTarget);
0181   
0182 }
0183 
0184 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0185 
0186 void RunAction::InitializeWorker(const G4Run*)
0187 {
0188     if (fpTrackingAction == 0)
0189     {
0190         fpTrackingAction = (TrackingAction*) G4RunManager::GetRunManager()->GetUserTrackingAction();
0191 
0192         if(fpTrackingAction == 0 && isMaster == false)
0193         {
0194             G4ExceptionDescription exDescrption ;
0195             exDescrption << "fpTrackingAction is a null pointer. Has it been correctly initialized ?";
0196             G4Exception("RunAction::BeginOfRunAction","RunAction001",FatalException, exDescrption);
0197         }
0198     }
0199 
0200     fInitialized = true;
0201 }
0202 
0203 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0204 
0205 void RunAction::CreateHistogram()
0206 {
0207     // Book histograms, ntuple
0208 
0209     // Create analysis manager
0210     // The choice of analysis technology is done via selection of a namespace
0211     // in Analysis.hh
0212 
0213     G4cout << "##### Create analysis manager " << "  " << this << G4endl;
0214     G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
0215   analysisManager->SetDefaultFileType("root");
0216 
0217     G4cout << "Using " << analysisManager->GetType() << " analysis manager" << G4endl;
0218 
0219     // Create directories
0220 
0221     //analysisManager->SetHistoDirectoryName("histograms");
0222     //analysisManager->SetNtupleDirectoryName("ntuple");
0223     analysisManager->SetVerboseLevel(1);
0224 
0225     // Open an output file
0226     
0227     analysisManager->OpenFile(fFileName);
0228 
0229     // Creating ntuple
0230 
0231     analysisManager->CreateNtuple("microelectronics", "physics");
0232     analysisManager->CreateNtupleDColumn("flagParticle");
0233     analysisManager->CreateNtupleDColumn("flagProcess");
0234     analysisManager->CreateNtupleDColumn("x");
0235     analysisManager->CreateNtupleDColumn("y");
0236     analysisManager->CreateNtupleDColumn("z");
0237     analysisManager->CreateNtupleDColumn("totalEnergyDeposit");
0238     analysisManager->CreateNtupleDColumn("stepLength");
0239     analysisManager->CreateNtupleDColumn("kineticEnergyDifference");
0240     analysisManager->FinishNtuple();
0241 }
0242 
0243 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0244 
0245 void RunAction::WriteHistogram()
0246 {
0247     // print histogram statistics
0248     //
0249     G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
0250 
0251     // save histograms
0252     //
0253     analysisManager->Write();
0254     analysisManager->CloseFile();
0255 }
0256 
0257 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0258 
0259 void RunAction::PrintRunInfo(const G4Run* run)
0260 {
0261     G4cout << "°°°°°°°°°°°°°°°° Run is = " << run->GetRunID() << G4endl;
0262     G4cout << "°°°°°°°°°°°°°°°° Run type is = " << G4RunManager::GetRunManager()->GetRunManagerType() << G4endl;
0263     G4cout << "°°°°°°°°°°°°°°°° Event processed = " << run->GetNumberOfEventToBeProcessed() << G4endl;
0264     G4cout << "°°°°°°°°°°°°°°°° N° Event = " << run->GetNumberOfEvent() << G4endl;
0265 }
0266 
0267 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0268 void PrintNParticles(std::map<const G4ParticleDefinition*, int>& container)
0269 {
0270     std::map<const G4ParticleDefinition*, int>::iterator it;
0271     for(it = container.begin() ;
0272         it != container.end(); it ++)
0273     {
0274         G4cout << "N " << it->first->GetParticleName() << " : " << it->second << G4endl;
0275     }
0276 }
0277  
0278 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....