CMS 3D CMS Logo

SensitiveDetector.cc
Go to the documentation of this file.
2 
5 
6 #include "G4SDManager.hh"
7 #include "G4Step.hh"
8 #include "G4Track.hh"
9 #include "G4StepPoint.hh"
10 #include "G4Transform3D.hh"
11 #include "G4LogicalVolumeStore.hh"
12 #include "G4TouchableHistory.hh"
13 #include "G4VUserTrackInformation.hh"
14 
15 #include <sstream>
16 
18  : G4VSensitiveDetector(iname), m_isCalo(calo) {
19  // for CMS hits
20  m_namesOfSD.push_back(iname);
21 
22  // Geant4 hit collection
23  collectionName.insert(iname);
24 
25  // register sensitive detector
26  G4SDManager* SDman = G4SDManager::GetSDMpointer();
27  SDman->AddNewDetector(this);
28 
29  const std::vector<std::string_view>& lvNames = clg.logicalNames(iname);
30  std::stringstream ss;
31  for (auto& lvname : lvNames) {
32  this->AssignSD({lvname.data(), lvname.size()});
33  ss << " " << lvname << "\n";
34  }
35  edm::LogVerbatim("SensitiveDetector") << " <" << iname << "> : Assigns SD to " << lvNames.size() << " LVs "
36  << ss.str();
37 }
38 
40 
41 void SensitiveDetector::Initialize(G4HCofThisEvent* eventHC) {}
42 
43 void SensitiveDetector::EndOfEvent(G4HCofThisEvent* eventHC) {}
44 
46  G4LogicalVolumeStore* theStore = G4LogicalVolumeStore::GetInstance();
47  for (auto& lv : *theStore) {
48  if (vname == lv->GetName()) {
49  lv->SetSensitiveDetector(this);
50  }
51  }
52 }
53 
55  const G4StepPoint* preStepPoint = step->GetPreStepPoint();
56  const G4ThreeVector& globalCoordinates = preStepPoint->GetPosition();
57  if (cd == WorldCoordinates) {
58  return ConvertToLocal3DPoint(globalCoordinates);
59  }
60  const G4TouchableHistory* theTouchable = static_cast<const G4TouchableHistory*>(preStepPoint->GetTouchable());
61  const G4ThreeVector localCoordinates =
62  theTouchable->GetHistory()->GetTopTransform().TransformPoint(globalCoordinates);
63  return ConvertToLocal3DPoint(localCoordinates);
64 }
65 
67  const G4StepPoint* postStepPoint = step->GetPostStepPoint();
68  const G4ThreeVector& globalCoordinates = postStepPoint->GetPosition();
69  if (cd == WorldCoordinates) {
70  return ConvertToLocal3DPoint(globalCoordinates);
71  }
72  const G4StepPoint* preStepPoint = step->GetPreStepPoint();
73  const G4ThreeVector localCoordinates =
74  preStepPoint->GetTouchable()->GetHistory()->GetTopTransform().TransformPoint(globalCoordinates);
75  return ConvertToLocal3DPoint(localCoordinates);
76 }
77 
79  const G4StepPoint* preStepPoint = step->GetPreStepPoint();
80  G4ThreeVector localCoordinates =
81  preStepPoint->GetTouchable()->GetHistory()->GetTopTransform().TransformPoint(preStepPoint->GetPosition());
82  return ConvertToLocal3DPoint(localCoordinates);
83 }
84 
86  const G4ThreeVector& globalCoordinates = step->GetPostStepPoint()->GetPosition();
87  G4ThreeVector localCoordinates =
88  step->GetPreStepPoint()->GetTouchable()->GetHistory()->GetTopTransform().TransformPoint(globalCoordinates);
89  return ConvertToLocal3DPoint(localCoordinates);
90 }
91 
93  TrackInformation* info = (TrackInformation*)(aTrack->GetUserInformation());
94  if (nullptr == info) {
95  edm::LogWarning("SensitiveDetector") << " no TrackInformation available for trackID= " << aTrack->GetTrackID()
96  << " inside SD " << GetName();
97  G4Exception(
98  "SensitiveDetector::cmsTrackInformation()", "sd01", FatalException, "cannot handle hits without trackinfo");
99  }
100  return info;
101 }
102 
103 void SensitiveDetector::setNames(const std::vector<std::string>& hnames) {
104  m_namesOfSD.clear();
105  m_namesOfSD = hnames;
106 }
107 
108 void SensitiveDetector::NaNTrap(const G4Step* aStep) const {
109  G4Track* currentTrk = aStep->GetTrack();
110  double ekin = currentTrk->GetKineticEnergy();
111  if (ekin < 0.0) {
112  const G4VPhysicalVolume* pCurrentVol = aStep->GetPreStepPoint()->GetPhysicalVolume();
113  edm::LogWarning("SensitiveDetector") << "Negative kinetic energy Ekin(MeV)=" << ekin / CLHEP::MeV << " of "
114  << currentTrk->GetDefinition()->GetParticleName()
115  << " trackID= " << currentTrk->GetTrackID() << " inside "
116  << pCurrentVol->GetName();
117  currentTrk->SetKineticEnergy(0.0);
118  }
119  const G4ThreeVector& currentPos = currentTrk->GetPosition();
120  double xyz = currentPos.x() + currentPos.y() + currentPos.z();
121  const G4ThreeVector& currentMom = currentTrk->GetMomentum();
122  xyz += currentMom.x() + currentMom.y() + currentMom.z();
123 
124  if (edm::isNotFinite(xyz)) {
125  const G4VPhysicalVolume* pCurrentVol = aStep->GetPreStepPoint()->GetPhysicalVolume();
126  edm::LogWarning("SensitiveDetector") << "NaN detected for trackID= " << currentTrk->GetTrackID() << " inside "
127  << pCurrentVol->GetName();
128  G4Exception("SensitiveDetector::NaNTrap()", "sd01", FatalException, "corrupted event or step");
129  }
130 }
Log< level::Info, true > LogVerbatim
static const TGPicture * info(bool iBackgroundIsBlack)
~SensitiveDetector() override
constexpr bool isNotFinite(T x)
Definition: isFinite.h:9
SensitiveDetector(const std::string &iname, const SensitiveDetectorCatalog &, bool calo)
const std::vector< std::string_view > logicalNames(const std::string &readoutName) const
void AssignSD(const std::string &vname)
Local3DPoint FinalStepPosition(const G4Step *step, coordinates) const
std::vector< std::string > m_namesOfSD
Local3DPoint ConvertToLocal3DPoint(const G4ThreeVector &point) const
Local3DPoint InitialStepPosition(const G4Step *step, coordinates) const
TrackInformation * cmsTrackInformation(const G4Track *aTrack)
Local3DPoint LocalPreStepPosition(const G4Step *step) const
void EndOfEvent(G4HCofThisEvent *eventHC) override
void setNames(const std::vector< std::string > &)
Local3DPoint LocalPostStepPosition(const G4Step *step) const
step
Definition: StallMonitor.cc:98
Log< level::Warning, false > LogWarning
void Initialize(G4HCofThisEvent *eventHC) override
void NaNTrap(const G4Step *step) const
Definition: Common.h:9