00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "GaudiKernel/Bootstrap.h"
00020 #include "GaudiKernel/ISvcLocator.h"
00021 #include "G4Svc/G4Svc.h"
00022
00023 #include "BesMucSD.hh"
00024 #include "BesMucDigit.hh"
00025 #include "G4HCofThisEvent.hh"
00026 #include "G4Step.hh"
00027 #include "G4ThreeVector.hh"
00028 #include "G4SDManager.hh"
00029 #include "G4EventManager.hh"
00030 #include "G4ios.hh"
00031 #include "G4UnitsTable.hh"
00032 #include "BesMucEfficiency.hh"
00033 #include "BesMucNoise.hh"
00034 #include "ReadBoostRoot.hh"
00035 #include "Randomize.hh"
00036 #include "strstream"
00037 #include "G4Box.hh"
00038 #include "stdlib.h"
00039
00040 BesMucSD::BesMucSD(G4String name,BesMucConstruction* Det)
00041 :BesSensitiveDetector(name),detector(Det)
00042 {
00043 collectionName.insert("BesMucHitsCollection");
00044 collectionName.insert("BesMucHitsList");
00045 HitID = new G4int[500];
00046
00047
00048 G4String GeometryPath = ReadBoostRoot::GetBoostRoot();
00049 G4String GeometryPath2 = GeometryPath;
00050 if(!GeometryPath){
00051 G4Exception("BOOST environment not set!");
00052 }
00053 GeometryPath += "/dat/muc-effi.dat";
00054 GeometryPath2 += "/dat/muc-noise.dat";
00055
00056 m_effi=BesMucEfficiency::Instance();
00057
00058
00059
00060 ISvcLocator* svcLocator = Gaudi::svcLocator();
00061 IG4Svc* iG4Svc;
00062 StatusCode sc=svcLocator->service("G4Svc", iG4Svc);
00063 m_G4Svc = dynamic_cast<G4Svc *>(iG4Svc);
00064 m_noiseMode = m_G4Svc->MucNoiseMode();
00065 G4cout << "MucNoiseMode:\t"<<m_noiseMode<<G4endl;
00066
00067 if( m_noiseMode != 0 )
00068 {
00069 m_noise=BesMucNoise::Instance();
00070 G4LogicalVolume* logicalMuc = detector->GetPhysicalMuc()->GetLogicalVolume();
00071 m_noise->Initialize(GeometryPath2,logicalMuc);
00072
00073 }
00074
00075 m_PreviousPrimaryTrackG4Id = 0;
00076 m_CurEvent = 0;
00077 m_TrackCon = 0;
00078
00079 }
00080
00081 BesMucSD::~BesMucSD(){
00082 delete [] HitID;
00083 }
00084
00085 void BesMucSD::Initialize(G4HCofThisEvent*)
00086 {
00087 MucHitCollection = new BesMucHitsCollection(SensitiveDetectorName, collectionName[0]);
00088
00089 TotEneDeposit = 0;
00090
00091 }
00092
00093 void BesMucSD::BeginOfTruthEvent(const G4Event* )
00094 {
00095 MucHitList = new BesMucHitsCollection(SensitiveDetectorName, collectionName[1]);
00096 m_trackIndex = -99;
00097 m_trackIndexes.clear();
00098 m_prePart = -99;
00099 m_preSeg = -99;
00100 m_preGap = -99;
00101 m_preStrip = -99;
00102
00103
00104
00105 if(m_noiseMode != 0) m_noise->AddNoise(m_noiseMode, MucHitCollection, MucHitList);
00106 }
00107
00108 void BesMucSD::EndOfTruthEvent(const G4Event* evt)
00109 {
00110 static G4int HLID=-1;
00111 if(HLID<0)
00112 HLID = G4SDManager::GetSDMpointer()->GetCollectionID(collectionName[1]);
00113 G4HCofThisEvent* HCE = evt->GetHCofThisEvent();
00114 HCE->AddHitsCollection(HLID,MucHitList);
00115 }
00116
00117 G4bool BesMucSD::ProcessHits(G4Step* aStep,G4TouchableHistory*)
00118 {
00119 G4Track *curTrack = aStep->GetTrack();
00120
00121 m_CurEvent = G4EventManager::GetEventManager()->GetConstCurrentEvent();
00122
00123 if (m_CurEvent) {
00124 m_TrackCon = m_CurEvent->GetTrajectoryContainer();
00125
00126
00127
00128
00129
00130
00131
00132
00133 }
00134
00135
00136 if (curTrack->GetDefinition()->GetPDGCharge() == 0.) return false;
00137
00138 G4double edep = aStep->GetTotalEnergyDeposit();
00139
00140
00141
00142 G4int trackIndex = -99, g4TrackId = -99;
00143 GetCurrentTrackIndex(trackIndex, g4TrackId);
00144
00145 G4TouchableHistory* theTouchable
00146 = (G4TouchableHistory*)(aStep->GetPreStepPoint()->GetTouchable());
00147
00148 BesMucHit *newHit = new BesMucHit();
00149
00150 G4int trackID = curTrack->GetTrackID();
00151 G4int parentID = curTrack->GetParentID();
00152 newHit->SetTrackID(trackID);
00153 newHit->SetTrackIndex(trackIndex);
00154
00155 G4int pdg = curTrack->GetDefinition()->GetPDGEncoding();
00156 newHit->SetPDGCode(pdg);
00157
00158 newHit->SetEdep(edep);
00159
00160 G4ThreeVector pos = 0.5*( aStep->GetPostStepPoint()->GetPosition()
00161 + aStep->GetPreStepPoint()->GetPosition() );
00162 newHit->SetPos(pos);
00163
00164 G4ThreeVector posInGas = theTouchable->GetHistory()->GetTopTransform().TransformPoint(pos);
00165 G4int stackDepth = theTouchable->GetHistory()->GetDepth();
00166 G4ThreeVector posInBox = theTouchable->GetHistory()->GetTransform(stackDepth-1).TransformPoint(pos);
00167 newHit->SetPosLocal(posInBox);
00168
00169 G4ThreeVector posInGap = theTouchable->GetHistory()->GetTransform(stackDepth-2).TransformPoint(pos);
00170
00171
00172
00173
00174 G4double energy = aStep->GetPreStepPoint()->GetKineticEnergy();
00175 newHit->SetEnergy(energy);
00176
00177 G4ThreeVector dir = aStep->GetPreStepPoint()->GetMomentumDirection();
00178 newHit->SetDir(dir);
00179
00180 G4ThreeVector momentum = aStep->GetPreStepPoint()->GetMomentum();
00181 newHit->SetMomentum(momentum);
00182
00183 G4double GlobalTime = aStep->GetPostStepPoint()->GetGlobalTime();
00184 newHit->SetTime(GlobalTime);
00185
00186 G4VPhysicalVolume* vl = theTouchable->GetVolume(0);
00187 newHit->SetVolume(vl);
00188
00189
00190
00191 newHit->Draw();
00192
00193
00194 m_PreviousPrimaryTrackG4Id = g4TrackId;
00195
00196
00197
00198
00199 if (MucHitList) {
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210 G4int newTrackFlag = 0;
00211 newHit->SetTrackIndex(trackIndex);
00212 if(m_trackIndex != trackIndex) {
00213 m_trackIndex = trackIndex;
00214 G4int size = m_trackIndexes.size();
00215 newTrackFlag = 1;
00216 if (size > 0) {
00217 for(G4int i=0;i<size;i++)
00218 if(m_trackIndexes[i] == trackIndex ) {
00219 newTrackFlag = 0;
00220 break;
00221 }
00222 }
00223 }
00224
00225 if (newTrackFlag) {
00226 m_trackIndexes.push_back(trackIndex);
00227 m_prePart = -99;
00228 m_preSeg = -99;
00229 m_preGap = -99;
00230 m_preStrip = -99;
00231 }
00232 BesMucHit* truHit = new BesMucHit();
00233 *truHit = *newHit;
00234 if (g4TrackId != trackID) {
00235
00236 trackIndex += 0;
00237 truHit->SetTrackIndex(trackIndex);
00238 }
00239
00240
00241 BesMucDigit aDigit;
00242 aDigit.SetHit(truHit);
00243 G4int curPart, curSeg, curGap, curStrip;
00244 curPart = aDigit.GetPart();
00245 curSeg = aDigit.GetSeg();
00246 curGap = aDigit.GetGap();
00247 curStrip = aDigit.GetNearestStripNo();
00248
00249 m_effi->CheckCalibSvc();
00250 m_effi->SetHit(truHit);
00251 G4double need_eff = m_effi->GetEfficiency();
00252
00253
00254
00255 if (curPart == m_prePart && curSeg == m_preSeg &&
00256 curGap == m_preGap && curStrip == m_preStrip) {
00257
00258 delete truHit;delete newHit;
00259 }
00260 else {
00261
00262 truHit->SetPart(curPart);
00263 truHit->SetSeg(curSeg);
00264 truHit->SetGap(curGap);
00265 truHit->SetStrip(curStrip);
00266
00267
00268
00269 bool truHitExist = false;
00270 G4int n_hit = MucHitList->entries();
00271 for(G4int iTru=0;iTru<n_hit;iTru++) {
00272 BesMucHit* aTruHit = (*MucHitList)[iTru];
00273 if ( aTruHit->GetTrackIndex()%1000 == truHit->GetTrackIndex()%1000 &&
00274 aTruHit->GetPart() == truHit->GetPart() &&
00275 aTruHit->GetSeg() == truHit->GetSeg() &&
00276 aTruHit->GetGap() == truHit->GetGap() &&
00277 aTruHit->GetStrip() == truHit->GetStrip() )
00278 {
00279 truHitExist = true;
00280 break;
00281 }
00282 }
00283 G4float random=G4UniformRand();
00284
00285
00286 if (random<=need_eff){ MucHitCollection->insert(newHit);}
00287 else delete newHit;
00288 if (!truHitExist&&random<=need_eff)
00289 { MucHitList->insert(truHit);}
00290 else delete truHit;
00291 m_prePart = curPart;
00292 m_preSeg = curSeg;
00293 m_preGap = curGap;
00294 m_preStrip = curStrip;
00295 }
00296
00297 }
00298
00299 return true;
00300 }
00301
00302 bool BesMucSD::IsChildOf(G4Track *curTrack, G4int primaryG4TrackID)
00303 {
00304 G4cout << "IsChildof " << "curTrackID " << curTrack->GetTrackID() << G4endl;
00305
00306 G4VTrajectory* aTraj = GetTrajFromID(curTrack->GetParentID());
00307 G4cout << "IsChildof " << "parentTrackID " << aTraj->GetTrackID() << G4endl;
00308
00309 while ( aTraj->GetTrackID() != 0 && aTraj->GetTrackID() != primaryG4TrackID ) {
00310
00311 aTraj = GetTrajFromID(aTraj->GetParentID());
00312 }
00313
00314 if (aTraj->GetTrackID() == primaryG4TrackID) return true;
00315 else return false;
00316 }
00317
00318 G4VTrajectory* BesMucSD::GetTrajFromID(G4int id)
00319 {
00320
00321 G4cout << "begin of GetTrajFromID, id : " << id << G4endl;
00322
00323 if (!m_TrackCon) G4cout << "Trajectory not saved? Set /tracking/storeTrajectory 1" << G4endl;
00324
00325 if (m_TrackCon->size() == 0) {
00326 G4cout << "BesMucSD::GetTrajFromID, TrackCon size 0" << G4endl;
00327 return 0;
00328 }
00329
00330 int k = 0;
00331 while( k < (int)m_TrackCon->size() && (*m_TrackCon)[k]->GetTrackID() != id ) {
00332 G4cout << "GetTrajFromID " << k << " : ID " << (*m_TrackCon)[k]->GetTrackID() << G4endl;
00333 k++;
00334 if (!(*m_TrackCon)[k]) {
00335 G4cout << "G4Track ID " << (*m_TrackCon)[k]->GetTrackID() << " doesnt exist in TrajContainer of this event! " << G4endl;
00336 return 0;
00337 }
00338 }
00339
00340 if ( k == (int)m_TrackCon->size() ) {
00341 G4cout << "BesMucSD::GetTrajFromID, track with ID " << id << " not found" << G4endl;
00342 return 0;
00343 }
00344
00345 return (*m_TrackCon)[k];
00346 }
00347
00348 void BesMucSD::EndOfEvent(G4HCofThisEvent* HCE)
00349 {
00350 static G4int HCID=-1;
00351 if(HCID<0)
00352 {HCID = G4SDManager::GetSDMpointer()->GetCollectionID(collectionName[0]); }
00353 HCE->AddHitsCollection(HCID, MucHitCollection);
00354 }
00355