Skip to content
Snippets Groups Projects
Commit a2e8ed16 authored by lintao@ihep.ac.cn's avatar lintao@ihep.ac.cn
Browse files

In order to get hits, need to construct SD first.

parent 9274d3cb
No related branches found
No related tags found
No related merge requests found
...@@ -66,5 +66,6 @@ DetectorConstruction::Construct() { ...@@ -66,5 +66,6 @@ DetectorConstruction::Construct() {
void void
DetectorConstruction::ConstructSDandField() { DetectorConstruction::ConstructSDandField() {
// Each detelem is responsible for associating SD/Field and its volumes.
m_root_detelem->ConstructSDandField();
} }
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include "G4OpticalSurface.hh" #include "G4OpticalSurface.hh"
#include "DD4hep/Detector.h" #include "DD4hep/Detector.h"
#include "DD4hep/Plugins.h"
#include "DDG4/Geant4Converter.h" #include "DDG4/Geant4Converter.h"
#include "DDG4/Geant4Mapping.h" #include "DDG4/Geant4Mapping.h"
...@@ -62,6 +63,63 @@ AnExampleDetElemTool::getLV() { ...@@ -62,6 +63,63 @@ AnExampleDetElemTool::getLV() {
return logicAnExample; return logicAnExample;
} }
void
AnExampleDetElemTool::ConstructSDandField() {
//
// Construct SD using DD4hep.
// Refer to FCCSW/Detector/DetComponents/src/
//
typedef std::set<const TGeoVolume*> VolSet;
typedef std::map<dd4hep::SensitiveDetector, VolSet> _SV;
dd4hep::sim::Geant4GeometryInfo* p = dd4hep::sim::Geant4Mapping::instance().ptr();
_SV& vols = p->sensitives;
auto lcdd = &(dd4hep::Detector::getInstance());
for (_SV::const_iterator iv = vols.begin(); iv != vols.end(); ++iv) {
dd4hep::SensitiveDetector sd = (*iv).first;
std::string typ = sd.type(), nam = sd.name();
info() << "Type/Name: "
<< typ << "/" << nam
<< endmsg;
// continue;
// Sensitive detectors are deleted in ~G4SDManager
G4VSensitiveDetector* g4sd = dd4hep::PluginService::Create<G4VSensitiveDetector*>(typ, nam, lcdd);
if (g4sd == nullptr) {
std::string tmp = typ;
// tmp[0] = ::toupper(tmp[0]);
typ = "Geant4" + tmp;
g4sd = dd4hep::PluginService::Create<G4VSensitiveDetector*>(typ, nam, lcdd);
if (g4sd == nullptr) {
dd4hep::PluginDebug dbg;
g4sd = dd4hep::PluginService::Create<G4VSensitiveDetector*>(typ, nam, lcdd);
if (g4sd == nullptr) {
throw std::runtime_error("ConstructSDandField: FATAL Failed to "
"create Geant4 sensitive detector " +
nam + " of type " + typ + ".");
}
}
}
g4sd->Activate(true);
G4SDManager::GetSDMpointer()->AddNewDetector(g4sd);
const VolSet& sens_vols = (*iv).second;
for (VolSet::const_iterator i = sens_vols.begin(); i != sens_vols.end(); ++i) {
const TGeoVolume* vol = *i;
G4LogicalVolume* g4v = p->g4Volumes[vol];
if (g4v == nullptr) {
throw std::runtime_error("ConstructSDandField: Failed to access G4LogicalVolume for SD " + nam + " of type " +
typ + ".");
}
G4SDManager::GetSDMpointer()->AddNewDetector(g4sd);
g4v->SetSensitiveDetector(g4sd);
}
}
}
StatusCode StatusCode
AnExampleDetElemTool::initialize() { AnExampleDetElemTool::initialize() {
StatusCode sc; StatusCode sc;
......
...@@ -14,6 +14,7 @@ public: ...@@ -14,6 +14,7 @@ public:
using extends::extends; using extends::extends;
G4LogicalVolume* getLV() override; G4LogicalVolume* getLV() override;
void ConstructSDandField() override;
StatusCode initialize() override; StatusCode initialize() override;
StatusCode finalize() override; StatusCode finalize() override;
......
...@@ -50,6 +50,14 @@ WorldDetElemTool::getLV() { ...@@ -50,6 +50,14 @@ WorldDetElemTool::getLV() {
return logicWorld; return logicWorld;
} }
void
WorldDetElemTool::ConstructSDandField() {
// After the whole detector volumes are constructed,
// now start the construction of SD.
ToolHandle<IDetElemTool> inner_detelem_tool("AnExampleDetElemTool");
inner_detelem_tool->ConstructSDandField();
}
StatusCode StatusCode
WorldDetElemTool::initialize() { WorldDetElemTool::initialize() {
StatusCode sc; StatusCode sc;
......
...@@ -10,6 +10,7 @@ public: ...@@ -10,6 +10,7 @@ public:
using extends::extends; using extends::extends;
G4LogicalVolume* getLV() override; G4LogicalVolume* getLV() override;
void ConstructSDandField() override;
StatusCode initialize() override; StatusCode initialize() override;
StatusCode finalize() override; StatusCode finalize() override;
......
...@@ -19,7 +19,7 @@ public: ...@@ -19,7 +19,7 @@ public:
// return the constructed detector // return the constructed detector
virtual G4LogicalVolume* getLV() = 0; virtual G4LogicalVolume* getLV() = 0;
virtual void ConstructSDandField() {}
}; };
#endif #endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment