Geant4  10.03.p03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
B5EventAction.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: B5EventAction.cc 101036 2016-11-04 09:00:23Z gcosmo $
27 //
30 
31 #include "B5EventAction.hh"
32 #include "B5HodoscopeHit.hh"
33 #include "B5DriftChamberHit.hh"
34 #include "B5EmCalorimeterHit.hh"
35 #include "B5HadCalorimeterHit.hh"
36 #include "B5Constants.hh"
37 #include "B5Analysis.hh"
38 
39 #include "G4Event.hh"
40 #include "G4RunManager.hh"
41 #include "G4EventManager.hh"
42 #include "G4HCofThisEvent.hh"
43 #include "G4VHitsCollection.hh"
44 #include "G4SDManager.hh"
45 #include "G4SystemOfUnits.hh"
46 #include "G4ios.hh"
47 
48 
49 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
50 
53  fHodHC1ID(-1),
54  fHodHC2ID(-1),
55  fDriftHC1ID(-1),
56  fDriftHC2ID(-1),
57  fEmCalHCID(-1),
58  fHadCalHCID(-1),
59  fEmCalEdep(kNofEmCells, 0.),
60  fHadCalEdep(kNofHadCells, 0.)
61 {
62  // set printing per each event
64 }
65 
66 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
67 
69 {}
70 
71 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
72 
74 {
75  if (fHodHC1ID==-1) {
76  auto sdManager = G4SDManager::GetSDMpointer();
77  fHodHC1ID = sdManager->GetCollectionID("hodoscope1/hodoscopeColl");
78  fHodHC2ID = sdManager->GetCollectionID("hodoscope2/hodoscopeColl");
79  fDriftHC1ID = sdManager->GetCollectionID("chamber1/driftChamberColl");
80  fDriftHC2ID = sdManager->GetCollectionID("chamber2/driftChamberColl");
81  fEmCalHCID = sdManager->GetCollectionID("EMcalorimeter/EMcalorimeterColl");
82  fHadCalHCID = sdManager->GetCollectionID("HadCalorimeter/HadCalorimeterColl");
83  }
84 }
85 
86 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
87 
89 {
90  auto hce = event->GetHCofThisEvent();
91  if (!hce) {
93  msg << "No hits collection of this event found." << G4endl;
94  G4Exception("B5EventAction::EndOfEventAction()",
95  "B5Code001", JustWarning, msg);
96  return;
97  }
98 
99  // Get hits collections
100  auto hHC1
101  = static_cast<B5HodoscopeHitsCollection*>(hce->GetHC(fHodHC1ID));
102 
103  auto hHC2
104  = static_cast<B5HodoscopeHitsCollection*>(hce->GetHC(fHodHC2ID));
105 
106  auto dHC1
107  = static_cast<B5DriftChamberHitsCollection*>(hce->GetHC(fDriftHC1ID));
108 
109  auto dHC2
110  = static_cast<B5DriftChamberHitsCollection*>(hce->GetHC(fDriftHC2ID));
111 
112  auto ecHC
113  = static_cast<B5EmCalorimeterHitsCollection*>(hce->GetHC(fEmCalHCID));
114 
115  auto hcHC
116  = static_cast<B5HadCalorimeterHitsCollection*>(hce->GetHC(fHadCalHCID));
117 
118  if ( (!hHC1) || (!hHC2) || (!dHC1) || (!dHC2) || (!ecHC) || (!hcHC) ) {
120  msg << "Some of hits collections of this event not found." << G4endl;
121  G4Exception("B5EventAction::EndOfEventAction()",
122  "B5Code001", JustWarning, msg);
123  return;
124  }
125 
126  //
127  // Fill histograms & ntuple
128  //
129 
130  // Get analysis manager
131  auto analysisManager = G4AnalysisManager::Instance();
132 
133  // Fill histograms
134 
135  auto nhit = dHC1->entries();
136  analysisManager->FillH1(0, nhit );
137 
138  for (auto i=0;i<nhit ;i++) {
139  auto hit = (*dHC1)[i];
140  auto localPos = hit->GetLocalPos();
141  analysisManager->FillH2(0, localPos.x(), localPos.y());
142  }
143 
144  nhit = dHC2->entries();
145  analysisManager->FillH1(1, nhit );
146 
147  for (auto i=0;i<nhit ;i++) {
148  auto hit = (*dHC2)[i];
149  auto localPos = hit->GetLocalPos();
150  analysisManager->FillH2(1, localPos.x(), localPos.y());
151  }
152 
153  // Fill ntuple
154 
155  // Dc1Hits
156  analysisManager->FillNtupleIColumn(0, dHC1->entries());
157  // Dc2Hits
158  analysisManager->FillNtupleIColumn(1, dHC1->entries());
159 
160  // ECEnergy
161  G4int totalEmHit = 0;
162  G4double totalEmE = 0.;
163  for (auto i=0;i<kNofEmCells;i++) {
164  auto hit = (*ecHC)[i];
165  auto edep = hit->GetEdep();
166  if (edep>0.) {
167  totalEmHit++;
168  totalEmE += edep;
169  }
170  fEmCalEdep[i] = edep;
171  }
172  analysisManager->FillNtupleDColumn(2, totalEmE);
173 
174  // HCEnergy
175  G4int totalHadHit = 0;
176  G4double totalHadE = 0.;
177  for (auto i=0;i<kNofHadCells;i++) {
178  auto hit = (*hcHC)[i];
179  auto edep = hit->GetEdep();
180  if (edep>0.) {
181  totalHadHit++;
182  totalHadE += edep;
183  }
184  fHadCalEdep[i] = edep;
185  }
186  analysisManager->FillNtupleDColumn(3, totalHadE);
187 
188  // Time 1
189  for (auto i=0;i<hHC1->entries();i++) {
190  analysisManager->FillNtupleDColumn(4,(*hHC1)[i]->GetTime());
191  }
192 
193  // Time 2
194  for (auto i=0;i<hHC2->entries();i++) {
195  analysisManager->FillNtupleDColumn(5,(*hHC2)[i]->GetTime());
196  }
197 
198  analysisManager->AddNtupleRow();
199 
200  //
201  // Print diagnostics
202  //
203 
204  auto printModulo = G4RunManager::GetRunManager()->GetPrintProgress();
205  if ( printModulo==0 || event->GetEventID() % printModulo != 0) return;
206 
207  auto primary = event->GetPrimaryVertex(0)->GetPrimary(0);
208  G4cout
209  << G4endl
210  << ">>> Event " << event->GetEventID() << " >>> Simulation truth : "
211  << primary->GetG4code()->GetParticleName()
212  << " " << primary->GetMomentum() << G4endl;
213 
214  // Hodoscope 1
215  nhit = hHC1->entries();
216  G4cout << "Hodoscope 1 has " << nhit << " hits." << G4endl;
217  for (auto i=0;i<nhit ;i++) {
218  auto hit = (*hHC1)[i];
219  hit->Print();
220  }
221 
222  // Hodoscope 2
223  nhit = hHC2->entries();
224  G4cout << "Hodoscope 2 has " << nhit << " hits." << G4endl;
225  for (auto i=0;i<nhit ;i++) {
226  auto hit = (*hHC2)[i];
227  hit->Print();
228  }
229 
230  // Drift chamber 1
231  nhit = dHC1->entries();
232  G4cout << "Drift Chamber 1 has " << nhit << " hits." << G4endl;
233  for (auto layer=0;layer<kNofChambers;layer++) {
234  for (auto i=0;i<nhit ;i++) {
235  auto hit = (*dHC1)[i];
236  if (hit->GetLayerID()==layer) hit->Print();
237  }
238  }
239 
240  // Drift chamber 2
241  nhit = dHC2->entries();
242  G4cout << "Drift Chamber 2 has " << nhit << " hits." << G4endl;
243  for (auto layer=0;layer<kNofChambers;layer++) {
244  for (auto i=0;i<nhit ;i++) {
245  auto hit = (*dHC2)[i];
246  if (hit->GetLayerID()==layer) hit->Print();
247  }
248  }
249 
250  // EM calorimeter
251  G4cout << "EM Calorimeter has " << totalEmHit << " hits. Total Edep is "
252  << totalEmE/MeV << " (MeV)" << G4endl;
253 
254  // Had calorimeter
255  G4cout << "Hadron Calorimeter has " << totalHadHit << " hits. Total Edep is "
256  << totalHadE/MeV << " (MeV)" << G4endl;
257 }
258 
259 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
G4int GetPrintProgress()
Definition of the B5EventAction class.
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
void SetPrintProgress(G4int i)
constexpr G4int kNofChambers
Definition: B5Constants.hh:38
constexpr G4int kNofHadCells
Definition: B5Constants.hh:44
int G4int
Definition: G4Types.hh:78
G4int GetEventID() const
Definition: G4Event.hh:151
Definition of the B5EmCalorimeterHit class.
G4GLOB_DLL std::ostream G4cout
Definition of the B5HodoscopeHit class.
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
Selection of the analysis technology.
static G4RunManager * GetRunManager()
Definition: G4RunManager.cc:79
Definition of constants.
static G4SDManager * GetSDMpointer()
Definition: G4SDManager.cc:40
virtual void BeginOfEventAction(const G4Event *)
virtual ~B5EventAction()
#define G4endl
Definition: G4ios.hh:61
static constexpr double MeV
Definition: G4SIunits.hh:214
double G4double
Definition: G4Types.hh:76
constexpr G4int kNofEmCells
Definition: B5Constants.hh:41
virtual void EndOfEventAction(const G4Event *)
Definition of the B5DriftChamberHit class.
Definition of the B5HadCalorimeterHit class.