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