diff --git a/Simulation/DetSimSD/include/DetSimSD/CaloSensitiveDetector.h b/Simulation/DetSimSD/include/DetSimSD/CaloSensitiveDetector.h index 28b3ab3b11f7e48b6f300402e1e9e9edefd986bb..0ad3398ae7592136d63c682ba3a1d1f5efdbf487 100644 --- a/Simulation/DetSimSD/include/DetSimSD/CaloSensitiveDetector.h +++ b/Simulation/DetSimSD/include/DetSimSD/CaloSensitiveDetector.h @@ -15,7 +15,7 @@ public: typedef G4THitsCollection<CalorimeterHit> CaloHitCollection; public: - CaloSensitiveDetector(const std::string& name, dd4hep::Detector& description); + CaloSensitiveDetector(const std::string& name, dd4hep::Detector& description, bool unmerge=true); public: // Geant4 interface @@ -30,6 +30,8 @@ protected: protected: HitCollection* m_hc; + std::map<unsigned long, CalorimeterHit*> m_hitMap; + bool m_unmerge; }; diff --git a/Simulation/DetSimSD/src/CaloSensitiveDetector.cpp b/Simulation/DetSimSD/src/CaloSensitiveDetector.cpp index 1547f9fe3506be659803ef0d8f5da2e3bcd79fdf..8fd0dcb7547e709e787987b34b5919d8bb95ee30 100644 --- a/Simulation/DetSimSD/src/CaloSensitiveDetector.cpp +++ b/Simulation/DetSimSD/src/CaloSensitiveDetector.cpp @@ -4,9 +4,10 @@ #include <algorithm> -CaloSensitiveDetector::CaloSensitiveDetector(const std::string& name, dd4hep::Detector& description) +CaloSensitiveDetector::CaloSensitiveDetector(const std::string& name, dd4hep::Detector& description, bool unmerge) : DDG4SensitiveDetector(name, description), - m_hc(nullptr) { + m_hc(nullptr), + m_unmerge(unmerge){ const std::string& coll_name = m_sensitive.hitsCollection(); collectionName.insert(coll_name); } @@ -23,6 +24,7 @@ CaloSensitiveDetector::Initialize(G4HCofThisEvent* HCE) { if(HCID<0) HCID = G4SDManager::GetSDMpointer()->GetCollectionID(m_hc); HCE->AddHitsCollection( HCID, m_hc ); + m_hitMap.clear(); } G4bool @@ -33,13 +35,21 @@ CaloSensitiveDetector::ProcessHits(G4Step* step, G4TouchableHistory*) { dd4hep::sim::Geant4StepHandler h(step); dd4hep::Position pos = 0.5 * (h.prePos() + h.postPos()); HitContribution contrib = dd4hep::sim::Geant4Hit::extractContribution(step); - CalorimeterHit* hit=find(m_hc,dd4hep::sim::HitPositionCompare<CalorimeterHit>(pos)); - + const std::string& name = GetName(); + unsigned long id = getCellID( step ); + //std::cout << name << " " << id << std::endl; + CalorimeterHit* hit=nullptr; + if(m_unmerge) hit=find(m_hc,dd4hep::sim::HitPositionCompare<CalorimeterHit>(pos)); + else{ + std::map<unsigned long, CalorimeterHit*>::iterator it = m_hitMap.find(id); + if(it!=m_hitMap.end()) hit = it->second; + } // G4cout << "----------- Geant4GenericSD<Calorimeter>::buildHits : position : " << pos << G4endl; if ( !hit ) { hit = new CalorimeterHit(pos); - hit->cellID = getCellID( step ); + hit->cellID = id; //getCellID( step ); m_hc->insert(hit); + if(!m_unmerge) m_hitMap[id] = hit; } hit->truth.push_back(contrib); hit->energyDeposit += contrib.deposit; diff --git a/Simulation/DetSimSD/src/CalorimeterSensDetTool.cpp b/Simulation/DetSimSD/src/CalorimeterSensDetTool.cpp index 844104e28b4fda57d60ab1100d38d7ae7d3c25b8..cb7be22964898405fc9d6d8c5cae0e66cf34d9ed 100644 --- a/Simulation/DetSimSD/src/CalorimeterSensDetTool.cpp +++ b/Simulation/DetSimSD/src/CalorimeterSensDetTool.cpp @@ -35,7 +35,15 @@ CalorimeterSensDetTool::createSD(const std::string& name) { dd4hep::Detector* dd4hep_geo = m_geosvc->lcdd(); - G4VSensitiveDetector* sd = new CaloSensitiveDetector(name, *dd4hep_geo); + bool flagUnmerge = false; + for(auto cal_name : m_unmergeCals){ + if(cal_name==name){ + flagUnmerge = true; + break; + } + } + G4VSensitiveDetector* sd = new CaloSensitiveDetector(name, *dd4hep_geo, flagUnmerge); + debug() << name << " set to merge true/false = " << !flagUnmerge << endmsg; return sd; } diff --git a/Simulation/DetSimSD/src/CalorimeterSensDetTool.h b/Simulation/DetSimSD/src/CalorimeterSensDetTool.h index f3e0796b55670534b10cfb242093a577c724f4e9..dd1b24f078b605b1b0ee4c8cd67ddf87a9aa1784 100644 --- a/Simulation/DetSimSD/src/CalorimeterSensDetTool.h +++ b/Simulation/DetSimSD/src/CalorimeterSensDetTool.h @@ -29,6 +29,7 @@ private: // in order to initialize SD, we need to get the lcdd() SmartIF<IGeomSvc> m_geosvc; + Gaudi::Property<std::vector<std::string> > m_unmergeCals{this, "UnmergedCalNames", {}}; }; #endif