LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
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  //--------------------------------------------------------------------
27  PhotonLibraryHybrid::PhotonLibraryHybrid(const std::string& fname,
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 
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 TVector3 voxvec = fVoxDef.GetPhotonVoxel(vox).GetCenter();
116  const double xyzvox[] = {voxvec.X(), voxvec.Y(), voxvec.Z()};
117 
118  const double dist = opdet.DistanceToPoint(xyzvox);
119 
120  return rec.fit.Eval(dist);
121  }
122 }
OpDetGeo const & OpDetGeoFromOpDet(unsigned int OpDet) const
Number of OpDets in the whole detector.
virtual const float * GetCounts(size_t Voxel) const override
Returns a pointer to NOpChannels() visibility values, one per channel.
Float_t vox
Definition: plot.C:40
TVector3 GetCenter() const
PhotonLibraryHybrid(const std::string &fname, const sim::PhotonVoxelDef &voxdef)
int GetNVoxels() const
TFile f
Definition: plotHisto.C:6
virtual int NVoxels() const override
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:114
General LArSoft Utilities.
TDirectory * dir
Definition: macro.C:5
virtual float GetCount(size_t Voxel, size_t OpChannel) const override
std::vector< OpDetRecord > fRecords
art framework interface to geometry description
PhotonVoxel GetPhotonVoxel(int ID) const