ATLAS Offline Software
Loading...
Searching...
No Matches
TGCSensitiveDetectorCosmics.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3*/
4
6#include <string>
10#include "G4Exception.hh"
11#include "G4Geantino.hh"
12#include "G4ChargedGeantino.hh"
13
15
16// construction/destruction
17TGCSensitiveDetectorCosmics::TGCSensitiveDetectorCosmics(const std::string& name, const std::string& hitCollectionName)
18 : G4VSensitiveDetector( name )
19 , m_momMag(0)
20 , m_globalTime(0)
21 , m_hitCollectionName( hitCollectionName )
22{
24}
25
26// Implemenation of member functions
28{
29 m_myTGCHitColl = nullptr;
30 if (auto* eventInfo = AtlasG4EventUserInfo::GetEventUserInfo()) {
31 m_myTGCHitColl = eventInfo->GetHitCollectionMap()->Find<TGCSimHitCollection>(m_hitCollectionName);
32 m_g4UserEventInfo = eventInfo;
33 }
34 // START OF COSMICS-SPECIFIC CODE
35 m_mom = Amg::Vector3D(0.,0.,0.);
36 m_globH = Amg::Vector3D(0.,0.,0.);
37 // END OF COSMICS-SPECIFIC CODE
38}
39
40G4bool TGCSensitiveDetectorCosmics::ProcessHits(G4Step* aStep,G4TouchableHistory*) {
41
42 if (!m_myTGCHitColl) {
43 G4Exception("TGCSensitiveDetectorCosmics::ProcessHits", "TGCCosmicHitCollectionMissing", FatalException,
44 "Hit collection not initialized; did SetupEvent run?");
45 return false;
46 }
47
48 G4Track* track = aStep->GetTrack();
49
50 if (track->GetDefinition()->GetPDGCharge() == 0.0) {
51 if (track->GetDefinition()!=G4Geantino::GeantinoDefinition()) return true;
52 else if (track->GetDefinition()==G4ChargedGeantino::ChargedGeantinoDefinition()) return true;
53 }
54 const G4TouchableHistory* touchHist = static_cast<const G4TouchableHistory*>(aStep->GetPreStepPoint()->GetTouchable());
55 G4ThreeVector position = aStep->GetPreStepPoint()->GetPosition();
56 const G4AffineTransform trans = track->GetTouchable()->GetHistory()->GetTopTransform();
57
58 // fields for the TGC identifier construction
59 std::string stationName;
60 int stationEta(0);
61 int stationPhi(0);
62 int gasGap = 0;
63
64 // TGC hit information
65 double globalTime = aStep->GetPreStepPoint()->GetGlobalTime();
66 Amg::Vector3D localPosition = Amg::Hep3VectorToEigen( trans.TransformPoint(position) );
67 G4ThreeVector direcos = aStep->GetPreStepPoint()->GetMomentumDirection();
68 Amg::Vector3D localDireCos = Amg::Hep3VectorToEigen( trans.TransformAxis(direcos) );
69
70 // START OF COSMICS-SPECIFIC CODE
71 // global coordinates
72 G4ThreeVector globVrtx = aStep->GetPreStepPoint()->GetPosition();
73
74 // distance of the hit from (0,0,0) vertex - calculated from the PreStepPoint (approximation)
75 double dist = globVrtx.mag();
76 double inv_lightspeed = 1. / CLHEP::c_light;
77 double tOrigin = dist * inv_lightspeed;
78
79 G4int trackid = aStep->GetTrack()->GetTrackID();
80 m_currVertex = Amg::Hep3VectorToEigen(aStep->GetTrack()->GetVertexPosition());
81
82 // for cosmics: only primary muon tracks - track momentum when first entering the spectrometer (one muon per event)
83 if ((m_currVertex != m_vertex) && (trackid == 1)) {
84 // after calculationg the momentum magnidude, normalize it
85 m_mom = Amg::Hep3VectorToEigen(track->GetMomentum());
86 m_momMag = m_mom.mag();
87 m_mom.normalize();
88 // the direction of the primary mu is used to calculate the t0, the position ot the t0, m_globH, is ONE for a track
89 Amg::Vector3D globVrtxFix = Amg::Hep3VectorToEigen( globVrtx );
90 double AlphaGlobal = -1*(globVrtxFix[0]*m_mom[0] + globVrtxFix[1]*m_mom[1] + globVrtxFix[2]*m_mom[2])/(m_mom[0]*m_mom[0] + m_mom[1]*m_mom[1] + m_mom[2]*m_mom[2]);
91 m_globH = globVrtxFix + AlphaGlobal*m_mom;
92 // G4cout << "COSMICS MAIN TRACK IN THE TGC!" << G4endl;
93 }
94 double globalDist = sqrt((m_globH[0] - globVrtx[0])*(m_globH[0] - globVrtx[0]) +
95 (m_globH[1] - globVrtx[1])*(m_globH[1] - globVrtx[1]) +
96 (m_globH[2] - globVrtx[2])*(m_globH[2] - globVrtx[2]));
97 double tof = globalDist * inv_lightspeed;
98 // END OF COSMICS-SPECIFIC CODE
99
100 // scan geometry tree to identify hit channel
101 int zside(0);
102 for (int i=touchHist->GetHistoryDepth();i>=0;i--) {
103
104 std::string::size_type npos;
105 std::string volName = touchHist->GetVolume(i)->GetName();
106
107 // stationName and stationPhi
108 if ((npos = volName.find("station")) != std::string::npos) {
109
110 stationName = volName.substr(0,npos-2);
111 int volCopyNo = touchHist->GetVolume(i)->GetCopyNo();
112 if (volCopyNo > 0) {
113 zside = 1;
114 } else {
115 zside = -1;
116 }
117 if (stationName.compare(2,1, "F") == 0) {
118
119 stationPhi = (abs(volCopyNo%100)-1)*3;
120 if (abs(volCopyNo/100) > 3) {
121 stationPhi += abs(volCopyNo/100)-3;
122 } else {
123 stationPhi += abs(volCopyNo/100);
124 }
125
126 stationPhi -= 1;
127 if (stationPhi <= 0) {
128 stationPhi = 24 - stationPhi;
129 }
130
131 } else if (stationName.compare(2,1,"E") == 0) {
132 if (stationName.compare(1,1,"4") == 0) {
133
134 stationPhi = (abs(volCopyNo%100)-1)*3+abs(volCopyNo/100);
135
136 if (abs(volCopyNo%100) < 4) {
137 stationPhi = stationPhi - 1;
138 if (stationPhi <= 0) {
139 stationPhi = 21 - stationPhi;
140 }
141 } else if(abs(volCopyNo%100) < 7) {
142 stationPhi = stationPhi - 1 - 1;
143 } else {
144 stationPhi = stationPhi - 2 - 1;
145 }
146
147 } else {
148
149 stationPhi = (abs(volCopyNo%100)-1)*6+abs(volCopyNo/100);
150
151 stationPhi -= 2;
152 if (stationPhi <= 0) {
153 stationPhi = 48 - stationPhi;
154 }
155 }
156 }
157
158 // stationEta
159 } else if ((npos = volName.find("tgccomponent")) != std::string::npos) {
160 int volCopyNo = abs(touchHist->GetVolume(i)->GetCopyNo());
161 stationEta = zside*volCopyNo%100;
162 if (volCopyNo > 1000) { // stationPhi overridden by the number assigned by MuonGeoModel
163 stationPhi = volCopyNo/1000;
164 }
165
166 // gasGap
167 } else if ((npos = volName.find("Gas Volume Layer")) != std::string::npos) {
168 int volCopyNo = touchHist->GetVolume(i)->GetCopyNo();
169 int iStation = atoi(stationName.substr(1,1).c_str());
170 if (zside < 0) {
171 if (iStation == 1)
172 gasGap = 3-(volCopyNo-3)/4;
173 else
174 gasGap = 2-(volCopyNo-3)/4;
175 } else {
176// if (iStation == 1)
177 gasGap = (volCopyNo-3)/4+1;
178// else
179// gasGap = (volCopyNo-3)/4+1;
180 }
181 } else if ((npos = volName.find("TGCGas")) != std::string::npos) {
182
183 int volCopyNo = touchHist->GetVolume(i)->GetCopyNo();
184
185 if (volCopyNo != 0)
186 gasGap = volCopyNo;
187 }
188 }
189
190 //construct the hit identifier
191 HitID TGCid = m_muonHelper->BuildTgcHitId(stationName,
192 stationPhi,
193 stationEta,
194 gasGap);
195 //m_muonHelper->Print(TGCid);
196 // START OF COSMICS-SPECIFIC CODE
197 m_vertex = Amg::Hep3VectorToEigen(aStep->GetTrack()->GetVertexPosition());
198 // if the track m_vertex is far from (0,0,0), takes the tof, otherwise take the "usual" g4 globalTime
199 ((((m_vertex.mag()) < 100) || ((fabs(globalTime - tOrigin)) < 0.1) ) ? (m_globalTime = globalTime) : (m_globalTime = tof));
200 // if m_globalTime != globalTime and m_globalTime != tof in the output, this is due to multiple hits
201 // before founding the good one (small approximation)
202 // END OF COSMICS-SPECIFIC CODE
203
204 // construct new mdt hit
205 TrackHelper trHelp(aStep->GetTrack());
206
207 m_myTGCHitColl->Emplace(TGCid,
209 localPosition,
210 localDireCos,
211 trHelp.GenerateParticleLink(m_g4UserEventInfo ? m_g4UserEventInfo->GetEventStore() : nullptr),
212 aStep->GetTotalEnergyDeposit(),
213 aStep->GetStepLength());
214 return true;
215}
int HitID
AtlasHitsVector< TGCSimHit > TGCSimHitCollection
static AtlasG4EventUserInfo * GetEventUserInfo()
void Initialize(G4HCofThisEvent *HCE) override final
member functions
G4bool ProcessHits(G4Step *aStep, G4TouchableHistory *ROHist) override final
TGCSensitiveDetectorCosmics(const std::string &name, const std::string &hitCollectionName)
construction/destruction
static const TgcHitIdHelper * GetHelper()
HepMcParticleLink GenerateParticleLink()
Generates a creates new HepMcParticleLink object on the stack based on GetUniqueID(),...
Definition TrackHelper.h:52
Amg::Vector3D Hep3VectorToEigen(const CLHEP::Hep3Vector &CLHEPvector)
Converts a CLHEP-based CLHEP::Hep3Vector into an Eigen-based Amg::Vector3D.
Eigen::Matrix< double, 3, 1 > Vector3D