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 
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){
36  TheOpDetLookup = new OpDetLookup;
37  }
38  return TheOpDetLookup;
39  }
40 
41  //--------------------------------------------------
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  {
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 }
static constexpr double cm
Definition: Units.h:68
OpDetGeo const & OpDetGeoFromOpDet(unsigned int OpDet) const
Returns the geo::OpDetGeo object for the given detector number.
std::string string
Definition: nybbler.cc:12
static const std::string volume[nvol]
Geant4 interface.
std::map< std::string, int > fTheOpDetMap
Definition: OpDetLookup.h:60
void GetCenter(double *xyz, double localz=0.0) const
Definition: OpDetGeo.cxx:40
art framework interface to geometry description
void AddPhysicalVolume(G4VPhysicalVolume *)
Definition: OpDetLookup.cxx:96
double distance(double x1, double y1, double z1, double x2, double y2, double z2)
static OpDetLookup * Instance()
Definition: OpDetLookup.cxx:33
int FindClosestOpDet(G4VPhysicalVolume *vol, double &Distance)
Definition: OpDetLookup.cxx:57
unsigned int NOpDets() const
Number of OpDets in the whole detector.
OpDetLookup * TheOpDetLookup
Definition: OpDetLookup.cxx:24
Encapsulate the geometry of an optical detector.
int GetOpDet(G4VPhysicalVolume *)
Definition: OpDetLookup.cxx:48
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33