PhotonLibraryHybrid.cxx
Go to the documentation of this file.
2 
4 
6 
8 
9 #include "TFile.h"
10 #include "TTree.h"
11 #include "TVectorD.h"
12 
13 #include <iostream>
14 
15 namespace
16 {
17  bool fatal(const std::string& msg)
18  {
19  std::cerr << "FATAL: PhotonLibraryHybrid: " << msg << std::endl;
20  abort();
21  }
22 }
23 
24 namespace phot
25 {
26  //--------------------------------------------------------------------
28  const sim::PhotonVoxelDef& voxdef)
29  : fVoxDef(voxdef)
30  {
31  TFile f(fname.c_str());
32  !f.IsZombie() || fatal("Could not open PhotonLibrary "+fname);
33 
34  for(int opdetIdx = 0; true; ++opdetIdx){
35  const std::string dirname = TString::Format("opdet_%d", opdetIdx).Data();
36  TDirectory* dir = (TDirectory*)f.Get(dirname.c_str());
37  if(!dir) break; // Ran out of opdets
38 
40 
41  TVectorD* fit = (TVectorD*)dir->Get("fit");
42  fit || fatal("Didn't find "+dirname+"/fit in "+fname);
43  rec.fit = FitFunc((*fit)[0], (*fit)[1]);
44 
45  TTree* tr = (TTree*)dir->Get("tr");
46  tr || fatal("Didn't find "+dirname+"/tr in "+fname);
47  int vox;
48  float vis;
49  tr->SetBranchAddress("vox", &vox);
50  tr->SetBranchAddress("vis", &vis);
51 
52  rec.exceptions.reserve(tr->GetEntries());
53  for(int i = 0; i < tr->GetEntries(); ++i){
54  tr->GetEntry(i);
55  vox < NVoxels() || fatal("Voxel out of range");
56  rec.exceptions.emplace_back(vox, vis);
57  }
58  rec.exceptions.shrink_to_fit(); // In case we don't trust reserve()
59 
60  // In case they weren't sorted in the file
61  std::sort(rec.exceptions.begin(), rec.exceptions.end());
62 
63  fRecords.push_back(rec);
64  } // end for opdetIdx
65 
66  !fRecords.empty() || fatal("No opdet_*/ directories in "+fname);
67 
69  geom->NOpDets() == fRecords.size() || fatal("Number of opdets mismatch");
70 
71  fRecords.shrink_to_fit(); // save memory
72  }
73 
74  //--------------------------------------------------------------------
76  {
77  }
78 
79  //--------------------------------------------------------------------
81  {
82  return fVoxDef.GetNVoxels();
83  }
84 
85  //--------------------------------------------------------------------
86  const float* PhotonLibraryHybrid::GetCounts(size_t vox) const
87  {
88  // The concept of GetCounts() conflicts fairly badly with how this library
89  // works. This is probably the best we can do.
90 
91  static float* counts = 0;
92  if(!counts) counts = new float[NOpChannels()];
93  for(int od = 0; od < NOpChannels(); ++od) counts[od] = GetCount(vox, od);
94  return counts;
95  }
96 
97  //--------------------------------------------------------------------
98  float PhotonLibraryHybrid::GetCount(size_t vox, size_t opchan) const
99  {
100  int(vox) < NVoxels() || fatal("GetCount(): Voxel out of range");
101  int(opchan) < NOpChannels() || fatal("GetCount(): OpChan out of range");
102 
103  const OpDetRecord& rec = fRecords[opchan];
104 
105  auto it2 = std::lower_bound(rec.exceptions.begin(),
106  rec.exceptions.end(),
107  vox);
108  if(it2->vox == vox) return it2->vis;
109 
110  // Otherwise, we use the interpolation, which requires a distance
111 
113  const geo::OpDetGeo& opdet = geom->OpDetGeoFromOpDet(opchan);
114 
115  const auto voxvec = fVoxDef.GetPhotonVoxel(vox).GetCenter();
116 
117  const double dist = opdet.DistanceToPoint(voxvec);
118 
119  return rec.fit.Eval(dist);
120  }
121 }
Definitions of voxel data structures.
rec
Definition: tracks.py:88
OpDetGeo const & OpDetGeoFromOpDet(unsigned int OpDet) const
Returns the geo::OpDetGeo object for the given detector number.
virtual const float * GetCounts(size_t Voxel) const override
Returns a pointer to NOpChannels() visibility values, one per channel.
Format
Definition: utils.h:7
void msg(const char *fmt,...)
Definition: message.cpp:107
std::string string
Definition: nybbler.cc:12
Representation of a region of space diced into voxels.
Definition: PhotonVoxels.h:58
string dir
PhotonLibraryHybrid(const std::string &fname, const sim::PhotonVoxelDef &voxdef)
art framework interface to geometry description
virtual int NVoxels() const override
counts_as<> counts
Number of ADC counts, represented by signed short int.
Definition: electronics.h:116
void fatal(const char *msg,...)
Definition: qglobal.cpp:465
const sim::PhotonVoxelDef & fVoxDef
virtual int NOpChannels() const override
unsigned int NOpDets() const
Number of OpDets in the whole detector.
double DistanceToPoint(geo::Point_t const &point) const
Returns the distance of the specified point from detector center [cm].
Definition: OpDetGeo.cxx:123
General LArSoft Utilities.
virtual float GetCount(size_t Voxel, size_t OpChannel) const override
constexpr double dist(const TReal *x, const TReal *y, const unsigned int dimension)
Point GetCenter() const
Returns the center of the voxel (type Point).
Definition: PhotonVoxels.h:199
unsigned int GetNVoxels() const
Returns the total number of voxels in the volume.
std::vector< OpDetRecord > fRecords
QTextStream & endl(QTextStream &s)
PhotonVoxel GetPhotonVoxel(int ID) const