-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadaptive_grid_tsel.cc
291 lines (226 loc) · 11.2 KB
/
adaptive_grid_tsel.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include <TH3D.h>
#include <vector>
#include <TMath.h>
#include <TFile.h>
#include <TTree.h>
#include <iostream>
#include <TCanvas.h>
#include <RAT/DU/Utility.hh>
#include <RAT/DU/PMTInfo.hh>
#include <RAT/DU/DSReader.hh>
#include <RAT/DS/Entry.hh>
#include <RAT/DS/MC.hh>
#include <RAT/DB.hh>
#include <RAT/DU/Point3D.hh>
#include <Cube4D.hh>
#include <Cube4DCollection.hh>
size_t fAVSystemId;
void AdapGrid( Cube4DCollection* col, Cube4DCollection* &final_cube_col, RAT::DU::PMTInfo pmt_info, RAT::DU::TimeResidualCalculator time_res_calc, RAT::DS::CalPMTs calibrated_PMTs, std::vector< std::pair < UInt_t, double > > pmts, double min_t, double max_t, double init_cube_rad_t, double res, int factor, double hit_cut );
double CalcOverlap( Cube4DCollection* &col, RAT::DU::PMTInfo pmt_info, RAT::DU::TimeResidualCalculator time_res_calc, RAT::DS::CalPMTs calibrated_PMTs );
int main( int argc, char **argv ) {
if (argc != 3) {
std::cout << "Syntax is $: adaptive_grid inputfile outputfile" << std::endl;
exit(-1);
}
const std::string fname = argv[1];
const std::string out_fname = argv[2];
//// Open file
RAT::DU::DSReader ds_reader( fname.c_str() );
//// RAT begin of runs etc
RAT::DU::PMTInfo pmt_info = RAT::DU::Utility::Get()->GetPMTInfo();
RAT::DU::TimeResidualCalculator time_res_calc = RAT::DU::Utility::Get()->GetTimeResidualCalculator();
RAT::DU::Point3D::BeginOfRun();
fAVSystemId = RAT::DU::Point3D::GetSystemId("av");
//// Get event and fit vertex
const RAT::DS::Entry& r_DS = ds_reader.GetEntry( 0 );
const RAT::DS::MC& mc = r_DS.GetMC();
const RAT::DS::EV& r_Ev = r_DS.GetEV( 0 );
const RAT::DS::FitVertex& r_vertex = r_Ev.GetFitResult("scintFitter").GetVertex(0);
const TVector3 fit_pos = r_vertex.GetPosition();
const double fit_time = r_vertex.GetTime();
RAT::DS::CalPMTs calibrated_PMTs = r_Ev.GetCalPMTs();
//// Set up the #cube
int init_num_t_above = 16;
int init_num_t_below = 3;
double init_cube_rad_t = 1.5;
double min_t = fit_time - 2*init_cube_rad_t*(init_num_t_below);
double max_t = fit_time + 2*init_cube_rad_t*(init_num_t_above);
double min_xyz = -5500;
double max_xyz = 5500;
double init_cube_rad = 500;
int init_num_cubes = floor( ( max_xyz - min_xyz ) / 2*init_cube_rad );
double cube_min_rad_t = 0.3;
double cube_min_rad = 100;
//// Adaptive grid parameters
double res = 150;
double factor = 10;
int num_mini_cubes = floor( ( max_xyz - min_xyz ) / cube_min_rad );
int num_t = floor( ( max_t - min_t ) / cube_min_rad_t );
double hit_cut = 300;
std::cout << std::endl;
//// Make vector of pmts to assign to each initial cube
std::vector< std::pair < UInt_t, double > > pmts;
for(size_t i_pmt = 0; i_pmt < calibrated_PMTs.GetCount(); i_pmt++) {
RAT::DS::PMTCal pmt = calibrated_PMTs.GetPMT( i_pmt );
UInt_t PMT_ID = pmt.GetID();
double PMT_t = pmt.GetTime();
std::pair < UInt_t, double > pmt_pair( PMT_ID, PMT_t);
pmts.push_back( pmt_pair );
}
std::cout << "xyz: " << min_xyz << " " << max_xyz << std::endl;
std::cout << "t: " << min_t << " " << max_t << std::endl;
std::cout << "fit t " << fit_time << std::endl;
Cube4DCollection* init_cube_col = new Cube4DCollection;
//// Loop xyz positions to make initial cubes
for(double x = min_xyz + init_cube_rad; x < max_xyz; x += 2*init_cube_rad) {
for(double y = min_xyz + init_cube_rad; y < max_xyz; y += 2*init_cube_rad) {
for(double z = min_xyz + init_cube_rad; z < max_xyz; z += 2*init_cube_rad) {
TVector3 cube_pos(x, y, z);
if( cube_pos.Mag() > 5500)
continue;
//// Make cube
Cube4D* cub = new Cube4D( x, y, z, init_cube_rad );
//// Get PMTs associated with the cube (at this point, all hit PMTs)
cub->SetPMTs( pmts );
//std::cout << "Adding initial cube at " << x << " " << y << " " << z << std::endl;
//// Add it to collection
init_cube_col->AddCube( cub );
}
}
} //// End loop over xyz of cubes
//// Adaptive Grid on Cube Collection
Cube4DCollection* final_cube_col = new Cube4DCollection();
AdapGrid( init_cube_col, final_cube_col, pmt_info, time_res_calc, calibrated_PMTs, pmts, min_t, max_t, init_cube_rad_t, res, factor, hit_cut );
//// Gonna make some histograms
TH3D* hists[num_t];
for(int i=0; i<num_t; i++){
TString hname = Form("h_%d",i);
TString htitle = Form("h_%f", min_t + 2*cube_min_rad_t*i);
hists[i] = new TH3D( hname, htitle, num_mini_cubes, min_xyz, max_xyz, num_mini_cubes, min_xyz, max_xyz, num_mini_cubes, min_xyz, max_xyz );
hists[i]->GetXaxis()->SetTitle("X, mm ");
hists[i]->GetXaxis()->SetTitleOffset(1.5);
hists[i]->GetYaxis()->SetTitle("Y, mm");
hists[i]->GetYaxis()->SetTitleOffset(2.0);
hists[i]->GetZaxis()->SetTitle("Z, mm");
hists[i]->GetZaxis()->SetTitleOffset(1.3);
hists[i]->SetLineWidth(0);
}
std::cout << std::endl;
//// Now loop over cubes and fill histo
for( int i_cube = 0; i_cube < final_cube_col->GetNCubes(); i_cube++ ){
Cube4D* cub = final_cube_col->GetCube( i_cube );
double cube_x = cub->GetX();
double cube_y = cub->GetY();
double cube_z = cub->GetZ();
double cube_t = cub->GetT();
double overlap = cub->GetLLH();
//std::cout << "looping " << i_cube << " of " << final_cube_col->GetNCubes() << " " << cub->GetLLH() << std::endl;
//std::cout << cube_x << " " << cube_y << " " << cube_z << " " << cube_t << " " << overlap << std::endl;
//// Fill histogram for this time slice if we have some density
if( overlap > 0 ){
int hist_num = floor((cube_t - min_t)/cube_min_rad_t) + 1;
if(hist_num >= num_t){
std::cout << "WARNING: calculated hist num " << hist_num << " setting to" << num_t-1 << std::endl;
hist_num = num_t - 1;
}
int bin_number = hists[hist_num]->FindBin(cube_x, cube_y, cube_z);
if(overlap > hists[hist_num]->GetBinContent(bin_number));{
//std::cout << "Filling " << cube_x << " " << cube_y << " " << cube_z << " " << overlap << " " << cube_t << " " << hist_num << std::endl;
hists[hist_num]->SetBinContent( bin_number, overlap );
}
}
}
delete final_cube_col;
delete init_cube_col;
//// Write all histograms to file
TFile *out_file = TFile::Open( out_fname.c_str(), "RECREATE");
for(int i=0; i<num_t; i++){
hists[i]->Write();
std::cout << hists[i]->Integral() << std::endl;
}
out_file->Close();
}
//// Function to recursively perform the adaptive grid
void AdapGrid( Cube4DCollection* init_cube_col, Cube4DCollection* &final_cube_col, RAT::DU::PMTInfo pmt_info, RAT::DU::TimeResidualCalculator time_res_calc, RAT::DS::CalPMTs calibrated_PMTs, std::vector< std::pair < UInt_t, double > > pmts, double min_t, double max_t, double init_cube_rad_t, double res, int factor, double hit_cut ) {
for(double t = min_t + init_cube_rad_t; t < max_t; t += 2*init_cube_rad_t){
//std::cout << std::endl;
std::cout << "Adap grid for " << t << std::endl;
Cube4DCollection* col = new Cube4DCollection(*init_cube_col);
col->SetT(t);
col->SetTRadius(init_cube_rad_t);
col->SetPMTs(pmts);
double best_global_overlap = CalcOverlap( col, pmt_info, time_res_calc, calibrated_PMTs );
col->RemoveRepeatedPMTs();
best_global_overlap = CalcOverlap( col, pmt_info, time_res_calc, calibrated_PMTs );
std::cout << "Best Global Overlap " << best_global_overlap << std::endl;
//// Loop Cubes
for( int i_cube = 0; i_cube < col->GetNCubes(); i_cube++ ){
// std::cout << "final loop " << i_cube << " out of " << col->GetNCubes() << std::endl;
Cube4D* cub = new Cube4D(*col->GetCube( i_cube ));
double cube_x = cub->GetX();
double cube_y = cub->GetY();
double cube_z = cub->GetZ();
double cube_r = cub->GetRadius();
//std::cout << cube_x << " " << cube_y << " " << cube_z << " " << cub->GetPMTs().size() << " " << cub->GetT() << " " << std::endl;
//// If we're above the resolution, we might want to divide the cube into subcubes
if( cube_r > res && best_global_overlap > hit_cut ){
//// If llh > 50% best
if( cub->GetLLH() > hit_cut ){ // 0.5*best_global_overlap ) {
Cube4DCollection* new_col = cub->Divide( factor );
//// Each new cube has same associated PMTs as the parent bigger cube
new_col->SetPMTs( cub->GetPMTs() );
double new_rad_t = init_cube_rad_t / factor;
double new_min_t = t - init_cube_rad_t;
double new_max_t = t + init_cube_rad_t;
std::cout << std::endl << "Dividing cube " << cube_x << " " << cube_y << " " << cube_z << " " << cub->GetLLH() << " " << factor << std::endl;
//std::cout << "\t starts " << cube_x - cube_r << " " << cube_y - cube_r << " " << cube_z - cube_r << std::endl;
//std::cout << "\t end " << cube_x + cube_r <<" " << cube_y + cube_r << " " << cube_z + cube_r<< std::endl;
//// Rerun adaptive grid on new collection
AdapGrid( new_col, final_cube_col, pmt_info, time_res_calc, calibrated_PMTs, pmts, new_min_t, new_max_t, new_rad_t, res, factor, hit_cut/(factor) );
}
// std::cout << "ending journey " << cube_x << " " << cube_y << " " << cube_z << std::endl;
}
else {
//std::cout << "Adding final cubes " << cube_x << " " << cube_y << " " << cube_z << " " << cub->GetLLH() << std::endl;
final_cube_col->AddCube( cub );
}
} //// End 2nd loop over cubes
delete col;
} //// End loop over t
}
//// Function to calculate overlap for cubes in a cube collection
double CalcOverlap( Cube4DCollection* &col, RAT::DU::PMTInfo pmt_info, RAT::DU::TimeResidualCalculator time_res_calc, RAT::DS::CalPMTs calibrated_PMTs ) {
double best_global_overlap = 0;
//// Loop Cubes
for( int i_cube = 0; i_cube < col->GetNCubes(); i_cube++ ){
Cube4D* cub = col->GetCube( i_cube );
double cube_x = cub->GetX();
double cube_y = cub->GetY();
double cube_z = cub->GetZ();
TVector3 cube_pos( cube_x, cube_y, cube_z );
double cube_t = cub->GetT();
double cube_rad_t = cub->GetTRadius();
double overlap = 0;
std::vector<std::pair< UInt_t, double > > pmt_list;
//// Now loop over hits
std::vector< std::pair< UInt_t, double > > pmts = cub->GetPMTs();
for( size_t i_pmt = 0; i_pmt < pmts.size(); i_pmt++ ) {
//// Loop PMTs & get LLH
const std::pair< UInt_t, double > pmt_pair = pmts.at(i_pmt);
RAT::DU::Point3D cubePos(fAVSystemId, cube_pos);
double emission_t = time_res_calc.CalcTimeResidual( pmt_pair.first, pmt_pair.second, cubePos, cube_t, false, 3.103125 * 1e-6, true, 0.0, false, 800 );
if(emission_t > -cube_rad_t && emission_t < cube_rad_t){
//// If we have time residual close to 0, add one to overlap, and save PMT so it's remains associated with the cube
overlap++;
pmt_list.push_back( pmt_pair );
}
} //// End loop over hits
// std::cout << "Cube at " << cube_x << " " << cube_y << " " << cube_z << " overlap " << overlap << " at " << cube_t << std::endl;
//// Store overlap PMT IDs
cub->SetLLH( overlap );
cub->SetPMTs( pmt_list );
if( overlap > best_global_overlap )
best_global_overlap = overlap;
} //// End loop over cubes
return best_global_overlap;
}