source: trunk/examples/advanced/composite_calorimeter/src/CCalEndOfEventAction.cc

Last change on this file was 1313, checked in by garnier, 14 years ago

geant4.9.4 beta rc0

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