ATLAS Offline Software
CSCSensitiveDetectorCosmics.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 "G4Trd.hh"
7 #include <string>
8 #include <sstream>
10 #include "MCTruth/TrackHelper.h"
11 #include "G4Geantino.hh"
12 #include "G4ChargedGeantino.hh"
13 
15 
16 //comment the following line if you want hit information print out
17 //#define CSCG4_DEBUG
18 
19 // construction/destruction
20 CSCSensitiveDetectorCosmics::CSCSensitiveDetectorCosmics(const std::string& name, const std::string& hitCollectionName)
21  : G4VSensitiveDetector( name )
22  // START OF COSMICS-SPECIFIC CODE
23  , m_momMag(0.)
24  , m_globalTime(0.)
25  // END OF COSMICS-SPECIFIC CODE
26  , m_myCSCHitColl( hitCollectionName )
27 {
29 }
30 
31 // Implemenation of memebr functions
33 {
34  if (!m_myCSCHitColl.isValid()) m_myCSCHitColl = std::make_unique<CSCSimHitCollection>();
35  // START OF COSMICS-SPECIFIC CODE
36  m_mom = Amg::Vector3D(0.,0.,0.);
37  m_globH = Amg::Vector3D(0.,0.,0.);
38  // END OF COSMICS-SPECIFIC CODE
39 }
40 
41 G4bool CSCSensitiveDetectorCosmics::ProcessHits(G4Step* aStep,G4TouchableHistory* /*ROHist*/) {
42 
43  G4Track* currentTrack = aStep->GetTrack();
44 
46  auto trackDef = currentTrack->GetDefinition();
47  if (trackDef->GetPDGCharge() == 0.0) {
48  if (trackDef != G4Geantino::GeantinoDefinition()) return true;
49  else if (trackDef == G4ChargedGeantino::ChargedGeantinoDefinition()) return true;
50  }
51 
52  const G4TouchableHistory* touchHist = static_cast<const G4TouchableHistory*>(aStep->GetPreStepPoint()->GetTouchable());
53  G4ThreeVector startPos=aStep->GetPreStepPoint()->GetPosition();
54  G4ThreeVector endPos=aStep->GetPostStepPoint()->GetPosition();
55  double kinEnergy = aStep->GetPreStepPoint()->GetKineticEnergy();
56 
59  std::string stationName="";
60  int stationEta=0;
61  int stationPhi=0;
62  int multiLayer=0;
63  int wireLayer=0;
64 
67  Amg::Vector3D HitStart = Amg::Vector3D(-1000,-1000,-1000);
68  Amg::Vector3D HitEnd = Amg::Vector3D(-1000,-1000,-1000);
69  double globalTime = -1;
70  double energyDeposit= -1;
71  G4int lundcode = -1;
73  //const int trackid(aStep->GetTrack()->GetTrackID());
74 
75  // START OF COSMICS-SPECIFIC CODE
76  // global coordinates
77  Amg::Vector3D globVrtx = Amg::Hep3VectorToEigen( aStep->GetPreStepPoint()->GetPosition() );
78 
79  // distance of the hit from (0,0,0) vertex - calculated from the PreStepPoint (approximation)
80  const double dist = globVrtx.mag();
81  const double lightspeed = 299.792458; /* in vacuo speed of light [mm/ns] */ //FIXME this should be taken from standard headers
82  const double tOrigin = dist / lightspeed;
83 
84  m_currVertex = Amg::Hep3VectorToEigen( aStep->GetTrack()->GetVertexPosition() );
85 
86  // for cosmics: only primary muon tracks - track momentum when first entering the spectrometer (one muon per event)
87  if ((m_currVertex != m_vertex) && (aStep->GetTrack()->GetTrackID() == 1)) {
88  // after calculationg the momentum magnidude, normalize it
89  m_mom = Amg::Hep3VectorToEigen( currentTrack->GetMomentum() );
90  m_momMag = m_mom.mag();
91  m_mom.normalize();
92  // the direction of the primary mu is used to calculate the t0, the position to the t0, m_globH, is ONE for a track
93  Amg::Vector3D globVrtxFix = globVrtx;
94  const 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]);
95  m_globH = globVrtxFix + AlphaGlobal*m_mom;
96  // G4cout << "COSMICS MAIN TRACK IN THES CSC!" << G4endl;
97  }
98  const double globalDist = sqrt((m_globH[0] - globVrtx[0])*(m_globH[0] - globVrtx[0]) +
99  (m_globH[1] - globVrtx[1])*(m_globH[1] - globVrtx[1]) +
100  (m_globH[2] - globVrtx[2])*(m_globH[2] - globVrtx[2]));
101  double tof = globalDist / lightspeed;
102  // END OF COSMICS SPECIFIC CODE
103 
106  bool isAssembly = false;
107  for (int i = touchHist->GetHistoryDepth(); i>=0; --i) {
108  std::string::size_type npos;
109  std::string volName = touchHist->GetVolume(i)->GetName();
110 
112  if ((npos = volName.find("av_")) != std::string::npos &&
113  (npos = volName.find("impr_")) != std::string::npos) isAssembly = true;
114 
115  if ((npos = volName.find("station")) != std::string::npos && (!isAssembly)) {
116 
118  volName.resize(npos-2);
119  int volCopyNo = touchHist->GetVolume(i)->GetCopyNo();
120  stationName = volName;
121  //stationEta = volCopyNo/100;
122  // bug fix for 14.2.0 to cope with non-mirrored chambers as proposed by
123  // S.Spagnolo (ported from CSCSensitiveDetector.cxx for rel 20.3.3)
124  stationEta = (volCopyNo%1000)/100;
125  stationPhi = abs(volCopyNo%100);
126 
127  } else if ((npos = volName.find("CSC")) != std::string::npos && isAssembly ) {
128  // vol name for Assembly components are
129  // av_WWW_impr_XXX_Muon::BMSxMDTxx_pv_ZZZ_NAME
130  // where WWW is ass. istance nr.
131  // XXX is comp. imprint nr.
132  // BMSxMDTxx is the name of the comp. log.Vol.
133  // x station sub-type; xx technology subtype
134  // ZZZ is No of order of this daugther
135  // NAME is the comp. tag (geoIdentifierTag)
136  // for CSCs it is cl[1] or cl[2]
137  // copy numbers for Ass.components are =
138  // CopyNoBase(= geoIdentifierTag of the assembly) + geoIdentifierTag of the component
139  // geoIdentifierTag of the component = Job
140  // geoIdentifierTag of the assembly = (sideC*10000 +
141  // mirsign*1000 + abs(zi)*100 + fi+1)*100000;
142  // mirsign*1000 + abs(zi)*100 + fi+1)*100000;
143 
145  std::string::size_type loc1,loc2;
146  if ((loc1 = volName.find("Muon::")) != std::string::npos) {
147  stationName = volName.substr(loc1+6,3); //type only
148  }
149 
151  int volCopyNo = touchHist->GetVolume(i)->GetCopyNo();
152  int copyNrBase = int(volCopyNo/100000);
153  int sideC = int(copyNrBase/10000);
154  int zi = int((copyNrBase%1000)/100);
155  // int mirfl = int((copyNrBase%10000)/1000);
156  int fi = int(copyNrBase%100);
157  if (sideC == 1) zi = -zi;
158  stationEta = zi;
159  stationPhi = fi;
160 
161  // now get the geoIdentifierTag of the rpc components
162  int gmID = 0;
163  if ((loc1 = volName.find('[')) != std::string::npos) {
164  if ((loc2 = volName.find(']', loc1+1)) != std::string::npos) {
165  std::istringstream istrvar(volName.substr(loc1+1,loc2-loc1-1));
166  istrvar>>gmID;
167  }
168  }
170  multiLayer = gmID;
171  } else if ((npos = volName.find("component")) != std::string::npos && (!isAssembly)) {
172 
174  multiLayer = touchHist->GetVolume(i)->GetCopyNo();
175  if(multiLayer==3) multiLayer=2; //multilayer index
176  // G4cout << "CSC:::::: multiLayer "<<multiLayer << G4endl;
177  } else if ((npos = volName.find("CscArCO2")) != std::string::npos) {
178 
180  wireLayer=touchHist->GetVolume(i)->GetCopyNo();
181  wireLayer+=1;
182  if(wireLayer==4) wireLayer=1;
183  else if(wireLayer==3) wireLayer=2;
184  else if(wireLayer==2) wireLayer=3;
185  else if(wireLayer==1) wireLayer=4;
186  // G4cout << "CSC:::::: wireLayer "<<wireLayer << G4endl;
187 
189  G4String particle=aStep->GetTrack()->GetDefinition()->GetParticleName();
190  // http://www.slac.stanford.edu/BFROOT/www/Computing/Environment/NewUser/htmlbug/node51.html #GEANT code
191  if (particle=="gamma") lundcode=1; // FIXME was photon, but changed to gamma to match CSCSensitiveDetctor
192  else if (particle=="e+") lundcode=2;
193  else if (particle=="e-") lundcode=3;
194  else if (particle=="mu+") lundcode=5;
195  else if (particle=="mu-") lundcode=6;
196  else if (particle=="pi+") lundcode=8;
197  else if (particle=="pi-") lundcode=9;
198  else if (particle=="kaon+") lundcode=11;
199  else if (particle=="kaon-") lundcode=12;
200  else if (particle=="proton") lundcode=14;
201  else if (particle=="anti_proton") lundcode=15;
202  else if (particle=="sigma+") lundcode=19;
203  else if (particle=="sigma-") lundcode=21;
204  else if (particle=="anti_sigma-") lundcode=27;
205  else if (particle=="anti_sigma+") lundcode=29;
206  else if (particle=="deuteron") lundcode=45;
207  else if (particle=="geantino") lundcode=999;
208  else lundcode=0;
209 
211  globalTime = aStep->GetPreStepPoint()->GetGlobalTime();
212 
214  energyDeposit = aStep->GetTotalEnergyDeposit();
215 
217  const G4AffineTransform transform = touchHist->GetHistory()->GetTopTransform();
218 
220  HitStart = Amg::Hep3VectorToEigen( transform.TransformPoint(startPos) );
221 
224  HitEnd = Amg::Hep3VectorToEigen( transform.TransformPoint(endPos) );
225 
226  }
227  }
228 
229  // START OF COSMICS SPECIFIC CODE
230  m_vertex = Amg::Hep3VectorToEigen( aStep->GetTrack()->GetVertexPosition() );
231  // if the track vertex is far from (0,0,0), takes the tof, otherwise take the "usual" g4 globalTime
232  (((m_vertex.mag() < 100) || ((fabs(globalTime - tOrigin)) < 0.1) ) ? (m_globalTime = globalTime)
233  : (m_globalTime = tof));
234  // if m_globalTime != globalTime and m_globalTime != tof in the output, this is due to multiple hits
235  // before founding the good one (small approximation)
236  // END OF COSMICS SPECIFIC CODE
237 
238  TrackHelper trHelp(aStep->GetTrack());
239 
242  stationEta, multiLayer, wireLayer);
243 
245  m_myCSCHitColl->Emplace(CSCid, m_globalTime, energyDeposit,
246  HitStart, HitEnd, lundcode,
247  trHelp.GenerateParticleLink(), kinEnergy);
248 
249  // #ifndef CSCG4_DEBUG
250  //
251  // // printouts for cosmics
252  // G4cout << "------------------CSC------------------" << G4endl;
253  // G4cout << "Track "<<aStep->GetTrack()->GetTrackID() << G4endl;
254  // G4cout << "Track m_vertex "<<m_vertex[0]<<" " <<m_vertex[1]<<" " <<m_vertex[2] << G4endl;
255 // G4cout << "Global position of the hit " << globVrtx[0] <<" " << globVrtx[1] <<" " << globVrtx[2] << G4endl;
256 // G4cout << "Distance from (0,0,0) and time " << dist << " " <<tOrigin << G4endl;
257 // G4cout << "Momentum "<<m_momMag << G4endl;
258 // G4cout << "Momentum director cosines " <<m_mom[0]<<" " <<m_mom[1]<<" " <<m_mom[2] << G4endl;
259 // G4cout << "Eta and phi "<<m_mom.eta()<<" " <<m_mom.phi() << G4endl;
260 // G4cout << "Closest approach position and distance from (0,0,0) "
261 // << m_globH[0] <<" "<<m_globH[1]<<" "<<m_globH[2]<<" "
262 // << sqrt(m_globH[0]*m_globH[0] + m_globH[1]*m_globH[1] + m_globH[2]*m_globH[2]) << G4endl;
263 // G4cout << "Distance from t0 and tof " << globalDist <<" " <<tof << G4endl;
264 // G4cout << "g4 globalTime " << globalTime << G4endl;
265 // G4cout << "Time " << m_globalTime << G4endl;
266 //
267 // G4cout << "TUB "<<m_muonHelper->GetStationName(CSCid)
268 // << " "<<m_muonHelper->GetFieldValue("PhiSector")
269 // << " "<<m_muonHelper->GetFieldValue("ZSector")
270 // << " "<<m_muonHelper->GetFieldValue("ChamberLayer")
271 // << " "<<m_muonHelper->GetFieldValue("WireLayer") << G4endl;
272 //
273 // G4cout << m_muonHelper->GetStationName(CSCid)<<" "<<newHit->print() << G4endl;
274 //
275 // #endif
276 
277  return true;
278 }
Muon::nsw::STGTPSegments::moduleIDBits::stationPhi
constexpr uint8_t stationPhi
station Phi 1 to 8
Definition: NSWSTGTPDecodeBitmaps.h:129
Trk::ParticleSwitcher::particle
constexpr ParticleHypothesis particle[PARTICLEHYPOTHESES]
the array of masses
Definition: ParticleHypothesis.h:76
CSCSensitiveDetectorCosmics::ProcessHits
G4bool ProcessHits(G4Step *aStep, G4TouchableHistory *ROhist) override final
Definition: CSCSensitiveDetectorCosmics.cxx:41
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
ZDC::sideC
BySideTypeMod sideC(-1)
dumpTgcDigiDeadChambers.stationName
dictionary stationName
Definition: dumpTgcDigiDeadChambers.py:30
CSCSensitiveDetectorCosmics::m_mom
Amg::Vector3D m_mom
Definition: CSCSensitiveDetectorCosmics.h:79
TrackHelper.h
CSCSensitiveDetectorCosmics::m_myCSCHitColl
SG::WriteHandle< CSCSimHitCollection > m_myCSCHitColl
member data
Definition: CSCSensitiveDetectorCosmics.h:87
Trk::loc2
@ loc2
generic first and second local coordinate
Definition: ParamDefs.h:41
CSCSensitiveDetectorCosmics::m_currVertex
Amg::Vector3D m_currVertex
Definition: CSCSensitiveDetectorCosmics.h:82
Trk::energyDeposit
@ energyDeposit
Definition: MeasurementType.h:32
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
CscHitIdHelper::GetHelper
static const CscHitIdHelper * GetHelper()
Definition: CscHitIdHelper.cxx:23
CscHitIdHelper::BuildCscHitId
int BuildCscHitId(const std::string &, const int, const int, const int, const int) const
Definition: CscHitIdHelper.cxx:89
lumiFormat.i
int i
Definition: lumiFormat.py:92
Amg::transform
Amg::Vector3D transform(Amg::Vector3D &v, Amg::Transform3D &tr)
Transform a point from a Trasformation3D.
Definition: GeoPrimitivesHelpers.h:156
CSCSensitiveDetectorCosmics::CSCSensitiveDetectorCosmics
CSCSensitiveDetectorCosmics(const std::string &name, const std::string &hitCollectionName)
construction/destruction
Definition: CSCSensitiveDetectorCosmics.cxx:20
CSCSensitiveDetectorCosmics::m_globalTime
double m_globalTime
Definition: CSCSensitiveDetectorCosmics.h:84
CSCSensitiveDetectorCosmics::m_muonHelper
const CscHitIdHelper * m_muonHelper
Definition: CSCSensitiveDetectorCosmics.h:88
CSCSensitiveDetectorCosmics::m_momMag
double m_momMag
Definition: CSCSensitiveDetectorCosmics.h:80
CscHitIdHelper.h
CLHEPtoEigenConverter.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
Amg::Vector3D
Eigen::Matrix< double, 3, 1 > Vector3D
Definition: GeoPrimitives.h:47
CSCSensitiveDetectorCosmics.h
checkFileSG.fi
fi
Definition: checkFileSG.py:65
TrackHelper::GenerateParticleLink
HepMcParticleLink GenerateParticleLink()
Generates a creates new HepMcParticleLink object on the stack based on GetUniqueID(),...
Definition: TrackHelper.h:35
Muon::nsw::STGTPSegments::moduleIDBits::stationEta
constexpr uint8_t stationEta
1 to 3
Definition: NSWSTGTPDecodeBitmaps.h:127
HitID
int HitID
Definition: GenericMuonSimHit.h:13
Trk::loc1
@ loc1
Definition: ParamDefs.h:40
sTgcDigitEffiDump.multiLayer
int multiLayer
Definition: sTgcDigitEffiDump.py:36
CSCSensitiveDetectorCosmics::m_globH
Amg::Vector3D m_globH
Definition: CSCSensitiveDetectorCosmics.h:83
CSCSensitiveDetectorCosmics::Initialize
void Initialize(G4HCofThisEvent *HCE) override final
member functions
Definition: CSCSensitiveDetectorCosmics.cxx:32
CSCSensitiveDetectorCosmics::m_vertex
Amg::Vector3D m_vertex
Definition: CSCSensitiveDetectorCosmics.h:81