Geant4  10.03.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
B4dEventAction.cc
Go to the documentation of this file.
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 // $Id: B4dEventAction.cc 100946 2016-11-03 11:28:08Z gcosmo $
27 //
30 
31 #include "B4dEventAction.hh"
32 #include "B4Analysis.hh"
33 
34 #include "G4RunManager.hh"
35 #include "G4Event.hh"
36 #include "G4SDManager.hh"
37 #include "G4HCofThisEvent.hh"
38 #include "G4UnitsTable.hh"
39 
40 #include "Randomize.hh"
41 #include <iomanip>
42 
43 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
44 
47  fAbsoEdepHCID(-1),
48  fGapEdepHCID(-1),
49  fAbsoTrackLengthHCID(-1),
50  fGapTrackLengthHCID(-1)
51 {}
52 
53 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
54 
56 {}
57 
58 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
59 
61 B4dEventAction::GetHitsCollection(G4int hcID,
62  const G4Event* event) const
63 {
64  auto hitsCollection
65  = static_cast<G4THitsMap<G4double>*>(
66  event->GetHCofThisEvent()->GetHC(hcID));
67 
68  if ( ! hitsCollection ) {
70  msg << "Cannot access hitsCollection ID " << hcID;
71  G4Exception("B4dEventAction::GetHitsCollection()",
72  "MyCode0003", FatalException, msg);
73  }
74 
75  return hitsCollection;
76 }
77 
78 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
79 
80 G4double B4dEventAction::GetSum(G4THitsMap<G4double>* hitsMap) const
81 {
82  G4double sumValue = 0.;
83  for ( auto it : *hitsMap->GetMap() ) {
84  // hitsMap->GetMap() returns the map of std::map<G4int, G4double*>
85  sumValue += *(it.second);
86  }
87  return sumValue;
88 }
89 
90 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
91 
92 void B4dEventAction::PrintEventStatistics(
93  G4double absoEdep, G4double absoTrackLength,
94  G4double gapEdep, G4double gapTrackLength) const
95 {
96  // Print event statistics
97  //
98  G4cout
99  << " Absorber: total energy: "
100  << std::setw(7) << G4BestUnit(absoEdep, "Energy")
101  << " total track length: "
102  << std::setw(7) << G4BestUnit(absoTrackLength, "Length")
103  << G4endl
104  << " Gap: total energy: "
105  << std::setw(7) << G4BestUnit(gapEdep, "Energy")
106  << " total track length: "
107  << std::setw(7) << G4BestUnit(gapTrackLength, "Length")
108  << G4endl;
109 }
110 
111 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
112 
114 {}
115 
116 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
117 
119 {
120  // Get hist collections IDs
121  if ( fAbsoEdepHCID == -1 ) {
122  fAbsoEdepHCID
123  = G4SDManager::GetSDMpointer()->GetCollectionID("Absorber/Edep");
124  fGapEdepHCID
126  fAbsoTrackLengthHCID
127  = G4SDManager::GetSDMpointer()->GetCollectionID("Absorber/TrackLength");
128  fGapTrackLengthHCID
129  = G4SDManager::GetSDMpointer()->GetCollectionID("Gap/TrackLength");
130  }
131 
132  // Get sum values from hits collections
133  //
134  auto absoEdep = GetSum(GetHitsCollection(fAbsoEdepHCID, event));
135  auto gapEdep = GetSum(GetHitsCollection(fGapEdepHCID, event));
136 
137  auto absoTrackLength
138  = GetSum(GetHitsCollection(fAbsoTrackLengthHCID, event));
139  auto gapTrackLength
140  = GetSum(GetHitsCollection(fGapTrackLengthHCID, event));
141 
142  // get analysis manager
143  auto analysisManager = G4AnalysisManager::Instance();
144 
145  // fill histograms
146  //
147  analysisManager->FillH1(0, absoEdep);
148  analysisManager->FillH1(1, gapEdep);
149  analysisManager->FillH1(2, absoTrackLength);
150  analysisManager->FillH1(3, gapTrackLength);
151 
152  // fill ntuple
153  //
154  analysisManager->FillNtupleDColumn(0, absoEdep);
155  analysisManager->FillNtupleDColumn(1, gapEdep);
156  analysisManager->FillNtupleDColumn(2, absoTrackLength);
157  analysisManager->FillNtupleDColumn(3, gapTrackLength);
158  analysisManager->AddNtupleRow();
159 
160  //print per event (modulo n)
161  //
162  auto eventID = event->GetEventID();
163  auto printModulo = G4RunManager::GetRunManager()->GetPrintProgress();
164  if ( ( printModulo > 0 ) && ( eventID % printModulo == 0 ) ) {
165  G4cout << "---> End of event: " << eventID << G4endl;
166  PrintEventStatistics(absoEdep, absoTrackLength, gapEdep, gapTrackLength);
167  }
168 }
169 
170 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
virtual void BeginOfEventAction(const G4Event *event)
G4int GetPrintProgress()
G4int GetCollectionID(G4String colName)
Definition: G4SDManager.cc:135
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
#define G4BestUnit(a, b)
#define G4_USE_G4BESTUNIT_FOR_VERBOSE 1
int G4int
Definition: G4Types.hh:78
G4GLOB_DLL std::ostream G4cout
virtual void EndOfEventAction(const G4Event *event)
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
static G4RunManager * GetRunManager()
Definition: G4RunManager.cc:79
static G4SDManager * GetSDMpointer()
Definition: G4SDManager.cc:40
Definition of the B4dEventAction class.
std::map< G4int, T * > * GetMap() const
Definition: G4THitsMap.hh:99
virtual ~B4dEventAction()
#define G4endl
Definition: G4ios.hh:61
double G4double
Definition: G4Types.hh:76