/scratch0/jsnuveri/BDSIM/BDSIMgit/bdsim/src/BDSSamplerSD.cc

00001 /* BDSIM code.    Version 1.0
00002    Author: Grahame A. Blair, Royal Holloway, Univ. of London.
00003    Last modified 24.7.2002
00004    Copyright (c) 2002 by G.A.Blair.  ALL RIGHTS RESERVED. 
00005 
00006    Modified 22.03.05 by J.C.Carter, Royal Holloway, Univ. of London.
00007    Changed Samplers to account for plane and cylinder types (GABs code)
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   // Create Sampler hits collection
00050   SamplerCollection = new BDSSamplerHitsCollection(SensitiveDetectorName,itsCollectionName);
00051 
00052   // Record id for use in EventAction to save time
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   //Do not store hit if the particle is not on the boundary 
00065   if(preStepPoint->GetStepStatus()!=fGeomBoundary) return false;
00066 
00067   //unique ID of track
00068   G4int TrackID = theTrack->GetTrackID();
00069   //unique ID of track's mother
00070   G4int ParentID = theTrack->GetParentID();
00071   //time since track creation
00072   G4double t = theTrack->GetGlobalTime();
00073   //total track energy 
00074   G4double energy = theTrack->GetKineticEnergy()+theTrack->GetDefinition()->GetPDGMass();
00075   //Turn Number
00076   G4int turnstaken = BDSGlobalConstants::Instance()->GetTurnsTaken();  
00077   
00078   //current particle position (global)
00079   G4ThreeVector pos = theTrack->GetPosition();
00080   //total track length
00081   G4double s = theTrack->GetTrackLength();
00082   if(ParentID != 0) s = pos.z();
00083   //G4ThreeVector pos = preStepPoint->GetPosition();
00084   //current particle direction (global)
00085   G4ThreeVector momDir = theTrack->GetMomentumDirection();
00086   //G4ThreeVector momDir = preStepPoint->GetMomentumDirection();
00087   
00088   // Get Translation and Rotation of Sampler Volume w.r.t the World Volume
00089   // as described in Geant4 FAQ's: http://geant4.cern.ch/support/faq.shtml
00090   G4AffineTransform tf(preStepPoint->GetTouchableHandle()->GetHistory()->GetTopTransform());
00091   //      const G4RotationMatrix Rot=tf.NetRotation();
00092   //      const G4ThreeVector Trans=-tf.NetTranslation();
00093   
00094   //Old method - works for standard Samplers, but not samplers within a deeper
00095   //hierarchy of volumes (e.g. Mokka built samplers)
00096   //const G4RotationMatrix* Rot=theTrack->GetVolume()->GetFrameRotation();
00097   //const G4ThreeVector Trans=theTrack->GetVolume()->GetFrameTranslation();
00098   
00099   //      G4ThreeVector LocalPosition=pos+Trans; 
00100   //      G4ThreeVector LocalDirection=Rot*momDir; 
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   // apply a correction that takes ac... gab to do later!
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   // store production/scatter point
00139   BDSParticle lastScatter(posLastScatter,momDirLastScatter,energyLastScatter,timeLastScatter);
00140 
00141   //production point
00142   BDSParticle production(vtx,dir,vertexEnergy,vertexTime);
00143 
00144   // global point
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   //The hit was stored, so the return value is "true".
00180   return true;
00181 }

Generated on 28 Jun 2015 for BDSIM by  doxygen 1.4.7