diff --git a/DDG4/include/DDG4/Defs.h b/DDG4/include/DDG4/Defs.h index d582d5f150a968a44b36d8f635b07342559f5c92..a898eb46dfeea63430e2ecff0e8c96d7f02dab33 100644 --- a/DDG4/include/DDG4/Defs.h +++ b/DDG4/include/DDG4/Defs.h @@ -31,6 +31,18 @@ namespace DD4hep { typedef Geometry::DetElement DetElement; typedef Geometry::SensitiveDetector SensitiveDetector; + template<class HIT> struct HitCompare; + template<class HIT> struct HitPositionCompare; + class Geant4StepHandler; + class Geant4Hit; + class Geant4TrackerHit; + class Geant4CalorimeterHit; + + class Geant4SensitiveDetector; + template <class T> class Geant4GenericSD; + + + } // End namespace Simulation } // End namespace DD4hep #endif // DDG4_DEFS_H diff --git a/DDG4/include/DDG4/Factories.h b/DDG4/include/DDG4/Factories.h index ddffb4069db91c35741c644c95b4a2ba81a1af13..c15e84491a86ac51fdc59abdc38a2d2348be5497 100644 --- a/DDG4/include/DDG4/Factories.h +++ b/DDG4/include/DDG4/Factories.h @@ -16,49 +16,26 @@ // Framework include files #include "DDG4/Defs.h" +#include <string> -class G4VSensitiveDetector; +// Forward declarations +class Geant4SensitiveDetector; -/* - * DD4hep namespace declaration - */ -namespace DD4hep { - - /* - * Simulation namespace declaration - */ - namespace Simulation { -#if 0 - /** @class XMLElementFactory Factories.h DDCore/Factories.h - * Create an arbitrary object from it's XML representation. - * - * @author M.Frank - * @version 1.0 - * @date 2012/07/31 - */ - template <typename T> class Geant4SensitiveDetectorFactory { - public: - static G4VSensitiveDetector* create(const std::string& name,LCDD& lcdd) { return new T(name,lcdd); } - }; -#endif - } // End namespace Simulation -} // End namespace DD4hep +namespace DD4hep { + namespace Geometry { class LCDD; } + namespace Simulation { class Geant4SensitiveDetector; } +} namespace { - template < typename P > class Factory<P, G4VSensitiveDetector*(const std::string& name,DD4hep::Geometry::LCDD*)> { - public: - static void Func(void *retaddr, void*, const std::vector<void*>& arg, void*) { - typedef G4VSensitiveDetector SD; - std::string nam = *(std::string*)arg[0]; - DD4hep::Geometry::LCDD* lcdd = (DD4hep::Geometry::LCDD* )arg[1]; - *(SD**)retaddr = new P(nam,*lcdd); - } + template < typename P > class Factory<P, DD4hep::Simulation::Geant4SensitiveDetector*(std::string,DD4hep::Geometry::LCDD*)> { + public: typedef DD4hep::Simulation::Geant4SensitiveDetector SD; + static void Func(void *retaddr, void*, const std::vector<void*>& arg, void*) + { *(SD**)retaddr = (SD*)new P(*(std::string*)arg[0], *(DD4hep::Geometry::LCDD*)arg[1]); } }; } -#define DECLARE_GEANT4SENSITIVEDETECTOR(name) \ - using DD4hep::Simulation::name; \ - PLUGINSVC_FACTORY(name,G4VSensitiveDetector*(const std::string&,DD4hep::Geometry::LCDD*)) - +#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*)) #endif // DDG4_FACTORIES_H diff --git a/DDG4/include/DDG4/Geant4Converter.h b/DDG4/include/DDG4/Geant4Converter.h index 1ad20aa1c95966d2d6aa91aebe0abe3b6050d753..3cf92fa001499799fd89fef5384882833a2a947e 100644 --- a/DDG4/include/DDG4/Geant4Converter.h +++ b/DDG4/include/DDG4/Geant4Converter.h @@ -13,32 +13,6 @@ #include "DD4hep/GeoHandler.h" #include "DD4hep/LCDD.h" -// Geant4 include files -#include "G4Element.hh" -#include "G4Box.hh" -#include "G4Tubs.hh" -#include "G4Trd.hh" -#include "G4Paraboloid.hh" -#include "G4Polycone.hh" -#include "G4Polyhedra.hh" -#include "G4Sphere.hh" -#include "G4Torus.hh" -#include "G4UnionSolid.hh" -#include "G4SubtractionSolid.hh" -#include "G4IntersectionSolid.hh" - -#include "G4Region.hh" -#include "G4UserLimits.hh" -#include "G4VSensitiveDetector.hh" - -#include "G4LogicalVolume.hh" -#include "G4Material.hh" -#include "G4Element.hh" -#include "G4Isotope.hh" -#include "G4Transform3D.hh" -#include "G4ThreeVector.hh" -#include "G4PVPlacement.hh" - // C/C++ include files #include <set> #include <map> @@ -46,8 +20,20 @@ // Forward declarations class TGeoVolume; +class TGeoElement; +class TGeoShape; +class TGeoMedium; class TGeoNode; +class G4Element; +class G4Material; +class G4VSolid; +class G4LogicalVolume; +class G4PVPlacement; +class G4Region; +class Geant4SensitiveDetector; +class G4UserLimits; + /* * DD4hep namespace declaration */ @@ -59,6 +45,8 @@ namespace DD4hep { namespace Simulation { + class Geant4SensitiveDetector; + /** @class Geant4Converter Geant4Converter.h DDG4/Geant4Converter.h * * Geometry converter from DD4hep to Geant 4. @@ -70,14 +58,14 @@ namespace DD4hep { typedef std::map<const TNamed*,G4UserLimits*> LimitMap; struct G4GeometryInfo : public GeometryInfo { - std::map<const TGeoElement*,G4Element*> g4Elements; - std::map<const TGeoMedium*, G4Material*> g4Materials; - std::map<const TGeoShape*, G4VSolid*> g4Solids; - std::map<const TGeoVolume*, G4LogicalVolume*> g4Volumes; - std::map<const TGeoNode*, G4PVPlacement*> g4Placements; - std::map<const TNamed*, G4Region*> g4Regions; - LimitMap g4Limits; - std::map<const TNamed*, G4VSensitiveDetector*> g4SensDets; + std::map<const TGeoElement*,G4Element*> g4Elements; + std::map<const TGeoMedium*, G4Material*> g4Materials; + std::map<const TGeoShape*, G4VSolid*> g4Solids; + std::map<const TGeoVolume*, G4LogicalVolume*> g4Volumes; + std::map<const TGeoNode*, G4PVPlacement*> g4Placements; + std::map<const TNamed*, G4Region*> g4Regions; + LimitMap g4Limits; + std::map<const TNamed*, Geant4SensitiveDetector*> g4SensDets; SensitiveVolumes sensitives; RegionVolumes regions; @@ -87,7 +75,7 @@ namespace DD4hep { LCDD& m_lcdd; G4GeometryInfo* m_dataPtr; G4GeometryInfo& data() const { return *m_dataPtr; } - + /// Constructor Geant4Converter( LCDD& lcdd ) : m_lcdd(lcdd) {} /// Standard destructor diff --git a/DDG4/include/DDG4/Geant4Hits.h b/DDG4/include/DDG4/Geant4Hits.h index a22c62f269b8592c42972714677d5cdb1ba5842f..55ff90837688d5601d1b8c5392f955699dfbc329 100644 --- a/DDG4/include/DDG4/Geant4Hits.h +++ b/DDG4/include/DDG4/Geant4Hits.h @@ -29,6 +29,14 @@ namespace DD4hep { */ namespace Simulation { + // Forward declarations; + template<class HIT> struct HitCompare; + template<class HIT> struct HitPositionCompare; + class Geant4StepHandler; + class Geant4Hit; + class Geant4TrackerHit; + class Geant4CalorimeterHit; + /** @class HitCompare Geant4Hits.h DDG4/Geant4Hits.h * @@ -57,6 +65,49 @@ namespace DD4hep { virtual bool operator()(const HIT* h) const { return pos == h->position; } }; + /** @class Geant4StepHandler Geant4SensitiveDetector.h DDG4/Geant4SensitiveDetector.h + * + * Tiny helper/utility class to easily access Geant4 step information. + * Born by lazyness: Avoid typing millions of statements! + * + * @author M.Frank + * @version 1.0 + */ + class Geant4StepHandler { + public: + G4Step* step; + G4StepPoint* pre; + G4StepPoint* post; + G4Track* track; + Geant4StepHandler(G4Step* s) : step(s) { + pre = s->GetPreStepPoint(); + post = s->GetPostStepPoint(); + track = s->GetTrack(); + } + Position prePos() const { + const G4ThreeVector& p = pre->GetPosition(); + return Position(p.x(),p.y(),p.z()); + } + Position postPos() const { + const G4ThreeVector& p = post->GetPosition(); + return Position(p.x(),p.y(),p.z()); + } + Momentum preMom() const { + const G4ThreeVector& p = pre->GetMomentum(); + return Momentum(p.x(),p.y(),p.z()); + } + Momentum postMom() const { + const G4ThreeVector& p = post->GetMomentum(); + return Momentum(p.x(),p.y(),p.z()); + } + G4VPhysicalVolume* volume(G4StepPoint* p) const { + return p->GetTouchableHandle()->GetVolume(); + } + G4VSensitiveDetector* sd(G4StepPoint* p) const { + return p->GetPhysicalVolume()->GetLogicalVolume()->GetSensitiveDetector(); + } + }; + /** @class Geant4Hit Geant4Hits.h DDG4/Geant4Hits.h * * Geant4 hit base class. Here only the basic @@ -100,8 +151,6 @@ namespace DD4hep { virtual ~Geant4Hit() {} /// Check if the Geant4 track is a Geantino static bool isGeantino(G4Track* track); - /// Create tracker hit from step information - static Geant4Hit* createTrackerHit(G4Step* step); /// Extract the MC contribution for a given hit from the step information static Contribution extractContribution(G4Step* step); }; diff --git a/DDG4/include/DDG4/Geant4SensitiveDetector.h b/DDG4/include/DDG4/Geant4SensitiveDetector.h index 3e84386e856cc97742b4fc4c6c85a1161ef3ace0..7a830a06e4f825423a4d3feba254e4a2571e2f90 100644 --- a/DDG4/include/DDG4/Geant4SensitiveDetector.h +++ b/DDG4/include/DDG4/Geant4SensitiveDetector.h @@ -32,6 +32,10 @@ namespace DD4hep { */ namespace Simulation { + // Forward declarations + class Geant4StepHandler; + class Geant4SensitiveDetector; + /** @class Geant4SensitiveDetector Geant4SensitiveDetector.h DDG4/Geant4SensitiveDetector.h * * @author M.Frank @@ -40,9 +44,9 @@ namespace DD4hep { struct Geant4SensitiveDetector : public G4VSensitiveDetector { public: - typedef std::vector<std::string> HitCollectionNames; typedef G4THitsCollection<Geant4Hit> HitCollection; typedef Geant4Hit::Contribution HitContribution; + typedef Geant4StepHandler StepHandler; protected: @@ -67,10 +71,7 @@ namespace DD4hep { Geant4SensitiveDetector(const std::string& name, LCDD& lcdd); /// Initialize the sensitive detector for the usage of a single hit collection - virtual bool defineCollections(const std::string& coll_name); - - /// Initialize the sensitive detector for the usage of a multiple hit collection - virtual bool defineCollections(const HitCollectionNames& coll_names); + virtual bool defineCollection(const std::string& coll_name); /// Standard destructor virtual ~Geant4SensitiveDetector(); @@ -123,10 +124,7 @@ namespace DD4hep { Geant4GenericSD(const std::string& name, LCDD& lcdd); /// Initialize the sensitive detector for the usage of a single hit collection - virtual bool defineCollections(const std::string& coll_name); - - /// Initialize the sensitive detector for the usage of a multiple hit collection - virtual bool defineCollections(const HitCollectionNames& coll_names); + virtual bool defineCollection(const std::string& coll_name); /// Method for generating hit(s) using the information of G4Step object. virtual G4bool ProcessHits(G4Step* step,G4TouchableHistory* history); diff --git a/DDG4/include/DDG4/Geant4SensitiveDetector_inline.h b/DDG4/include/DDG4/Geant4SensitiveDetector_inline.h index 052c169001e21d17f4060199b4c8397fb8fb6a48..3e61e769db50ba088093f82ed24d1ac1c31299ae 100644 --- a/DDG4/include/DDG4/Geant4SensitiveDetector_inline.h +++ b/DDG4/include/DDG4/Geant4SensitiveDetector_inline.h @@ -20,15 +20,10 @@ template<class SD> DD4hep::Simulation::Geant4GenericSD<SD>::Geant4GenericSD(cons } /// Initialize the sensitive detector for the usage of a single hit collection -template<class SD> bool DD4hep::Simulation::Geant4GenericSD<SD>::defineCollections(const std::string& coll_name) { - return Geant4SensitiveDetector::defineCollections(coll_name); +template<class SD> bool DD4hep::Simulation::Geant4GenericSD<SD>::defineCollection(const std::string& coll_name) { + return Geant4SensitiveDetector::defineCollection(coll_name); } -/// Initialize the sensitive detector for the usage of a multiple hit collection -template<class SD> bool DD4hep::Simulation::Geant4GenericSD<SD>::defineCollections(const HitCollectionNames& coll_names) { - return Geant4SensitiveDetector::defineCollections(coll_names); -} - /** Method invoked at the begining of each event. * The hits collection(s) created by this sensitive detector must * be set to the G4HCofThisEvent object at one of these two methods. diff --git a/DDG4/src/Geant4CalorimeterSD.cpp b/DDG4/src/Geant4CalorimeterSD.cpp index 4f48802481871013e50c615fdb81c18661181b64..586d905fd13952f596505cfa46961c82cedc7f05 100644 --- a/DDG4/src/Geant4CalorimeterSD.cpp +++ b/DDG4/src/Geant4CalorimeterSD.cpp @@ -10,33 +10,89 @@ // Framework include files #include "DDG4/Geant4SensitiveDetector_inline.h" #include "DDG4/Factories.h" -#include "DD4hep/Objects.h" + +// Geant4 include files +#include "G4OpticalPhoton.hh" +#include "G4VProcess.hh" + +/* + * DD4hep::Simulation namespace declaration + */ +namespace DD4hep { namespace Simulation { + + /// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + /// Geant4GenericSD<Calorimeter> + /// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + struct Calorimeter {}; + /// Method for generating hit(s) using the information of G4Step object. + template <> bool Geant4GenericSD<Calorimeter>::buildHits(G4Step* step,G4TouchableHistory*) { + StepHandler h(step); + Position pos = 0.5 * (h.prePos() + h.postPos()); + HitContribution contrib = Geant4Hit::extractContribution(step); + Geant4CalorimeterHit* hit=find(collection(0),HitPositionCompare<Geant4CalorimeterHit>(pos)); + + if ( !hit ) { + collection(0)->insert(hit=new Geant4CalorimeterHit(pos)); + } + hit->truth.push_back(contrib); + hit->energyDeposit += contrib.deposit; + return true; + } + typedef Geant4GenericSD<Calorimeter> Geant4Calorimeter; +}} // End namespace DD4hep::Simulation + +DECLARE_GEANT4SENSITIVEDETECTOR(Geant4Calorimeter); /* * DD4hep::Simulation namespace declaration */ namespace DD4hep { namespace Simulation { - /// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - /// Geant4GenericSD<Calorimeter> - /// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - struct Calorimeter {}; + + /// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + /// Geant4GenericSD<OpticalCalorimeter> + /// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + enum { Edep_type=0, Cerenkov_type=1 }; + struct OpticalCalorimeter {}; + template <> class Geant4GenericSD<OpticalCalorimeter> : public Geant4GenericSD<Calorimeter> { + public: + /// Constructor. The sensitive detector element is identified by the detector name + Geant4GenericSD(const std::string& name, LCDD& lcdd) + : Geant4GenericSD<Calorimeter>(name,lcdd) { } + + /// Initialize the sensitive detector for the usage of a single hit collection + bool defineCollection(const std::string& coll_name) { + Geant4SensitiveDetector::defineCollection("Edep_" + coll_name); + Geant4SensitiveDetector::defineCollection("Ceren_" + coll_name); + return true; + } + /// Method for generating hit(s) using the information of G4Step object. - template <> bool Geant4GenericSD<Calorimeter>::buildHits(G4Step* step,G4TouchableHistory*) { - G4StepPoint* pre = step->GetPreStepPoint(); - G4StepPoint* post = step->GetPostStepPoint(); - G4ThreeVector pos = 0.5 * (pre->GetPosition() + post->GetPosition()); - HitContribution contrib = Geant4Hit::extractContribution(step); - Position position(pos.x(),pos.y(),pos.z()); - Geant4CalorimeterHit* hit=find(collection(0),HitPositionCompare<Geant4CalorimeterHit>(position)); - - if ( !hit ) { - collection(0)->insert(hit=new Geant4CalorimeterHit(position)); + virtual G4bool ProcessHits(G4Step* step,G4TouchableHistory* history) { + G4Track * track = step->GetTrack(); + // check that particle is optical photon: + if( track->GetDefinition() != G4OpticalPhoton::OpticalPhotonDefinition() ) { + return this->Geant4GenericSD<Calorimeter>::ProcessHits(step,history); + } + else if ( track->GetCreatorProcess()->G4VProcess::GetProcessName() != "Cerenkov") { + track->SetTrackStatus(fStopAndKill); + return false; + } + else { + StepHandler h(step); + HitContribution contrib = Geant4Hit::extractContribution(step); + Position pos = h.prePos(); + Geant4CalorimeterHit* hit=find(collection(Cerenkov_type),HitPositionCompare<Geant4CalorimeterHit>(pos)); + if ( !hit ) { + collection(Cerenkov_type)->insert(hit=new Geant4CalorimeterHit(pos)); + } + hit->energyDeposit += contrib.deposit; + hit->truth.push_back(contrib); + track->SetTrackStatus(fStopAndKill); // don't step photon any further + return true; } - hit->truth.push_back(contrib); - hit->energyDeposit += contrib.deposit; - return true; } - typedef Geant4GenericSD<Calorimeter> Geant4CalorimeterSD; + }; + typedef Geant4GenericSD<OpticalCalorimeter> Geant4OpticalCalorimeter; }} // End namespace DD4hep::Simulation -DECLARE_GEANT4SENSITIVEDETECTOR(Geant4CalorimeterSD); +DECLARE_GEANT4SENSITIVEDETECTOR(Geant4OpticalCalorimeter); diff --git a/DDG4/src/Geant4Converter.cpp b/DDG4/src/Geant4Converter.cpp index d919a13f7a893e41f290d6589fe965ac33a3167b..77ffe5547ffc58cba799c70beeb90ded48395a5d 100644 Binary files a/DDG4/src/Geant4Converter.cpp and b/DDG4/src/Geant4Converter.cpp differ diff --git a/DDG4/src/Geant4Hits.cpp b/DDG4/src/Geant4Hits.cpp index 60122f8b2419227984a879043c35e241149fe5ea..5e652cd1006038a6ee22fd2eee30e5b71ffbd9da 100644 --- a/DDG4/src/Geant4Hits.cpp +++ b/DDG4/src/Geant4Hits.cpp @@ -47,32 +47,6 @@ Geant4Hit::Contribution Geant4Hit::extractContribution(G4Step* step) { return contrib; } -/// Create tracker hit -Geant4Hit* Geant4Hit::createTrackerHit(G4Step* step) { - G4Track* trk = step->GetTrack(); - G4StepPoint* pre = step->GetPreStepPoint(); - G4StepPoint* post = step->GetPostStepPoint(); - G4ThreeVector prePos = pre->GetPosition(); - G4ThreeVector postPos = post->GetPosition(); - G4ThreeVector direction = (postPos - prePos); - G4ThreeVector position = 0.5 * (prePos + postPos); - double hit_len = direction.mag(); - double avgMom = (pre->GetMomentum().mag() + post->GetMomentum().mag()) / 2; - if (hit_len > 0) - direction.setMag(avgMom); - else - cerr << "prePos - postPos = 0 --> " << prePos << " | " << postPos << endl; - - Geant4TrackerHit* hit = new Geant4TrackerHit(trk->GetTrackID(), - trk->GetDefinition()->GetPDGEncoding(), - step->GetTotalEnergyDeposit(), - trk->GetGlobalTime()); - hit->position.set(position.x(),position.y(),position.z()); - hit->momentum.set(direction.x(),direction.y(),direction.z()); - hit->length = hit_len; - return hit; -} - static G4Allocator<Geant4TrackerHit> TrackerHitAllocator; /// Default constructor diff --git a/DDG4/src/Geant4SensitiveDetector.cpp b/DDG4/src/Geant4SensitiveDetector.cpp index 801c4e24545f55346f37ef3c3530b0527c8aaf37..a598f1ba9355a47db2095d361f717a4d3940c6fe 100644 --- a/DDG4/src/Geant4SensitiveDetector.cpp +++ b/DDG4/src/Geant4SensitiveDetector.cpp @@ -34,17 +34,14 @@ Geant4SensitiveDetector::~Geant4SensitiveDetector() { } /// Initialize the sensitive detector for the usage of a single hit collection -bool Geant4SensitiveDetector::defineCollections(const std::string& coll_name) { +bool Geant4SensitiveDetector::defineCollection(const string& coll_name) { + if ( coll_name.empty() ) { + throw runtime_error("Geant4SensitiveDetector: No collection defined for "+name()+ + " of type "+string(m_sensitive.type())); + } collectionName.insert(coll_name); } -/// Initialize the sensitive detector for the usage of a multiple hit collection -bool Geant4SensitiveDetector::defineCollections(const HitCollectionNames& coll_names) { - for(HitCollectionNames::const_iterator i=coll_names.begin(); i!=coll_names.end();++i) - collectionName.insert(*i); -} - - /// Access HitCollection container names const string& Geant4SensitiveDetector::hitCollectionName(int which) const { if ( which >= collectionName.size() || which < 0 ) { diff --git a/DDG4/src/Geant4TrackerCombineSD.cpp b/DDG4/src/Geant4TrackerCombineSD.cpp index 82dcb93e22f0f8b2c966625ad8edfde475f4313f..8be0611169c60d4dc8ad2955797cf3726e71be77 100644 --- a/DDG4/src/Geant4TrackerCombineSD.cpp +++ b/DDG4/src/Geant4TrackerCombineSD.cpp @@ -10,14 +10,12 @@ // Framework include files #include "DDG4/Geant4SensitiveDetector_inline.h" #include "DDG4/Factories.h" -#include "DD4hep/Objects.h" /* * DD4hep::Simulation namespace declaration */ namespace DD4hep { namespace Simulation { - //typedef Geant4SensitiveDetector::HitCollection HitCollection; - // typedef Geant4Hit::MonteCarloContrib HitContribution; + /// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ /// Geant4GenericSD<TrackerCombine> /// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ @@ -44,27 +42,28 @@ namespace DD4hep { namespace Simulation { current = -1; track = 0; } - Geant4TrackerHit* extractHit() { - if ( !track ) { - return 0; - } - else if ( pre.truth.deposit <= e_cut && !Geant4Hit::isGeantino(track) ) { + Geant4TrackerHit* extractHit(Geant4SensitiveDetector::HitCollection* c) { + if ( current == -1 || !track ) { + return 0; + } + else if ( pre.truth.deposit <= e_cut && !Geant4Hit::isGeantino(track) ) { + clear(); + return 0; + } + Position pos = 0.5 * (pre.position + post.position); + Momentum mom = 0.5 * (pre.momentum + post.momentum); + double path_len = (post.position - pre.position).length(); + Geant4TrackerHit* hit = new Geant4TrackerHit(pre.truth.trackID, + pre.truth.pdgID, + pre.truth.deposit, + pre.truth.time); + hit->position = pos; + hit->momentum = mom; + hit->length = path_len; clear(); - return 0; + c->insert(hit); + return hit; } - Position pos = 0.5 * (pre.position + post.position); - Momentum mom = 0.5 * (pre.momentum + post.momentum); - double path_len = (post.position - pre.position).length(); - Geant4TrackerHit* hit = new Geant4TrackerHit(pre.truth.trackID, - pre.truth.pdgID, - pre.truth.deposit, - pre.truth.time); - hit->position = pos; - hit->momentum = mom; - hit->length = path_len; - clear(); - return hit; - } }; /// Method invoked at the begining of each event. @@ -81,45 +80,34 @@ namespace DD4hep { namespace Simulation { /// Method for generating hit(s) using the information of G4Step object. template <> G4bool Geant4GenericSD<TrackerCombine>::ProcessHits(G4Step* step,G4TouchableHistory* ) { - G4Track* trk = step->GetTrack(); + StepHandler h(step); bool return_code = false; - if ( userData.current == -1 ) { - userData.start(step,step->GetPreStepPoint()); - } - else if ( userData.current != trk->GetTrackID() ) { - Geant4Hit* hit = userData.extractHit(); - if ( hit ) collection(0)->insert(hit); - return_code = hit != 0; - userData.start(step,step->GetPreStepPoint()); + if ( !userData.track || userData.current != h.track->GetTrackID() ) { + return_code = userData.extractHit(collection(0)) != 0; + userData.start(step, h.pre); } + // ....update ..... userData.update(step); - G4StepPoint* pre = step->GetPreStepPoint(); - G4StepPoint* post = step->GetPostStepPoint(); - G4VPhysicalVolume* prePV = pre->GetTouchableHandle()->GetVolume(); - G4VPhysicalVolume* postPV = post->GetTouchableHandle()->GetVolume(); + void *prePV = h.volume(h.pre), *postPV = h.volume(h.post); if ( prePV != postPV ) { - G4VSensitiveDetector* postSD = post->GetPhysicalVolume()->GetLogicalVolume()->GetSensitiveDetector(); - Geant4Hit* hit = userData.extractHit(); - if ( hit ) collection(0)->insert(hit); - return_code = hit != 0; + void* postSD = h.sd(h.post); + return_code = userData.extractHit(collection(0)) != 0; if ( 0 != postSD ) { - G4VSensitiveDetector* preSD = pre->GetPhysicalVolume()->GetLogicalVolume()->GetSensitiveDetector(); + void* preSD = h.sd(h.pre); if ( preSD == postSD ) { - userData.start(step,post); + userData.start(step,h.post); } } } else if ( userData.track->GetTrackStatus() == fStopAndKill ) { - Geant4Hit* hit = userData.extractHit(); - if ( hit ) collection(0)->insert(hit); - return_code = hit != 0; + return_code = userData.extractHit(collection(0)) != 0; } return return_code; } - typedef Geant4GenericSD<TrackerCombine> Geant4TrackerCombineSD; + typedef Geant4GenericSD<TrackerCombine> Geant4TrackerCombine; }} // End namespace DD4hep::Simulation -DECLARE_GEANT4SENSITIVEDETECTOR(Geant4TrackerCombineSD); +DECLARE_GEANT4SENSITIVEDETECTOR(Geant4TrackerCombine); diff --git a/DDG4/src/Geant4TrackerSD.cpp b/DDG4/src/Geant4TrackerSD.cpp index 2a8704f3eded602e84dae2ba63ebdd404d8e34d7..860228dfdf7446f2f97df20ec89a3cdda0e1e143 100644 --- a/DDG4/src/Geant4TrackerSD.cpp +++ b/DDG4/src/Geant4TrackerSD.cpp @@ -20,12 +20,27 @@ namespace DD4hep { namespace Simulation { struct Tracker {}; /// Method for generating hit(s) using the information of G4Step object. template <> bool Geant4GenericSD<Tracker>::buildHits(G4Step* step,G4TouchableHistory* ) { - /// Add a new hit to the collection - Geant4Hit* hit = Geant4Hit::createTrackerHit(step); + StepHandler h(step); + Position prePos = h.prePos(); + Position postPos = h.postPos(); + Position direction = (postPos - prePos); + Position position = mean_direction(prePos,postPos); + double hit_len = direction.length(); + if (hit_len > 0) { + direction.setLength(mean_length(h.preMom(),h.postMom())); + } + Geant4TrackerHit* hit = + new Geant4TrackerHit(h.track->GetTrackID(), + h.track->GetDefinition()->GetPDGEncoding(), + step->GetTotalEnergyDeposit(), + h.track->GetGlobalTime()); + hit->position = position; + hit->momentum = direction; + hit->length = hit_len; if ( hit ) collection(0)->insert(hit); return hit != 0; } - typedef Geant4GenericSD<Tracker> Geant4TrackerSD; + typedef Geant4GenericSD<Tracker> Geant4Tracker; }} // End namespace DD4hep::Simulation -DECLARE_GEANT4SENSITIVEDETECTOR(Geant4TrackerSD); +DECLARE_GEANT4SENSITIVEDETECTOR(Geant4Tracker);