LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
PhotonVoxels.cxx
Go to the documentation of this file.
2 
3 #include <iostream>
4 
5 namespace sim {
6 
7 
8  // PhotonVoxel class
9  //----------------------------------------------------------------------------
11  double xMax,
12  double yMin,
13  double yMax,
14  double zMin,
15  double zMax,
16  int N)
17  {
18  xVoxelMin = xMin;
19  xVoxelMax = xMax;
20  yVoxelMin = yMin;
21  yVoxelMax = yMax;
22  zVoxelMin = zMin;
23  zVoxelMax = zMax;
24  NPhotons = N;
25  }
26 
27  //----------------------------------------------------------------------------
29  {
30  }
31 
32  //----------------------------------------------------------------------------
33  TVector3 PhotonVoxel::GetLowerCorner() const
34  {
35  TVector3 LowerCorner = TVector3(xVoxelMin, yVoxelMin, zVoxelMin);
36  return LowerCorner;
37  }
38 
39 
40  //----------------------------------------------------------------------------
41  TVector3 PhotonVoxel::GetUpperCorner() const
42  {
43  TVector3 UpperCorner = TVector3(xVoxelMax, yVoxelMax, zVoxelMax);
44  return UpperCorner;
45  }
46 
47 
48  //----------------------------------------------------------------------------
49  TVector3 PhotonVoxel::GetCenter() const
50  {
51  TVector3 Center = TVector3((xVoxelMin+xVoxelMax)/2.0, (yVoxelMin+yVoxelMax)/2.0, (zVoxelMin+zVoxelMax)/2.0);
52  return Center;
53  }
54 
55 
56  //----------------------------------------------------------------------------
58  double xMax,
59  int xN,
60  double yMin,
61  double yMax,
62  int yN,
63  double zMin,
64  double zMax,
65  int zN)
66  {
67  fxSteps = xN;
68  fySteps = yN;
69  fzSteps = zN;
70 
71 
72  fLowerCorner = TVector3(xMin,yMin,zMin);
73  fUpperCorner = TVector3(xMax,yMax,zMax);
74  }
75 
76  //----------------------------------------------------------------------------
78  {
79  }
80 
81  //----------------------------------------------------------------------------
83  {
84  return fLowerCorner;
85  }
86 
87  //----------------------------------------------------------------------------
89  {
90  return fUpperCorner;
91  }
92 
93  //----------------------------------------------------------------------------
94  TVector3 PhotonVoxelDef::GetSteps() const
95  {
96  TVector3 Steps = TVector3(fxSteps, fySteps, fzSteps);
97  return Steps;
98  }
99 
100  //----------------------------------------------------------------------------
102  {
103  return ( ( GetRegionUpperCorner() == right.GetRegionUpperCorner() ) &&
104  ( GetRegionLowerCorner() == right.GetRegionLowerCorner() ) &&
105  ( GetSteps() == right.GetSteps()) );
106  }
107 
108  //----------------------------------------------------------------------------
110  {
111  return fxSteps * fySteps * fzSteps;
112  }
113 
114  //----------------------------------------------------------------------------
115  int PhotonVoxelDef::GetVoxelID(const TVector3& p) const
116  {
117  const double xyz[3] = {p.X(), p.Y(), p.Z()};
118  return GetVoxelID(xyz);
119  }
120 
121  //----------------------------------------------------------------------------
122  int PhotonVoxelDef::GetVoxelID(double const* Position) const
123  {
124  // figure out how many steps this point is in the x,y,z directions
125  int xStep = int ((Position[0]-fLowerCorner[0]) / (fUpperCorner[0]-fLowerCorner[0]) * fxSteps );
126  int yStep = int ((Position[1]-fLowerCorner[1]) / (fUpperCorner[1]-fLowerCorner[1]) * fySteps );
127  int zStep = int ((Position[2]-fLowerCorner[2]) / (fUpperCorner[2]-fLowerCorner[2]) * fzSteps );
128 
129  // check if point lies within the voxelized region
130  if((0 <= xStep) && (xStep < fxSteps) &&
131  (0 <= yStep) && (yStep < fySteps) &&
132  (0 <= zStep) && (zStep < fzSteps) ){
133  // if within bounds, generate the voxel ID
134  return (xStep
135  + yStep * (fxSteps)
136  + zStep * (fxSteps * fySteps));
137  }
138  else{
139  // out of bounds
140  return -1;
141  }
142  }
143 
144  //----------------------------------------------------------------------------
145  void PhotonVoxelDef::
146  GetNeighboringVoxelIDs(const TVector3& v, std::vector<NeiInfo>& ret) const
147  {
148  ret.clear();
149  ret.reserve(8);
150 
151  // Position in voxel coordinates including floating point part
152  double rStepD[3];
153  for(int i = 0; i < 3; ++i){
154  // If we're outside the cuboid we have values for, return empty vector,
155  // ie failure.
156  if(v[i] < fLowerCorner[i] || v[i] > fUpperCorner[i]) return;// {};
157  // Figure out our position wrt to the centres of the voxels
158  rStepD[i] = ((v[i]-fLowerCorner[i]) / (fUpperCorner[i]-fLowerCorner[i]) * GetSteps()[i] ) - 0.5;
159  }
160 
161  // The neighbours are the 8 corners of a cube around this point
162  for(int dx = 0; dx <= 1; ++dx){
163  for(int dy = 0; dy <= 1; ++dy){
164  for(int dz = 0; dz <= 1; ++dz){
165  // The full 3D step
166  const int dr[3] = {dx, dy, dz};
167 
168  // The integer-only position of the current corner
169  int rStepI[3];
170  for(int d = 0; d < 3; ++d){
171  // Round down to get the "lower left" corner
172  rStepI[d] = int(rStepD[d]);
173  // Ensure we'll stay in-bounds
174  rStepI[d] = std::max(0, rStepI[d]);
175  rStepI[d] = std::min(rStepI[d], int(GetSteps()[d])-2);
176  // Adjust to the corner we're actually considering
177  rStepI[d] += dr[d];
178  }
179 
180  double w = 1;
181  for(int d = 0; d < 3; ++d){
182  // These expressions will interpolate when between the 8 corners,
183  // and extrapolate in the half-voxel space around the edges.
184  if(dr[d] == 0)
185  w *= 1+rStepI[d]-rStepD[d];
186  else
187  w *= 1-rStepI[d]+rStepD[d];
188  }
189 
190  const int id = (rStepI[0] +
191  rStepI[1] * (fxSteps) +
192  rStepI[2] * (fxSteps * fySteps));
193 
194  ret.emplace_back(id, w);
195  }
196  }
197  }
198 
199  // Sanity check the weights sum to 1
200  double wSum = 0;
201  for(const NeiInfo& n: ret) wSum += n.weight;
202  if(fabs(wSum-1) > 1e-3){
203  std::cout << "PhotonVoxelDef::GetNeighboringVoxelIDs(): "
204  << "Weights sum to " << wSum << " (should be 1). "
205  << "Weights are:";
206  for(const NeiInfo& n: ret) std::cout << " " << n.weight;
207  std::cout << " Aborting." << std::endl;
208  abort();
209  }
210  }
211 
212  //----------------------------------------------------------------------------
214  {
215  TVector3 TheSize = TVector3((GetRegionUpperCorner()[0]-GetRegionLowerCorner()[0]) / fxSteps,
216  (GetRegionUpperCorner()[1]-GetRegionLowerCorner()[1]) / fySteps,
217  (GetRegionUpperCorner()[2]-GetRegionLowerCorner()[2]) / fzSteps);
218  return TheSize;
219  }
220 
221 
222  //----------------------------------------------------------------------------
224  {
225  // float TempID = (float) ID;
226 
227  // Decompose ID into steps in each direction
228  int xStep = ID % fxSteps ;
229  int yStep = ((ID - xStep ) / fxSteps) % fySteps ;
230  int zStep = ((ID - xStep - (yStep * fxSteps)) / (fySteps * fxSteps)) % fzSteps ;
231 
232 
233  TVector3 VoxelSize = GetVoxelSize();
234 
235  double xMin = VoxelSize[0] * (xStep) + fLowerCorner[0];
236  double xMax = VoxelSize[0] * (xStep+1) + fLowerCorner[0];
237  double yMin = VoxelSize[1] * (yStep) + fLowerCorner[1];
238  double yMax = VoxelSize[1] * (yStep+1) + fLowerCorner[1];
239  double zMin = VoxelSize[2] * (zStep) + fLowerCorner[2];
240  double zMax = VoxelSize[2] * (zStep+1) + fLowerCorner[2];
241 
242 
243 
244  return PhotonVoxel(xMin, xMax, yMin, yMax, zMin, zMax);
245  }
246 
247  //----------------------------------------------------------------------------
249  {
250  return (( ID > -1) && (ID<GetNVoxels()));
251  }
252 
253  std::vector<int> PhotonVoxelDef::GetVoxelCoords(int ID) const
254  {
255  std::vector<int> ReturnVector;
256  ReturnVector.resize(3);
257  ReturnVector.at(0) = ID % fxSteps ;
258  ReturnVector.at(1) = ((ID - ReturnVector.at(0) ) / fxSteps) % fySteps ;
259  ReturnVector.at(2) = ((ID - ReturnVector.at(0) - (ReturnVector.at(1) * fxSteps)) / (fySteps * fxSteps)) % fzSteps ;
260  return ReturnVector;
261 
262  }
263 }
void GetNeighboringVoxelIDs(const TVector3 &v, std::vector< NeiInfo > &ret) const
constexpr auto const & right(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:112
TVector3 GetSteps() const
bool operator==(const PhotonVoxelDef &rhs) const
TVector3 GetRegionLowerCorner() const
TVector3 GetCenter() const
TVector3 GetUpperCorner() const
TVector3 GetRegionUpperCorner() const
int GetNVoxels() const
Int_t max
Definition: plot.C:27
TVector3 GetLowerCorner() const
TVector3 GetVoxelSize() const
Float_t d
Definition: plot.C:237
int GetVoxelID(const TVector3 &) const
Monte Carlo Simulation.
bool IsLegalVoxelID(int) const
std::vector< int > GetVoxelCoords(int ID) const
Int_t min
Definition: plot.C:26
Char_t n[5]
Float_t e
Definition: plot.C:34
Float_t w
Definition: plot.C:23
PhotonVoxel GetPhotonVoxel(int ID) const