-
Notifications
You must be signed in to change notification settings - Fork 3
/
library_access.cc
205 lines (163 loc) · 5.83 KB
/
library_access.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <iostream>
#include <sstream>
#include "TFile.h"
#include "TTree.h"
#include "TKey.h"
#include "TRandom3.h"
#include "TMath.h"
#include "library_access.h"
#include "utility_functions.h"
using namespace std;
LibraryAccess::LibraryAccess()
: table_(std::vector<std::vector<float> >()),
reflected_table_(std::vector<std::vector<float> >()),
reflT_table_(std::vector<std::vector<float> >())
{
}
void LibraryAccess::LoadLibraryFromFile(std::string libraryfile, bool reflected, bool reflT0)
{
cout << "Reading photon library from input file: " << libraryfile.c_str()<<endl;
TFile *f = nullptr;
TTree *tt = nullptr;
try
{
f = TFile::Open(libraryfile.c_str());
tt = (TTree*)f->Get("PhotonLibraryData");
if (!tt) {
TKey *key = f->FindKeyAny("PhotonLibraryData");
if (key)
tt = (TTree*)key->ReadObj();
else {
cout << "PhotonLibraryData not found in file" <<libraryfile;
}
}
}
catch(...)
{
cout << "Error in ttree load, reading photon library: " << libraryfile.c_str()<<endl;
}
int voxel;
int opChannel;
float visibility;
float reflVisibility;
float reflT;
int maxvoxel = tt->GetMaximum("Voxel")+1;
int maxopChannel = tt->GetMaximum("OpChannel")+2;
cout << "Photon lookup table size : " << maxvoxel << " voxels, " << maxopChannel <<" channels " << endl;
table_.resize(maxvoxel, std::vector<float>(maxopChannel, 0));
reflected_table_.resize(maxvoxel, std::vector<float>(maxopChannel, 0));
reflT_table_.resize(maxvoxel, std::vector<float>(maxopChannel, 0));
tt->SetBranchAddress("Voxel", &voxel);
tt->SetBranchAddress("OpChannel", &opChannel);
tt->SetBranchAddress("Visibility", &visibility);
if(reflected) {tt->SetBranchAddress("ReflVisibility", &reflVisibility); }
if(reflT0) {tt->SetBranchAddress("ReflTfirst", &reflT); }
size_t nentries = tt->GetEntries();
for(size_t i=0; i!=nentries; ++i)
{
tt->GetEntry(i);
if((voxel<0)||(voxel>= maxvoxel)||(opChannel<0)||(opChannel>= maxopChannel))
{}
else
{
table_.at(voxel).at(opChannel) = visibility;
if(reflected) {reflected_table_.at(voxel).at(opChannel) = reflVisibility; }
else{reflected_table_.at(voxel).at(opChannel) = 0; }
if(reflT0) {reflT_table_.at(voxel).at(opChannel) = reflT; }
else{reflT_table_.at(voxel).at(opChannel) = 0; }
}
}
try
{
f->Close();
}
catch(...)
{
cout << "Error in closing file : " << libraryfile.c_str()<<endl;
}
}
const float* LibraryAccess::GetReflT0(size_t voxel, int no_pmt)
{
return &reflT_table_.at(voxel).at(no_pmt);
}
const float* LibraryAccess::GetReflCounts(size_t voxel, int no_pmt, bool reflected)
{
if(reflected) {return &reflected_table_.at(voxel).at(no_pmt); }
else{return 0; }
}
const float* LibraryAccess::GetCounts(size_t voxel, int no_pmt)
{
return &table_.at(voxel).at(no_pmt);
}
const float* LibraryAccess::GetLibraryEntries(int voxID, bool reflected, int no_pmt)
{
if(!reflected)
return GetCounts(voxID, no_pmt);
else
return GetReflCounts(voxID, no_pmt, reflected);
}
vector<int> LibraryAccess::GetVoxelCoords(int id, double position[3])
{
vector<int> returnvector;
returnvector.resize(3);
returnvector.at(0) = id % gxSteps;
returnvector.at(1) = ((id - returnvector.at(0) ) / gxSteps) % gySteps;
returnvector.at(2) = ((id - returnvector.at(0) - (returnvector.at(1) * gxSteps)) / (gySteps * gxSteps)) % gzSteps;
position[0] = gLowerCorner[0] + (returnvector.at(0) + 0.5)*(gUpperCorner[0] - gLowerCorner[0])/gxSteps;
position[1] = gLowerCorner[1] + (returnvector.at(1) + 0.5)*(gUpperCorner[1] - gLowerCorner[1])/gySteps;
position[2] = gLowerCorner[2] + (returnvector.at(2) + 0.5)*(gUpperCorner[2] - gLowerCorner[2])/gzSteps;
return returnvector;
}
//This function takes is most of the information needed to calculate the number
//of photoelectrons on a given PMT
std::vector<double> LibraryAccess::PhotonLibraryAnalyzer(double _energy, const int _scint_yield, const double _quantum_efficiency, int _pmt_number, int _rand_voxel)
{
//The number of photons created is determined by this formula:
//Nphotons_created = Poisson < Scintillation Yield (24000/MeV) * dE/dX (MeV)>
const int scint_yield = _scint_yield;
const double quantum_efficiency = _quantum_efficiency;
double energy = _energy;
int i = _rand_voxel;
vector<double> pmt_hits;
int pre_Nphotons_created = scint_yield * energy;
//Applying quantum efficiency right after calculation reduces total number
//of photons working with initially
int Nphotons_created = utility::poisson(pre_Nphotons_created, gRandom->Uniform(1.), energy);
//Find position of event
double position[3];
vector<int> Coords = GetVoxelCoords(i, position);
//Look up visibility parameter/timing by comparing the optical channel (PMT Number)
//and the detector location (voxel, i)
const float* Visibilities = GetLibraryEntries(i, false, _pmt_number);
const float* ReflVisibilities = GetLibraryEntries(i, true, _pmt_number);
const float* reflected_T0 = GetReflT0(i, _pmt_number);
const float vis = *Visibilities;
const float reflvis = *ReflVisibilities;
//int ichan = _pmt_number;
//Number of photoelectrons for a given PMT
double hits_vuv = Nphotons_created * vis;
double hits_vis = Nphotons_created * reflvis;
double reflT0 = *reflected_T0;
/// Cast the hits into int type
int int_hits_vuv = hits_vuv;
int int_hits_vis = hits_vis;
int total_hits_vuv = 0;
int total_hits_vis = 0;
for(int i = 0; i < int_hits_vuv; i++)
{
if(gRandom->Uniform(1.) <= quantum_efficiency){total_hits_vuv++;}
}
for(int j = 0; j < int_hits_vis; j++)
{
if(gRandom->Uniform(1.) <= quantum_efficiency){total_hits_vis++;}
}
//Push information back into a vector to readout in:
//libraryanalyze_light_histo
pmt_hits.push_back(total_hits_vuv);
pmt_hits.push_back(total_hits_vis);
pmt_hits.push_back(position[0]);//x position of voxel
pmt_hits.push_back(position[1]);//y
pmt_hits.push_back(position[2]);//z
pmt_hits.push_back(reflT0);
return pmt_hits;
}