Geant4  9.6.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CCalEndOfEventAction.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 //
27 // File: CCalEndOfEventAction.cc
28 // Description: CCalEndOfEventAction provides User actions at end of event
30 #include "CCalEndOfEventAction.hh"
31 #include "CCaloSD.hh"
33 #include "CCalG4HitCollection.hh"
34 #include "CCalG4Hit.hh"
35 #include "CCaloOrganization.hh"
36 #include "CCalSDList.hh"
37 #include "CCalSteppingAction.hh"
38 
39 #include "G4ios.hh"
40 #include "G4SystemOfUnits.hh"
41 #include "G4Event.hh"
42 #include "G4SDManager.hh"
43 #include "G4HCofThisEvent.hh"
44 #include "G4RunManager.hh"
45 #include "G4UserSteppingAction.hh"
46 
47 #include <iostream>
48 #include <vector>
49 #include <map>
50 
51 #ifdef G4ANALYSIS_USE
52 #include "CCalAnalysis.hh"
53 #endif
54 
55 //#define debug
56 //#define ddebug
57 
58 
60  isInitialized(false),SDnames(0),numberOfSD(0) {
61 
62  primaryGenerator = pg;
63 #ifdef debug
64  G4cout << "Instantiate CCalEndOfEventAction" << G4endl;
65 #endif
66 
67  G4cout << "Now Instantiate stepping action" << G4endl;
68  instanciateSteppingAction();
69 
70  G4cout << "Get Calorimter organisation" << G4endl;
71  theOrg = new CCaloOrganization;
72  G4cout << "end of instantiation of EndofEventAction" << G4endl;
73 }
74 
75 
77 
78  if (theOrg)
79  delete theOrg;
80  if (SDnames)
81  delete[] SDnames;
82  G4cout << "CCalEndOfEventAction deleted" << G4endl;
83 }
84 
85 
86 void CCalEndOfEventAction::initialize() {
87 
88  isInitialized = true;
90 #ifdef debug
91  G4cout << "CCalEndOfEventAction look for " << numberOfSD
92  << " calorimeter-like SD" << G4endl;
93 #endif
94  if (numberOfSD > 0) {
95  G4int n = numberOfSD;
96  if (n < 2) n = 2;
97  SDnames = new nameType[n];
98  }
99  for (int i=0; i<numberOfSD; i++) {
100  SDnames[i] = G4String(CCalSDList::getInstance()->getCaloSDName(i));
101 #ifdef debug
102  G4cout << "CCalEndOfEventAction: found SD " << i << " name "
103  << SDnames[i] << G4endl;
104 #endif
105  }
106 }
107 
108 
110  G4cout << "\n---> Begin of event: " << evt->GetEventID() << G4endl;
111 }
112 
113 
115 
116 #ifdef debug
117  G4cout << G4endl << "=== Begin of EndOfEventAction === " << G4endl;
118 #endif
119 
120  if (!isInitialized) initialize();
121 
122  theSteppingAction->endOfEvent();
123 
124  //
125  // Look for the Hit Collection
126  //
127  G4HCofThisEvent* allHC = evt->GetHCofThisEvent();
128  if (allHC == 0) {
129 #ifdef debug
130  G4cout << "CCalEndOfEventAction: No Hit Collection in this event"
131  << G4endl;
132 #endif
133  return;
134  }
135 
136  //
137  // hits info
138  //
139 
140  //Now make summary
141  float hcalE[28], ecalE[49], fullE=0., edec=0, edhc=0;
142  int i = 0;
143  for (i = 0; i < 28; i++) {hcalE[i]=0.;}
144  for (i = 0; i < 49; i++) {ecalE[i]=0.;}
145 
146  float* edep = new float[numberOfSD];
147  int nhit=0;
148  for (i = 0; i < numberOfSD; i++){
149 
150  //
151  // Look for the Hit Collection
152  //
153  edep[i] = 0;
154  int caloHCid = G4SDManager::GetSDMpointer()->GetCollectionID(SDnames[i]);
155 
156  if (caloHCid >= 0) {
157  CCalG4HitCollection* theHC =
158  (CCalG4HitCollection*) allHC->GetHC(caloHCid);
159 
160  if (theHC != 0) {
161 
162  G4int nentries = theHC->entries();
163 #ifdef debug
164  G4cout << " There are " << nentries << " hits in " << SDnames[i]
165  << " :" << G4endl;
166 #endif
167 
168  if (nentries > 0) {
169 
170  int j;
171  for (j=0; j<nentries; j++){
172 #ifdef ddebug
173  G4cout << "Hit " << j;
174 #endif
175  CCalG4Hit* aHit = (*theHC)[j];
176  float En = aHit->getEnergyDeposit();
177  int unitID = aHit->getUnitID();
178  int id=-1;
179  if (unitID > 0 && unitID < 29) {
180  id = unitID - 1; // HCal
181  hcalE[id] += En/GeV;
182  } else {
183  int i0 = unitID/4096;
184  int i1 = (unitID/64)%64;
185  int i2 = unitID%64;
186  if (i0 == 1 && i1 < 8 && i2 < 8) {
187  id = i1*7 + i2; // ECal
188  ecalE[id] += En/GeV;
189  }
190  }
191 #ifdef ddebug
192  G4cout << " with Energy = " << En/MeV << " MeV in Unit " << unitID
193  << " " << id << G4endl;
194 #endif
195  fullE += En/GeV;
196  edep[i] += En/GeV;
197  nhit++;
198  }
199 #ifdef ddebug
200  G4cout << " ===> Total Energy Deposit in this Calorimeter = "
201  << edep[i]*1000.0 << " MeV " << G4endl;
202 #endif
203  }
204  }
205  }
206  if (SDnames[i] == "HadronCalorimeter") {
207  edhc = edep[i];
208  } else if (SDnames[i] == "CrystalMatrix") {
209  edec = edep[i];
210  }
211  }
212 
213  delete[] edep;
214 
215 #ifdef G4ANALYSIS_USE
216  G4ThreeVector pos = primaryGenerator->GetParticlePosition();
217  float ener = primaryGenerator->GetParticleEnergy()/GeV;
218  float x = pos.x()/mm;
219  float y = pos.y()/mm;
220  float z = pos.z()/mm;
221 
222  CCalAnalysis* analysis = CCalAnalysis::getInstance();
223  analysis->InsertEnergy(fullE);
224  analysis->InsertEnergyHcal(hcalE);
225  analysis->InsertEnergyEcal(ecalE);
226  analysis->setNtuple(hcalE, ecalE, ener, x, y, z, fullE, edec, edhc);
227  analysis->EndOfEvent(nhit);
228  for (i = 0; i < numberOfSD; i++){
229  int caloHCid = G4SDManager::GetSDMpointer()->GetCollectionID(SDnames[i]);
230  if (caloHCid >= 0) {
231  CCalG4HitCollection* theHC =
232  (CCalG4HitCollection*) allHC->GetHC(caloHCid);
233  if (theHC != 0) {
234  G4int nentries = theHC->entries();
235  if (nentries > 0) {
236  for (G4int k=0; k<nentries; k++) {
237  CCalG4Hit* aHit = (*theHC)[k];
238  analysis->InsertTimeProfile(k,aHit->getTimeSlice(),
239  aHit->getEnergyDeposit()/GeV);
240  }
241  }
242  }
243  }
244  }
245 #endif
246 }
247 
248 
249 void CCalEndOfEventAction::instanciateSteppingAction(){
250 
252 
253  if (theUA == 0) {
254 #ifdef debug
255  G4cout << " CCalEndOfEventAction::instanciateSteppingAction creates"
256  << " CCalSteppingAction" << G4endl;
257 #endif
258  theSteppingAction = new CCalSteppingAction;
259  G4RunManager::GetRunManager()->SetUserAction(theSteppingAction);
260  }
261 
262 }