All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PhotonVisibilityService.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // \file PhotonVisibilityService_service.cc
4 //
5 ////////////////////////////////////////////////////////////////////////
6 //
7 // Ben Jones, MIT 2012
8 //
9 // This service reports the visibility of a particular point in
10 // the detector to each OpDet. This is used by the fast
11 // optical simulation and by track-light association algorithms.
12 //
13 // Visibility is defined as the fraction of isotropically produced
14 // photons from a detector voxel which are expected to reach the
15 // OpDet in question.
16 //
17 // This information is lookup up from a previousely generated
18 // optical library file, whose path is specified to this service.
19 //
20 // Note that it is important that the voxelization schemes match
21 // between the library and the service instance for sensible results.
22 //
23 //
24 // Framework includes
25 
26 // LArSoft includes
28 #include "PhotonLibrary.h"
29 #include <iostream>
30 #include <stdlib.h>
31 #include <stdio.h>
32 //#include "messagefacility/MessageLogger/MessageLogger.h"
33 //#include "Geometry/Geometry.h"
34 //#include "Geometry/CryostatGeo.h"
35 //#include "Geometry/OpDetGeo.h"
36 #include <chrono>
37 
38 using namespace std::chrono;
39 namespace phot{
40 
41  PhotonVisibilityService* PhotonVisibilityService::_me = nullptr;
42 
43  //--------------------------------------------------------------------
44  /*
45  PhotonVisibilityService::PhotonVisibilityService(fhicl::ParameterSet const& pset, art::ActivityRegistry &) :
46  fCurrentVoxel(0),
47  fCurrentValue(0.),
48  fXmin(0.),
49  fXmax(0.),
50  fYmin(0.),
51  fYmax(0.),
52  fZmin(0.),
53  fZmax(0.),
54  fNx(0),
55  fNy(0),
56  fNz(0),
57  fUseCryoBoundary(false),
58  fLibraryBuildJob(false),
59  fDoNotLoadLibrary(false),
60  fParameterization(false),
61  fTheLibrary(0)
62  {
63  this->reconfigure(pset);
64  mf::LogInfo("PhotonVisibilityService")<<"PhotonVisbilityService initializing"<<std::endl;
65  }
66  */
67 
68  //--------------------------------------------------------------------
69  //void PhotonVisibilityService::reconfigure(fhicl::ParameterSet const& p)
70  PhotonVisibilityService::PhotonVisibilityService(std::string library) :
71  fCurrentVoxel(0),
72  fCurrentValue(0.),
73  fXmin( -395.0 ),
74  fXmax( -45.0 ),
75  fYmin( -215.2 ),
76  fYmax( 174.8 ),
77  fZmin( -995.0 ),
78  fZmax( 965.0 ),
79  fNx(70),
80  fNy(78),
81  fNz(392),
82  fNOpDetChannels(180),
83  fUseCryoBoundary(true),
84  fLibraryBuildJob(false),
85  fDoNotLoadLibrary(false),
86  fParameterization(false),
87  fLibraryFile(library),
88  fTheLibrary(nullptr)
89  {
91  return;
92  }
93 
94  std::vector<std::vector<float> >
96  std::vector<std::vector<float> > result(fNy,std::vector<float>(fNz,0.));
97  if(x < fXmin || x > fXmax) return result;
98 
99  if(fTheLibrary == 0)
100  LoadLibrary();
101 
102  for(int iy=0; iy<fNy; ++iy) {
103  for(int iz=0; iz<fNz; ++iz) {
104  int vox_id = iy*fNx + iz * (fNy + fNx);
105  double vis_sum = 0.;
106  for(auto const& vis_pmt : *(fTheLibrary->GetCounts(vox_id)))
107  vis_sum += ((double)(vis_pmt));
108  result[iy][iz] = vis_sum;
109  }
110  }
111  return result;
112  }
113 
114  std::vector<std::vector<float> >
116  std::vector<std::vector<float> > result(fNz,std::vector<float>(fNx,0.));
117  if(y < fYmin || y > fYmax) return result;
118 
119  if(fTheLibrary == 0)
120  LoadLibrary();
121 
122  for(int ix=0; ix<fNx; ++ix) {
123  for(int iz=0; iz<fNz; ++iz) {
124  int vox_id = ix + iz * (fNy + fNx);
125  double vis_sum = 0.;
126  for(auto const& vis_pmt : *(fTheLibrary->GetCounts(vox_id)))
127  vis_sum += ((double)(vis_pmt));
128  result[iz][ix] = vis_sum;
129  }
130  }
131  return result;
132  }
133 
134  std::vector<std::vector<float> >
136  std::vector<std::vector<float> > result(fNx,std::vector<float>(fNy,0.));
137  if(z < fZmin || z > fZmax) return result;
138 
139  if(fTheLibrary == 0)
140  LoadLibrary();
141 
142  for(int ix=0; ix<fNx; ++ix) {
143  for(int iy=0; iy<fNy; ++iy) {
144  int vox_id = ix + iy * fNx;
145  double vis_sum = 0.;
146  for(auto const& vis_pmt : *(fTheLibrary->GetCounts(vox_id)))
147  vis_sum += ((double)(vis_pmt));
148  result[ix][iy] = vis_sum;
149  }
150  }
151  return result;
152  }
153 
155  { assert(frac>=0. && frac <=1.); return fXmin + (fXmax - fXmin) * frac; }
157  { assert(frac>=0. && frac <=1.); return fYmin + (fYmax - fYmin) * frac; }
159  { assert(frac>=0. && frac <=1.); return fZmin + (fZmax - fZmin) * frac; }
160 
161  //--------------------------------------------------------------------
163  {
164  // Don't do anything if the library has already been loaded.
165  std::cout<<"Loading library..."<<std::endl;
166  if(fTheLibrary == 0) {
167  fTheLibrary = new PhotonLibrary();
168 
169 
171 
172  std::string LibraryFileWithPath;
173  if ( fLibraryFile.front()!='/' ) {
174  // relative path provided. use designated folder in larlite
175  LibraryFileWithPath = std::string(getenv("FMATCH_DATADIR"));
176  LibraryFileWithPath += "/";
177  LibraryFileWithPath += fLibraryFile;
178  }
179  else {
180  // absolute path
181  LibraryFileWithPath = fLibraryFile;
182  }
183  if(!fParameterization) {
184  std::cout << "PhotonVisibilityService Loading photon library from file "
185  << LibraryFileWithPath
186  << std::endl;
187  size_t NVoxels = GetVoxelDef().GetNVoxels();
188  fTheLibrary->LoadLibraryFromFile(LibraryFileWithPath, NVoxels);
189  }
190  }
191  else {
192  // building library, so creating an empty one
193  //art::ServiceHandle<geo::Geometry> geom;
194 
195  size_t NVoxels = GetVoxelDef().GetNVoxels();
196  std::cout << " Vis service running library build job. Please ensure "
197  << " job contains LightSource, LArG4, SimPhotonCounter"<<std::endl;
198  fTheLibrary->CreateEmptyLibrary(NVoxels, fNOpDetChannels);
199  }
200  }
201  }
202 
203  //--------------------------------------------------------------------
205  {
206  if(fTheLibrary == 0)
207  LoadLibrary();
208 
209  if(fLibraryBuildJob )
210  {
211  std::cout<< " Vis service "
212  << " Storing Library entries to file..." <<std::endl;
213  fTheLibrary->StoreLibraryToFile(fLibraryFile);
214  }
215  }
216 
217 
218  //------------------------------------------------------
219 
220  // Eventually we will calculate the light quenching factor here
221  /*
222  double PhotonVisibilityService::GetQuenchingFactor(double)
223  {
224  // for now, no quenching
225  return 1.0;
226 
227  }
228  */
229 
230  //------------------------------------------------------
231 
232  // Get a vector of the relative visibilities of each OpDet
233  // in the event to a point xyz
234 
235  const std::vector<float>* PhotonVisibilityService::GetAllVisibilities(double * xyz) const
236  {
237  int VoxID = fVoxelDef.GetVoxelID(xyz);
238  return GetLibraryEntries(VoxID);
239  }
240 
241 
242  //------------------------------------------------------
243 
244  // Get distance to optical detector OpDet
245  /*
246  double PhotonVisibilityService::DistanceToOpDet( double* xyz, unsigned int OpDet )
247  {
248  art::ServiceHandle<geo::Geometry> geom;
249  return geom->OpDetGeoFromOpDet(OpDet).DistanceToPoint(xyz);
250  }
251  */
252 
253  //------------------------------------------------------
254 
255 
256  // Get the solid angle reduction factor for planar optical detector OpDet
257  /*
258  double PhotonVisibilityService::SolidAngleFactor( double* xyz, unsigned int OpDet )
259  {
260  art::ServiceHandle<geo::Geometry> geom;
261  return geom->OpDetGeoFromOpDet(OpDet).CosThetaFromNormal(xyz);
262  }
263  */
264 
265  //------------------------------------------------------
266 
267  float PhotonVisibilityService::GetVisibility(double * xyz, unsigned int OpChannel) const
268  {
269  int VoxID = fVoxelDef.GetVoxelID(xyz);
270  return GetLibraryEntry(VoxID, OpChannel);
271  }
272 
273  float PhotonVisibilityService::GetVisibility(double x, double y, double z, unsigned int OpChannel) const
274  {
275  int VoxID = fVoxelDef.GetVoxelID(x,y,z);
276  return GetLibraryEntry(VoxID, OpChannel);
277  }
278 
279 
280  //------------------------------------------------------
281 
282  void PhotonVisibilityService::StoreLightProd(int VoxID, double N)
283  {
284  fCurrentVoxel = VoxID;
285  fCurrentValue = N;
286  std::cout << " PVS notes production of " << N << " photons at Vox " << VoxID<<std::endl;
287  }
288 
289 
290  //------------------------------------------------------
291 
292 
293  void PhotonVisibilityService::RetrieveLightProd(int& VoxID, double& N) const
294  {
295  N = fCurrentValue;
296  VoxID = fCurrentVoxel;
297  }
298 
299  //------------------------------------------------------
300 
301  void PhotonVisibilityService::SetLibraryEntry(int VoxID, int OpChannel, float N)
302  {
303  if(fTheLibrary == 0)
304  LoadLibrary();
305 
306  fTheLibrary->SetCount(VoxID,OpChannel, N);
307  std::cout<< " PVS logging " << VoxID << " " << OpChannel<<std::endl;
308  }
309 
310  //------------------------------------------------------
311 
312 
313 
314  const std::vector<float>* PhotonVisibilityService::GetLibraryEntries(int VoxID) const
315  {
316  if(fTheLibrary == 0)
317  LoadLibrary();
318 
319  return fTheLibrary->GetCounts(VoxID);
320  }
321 
322  //------------------------------------------------------
323 
324  float PhotonVisibilityService::GetLibraryEntry(int VoxID, int Channel) const
325  {
326  if(fTheLibrary == 0)
327  LoadLibrary();
328 
329  return fTheLibrary->GetCount(VoxID, Channel);
330  }
331 
332  //------------------------------------------------------
333  /*
334  int PhotonVisibilityService::NOpChannels()
335  {
336  if(fTheLibrary == 0)
337  LoadLibrary();
338 
339  return fTheLibrary->NOpChannels();
340  }
341  */
342 } // namespace
process_name opflash particleana ie ie ie z
std::vector< std::vector< float > > GetVisibilityYZ(double x) const
createEngine fYmax
std::vector< std::vector< float > > GetVisibilityXY(double z) const
phot::IPhotonLibrary::Counts_t GetLibraryEntries(int VoxID, bool wantReflected=false) const
void RetrieveLightProd(int &VoxID, double &N) const
std::vector< std::vector< float > > GetVisibilityZX(double y) const
process_name opflash particleana ie x
Representation of a region of space diced into voxels.
virtual float GetCount(size_t Voxel, size_t OpChannel) const =0
float GetLibraryEntry(int VoxID, OpDetID_t libOpChannel, bool wantReflected=false) const
int GetVoxelID(Point const &p) const
Returns the ID of the voxel containing p, or -1 if none.
unsigned int GetNVoxels() const
Returns the total number of voxels in the volume.
void StoreLightProd(int VoxID, double N)
createEngine fXmin
process_name opflash particleana ie ie y
createEngine fXmax
float Fraction2AbsoluteZ(float frac) const
createEngine fYmin
MappedCounts_t GetAllVisibilities(Point const &p, bool wantReflected=false) const
virtual Counts_t GetCounts(size_t Voxel) const =0
Returns a pointer to NOpChannels() visibility values, one per channel.
void SetLibraryEntry(int VoxID, OpDetID_t libOpChannel, float N, bool wantReflected=false)
process_name largeant stream1 can override from command line with o or output physics producers generator N
createEngine fZmin
createEngine fZmax
float Fraction2AbsoluteY(float frac) const
float GetVisibility(Point const &p, unsigned int OpChannel, bool wantReflected=false) const
BEGIN_PROLOG don t mess with this pandoraTrackGausCryoW true
BEGIN_PROLOG could also be cout
float Fraction2AbsoluteX(float frac) const