diff --git a/DDG4/include/DDG4/Geant4SensitiveDetector.h b/DDG4/include/DDG4/Geant4SensitiveDetector.h index c834a9f063d5ef0786ae18a22ac5d4dd77ddd695..db8f41aa6f1496fd6b65f6f500095aee4b67e71e 100644 --- a/DDG4/include/DDG4/Geant4SensitiveDetector.h +++ b/DDG4/include/DDG4/Geant4SensitiveDetector.h @@ -98,7 +98,9 @@ namespace DD4hep { virtual bool buildHits(G4Step* /* step */,G4TouchableHistory* /* history */) { return false; } - /// Returns the volumeID of the sensitive volume corresponding to the step. + /// Returns the volumeID of the sensitive volume corresponding to the step - + /// combining the VolIDS of the complete geometry path (Geant4TouchableHistory) + // from the current sensitive volume to the world volume long long getVolumeID(G4Step* step ) ; /** G4VSensitiveDetector interface: Method invoked at the begining of each event. diff --git a/DDG4/src/Geant4CalorimeterSD.cpp b/DDG4/src/Geant4CalorimeterSD.cpp index 454e509062e439386d72a8846636e2b9a7ea15b2..ab69cff5b5af5afd4d90770405ac1558bca04697 100644 --- a/DDG4/src/Geant4CalorimeterSD.cpp +++ b/DDG4/src/Geant4CalorimeterSD.cpp @@ -34,15 +34,14 @@ namespace DD4hep { namespace Simulation { HitContribution contrib = Geant4Hit::extractContribution(step); Geant4CalorimeterHit* hit=find(collection(0),HitPositionCompare<Geant4CalorimeterHit>(pos)); + G4cout << "----------- Geant4GenericSD<Calorimeter>::buildHits : position : " << pos << G4endl ; + if ( !hit ) { hit = new Geant4CalorimeterHit(pos) ; - // set the cellID to the volumeID which is the or - // of all physicalVolIDs set along the path - // of the current placed volume hit->cellID = getVolumeID( step ) ; - + collection(0)->insert(hit) ; } hit->truth.push_back(contrib); diff --git a/DDG4/src/Geant4SensitiveDetector.cpp b/DDG4/src/Geant4SensitiveDetector.cpp index 73d2b3acde9151beaa43a6026dbdc5fdf2b3702a..e9f3ffbde612e5364ce0e2c39d0aefa6d0111262 100644 --- a/DDG4/src/Geant4SensitiveDetector.cpp +++ b/DDG4/src/Geant4SensitiveDetector.cpp @@ -167,12 +167,51 @@ void Geant4SensitiveDetector::dumpStep(G4Step* st, G4TouchableHistory* /* histor long long Geant4SensitiveDetector::getVolumeID(G4Step* step ){ Geant4Mapping& mapping = Geant4Mapping::instance(); - Geant4StepHandler stepH(step); - const G4VPhysicalVolume* pv = stepH.volume(stepH.pre); - Geometry::PlacedVolume placed = mapping.placement(pv); - Geometry::VolumeManager vm = m_lcdd.volumeManager(); - return 0; - //return ( placed.isValid() ? vm.lookupID( placed ) : 0 ) ; + + const G4NavigationHistory* hist = step->GetPreStepPoint()->GetTouchableHandle()->GetHistory() ; + int depth = hist->GetDepth() ; + //------------ get the cellID description string ----------------------------------- + + const G4VPhysicalVolume* g4v = hist->GetVolume( depth ) ; + Geometry::PlacedVolume place = mapping.placement( g4v ) ; + + if( ! place.isValid() || ! place.volume().isSensitive() ) { + G4cout << " **** Error in Geant4SensitiveDetector::getVolumeID: invalid first sensitive volume in buildHits is not sensitive !!! " << std::endl ; + } + Geometry::Volume vol = place.volume(); + Geometry::SensitiveDetector sd = vol.sensitiveDetector(); + Geometry::Readout ro = sd.readout(); + + + std::string idDescStr = ro.idSpec().fieldDescription() ; + + // G4cout << "----------- Geant4SensitiveDetector::getVolumeID - idDescStr = " << idDescStr << std::endl ; + + BitField64 bf( idDescStr ) ; + + //------------ now fill the cellID from the volIDs of the complete path ----------------------------------- + + for(int i=depth ; i>0 ; --i ) { + + g4v = hist->GetVolume(i) ; + Geometry::PlacedVolume place = mapping.placement( g4v ) ; + + if( ! place.isValid() ) { + G4cout << " **** WARNING in Geant4SensitiveDetector::getVolumeID: ingnoring invalid PlacedVolume for : " << g4v->GetName() << std::endl ; + continue ; + } + + // G4cout << "--- VolIDs : " << std::endl ; + Geometry::PlacedVolume::VolIDs ids = place.volIDs() ; + + for( Geometry::PlacedVolume::VolIDs::const_iterator it = ids.begin() ; it != ids.end() ; ++it ){ + + // G4cout << "--- " << it->first << " -- " << it->second << std:: + bf[ it->first ] = it->second ; + } + } + + return bf.getValue() ; } diff --git a/DDG4/src/Geant4TrackerSD.cpp b/DDG4/src/Geant4TrackerSD.cpp index 1a66c2fa834b2b768ac374d5d0e3711eae9a6721..a1032aa7b991c5ca6b5c76fc43d17df46b9d3f99 100644 --- a/DDG4/src/Geant4TrackerSD.cpp +++ b/DDG4/src/Geant4TrackerSD.cpp @@ -11,12 +11,8 @@ #include "DDG4/Factories.h" #include "DDG4/Geant4StepHandler.h" #include "DDG4/Geant4Mapping.h" - #include "DD4hep/BitField64.h" -//#include "G4Step.hh" - - /* * DD4hep::Simulation namespace declaration */ @@ -28,96 +24,8 @@ 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* /*hist*/ ) { - - StepHandler h(step); - - - // //------------ get the cellID ----------------------------------- - -// Geant4Mapping& mapping = Geant4Mapping::instance(); -// Geant4StepHandler stepH(step); -// const G4VPhysicalVolume* pv = stepH.volume(stepH.pre); - -// int cellID = 0 ; - -// Geometry::PlacedVolume place = mapping.placement(pv); - -// if ( place.isValid() ) { -// if ( place.volume().isSensitive() ) { - -// // G4cout << "----------- Geant4GenericSD<Tracker>::buildHits - get volumeID from " << place.toString() << std::endl ; - -// Geometry::VolumeManager vm = mapping.lcdd().volumeManager(); -// Geometry::VolumeManager::VolumeID cell_id = vm.lookupID( place ); - -// // const Geometry::PlacedVolume::VolIDs& ids = place.volIDs(); -// // printf(" Found Sensitive TGeoNode:%s CellID: %llx #VolIDs: %d --- at level: %d \n", place.name(), cell_id , ids.size() , i ); -// // for( Geometry::PlacedVolume::VolIDs::const_iterator it = ids.begin() ; it != ids.end() ; ++it ){ -// // G4cout << "--- " << it->first << " -- " << it->second << std::endl ; -// // } - -// // Geometry::Volume vol = place.volume(); -// // Geometry::SensitiveDetector sd = vol.sensitiveDetector(); -// // Geometry::Readout ro = sd.readout(); -// // Geometry::IDDescriptor iddesc = ro.idSpec(); - -// Geometry::IDDescriptor iddesc = m_readout.idSpec(); - -// //----- use a BitField64 object to store the cellID in the Linear collider convention -// BitField64 bf( iddesc.toString() ) ; -// #define test_cellID 0 -// #if test_cellID - -// for( unsigned i=0, N=bf.size() ;i<N;++i){ - -// BitFieldValue& v = bf[i] ; - -// long val = iddesc.field( v.name() ).decode( cell_id ) ; - -// if( v.isSigned() && ( val & ( 1LL << ( v.width() - 1 ) ) ) != 0 ) { // negative value - -// val -= ( 1LL << v.width() ); -// } -// v = val ; - -// // this does not work as the IDDescriptor does not handle signed (negative) values correctly -// // b[i] = iddesc.field( b[i].name() ).decode( cell_id ) ; -// } - -// G4cout << "--- IDDescriptor: " << iddesc.toString() -// << std::endl ; - - -// BitField64 testBF( iddesc.toString() ) ; -// testBF.setValue( cell_id ) ; -// G4cout << "--- testing BitField64 testBF \n " << testBF -// << " \n bf from IDDescriptor : " << bf << std::endl ; - -// assert( testBF.getValue() == bf.getValue() ) ; -// #else -// bf.setValue( cell_id ) ; // this sets the unused high bits to 0 ! -// #endif -// // cellID = bf.lowWord() ; -// -// -// cellID = cell_id ; -// -// -// } else { -// -// G4cerr << "**ERROR** Geant4GenericSD<Tracker>::buildHits - called for insensitive volume: " -// << place.toString() -// << std::endl ; -// -// } -// -// } else{ -// -// G4cerr << "----------- Geant4GenericSD<Tracker>::buildHits - invalid Volume found: " << std::endl ; -// } - - + StepHandler h(step); Position prePos = h.prePos(); Position postPos = h.postPos(); Position direction = postPos - prePos; @@ -128,7 +36,7 @@ namespace DD4hep { namespace Simulation { direction *= new_len/hit_len; } - // G4cout << "----------- Geant4GenericSD<Tracker>::buildHits : position : " << prePos << G4endl ; + G4cout << "----------- Geant4GenericSD<Tracker>::buildHits : position : " << prePos << G4endl ; Geant4TrackerHit* hit = new Geant4TrackerHit(h.track->GetTrackID(), @@ -136,13 +44,8 @@ namespace DD4hep { namespace Simulation { step->GetTotalEnergyDeposit(), h.track->GetGlobalTime()); - HitContribution contrib = Geant4Hit::extractContribution(step); - - // set the cellID to the volumeID which is the or - // of all physicalVolIDs set along the path - // of the current placed volume hit->cellID = getVolumeID( step ) ; hit->energyDeposit = contrib.deposit ;