Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:17:16

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 /// \file B4/B4d/src/EventAction.cc
0028 /// \brief Implementation of the B4d::EventAction class
0029 
0030 #include "EventAction.hh"
0031 
0032 #include "G4AnalysisManager.hh"
0033 #include "G4Event.hh"
0034 #include "G4HCofThisEvent.hh"
0035 #include "G4RunManager.hh"
0036 #include "G4SDManager.hh"
0037 #include "G4THitsMap.hh"
0038 #include "G4UnitsTable.hh"
0039 
0040 #include <iomanip>
0041 
0042 namespace B4d
0043 {
0044 
0045 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0046 
0047 G4THitsMap<G4double>* EventAction::GetHitsCollection(G4int hcID, const G4Event* event) const
0048 {
0049   auto hitsCollection = static_cast<G4THitsMap<G4double>*>(event->GetHCofThisEvent()->GetHC(hcID));
0050 
0051   if (!hitsCollection) {
0052     G4ExceptionDescription msg;
0053     msg << "Cannot access hitsCollection ID " << hcID;
0054     G4Exception("EventAction::GetHitsCollection()", "MyCode0003", FatalException, msg);
0055   }
0056 
0057   return hitsCollection;
0058 }
0059 
0060 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0061 
0062 G4double EventAction::GetSum(G4THitsMap<G4double>* hitsMap) const
0063 {
0064   G4double sumValue = 0.;
0065   for (auto it : *hitsMap->GetMap()) {
0066     // hitsMap->GetMap() returns the map of std::map<G4int, G4double*>
0067     sumValue += *(it.second);
0068   }
0069   return sumValue;
0070 }
0071 
0072 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0073 
0074 void EventAction::PrintEventStatistics(G4double absoEdep, G4double absoTrackLength,
0075                                        G4double gapEdep, G4double gapTrackLength) const
0076 {
0077   // Print event statistics
0078   //
0079   G4cout << "   Absorber: total energy: " << std::setw(7) << G4BestUnit(absoEdep, "Energy")
0080          << "       total track length: " << std::setw(7) << G4BestUnit(absoTrackLength, "Length")
0081          << G4endl << "        Gap: total energy: " << std::setw(7) << G4BestUnit(gapEdep, "Energy")
0082          << "       total track length: " << std::setw(7) << G4BestUnit(gapTrackLength, "Length")
0083          << G4endl;
0084 }
0085 
0086 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0087 
0088 void EventAction::BeginOfEventAction(const G4Event* /*event*/) {}
0089 
0090 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0091 
0092 void EventAction::EndOfEventAction(const G4Event* event)
0093 {
0094   // Get hist collections IDs
0095   if (fAbsoEdepHCID == -1) {
0096     fAbsoEdepHCID = G4SDManager::GetSDMpointer()->GetCollectionID("Absorber/Edep");
0097     fGapEdepHCID = G4SDManager::GetSDMpointer()->GetCollectionID("Gap/Edep");
0098     fAbsoTrackLengthHCID = G4SDManager::GetSDMpointer()->GetCollectionID("Absorber/TrackLength");
0099     fGapTrackLengthHCID = G4SDManager::GetSDMpointer()->GetCollectionID("Gap/TrackLength");
0100   }
0101 
0102   // Get sum values from hits collections
0103   //
0104   auto absoEdep = GetSum(GetHitsCollection(fAbsoEdepHCID, event));
0105   auto gapEdep = GetSum(GetHitsCollection(fGapEdepHCID, event));
0106 
0107   auto absoTrackLength = GetSum(GetHitsCollection(fAbsoTrackLengthHCID, event));
0108   auto gapTrackLength = GetSum(GetHitsCollection(fGapTrackLengthHCID, event));
0109 
0110   // get analysis manager
0111   auto analysisManager = G4AnalysisManager::Instance();
0112 
0113   // fill histograms
0114   //
0115   analysisManager->FillH1(0, absoEdep);
0116   analysisManager->FillH1(1, gapEdep);
0117   analysisManager->FillH1(2, absoTrackLength);
0118   analysisManager->FillH1(3, gapTrackLength);
0119 
0120   // fill ntuple
0121   //
0122   analysisManager->FillNtupleDColumn(0, absoEdep);
0123   analysisManager->FillNtupleDColumn(1, gapEdep);
0124   analysisManager->FillNtupleDColumn(2, absoTrackLength);
0125   analysisManager->FillNtupleDColumn(3, gapTrackLength);
0126   analysisManager->AddNtupleRow();
0127 
0128   // print per event (modulo n)
0129   //
0130   auto eventID = event->GetEventID();
0131   auto printModulo = G4RunManager::GetRunManager()->GetPrintProgress();
0132   if ((printModulo > 0) && (eventID % printModulo == 0)) {
0133     PrintEventStatistics(absoEdep, absoTrackLength, gapEdep, gapTrackLength);
0134     G4cout << "--> End of event: " << eventID << "\n" << G4endl;
0135   }
0136 }
0137 
0138 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0139 
0140 }  // namespace B4d