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

Last change on this file since 1253 was 1230, checked in by garnier, 15 years ago

update to geant4.9.3

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