source: trunk/examples/novice/gemc/src/MEventAction.cc@ 1275

Last change on this file since 1275 was 807, checked in by garnier, 17 years ago

update

File size: 4.4 KB
Line 
1// %%%%%%%%%%
2// G4 headers
3// %%%%%%%%%%
4#include "G4Event.hh"
5
6// %%%%%%%%%%%%%
7// gemc headers
8// %%%%%%%%%%%%%
9#include "MEventAction.h"
10#include "MHit.h"
11
12#include <iostream>
13using namespace std;
14
15MEventAction::MEventAction(gemc_opts opts) {gemcOpt = opts;}
16MEventAction::~MEventAction(){;}
17
18void MEventAction::BeginOfEventAction(const G4Event* evt)
19{
20 string hd_msg = gemcOpt.args["LOG_MSG"].args + " Event Action: >> ";
21 int Modulo = (int) gemcOpt.args["PRINT_EVENT"].arg ;
22
23 if(evtN%Modulo == 0 )
24 cout << hd_msg << " Begin of event " << evtN << endl;
25
26}
27
28void MEventAction::EndOfEventAction(const G4Event* evt)
29{
30 string hd_msg = gemcOpt.args["LOG_MSG"].args + " Event Action: >> ";
31 int Modulo = (int) gemcOpt.args["PRINT_EVENT"].arg ;
32 double VERB = gemcOpt.args["OUT_VERBOSITY"].arg ;
33 string catch_v = gemcOpt.args["CATCH"].args;
34
35
36 if(evtN%Modulo == 0 )
37 cout << hd_msg << " End of event " << evtN << endl << endl;
38
39 MHitCollection* MHC;
40 MHit* aHit;
41 int nhits;
42 MPHBaseClass *ProcessHitRoutine = NULL;
43 MOutputBaseClass *ProcessOutput = NULL;
44
45 PH_output output;
46 int bitcid;
47
48 double Etot;
49
50 // Making sure the output routine exists in the ProcessOutput map
51 map<string, MOutput_Factory>::iterator ito = Out->find(MOut->outType);
52 if(ito == Out->end())
53 {
54 if(MOut->outType != "no")
55 cout << hd_msg << " Warning: output type <" << MOut->outType << "> is not registered in the <MOutput_Factory>. " << endl
56 << " This event will not be written out." << endl;
57 return;
58 }
59 ProcessOutput = GetMOutputClass((*Out), MOut->outType);
60
61 // Header Bank contains event number
62 // Need to change this to read DB header bank
63 ProcessOutput->SetOutpHeader(evtN, MOut);
64
65 // Getting Generated Particles info
66 vector<MGeneratedParticle> MPrimaries;
67 for(int pv=0; pv< evt->GetNumberOfPrimaryVertex(); pv++)
68 {
69 MGeneratedParticle Mparticle;
70 G4PrimaryVertex* MPV = evt->GetPrimaryVertex(pv);
71 Mparticle.vertex = MPV->GetPosition();
72 for(int pp = 0; pp<MPV->GetNumberOfParticle(); pp++)
73 {
74
75 G4PrimaryParticle *PP = MPV->GetPrimary(pp);
76 Mparticle.momentum = PP->GetMomentum();
77 Mparticle.PID = PP->GetPDGcode();
78 }
79 MPrimaries.push_back(Mparticle) ;
80 }
81 ProcessOutput-> WriteGenerated(MOut, MPrimaries);
82
83 for(map<string, MSensitiveDetector*>::iterator it = SeDe_Map.begin(); it!= SeDe_Map.end(); it++)
84 {
85 string hitType;
86 ProcessOutput->SetBankHeader(it->second->SDID.id, it->first, MOut);
87
88 MHC = it->second->GetMHitCollection();
89 nhits = MHC->GetSize();
90 // The same ProcessHit Routine must apply to all the hits in this HitCollection.
91 // Instantiating the ProcessHitRoutine only once for the first hit.
92 if(nhits)
93 {
94 aHit = (*MHC)[0];
95 string vname = aHit->GetDetector().name;
96 hitType = it->second->GetDetectorHitType(vname);
97
98 // Making sure the Routine exists in the MProcessHit_Map
99 map<string, MPHB_Factory>::iterator itp = MProcessHit_Map->find(hitType);
100 if(itp == MProcessHit_Map->end())
101 {
102 cout << hd_msg << " Warning: hit Type <" << hitType << "> is not registered in the <MProcessHit_Map>. " << endl
103 << " This hit collection will not be processed." << endl;
104 return;
105 }
106 ProcessHitRoutine = GetMPHClass((*MProcessHit_Map), hitType);
107
108 for(int h=0; h<nhits; h++)
109 {
110 aHit = (*MHC)[h];
111 output = ProcessHitRoutine->ProcessHit(aHit, gemcOpt);
112 bitcid = it->second->get_bit_compressed_id(output.identity);
113 ProcessOutput->ProcessOutput(bitcid, output, MOut, (*MBank_Map)[hitType]);
114
115 string vname = aHit->GetId()[aHit->GetId().size()-1].name;
116 if(VERB > 4 || vname.find(catch_v) != string::npos)
117 {
118 cout << hd_msg << " Hit " << h + 1 << " -- total number of steps this hit: " << aHit->GetPos().size() << endl;
119 cout << aHit->GetId();
120 Etot = 0;
121 for(int e=0; e<aHit->GetPos().size(); e++) Etot = Etot + aHit->GetEdep()[e];
122 cout << " Total energy deposited: " << Etot/MeV << " MeV" << endl;
123 }
124 }
125 // cout << it->second->HCname << " " << evtN << " " << nhits << endl;
126 }
127 ProcessOutput->RecordAndClear(MOut, (*MBank_Map)[hitType]);
128
129 }
130 ProcessOutput->WriteEvent(MOut);
131
132 delete ProcessOutput;
133 delete ProcessHitRoutine;
134
135 evtN++;
136}
137
138
139
140
141
142
143
144
145
146
147
Note: See TracBrowser for help on using the repository browser.