Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:30:13

0001 #include "SimG4CMS/Tracker/interface/TrackerG4SimHitNumberingScheme.h"
0002 #include "Geometry/TrackerNumberingBuilder/interface/GeometricDet.h"
0003 #include "SimG4Core/Geometry/interface/DD4hep2DDDName.h"
0004 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0005 
0006 #include "G4TransportationManager.hh"
0007 #include "G4Navigator.hh"
0008 #include "G4VTouchable.hh"
0009 #include "G4TouchableHistory.hh"
0010 #include "G4VSensitiveDetector.hh"
0011 
0012 //#define EDM_ML_DEBUG
0013 
0014 TrackerG4SimHitNumberingScheme::TrackerG4SimHitNumberingScheme(const GeometricDet& det)
0015     : alreadySet_(false), geomDet_(&det) {}
0016 
0017 void TrackerG4SimHitNumberingScheme::buildAll() {
0018   if (alreadySet_)
0019     return;
0020   alreadySet_ = true;
0021 
0022   G4Navigator* theStdNavigator = G4TransportationManager::GetTransportationManager()->GetNavigatorForTracking();
0023   G4Navigator theNavigator;
0024   theNavigator.SetWorldVolume(theStdNavigator->GetWorldVolume());
0025 
0026   std::vector<const GeometricDet*> allSensitiveDets;
0027   geomDet_->deepComponents(allSensitiveDets);
0028   edm::LogVerbatim("TrackerSimInfoNumbering")
0029       << " TouchableTo History: got " << allSensitiveDets.size() << " sensitive detectors from GeometricDet.";
0030 
0031   for (auto& theSD : allSensitiveDets) {
0032     auto const& t = theSD->translation();
0033     theNavigator.LocateGlobalPointAndSetup(G4ThreeVector(t.x(), t.y(), t.z()));
0034     G4TouchableHistory* hist = theNavigator.CreateTouchableHistory();
0035     TrackerG4SimHitNumberingScheme::Nav_Story st;
0036     touchToNavStory(hist, st);
0037 
0038     directMap_[st] = theSD->geographicalId();
0039 
0040     LogDebug("TrackerSimDebugNumbering") << " INSERTING LV " << hist->GetVolume()->GetLogicalVolume()->GetName()
0041                                          << " SD: "
0042                                          << hist->GetVolume()->GetLogicalVolume()->GetSensitiveDetector()->GetName()
0043                                          << " Now size is " << directMap_.size();
0044     delete hist;
0045   }
0046   edm::LogVerbatim("TrackerSimInfoNumbering")
0047       << " TrackerG4SimHitNumberingScheme: mapped " << directMap_.size() << " detectors to Geant4.";
0048 
0049   if (directMap_.size() != allSensitiveDets.size()) {
0050     edm::LogError("TrackerSimInfoNumbering") << " ERROR: GeomDet sensitive detectors do not match Geant4 ones.";
0051     throw cms::Exception("TrackerG4SimHitNumberingScheme::buildAll")
0052         << " cannot resolve structure of tracking sensitive detectors";
0053   }
0054 }
0055 
0056 void TrackerG4SimHitNumberingScheme::touchToNavStory(const G4VTouchable* v,
0057                                                      TrackerG4SimHitNumberingScheme::Nav_Story& st) {
0058 #ifdef EDM_ML_DEBUG
0059   std::vector<int> debugint;
0060   std::vector<std::string> debugstring;
0061 #endif
0062   int levels = v->GetHistoryDepth();
0063 
0064   for (int k = 0; k <= levels; ++k) {
0065     if (DD4hep2DDDName::noNameSpace(static_cast<std::string>(v->GetVolume(k)->GetLogicalVolume()->GetName())) !=
0066         "TOBInactive") {
0067       st.emplace_back(std::pair<int, std::string>(
0068           v->GetVolume(k)->GetCopyNo(),
0069           DD4hep2DDDName::noNameSpace(static_cast<std::string>(v->GetVolume(k)->GetLogicalVolume()->GetName()))));
0070 #ifdef EDM_ML_DEBUG
0071       debugint.emplace_back(v->GetVolume(k)->GetCopyNo());
0072       debugstring.emplace_back(v->GetVolume(k)->GetLogicalVolume()->GetName());
0073 #endif
0074     }
0075   }
0076 #ifdef EDM_ML_DEBUG
0077   edm::LogVerbatim("TrackerSimDebugNumbering") << " G4 TrackerG4SimHitNumberingScheme " << debugint.size();
0078   for (u_int32_t jj = 0; jj < debugstring.size(); jj++)
0079     edm::LogVerbatim("TrackerSimDebugNumbering") << " " << debugstring[jj];
0080 #endif
0081 }
0082 
0083 unsigned int TrackerG4SimHitNumberingScheme::g4ToNumberingScheme(const G4VTouchable* v) {
0084   if (alreadySet_ == false) {
0085     buildAll();
0086   }
0087   TrackerG4SimHitNumberingScheme::Nav_Story st;
0088   touchToNavStory(v, st);
0089 
0090 #ifdef EDM_ML_DEBUG
0091   dumpG4VPV(v);
0092   edm::LogVerbatim("TrackerSimDebugNumbering") << " Returning: " << directMap_[st];
0093 #endif
0094 
0095   return directMap_[st];
0096 }
0097 
0098 void TrackerG4SimHitNumberingScheme::dumpG4VPV(const G4VTouchable* v) {
0099   int levels = v->GetHistoryDepth();
0100 
0101   edm::LogVerbatim("TrackerSimDebugNumbering") << " NAME : " << v->GetVolume()->GetLogicalVolume()->GetName();
0102   for (int k = 0; k <= levels; k++) {
0103     edm::LogVerbatim("TrackerSimInfoNumbering")
0104         << " Hist: " << v->GetVolume(k)->GetLogicalVolume()->GetName() << " Copy " << v->GetVolume(k)->GetCopyNo();
0105   }
0106 }