ATLAS Offline Software
HGTDSensorSD.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // HGTD Sensitive Detector.
6 // The Hits are processed here. For every hit, the position and information
7 // on the sensor in which the interaction happened are obtained.
8 
9 // Class header
10 #include "HGTDSensorSD.h"
11 
12 // Athena headers
13 #include "MCTruth/TrackHelper.h"
14 
15 // Geant4 headers
16 #include "G4ChargedGeantino.hh"
17 #include "G4Geantino.hh"
18 #include "G4SDManager.hh"
19 #include "G4Step.hh"
20 #include "G4VisExtent.hh" // Added to get extent of sensors
21 #include "G4VSolid.hh" // Added to get extent of sensors
22 
23 // CLHEP headers
24 #include "CLHEP/Geometry/Transform3D.h"
25 #include "CLHEP/Units/SystemOfUnits.h"
26 #include <stdint.h>
27 #include <string.h>
28 
29 HGTDSensorSD::HGTDSensorSD(const std::string& name, const std::string& hitCollectionName)
30  : G4VSensitiveDetector( name ),
31  m_HitColl( hitCollectionName )
32 {
33 
34 }
35 
36 // Initialize from G4
37 void HGTDSensorSD::Initialize(G4HCofThisEvent *)
38 {
39  if (!m_HitColl.isValid()) m_HitColl = std::make_unique<SiHitCollection>();
40 }
41 
42 G4bool HGTDSensorSD::ProcessHits(G4Step* aStep, G4TouchableHistory* /*ROhist*/)
43 {
44  if (verboseLevel>5) G4cout << "Process Hit" << G4endl;
45 
46  G4double edep = aStep->GetTotalEnergyDeposit();
47  edep *= CLHEP::MeV;
48 
49  if (edep==0.) {
50  if (aStep->GetTrack()->GetDefinition() != G4Geantino::GeantinoDefinition() &&
51  aStep->GetTrack()->GetDefinition() != G4ChargedGeantino::ChargedGeantinoDefinition())
52  return false;
53  }
54 
55  //
56  // Get the Touchable History:
57  //
58  const G4TouchableHistory* myTouch = dynamic_cast<const G4TouchableHistory*>(aStep->GetPreStepPoint()->GetTouchable());
59 
60  if(verboseLevel>5){
61  for (int i=0;i<myTouch->GetHistoryDepth();i++){
62  std::string detname = myTouch->GetVolume(i)->GetLogicalVolume()->GetName();
63  int copyno = myTouch->GetVolume(i)->GetCopyNo();
64  G4cout << "Volume " << detname << " Copy Nr. " << copyno << G4endl;
65  }
66  }
67 
68  //
69  // Get the hit coordinates. Start and End Point
70  //
71  G4ThreeVector startCoord = aStep->GetPreStepPoint()->GetPosition();
72  G4ThreeVector endCoord = aStep->GetPostStepPoint()->GetPosition();
73  // get the average position for writing
74  G4ThreeVector globalPosition = (startCoord + endCoord)/2;
75 
76  // now deal with the Identifier
77  int posNegEndcap = ( globalPosition.z() > 0. ? 1 : -1 );
78 
79  // make sure that it's a Sensor:
80  std::string detname_layer=myTouch->GetVolume(0)->GetLogicalVolume()->GetName();
81  if ( detname_layer.find("Sensor") == std::string::npos ) {
82  // Do not expect other names. Need to fix HGTDGeoModel if this occurs.
83  G4ExceptionDescription description;
84  description << "ProcessHits: No HGTD sensitive detector with substring Sensor found. Check HGTD Detector Description.";
85  G4Exception("HGTDSensorSD", "UnrecognizedHGTDGeometry", FatalException, description);
86  abort();
87  }
88 
89  if(verboseLevel>5){
90  // The following code has been used to check the transformations between the detector elements
91  // TRANSFORM from local to global / step by step
92  for ( int i = myTouch->GetHistory()->GetDepth(); i >= 0 ; i-- ) {
93  std::string detname = myTouch->GetHistory()->GetVolume(i)->GetLogicalVolume()->GetName();
94  G4VisExtent extent = myTouch->GetHistory()->GetVolume(i)->GetLogicalVolume()->GetSolid()->GetExtent();
95  int copyno=myTouch->GetHistory()->GetVolume(i)->GetCopyNo();
96  const G4AffineTransform transformation1 = myTouch->GetHistory()->GetTransform( i ); // Transformation from global to current
97  const G4AffineTransform transformationInverse = transformation1.Inverse(); // Transformation from current to global
98  G4ThreeVector pos_center_local(0.0, 0.0, 0.0);
99  G4ThreeVector pos_center_global = transformationInverse.TransformPoint( pos_center_local );
100  G4ThreeVector pos_current = transformation1.TransformPoint( globalPosition );
101  G4cout << "DEBUG HGTDG4SD : "
102  << "VOLUME: " << i << " detname: " << detname
103  << ", center of element : " << pos_center_global.x()*CLHEP::mm << ", y: " << pos_center_global.y()*CLHEP::mm << ", z: " << pos_center_global.z()*CLHEP::mm
104  << ", extent: x: " << extent.GetXmax() - extent.GetXmin() << ", y: " << extent.GetYmax() - extent.GetYmin() << ", z: " << extent.GetZmax() - extent.GetZmin()
105  << ", copyno: " << copyno << G4endl;
106  G4cout << "DEBUG HGTDG4SD : LOCAL: x: " << pos_current.x()*CLHEP::mm << ", y: " << pos_current.y()*CLHEP::mm << ", z: " << pos_current.z()*CLHEP::mm << G4endl;
107 
108  if ( i >= 1) {
109  const G4AffineTransform transformation2 = myTouch->GetHistory()->GetTransform( i-1 ); // Transformation from global to 1 up
110  G4AffineTransform transformation_up; // Transformation from current to 1 up
111  transformation_up.Product( transformationInverse, transformation2 );
112  G4ThreeVector pos_up = transformation_up.TransformPoint( pos_current );
113  G4RotationMatrix rotmat = transformation_up.NetRotation(); // https://www-zeuthen.desy.de/geant4/clhep-2.0.4.3/classCLHEP_1_1HepRotation.html
114  G4ThreeVector translation = transformation_up.NetTranslation(); // https://www-zeuthen.desy.de/ILC/geant4/clhep-2.0.4.3/classCLHEP_1_1Hep3Vector.html
115  G4cout << "DEBUG HGTDG4SD : Rotation:"
116  << "| xx:" << rotmat.xx() << ", xy: " << rotmat.xy() << ", xz: " << rotmat.xz()
117  << "| yx:" << rotmat.yx() << ", yy: " << rotmat.yy() << ", yz: " << rotmat.yz()
118  << "| zx:" << rotmat.zx() << ", zy: " << rotmat.zy() << ", zz: " << rotmat.zz() << " | " << G4endl;
119  G4cout << "DEBUG HGTDG4SD : Translation: x: " << translation.x() << ", y:" << translation.y() << ", z:" << translation.z() << G4endl;
120  G4cout << "DEBUG HGTDG4SD : TRANSFORMED: x:" << pos_up.x()*CLHEP::mm << ", y:" << pos_up.y()*CLHEP::mm << ", z:" << pos_up.z()*CLHEP::mm << G4endl;
121  }
122  } // end loop stackdepth
123  }
124 
125  // Create the SiHits
126 
127  const G4AffineTransform transformation = myTouch->GetHistory()->GetTopTransform();
128 
129  G4ThreeVector localPosition1 = transformation.TransformPoint(startCoord);
130  G4ThreeVector localPosition2 = transformation.TransformPoint(endCoord);
131 
132  if(verboseLevel>5){
133  G4cout << " PreStepPoint " << G4endl;
134  G4cout << " x (global/local) " << startCoord.x()*CLHEP::mm << " " << localPosition1[0]*CLHEP::mm << G4endl;
135  G4cout << " y (global/local) " << startCoord.y()*CLHEP::mm << " " << localPosition1[1]*CLHEP::mm << G4endl;
136  G4cout << " z (global/local) " << startCoord.z()*CLHEP::mm << " " << localPosition1[2]*CLHEP::mm << G4endl;
137 
138  G4cout << " PostStepPoint: " << G4endl;
139  G4cout << " x (global/local) " << endCoord.x()*CLHEP::mm << " " << localPosition2[0]*CLHEP::mm << G4endl;
140  G4cout << " y (global/local) " << endCoord.y()*CLHEP::mm << " " << localPosition2[1]*CLHEP::mm << G4endl;
141  G4cout << " z (global/local) " << endCoord.z()*CLHEP::mm << " " << localPosition2[2]*CLHEP::mm << G4endl;
142  }
143 
144  HepGeom::Point3D<double> lP1,lP2;
145  lP1[SiHit::xEta] = localPosition1[1]*CLHEP::mm; //long edge of the module
146  lP1[SiHit::xPhi] = localPosition1[0]*CLHEP::mm; //short edge of the module
147  lP1[SiHit::xDep] = localPosition1[2]*CLHEP::mm; //depth (z)
148 
149  lP2[SiHit::xEta] = localPosition2[1]*CLHEP::mm;
150  lP2[SiHit::xPhi] = localPosition2[0]*CLHEP::mm;
151  lP2[SiHit::xDep] = localPosition2[2]*CLHEP::mm;
152 
153  std::string module_indices = myTouch->GetVolume(1)->GetLogicalVolume()->GetName();
154  std::size_t found = module_indices.find_last_of("_");
155 
156  // get indices from the volume name
157  // nomenclature is expected to be e.g. "HGTDModule0_layer_0_1_2"
158  // for layer=0, phi=1, eta=2 (defined from HGTD_DetectorFactory)
159  int eta = atoi((module_indices.substr(found+1)).c_str());
160  module_indices.erase(found);
161  found = module_indices.find_last_of("_");
162  int phi = atoi((module_indices.substr(found+1)).c_str());
163  module_indices.erase(found);
164  found = module_indices.find_last_of("_");
165  int layer = atoi((module_indices.substr(found+1)).c_str());
166 
167  int endcap_side = 2*posNegEndcap;
168 
169  TrackHelper trHelp(aStep->GetTrack());
170  m_HitColl->Emplace(lP1,
171  lP2,
172  edep,
173  aStep->GetPreStepPoint()->GetGlobalTime(),
174  trHelp.GenerateParticleLink(),
175  // this is hgtd, endcap_barrel, layer_disk, eta_module, phi_module, side
176  2,endcap_side,layer,eta,phi,0);
177 
178  return true;
179 }
HGTDSensorSD::ProcessHits
G4bool ProcessHits(G4Step *, G4TouchableHistory *) override final
Definition: HGTDSensorSD.cxx:42
phi
Scalar phi() const
phi method
Definition: AmgMatrixBasePlugin.h:67
eta
Scalar eta() const
pseudorapidity method
Definition: AmgMatrixBasePlugin.h:83
TrackHelper.h
python.SystemOfUnits.MeV
int MeV
Definition: SystemOfUnits.py:154
HGTDSensorSD::Initialize
void Initialize(G4HCofThisEvent *) override final
Definition: HGTDSensorSD.cxx:37
TrackHelper
Definition: TrackHelper.h:14
SiHit::xDep
@ xDep
Definition: SiHit.h:162
lumiFormat.i
int i
Definition: lumiFormat.py:85
TRT::Hit::layer
@ layer
Definition: HitInfo.h:79
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
SiHit::xEta
@ xEta
Definition: SiHit.h:162
python.SystemOfUnits.mm
int mm
Definition: SystemOfUnits.py:83
HGTDSensorSD::HGTDSensorSD
HGTDSensorSD(const std::string &name, const std::string &hitCollectionName)
Definition: HGTDSensorSD.cxx:29
CondAlgsOpts.found
int found
Definition: CondAlgsOpts.py:101
HGTDSensorSD.h
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
SiHit::xPhi
@ xPhi
Definition: SiHit.h:162
TrackHelper::GenerateParticleLink
HepMcParticleLink GenerateParticleLink()
Generates a creates new HepMcParticleLink object on the stack based on GetUniqueID(),...
Definition: TrackHelper.h:35
HGTDSensorSD::m_HitColl
SG::WriteHandle< SiHitCollection > m_HitColl
Definition: HGTDSensorSD.h:48
description
std::string description
glabal timer - how long have I taken so far?
Definition: hcg.cxx:88