LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
OpDetLookup.cxx
Go to the documentation of this file.
1 //
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 
14 
15 #include "Geant4/G4VPhysicalVolume.hh"
16 
20 
21 namespace larg4 {
23 
24  //--------------------------------------------------
26  {
27  fTheTopOpDet = 0;
28  }
29 
30  //--------------------------------------------------
32  {
33  if (!TheOpDetLookup) { TheOpDetLookup = new OpDetLookup; }
34  return TheOpDetLookup;
35  }
36 
37  //--------------------------------------------------
38  int OpDetLookup::GetOpDet(std::string const& TheName)
39  {
40  return fTheOpDetMap[TheName];
41  }
42 
43  //--------------------------------------------------
44  int OpDetLookup::GetOpDet(G4VPhysicalVolume const* TheVolume)
45  {
46  return GetOpDet(TheVolume->GetName());
47  }
48 
49  //--------------------------------------------------
50 
51  int OpDetLookup::FindClosestOpDet(G4VPhysicalVolume* vol, double& distance)
52  {
54  int OpDetCount = 0;
55 
56  double MinDistance = UINT_MAX;
57  int ClosestOpDet = -1;
58 
59  for (size_t o = 0; o != geom->NOpDets(); o++) {
60  auto const xyz = geom->OpDetGeoFromOpDet(o).GetCenter();
61 
62  CLHEP::Hep3Vector DetPos(xyz.X(), xyz.Y(), xyz.Z());
63  CLHEP::Hep3Vector ThisVolPos = vol->GetTranslation();
64 
65  ThisVolPos /= CLHEP::cm;
66 
67  double Distance = (DetPos - ThisVolPos).mag();
68  if (Distance < MinDistance) {
69  MinDistance = Distance;
70  ClosestOpDet = o;
71  }
72  OpDetCount++;
73  }
74  if (ClosestOpDet < 0) {
75  throw cet::exception("OpDetLookup Error") << "No nearby OpDet found!\n";
76  }
77 
78  distance = MinDistance;
79  return ClosestOpDet;
80  }
81 
82  //--------------------------------------------------
83  void OpDetLookup::AddPhysicalVolume(G4VPhysicalVolume* volume)
84  {
85  std::stringstream VolName("");
86  double Distance = 0;
87 
88  int NearestOpDet = FindClosestOpDet(volume, Distance);
89 
90  VolName.flush();
91  VolName << volume->GetName() << "_" << NearestOpDet;
92  volume->SetName(VolName.str().c_str());
93 
94  fTheOpDetMap[VolName.str()] = NearestOpDet;
95  }
96 
97  //--------------------------------------------------
98  int OpDetLookup::GetN() const
99  {
100  return fTheTopOpDet;
101  }
102 
103 }
OpDetGeo const & OpDetGeoFromOpDet(unsigned int OpDet) const
Returns the geo::OpDetGeo object for the given detector number.
Geant4 interface.
std::map< std::string, int > fTheOpDetMap
Definition: OpDetLookup.h:57
int GetOpDet(G4VPhysicalVolume const *)
Definition: OpDetLookup.cxx:44
void AddPhysicalVolume(G4VPhysicalVolume *)
Definition: OpDetLookup.cxx:83
static OpDetLookup * Instance()
Definition: OpDetLookup.cxx:31
int FindClosestOpDet(G4VPhysicalVolume *vol, double &Distance)
Definition: OpDetLookup.cxx:51
unsigned int NOpDets() const
Number of OpDets in the whole detector.
OpDetLookup * TheOpDetLookup
Definition: OpDetLookup.cxx:22
Encapsulate the geometry of an optical detector.
geo::Point_t const & GetCenter() const
Definition: OpDetGeo.h:72
int GetN() const
Definition: OpDetLookup.cxx:98
art framework interface to geometry description
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33