update parameters for future event Note: DO NOT use the finalT1 here. t1New and finalT1 are not consistent
91 {
92 long t1Old = header->
etsT1();
93
94 if ( t1Old == 0 ) { return; }
95
97 int tnSec = header->
time() - m_t0Sec;
98 long t1NanoSec = t1Old % OneSecVar;
99 long t1New = t1NanoSec;
100
102
103 if ( m_count != 0 )
104 {
105
106 int curIdx = tnSec % m_nPre;
107
108 int preIdx = curIdx;
109 int secShift = 0;
110 if ( tnSec != m_preSec[preIdx] )
111 {
112 preIdx = ( preIdx + m_nPre - 1 ) % m_nPre;
113 secShift = 1;
114 while ( m_preEvt[preIdx] == -999 ||
115 m_preSec[preIdx] < m_preSec[( preIdx + 1 ) % m_nPre] )
116 {
117 preIdx = ( preIdx + m_nPre - 1 ) % m_nPre;
118 ++secShift;
119 }
120 if ( ( m_preSec[preIdx] + secShift ) <= ( tnSec - m_nPre ) )
121 { secShift += m_nPre * ( ( tnSec - secShift - m_preSec[preIdx] ) / m_nPre ); }
122
123
124 }
125 t1New += ( m_preT1[preIdx] / OneSecVar + secShift ) * OneSecVar;
126
127
128
129 int eDiff = evtNo - m_preEvt[preIdx];
130 long tDiff = t1New - m_preT1[preIdx];
131 long t1Expect = m_preT1[preIdx] + m_refSlope * eDiff;
132 long t1Shift = t1New - t1Expect;
133 if ( t1Shift > ShiftThreshold )
134 {
135 t1New -= OneSecVar;
136 tDiff -= OneSecVar;
137 }
138 else if ( t1Shift < -ShiftThreshold )
139 {
140 t1New += OneSecVar;
141 tDiff += OneSecVar;
142 }
143
144
145
146
147 if ( tnSec - m_t0SecLocal > 3 )
148 {
149 long tXX = long( tnSec ) * OneSecVar - t1New -
150 long( tnSec - t1Old / OneSecVar ) * OneSecVar * ( m_factor - 1 );
151
152
153
154 while ( tXX < -m_critical - OneSecVar * 0.04 )
155 {
156 t1New -= OneSecVar;
157 tDiff -= OneSecVar;
158 tXX += OneSecVar;
159 }
160 while ( tXX > OneSecVar * 1.04 - m_critical )
161 {
162 t1New += OneSecVar;
163 tDiff += OneSecVar;
164 tXX -= OneSecVar;
165 }
166 }
167 else
168 {
169 if ( secShift == 1 )
170 {
171 m_critical = t1New - long( tnSec - 1 ) * OneSecVar;
172
173
174 }
175 }
176
177
178
179 bool noNeighbor = true;
180 int nearestIdx = NLastBackup - 1;
181 int nearestDE = 10000000;
182 for ( int i = nearestIdx; i >= 0; --i )
183 {
184 int lidx = ( m_count + i ) % NLastBackup;
185 int lediff = evtNo - m_lastEvt[lidx];
186 if (
abs( lediff ) < 20 )
187 {
188 long ltdiff = t1New - m_lastT1[lidx];
189
190 while ( ltdiff > ShiftThreshold )
191 {
192 t1New -= OneSecVar;
193 tDiff -= OneSecVar;
194 ltdiff -= OneSecVar;
195 }
196 while ( ltdiff < -ShiftThreshold )
197 {
198 t1New += OneSecVar;
199 tDiff += OneSecVar;
200 ltdiff += OneSecVar;
201 }
202 if ( lediff > 0 )
203 {
204 while ( ltdiff < 0 )
205 {
206 t1New += OneSecVar;
207 tDiff += OneSecVar;
208 ltdiff += OneSecVar;
209 }
210 }
211 else
212 {
213 while ( ltdiff > 0 )
214 {
215 t1New -= OneSecVar;
216 tDiff -= OneSecVar;
217 ltdiff -= OneSecVar;
218 }
219 }
220 noNeighbor = false;
221 break;
222 }
223 else
224 {
225 if (
abs( lediff ) < nearestDE ) { nearestIdx = i; }
226 }
227 }
228
229
230
231 if ( noNeighbor )
232 {
233 int lidx = ( m_count + nearestIdx ) % NLastBackup;
234 int lediff = evtNo - m_lastEvt[lidx];
235 long ltdiff = t1New - m_lastT1[lidx];
236
237 if ( lediff > 0 )
238 {
239 while ( ltdiff * 1.0 / lediff < m_refSlope * 0.2 )
240 {
241 t1New += OneSecVar;
242 tDiff += OneSecVar;
243 ltdiff += OneSecVar;
244 if ( ltdiff > OneSecVar && ltdiff * 1.0 / lediff > m_refSlope * 5 )
245 {
246 t1New -= OneSecVar;
247 tDiff -= OneSecVar;
248 ltdiff -= OneSecVar;
249 break;
250 }
251 }
252 }
253 else
254 {
255 while ( ltdiff * 1.0 / lediff > m_refSlope * 5 )
256 {
257 t1New += OneSecVar;
258 tDiff += OneSecVar;
259 ltdiff += OneSecVar;
260 }
261 while ( ltdiff > 0 )
262 {
263 t1New -= OneSecVar;
264 tDiff -= OneSecVar;
265 ltdiff -= OneSecVar;
266 }
267 while ( ltdiff * 1.0 / lediff < m_refSlope * 0.2 )
268 {
269 t1New -= OneSecVar;
270 tDiff -= OneSecVar;
271 ltdiff -= OneSecVar;
272 }
273 }
274 }
275
276
277
278 float curSlope = ( 1.0 * tDiff ) / eDiff;
279 if ( curSlope > 0 )
280 {
281 if ( m_count > 30 )
282 {
283 float guard = curSlope / m_refSlope;
284 if ( guard > 0.75 && guard < 1.5 ) { m_refSlope = m_refSlope * 0.9 + curSlope * 0.1; }
285 }
286 else { m_refSlope = ( m_refSlope * ( m_count - 1 ) + curSlope ) / m_count; }
287 }
288
289
290
291 if ( secShift == 1 )
292 {
293
294 m_preEvt[curIdx] = evtNo;
295 m_preSec[curIdx] = tnSec;
296 m_preT1[curIdx] = t1New;
297 }
298 m_lastEvt[m_count % NLastBackup] = evtNo;
299 m_lastT1[m_count % NLastBackup] = t1New;
300
301
302 if ( t1New != t1Old )
303 {
304 int t1Sec = t1New / OneSecVar;
305 int fIdx = t1Sec % m_nPre;
306 if ( t1Sec == m_preT1Sec[fIdx] ) { m_pileup = m_prePileup[fIdx]; }
307 else
308 {
309 for ( int i = 1; i < m_nPre; ++i )
310 {
311 int pT1Sec = t1Sec - i;
312 int pFIdx = ( pT1Sec + m_nPre ) % m_nPre;
313 if ( pT1Sec == m_preT1Sec[pFIdx] )
314 {
315 int fTotalPileup = t1Sec - t1Old / OneSecVar;
316 int pTotalPileup = m_preT1Sec[pFIdx] - m_preT1Old[pFIdx] / OneSecVar;
317 if ( fTotalPileup != pTotalPileup )
318 {
319 m_pileup = m_prePileup[pFIdx] + long( fTotalPileup - pTotalPileup ) * OneSecVar;
320 }
321 else
322 {
323 m_pileup = 0;
324 }
325 break;
326 }
327 }
328 m_preT1Sec[fIdx] = t1Sec;
329 m_preT1Old[fIdx] = t1Old;
330 m_prePileup[fIdx] = m_pileup;
331 }
332 long finalT1 = t1New + ( m_factor - 1.0 ) * m_pileup;
333 if ( finalT1 > 0 ) { header->
setEtsT1( finalT1 ); }
335 }
336
337
338
339
340
341
342 }
343 else
344 {
345 int firstSec = t1Old / OneSecVar;
346 int firstIdx = firstSec % m_nPre;
347 m_t0Sec = header->
time() - firstSec;
348 m_t0SecLocal = firstSec;
349 m_preEvt[firstIdx] = evtNo;
350 m_preSec[firstIdx] = firstSec;
351 m_preT1[firstIdx] = t1Old;
352
353 m_lastEvt[0] = evtNo;
354 m_lastT1[0] = t1Old;
355
356 m_pileup = 0;
357 m_preT1Sec[firstIdx] = firstSec;
358 m_preT1Old[firstIdx] = t1Old;
359 m_prePileup[firstIdx] = 0;
360 }
361
362 ++m_count;
363
364
365
366
367
368
369
370
371
372
373
374}