Geant4  10.01.p03
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 #include "CCalAnalysis.hh"
52 
53 //#define debug
54 //#define ddebug
55 
56 
58  isInitialized(false),SDnames(0),numberOfSD(0) {
59 
60  primaryGenerator = pg;
61 #ifdef debug
62  G4cout << "Instantiate CCalEndOfEventAction" << G4endl;
63 #endif
64 
65  G4cout << "Now Instantiate stepping action" << G4endl;
67 
68  G4cout << "Get Calorimter organisation" << G4endl;
70  G4cout << "end of instantiation of EndofEventAction" << G4endl;
71 }
72 
73 
75 
76  if (theOrg)
77  delete theOrg;
78  if (SDnames)
79  delete[] SDnames;
80  G4cout << "CCalEndOfEventAction deleted" << G4endl;
81 }
82 
83 
85 
86  isInitialized = true;
88 #ifdef debug
89  G4cout << "CCalEndOfEventAction look for " << numberOfSD
90  << " calorimeter-like SD" << G4endl;
91 #endif
92  if (numberOfSD > 0) {
93  G4int n = numberOfSD;
94  if (n < 2) n = 2;
95  SDnames = new nameType[n];
96  }
97  for (int i=0; i<numberOfSD; i++) {
98  SDnames[i] = G4String(CCalSDList::getInstance()->getCaloSDName(i));
99 #ifdef debug
100  G4cout << "CCalEndOfEventAction: found SD " << i << " name "
101  << SDnames[i] << G4endl;
102 #endif
103  }
104 }
105 
106 
108  G4cout << "\n---> Begin of event: " << evt->GetEventID() << G4endl;
109 }
110 
111 
113 
114 #ifdef debug
115  G4cout << G4endl << "=== Begin of EndOfEventAction === " << G4endl;
116 #endif
117 
118  if (!isInitialized) initialize();
119 
121 
122  //
123  // Look for the Hit Collection
124  //
125  G4HCofThisEvent* allHC = evt->GetHCofThisEvent();
126  if (allHC == 0) {
127 #ifdef debug
128  G4cout << "CCalEndOfEventAction: No Hit Collection in this event"
129  << G4endl;
130 #endif
131  return;
132  }
133 
134  //
135  // hits info
136  //
137 
138  //Now make summary
139  float hcalE[28], ecalE[49], fullE=0., edec=0, edhc=0;
140  int i = 0;
141  for (i = 0; i < 28; i++) {hcalE[i]=0.;}
142  for (i = 0; i < 49; i++) {ecalE[i]=0.;}
143 
144  float* edep = new float[numberOfSD];
145  int nhit=0;
146  for (i = 0; i < numberOfSD; i++){
147 
148  //
149  // Look for the Hit Collection
150  //
151  edep[i] = 0;
152  int caloHCid = G4SDManager::GetSDMpointer()->GetCollectionID(SDnames[i]);
153 
154  if (caloHCid >= 0) {
155  CCalG4HitCollection* theHC =
156  (CCalG4HitCollection*) allHC->GetHC(caloHCid);
157 
158  if (theHC != 0) {
159 
160  G4int nentries = theHC->entries();
161 #ifdef debug
162  G4cout << " There are " << nentries << " hits in " << SDnames[i]
163  << " :" << G4endl;
164 #endif
165 
166  if (nentries > 0) {
167 
168  int j;
169  for (j=0; j<nentries; j++){
170 #ifdef ddebug
171  G4cout << "Hit " << j;
172 #endif
173  CCalG4Hit* aHit = (*theHC)[j];
174  float En = aHit->getEnergyDeposit();
175  int unitID = aHit->getUnitID();
176  int id=-1;
177  if (unitID > 0 && unitID < 29) {
178  id = unitID - 1; // HCal
179  hcalE[id] += En/GeV;
180  } else {
181  int i0 = unitID/4096;
182  int i1 = (unitID/64)%64;
183  int i2 = unitID%64;
184  if (i0 == 1 && i1 < 8 && i2 < 8) {
185  id = i1*7 + i2; // ECal
186  ecalE[id] += En/GeV;
187  }
188  }
189 #ifdef ddebug
190  G4cout << " with Energy = " << En/MeV << " MeV in Unit " << unitID
191  << " " << id << G4endl;
192 #endif
193  fullE += En/GeV;
194  edep[i] += En/GeV;
195  nhit++;
196  }
197 #ifdef ddebug
198  G4cout << " ===> Total Energy Deposit in this Calorimeter = "
199  << edep[i]*1000.0 << " MeV " << G4endl;
200 #endif
201  }
202  }
203  }
204  if (SDnames[i] == "HadronCalorimeter") {
205  edhc = edep[i];
206  } else if (SDnames[i] == "CrystalMatrix") {
207  edec = edep[i];
208  }
209  }
210 
211  delete[] edep;
212 
214  float ener = primaryGenerator->GetParticleEnergy()/GeV;
215  float x = pos.x()/mm;
216  float y = pos.y()/mm;
217  float z = pos.z()/mm;
218 
220  analysis->InsertEnergy(fullE);
221  analysis->InsertEnergyHcal(hcalE);
222  analysis->InsertEnergyEcal(ecalE);
223  analysis->setNtuple(hcalE, ecalE, ener, x, y, z, fullE, edec, edhc);
224  analysis->EndOfEvent(nhit);
225  for (i = 0; i < numberOfSD; i++){
226  int caloHCid = G4SDManager::GetSDMpointer()->GetCollectionID(SDnames[i]);
227  if (caloHCid >= 0) {
228  CCalG4HitCollection* theHC =
229  (CCalG4HitCollection*) allHC->GetHC(caloHCid);
230  if (theHC != 0) {
231  G4int nentries = theHC->entries();
232  if (nentries > 0) {
233  for (G4int k=0; k<nentries; k++) {
234  CCalG4Hit* aHit = (*theHC)[k];
235  analysis->InsertTimeProfile(k,aHit->getTimeSlice(),
236  aHit->getEnergyDeposit()/GeV);
237  }
238  }
239  }
240  }
241  }
242 }
243 
244 
246 
248 
249  if (theUA == 0) {
250 #ifdef debug
251  G4cout << " CCalEndOfEventAction::instanciateSteppingAction creates"
252  << " CCalSteppingAction" << G4endl;
253 #endif
256  }
257 
258 }
static const double MeV
Definition: G4SIunits.hh:193
G4VHitsCollection * GetHC(G4int i)
G4int GetCollectionID(G4String colName)
Definition: G4SDManager.cc:131
void setNtuple(float *hcalE, float *ecalE, float elab, float x, float y, float z, float edep, float edec, float edhc)
CLHEP::Hep3Vector G4ThreeVector
G4double z
Definition: TRTMaterials.hh:39
static CCalSDList * getInstance()
Definition: CCalSDList.cc:37
virtual void StartOfEventAction(const G4Event *evt)
void InsertEnergyHcal(float *)
CCalPrimaryGeneratorAction * primaryGenerator
G4int entries() const
double getEnergyDeposit() const
Definition: CCalHit.cc:86
int G4int
Definition: G4Types.hh:78
const G4UserSteppingAction * GetUserSteppingAction() const
void InsertEnergyEcal(float *)
virtual void EndOfEventAction(const G4Event *evt)
G4int GetEventID() const
Definition: G4Event.hh:151
G4GLOB_DLL std::ostream G4cout
CCalEndOfEventAction(CCalPrimaryGeneratorAction *)
double getTimeSlice() const
Definition: CCalHit.cc:77
static const double GeV
Definition: G4SIunits.hh:196
const G4int n
void InsertEnergy(float v)
void EndOfEvent(G4int flag)
static G4RunManager * GetRunManager()
Definition: G4RunManager.cc:79
CCaloOrganization * theOrg
Defines the format which is used for the output file default is a ROOT file. Comment the g4root and u...
Definition: CCalAnalysis.hh:44
int getNumberOfCaloSD()
Definition: CCalSDList.cc:76
static CCalAnalysis * getInstance()
static G4SDManager * GetSDMpointer()
Definition: G4SDManager.cc:40
unsigned int getUnitID() const
Definition: CCalHit.cc:74
#define G4endl
Definition: G4ios.hh:61
CCalSteppingAction * theSteppingAction
G4HCofThisEvent * GetHCofThisEvent() const
Definition: G4Event.hh:185
void InsertTimeProfile(int, double, double)
static const double mm
Definition: G4SIunits.hh:102
G4bool isInitialized()
Check if the generator is initialized.
static const G4double pos
virtual void SetUserAction(G4UserRunAction *userAction)