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
use crate::errors::Error;
use crate::headers::{File, RecordType};
use crate::parsers::ptu;
use crate::tttr_tools::circular_buffer::CircularBuffer;
use crate::{Click, TTTRFile, TTTRStream};
use std::fmt::Debug;
const MAX_BUFFER_SIZE: usize = 4096;
struct G2<P: TTTRStream + Iterator> {
pub click_stream: P,
pub params: G2Params,
}
pub struct G2Result {
pub t: Vec<f64>,
pub hist: Vec<u64>,
}
#[derive(Debug, Copy, Clone)]
pub struct G2Params {
pub channel_1: i32,
pub channel_2: i32,
pub correlation_window: f64,
pub resolution: f64,
pub start_record: Option<usize>,
pub stop_record: Option<usize>,
}
impl<P: TTTRStream + Iterator> G2<P> {
fn compute(self) -> G2Result
where
<P as Iterator>::Item: Debug + Click,
{
let real_resolution = self.params.resolution.clone();
let n_bins = (self.params.correlation_window / self.params.resolution) as u64;
let correlation_window =
self.params.correlation_window / (self.click_stream.time_resolution());
let resolution = (correlation_window / (n_bins as f64)) as u64;
let correlation_window = n_bins * resolution;
let n_bins = n_bins * 2;
let central_bin = n_bins / 2;
let mut histogram = vec![0; n_bins as usize];
let mut buff_1 = CircularBuffer::new(MAX_BUFFER_SIZE);
let mut buff_2 = CircularBuffer::new(MAX_BUFFER_SIZE);
for rec in self.click_stream.into_iter() {
let (tof, channel) = (*rec.tof(), *rec.channel());
if channel == self.params.channel_1 {
buff_1.push(tof);
for click in buff_2.iter() {
let delta = tof - click;
if delta < correlation_window {
let hist_idx = central_bin - delta / resolution - 1;
histogram[hist_idx as usize] += 1;
} else {
break;
}
}
} else if channel == self.params.channel_2 {
buff_2.push(tof);
for click in buff_1.iter() {
let delta = tof - click;
if delta < correlation_window {
let hist_idx = central_bin + delta / resolution;
histogram[hist_idx as usize] += 1;
} else {
break;
}
}
}
}
let t = (0..n_bins)
.map(|i| ((i as f64) - (central_bin as f64)) * real_resolution)
.collect::<Vec<f64>>();
G2Result {
t: t,
hist: histogram,
}
}
}
pub fn g2(f: &File, params: &G2Params) -> Result<G2Result, Error> {
let start_record = params.start_record;
let stop_record = params.stop_record;
match f {
File::PTU(x) => match x.record_type().unwrap() {
RecordType::PHT2 => {
let stream = ptu::streamers::PHT2Stream::new(x, start_record, stop_record)?;
let tt = G2 {
click_stream: stream,
params: *params,
};
Ok(tt.compute())
}
RecordType::HHT2_HH1 => {
let stream = ptu::streamers::HHT2_HH1Stream::new(x, start_record, stop_record)?;
let tt = G2 {
click_stream: stream,
params: *params,
};
Ok(tt.compute())
}
RecordType::HHT2_HH2 => {
let stream = ptu::streamers::HHT2_HH2Stream::new(x, start_record, stop_record)?;
let tt = G2 {
click_stream: stream,
params: *params,
};
Ok(tt.compute())
}
RecordType::HHT3_HH2 => {
let stream = ptu::streamers::HHT3_HH2Stream::new(x, start_record, stop_record)?;
let tt = G2 {
click_stream: stream,
params: *params,
};
Ok(tt.compute())
}
RecordType::NotImplemented => panic! {"Record type not implemented"},
},
}
}