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 "BDSSamplerSD.hh"
00013 #include "BDSSamplerHit.hh"
00014 #include "G4VPhysicalVolume.hh"
00015 #include "G4LogicalVolume.hh"
00016 #include "G4Track.hh"
00017 #include "G4Step.hh"
00018 #include "G4StepPoint.hh"
00019 #include "G4ParticleDefinition.hh"
00020 #include "G4VTouchable.hh"
00021 #include "G4TouchableHistory.hh"
00022 #include "G4ios.hh"
00023 #include "G4RotationMatrix.hh"
00024 #include "G4ThreeVector.hh"
00025 
00026 #include "G4AffineTransform.hh"
00027 
00028 #include "G4RunManager.hh"
00029 #include <vector>
00030 
00031 #include "G4SDManager.hh"
00032 
00033 //typedef std::vector<G4int> MuonTrackVector;
00034 //extern MuonTrackVector* theMuonTrackVector;
00035 
00036 extern G4double
00037   initial_x, initial_xp,
00038   initial_y, initial_yp,
00039   initial_z, initial_zp,
00040   initial_E, initial_t;
00041 
00042 
00043 BDSSamplerSD::BDSSamplerSD(G4String name, G4String type)
00044   :G4VSensitiveDetector(name),SamplerCollection(NULL),
00045    itsType(type)
00046 {
00047   itsCollectionName="Sampler_"+type;
00048   collectionName.insert(itsCollectionName);
00049 }
00050 
00051 BDSSamplerSD::~BDSSamplerSD()
00052 {;}
00053 
00054 void BDSSamplerSD::Initialize(G4HCofThisEvent*)
00055 {
00056   // Create Sampler hits collection
00057   SamplerCollection = new BDSSamplerHitsCollection(SensitiveDetectorName,itsCollectionName);
00058 
00059   // Record the collection ID for later
00060   //  G4SDManager *SDman = G4SDManager::GetSDMpointer();
00061   //  itsHCID = SDman->GetCollectionID(itsCollectionName);
00062 
00063 }
00064 
00065 G4bool BDSSamplerSD::ProcessHits(G4Step*aStep,G4TouchableHistory*)
00066 {
00067   G4Track* theTrack = aStep->GetTrack();
00068   G4StepPoint* preStepPoint = aStep->GetPreStepPoint();
00069   //  G4StepPoint* postStepPoint = aStep->GetPostStepPoint();
00070   //  // tmp - only store muons
00071   //     G4String pName=theTrack->GetDefinition()->GetParticleName();
00072   //    if(pName=="mu+"||pName=="mu-")
00073   //    { // tm
00074   //Do not store hit if particles is "twiss" or "reference"
00075   if(BDSGlobalConstants::Instance()->DoTwiss() || BDSGlobalConstants::Instance()->isReference) return false;
00076   //Do not store hit if the particle is not on the boundary 
00077   if(preStepPoint->GetStepStatus()!=fGeomBoundary) return false;
00078   
00079   //unique ID of track
00080   G4int TrackID = theTrack->GetTrackID();
00081   //unique ID of track's mother
00082   G4int ParentID = theTrack->GetParentID();
00083   //time since track creation
00084   G4double t = theTrack->GetGlobalTime();
00085   //total track energy
00086   
00087   
00088   G4double energy = theTrack->GetKineticEnergy()+ 
00089     theTrack->GetDefinition()->GetPDGMass();
00090   
00091   
00092   //current particle position (global)
00093   G4ThreeVector pos = theTrack->GetPosition();
00094   //total track length
00095   G4double s = theTrack->GetTrackLength();
00096   if(ParentID != 0) s = pos.z();
00097   //G4ThreeVector pos = preStepPoint->GetPosition();
00098   //current particle direction (global)
00099   G4ThreeVector momDir = theTrack->GetMomentumDirection();
00100   //G4ThreeVector momDir = preStepPoint->GetMomentumDirection();
00101   
00102   // Get Translation and Rotation of Sampler Volume w.r.t the World Volume
00103   // as described in Geant4 FAQ's: http://geant4.cern.ch/support/faq.shtml
00104   G4AffineTransform tf(preStepPoint->GetTouchableHandle()->GetHistory()->GetTopTransform());
00105   //      const G4RotationMatrix Rot=tf.NetRotation();
00106   //      const G4ThreeVector Trans=-tf.NetTranslation();
00107   
00108   //Old method - works for standard Samplers, but not samplers within a deeper
00109   //hierarchy of volumes (e.g. Mokka built samplers)
00110   //const G4RotationMatrix* Rot=theTrack->GetVolume()->GetFrameRotation();
00111   //const G4ThreeVector Trans=theTrack->GetVolume()->GetFrameTranslation();
00112   
00113   //      G4ThreeVector LocalPosition=pos+Trans; 
00114   //      G4ThreeVector LocalDirection=Rot*momDir; 
00115   G4ThreeVector LocalPosition = tf.TransformPoint(pos);
00116   G4ThreeVector LocalDirection = tf.TransformAxis(momDir);
00117   
00118   G4double x=LocalPosition.x();
00119   G4double y=LocalPosition.y();
00120   G4double z=LocalPosition.z();
00121   G4double xPrime=LocalDirection.x();
00122   G4double yPrime=LocalDirection.y();
00123   G4double zPrime=LocalDirection.z();
00124   
00125   // Changed z output by Samplers to be the position of the sampler
00126   // not time of flight of the particle JCC 15/10/05
00127   //G4double z=-(time*c_light-(pos.z()+BDSGlobalConstants::Instance()->GetWorldSizeZ()));
00128   //G4double z=pos.z();
00129   if(zPrime<0) energy*=-1;
00130   // apply a correction that takes ac... gab to do later!
00131   
00132   G4int nEvent= 
00133     G4RunManager::GetRunManager()->GetCurrentEvent()->GetEventID();
00134   
00135   nEvent+=BDSGlobalConstants::Instance()->GetEventNumberOffset();
00136   
00137   G4int nSampler=theTrack->GetVolume()->GetCopyNo()+1;
00138   G4String SampName = theTrack->GetVolume()->GetName()+"_"+BDSGlobalConstants::Instance()->StringFromInt(nSampler);
00139   G4int PDGtype=theTrack->GetDefinition()->GetPDGEncoding();
00140   
00141   G4String pName=theTrack->GetDefinition()->GetParticleName();
00142   
00143 #ifdef DEBUG
00144   G4cout << __METHOD_NAME__ << "BDSSamplerSD> Particle name: " << pName << G4endl;  
00145   G4cout << __METHOD_NAME__ << "BDSSamplerSD> PDG encoding: " << PDGtype << G4endl;  
00146   G4cout << __METHOD_NAME__ << "BDSSamplerSD> TrackID: " << TrackID << G4endl;  
00147 #endif
00148   
00149   G4ThreeVector vtx=theTrack->GetVertexPosition();
00150   G4ThreeVector dir=theTrack->GetVertexMomentumDirection();
00151   
00152   G4double 
00153     start_x, start_xp,
00154     start_y, start_yp,
00155     start_z, start_zp,
00156     start_E, start_t;
00157   
00158   if(pName!="e+" && pName!="e-") 
00159     {
00160       // store production point
00161       start_x   =  vtx.x();
00162       start_xp  =  dir.x();
00163       start_y   =  vtx.y();
00164       start_yp  =  dir.y();       
00165       start_z   =  vtx.z();
00166       start_zp  =  dir.z();
00167       
00168       start_E   =  theTrack->GetVertexKineticEnergy()+ 
00169         theTrack->GetDefinition()->GetPDGMass();
00170       
00171       start_t   = t - theTrack->GetLocalTime();
00172     }
00173   else
00174     {// for electrons (and positrons) store the point of last scatter
00175       start_x  = initial_x;
00176       start_xp = initial_xp;
00177       start_y  = initial_y;
00178       start_yp = initial_yp;
00179       start_z  = initial_z;
00180       start_zp = initial_zp;
00181       start_E  = initial_E;
00182       start_t  = initial_t;
00183     }
00184   G4double weight=theTrack->GetWeight();
00185   
00186   /*
00187     if(BDSGlobalConstants::Instance()->GetStoreMuonTrajectories())
00188     if(pName=="mu+"||pName=="mu-") 
00189     theMuonTrackVector->push_back(theTrack->GetTrackID());
00190   */
00191   
00192   BDSSamplerHit* smpHit
00193     = new BDSSamplerHit(
00194                         SampName,
00195                         start_E,
00196                         start_x, start_xp,
00197                         start_y, start_yp,
00198                         start_z, start_zp,
00199                         start_t,
00200                         energy,
00201                         x, xPrime,
00202                         y, yPrime,
00203                         z, zPrime,
00204                         t,
00205                         s,
00206                         weight,
00207                         PDGtype,nEvent, ParentID, TrackID);
00208   smpHit->SetGlobalX(pos.x());
00209   smpHit->SetGlobalY(pos.y());
00210   smpHit->SetGlobalZ(pos.z());
00211   smpHit->SetGlobalXPrime(momDir.x());
00212   smpHit->SetGlobalYPrime(momDir.y());
00213   smpHit->SetGlobalZPrime(momDir.z());
00214   smpHit->SetType(itsType);
00215   
00216 #ifdef DEBUG
00217   G4cout << __METHOD_NAME__ << " Sampler : " << SampName << G4endl;
00218   G4cout << __METHOD_NAME__ << " Storing hit: E, x, y, z, xPrime, yPrime" << G4endl;
00219   G4cout << __METHOD_NAME__ << " " << energy <<" "  << x << " " << y << " " << z << " " << xPrime << " " << yPrime << G4endl;
00220   G4cout << __METHOD_NAME__ << " Storing hit: E, x, y, z, xPrime, yPrime" << G4endl;
00221   G4cout << __METHOD_NAME__ << " " << energy <<" "  << pos.x() << " " << pos.y() << " " << pos.z() << " " << xPrime << " " << yPrime << G4endl;
00222   G4cout << __METHOD_NAME__ << " entries in hits collection before inserting hit: " << SamplerCollection->entries() << G4endl;
00223 #endif
00224   SamplerCollection->insert(smpHit);
00225 #ifdef DEBUG
00226   G4cout << __METHOD_NAME__ << " entries in hits collection after inserting hit: " << SamplerCollection->entries() << G4endl;
00227 #endif
00228 
00229   //The hit was stored, so the return value is "true".
00230   return true;
00231 }
00232 
00233 void BDSSamplerSD::EndOfEvent(G4HCofThisEvent*HCE)
00234 {
00235   G4SDManager * SDman = G4SDManager::GetSDMpointer();
00236   G4int HCID = SDman->GetCollectionID(itsCollectionName);
00237   HCE->AddHitsCollection(HCID, SamplerCollection );
00238 
00239   //  HCE->AddHitsCollection(itsHCID, SamplerCollection );
00240 
00241 
00242 }
00243 
00244 void BDSSamplerSD::clear(){} 
00245 
00246 void BDSSamplerSD::DrawAll(){} 
00247 
00248 void BDSSamplerSD::PrintAll(){} 

Generated on 27 Aug 2013 for BDSIM by  doxygen 1.4.7