92 long t1Old = header->
etsT1();
94 if ( t1Old == 0 ) {
return; }
97 int tnSec = header->
time() - m_t0Sec;
98 long t1NanoSec = t1Old % OneSecVar;
99 long t1New = t1NanoSec;
106 int curIdx = tnSec % m_nPre;
110 if ( tnSec != m_preSec[preIdx] )
112 preIdx = ( preIdx + m_nPre - 1 ) % m_nPre;
114 while ( m_preEvt[preIdx] == -999 ||
115 m_preSec[preIdx] < m_preSec[( preIdx + 1 ) % m_nPre] )
117 preIdx = ( preIdx + m_nPre - 1 ) % m_nPre;
120 if ( ( m_preSec[preIdx] + secShift ) <= ( tnSec - m_nPre ) )
121 { secShift += m_nPre * ( ( tnSec - secShift - m_preSec[preIdx] ) / m_nPre ); }
125 t1New += ( m_preT1[preIdx] / OneSecVar + secShift ) * OneSecVar;
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 )
138 else if ( t1Shift < -ShiftThreshold )
147 if ( tnSec - m_t0SecLocal > 3 )
149 long tXX = long( tnSec ) * OneSecVar - t1New -
150 long( tnSec - t1Old / OneSecVar ) * OneSecVar * ( m_factor - 1 );
154 while ( tXX < -m_critical - OneSecVar * 0.04 )
160 while ( tXX > OneSecVar * 1.04 - m_critical )
171 m_critical = t1New - long( tnSec - 1 ) * OneSecVar;
179 bool noNeighbor =
true;
180 int nearestIdx = NLastBackup - 1;
181 int nearestDE = 10000000;
182 for (
int i = nearestIdx; i >= 0; --i )
184 int lidx = ( m_count + i ) % NLastBackup;
185 int lediff = evtNo - m_lastEvt[lidx];
186 if (
abs( lediff ) < 20 )
188 long ltdiff = t1New - m_lastT1[lidx];
190 while ( ltdiff > ShiftThreshold )
196 while ( ltdiff < -ShiftThreshold )
225 if (
abs( lediff ) < nearestDE ) { nearestIdx = i; }
233 int lidx = ( m_count + nearestIdx ) % NLastBackup;
234 int lediff = evtNo - m_lastEvt[lidx];
235 long ltdiff = t1New - m_lastT1[lidx];
239 while ( ltdiff * 1.0 / lediff < m_refSlope * 0.2 )
244 if ( ltdiff > OneSecVar && ltdiff * 1.0 / lediff > m_refSlope * 5 )
255 while ( ltdiff * 1.0 / lediff > m_refSlope * 5 )
267 while ( ltdiff * 1.0 / lediff < m_refSlope * 0.2 )
278 float curSlope = ( 1.0 * tDiff ) / eDiff;
283 float guard = curSlope / m_refSlope;
284 if ( guard > 0.75 && guard < 1.5 ) { m_refSlope = m_refSlope * 0.9 + curSlope * 0.1; }
286 else { m_refSlope = ( m_refSlope * ( m_count - 1 ) + curSlope ) / m_count; }
294 m_preEvt[curIdx] = evtNo;
295 m_preSec[curIdx] = tnSec;
296 m_preT1[curIdx] = t1New;
298 m_lastEvt[m_count % NLastBackup] = evtNo;
299 m_lastT1[m_count % NLastBackup] = t1New;
302 if ( t1New != t1Old )
304 int t1Sec = t1New / OneSecVar;
305 int fIdx = t1Sec % m_nPre;
306 if ( t1Sec == m_preT1Sec[fIdx] ) { m_pileup = m_prePileup[fIdx]; }
309 for (
int i = 1; i < m_nPre; ++i )
311 int pT1Sec = t1Sec - i;
312 int pFIdx = ( pT1Sec + m_nPre ) % m_nPre;
313 if ( pT1Sec == m_preT1Sec[pFIdx] )
315 int fTotalPileup = t1Sec - t1Old / OneSecVar;
316 int pTotalPileup = m_preT1Sec[pFIdx] - m_preT1Old[pFIdx] / OneSecVar;
317 if ( fTotalPileup != pTotalPileup )
319 m_pileup = m_prePileup[pFIdx] + long( fTotalPileup - pTotalPileup ) * OneSecVar;
328 m_preT1Sec[fIdx] = t1Sec;
329 m_preT1Old[fIdx] = t1Old;
330 m_prePileup[fIdx] = m_pileup;
332 long finalT1 = t1New + ( m_factor - 1.0 ) * m_pileup;
333 if ( finalT1 > 0 ) { header->
setEtsT1( finalT1 ); }
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;
353 m_lastEvt[0] = evtNo;
357 m_preT1Sec[firstIdx] = firstSec;
358 m_preT1Old[firstIdx] = t1Old;
359 m_prePileup[firstIdx] = 0;