Geant4  9.6.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$
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 "G4GenericMessenger.hh"
39 #include "G4UnitsTable.hh"
40 
41 #include "Randomize.hh"
42 #include <iomanip>
43 
44 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
45 
48  fMessenger(0),
49  fPrintModulo(1)
50 {
51  // Define /B4/event commands using generic messenger class
52  fMessenger = new G4GenericMessenger(this, "/B4/event/", "Event control");
53 
54  // Define /B4/event/setPrintModulo command
55  G4GenericMessenger::Command& setPrintModulo
56  = fMessenger->DeclareProperty("setPrintModulo",
57  fPrintModulo,
58  "Print events modulo n");
59  setPrintModulo.SetRange("value>0");
60 }
61 
62 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
63 
65 {
66  delete fMessenger;
67 }
68 
69 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
70 
72 B4dEventAction::GetHitsCollection(const G4String& hcName,
73  const G4Event* event) const
74 {
75  G4int hcID
77  G4THitsMap<G4double>* hitsCollection
78  = static_cast<G4THitsMap<G4double>*>(
79  event->GetHCofThisEvent()->GetHC(hcID));
80 
81  if ( ! hitsCollection ) {
82  G4cerr << "Cannot access hitsCollection " << hcName << G4endl;
83  exit(1);
84  }
85 
86  return hitsCollection;
87 }
88 
89 G4double B4dEventAction::GetSum(G4THitsMap<G4double>* hitsMap) const
90 {
91  G4double sumValue = 0;
92  std::map<G4int, G4double*>::iterator it;
93  for ( it = hitsMap->GetMap()->begin(); it != hitsMap->GetMap()->end(); it++) {
94  sumValue += *(it->second);
95  }
96  return sumValue;
97 }
98 
99 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
100 
101 void B4dEventAction::PrintEventStatistics(
102  G4double absoEdep, G4double absoTrackLength,
103  G4double gapEdep, G4double gapTrackLength) const
104 {
105  // Print event statistics
106  //
107  G4cout
108  << " Absorber: total energy: "
109  << std::setw(7) << G4BestUnit(absoEdep, "Energy")
110  << " total track length: "
111  << std::setw(7) << G4BestUnit(absoTrackLength, "Length")
112  << G4endl
113  << " Gap: total energy: "
114  << std::setw(7) << G4BestUnit(gapEdep, "Energy")
115  << " total track length: "
116  << std::setw(7) << G4BestUnit(gapTrackLength, "Length")
117  << G4endl;
118 }
119 
120 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
121 
123 {
124 
125  G4int eventID = event->GetEventID();
126  if ( eventID % fPrintModulo == 0 ) {
127  G4cout << "\n---> Begin of event: " << eventID << G4endl;
128  //CLHEP::HepRandom::showEngineStatus();
129  }
130 }
131 
132 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
133 
135 {
136  // Get sum value from hits collections
137  //
138  G4double absoEdep
139  = GetSum(GetHitsCollection("Absorber/Edep", event));
140 
141  G4double gapEdep
142  = GetSum(GetHitsCollection("Gap/Edep", event));
143 
144  G4double absoTrackLength
145  = GetSum(GetHitsCollection("Absorber/TrackLength", event));
146 
147  G4double gapTrackLength
148  = GetSum(GetHitsCollection("Gap/TrackLength", event));
149 
150  // get analysis manager
151  G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
152 
153  // fill histograms
154  //
155  analysisManager->FillH1(1, absoEdep);
156  analysisManager->FillH1(2, gapEdep);
157  analysisManager->FillH1(3, absoTrackLength);
158  analysisManager->FillH1(4, gapTrackLength);
159 
160  // fill ntuple
161  //
162  analysisManager->FillNtupleDColumn(0, absoEdep);
163  analysisManager->FillNtupleDColumn(1, gapEdep);
164  analysisManager->FillNtupleDColumn(2, absoTrackLength);
165  analysisManager->FillNtupleDColumn(3, gapTrackLength);
166  analysisManager->AddNtupleRow();
167 
168  //print per event (modulo n)
169  //
170  G4int eventID = event->GetEventID();
171  if ( eventID % fPrintModulo == 0) {
172  G4cout << "---> End of event: " << eventID << G4endl;
173 
174  PrintEventStatistics(absoEdep, absoTrackLength, gapEdep, gapTrackLength);
175  }
176 }
177 
178 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......