All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OpDetLookup.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 /// \file OpDetLookup.cxx
3 //
4 /// \author bjpjones@mit.edu
5 ////////////////////////////////////////////////////////////////////////
6 // Implementation of the OpDetLookup class.
7 //
8 // See comments in the OpDetLookup.h file.
9 //
10 // Ben Jones, MIT, 06/04/2010
11 //
12 
13 
14 #include "art/Framework/Services/Registry/ServiceHandle.h"
15 
16 #include "Geant4/G4VPhysicalVolume.hh"
17 
21 
22 
23 namespace larg4 {
25 
26  //--------------------------------------------------
28  {
29  fTheTopOpDet=0;
30  }
31 
32  //--------------------------------------------------
34  {
35  if(!TheOpDetLookup){
37  }
38  return TheOpDetLookup;
39  }
40 
41  //--------------------------------------------------
42  int OpDetLookup::GetOpDet(std::string TheName)
43  {
44  return fTheOpDetMap[TheName];
45  }
46 
47  //--------------------------------------------------
48  int OpDetLookup::GetOpDet(G4VPhysicalVolume* TheVolume)
49  {
50  std::string TheName = TheVolume->GetName();
51  return GetOpDet(TheName);
52  }
53 
54 
55  //--------------------------------------------------
56 
57  int OpDetLookup::FindClosestOpDet(G4VPhysicalVolume* vol, double& distance)
58  {
59  art::ServiceHandle<geo::Geometry const> geom;
60  int OpDetCount = 0;
61 
62  double MinDistance = UINT_MAX;
63  int ClosestOpDet = -1;
64 
65  for(size_t o=0; o!=geom->NOpDets(); o++) {
66  double xyz[3];
67  geom->OpDetGeoFromOpDet(o).GetCenter(xyz);
68 
69  CLHEP::Hep3Vector DetPos(xyz[0],xyz[1],xyz[2]);
70  CLHEP::Hep3Vector ThisVolPos = vol->GetTranslation();
71 
72  ThisVolPos/=CLHEP::cm;
73 
74  // std::cout<<"Det: " << xyz[0]<< " " <<xyz[1]<< " " << xyz[2]<<std::endl;
75  // std::cout<<"Vol: " << ThisVolPos.x()<< " " <<ThisVolPos.y() << " " <<ThisVolPos.z()<<std::endl;
76 
77  double Distance = (DetPos-ThisVolPos).mag();
78  if(Distance < MinDistance)
79  {
80  MinDistance = Distance;
81  ClosestOpDet = o;
82  }
83  OpDetCount++;
84  }
85  if(ClosestOpDet<0)
86  {
87  throw cet::exception("OpDetLookup Error") << "No nearby OpDet found!\n";
88  }
89 
90  distance = MinDistance;
91  return ClosestOpDet;
92  }
93 
94 
95  //--------------------------------------------------
96  void OpDetLookup::AddPhysicalVolume(G4VPhysicalVolume * volume)
97  {
98 
99  // mf::LogInfo("Optical") <<"G4 placing sensitive opdet"<<std::endl;
100 
101  std::stringstream VolName("");
102  double Distance = 0;
103 
104  int NearestOpDet = FindClosestOpDet(volume, Distance);
105 
106  VolName.flush();
107  VolName << volume->GetName() << "_" << NearestOpDet;
108  volume->SetName(VolName.str().c_str());
109 
110  fTheOpDetMap[VolName.str()] = NearestOpDet;
111 
112  // mf::LogInfo("Optical") << "Found closest volume: " << VolName.str().c_str() << " OpDet : " << fTheOpDetMap[VolName.str()]<<" distance : " <<Distance<<std::endl;
113 
114  }
115 
116 
117  //--------------------------------------------------
119  {
120  return fTheTopOpDet;
121  }
122 
123 }
std::map< std::string, int > fTheOpDetMap
Definition: OpDetLookup.h:60
double distance(geo::Point_t const &point, CathodeDesc_t const &cathode)
Returns the distance of a point from the cathode.
void AddPhysicalVolume(G4VPhysicalVolume *)
Definition: OpDetLookup.cxx:96
static OpDetLookup * Instance()
Definition: OpDetLookup.cxx:33
int FindClosestOpDet(G4VPhysicalVolume *vol, double &Distance)
Definition: OpDetLookup.cxx:57
OpDetLookup * TheOpDetLookup
Definition: OpDetLookup.cxx:24
Encapsulate the geometry of an optical detector.
int GetOpDet(G4VPhysicalVolume *)
Definition: OpDetLookup.cxx:48
art framework interface to geometry description