LArSoft  v07_13_02
Liquid Argon Software toolkit - http://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 
13 
19 
20 
21 namespace larg4 {
23 
24  //--------------------------------------------------
26  {
27  fTheTopOpDet=0;
28  }
29 
30  //--------------------------------------------------
32  {
33  if(!TheOpDetLookup){
34  TheOpDetLookup = new OpDetLookup;
35  }
36  return TheOpDetLookup;
37  }
38 
39  //--------------------------------------------------
40  int OpDetLookup::GetOpDet(std::string TheName)
41  {
42  return fTheOpDetMap[TheName];
43  }
44 
45  //--------------------------------------------------
46  int OpDetLookup::GetOpDet(G4VPhysicalVolume* TheVolume)
47  {
48  std::string TheName = TheVolume->GetName();
49  return GetOpDet(TheName);
50  }
51 
52 
53  //--------------------------------------------------
54 
55  int OpDetLookup::FindClosestOpDet(G4VPhysicalVolume* vol, double& distance)
56  {
58  int OpDetCount = 0;
59 
60  double MinDistance = UINT_MAX;
61  int ClosestOpDet = -1;
62 
63  for(size_t o=0; o!=geom->NOpDets(); o++) {
64  double xyz[3];
65  geom->OpDetGeoFromOpDet(o).GetCenter(xyz);
66 
67  CLHEP::Hep3Vector DetPos(xyz[0],xyz[1],xyz[2]);
68  CLHEP::Hep3Vector ThisVolPos = vol->GetTranslation();
69 
70  ThisVolPos/=CLHEP::cm;
71 
72  // std::cout<<"Det: " << xyz[0]<< " " <<xyz[1]<< " " << xyz[2]<<std::endl;
73  // std::cout<<"Vol: " << ThisVolPos.x()<< " " <<ThisVolPos.y() << " " <<ThisVolPos.z()<<std::endl;
74 
75  double Distance = (DetPos-ThisVolPos).mag();
76  if(Distance < MinDistance)
77  {
78  MinDistance = Distance;
79  ClosestOpDet = o;
80  }
81  OpDetCount++;
82  }
83  if(ClosestOpDet<0)
84  {
85  throw cet::exception("OpDetLookup Error") << "No nearby OpDet found!\n";
86  }
87 
88  distance = MinDistance;
89  return ClosestOpDet;
90  }
91 
92 
93  //--------------------------------------------------
94  void OpDetLookup::AddPhysicalVolume(G4VPhysicalVolume * volume)
95  {
96 
97  // mf::LogInfo("Optical") <<"G4 placing sensitive opdet"<<std::endl;
98 
99  std::stringstream VolName("");
100  double Distance = 0;
101 
102  int NearestOpDet = FindClosestOpDet(volume, Distance);
103 
104  VolName.flush();
105  VolName << volume->GetName() << "_" << NearestOpDet;
106  volume->SetName(VolName.str().c_str());
107 
108  fTheOpDetMap[VolName.str()] = NearestOpDet;
109 
110  // mf::LogInfo("Optical") << "Found closest volume: " << VolName.str().c_str() << " OpDet : " << fTheOpDetMap[VolName.str()]<<" distance : " <<Distance<<std::endl;
111 
112  }
113 
114 
115  //--------------------------------------------------
117  {
118  return fTheTopOpDet;
119  }
120 
121 }
OpDetGeo const & OpDetGeoFromOpDet(unsigned int OpDet) const
Number of OpDets in the whole detector.
Encapsulate the construction of a single cyostat.
Geant4 interface.
std::map< std::string, int > fTheOpDetMap
Definition: OpDetLookup.h:60
void GetCenter(double *xyz, double localz=0.0) const
Definition: OpDetGeo.cxx:47
void AddPhysicalVolume(G4VPhysicalVolume *)
Definition: OpDetLookup.cxx:94
static OpDetLookup * Instance()
Definition: OpDetLookup.cxx:31
int FindClosestOpDet(G4VPhysicalVolume *vol, double &Distance)
Definition: OpDetLookup.cxx:55
unsigned int NOpDets() const
Number of OpDets in the whole detector.
OpDetLookup * TheOpDetLookup
Definition: OpDetLookup.cxx:22
Encapsulate the geometry of an optical detector.
int GetOpDet(G4VPhysicalVolume *)
Definition: OpDetLookup.cxx:46
art framework interface to geometry description
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33