00001
00002
00003
00004
00005
00006 #include "BDSExecOptions.hh"
00007 #include "BDSGlobalConstants.hh"
00008
00009 #include "BDSEnergyCounterSD.hh"
00010 #include "BDSEnergyCounterHit.hh"
00011 #include "G4VPhysicalVolume.hh"
00012 #include "G4LogicalVolume.hh"
00013 #include "G4Track.hh"
00014 #include "G4Step.hh"
00015 #include "G4ParticleDefinition.hh"
00016 #include "G4VTouchable.hh"
00017 #include "G4TouchableHistory.hh"
00018 #include "G4ios.hh"
00019 #include "G4RotationMatrix.hh"
00020 #include "G4ThreeVector.hh"
00021
00022 #include "G4Navigator.hh"
00023 #include "G4AffineTransform.hh"
00024
00025 #include "G4RunManager.hh"
00026 #include "G4Version.hh"
00027
00028 #include <string>
00029
00030 extern G4int event_number;
00031
00032
00033 BDSEnergyCounterSD::BDSEnergyCounterSD(G4String name)
00034 :G4VSensitiveDetector(name),BDSEnergyCounterCollection(NULL),
00035 enrg(0.0),xpos(0.0),ypos(0.0),zpos(0.0)
00036 {
00037 verbose = BDSExecOptions::Instance()->GetVerbose();
00038 itsName = name;
00039 collectionName.insert("EC_"+name);
00040 #define NMAXCOPY 5
00041 HitID = new G4int[NMAXCOPY];
00042 }
00043
00044 BDSEnergyCounterSD::~BDSEnergyCounterSD()
00045 {
00046 delete [] HitID;
00047 }
00048
00049 void BDSEnergyCounterSD::Initialize(G4HCofThisEvent*)
00050 {
00051 BDSEnergyCounterCollection = new BDSEnergyCounterHitsCollection
00052 (SensitiveDetectorName,collectionName[0]);
00053 for(G4int i=0; i<NMAXCOPY;i++)HitID[i]=-1;
00054 }
00055
00056 G4bool BDSEnergyCounterSD::ProcessHits(G4Step*aStep,G4TouchableHistory*)
00057 {
00058 if(BDSGlobalConstants::Instance()->GetStopTracks())
00059 #if G4VERSION_NUMBER > 940
00060 enrg = (aStep->GetTrack()->GetTotalEnergy() - aStep->GetTotalEnergyDeposit());
00061 #else
00062 enrg = (aStep->GetTrack()->GetTotalEnergy() - aStep->GetDeltaEnergy());
00063 #endif
00064 else
00065 enrg = aStep->GetTotalEnergyDeposit();
00066 #ifdef DEBUG
00067 G4cout << "BDSEnergyCounterSD> enrg = " << enrg << G4endl;
00068 #endif
00069 if (enrg==0.) return false;
00070
00071
00072 G4int nCopy=aStep->GetPreStepPoint()->GetPhysicalVolume()->GetCopyNo();
00073 #ifdef DEBUG
00074 if(nCopy>0){
00075 G4cout << "BDSEnergyCounterSD::ProcessHits> nCopy = " << nCopy << G4endl;
00076 }
00077 #endif
00078 if(nCopy>NMAXCOPY-1)
00079 {
00080 G4cerr<<" BDSEnergyCounterSD: nCopy too large: nCopy="<<nCopy<<
00081 "NMAXCOPY="<<NMAXCOPY<<" Volume="<<
00082 aStep->GetPreStepPoint()->GetPhysicalVolume()->GetName()<<G4endl;
00083 G4Exception("Killing program in BDSEnergyCounterSD::ProcessHits", "-1", FatalException, "");
00084 }
00085
00086
00087
00088 G4AffineTransform tf = (aStep->GetPreStepPoint()->GetTouchableHandle()->GetHistory()->GetTopTransform());
00089 G4ThreeVector pos = aStep->GetTrack()->GetPosition();
00090 G4ThreeVector momDir = aStep->GetTrack()->GetMomentumDirection();
00091
00092 G4ThreeVector LocalPosition = tf.TransformPoint(pos);
00093 G4ThreeVector LocalDirection = tf.TransformAxis(momDir);
00094
00095
00096
00097
00098
00099
00100 zpos=0.5*(aStep->GetPreStepPoint()->GetPosition().z()
00101 + aStep->GetPostStepPoint()->GetPosition().z());
00102
00103 xpos=0.5*(aStep->GetPreStepPoint()->GetPosition().x()
00104 + aStep->GetPostStepPoint()->GetPosition().x());
00105
00106 ypos=0.5*(aStep->GetPreStepPoint()->GetPosition().y()
00107 + aStep->GetPostStepPoint()->GetPosition().y());
00108
00109 if(verbose && BDSGlobalConstants::Instance()->GetStopTracks()) {
00110 G4cout << "BDSEnergyCounterSD: Current Volume: " << aStep->GetPreStepPoint()->GetPhysicalVolume()->GetName()
00111 <<"\tEvent: " << event_number << "\tEnergy: " << enrg/GeV << "GeV\tPosition: " << zpos/m <<"m"<< G4endl;
00112 }
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123 G4double weight = aStep->GetTrack()->GetWeight();
00124 if (weight == 0){
00125 G4cerr << "Error: BDSEnergyCounterSD: weight = 0" << G4endl;
00126 exit(1);
00127 }
00128 int ptype = aStep->GetTrack()->GetDefinition()->GetPDGEncoding();
00129 G4String volName = aStep->GetPreStepPoint()->GetPhysicalVolume()->GetName();
00130 G4String regionName = aStep->GetPreStepPoint()->GetPhysicalVolume()->GetLogicalVolume()->GetRegion()->GetName();
00131 G4bool precisionRegion=false;
00132 if (regionName == (G4String)"precisionRegion") {
00133 precisionRegion=true;
00134 }
00135
00136
00137 if ((HitID[nCopy]==-1) || precisionRegion){
00138
00139
00140
00141 BDSEnergyCounterHit* ECHit = new BDSEnergyCounterHit(nCopy,enrg,xpos,ypos,zpos,volName, ptype, weight, precisionRegion);
00142 HitID[nCopy]= BDSEnergyCounterCollection->insert(ECHit)-1;
00143 } else {
00144
00145
00146 (*BDSEnergyCounterCollection)[HitID[nCopy]]-> AddEnergyWeightedPosition(enrg, xpos, ypos, zpos, weight);
00147 }
00148
00149 if(BDSGlobalConstants::Instance()->GetStopTracks())
00150 aStep->GetTrack()->SetTrackStatus(fStopAndKill);
00151
00152
00153 return true;
00154 }
00155
00156
00157
00158 G4bool BDSEnergyCounterSD::ProcessHits(G4GFlashSpot *aSpot,G4TouchableHistory*)
00159 {
00160 enrg = aSpot->GetEnergySpot()->GetEnergy();
00161 #ifdef DEBUG
00162 G4cout << "BDSEnergyCounterSD>gflash enrg = " << enrg << G4endl;
00163 #endif
00164 if (enrg==0.) return false;
00165 G4VPhysicalVolume* pCurrentVolume = aSpot->GetTouchableHandle()->GetVolume();
00166 G4String volName = pCurrentVolume->GetName();
00167 G4int nCopy=pCurrentVolume->GetCopyNo();
00168 #ifdef DEBUG
00169 if(nCopy>0){
00170 G4cout << "BDSEnergyCounterSD::ProcessHits>gFlash nCopy = " << nCopy << G4endl;
00171 }
00172 #endif
00173 if(nCopy>NMAXCOPY-1)
00174 {
00175 G4cerr<<" BDSEnergyCounterSD: nCopy too large: nCopy="<<nCopy<<
00176 "NMAXCOPY="<<NMAXCOPY<<" Volume="<< volName;
00177
00178 G4Exception("Killing program in BDSEnergyCounterSD::ProcessHits", "-1", FatalException, "");
00179 }
00180
00181
00182
00183 G4AffineTransform tf=(aSpot->GetTouchableHandle()->GetHistory()->GetTopTransform());
00184 G4ThreeVector pos = aSpot->GetPosition();
00185 G4ThreeVector LocalPosition = tf.TransformPoint(pos);
00186
00187 zpos=pos.z();
00188 xpos=pos.x();
00189 ypos=pos.y();
00190
00191 if(verbose && BDSGlobalConstants::Instance()->GetStopTracks()) G4cout << "BDSEnergyCounterSD: Current Volume: " << volName <<"\tEvent: " << event_number << "\tEnergy: " << enrg/GeV << "GeV\tPosition: " << zpos/m <<"m"<< G4endl;
00192
00193 G4double weight = aSpot->GetOriginatorTrack()->GetPrimaryTrack()->GetWeight();
00194 if (weight == 0){
00195 G4cerr << "Error: BDSEnergyCounterSD: weight = 0" << G4endl;
00196 exit(1);
00197 }
00198 int ptype = aSpot->GetOriginatorTrack()->GetPrimaryTrack()->GetDefinition()->GetPDGEncoding();
00199 if (HitID[nCopy]==-1){
00200 BDSEnergyCounterHit* ECHit = new BDSEnergyCounterHit(nCopy,enrg,xpos,ypos,zpos,volName, ptype, weight, 0);
00201 HitID[nCopy]= BDSEnergyCounterCollection->insert(ECHit)-1;
00202 } else {
00203 (*BDSEnergyCounterCollection)[HitID[nCopy]]-> AddEnergyWeightedPosition(enrg, xpos, ypos, zpos, weight);
00204 }
00205 return true;
00206 }
00207
00208
00209 void BDSEnergyCounterSD::EndOfEvent(G4HCofThisEvent*HCE)
00210 {
00211 G4int HCID = GetCollectionID(0);
00212 HCE->AddHitsCollection( HCID, BDSEnergyCounterCollection );
00213
00214
00215
00216
00217 }
00218
00219 void BDSEnergyCounterSD::clear()
00220 {}
00221
00222 void BDSEnergyCounterSD::DrawAll()
00223 {}
00224
00225 void BDSEnergyCounterSD::PrintAll()
00226 {}
00227