5static const int NLastBackup = 10;
6static const long OneSecVar = 2000000;
7static const long ShiftThreshold = OneSecVar*0.5;
22 : m_nPre(preRefCount),
28 m_critical(OneSecVar+999),
31 m_preEvt =
new int[m_nPre];
32 m_preSec =
new int[m_nPre];
33 m_preT1 =
new long[m_nPre];
35 m_preT1Sec =
new int[m_nPre];
36 m_preT1Old =
new long[m_nPre];
37 m_prePileup =
new long[m_nPre];
39 for (
int i = 0; i < m_nPre; ++i ) {
46 m_prePileup[i] = -999;
49 m_lastEvt =
new int[NLastBackup];
50 m_lastT1 =
new long[NLastBackup];
52 for (
int i = 0; i < NLastBackup; ++i ) {
82 delete [] m_prePileup;
93 long t1Old = header->
etsT1();
100 int tnSec = header->
time() - m_t0Sec;
101 long t1NanoSec = t1Old % OneSecVar;
102 long t1New = t1NanoSec;
106 if ( m_count != 0 ) {
108 int curIdx = tnSec % m_nPre;
112 if ( tnSec != m_preSec[preIdx] ) {
113 preIdx = (preIdx+m_nPre-1) % m_nPre;
115 while ( m_preEvt[preIdx] == -999 || m_preSec[preIdx] < m_preSec[(preIdx+1)%m_nPre] ) {
116 preIdx = (preIdx+m_nPre-1) % m_nPre;
119 if ( (m_preSec[preIdx]+secShift) <= (tnSec-m_nPre) ) {
120 secShift += m_nPre * ((tnSec-secShift-m_preSec[preIdx]) / m_nPre);
124 t1New += (m_preT1[preIdx]/OneSecVar + secShift) * OneSecVar;
128 int eDiff = evtNo - m_preEvt[preIdx];
129 long tDiff = t1New - m_preT1[preIdx];
130 long t1Expect = m_preT1[preIdx] + m_refSlope * eDiff;
131 long t1Shift = t1New - t1Expect;
132 if ( t1Shift > ShiftThreshold ) {
136 else if ( t1Shift < -ShiftThreshold ) {
144 if (tnSec - m_t0SecLocal > 3) {
145 long tXX = long(tnSec)*OneSecVar - t1New - long(tnSec-t1Old/OneSecVar)*OneSecVar*(m_factor-1);
148 while ( tXX < -m_critical-OneSecVar*0.04 ) {
153 while ( tXX > OneSecVar*1.04-m_critical ) {
160 if ( secShift == 1 ) {
161 m_critical = t1New-long(tnSec-1)*OneSecVar;
168 bool noNeighbor =
true;
169 int nearestIdx = NLastBackup-1;
170 int nearestDE = 10000000;
171 for (
int i = nearestIdx; i >= 0; --i) {
172 int lidx = (m_count+i)%NLastBackup;
173 int lediff = evtNo-m_lastEvt[lidx];
174 if (
abs(lediff) < 20) {
175 long ltdiff = t1New - m_lastT1[lidx];
177 while ( ltdiff > ShiftThreshold ) {
182 while ( ltdiff < -ShiftThreshold ) {
188 while ( ltdiff < 0 ) {
195 while ( ltdiff > 0 ) {
205 if (
abs(lediff) < nearestDE) {
214 int lidx = (m_count+nearestIdx)%NLastBackup;
215 int lediff = evtNo-m_lastEvt[lidx];
216 long ltdiff = t1New - m_lastT1[lidx];
219 while ( ltdiff*1.0/lediff < m_refSlope*0.2 ) {
223 if ( ltdiff > OneSecVar && ltdiff*1.0/lediff > m_refSlope*5 ) {
232 while ( ltdiff*1.0/lediff > m_refSlope*5 ) {
237 while ( ltdiff > 0 ) {
242 while ( ltdiff*1.0/lediff < m_refSlope*0.2 ) {
252 float curSlope = (1.0 * tDiff) / eDiff;
253 if ( curSlope > 0 ) {
255 float guard = curSlope / m_refSlope;
256 if ( guard > 0.75 && guard < 1.5 ) {
257 m_refSlope = m_refSlope*0.9 + curSlope*0.1;
261 m_refSlope = (m_refSlope*(m_count-1) + curSlope) / m_count;
267 if ( secShift == 1 ) {
269 m_preEvt[curIdx] = evtNo;
270 m_preSec[curIdx] = tnSec;
271 m_preT1[curIdx] = t1New;
273 m_lastEvt[m_count%NLastBackup] = evtNo;
274 m_lastT1[m_count%NLastBackup] = t1New;
277 if ( t1New != t1Old ) {
278 int t1Sec = t1New/OneSecVar;
279 int fIdx = t1Sec % m_nPre;
280 if ( t1Sec == m_preT1Sec[fIdx] ) {
281 m_pileup = m_prePileup[fIdx];
284 for (
int i = 1; i < m_nPre; ++i) {
285 int pT1Sec = t1Sec - i;
286 int pFIdx = (pT1Sec + m_nPre) % m_nPre;
287 if ( pT1Sec == m_preT1Sec[pFIdx] ) {
288 int fTotalPileup = t1Sec - t1Old/OneSecVar;
289 int pTotalPileup = m_preT1Sec[pFIdx] - m_preT1Old[pFIdx]/OneSecVar;
290 if ( fTotalPileup != pTotalPileup ) {
291 m_pileup = m_prePileup[pFIdx] + long(fTotalPileup - pTotalPileup)*OneSecVar;
299 m_preT1Sec[fIdx] = t1Sec;
300 m_preT1Old[fIdx] = t1Old;
301 m_prePileup[fIdx] = m_pileup;
303 long finalT1 = t1New + (m_factor-1.0)*m_pileup;
319 int firstSec = t1Old / OneSecVar;
320 int firstIdx = firstSec % m_nPre;
321 m_t0Sec = header->
time() - firstSec;
322 m_t0SecLocal = firstSec;
323 m_preEvt[firstIdx] = evtNo;
324 m_preSec[firstIdx] = firstSec;
325 m_preT1[firstIdx] = t1Old;
327 m_lastEvt[0] = evtNo;
331 m_preT1Sec[firstIdx] = firstSec;
332 m_preT1Old[firstIdx] = t1Old;
333 m_prePileup[firstIdx] = 0;
EtsFixing(int preRefCount=30, double factor=1.0000115)
void fixT1(Event::EventHeader *header)