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;
34  }
35  edm::LogVerbatim("SensitiveDetector") << " <" << iname << "> : Assigns SD to LVs " << ss.str();
36 }
37 
39 
40 void SensitiveDetector::Initialize(G4HCofThisEvent* eventHC) {}
41 
42 void SensitiveDetector::EndOfEvent(G4HCofThisEvent* eventHC) {}
43 
45  G4LogicalVolumeStore* theStore = G4LogicalVolumeStore::GetInstance();
46  for (auto& lv : *theStore) {
47  if (vname == lv->GetName()) {
48  lv->SetSensitiveDetector(this);
49  }
50  }
51 }
52 
54  const G4StepPoint* preStepPoint = step->GetPreStepPoint();
55  const G4ThreeVector& globalCoordinates = preStepPoint->GetPosition();
56  if (cd == WorldCoordinates) {
57  return ConvertToLocal3DPoint(globalCoordinates);
58  }
59  const G4TouchableHistory* theTouchable = static_cast<const G4TouchableHistory*>(preStepPoint->GetTouchable());
60  const G4ThreeVector localCoordinates =
61  theTouchable->GetHistory()->GetTopTransform().TransformPoint(globalCoordinates);
62  return ConvertToLocal3DPoint(localCoordinates);
63 }
64 
66  const G4StepPoint* postStepPoint = step->GetPostStepPoint();
67  const G4ThreeVector& globalCoordinates = postStepPoint->GetPosition();
68  if (cd == WorldCoordinates) {
69  return ConvertToLocal3DPoint(globalCoordinates);
70  }
71  const G4StepPoint* preStepPoint = step->GetPreStepPoint();
72  const G4ThreeVector localCoordinates =
73  preStepPoint->GetTouchable()->GetHistory()->GetTopTransform().TransformPoint(globalCoordinates);
74  return ConvertToLocal3DPoint(localCoordinates);
75 }
76 
78  const G4StepPoint* preStepPoint = step->GetPreStepPoint();
79  G4ThreeVector localCoordinates =
80  preStepPoint->GetTouchable()->GetHistory()->GetTopTransform().TransformPoint(preStepPoint->GetPosition());
81  return ConvertToLocal3DPoint(localCoordinates);
82 }
83 
85  const G4ThreeVector& globalCoordinates = step->GetPostStepPoint()->GetPosition();
86  G4ThreeVector localCoordinates =
87  step->GetPreStepPoint()->GetTouchable()->GetHistory()->GetTopTransform().TransformPoint(globalCoordinates);
88  return ConvertToLocal3DPoint(localCoordinates);
89 }
90 
92  TrackInformation* info = (TrackInformation*)(aTrack->GetUserInformation());
93  if (nullptr == info) {
94  edm::LogWarning("SensitiveDetector") << " no TrackInformation available for trackID= " << aTrack->GetTrackID()
95  << " inside SD " << GetName();
96  G4Exception(
97  "SensitiveDetector::cmsTrackInformation()", "sd01", FatalException, "cannot handle hits without trackinfo");
98  }
99  return info;
100 }
101 
102 void SensitiveDetector::setNames(const std::vector<std::string>& hnames) {
103  m_namesOfSD.clear();
104  m_namesOfSD = hnames;
105 }
106 
107 void SensitiveDetector::NaNTrap(const G4Step* aStep) const {
108  G4Track* currentTrk = aStep->GetTrack();
109  double ekin = currentTrk->GetKineticEnergy();
110  if (ekin < 0.0) {
111  const G4VPhysicalVolume* pCurrentVol = aStep->GetPreStepPoint()->GetPhysicalVolume();
112  edm::LogWarning("SensitiveDetector") << "Negative kinetic energy Ekin(MeV)=" << ekin / CLHEP::MeV << " of "
113  << currentTrk->GetDefinition()->GetParticleName()
114  << " trackID= " << currentTrk->GetTrackID() << " inside "
115  << pCurrentVol->GetName();
116  currentTrk->SetKineticEnergy(0.0);
117  }
118  const G4ThreeVector& currentPos = currentTrk->GetPosition();
119  double xyz = currentPos.x() + currentPos.y() + currentPos.z();
120  const G4ThreeVector& currentMom = currentTrk->GetMomentum();
121  xyz += currentMom.x() + currentMom.y() + currentMom.z();
122 
123  if (edm::isNotFinite(xyz)) {
124  const G4VPhysicalVolume* pCurrentVol = aStep->GetPreStepPoint()->GetPhysicalVolume();
125  edm::LogWarning("SensitiveDetector") << "NaN detected for trackID= " << currentTrk->GetTrackID() << " inside "
126  << pCurrentVol->GetName();
127  G4Exception("SensitiveDetector::NaNTrap()", "sd01", FatalException, "corrupted event or step");
128  }
129 }
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