All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PhotonLibraryHybrid.cxx
Go to the documentation of this file.
2 
4 
6 
7 #include "art/Framework/Services/Registry/ServiceHandle.h"
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 
39  OpDetRecord rec;
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 
68  art::ServiceHandle<geo::Geometry const> geom;
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 
112  static art::ServiceHandle<geo::Geometry const> geom;
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 }
string fname
Definition: demo.py:5
virtual int NVoxels() const override
BEGIN_PROLOG could also be cerr
Representation of a region of space diced into voxels.
PhotonLibraryHybrid(const std::string &fname, const sim::PhotonVoxelDef &voxdef)
unsigned int GetNVoxels() const
Returns the total number of voxels in the volume.
Definitions of voxel data structures.
virtual float GetCount(size_t Voxel, size_t OpChannel) const override
counts_as<> counts
Number of ADC counts, represented by signed short int.
Definition: electronics.h:116
Point GetCenter() const
Returns the center of the voxel (type Point).
const sim::PhotonVoxelDef & fVoxDef
tuple dir
Definition: dropbox.py:28
double DistanceToPoint(geo::Point_t const &point) const
Returns the distance of the specified point from detector center [cm].
Definition: OpDetGeo.cxx:123
constexpr double dist(const TReal *x, const TReal *y, const unsigned int dimension)
std::vector< OpDetRecord > fRecords
virtual const float * GetCounts(size_t Voxel) const override
Returns a pointer to NOpChannels() visibility values, one per channel.
virtual int NOpChannels() const override
art framework interface to geometry description