BOSS 8.0.0
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: youzy@hep.pku.cn
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.14 2022/01/24 21:40:36 dengzy Exp $
16// GEANT4 tag $Name: MucSim-00-01-04 $
17//
18
19#include "G4Box.hh"
20#include "G4EventManager.hh"
21#include "G4HCofThisEvent.hh"
22#include "G4SDManager.hh"
23#include "G4Step.hh"
24#include "G4ThreeVector.hh"
25#include "G4UnitsTable.hh"
26#include "G4ios.hh"
27#include "GaudiKernel/Bootstrap.h"
28#include "GaudiKernel/ISvcLocator.h"
29#include "Randomize.hh"
30#include "stdlib.h"
31#include "strstream"
32
33#include "SimUtil/ReadBoostRoot.hh"
34
35// #include "G4Svc/G4Svc.h"
36#include "MucSim/BesMucDigit.hh"
37#include "MucSim/BesMucEfficiency.hh"
38#include "MucSim/BesMucNoise.hh"
39#include "MucSim/BesMucSD.hh"
40
42 : BesSensitiveDetector( name ), detector( Det ) {
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 {
52 G4cout << "BOOST environment not set!" << G4endl;
53 exit( -1 );
54 }
55 GeometryPath += "/dat/muc-effi.dat";
56 GeometryPath2 += "/dat/muc-noise.dat";
57
59 // m_effi->Initialize(GeometryPath);
60
61 // retrieve G4Svc
62 ISvcLocator* svcLocator = Gaudi::svcLocator();
63 IG4Svc* iG4Svc;
64 StatusCode sc = svcLocator->service( "G4Svc", iG4Svc );
65 // m_G4Svc = dynamic_cast<G4Svc *>(iG4Svc);
66 m_G4Svc = iG4Svc;
67 m_noiseMode = m_G4Svc->MucNoiseMode();
68 G4cout << "MucNoiseMode:\t" << m_noiseMode << G4endl;
69
70 if ( m_noiseMode != 0 )
71 {
72 m_noise = BesMucNoise::Instance();
73 G4LogicalVolume* logicalMuc = detector->GetPhysicalMuc()->GetLogicalVolume();
74 m_noise->Initialize( GeometryPath2, logicalMuc );
75 // m_noise->Initialize(GeometryPath2,logicalMuc,"ROOT");
76 }
77
78 m_PreviousPrimaryTrackG4Id = 0;
79 m_CurEvent = 0;
80 m_TrackCon = 0;
81}
82
83BesMucSD::~BesMucSD() { delete[] HitID; }
84
85void BesMucSD::Initialize( G4HCofThisEvent* ) {
86 MucHitCollection = new BesMucHitsCollection( SensitiveDetectorName, collectionName[0] );
87 // for (G4int j=0;j<detector->GetBesMucNbOfTraps();j++) {HitID[j] = -1;};
88 TotEneDeposit = 0;
89 // G4cout<<"----------in SD::Init()---
90 // "<<detector->GetPhysicalMuc()->GetLogicalVolume()->GetName()<<G4endl;
91}
92
93void BesMucSD::BeginOfTruthEvent( const G4Event* ) {
94 MucHitList = new BesMucHitsCollection( SensitiveDetectorName, collectionName[1] );
95 m_trackIndex = -99;
96 m_trackIndexes.clear();
97 m_prePart = -99;
98 m_preSeg = -99;
99 m_preGap = -99;
100 m_preStrip = -99;
101 // G4cout<<"---in BesMucSD::BeginOfTruthEvent()---"<<MucHitCollection->entries()<<"
102 // "<<MucHitList->entries()<<G4endl;
103
104 // add oise
105 if ( m_noiseMode != 0 )
106 m_noise->AddNoise( m_noiseMode, MucHitCollection, MucHitList ); // commen out 2006.10.21
107}
108
109void BesMucSD::EndOfTruthEvent( const G4Event* evt ) {
110 static G4int HLID = -1;
111 if ( HLID < 0 ) HLID = G4SDManager::GetSDMpointer()->GetCollectionID( collectionName[1] );
112 G4HCofThisEvent* HCE = evt->GetHCofThisEvent();
113 HCE->AddHitsCollection( HLID, MucHitList );
114}
115
116G4bool BesMucSD::ProcessHits( G4Step* aStep, G4TouchableHistory* ) {
117 G4Track* curTrack = aStep->GetTrack();
118
119 m_CurEvent = G4EventManager::GetEventManager()->GetConstCurrentEvent();
120 // if(m_CurEvent->GetEventID()<19565)return true; // ---del
121 if ( m_CurEvent )
122 {
123 m_TrackCon = m_CurEvent->GetTrajectoryContainer();
124
125 // if (!m_TrackCon) G4cout << "m_TrackCon does not exist " << G4endl;
126 // else {
127 // G4cout << "m_TrackCon size " << m_TrackCon->size() << G4endl;
128 // for (int i = 0; i < (int)m_TrackCon->size(); i++) {
129 // G4cout << "track " << i << " g4TrackId " << (*m_TrackCon)[i]->GetTrackID() << " parent
130 // ID " <<
131 // (*m_TrackCon)[i]->GetParentID() << G4endl;
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 =
167 theTouchable->GetHistory()->GetTransform( stackDepth - 1 ).TransformPoint( pos );
168 newHit->SetPosLocal( posInBox );
169
170 G4ThreeVector posInGap =
171 theTouchable->GetHistory()->GetTransform( stackDepth - 2 ).TransformPoint( pos );
172 // cout<<" pos "<<pos.x()<<" "<<pos.y()<<" "<<pos.z()<<"depth= "<<stackDepth<<endl
173 // <<" posInBox(3)"<<posInBox.x()<<" "<<posInBox.y()<<" "<<posInBox.z()<<endl
174 // <<" posInGap(2)"<<posInGap.x()<<" "<<posInGap.y()<<" "<<posInGap.z()<<endl;
175
176 G4double energy = aStep->GetPreStepPoint()->GetKineticEnergy();
177 newHit->SetEnergy( energy );
178
179 G4ThreeVector dir = aStep->GetPreStepPoint()->GetMomentumDirection();
180 newHit->SetDir( dir );
181
182 G4ThreeVector momentum = aStep->GetPreStepPoint()->GetMomentum();
183 newHit->SetMomentum( momentum );
184
185 G4double GlobalTime = aStep->GetPostStepPoint()->GetGlobalTime();
186 newHit->SetTime( GlobalTime );
187
188 G4VPhysicalVolume* vl = theTouchable->GetVolume( 0 );
189 newHit->SetVolume( vl );
190
191 // G4cout<<"in SD "<<newHit->GetPart()<<" "<<newHit->GetSeg()<<" "<<newHit->GetGap()<<"
192 // "<<newHit->GetGasChamber()<<"
193 // "<<newHit->GetPanel()<<endl;
194
195 newHit->Draw();
196 // newHit->Print();
197 // MucHitCollection->insert(newHit);
198 m_PreviousPrimaryTrackG4Id = g4TrackId;
199
200 //-----------add by liangyt for efficiency
201
202 // for MC Truth
203 if ( MucHitList )
204 {
205
206 // cout <<"g4TrackId " << g4TrackId << " trackID " << trackID << " parentID " << parentID
207 // << "trackIndex " << trackIndex
208 // << endl; g4TrackId: G4 track ID of previous primary track in trackList, trackID: G4
209 // track ID of current track. parentID: G4 track ID of parent track of current track.
210 // trackIndex: track ID in MC truth
211
212 // no longer necessary to find the track's ancestor primary track,
213 // it is kept in trackIndex untill next primary track generated. So IsChildof is uncessary
214 // if ( g4TrackId == trackID ) { //|| IsChildOf(curTrack, g4TrackId) ) { // This track
215 // is the primary track & will appear in MC truth
216 G4int newTrackFlag = 0;
217 newHit->SetTrackIndex( trackIndex );
218 if ( m_trackIndex != trackIndex )
219 {
220 m_trackIndex = trackIndex;
221 G4int size = m_trackIndexes.size();
222 newTrackFlag = 1;
223 if ( size > 0 )
224 {
225 for ( G4int i = 0; i < size; i++ )
226 if ( m_trackIndexes[i] == trackIndex )
227 {
228 newTrackFlag = 0;
229 break;
230 }
231 }
232 }
233
234 if ( newTrackFlag )
235 {
236 m_trackIndexes.push_back( trackIndex );
237 m_prePart = -99;
238 m_preSeg = -99;
239 m_preGap = -99;
240 m_preStrip = -99;
241 }
242 BesMucHit* truHit = new BesMucHit();
243 *truHit = *newHit;
244 if ( g4TrackId != trackID )
245 {
246 // trackIndex += 1000; // a sencondary track
247 trackIndex += 0; // 2006.12.22 do not indicate secondary track now
248 truHit->SetTrackIndex( trackIndex );
249 }
250 // cout << "trackIndex " << trackIndex << endl;
251
252 BesMucDigit aDigit;
253 aDigit.SetHit( truHit );
254 G4int curPart, curSeg, curGap, curStrip;
255 curPart = aDigit.GetPart();
256 curSeg = aDigit.GetSeg();
257 curGap = aDigit.GetGap();
258 curStrip = aDigit.GetNearestStripNo();
259
260 m_effi->CheckCalibSvc();
261 m_effi->SetHit( truHit );
262 G4double need_eff = m_effi->GetEfficiency();
263
264 // G4cout<<"in SD effi= "<<need_eff<<endl;
265 // need_eff = 1.0; //2006.12.28
266 if ( curPart == m_prePart && curSeg == m_preSeg && curGap == m_preGap &&
267 curStrip == m_preStrip )
268 {
269 // cout<<MucHitList->entries()<<" "<<MucHitCollection->entries()<<"
270 // "<<need_eff<<"---if----curPart "<<curPart<<"curSeg
271 // "<<curSeg<<"curGap "<<curGap<<"curStrip "<<curStrip<<" momentum "<<momentum.x()<<"
272 // "<<momentum.y()<<"
273 // "<<momentum.z()<<" "<<endl;
274 delete truHit;
275 delete newHit;
276 }
277 else
278 {
279 // cout<<MucHitList->entries()<<"----else--Part "<<curPart<<" Seg "<<curSeg<<" Gap
280 // "<<curGap<<" Strip
281 // "<<curStrip<<endl;
282 truHit->SetPart( curPart );
283 truHit->SetSeg( curSeg );
284 truHit->SetGap( curGap );
285 truHit->SetStrip( curStrip );
286
287 // if a truHit with the same id(part, seg, gap, strip) and trackIndex(%1000) exist,
288 // they belong to the same primary track,(maybe one is primary, the other is secondary),
289 // dont add.
290 bool truHitExist = false;
291 G4int n_hit = MucHitList->entries();
292 for ( G4int iTru = 0; iTru < n_hit; iTru++ )
293 {
294 BesMucHit* aTruHit = ( *MucHitList )[iTru];
295 if ( aTruHit->GetTrackIndex() % 1000 == truHit->GetTrackIndex() % 1000 &&
296 aTruHit->GetPart() == truHit->GetPart() &&
297 aTruHit->GetSeg() == truHit->GetSeg() && aTruHit->GetGap() == truHit->GetGap() &&
298 aTruHit->GetStrip() == truHit->GetStrip() )
299 {
300 truHitExist = true;
301 break;
302 }
303 }
304 G4float random = G4UniformRand(); //*** use other random
305 // G4float random=(rand()%100000)/100000.0;
306 // G4cout<<"---in SD---"<<random<<endl;
307 if ( random <= need_eff ) { MucHitCollection->insert( newHit ); }
308 else delete newHit;
309 if ( !truHitExist && random <= need_eff ) { MucHitList->insert( truHit ); }
310 else delete truHit;
311 m_prePart = curPart;
312 m_preSeg = curSeg;
313 m_preGap = curGap;
314 m_preStrip = curStrip;
315 }
316 //}
317 }
318
319 return true;
320}
321
322bool BesMucSD::IsChildOf( G4Track* curTrack, G4int primaryG4TrackID ) {
323 G4cout << "IsChildof "
324 << "curTrackID " << curTrack->GetTrackID() << G4endl;
325
326 G4VTrajectory* aTraj = GetTrajFromID( curTrack->GetParentID() );
327 G4cout << "IsChildof "
328 << "parentTrackID " << aTraj->GetTrackID() << G4endl;
329
330 while ( aTraj->GetTrackID() != 0 && aTraj->GetTrackID() != primaryG4TrackID )
331 {
332 // G4cout << "loop: parentID " << aTraj->GetParentID() << G4endl;
333 aTraj = GetTrajFromID( aTraj->GetParentID() );
334 }
335
336 if ( aTraj->GetTrackID() == primaryG4TrackID ) return true;
337 else return false;
338}
339
340G4VTrajectory* BesMucSD::GetTrajFromID( G4int id ) {
341 // TrajContainer does not contain current track
342 G4cout << "begin of GetTrajFromID, id : " << id << G4endl;
343
344 if ( !m_TrackCon )
345 G4cout << "Trajectory not saved? Set /tracking/storeTrajectory 1" << G4endl;
346
347 if ( m_TrackCon->size() == 0 )
348 {
349 G4cout << "BesMucSD::GetTrajFromID, TrackCon size 0" << G4endl;
350 return 0;
351 }
352
353 int k = 0;
354 while ( k < (int)m_TrackCon->size() && ( *m_TrackCon )[k]->GetTrackID() != id )
355 {
356 G4cout << "GetTrajFromID " << k << " : ID " << ( *m_TrackCon )[k]->GetTrackID() << G4endl;
357 k++;
358 if ( !( *m_TrackCon )[k] )
359 {
360 G4cout << "G4Track ID " << ( *m_TrackCon )[k]->GetTrackID()
361 << " doesnt exist in TrajContainer of this event! " << G4endl;
362 return 0;
363 }
364 }
365
366 if ( k == (int)m_TrackCon->size() )
367 {
368 G4cout << "BesMucSD::GetTrajFromID, track with ID " << id << " not found" << G4endl;
369 return 0;
370 }
371
372 return ( *m_TrackCon )[k];
373}
374
375void BesMucSD::EndOfEvent( G4HCofThisEvent* HCE ) {
376 static G4int HCID = -1;
377 if ( HCID < 0 )
378 { HCID = G4SDManager::GetSDMpointer()->GetCollectionID( collectionName[0] ); }
379 HCE->AddHitsCollection( HCID, MucHitCollection );
380}
**********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_KeyIHVP 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()
void SetHit(BesMucHit *hit)
static BesMucEfficiency * Instance(void)
void SetVolume(G4VPhysicalVolume *pv)
Definition BesMucHit.cc:86
void Draw()
Definition BesMucHit.cc:131
static BesMucNoise * Instance(void)
~BesMucSD()
Definition BesMucSD.cc:83
void BeginOfTruthEvent(const G4Event *)
Definition BesMucSD.cc:93
BesMucSD(G4String, BesMucConstruction *)
Definition BesMucSD.cc:41
G4bool ProcessHits(G4Step *, G4TouchableHistory *)
Definition BesMucSD.cc:116
void Initialize(G4HCofThisEvent *)
Definition BesMucSD.cc:85
void EndOfTruthEvent(const G4Event *)
Definition BesMucSD.cc:109
void EndOfEvent(G4HCofThisEvent *)
Definition BesMucSD.cc:375
void GetCurrentTrackIndex(G4int &trackIndex, G4int &g4TrackId) const
BesSensitiveDetector(const G4String name)
virtual int MucNoiseMode()=0