00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "BDSGlobalConstants.hh"
00011 #include "BDSExecOptions.hh"
00012 #include "BDSDebug.hh"
00013 #include "BDSParticle.hh"
00014 #include "BDSRunManager.hh"
00015 #include "BDSSamplerSD.hh"
00016 #include "BDSSamplerHit.hh"
00017 #include "BDSTrajectory.hh"
00018 #include "BDSTrajectoryPoint.hh"
00019 #include "G4VPhysicalVolume.hh"
00020 #include "G4LogicalVolume.hh"
00021 #include "G4Track.hh"
00022 #include "G4Step.hh"
00023 #include "G4StepPoint.hh"
00024 #include "G4ParticleDefinition.hh"
00025 #include "G4VTouchable.hh"
00026 #include "G4TouchableHistory.hh"
00027 #include "G4ios.hh"
00028 #include "G4ThreeVector.hh"
00029
00030 #include "G4AffineTransform.hh"
00031
00032 #include <vector>
00033
00034 #include "G4SDManager.hh"
00035
00036 BDSSamplerSD::BDSSamplerSD(G4String name, G4String type)
00037 :G4VSensitiveDetector(name),itsHCID(-1),SamplerCollection(NULL),
00038 itsType(type)
00039 {
00040 itsCollectionName="Sampler_"+type;
00041 collectionName.insert(itsCollectionName);
00042 }
00043
00044 BDSSamplerSD::~BDSSamplerSD()
00045 {;}
00046
00047 void BDSSamplerSD::Initialize(G4HCofThisEvent* HCE)
00048 {
00049
00050 SamplerCollection = new BDSSamplerHitsCollection(SensitiveDetectorName,itsCollectionName);
00051
00052
00053 if (itsHCID < 0){
00054 itsHCID = G4SDManager::GetSDMpointer()->GetCollectionID(itsCollectionName);}
00055 HCE->AddHitsCollection(itsHCID,SamplerCollection);
00056 }
00057
00058 G4bool BDSSamplerSD::ProcessHits(G4Step*aStep,G4TouchableHistory*)
00059 {
00060 G4Track* theTrack = aStep->GetTrack();
00061 BDSTrajectory* bdsTraj = new BDSTrajectory(theTrack);
00062 G4StepPoint* preStepPoint = aStep->GetPreStepPoint();
00063
00064
00065 if(preStepPoint->GetStepStatus()!=fGeomBoundary) return false;
00066
00067
00068 G4int TrackID = theTrack->GetTrackID();
00069
00070 G4int ParentID = theTrack->GetParentID();
00071
00072 G4double t = theTrack->GetGlobalTime();
00073
00074 G4double energy = theTrack->GetKineticEnergy()+theTrack->GetDefinition()->GetPDGMass();
00075
00076 G4int turnstaken = BDSGlobalConstants::Instance()->GetTurnsTaken();
00077
00078
00079 G4ThreeVector pos = theTrack->GetPosition();
00080
00081 G4double s = theTrack->GetTrackLength();
00082 if(ParentID != 0) s = pos.z();
00083
00084
00085 G4ThreeVector momDir = theTrack->GetMomentumDirection();
00086
00087
00088
00089
00090 G4AffineTransform tf(preStepPoint->GetTouchableHandle()->GetHistory()->GetTopTransform());
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101 G4ThreeVector LocalPosition = tf.TransformPoint(pos);
00102 G4ThreeVector LocalDirection = tf.TransformAxis(momDir);
00103
00104 G4double zPrime=LocalDirection.z();
00105 if(zPrime<0) energy*=-1;
00106
00107
00108 BDSParticle local(LocalPosition,LocalDirection,energy,t);
00109
00110 G4int nEvent=
00111 BDSRunManager::GetRunManager()->GetCurrentEvent()->GetEventID();
00112
00113 nEvent+=BDSGlobalConstants::Instance()->GetEventNumberOffset();
00114
00115 G4int nSampler=theTrack->GetVolume()->GetCopyNo()+1;
00116 G4String SampName = theTrack->GetVolume()->GetName()+"_"+BDSGlobalConstants::Instance()->StringFromInt(nSampler);
00117 SampName = SampName.substr(0,SampName.find("_pv_1"));
00118
00119 G4int PDGtype=theTrack->GetDefinition()->GetPDGEncoding();
00120
00121 G4String pName=theTrack->GetDefinition()->GetParticleName();
00122
00123 #ifdef BDSDEBUG
00124 G4cout << __METHOD_NAME__ << "BDSSamplerSD> Particle name: " << pName << G4endl;
00125 G4cout << __METHOD_NAME__ << "BDSSamplerSD> PDG encoding: " << PDGtype << G4endl;
00126 G4cout << __METHOD_NAME__ << "BDSSamplerSD> TrackID: " << TrackID << G4endl;
00127 #endif
00128
00129 G4ThreeVector vtx = theTrack->GetVertexPosition();
00130 G4ThreeVector dir = theTrack->GetVertexMomentumDirection();
00131 G4ThreeVector posLastScatter = bdsTraj->GetPositionOfLastScatter(theTrack);
00132 G4ThreeVector momDirLastScatter = bdsTraj->GetMomDirAtLastScatter(theTrack);
00133 G4double timeLastScatter = bdsTraj->GetTimeAtLastScatter(theTrack);
00134 G4double energyLastScatter = bdsTraj->GetEnergyAtLastScatter(theTrack);
00135 G4double vertexEnergy = theTrack->GetVertexKineticEnergy() + theTrack->GetParticleDefinition()->GetPDGMass();
00136 G4double vertexTime = bdsTraj->GetTimeAtVertex(theTrack);
00137
00138
00139 BDSParticle lastScatter(posLastScatter,momDirLastScatter,energyLastScatter,timeLastScatter);
00140
00141
00142 BDSParticle production(vtx,dir,vertexEnergy,vertexTime);
00143
00144
00145 BDSParticle global(pos,momDir,energy,t);
00146
00147 G4double weight=theTrack->GetWeight();
00148
00149 BDSSamplerHit* smpHit
00150 = new BDSSamplerHit(
00151 SampName,
00152 BDSGlobalConstants::Instance()->GetInitialPoint(),
00153 production,
00154 lastScatter,
00155 local,
00156 global,
00157 s,
00158 weight,
00159 PDGtype,
00160 nEvent,
00161 ParentID,
00162 TrackID,
00163 turnstaken,
00164 itsType);
00165
00166 #ifdef BDSDEBUG
00167 G4cout << __METHOD_NAME__ << " Sampler : " << SampName << G4endl;
00168 G4cout << __METHOD_NAME__ << " Storing hit: E, x, y, z, xPrime, yPrime" << G4endl;
00169 G4cout << __METHOD_NAME__ << " " << energy <<" " << LocalPosition.x() << " " << LocalPosition.y() << " " << LocalPosition.z() << " " << LocalDirection.x() << " " << LocalDirection.y() << G4endl;
00170 G4cout << __METHOD_NAME__ << " Storing hit: E, x, y, z, xPrime, yPrime" << G4endl;
00171 G4cout << __METHOD_NAME__ << " " << energy <<" " << pos.x() << " " << pos.y() << " " << pos.z() << " " << LocalDirection.x() << " " << LocalDirection.y() << G4endl;
00172 G4cout << __METHOD_NAME__ << " entries in hits collection before inserting hit: " << SamplerCollection->entries() << G4endl;
00173 #endif
00174 SamplerCollection->insert(smpHit);
00175 #ifdef BDSDEBUG
00176 G4cout << __METHOD_NAME__ << " entries in hits collection after inserting hit: " << SamplerCollection->entries() << G4endl;
00177 #endif
00178
00179
00180 return true;
00181 }