diff --git a/Simulation/DetSimAna/src/ExampleAnaElemTool.cpp b/Simulation/DetSimAna/src/ExampleAnaElemTool.cpp index d0611ef23db8c8ea8a88cb04f0d83456274f056b..743b2bb056471a5741546d75b4725c3832eab6ab 100644 --- a/Simulation/DetSimAna/src/ExampleAnaElemTool.cpp +++ b/Simulation/DetSimAna/src/ExampleAnaElemTool.cpp @@ -1,12 +1,15 @@ #include "ExampleAnaElemTool.h" #include "G4Event.hh" +#include "G4THitsCollection.hh" #include "DD4hep/Detector.h" #include "DD4hep/Plugins.h" #include "DDG4/Geant4Converter.h" #include "DDG4/Geant4Mapping.h" - +#include "DDG4/Geant4HitCollection.h" +#include "DDG4/Geant4Data.h" +#include "DDG4/Geant4Hits.h" DECLARE_COMPONENT(ExampleAnaElemTool) @@ -56,8 +59,65 @@ ExampleAnaElemTool::EndOfEventAction(const G4Event* anEvent) { << " #" << icol << " has " << nhits << " hits." << endmsg; + // There are different types (new and old) + + dd4hep::sim::Geant4HitCollection* coll = dynamic_cast<dd4hep::sim::Geant4HitCollection*>(collect); + if (coll) { + info() << " cast to dd4hep::sim::Geant4HitCollection. " << endmsg; + for(size_t i=0; i<nhits; ++i) { + + dd4hep::sim::Geant4HitData* h = coll->hit(i); + + dd4hep::sim::Geant4Tracker::Hit* trk_hit = dynamic_cast<dd4hep::sim::Geant4Tracker::Hit*>(h); + if ( 0 != trk_hit ) { + dd4hep::sim::Geant4HitData::Contribution& t = trk_hit->truth; + int trackID = t.trackID; + // t.trackID = m_truth->particleID(trackID); + } + // Geant4Calorimeter::Hit* cal_hit = dynamic_cast<Geant4Calorimeter::Hit*>(h); + // if ( 0 != cal_hit ) { + // Geant4HitData::Contributions& c = cal_hit->truth; + // for(Geant4HitData::Contributions::iterator j=c.begin(); j!=c.end(); ++j) { + // Geant4HitData::Contribution& t = *j; + // int trackID = t.trackID; + // // t.trackID = m_truth->particleID(trackID); + // } + // } + } + continue; + } + + typedef G4THitsCollection<dd4hep::sim::Geant4Hit> HitCollection; + HitCollection* coll2 = dynamic_cast<HitCollection*>(collect); + + if (coll2) { + info() << " cast to G4THitsCollection<dd4hep::sim::Geant4Hit>. " << endmsg; + + for(size_t i=0; i<nhits; ++i) { + dd4hep::sim::Geant4Hit* h = dynamic_cast<dd4hep::sim::Geant4Hit*>(coll2->GetHit(i)); + if (!h) { + warning() << "Failed to cast to dd4hep::sim::Geant4Hit. " << endmsg; + continue; + } + dd4hep::sim::Geant4TrackerHit* trk_hit = dynamic_cast<dd4hep::sim::Geant4TrackerHit*>(h); + if (trk_hit) { + info() << " cast to dd4hep::sim::Geant4TrackerHit. " << endmsg; + } + + dd4hep::sim::Geant4CalorimeterHit* cal_hit = dynamic_cast<dd4hep::sim::Geant4CalorimeterHit*>(h); + if (cal_hit) { + info() << " cast to dd4hep::sim::Geant4CalorimeterHit. " << endmsg; + } + + } + + continue; + } + warning() << "Failed to convert to collection " + << collect->GetName() + << endmsg; } }