All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sbncode/sbncode/OpT0Finder/flashmatch/Base/FMWKTools/PhotonLibrary.cxx
Go to the documentation of this file.
1 
2 //#include "art/Framework/Services/Registry/ServiceHandle.h"
3 //#include "art/Framework/Services/Optional/TFileService.h"
4 //#include "art/Framework/Services/Optional/TFileDirectory.h"
5 
6 //#include "Geometry/Geometry.h"
7 
8 #include "PhotonLibrary.h"
9 #include "PhotonVoxels.h"
10 #include <iostream>
11 //#include "messagefacility/MessageLogger/MessageLogger.h"
12 
13 #include "TFile.h"
14 #include "TTree.h"
15 #include "TKey.h"
16 
17 namespace phot{
18 
19  std::vector<float> PhotonLibrary::EmptyChannelsList; // used for invalid return value
20 
21  //------------------------------------------------------------
22 
24  {
26  }
27 
28 
29  //------------------------------------------------------------
30 
32  {
34  }
35 
36  //------------------------------------------------------------
37 
38  void PhotonLibrary::StoreLibraryToFile(std::string LibraryFile)
39  {
40  std::cout << "Writing photon library to input file: " << LibraryFile.c_str()<<std::endl;
41 
42  TFile fout(LibraryFile.c_str(),"RECREATE");
43 
44  TTree *tt = new TTree("PhotonLibraryData","PhotonLibraryData");
45 
46  Int_t Voxel;
47  Int_t OpChannel;
48  Float_t Visibility;
49 
50 
51  tt->Branch("Voxel", &Voxel, "Voxel/I");
52  tt->Branch("OpChannel", &OpChannel, "OpChannel/I");
53  tt->Branch("Visibility", &Visibility, "Visibility/F");
54 
55 
56  for(size_t ivox=0; ivox!=fLookupTable.size(); ++ivox)
57  {
58  for(size_t ichan=0; ichan!=fLookupTable.at(ivox).size(); ++ichan)
59  {
60  if(fLookupTable[ivox].at(ichan) > 0)
61  {
62  Voxel = ivox;
63  OpChannel = ichan;
64  Visibility = fLookupTable[ivox][ichan];
65  tt->Fill();
66  }
67  }
68  }
69  fout.cd();
70  tt->Write();
71  fout.Close();
72  }
73 
74 
75  //------------------------------------------------------------
76 
77  void PhotonLibrary::CreateEmptyLibrary( size_t NVoxels, size_t NOpChannels)
78  {
80 
81  fNVoxels = NVoxels;
83 
84  fLookupTable.resize(NVoxels);
85 
86  for(size_t ivox=0; ivox!=NVoxels; ivox++)
87  {
88  fLookupTable[ivox].resize(NOpChannels,0);
89  }
90  }
91 
92 
93  //------------------------------------------------------------
94 
95  void PhotonLibrary::LoadLibraryFromFile(std::string LibraryFile, size_t NVoxels)
96  {
98 
99  std::cout<< "Reading photon library from input file: " << LibraryFile.c_str()<<std::endl;
100 
101  TFile *f = nullptr;
102  TTree *tt = nullptr;
103 
104  try
105  {
106  f = TFile::Open(LibraryFile.c_str());
107  if(!f) {
108  std::cerr<<"\033[95m<<"<<__FUNCTION__<<">>\033[00m " << "Failed to open a ROOT file: " << LibraryFile.c_str()<<std::endl;
109  std::cerr<<"If you don't have photon library data file, download from below URL..."<<std::endl;
110  std::cerr<<"/cvmfs/icarus.opensciencegrid.org/products/icarus/icarus_data/v08_28_00/icarus_data/PhotonLibrary/PhotonLibrary-20180801.root"<<std::endl<<std::endl;
111  throw std::exception();
112  }
113  tt = (TTree*)f->Get("PhotonLibraryData");
114  if (!tt) { // Library not in the top directory
115  TKey *key = f->FindKeyAny("PhotonLibraryData");
116  if (key)
117  tt = (TTree*)key->ReadObj();
118  else {
119  std::cerr << "PhotonLibraryData not found in file" <<LibraryFile<<std::endl;
120  }
121  }
122  }
123  catch(...)
124  {
125  std::cerr << "Error in ttree load, reading photon library: " << LibraryFile.c_str()<<std::endl;
126  }
127 
128  Int_t Voxel;
129  Int_t OpChannel;
130  Float_t Visibility;
131 
132  tt->SetBranchAddress("Voxel", &Voxel);
133  tt->SetBranchAddress("OpChannel", &OpChannel);
134  tt->SetBranchAddress("Visibility", &Visibility);
135 
136 
137 
138 
139  fNVoxels = NVoxels;
140  fNOpChannels = 1; // Minimum default, overwritten by library reading
141 
142 
143  fLookupTable.resize(NVoxels);
144 
145 
146  size_t NEntries = tt->GetEntries();
147 
148  for(size_t i=0; i!=NEntries; ++i) {
149  tt->GetEntry(i);
150 
151  // Set # of optical channels to 1 more than largest one seen
152  if (OpChannel >= (int)fNOpChannels)
153  fNOpChannels = OpChannel+1;
154 
155  // Expand this voxel's vector if needed
156  if (fLookupTable[Voxel].size() < fNOpChannels)
157  fLookupTable[Voxel].resize(fNOpChannels, 0);
158 
159  // Set the visibility at this optical channel
160  fLookupTable[Voxel][OpChannel] = Visibility;
161  }
162 
163  // Go through the table and fill in any missing 0's
164  for(size_t ivox=0; ivox!=NVoxels; ivox++)
165  {
166  if (fLookupTable[ivox].size() < fNOpChannels)
168  }
169 
170 
171  std::cout << NVoxels << " voxels, " << fNOpChannels<<" channels" <<std::endl;
172 
173 
174  try
175  {
176  f->Close();
177  }
178  catch(...)
179  {
180  std::cerr << "Error in closing file : " << LibraryFile.c_str()<<std::endl;
181  }
182  }
183 
184  //----------------------------------------------------
185 
186  float PhotonLibrary::GetCount(size_t Voxel, size_t OpChannel)
187  {
188  //if(/*(Voxel<0)||*/(Voxel>=fNVoxels)||/*(OpChannel<0)||*/(OpChannel>=fNOpChannels))
189  // return 0;
190  //else
191  return fLookupTable[Voxel][OpChannel];
192  }
193 
194  //----------------------------------------------------
195 
196  void PhotonLibrary::SetCount(size_t Voxel, size_t OpChannel, float Count)
197  {
198  if(/*(Voxel<0)||*/(Voxel>=fNVoxels))
199  std::cerr <<"Error - attempting to set count in voxel " << Voxel<<" which is out of range" <<std::endl;
200  else
201  fLookupTable[Voxel].at(OpChannel) = Count;
202  }
203 
204  //----------------------------------------------------
205 
206  const std::vector<float>* PhotonLibrary::GetCounts(size_t Voxel) const
207  {
208  if(/*(Voxel<0)||*/(Voxel>=fNVoxels))
209  return EmptyList(); // FIXME!!! better to throw an exception!
210  else
211  return &(fLookupTable[Voxel]);
212  }
213 
214 
215 
216 }
void LoadLibraryFromFile(std::string LibraryFile, size_t NVoxels, bool storeReflected=false, bool storeReflT0=false, size_t storeTiming=0, int maxrange=200)
BEGIN_PROLOG could also be cerr
void CreateEmptyLibrary(size_t NVoxels, size_t NChannels, bool storeReflected=false, bool storeReflT0=false, size_t storeTiming=0)
size_type size() const noexcept
Returns the size of the vector.
Definition: LazyVector.h:156
void clear()
Removes all stored data and sets the nominal size to 0.
Definition: LazyVector.h:622
std::size_t size(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:561
void StoreLibraryToFile(std::string LibraryFile, bool storeReflected=false, bool storeReflT0=false, size_t storeTiming=0) const
void SetCount(size_t Voxel, size_t OpChannel, float Count)
virtual float const * GetCounts(size_t Voxel) const override
Returns a pointer to NOpChannels() visibility values, one per channel.
virtual float GetCount(size_t Voxel, size_t OpChannel) const override
value_type at(size_type pos) const
Returns a reference to the specified element, throws an exception if not present. ...
Definition: LazyVector.h:529
void resize(size_type newSize)
Changes the nominal size of the container.
Definition: LazyVector.h:602
BEGIN_PROLOG could also be cout