00001 #include "SimG4CMS/Tracker/interface/TouchableToHistory.h" 00002 #include "Geometry/TrackerNumberingBuilder/interface/TrackerMapDDDtoID.h" 00003 #include "DetectorDescription/Core/interface/DDExpandedView.h" 00004 #include "DetectorDescription/Core/interface/DDFilteredView.h" 00005 #include "DetectorDescription/Core/interface/DDCompactView.h" 00006 00007 #include <iomanip> 00008 #include <fstream> 00009 00010 #include "G4TransportationManager.hh" 00011 #include "G4Navigator.hh" 00012 #include "G4VTouchable.hh" 00013 #include "G4TouchableHistory.hh" 00014 00015 #include "FWCore/MessageLogger/interface/MessageLogger.h" 00016 using std::vector; 00017 using std::string; 00018 using std::map; 00019 00020 //#define DEBUG 00021 00022 void TouchableToHistory::buildAll(){ 00023 if (alreadySet == true) return; 00024 alreadySet = true; 00025 00026 TrackerMapDDDtoID dddToID(myGeomDet); 00027 std::vector<nav_type> allSensitiveDets = dddToID.allNavTypes(); 00028 edm::LogInfo("TrackerSimInfoNumbering")<<" TouchableTo History: got "<<allSensitiveDets.size()<<" sensitive detectors from TrackerMapDDDtoID."; 00029 //DDCompactView cv; 00030 DDExpandedView view(*myCompactView); 00031 G4Navigator* theStdNavigator = G4TransportationManager::GetTransportationManager()->GetNavigatorForTracking(); 00032 G4Navigator* theNavigator = new G4Navigator(); 00033 theNavigator->SetWorldVolume(theStdNavigator->GetWorldVolume()); 00034 00035 for (std::vector<nav_type>::iterator it = allSensitiveDets.begin(); it != allSensitiveDets.end(); it++){ 00036 view.goTo(*it); 00037 DDTranslation t =view.translation(); 00038 theNavigator->LocateGlobalPointAndSetup(G4ThreeVector(t.x(),t.y(),t.z())); 00039 G4TouchableHistory * hist = theNavigator->CreateTouchableHistory(); 00040 TouchableToHistory::Nav_Story st = touchableToNavStory(hist); 00041 00042 #ifdef DEBUG 00043 u_int32_t oldsize = myDirectMap.size(); 00044 #endif 00045 00046 myMap[st] = *it; 00047 myDirectMap[st] = dddToID.id(*it); 00048 00049 /* 00050 #ifdef DEBUG 00051 LogDebug("TrackerSimDebugNumbering")<< " INSERTING "<<view.logicalPart().name()<<" "<<t<<" "<<hist->GetVolume()->GetLogicalVolume()->GetName(); 00052 LogDebug("TrackerSimDebugNumbering")<<" Sensitive: "<<hist->GetVolume()->GetLogicalVolume()->GetSensitiveDetector()<<std::endl; 00053 LogDebug("TrackerSimDebugNumbering")<<"Now size is "<<myDirectMap.size()<<std::endl; 00054 if (oldsize == myDirectMap.size()) 00055 edm::LogError("TrackerSimInfoNumbering")<< "Touchable to History Error!!!!"; 00056 dumpG4VPV(hist); 00057 #endif 00058 */ 00059 delete hist; 00060 00061 } 00062 edm::LogInfo("TrackerSimInfoNumbering")<<" TouchableToHistory: mapped "<<myDirectMap.size()<<" detectors to G4."; 00063 00064 if (myDirectMap.size() != allSensitiveDets.size()){ 00065 edm::LogError("TrackerSimInfoNumbering")<<" ERROR: DDD sensitive detectors do not match Geant4 ones."; 00066 abort(); 00067 } 00068 00069 delete theNavigator; 00070 00071 } 00072 00073 DDFilteredView& TouchableToHistory::getFilteredView(const G4VTouchable& t, DDFilteredView& f){ 00074 if (alreadySet == false) 00075 buildAll(); 00076 f.goTo(myMap[touchableToNavStory(&t)]); 00077 return f; 00078 } 00079 TouchableToHistory::nav_type TouchableToHistory::getNavType(const G4VTouchable& t){ 00080 if (alreadySet == false) 00081 edm::LogError("TrackerSimInfoNumbering")<<" NOT READY "; 00082 return myMap[touchableToNavStory(&t)]; 00083 } 00084 00085 TouchableToHistory::Nav_Story TouchableToHistory::getNavStory(DDFilteredView& i){ 00086 if (alreadySet == false) buildAll(); 00087 DDTranslation t = i.translation(); 00088 00089 G4Navigator* theStdNavigator = G4TransportationManager::GetTransportationManager()->GetNavigatorForTracking(); 00090 G4Navigator* theNavigator = new G4Navigator(); 00091 theNavigator->SetWorldVolume(theStdNavigator->GetWorldVolume()); 00092 00093 theNavigator->LocateGlobalPointAndSetup(G4ThreeVector(t.x(),t.y(),t.z())); 00094 G4TouchableHistory* hist = theNavigator->CreateTouchableHistory(); 00095 TouchableToHistory::Nav_Story temp = touchableToNavStory(hist); 00096 delete hist; 00097 delete theNavigator; 00098 return (temp); 00099 } 00100 00101 TouchableToHistory::Nav_Story TouchableToHistory::touchableToNavStory(const G4VTouchable *v) { 00102 static G4String tobinactive("TOBInactive"); 00103 Nav_Story temp; 00104 #ifdef DEBUG 00105 std::vector<int> debugint; 00106 std::vector<std::string> debugstring; 00107 #endif 00108 int levels = v->GetHistoryDepth(); 00109 00110 for (int k=0; k<=levels; k++){ 00111 if (v->GetVolume(k)->GetLogicalVolume()->GetName() != tobinactive) { 00112 temp.push_back( 00113 std::pair<int,std::string> 00114 (v->GetVolume(k)->GetCopyNo(), 00115 v->GetVolume(k)->GetLogicalVolume()->GetName())); 00116 #ifdef DEBUG 00117 debugint.push_back(v->GetVolume(k)->GetCopyNo()); 00118 debugstring.push_back(v->GetVolume(k)->GetLogicalVolume()->GetName()); 00119 #endif 00120 } 00121 } 00122 #ifdef DEBUG 00123 // LogDebug("TrackerSimDebugNumbering")<<" G4 TouchableToHistory "<< debugint; 00124 for(u_int32_t jj=0;jj<debugstring.size();jj++)LogDebug("TrackerSimDebugNumbering")<<" "<<debugstring[jj]; 00125 #endif 00126 return temp; 00127 } 00128 00129 TouchableToHistory::nav_type TouchableToHistory::touchableToNavType(const G4VTouchable* v) { 00130 if (alreadySet == false) 00131 buildAll(); 00132 LogDebug("TrackerSimDebugNumbering")<<" NAME : "<<v->GetVolume()->GetLogicalVolume()->GetName(); 00133 dumpG4VPV(v); 00134 return myMap[touchableToNavStory(v)]; 00135 } 00136 int TouchableToHistory::touchableToInt(const G4VTouchable* v) { 00137 if (alreadySet == false) 00138 buildAll(); 00139 LogDebug("TrackerSimDebugNumbering")<<" NAME : "<<v->GetVolume()->GetLogicalVolume()->GetName(); 00140 dumpG4VPV(v); 00141 LogDebug("TrackerSimDebugNumbering")<<" Returning: "<< myDirectMap[touchableToNavStory(v)]<<std::endl; 00142 00143 return myDirectMap[touchableToNavStory(v)]; 00144 } 00145 00146 void TouchableToHistory::dumpG4VPV(const G4VTouchable* v){ 00147 int levels = v->GetHistoryDepth(); 00148 00149 for (int k=0; k<=levels; k++){ 00150 LogDebug("TrackerSimInfoNumbering") <<" Hist: "<< v->GetVolume(k)->GetLogicalVolume()->GetName()<< 00151 " Copy "<< v->GetVolume(k)->GetCopyNo(); 00152 } 00153 } 00154