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

Generated on Wed Mar 5 17:25:22 2008 for BDSIM by  doxygen 1.5.3