MW_SD.cc

Go to the documentation of this file.
00001 //! \file
00002 //!
00003 //! Source file for MWPC sensitive detector class.
00004 //!
00005 //! The MW_SD class defines the member functions for processing the hit
00006 //! information whenever a hit is simulated in the detector.  These
00007 //! routines initialise the hit collection, process hits, and perform
00008 //! the end of event action which fills the hit information arrays.
00009 //!
00010 //! \author D.K. Hasell
00011 //! \version 1.0
00012 //! \date 2010-10-31
00013 //!
00014 //! \ingroup detector
00015 
00016 // *+****1****+****2****+****3****+****4****+****5****+****6****+****7****+****
00017 
00018 // Include the MW header files and the user header files referenced here.
00019 
00020 #include "MW_SD.h"
00021 #include "MW_Hit.h"
00022 #include "MW_Data.h"
00023 #include "MW_Messenger.h"
00024 
00025 #include "EventAction.h"
00026 
00027 // Include the GEANT4 header files used here.
00028 
00029 #include "G4VSensitiveDetector.hh"
00030 #include "G4HCofThisEvent.hh"
00031 #include "G4SDManager.hh"
00032 #include "G4Step.hh"
00033 #include "G4TouchableHistory.hh"
00034 #include "G4TouchableHandle.hh"
00035 #include "G4StepPoint.hh"
00036 #include "G4ThreeVector.hh"
00037 
00038 #include "Randomize.hh"
00039 
00040 // Use the standard namespace.
00041 
00042 using namespace std;
00043 
00044 // *+****1****+****2****+****3****+****4****+****5****+****6****+****7****+****
00045 
00046 // MW_SD source code.
00047 
00048 // Constructor.
00049 
00050 MW_SD::MW_SD( G4String name ) : G4VSensitiveDetector( name ) {
00051 
00052    // Insert hits collection name.
00053 
00054    collectionName.insert( "MW_HC" );
00055 
00056    // Create pointer to MW SD Messenger.
00057 
00058    MW_Messenger::Instance()->setMW_SDptr( this );
00059 
00060    // Set defaults.
00061 
00062    MW_threshold = 1.0 * eV;
00063    MW_Xresol = 0.1 * cm;
00064    MW_Yresol = 0.1 * cm;
00065 
00066    // Initially transformations are not defined so Boolean is false.
00067 
00068    for( int i = 0; i < N_MW; ++i ) MW_Transform[i] = false;
00069 }
00070 
00071 // Destructor.
00072 
00073 MW_SD::~MW_SD(){}
00074 
00075 // Initialize hits collection.
00076 
00077 void MW_SD::Initialize( G4HCofThisEvent * HCE ) {
00078 
00079    MW_HC = new
00080       MW_HitsCollection( SensitiveDetectorName, collectionName[0] ); 
00081 
00082    static G4int HCID = -1;   
00083    if( HCID < 0 ) {
00084       HCID = G4SDManager::GetSDMpointer()->GetCollectionID(collectionName[0]);
00085    }
00086 
00087    HCE->AddHitsCollection( HCID, MW_HC );
00088 }
00089 
00090 // Process hits.
00091 
00092 G4bool MW_SD::ProcessHits( G4Step * step, G4TouchableHistory * ROhist ) {
00093 
00094    // Get the total energy deposited in this step.
00095 
00096    G4double edep = step->GetTotalEnergyDeposit();
00097 
00098    // Skip this hit if below threshold.
00099 
00100    if( edep < MW_threshold ) return false;
00101 
00102    // Get the PreStepPoint and the TouchableHandle.
00103 
00104    G4StepPoint * prestep = step->GetPreStepPoint();
00105 
00106    G4TouchableHandle touchable = prestep->GetTouchableHandle();
00107 
00108    // Get copy number (used to identify which MW).
00109 
00110    G4int copyno = touchable->GetCopyNumber();
00111 
00112    // Get the track.
00113 
00114    G4Track * track = step->GetTrack();
00115 
00116    // Get the global time and track ID.
00117 
00118    G4double time = prestep->GetGlobalTime();
00119    G4int id = track->GetTrackID();
00120 
00121    // Get the true world and local coordinate positions.
00122 
00123    G4ThreeVector trueworld = prestep->GetPosition();
00124 
00125    // If this first time store transformation and inverse.
00126 
00127    if( !MW_Transform[copyno] ) {
00128       MW_WorldtoLocal[copyno] = touchable->GetHistory()->GetTopTransform();
00129       MW_LocaltoWorld[copyno] = MW_WorldtoLocal[copyno].Inverse();
00130       MW_Transform[copyno] = true;
00131    }
00132 
00133    G4ThreeVector truelocal = MW_WorldtoLocal[copyno].TransformPoint(trueworld);
00134 
00135    // Create local coordinates smeared by resolutions.
00136 
00137    G4ThreeVector local(
00138       truelocal.x() + CLHEP::RandGauss::shoot( 0.0, MW_Xresol ),
00139       truelocal.y() + CLHEP::RandGauss::shoot( 0.0, MW_Yresol ),
00140       truelocal.z() );
00141 
00142    // Transform smeared local coordinates to world coordinate system.
00143 
00144    G4ThreeVector world = MW_LocaltoWorld[copyno].TransformPoint( local );
00145 
00146    // Create a pointer to a new Hit.
00147 
00148    MW_Hit * hit = new MW_Hit();
00149 
00150    // Fill the Hit information.
00151 
00152    hit->copyno  = copyno;
00153    hit->trackid = id;
00154    hit->edep    = edep;
00155    hit->time    = time;
00156    hit->tworld  = trueworld;
00157    hit->tlocal  = truelocal;
00158    hit->world   = world;
00159    hit->local   = local;
00160 
00161    // Add the Hit to the Hit Collection.
00162 
00163    MW_HC->insert( hit );
00164    
00165    //hit->Print();
00166 
00167    return true;
00168 }
00169 
00170 // Routine to process the hits at the end of an event.
00171 
00172 void MW_SD::EndOfEvent( G4HCofThisEvent * HCE ){
00173 
00174 //   PrintAll();
00175 
00176    MW_Data * mwdata = EventAction::mwdata;
00177 
00178    mwdata->Reset();
00179 
00180    G4int N_Hits = MW_HC->entries();
00181    
00182    mwdata->nMW = N_Hits;
00183 
00184    for( G4int i = 0; i < N_Hits; ++i ) {
00185 
00186       mwdata->id.push_back( ( *MW_HC )[i]->copyno );
00187       mwdata->tr.push_back( ( *MW_HC )[i]->trackid );
00188 
00189       mwdata->e.push_back( ( *MW_HC )[i]->edep / eV );
00190       mwdata->t.push_back( ( *MW_HC )[i]->time / ns );
00191 
00192       mwdata->tx.push_back( ( *MW_HC )[i]->tworld.x() / cm );
00193       mwdata->ty.push_back( ( *MW_HC )[i]->tworld.y() / cm );
00194       mwdata->tz.push_back( ( *MW_HC )[i]->tworld.z() / cm );
00195 
00196       mwdata->txl.push_back( ( *MW_HC )[i]->tlocal.x() / cm );
00197       mwdata->tyl.push_back( ( *MW_HC )[i]->tlocal.y() / cm );
00198       mwdata->tzl.push_back( ( *MW_HC )[i]->tlocal.z() / cm );
00199 
00200       mwdata->x.push_back( ( *MW_HC )[i]->world.x() / cm );
00201       mwdata->y.push_back( ( *MW_HC )[i]->world.y() / cm );
00202       mwdata->z.push_back( ( *MW_HC )[i]->world.z() / cm );
00203 
00204       mwdata->xl.push_back( ( *MW_HC )[i]->local.x() / cm );
00205       mwdata->yl.push_back( ( *MW_HC )[i]->local.y() / cm );
00206       mwdata->zl.push_back( ( *MW_HC )[i]->local.z() / cm );
00207    }
00208 
00209 }
00210 
00211 void MW_SD::clear(){} 
00212 
00213 void MW_SD::DrawAll() {} 
00214 
00215 // Print all the hits.
00216 
00217 void MW_SD::PrintAll() {
00218    G4int N_Hits = MW_HC->entries();
00219    G4cout << "\nMW Hits Collection N_Hits = " << N_Hits << "\n" << G4endl; 
00220    for ( G4int i = 0; i < N_Hits; ++i ) (*MW_HC)[i]->Print();
00221 } 
00222 
00223 // Set/Get energy threshold.
00224 
00225 G4double MW_SD::setThreshold( G4double thres ) { return MW_threshold = thres; }
00226 G4double MW_SD::getThreshold() { return MW_threshold; }
00227 
00228 // Set/Get X resolution.
00229 
00230 G4double MW_SD::setXresol( G4double res ) { return MW_Xresol = res; }
00231 G4double MW_SD::getXresol() { return MW_Xresol; }
00232 
00233 // Set/Get Y resolution.
00234 
00235 G4double MW_SD::setYresol( G4double res ) { return MW_Yresol = res; }
00236 G4double MW_SD::getYresol() { return MW_Yresol; }