PhotonLibrary.h
Go to the documentation of this file.
1 ////# PhotonLibrary.h header file
2 ////#
3 ////# Ben Jones, MIT, 2012
4 #ifndef PHOTONLIBRARY_H
5 #define PHOTONLIBRARY_H
6 
8 
10 
11 #include "TF1.h"
12 class TTree;
13 
15 
16 #include <limits> // std::numeric_limits
17 #include <optional>
18 
19 namespace art {
20  class TFileDirectory;
21 }
22 
23 namespace phot {
24 
25  class PhotonLibrary : public IPhotonLibrary {
26  public:
27  /// If no valid `pDir` is provided, storage features will not be supported.
28  PhotonLibrary(art::TFileDirectory* pDir = nullptr);
29 
30  TTree* ProduceTTree() const;
31 
32  virtual float GetCount(size_t Voxel, size_t OpChannel) const override;
33  void SetCount(size_t Voxel, size_t OpChannel, float Count);
34 
35  float GetTimingPar(size_t Voxel, size_t OpChannel, size_t parnum) const;
36  void SetTimingPar(size_t Voxel, size_t OpChannel, float Count, size_t parnum);
37 
38  // TF1& GetTimingTF1(size_t Voxel, size_t OpChannel) const;
39  void SetTimingTF1(size_t Voxel, size_t OpChannel, TF1 func);
40 
41  virtual float GetReflCount(size_t Voxel, size_t OpChannel) const override;
42  void SetReflCount(size_t Voxel, size_t OpChannel, float Count);
43 
44  virtual float GetReflT0(size_t Voxel, size_t OpChannel) const override;
45  void SetReflT0(size_t Voxel, size_t OpChannel, float reflT0);
46 
47  /// Returns a pointer to NOpChannels() visibility values, one per channel
48  virtual float const* GetCounts(size_t Voxel) const override;
49  const std::vector<float>* GetTimingPars(size_t Voxel) const;
50  TF1* GetTimingTF1s(size_t Voxel) const;
51 
52  virtual float const* GetReflCounts(size_t Voxel) const override;
53  virtual float const* GetReflT0s(size_t Voxel) const override;
54 
55  ///Returns whether the current library deals with time propagation distributions.
56  bool
57  hasTiming() const
58  {
59  return fHasTiming;
60  }
61 
62  /// Returns whether the current library deals with reflected light count.
63  virtual bool
64  hasReflected() const override
65  {
66  return fHasReflected;
67  }
68 
69  /// Returns whether the current library deals with reflected light timing.
70  virtual bool
71  hasReflectedT0() const override
72  {
73  return fHasReflectedT0;
74  }
75 
76  void StoreLibraryToFile(std::string LibraryFile,
77  bool storeReflected = false,
78  bool storeReflT0 = false,
79  size_t storeTiming = 0) const;
80  void LoadLibraryFromFile(std::string LibraryFile,
81  size_t NVoxels,
82  bool storeReflected = false,
83  bool storeReflT0 = false,
84  size_t storeTiming = 0,
85  int maxrange = 200);
86  void CreateEmptyLibrary(size_t NVoxels,
87  size_t NChannels,
88  bool storeReflected = false,
89  bool storeReflT0 = false,
90  size_t storeTiming = 0);
91 
92  // --- BEGIN --- Metadata: voxel information -------------------------------
93  /// @name Metadata: voxel information
94  /// @{
95 
96  /// Returns whether voxel metadata is available.
97  bool
98  hasVoxelDef() const
99  {
100  return fVoxelDef.has_value();
101  }
102 
103  /// Returns the current voxel metadata (undefined behaviour if none).
104  /// @see `hasVoxelDef()`
105  sim::PhotonVoxelDef const&
106  GetVoxelDef() const
107  {
108  assert(fVoxelDef);
109  return *fVoxelDef;
110  }
111 
112  /// Copies the specified voxel definition into our own
113  /// (overwrites the existing metadata if any).
114  void
116  {
117  fVoxelDef = voxelDef;
118  }
119 
120  /// @}
121  // --- END --- Metadata: voxel information ---------------------------------
122 
123  private:
124  virtual int
125  NOpChannels() const override
126  {
127  return fNOpChannels;
128  }
129  virtual int
130  NVoxels() const override
131  {
132  return fNVoxels;
133  }
134 
135  virtual bool
136  isVoxelValid(size_t Voxel) const override
137  {
138  return isVoxelValidImpl(Voxel);
139  }
140 
141  bool fHasReflected = false; ///< Whether the current library deals with reflected light counts.
142  bool fHasReflectedT0 =
143  false; ///< Whether the current library deals with reflected light timing.
144 
145  size_t fHasTiming =
146  0; ///< Whether the current library deals with time propagation distribution.
147 
148  // fLookupTable[unchecked_index(Voxel, OpChannel)] = Count
149  // for each voxel, all NChannels() channels are stored in sequence
157 
158  size_t fNOpChannels;
159  size_t fNVoxels;
160 
161  /// Voxel definition loaded from library metadata.
162  std::optional<sim::PhotonVoxelDef> fVoxelDef;
163 
164  /// ROOT directory where to write data.
165  art::TFileDirectory* fDir = nullptr;
166 
167  bool
168  isVoxelValidImpl(size_t Voxel) const
169  {
170  return Voxel < fNVoxels;
171  }
172 
173  /// Returns the index of visibility of specified voxel and cell
174  size_t
175  uncheckedIndex(size_t Voxel, size_t OpChannel) const
176  {
177  return Voxel * fNOpChannels + OpChannel;
178  }
179 
180  /// Unchecked access to a visibility datum
181  float
182  uncheckedAccess(size_t Voxel, size_t OpChannel) const
183  {
184  return fLookupTable[uncheckedIndex(Voxel, OpChannel)];
185  }
186 
187  /// Unchecked access to a visibility datum
188  float&
189  uncheckedAccess(size_t Voxel, size_t OpChannel)
190  {
191  return fLookupTable[uncheckedIndex(Voxel, OpChannel)];
192  }
193 
194  /// Unchecked access to a reflected visibility datum
195  float
196  uncheckedAccessRefl(size_t Voxel, size_t OpChannel) const
197  {
198  return fReflLookupTable[uncheckedIndex(Voxel, OpChannel)];
199  }
200 
201  /// Unchecked access to a reflected visibility datum
202  float&
203  uncheckedAccessRefl(size_t Voxel, size_t OpChannel)
204  {
205  return fReflLookupTable[uncheckedIndex(Voxel, OpChannel)];
206  }
207 
208  /// Unchecked access to a reflected T0 visibility datum
209  float
210  uncheckedAccessReflT(size_t Voxel, size_t OpChannel) const
211  {
212  return fReflTLookupTable[uncheckedIndex(Voxel, OpChannel)];
213  }
214 
215  /// Unchecked access to a reflected T0 visibility datum
216  float&
217  uncheckedAccessReflT(size_t Voxel, size_t OpChannel)
218  {
219  return fReflTLookupTable[uncheckedIndex(Voxel, OpChannel)];
220  }
221 
222  /// Unchecked access to a parameter the time distribution
223  float
224  uncheckedAccessTimingPar(size_t Voxel, size_t OpChannel, size_t parnum) const
225  {
226  return fTimingParLookupTable[uncheckedIndex(Voxel, OpChannel)][parnum];
227  }
228 
229  /// Unchecked access to a parameter of the time distribution
230  float&
231  uncheckedAccessTimingPar(size_t Voxel, size_t OpChannel, size_t parnum)
232  {
233  return fTimingParLookupTable[uncheckedIndex(Voxel, OpChannel)][parnum];
234  }
235 
236  /// Unchecked access to a parameter of the time distribution
237  TF1&
238  uncheckedAccessTimingTF1(size_t Voxel, size_t OpChannel)
239  {
240  return fTimingParTF1LookupTable[uncheckedIndex(Voxel, OpChannel)];
241  }
242 
243  /// Unchecked access to a parameter of the time distribution
244  const TF1&
245  uncheckedAccessTimingTF1(size_t Voxel, size_t OpChannel) const
246  {
247  // note that this will produce a segmentation fault if the formula is not there
248  return *(fTimingParTF1LookupTable.data_address(uncheckedIndex(Voxel, OpChannel)));
249  }
250 
251  /// Reads the metadata from specified ROOT directory and sets it as current.
252  void LoadMetadata(TDirectory& srcDir);
253 
254  /// Writes the current metadata (if any) into the ROOT output file.
255  void StoreMetadata() const;
256 
257  /// Name of the optical channel number in the input tree
259 
260  /// Returns the number of optical channels in the specified tree
261  static size_t ExtractNOpChannels(TTree* tree);
262 
263  /// Converts size_t into integer
264  static int
265  size_t2int(size_t val)
266  {
267  constexpr size_t int_max_as_size_t{std::numeric_limits<int>::max()};
269  }
270  };
271 
272 }
273 
274 #endif
Definitions of voxel data structures.
const_pointer data_address(size_type pos) const
Returns a constant pointer to the specified element.
Definition: LazyVector.h:584
Index OpChannel(Index detNum, Index channel)
static int size_t2int(size_t val)
Converts size_t into integer.
std::optional< sim::PhotonVoxelDef > fVoxelDef
Voxel definition loaded from library metadata.
float & uncheckedAccess(size_t Voxel, size_t OpChannel)
Unchecked access to a visibility datum.
util::LazyVector< float > fReflLookupTable
std::string string
Definition: nybbler.cc:12
sim::PhotonVoxelDef const & GetVoxelDef() const
bool hasVoxelDef() const
Returns whether voxel metadata is available.
Definition: PhotonLibrary.h:98
virtual int NOpChannels() const override
Representation of a region of space diced into voxels.
Definition: PhotonVoxels.h:58
static std::string const OpChannelBranchName
Name of the optical channel number in the input tree.
float uncheckedAccess(size_t Voxel, size_t OpChannel) const
Unchecked access to a visibility datum.
float uncheckedAccessReflT(size_t Voxel, size_t OpChannel) const
Unchecked access to a reflected T0 visibility datum.
std::string fTimingParFormula
size_t uncheckedIndex(size_t Voxel, size_t OpChannel) const
Returns the index of visibility of specified voxel and cell.
util::LazyVector< float > fLookupTable
virtual bool hasReflectedT0() const override
Returns whether the current library deals with reflected light timing.
Definition: PhotonLibrary.h:71
void SetVoxelDef(sim::PhotonVoxelDef const &voxelDef)
bool isVoxelValidImpl(size_t Voxel) const
const TF1 & uncheckedAccessTimingTF1(size_t Voxel, size_t OpChannel) const
Unchecked access to a parameter of the time distribution.
TF1 & uncheckedAccessTimingTF1(size_t Voxel, size_t OpChannel)
Unchecked access to a parameter of the time distribution.
float & uncheckedAccessTimingPar(size_t Voxel, size_t OpChannel, size_t parnum)
Unchecked access to a parameter of the time distribution.
AdcSimulator::Count Count
Contiguous data container with lazy resizing on access.
util::LazyVector< TF1 > fTimingParTF1LookupTable
static int max(int a, int b)
virtual bool isVoxelValid(size_t Voxel) const override
General LArSoft Utilities.
float uncheckedAccessRefl(size_t Voxel, size_t OpChannel) const
Unchecked access to a reflected visibility datum.
float & uncheckedAccessRefl(size_t Voxel, size_t OpChannel)
Unchecked access to a reflected visibility datum.
virtual bool hasReflected() const override
Returns whether the current library deals with reflected light count.
Definition: PhotonLibrary.h:64
def func()
Definition: docstring.py:7
int fNOpChannels
Interface shared by all PhotonLibrary-like classes.
util::LazyVector< std::vector< float > > fTimingParLookupTable
util::LazyVector< float > fReflTLookupTable
float & uncheckedAccessReflT(size_t Voxel, size_t OpChannel)
Unchecked access to a reflected T0 visibility datum.
virtual int NVoxels() const override
bool hasTiming() const
Returns whether the current library deals with time propagation distributions.
Definition: PhotonLibrary.h:57
float uncheckedAccessTimingPar(size_t Voxel, size_t OpChannel, size_t parnum) const
Unchecked access to a parameter the time distribution.