ATLAS Offline Software
MicromegasSensitiveDetector.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
7 #include "MCTruth/TrackHelper.h"
8 #include "G4Geantino.hh"
9 #include "G4ChargedGeantino.hh"
10 
11 #include "G4Track.hh"
12 
14 
15 #include <string>
16 
17 // construction/destruction
18 MicromegasSensitiveDetector::MicromegasSensitiveDetector(const std::string& name, const std::string& hitCollectionName)
19  : G4VSensitiveDetector( name )
20  , m_MMSimHitCollection( hitCollectionName )
21 {
23  //m_muonHelper->PrintFields();
24 }
25 
26 // Implemenation of memebr functions
28 {
29  if (!m_MMSimHitCollection.isValid()) m_MMSimHitCollection = std::make_unique<MMSimHitCollection>();
30 }
31 
32 G4bool MicromegasSensitiveDetector::ProcessHits(G4Step* aStep,G4TouchableHistory* /*ROHist*/)
33 {
34  G4Track* currentTrack = aStep->GetTrack();
35  int charge=currentTrack->GetDefinition()->GetPDGCharge();
36  bool geantinoHit = (currentTrack->GetDefinition()==G4Geantino::GeantinoDefinition()) ||
37  (currentTrack->GetDefinition()==G4ChargedGeantino::ChargedGeantinoDefinition());
38 
39  if (!charge && (!geantinoHit)) return false;
40  // G4cout << "\t\t MicromegasSD: Hit in a sensitive layer!!!!! " << G4endl;
41  G4StepPoint* postStep=aStep->GetPostStepPoint();
42  const G4Step* post_Step=aStep->GetTrack()->GetStep();
43 
44  Amg::Vector3D position = Amg::Hep3VectorToEigen( postStep->GetPosition() );
45 
46  int pdgCode=currentTrack->GetDefinition()->GetPDGEncoding();
47 
48  float globalTime=postStep->GetGlobalTime();
49  float eKin=postStep->GetKineticEnergy();
50  if (eKin<= 0. && (!geantinoHit)) return false;
51 
52  Amg::Vector3D direction = Amg::Hep3VectorToEigen( postStep->GetMomentumDirection() );
53  float depositEnergy=post_Step->GetTotalEnergyDeposit();
54 
55  if (depositEnergy<0.0001 && (!geantinoHit)) return false;
56 
57  const G4TouchableHistory* touchHist = static_cast<const G4TouchableHistory*>(aStep->GetPreStepPoint()->GetTouchable());
58 
59  // int iDepth=touchHist->GetHistoryDepth();
60  // G4cout << "\t\t\t\t Touchable history dump " << G4endl;
61  int nLayer=touchHist->GetVolume(0)->GetCopyNo();
62  std::string chName=touchHist->GetVolume(1)->GetLogicalVolume()->GetName();
63  std::string subType=chName.substr(chName.find('-')+1);
64  if (subType[0]!='M') G4cout << " something is wrong, this is no Micromegas!" << G4endl;
65  std::string temp(&subType[1]);
66  std::istringstream is(temp);
67  int iRing;
68  is>>iRing;
69  // identifiers have eta naming 0-1, eta encoded in subtype is 1-2
70  iRing--;
71  // double phiDiff=2*M_PI;
72 
73  G4ThreeVector posH=postStep->GetPosition(); //posH is equivalent to position - eigen not used to avoid additional dependence on EventPrimitives
74  if (subType[2]=='L') posH.rotateZ(M_PI/8.);
75  double phiHit=posH.phi();
76  if(phiHit<=0) phiHit+=2.*M_PI;
77  int iPhi=1+int(phiHit/(M_PI/4.));
78  iPhi*=2;
79  if (subType[2]=='L') iPhi-=1;
80 
81  int iSide=1;
82  if (position.z()<0) iSide=-1;
83 
84  int mLayer= atoi((subType.substr(3,1)).c_str());
85  if (mLayer != 1 && mLayer !=2) G4cout << " something is wrong - multilayer index is " << mLayer << G4endl;
86 
87  // G4cout << "\t\t Chamber "<<chName<<" subType "<<subType<<" layer nr. "<<nLayer<<" ring "<<iRing<<" sector "<<iPhi<<" side "<<iSide << G4endl;
88  int MmId = m_muonHelper->BuildMicromegasHitId(subType, iPhi, iRing, mLayer,nLayer, iSide);
89 
90  TrackHelper trHelp(aStep->GetTrack());
91 
92  m_MMSimHitCollection->Emplace(MmId, globalTime,position,pdgCode,eKin,direction,depositEnergy,trHelp.GenerateParticleLink());
93 
94  // G4cout << "MMs "<<m_muonHelper->GetStationName(MmId)
95  // << " "<<m_muonHelper->GetFieldValue("PhiSector")
96  // << " "<<m_muonHelper->GetFieldValue("ZSector")
97  // << " "<<m_muonHelper->GetFieldValue("MultiLayer")
98  // << " "<<m_muonHelper->GetFieldValue("Layer")
99  // << " "<<m_muonHelper->GetFieldValue("Side") << G4endl;
100 
101  //G4cout << m_muonHelper->GetStationName(MmId)<<" "<<aHit->print() << G4endl;
102  // G4cout << aHit->print() << G4endl;
103 
104  return true;
105 }
106 
MicromegasSensitiveDetector::MicromegasSensitiveDetector
MicromegasSensitiveDetector(const std::string &name, const std::string &hitCollectionName)
construction/destruction
Definition: MicromegasSensitiveDetector.cxx:18
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
MicromegasHitIdHelper.h
TrackHelper.h
M_PI
#define M_PI
Definition: ActiveFraction.h:11
MicromegasSensitiveDetector::ProcessHits
G4bool ProcessHits(G4Step *aStep, G4TouchableHistory *ROhist) override final
Definition: MicromegasSensitiveDetector.cxx:32
MicromegasSensitiveDetector::m_muonHelper
const MicromegasHitIdHelper * m_muonHelper
Definition: MicromegasSensitiveDetector.h:31
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
MicromegasSensitiveDetector::Initialize
void Initialize(G4HCofThisEvent *HCE) override final
member functions
Definition: MicromegasSensitiveDetector.cxx:27
CLHEPtoEigenConverter.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
charge
double charge(const T &p)
Definition: AtlasPID.h:494
Amg::Vector3D
Eigen::Matrix< double, 3, 1 > Vector3D
Definition: GeoPrimitives.h:47
MicromegasHitIdHelper::GetHelper
static const MicromegasHitIdHelper * GetHelper()
Definition: MicromegasHitIdHelper.cxx:25
Trk::iPhi
@ iPhi
Definition: ParamDefs.h:53
MicromegasHitIdHelper::BuildMicromegasHitId
int BuildMicromegasHitId(const std::string &, const int, const int, const int, const int, const int) const
Definition: MicromegasHitIdHelper.cxx:101
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
TrackHelper::GenerateParticleLink
HepMcParticleLink GenerateParticleLink()
Generates a creates new HepMcParticleLink object on the stack based on GetUniqueID(),...
Definition: TrackHelper.h:35
MicromegasSensitiveDetector.h
MicromegasSensitiveDetector::m_MMSimHitCollection
SG::WriteHandle< MMSimHitCollection > m_MMSimHitCollection
Definition: MicromegasSensitiveDetector.h:30