BOSS 7.0.1
BESIII Offline Software System
Loading...
Searching...
No Matches
BesMucSD.cc
Go to the documentation of this file.
1//---------------------------------------------------------------------------//
2// BOOST --- BESIII Object_Oriented Simulation Tool //
3//---------------------------------------------------------------------------//
4//Description:
5//Author: Youzy Peking University mail: [email protected]
6//Created: Nov, 2003
7//Modified:
8//Comment:
9// 2006.10.17 update to new gdml, structure changed
10// new gdml structure: gap<---aluminumbox<---gaschamber
11// but still use the former simulation structure
12//---------------------------------------------------------------------------//
13
14//
15// $Id: BesMucSD.cc,v 1.13 2009/08/25 13:33:54 xieyg Exp $
16// GEANT4 tag $Name: MucSim-00-01-03 $
17//
18
19#include "GaudiKernel/Bootstrap.h"
20#include "GaudiKernel/ISvcLocator.h"
21#include "G4Svc/G4Svc.h"
22
23#include "BesMucSD.hh"
24#include "BesMucDigit.hh"
25#include "G4HCofThisEvent.hh"
26#include "G4Step.hh"
27#include "G4ThreeVector.hh"
28#include "G4SDManager.hh"
29#include "G4EventManager.hh"
30#include "G4ios.hh"
31#include "G4UnitsTable.hh"
32#include "BesMucEfficiency.hh"
33#include "BesMucNoise.hh"
34#include "ReadBoostRoot.hh"
35#include "Randomize.hh"
36#include "strstream"
37#include "G4Box.hh"
38#include "stdlib.h"
39
41:BesSensitiveDetector(name),detector(Det)
42{
43 collectionName.insert("BesMucHitsCollection");
44 collectionName.insert("BesMucHitsList");
45 HitID = new G4int[500];
46
47 //liangyt 2006.3.1 initialize muc-efficiency;
48 G4String GeometryPath = ReadBoostRoot::GetBoostRoot();
49 G4String GeometryPath2 = GeometryPath;
50 if(!GeometryPath){
51 G4Exception("BOOST environment not set!");
52 }
53 GeometryPath += "/dat/muc-effi.dat";
54 GeometryPath2 += "/dat/muc-noise.dat";
55
57 // m_effi->Initialize(GeometryPath);
58
59 //retrieve G4Svc
60 ISvcLocator* svcLocator = Gaudi::svcLocator();
61 IG4Svc* iG4Svc;
62 StatusCode sc=svcLocator->service("G4Svc", iG4Svc);
63 m_G4Svc = dynamic_cast<G4Svc *>(iG4Svc);
64 m_noiseMode = m_G4Svc->MucNoiseMode();
65 G4cout << "MucNoiseMode:\t"<<m_noiseMode<<G4endl;
66
67 if( m_noiseMode != 0 )
68 {
69 m_noise=BesMucNoise::Instance();
70 G4LogicalVolume* logicalMuc = detector->GetPhysicalMuc()->GetLogicalVolume();
71 m_noise->Initialize(GeometryPath2,logicalMuc);
72 //m_noise->Initialize(GeometryPath2,logicalMuc,"ROOT");
73 }
74
75 m_PreviousPrimaryTrackG4Id = 0;
76 m_CurEvent = 0;
77 m_TrackCon = 0;
78
79}
80
82 delete [] HitID;
83}
84
85void BesMucSD::Initialize(G4HCofThisEvent*)
86{
87 MucHitCollection = new BesMucHitsCollection(SensitiveDetectorName, collectionName[0]);
88 // for (G4int j=0;j<detector->GetBesMucNbOfTraps();j++) {HitID[j] = -1;};
89 TotEneDeposit = 0;
90 // G4cout<<"----------in SD::Init()--- "<<detector->GetPhysicalMuc()->GetLogicalVolume()->GetName()<<G4endl;
91}
92
93void BesMucSD::BeginOfTruthEvent(const G4Event* )
94{
95 MucHitList = new BesMucHitsCollection(SensitiveDetectorName, collectionName[1]);
96 m_trackIndex = -99;
97 m_trackIndexes.clear();
98 m_prePart = -99;
99 m_preSeg = -99;
100 m_preGap = -99;
101 m_preStrip = -99;
102 //G4cout<<"---in BesMucSD::BeginOfTruthEvent()---"<<MucHitCollection->entries()<<" "<<MucHitList->entries()<<G4endl;
103
104 //add oise
105 if(m_noiseMode != 0) m_noise->AddNoise(m_noiseMode, MucHitCollection, MucHitList); //commen out 2006.10.21
106}
107
108void BesMucSD::EndOfTruthEvent(const G4Event* evt)
109{
110 static G4int HLID=-1;
111 if(HLID<0)
112 HLID = G4SDManager::GetSDMpointer()->GetCollectionID(collectionName[1]);
113 G4HCofThisEvent* HCE = evt->GetHCofThisEvent();
114 HCE->AddHitsCollection(HLID,MucHitList);
115}
116
117G4bool BesMucSD::ProcessHits(G4Step* aStep,G4TouchableHistory*)
118{
119 G4Track *curTrack = aStep->GetTrack();
120
121 m_CurEvent = G4EventManager::GetEventManager()->GetConstCurrentEvent();
122 // if(m_CurEvent->GetEventID()<19565)return true; // ---del
123 if (m_CurEvent) {
124 m_TrackCon = m_CurEvent->GetTrajectoryContainer();
125
126 // if (!m_TrackCon) G4cout << "m_TrackCon does not exist " << G4endl;
127 // else {
128 // G4cout << "m_TrackCon size " << m_TrackCon->size() << G4endl;
129 // for (int i = 0; i < (int)m_TrackCon->size(); i++) {
130 // G4cout << "track " << i << " g4TrackId " << (*m_TrackCon)[i]->GetTrackID() << " parent ID " << (*m_TrackCon)[i]->GetParentID() << G4endl;
131 // }
132 // }
133 }
134
135
136 if (curTrack->GetDefinition()->GetPDGCharge() == 0.) return false;
137
138 G4double edep = aStep->GetTotalEnergyDeposit();
139 //if(edep == 0.) return false;
140
141 //TotEneDeposit+=edep;
142 G4int trackIndex = -99, g4TrackId = -99;
143 GetCurrentTrackIndex(trackIndex, g4TrackId);
144
145 G4TouchableHistory* theTouchable
146 = (G4TouchableHistory*)(aStep->GetPreStepPoint()->GetTouchable());
147
148 BesMucHit *newHit = new BesMucHit();
149
150 G4int trackID = curTrack->GetTrackID(); //G4 track ID of current track.
151 G4int parentID = curTrack->GetParentID(); //G4 track ID of parent track.
152 newHit->SetTrackID(trackID);
153 newHit->SetTrackIndex(trackIndex); // MC truth track index
154
155 G4int pdg = curTrack->GetDefinition()->GetPDGEncoding();
156 newHit->SetPDGCode(pdg);
157
158 newHit->SetEdep(edep);
159
160 G4ThreeVector pos = 0.5*( aStep->GetPostStepPoint()->GetPosition()
161 + aStep->GetPreStepPoint()->GetPosition() );
162 newHit->SetPos(pos);
163
164 G4ThreeVector posInGas = theTouchable->GetHistory()->GetTopTransform().TransformPoint(pos);
165 G4int stackDepth = theTouchable->GetHistory()->GetDepth();
166 G4ThreeVector posInBox = theTouchable->GetHistory()->GetTransform(stackDepth-1).TransformPoint(pos);
167 newHit->SetPosLocal(posInBox);
168
169 G4ThreeVector posInGap = theTouchable->GetHistory()->GetTransform(stackDepth-2).TransformPoint(pos);
170 // cout<<" pos "<<pos.x()<<" "<<pos.y()<<" "<<pos.z()<<"depth= "<<stackDepth<<endl
171 // <<" posInBox(3)"<<posInBox.x()<<" "<<posInBox.y()<<" "<<posInBox.z()<<endl
172 // <<" posInGap(2)"<<posInGap.x()<<" "<<posInGap.y()<<" "<<posInGap.z()<<endl;
173
174 G4double energy = aStep->GetPreStepPoint()->GetKineticEnergy();
175 newHit->SetEnergy(energy);
176
177 G4ThreeVector dir = aStep->GetPreStepPoint()->GetMomentumDirection();
178 newHit->SetDir(dir);
179
180 G4ThreeVector momentum = aStep->GetPreStepPoint()->GetMomentum();
181 newHit->SetMomentum(momentum);
182
183 G4double GlobalTime = aStep->GetPostStepPoint()->GetGlobalTime();
184 newHit->SetTime(GlobalTime);
185
186 G4VPhysicalVolume* vl = theTouchable->GetVolume(0);
187 newHit->SetVolume(vl);
188
189 //G4cout<<"in SD "<<newHit->GetPart()<<" "<<newHit->GetSeg()<<" "<<newHit->GetGap()<<" "<<newHit->GetGasChamber()<<" "<<newHit->GetPanel()<<endl;
190
191 newHit->Draw();
192 //newHit->Print();
193 //MucHitCollection->insert(newHit);
194 m_PreviousPrimaryTrackG4Id = g4TrackId;
195
196 //-----------add by liangyt for efficiency
197
198 //for MC Truth
199 if (MucHitList) {
200
201 // cout <<"g4TrackId " << g4TrackId << " trackID " << trackID << " parentID " << parentID << "trackIndex " << trackIndex << endl;
202 // g4TrackId: G4 track ID of previous primary track in trackList,
203 // trackID: G4 track ID of current track.
204 // parentID: G4 track ID of parent track of current track.
205 // trackIndex: track ID in MC truth
206
207 // no longer necessary to find the track's ancestor primary track,
208 // it is kept in trackIndex untill next primary track generated. So IsChildof is uncessary
209 //if ( g4TrackId == trackID ) { //|| IsChildOf(curTrack, g4TrackId) ) { // This track is the primary track & will appear in MC truth
210 G4int newTrackFlag = 0;
211 newHit->SetTrackIndex(trackIndex);
212 if(m_trackIndex != trackIndex) {
213 m_trackIndex = trackIndex;
214 G4int size = m_trackIndexes.size();
215 newTrackFlag = 1;
216 if (size > 0) {
217 for(G4int i=0;i<size;i++)
218 if(m_trackIndexes[i] == trackIndex ) {
219 newTrackFlag = 0;
220 break;
221 }
222 }
223 }
224
225 if (newTrackFlag) {
226 m_trackIndexes.push_back(trackIndex);
227 m_prePart = -99;
228 m_preSeg = -99;
229 m_preGap = -99;
230 m_preStrip = -99;
231 }
232 BesMucHit* truHit = new BesMucHit();
233 *truHit = *newHit;
234 if (g4TrackId != trackID) {
235 // trackIndex += 1000; // a sencondary track
236 trackIndex += 0; //2006.12.22 do not indicate secondary track now
237 truHit->SetTrackIndex(trackIndex);
238 }
239 //cout << "trackIndex " << trackIndex << endl;
240
241 BesMucDigit aDigit;
242 aDigit.SetHit(truHit);
243 G4int curPart, curSeg, curGap, curStrip;
244 curPart = aDigit.GetPart();
245 curSeg = aDigit.GetSeg();
246 curGap = aDigit.GetGap();
247 curStrip = aDigit.GetNearestStripNo();
248
249 m_effi->CheckCalibSvc();
250 m_effi->SetHit(truHit);
251 G4double need_eff = m_effi->GetEfficiency();
252
253 //G4cout<<"in SD effi= "<<need_eff<<endl;
254 //need_eff = 1.0; //2006.12.28
255 if (curPart == m_prePart && curSeg == m_preSeg &&
256 curGap == m_preGap && curStrip == m_preStrip) {
257 //cout<<MucHitList->entries()<<" "<<MucHitCollection->entries()<<" "<<need_eff<<"---if----curPart "<<curPart<<"curSeg "<<curSeg<<"curGap "<<curGap<<"curStrip "<<curStrip<<" momentum "<<momentum.x()<<" "<<momentum.y()<<" "<<momentum.z()<<" "<<endl;
258 delete truHit;delete newHit;
259 }
260 else {
261 //cout<<MucHitList->entries()<<"----else--Part "<<curPart<<" Seg "<<curSeg<<" Gap "<<curGap<<" Strip "<<curStrip<<endl;
262 truHit->SetPart(curPart);
263 truHit->SetSeg(curSeg);
264 truHit->SetGap(curGap);
265 truHit->SetStrip(curStrip);
266
267 // if a truHit with the same id(part, seg, gap, strip) and trackIndex(%1000) exist,
268 // they belong to the same primary track,(maybe one is primary, the other is secondary), dont add.
269 bool truHitExist = false;
270 G4int n_hit = MucHitList->entries();
271 for(G4int iTru=0;iTru<n_hit;iTru++) {
272 BesMucHit* aTruHit = (*MucHitList)[iTru];
273 if ( aTruHit->GetTrackIndex()%1000 == truHit->GetTrackIndex()%1000 &&
274 aTruHit->GetPart() == truHit->GetPart() &&
275 aTruHit->GetSeg() == truHit->GetSeg() &&
276 aTruHit->GetGap() == truHit->GetGap() &&
277 aTruHit->GetStrip() == truHit->GetStrip() )
278 {
279 truHitExist = true;
280 break;
281 }
282 }
283 G4float random=G4UniformRand(); //*** use other random
284 // G4float random=(rand()%100000)/100000.0;
285 // G4cout<<"---in SD---"<<random<<endl;
286 if (random<=need_eff){ MucHitCollection->insert(newHit);}
287 else delete newHit;
288 if (!truHitExist&&random<=need_eff)
289 { MucHitList->insert(truHit);}
290 else delete truHit;
291 m_prePart = curPart;
292 m_preSeg = curSeg;
293 m_preGap = curGap;
294 m_preStrip = curStrip;
295 }
296 //}
297 }
298
299 return true;
300 }
301
302 bool BesMucSD::IsChildOf(G4Track *curTrack, G4int primaryG4TrackID)
303 {
304 G4cout << "IsChildof " << "curTrackID " << curTrack->GetTrackID() << G4endl;
305
306 G4VTrajectory* aTraj = GetTrajFromID(curTrack->GetParentID());
307 G4cout << "IsChildof " << "parentTrackID " << aTraj->GetTrackID() << G4endl;
308
309 while ( aTraj->GetTrackID() != 0 && aTraj->GetTrackID() != primaryG4TrackID ) {
310 //G4cout << "loop: parentID " << aTraj->GetParentID() << G4endl;
311 aTraj = GetTrajFromID(aTraj->GetParentID());
312 }
313
314 if (aTraj->GetTrackID() == primaryG4TrackID) return true;
315 else return false;
316 }
317
318 G4VTrajectory* BesMucSD::GetTrajFromID(G4int id)
319 {
320 // TrajContainer does not contain current track
321 G4cout << "begin of GetTrajFromID, id : " << id << G4endl;
322
323 if (!m_TrackCon) G4cout << "Trajectory not saved? Set /tracking/storeTrajectory 1" << G4endl;
324
325 if (m_TrackCon->size() == 0) {
326 G4cout << "BesMucSD::GetTrajFromID, TrackCon size 0" << G4endl;
327 return 0;
328 }
329
330 int k = 0;
331 while( k < (int)m_TrackCon->size() && (*m_TrackCon)[k]->GetTrackID() != id ) {
332 G4cout << "GetTrajFromID " << k << " : ID " << (*m_TrackCon)[k]->GetTrackID() << G4endl;
333 k++;
334 if (!(*m_TrackCon)[k]) {
335 G4cout << "G4Track ID " << (*m_TrackCon)[k]->GetTrackID() << " doesnt exist in TrajContainer of this event! " << G4endl;
336 return 0;
337 }
338 }
339
340 if ( k == (int)m_TrackCon->size() ) {
341 G4cout << "BesMucSD::GetTrajFromID, track with ID " << id << " not found" << G4endl;
342 return 0;
343 }
344
345 return (*m_TrackCon)[k];
346 }
347
348 void BesMucSD::EndOfEvent(G4HCofThisEvent* HCE)
349 {
350 static G4int HCID=-1;
351 if(HCID<0)
352 {HCID = G4SDManager::GetSDMpointer()->GetCollectionID(collectionName[0]); }
353 HCE->AddHitsCollection(HCID, MucHitCollection);
354 }
355
**********INTEGER nmxhep !maximum number of particles DOUBLE PRECISION vhep INTEGER jdahep COMMON hepevt $ !serial number $ !number of particles $ !status code $ !particle ident KF $ !parent particles $ !childreen particles $ !four momentum
G4THitsCollection< BesMucHit > BesMucHitsCollection
************Class m_ypar INTEGER m_KeyWgt INTEGER m_nphot INTEGER m_KeyGPS INTEGER m_IsBeamPolarized INTEGER m_EvtGenInterface DOUBLE PRECISION m_Emin DOUBLE PRECISION m_sphot DOUBLE PRECISION m_Xenph DOUBLE PRECISION m_q2 DOUBLE PRECISION m_PolBeam2 DOUBLE PRECISION m_xErrPb *COMMON c_KK2f $ !CMS energy average $ !Spin Polarization vector first beam $ !Spin Polarization vector second beam $ !Beam energy spread[GeV] $ !minimum hadronization energy[GeV] $ !input READ never touch them !$ !debug facility $ !maximum weight $ !inverse alfaQED $ !minimum real photon energy
Definition: KK2f.h:50
G4int GetNearestStripNo()
Definition: BesMucDigit.cc:63
void SetHit(BesMucHit *hit)
Definition: BesMucDigit.cc:29
void SetHit(BesMucHit *hit)
G4double GetEfficiency()
static BesMucEfficiency * Instance(void)
void SetPosLocal(G4ThreeVector xyzLocal)
void SetVolume(G4VPhysicalVolume *pv)
Definition: BesMucHit.cc:92
void Draw()
Definition: BesMucHit.cc:139
void SetMomentum(G4ThreeVector momentum)
G4int AddNoise(int model, BesMucHitsCollection *MucHitCollection, BesMucHitsCollection *MucHitList)
Definition: BesMucNoise.cc:220
static BesMucNoise * Instance(void)
Definition: BesMucNoise.cc:31
void Initialize(G4String filename, G4LogicalVolume *logicalMuc)
Definition: BesMucNoise.cc:84
~BesMucSD()
Definition: BesMucSD.cc:81
void BeginOfTruthEvent(const G4Event *)
Definition: BesMucSD.cc:93
BesMucSD(G4String, BesMucConstruction *)
Definition: BesMucSD.cc:40
G4bool ProcessHits(G4Step *, G4TouchableHistory *)
Definition: BesMucSD.cc:117
void Initialize(G4HCofThisEvent *)
Definition: BesMucSD.cc:85
void EndOfTruthEvent(const G4Event *)
Definition: BesMucSD.cc:108
void EndOfEvent(G4HCofThisEvent *)
Definition: BesMucSD.cc:348
void GetCurrentTrackIndex(G4int &trackIndex, G4int &g4TrackId) const