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