Loading...
Searching...
No Matches
Par04SensitiveDetector.cc
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
27#include <CLHEP/Vector/Rotation.h> // for HepRotation
28#include <CLHEP/Vector/ThreeVector.h> // for Hep3Vector
29#include <cmath> // for floor
30#include <G4CollectionNameVector.hh> // for G4CollectionNameVector
31#include <G4FastHit.hh> // for G4FastHit
32#include <G4FastTrack.hh> // for G4FastTrack
33#include <G4RotationMatrix.hh> // for G4RotationMatrix
34#include <G4StepPoint.hh> // for G4StepPoint
35#include <G4THitsCollection.hh> // for G4THitsCollection
36#include <G4ThreeVector.hh> // for G4ThreeVector
37#include <G4VSensitiveDetector.hh> // for G4VSensitiveDetector
38#include <G4VUserEventInformation.hh> // for G4VUserEventInformation
39#include <cstddef> // for size_t
40#include <vector> // for vector
41#include "G4Event.hh" // for G4Event
42#include "G4EventManager.hh" // for G4EventManager
43#include "G4HCofThisEvent.hh" // for G4HCofThisEvent
44#include "G4SDManager.hh" // for G4SDManager
45#include "G4Step.hh" // for G4Step
46#include "G4Track.hh" // for G4Track
47#include "Par04EventInformation.hh" // for Par04EventInformation
48#include "Par04Hit.hh" // for Par04Hit, Par04HitsCollection
49
50//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
51
54{
55 collectionName.insert("hits");
56}
57//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
58
60 G4ThreeVector aSize)
62 , fMeshNbOfCells(aNb)
63 , fMeshSizeOfCells(aSize)
64{
65 collectionName.insert("hits");
66}
67
68//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
69
71
72//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
73
75{
76 fHitsCollection = new Par04HitsCollection(SensitiveDetectorName, collectionName[0]);
77 if(fHitCollectionID < 0)
78 {
79 fHitCollectionID = G4SDManager::GetSDMpointer()->GetCollectionID(fHitsCollection);
80 }
81 aHCE->AddHitsCollection(fHitCollectionID, fHitsCollection);
82
83 // reset entrance position
84 fEntrancePosition.set(-1, -1, -1);
85 fEntranceDirection.set(-1, -1, -1);
86}
87
88//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
89
91{
92 G4double edep = aStep->GetTotalEnergyDeposit();
93 if(edep == 0.)
94 return true;
95
96 auto hit = RetrieveAndSetupHit(aStep->GetPostStepPoint()->GetPosition());
97 if(hit == nullptr)
98 return true;
99
100 // Add energy deposit from G4Step
101 hit->AddEdep(edep);
102 // Increment the counter
103 hit->AddNdep(1);
104
105 // Fill time information from G4Step
106 // If it's already filled, choose hit with earliest global time
107 if(hit->GetTime() == -1 || hit->GetTime() > aStep->GetTrack()->GetGlobalTime())
108 hit->SetTime(aStep->GetTrack()->GetGlobalTime());
109
110 // Set hit type to full simulation (only if hit is not already marked as fast
111 // sim)
112 if(hit->GetType() != 1)
113 hit->SetType(0);
114
115 return true;
116}
117
118//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
119
122{
123 G4double edep = aHit->GetEnergy();
124 if(edep == 0.)
125 return true;
126
127 auto hit = RetrieveAndSetupHit(aHit->GetPosition());
128 if(hit == nullptr)
129 return true;
130
131 // Add energy deposit from G4FastHit
132 hit->AddEdep(edep);
133 // Increment the counter
134 hit->AddNdep(1);
135
136 // Fill time information from G4FastTrack
137 // If it's already filled, choose hit with earliest global time
138 if(hit->GetTime() == -1 || hit->GetTime() > aTrack->GetPrimaryTrack()->GetGlobalTime())
139 {
140 hit->SetTime(aTrack->GetPrimaryTrack()->GetGlobalTime());
141 }
142
143 // Set hit type to fast simulation (even if hit was already marked as full
144 // sim, overwrite it)
145 hit->SetType(1);
146
147 return true;
148}
149
150//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
151
153{
154 if(fEntrancePosition.x() == -1)
155 {
156 auto info = dynamic_cast<Par04EventInformation*>(
157 G4EventManager::GetEventManager()->GetConstCurrentEvent()->GetUserInformation());
158 if(info == nullptr)
159 return nullptr;
161 fEntranceDirection = info->GetDirection();
162 }
163
164 auto delta = aGlobalPosition - fEntrancePosition;
165
166 // Calculate rotation matrix along the particle momentum direction
167 // It will rotate the shower axes to match the incoming particle direction
168 G4RotationMatrix rotMatrix = G4RotationMatrix();
169 double particleTheta = fEntranceDirection.theta();
170 double particlePhi = fEntranceDirection.phi();
171 rotMatrix.rotateZ(-particlePhi);
172 rotMatrix.rotateY(-particleTheta);
173 G4RotationMatrix rotMatrixInv = CLHEP::inverseOf(rotMatrix);
174
175 delta = rotMatrix * delta;
176
177 G4int rhoNo = std::floor(delta.perp() / fMeshSizeOfCells.x());
178 G4int phiNo = std::floor((CLHEP::pi + delta.phi()) / fMeshSizeOfCells.y());
179 G4int zNo = std::floor(delta.z() / fMeshSizeOfCells.z());
180
181 std::size_t hitID =
182 fMeshNbOfCells.x() * fMeshNbOfCells.z() * phiNo + fMeshNbOfCells.z() * rhoNo + zNo;
183
184 if(zNo >= fMeshNbOfCells.z() || rhoNo >= fMeshNbOfCells.x() || zNo < 0)
185 {
186 return nullptr;
187 }
188
189 auto hit = fHitsMap[hitID].get();
190 if (hit==nullptr) {
191 fHitsMap[hitID] = std::unique_ptr<Par04Hit>(new Par04Hit());
192 hit = fHitsMap[hitID].get();
193 hit->SetPhiId(phiNo);
194 hit->SetRhoId(rhoNo);
195 hit->SetZid(zNo);
196 hit->SetRot(rotMatrixInv);
197 hit->SetPos(fEntrancePosition +
198 rotMatrixInv * G4ThreeVector(0, 0, (zNo + 0.5) * fMeshSizeOfCells.z()));
199
200 }
201 return hit;
202}
203
204//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
205
207{
208 for(const auto& hits: fHitsMap){
209 fHitsCollection->insert(new Par04Hit(*hits.second.get()));
210 }
211 fHitsMap.clear();
212}
G4THitsCollection< Par04Hit > Par04HitsCollection
Definition Par04Hit.hh:145
G4double GetEnergy() const
G4ThreeVector GetPosition() const
G4ThreeVector GetPosition() const
Get particle position.
Hit class to store energy deposited in the sensitive detector.
G4ThreeVector fMeshSizeOfCells
Size of mesh readout cells in cylindrical coordinates.
G4ThreeVector fEntrancePosition
Retrieved once per event: position of entering particle.
std::unordered_map< G4int, std::unique_ptr< Par04Hit > > fHitsMap
Map of hits to be used in runtime.
G4ThreeVector fMeshNbOfCells
Number of mesh readout cells in cylindrical coordinates.
Par04Hit * RetrieveAndSetupHit(G4ThreeVector aPosition)
Process energy deposit - common part for full and fast simulation It is invoked from ProcessHits() me...
virtual void Initialize(G4HCofThisEvent *HCE) final
Create hit collection.
virtual G4bool ProcessHits(G4Step *aStep, G4TouchableHistory *aROhist) final
Process energy deposit from the full simulation.
virtual ~Par04SensitiveDetector()
G4int fHitCollectionID
ID of collection of hits.
virtual void EndOfEvent(G4HCofThisEvent *aHC) final
Rewrite hits map to a vector.
G4ThreeVector fEntranceDirection
Retrieved once per event: direction of entering particle.
Par04HitsCollection * fHitsCollection
Collection of hits.

Applications | User Support | Publications | Collaboration