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
use crate::errors::Error;
use crate::headers::{File, RecordType};
use crate::parsers::ptu;
use crate::tttr_tools::colored_circular_buffer::CCircularBuffer;
use crate::{Click, TTTRFile, TTTRStream};
use std::fmt::Debug;
use ndarray::Array2;
const MAX_BUFFER_SIZE: usize = 4096;
struct G3Sync<P: TTTRStream + Iterator> {
pub click_stream: P,
pub params: G3SyncParams,
pub sync_period: u64,
}
pub struct G3SyncResult {
pub t: Vec<f64>,
pub hist: Array2<u64>,
}
#[derive(Debug, Copy, Clone)]
pub struct G3SyncParams {
pub channel_sync: i32,
pub channel_1: i32,
pub channel_2: i32,
pub resolution: f64,
pub start_record: Option<usize>,
pub stop_record: Option<usize>,
}
impl<P: TTTRStream + Iterator> G3Sync<P> {
fn compute(self) -> G3SyncResult
where
<P as Iterator>::Item: Debug + Click,
{
let real_resolution = self.params.resolution.clone();
let correlation_window = (self.sync_period as f64) * 1e-12;
let n_bins = (correlation_window / self.params.resolution) as u64;
let resolution = self.sync_period / n_bins as u64;
let mut histogram = Array2::<u64>::zeros((n_bins as usize, n_bins as usize));
let mut click_buffer = CCircularBuffer::new(MAX_BUFFER_SIZE);
let relevant_channels: Vec<i32> =
vec![self.params.channel_sync, self.params.channel_1, self.params.channel_2];
for click_1 in self.click_stream.into_iter() {
let (&tof1, &chn1) = (click_1.tof(), click_1.channel());
if !relevant_channels.contains(&chn1) {
continue;
}
for click_2 in click_buffer.iter() {
let &(tof2, chn2) = click_2;
let delta12 = tof1 - tof2;
if delta12 > self.sync_period {
break;
}
for click_3 in click_buffer.iter() {
let &(tof3, chn3) = click_3;
if tof3 >= tof2 {
continue;
}
let delta23 = tof2 - tof3;
let delta13 = delta12 + delta23;
if chn1 == self.params.channel_1 {
if chn2 == self.params.channel_2 {
if chn3 == self.params.channel_sync {
let tau1 = delta13;
let tau2 = delta12;
let idx1 = (tau1 % self.sync_period) / resolution;
let idx2 = (tau2 % self.sync_period) / resolution;
if idx1 < n_bins && idx2 < n_bins {
histogram[[idx1 as usize, idx2 as usize]] += 1;
break;
}
}
}
} else if chn1 == self.params.channel_2 {
if chn2 == self.params.channel_1 {
if chn3 == self.params.channel_sync {
let tau1 = delta12;
let tau2 = delta13;
let idx1 = (tau1 % self.sync_period) / resolution;
let idx2 = (tau2 % self.sync_period) / resolution;
if idx1 < n_bins && idx2 < n_bins {
histogram[[idx1 as usize, idx2 as usize]] += 1;
break;
}
}
}
}
}
}
click_buffer.push(tof1, chn1);
}
let t = (0..n_bins)
.map(|i| (i as f64) * real_resolution)
.collect::<Vec<f64>>();
G3SyncResult {
t: t,
hist: histogram,
}
}
}
pub fn g3_sync(f: &File, params: &G3SyncParams) -> Result<G3SyncResult, 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 => Err(Error::NotImplemented(String::from(
"The synced g3 algorithm is only supported in T3 mode",
))),
RecordType::HHT2_HH1 => Err(Error::NotImplemented(String::from(
"The synced algorithm is only supported in T3 mode",
))),
RecordType::HHT2_HH2 => Err(Error::NotImplemented(String::from(
"The synced algorithm is only supported in T3 mode",
))),
RecordType::HHT3_HH2 => {
let stream = ptu::streamers::HHT3_HH2Stream::new(x, start_record, stop_record)?;
let sync_period = stream.sync_period;
let tt = G3Sync {
click_stream: stream,
params: *params,
sync_period,
};
Ok(tt.compute())
}
RecordType::NotImplemented => panic! {"Record type not implemented"},
},
}
}