diff --git a/DDCore/src/LCDDImp.cpp b/DDCore/src/LCDDImp.cpp index 40cae2fe8df58ce2c5b373a5c109101d9102962f..29ccadf5884c17796b70f487b42b05ecb0e56655 100644 --- a/DDCore/src/LCDDImp.cpp +++ b/DDCore/src/LCDDImp.cpp @@ -267,6 +267,7 @@ void LCDDImp::init() { if ( !m_world.isValid() ) { TGeoManager* mgr = m_manager; Box worldSolid("world_box","world_x","world_y","world_z"); + std::cout << " *********** created World volume with size : " << worldSolid->GetDX() << ", " << worldSolid->GetDY() << ", " << worldSolid->GetDZ() << std::endl ; Material vacuum = material("Vacuum"); Material air = material("Air"); Volume world("world_volume",worldSolid,air); diff --git a/DDG4/include/DDG4/Factories.h b/DDG4/include/DDG4/Factories.h index 3fce032e6bb87d5714be2321b29d155d60e75e14..7757be89d820fe4797b7edb3c861044a748e447d 100644 --- a/DDG4/include/DDG4/Factories.h +++ b/DDG4/include/DDG4/Factories.h @@ -32,6 +32,7 @@ namespace DD4hep { namespace Simulation { class Geant4Converter; class Geant4SensitiveDetector; + class G4SDFactory; template <typename T> class Geant4SetupAction { public: static long create(Geometry::LCDD& lcdd, const Geant4Converter& cnv, const std::map<std::string,std::string>& attrs); @@ -55,6 +56,13 @@ namespace { } }; + /// Factory to create Geant4 sensitive detectors + template <typename P> class Factory<P, DD4hep::Simulation::G4SDFactory*()> { + public: typedef DD4hep::Simulation::G4SDFactory SD; + static void Func(void *retaddr, void*, const std::vector<void*>& arg, void*) + { *(SD**)retaddr = (SD*)new P(); } + }; + /// Factory to create Geant4 sensitive detectors template <typename P> class Factory<P, DD4hep::Simulation::Geant4SensitiveDetector*(std::string,DD4hep::Geometry::LCDD*)> { public: typedef DD4hep::Simulation::Geant4SensitiveDetector SD; @@ -87,9 +95,20 @@ namespace { }; } +// Plugin definition to create Geant4 sensitive detectors +#define DECLARE_G4SDFACTORY(name) namespace DD4hep { namespace Simulation { }} using DD4hep::Simulation::name; \ + PLUGINSVC_FACTORY_WITH_ID(name,std::string(#name),DD4hep::Simulation::G4SDFactory*()) + +// // /// Plugin definition to create Geant4 sensitive detectors +// // #define DECLARE_GEANT4SENSITIVEDETECTOR(name) namespace DD4hep { namespace Simulation { }} using DD4hep::Simulation::name; \ +// // typedef DD4hep::Simulation::SimpleG4SDFactory<name> SDF##name ; \ +// // PLUGINSVC_FACTORY_WITH_ID(SDF##name,std::string(#name),SDF##name*()) + + /// Plugin definition to create Geant4 sensitive detectors #define DECLARE_GEANT4SENSITIVEDETECTOR(name) namespace DD4hep { namespace Simulation { }} using DD4hep::Simulation::name; \ - PLUGINSVC_FACTORY_WITH_ID(name,std::string(#name),DD4hep::Simulation::Geant4SensitiveDetector*(std::string,DD4hep::Geometry::LCDD*)) + PLUGINSVC_FACTORY_WITH_ID(name,std::string(#name),DD4hep::Simulation::G4SDFactory*()) + // PLUGINSVC_FACTORY_WITH_ID(name,std::string(#name),DD4hep::Simulation::Geant4SensitiveDetector*(std::string,DD4hep::Geometry::LCDD*)) /// Plugin definition to create Geant4 stpper objects #define DECLARE_GEANT4_STEPPER(name) PLUGINSVC_FACTORY_WITH_ID(G4##name,std::string(#name),G4MagIntegratorStepper*(G4EquationOfMotion*)) diff --git a/DDG4/include/DDG4/Geant4GeometryInfo.h b/DDG4/include/DDG4/Geant4GeometryInfo.h index 01f248497d805ebffaa3ac98dccf4b88aeb4d5ea..b842d27b085b01e74aae4f5ec1f7b59cc93e0ed7 100644 --- a/DDG4/include/DDG4/Geant4GeometryInfo.h +++ b/DDG4/include/DDG4/Geant4GeometryInfo.h @@ -33,6 +33,7 @@ class G4UserLimits; class G4VisAttributes; class G4VPhysicalVolume; class G4AssemblyVolume; +class G4VSensitiveDetector; /* * DD4hep namespace declaration @@ -46,7 +47,6 @@ namespace DD4hep { // Forward declarations class Geant4Mapping; - class Geant4SensitiveDetector; struct Geant4GeometryInfo : public TNamed, public Geometry::GeoHandlerTypes::GeometryInfo { public: @@ -58,7 +58,7 @@ namespace DD4hep { typedef std::map<const TGeoNode*, G4VPhysicalVolume*> PlacementMap; typedef std::map<const G4AssemblyVolume*,AssemblyChildren> AssemblyChildMap; typedef std::map<const TNamed*, G4Region*> RegionMap; - typedef std::map<const TNamed*, Geant4SensitiveDetector*> SensDetMap; + typedef std::map<const TNamed*, G4VSensitiveDetector*> SensDetMap; typedef std::map<const TGeoVolume*, G4LogicalVolume*> VolumeMap; typedef std::map<const TGeoShape*, G4VSolid*> SolidMap; typedef std::map<const TNamed*, G4VisAttributes*> VisMap; diff --git a/DDG4/include/DDG4/Geant4SensitiveDetector.h b/DDG4/include/DDG4/Geant4SensitiveDetector.h index ef91e5dbb2a730359e68b090deea7dac8c555992..f0ae7ccbccfdbdd4fa175d8fce534692209268ee 100644 --- a/DDG4/include/DDG4/Geant4SensitiveDetector.h +++ b/DDG4/include/DDG4/Geant4SensitiveDetector.h @@ -12,6 +12,7 @@ // Framework include files #include "DD4hep/LCDD.h" #include "DDG4/Geant4Hits.h" +#include "DDG4/G4SDFactory.h" // Geant 4 include files #include "G4Step.hh" @@ -37,6 +38,7 @@ namespace DD4hep { class Geant4SensitiveDetector; /** @class Geant4SensitiveDetector Geant4SensitiveDetector.h DDG4/Geant4SensitiveDetector.h + * Example G4VSensitiveDetector implementation. * * @author M.Frank * @version 1.0 diff --git a/DDG4/src/Geant4CalorimeterSD.cpp b/DDG4/src/Geant4CalorimeterSD.cpp index a769bc85dd9c18e00d1c91f94af182b842112568..a6edbda5f5a294bc71ccef0d608bf99abd1f9417 100644 --- a/DDG4/src/Geant4CalorimeterSD.cpp +++ b/DDG4/src/Geant4CalorimeterSD.cpp @@ -49,7 +49,8 @@ namespace DD4hep { namespace Simulation { return true; } - typedef Geant4GenericSD<Calorimeter> Geant4Calorimeter; + //typedef Geant4GenericSD<Calorimeter> Geant4Calorimeter; + typedef SimpleG4SDFactory< Geant4GenericSD<Calorimeter> > Geant4Calorimeter; }} // End namespace DD4hep::Simulation DECLARE_GEANT4SENSITIVEDETECTOR(Geant4Calorimeter) @@ -104,7 +105,8 @@ namespace DD4hep { namespace Simulation { } } }; - typedef Geant4GenericSD<OpticalCalorimeter> Geant4OpticalCalorimeter; + //typedef Geant4GenericSD<OpticalCalorimeter> Geant4OpticalCalorimeter; + typedef SimpleG4SDFactory< Geant4GenericSD<OpticalCalorimeter> > Geant4OpticalCalorimeter; }} // End namespace DD4hep::Simulation DECLARE_GEANT4SENSITIVEDETECTOR(Geant4OpticalCalorimeter) diff --git a/DDG4/src/Geant4Converter.cpp b/DDG4/src/Geant4Converter.cpp index 85e08cf0dcf797958095845a711049ece9c182eb..003c426abb14797df25a26564d7465ff3dd60306 100644 --- a/DDG4/src/Geant4Converter.cpp +++ b/DDG4/src/Geant4Converter.cpp @@ -12,6 +12,7 @@ #include "DD4hep/Printout.h" #include "DDG4/Geant4Field.h" #include "DDG4/Geant4Converter.h" +#include "DDG4/G4SDFactory.h" #include "DDG4/Geant4SensitiveDetector.h" // ROOT includes @@ -464,7 +465,7 @@ void* Geant4Converter::handleVolume(const string& name, const TGeoVolume* volume bool assembly = s->IsA() == TGeoShapeAssembly::Class(); SensitiveDetector det = _v.sensitiveDetector(); - Geant4SensitiveDetector* sd = 0; + G4VSensitiveDetector* sd = 0; if ( det.isValid() ) { sd = info.g4SensDets[det.ptr()]; if ( !sd ) { @@ -703,27 +704,73 @@ void* Geant4Converter::handleLimitSet(const TNamed* limitset, const set<const TG return g4; } +// /// Convert the geometry type SensitiveDetector into the corresponding Geant4 object(s). +// void* Geant4Converter::handleSensitive(const TNamed* sens_det, const set<const TGeoVolume*>& /* volumes */) const { +// Geant4GeometryInfo& info = data(); +// G4VSensitiveDetector* g4 = info.g4SensDets[sens_det]; +// if ( !g4 ) { +// SensitiveDetector sd = Ref_t(sens_det); +// string type = sd.type(), name = sd.name(); + +// g4 = ROOT::Reflex::PluginService::Create<G4VSensitiveDetector*>(type,name,&m_lcdd); +// if ( !g4 ) { +// string tmp = type; +// tmp[0] = ::toupper(tmp[0]); +// type = "Geant4"+tmp; +// g4 = ROOT::Reflex::PluginService::Create<G4VSensitiveDetector*>(type,name,&m_lcdd); +// if ( !g4 ) { +// throw runtime_error("Geant4Converter<SensitiveDetector>: FATAL Failed to " +// "create Geant4 sensitive detector "+name+" of type "+type+"."); +// } +// } +// g4->Activate(true); +// g4->defineCollection(sd.hitsCollection()); +// G4SDManager::GetSDMpointer()->AddNewDetector(g4); +// info.g4SensDets[sens_det] = g4; +// } +// return g4; +// } + + /// Convert the geometry type SensitiveDetector into the corresponding Geant4 object(s). void* Geant4Converter::handleSensitive(const TNamed* sens_det, const set<const TGeoVolume*>& /* volumes */) const { Geant4GeometryInfo& info = data(); - Geant4SensitiveDetector* g4 = info.g4SensDets[sens_det]; + G4VSensitiveDetector* g4 = info.g4SensDets[sens_det]; if ( !g4 ) { SensitiveDetector sd = Ref_t(sens_det); string type = sd.type(), name = sd.name(); - g4 = ROOT::Reflex::PluginService::Create<Geant4SensitiveDetector*>(type,name,&m_lcdd); - if ( !g4 ) { + // g4 = ROOT::Reflex::PluginService::Create<G4VSensitiveDetector*>(type,name,&m_lcdd); + G4SDFactory* sdf = ROOT::Reflex::PluginService::Create<G4SDFactory*>(type) ; + + if ( !sdf ) { string tmp = type; tmp[0] = ::toupper(tmp[0]); type = "Geant4"+tmp; - g4 = ROOT::Reflex::PluginService::Create<Geant4SensitiveDetector*>(type,name,&m_lcdd); - if ( !g4 ) { + + // g4 = ROOT::Reflex::PluginService::Create<G4VSensitiveDetector*>(type,name,&m_lcdd); + sdf = ROOT::Reflex::PluginService::Create<G4SDFactory*>(type) ; + + if ( !sdf ) { throw runtime_error("Geant4Converter<SensitiveDetector>: FATAL Failed to " - "create Geant4 sensitive detector "+name+" of type "+type+"."); + "create Geant4 sensitive detector factory "+name+" of type "+type+"."); } } + g4 = sdf->createSD( name , m_lcdd ) ; + + if ( !g4 ) { + throw runtime_error("Geant4Converter<SensitiveDetector>: FATAL Failed to " + "create SD from factory "+name+" of type "+type+"."); + } + g4->Activate(true); - g4->defineCollection(sd.hitsCollection()); + + // fg: don't know if this is needed ... + Geant4SensitiveDetector* g4sd = dynamic_cast<Geant4SensitiveDetector*>( g4 ) ; + if( g4sd) + g4sd->defineCollection(sd.hitsCollection()); + // ...but to be save we treat old Geant4SensitiveDetectors as before ... + G4SDManager::GetSDMpointer()->AddNewDetector(g4); info.g4SensDets[sens_det] = g4; } @@ -801,7 +848,7 @@ void Geant4Converter::handleProperties(LCDD::Properties& prp) const { /// Convert the geometry type SensitiveDetector into the corresponding Geant4 object(s). void* Geant4Converter::printSensitive(const TNamed* sens_det, const set<const TGeoVolume*>& /* volumes */) const { Geant4GeometryInfo& info = data(); - Geant4SensitiveDetector* g4 = info.g4SensDets[sens_det]; + G4VSensitiveDetector* g4 = info.g4SensDets[sens_det]; ConstVolumeSet& volset = info.sensitives[sens_det]; SensitiveDetector sd = Ref_t(sens_det); stringstream str; diff --git a/DDG4/src/Geant4TrackerCombineSD.cpp b/DDG4/src/Geant4TrackerCombineSD.cpp index d380c41cb6c4a5dfb0af30bf8e59c893695dd5bb..51c1dbc416dc4a1ce293ff2ed11127c4c4ddfc56 100644 --- a/DDG4/src/Geant4TrackerCombineSD.cpp +++ b/DDG4/src/Geant4TrackerCombineSD.cpp @@ -107,7 +107,8 @@ namespace DD4hep { namespace Simulation { } return return_code; } - typedef Geant4GenericSD<TrackerCombine> Geant4TrackerCombine; + // typedef Geant4GenericSD<TrackerCombine> Geant4TrackerCombine; + typedef SimpleG4SDFactory< Geant4GenericSD<TrackerCombine> > Geant4TrackerCombine; }} // End namespace DD4hep::Simulation DECLARE_GEANT4SENSITIVEDETECTOR(Geant4TrackerCombine) diff --git a/DDG4/src/Geant4TrackerSD.cpp b/DDG4/src/Geant4TrackerSD.cpp index 02abee5c03fe75274c5f80450c2cac4b75b86ea4..ca0908c31ac6968621868fd2082bb5e9940c1121 100644 --- a/DDG4/src/Geant4TrackerSD.cpp +++ b/DDG4/src/Geant4TrackerSD.cpp @@ -56,7 +56,8 @@ namespace DD4hep { namespace Simulation { collection(0)->insert(hit); return hit != 0; } - typedef Geant4GenericSD<Tracker> Geant4Tracker; + //typedef Geant4GenericSD<Tracker> Geant4Tracker; + typedef SimpleG4SDFactory< Geant4GenericSD<Tracker> > Geant4Tracker; }} // End namespace DD4hep::Simulation DECLARE_GEANT4SENSITIVEDETECTOR(Geant4Tracker)