@@ -218,56 +218,76 @@ void CalibTimeSlewingParamTOF::setTimeSlewingInfo(int channel, float offsetold,
218218 // work with float to avoid to loose precision due to short (in case offset will be adjust before to move deltat to short)
219219 std::vector<float > deltat;
220220
221- float minVal = 0 ;
222- float maxVal = 0 ;
223-
224- if (nnew >= nold) { // use new binning
225- int j = 0 ;
226- for (int i = 0 ; i < nnew; i++) {
227- while (j < nold && oldtot[j + 1 ] < newtot[i]) {
228- j++;
221+ float minVal = 100000 ;
222+ float maxVal = -100000 ;
223+
224+ // new scheme
225+ int k1 = 0 ;
226+ int k2 = 0 ;
227+
228+ int ntotFin = 0 ;
229+ std::vector<short > totFin;
230+
231+ while (k1 < nnew && k2 < nold) {
232+ if (newtot[k1] == oldtot[k2]) {
233+ k1++;
234+ } else if (newtot[k1] < oldtot[k2]) {
235+ totFin.push_back (newtot[k1]);
236+ if (k2 > 0 ) {
237+ deltat.push_back (float (newdt[k1]) + olddt[k2 - 1 ]);
238+ } else {
239+ deltat.push_back (float (newdt[k1] + olddt[k2]));
229240 }
230- float val = float (newdt[i]) + olddt[j];
231- // printf("%d %d -> %f\n",i,j,val);
232- deltat.push_back (val);
233- if (val < minVal) {
234- minVal = val;
241+ k1++;
242+ } else {
243+ totFin.push_back (oldtot[k2]);
244+ if (k1 > 0 ) {
245+ deltat.push_back (float (olddt[k2]) + newdt[k1 - 1 ]);
246+ } else {
247+ deltat.push_back (float (olddt[k2] + newdt[k1]));
235248 }
236- if (val > maxVal) {
237- maxVal = val;
238- }
239- }
240- float recentering = (minVal + maxVal) * 0.5 ;
241- (*(mGlobalOffset [sector]))[channel] = offsetold + recentering;
242- // printf("update channel offset for sec=%d/ch=%d -> %f\n",sector,channel,(*(mGlobalOffset[sector]))[channel]);
243- (*(mChannelStart [sector]))[channel] = mTimeSlewing [sector]->size ();
244-
245- for (int i = 0 ; i < nnew; i++) {
246- (*(mTimeSlewing [sector])).emplace_back (newtot[i], (short )(deltat[i] - recentering));
247- // printf("%d) %d\n",i,(*(mTimeSlewing[sector]))[i]);
249+ k2++;
248250 }
249- } else { // use old binning
250- int j = 0 ;
251- for (int i = 0 ; i < nold; i++) {
252- while (j < nnew && newtot[j + 1 ] < oldtot[i]) {
253- j++;
254- }
255- float val = float (olddt[i]) + newdt[j];
256- deltat.push_back (val);
257- if (val < minVal) {
258- minVal = val;
251+ }
252+
253+ if (k1 < nnew) {
254+ while (k1 < nnew) {
255+ totFin.push_back (newtot[k1]);
256+ if (nold) {
257+ deltat.push_back (float (newdt[k1]) + olddt[nold - 1 ]);
258+ } else {
259+ deltat.push_back (float (newdt[k1]));
259260 }
260- if (val > maxVal) {
261- maxVal = val;
261+ k1++;
262+ }
263+ } else if (k2 < nold) {
264+ while (k2 < nold) {
265+ totFin.push_back (oldtot[k2]);
266+ if (nnew) {
267+ deltat.push_back (float (olddt[k2]) + newdt[nnew - 1 ]);
268+ } else {
269+ deltat.push_back (float (olddt[k2]));
262270 }
271+ k2++;
263272 }
264- float recentering = (minVal + maxVal) * 0.5 ;
265- (*(mGlobalOffset [sector]))[channel] = offsetold + recentering;
266- (*(mChannelStart [sector]))[channel] = mTimeSlewing [sector]->size ();
267- for (int i = 0 ; i < nold; i++) {
268- (*(mTimeSlewing [sector])).emplace_back (oldtot[i], (short )(deltat[i] - recentering));
273+ }
274+
275+ for (int i = 0 ; i < deltat.size (); i++) {
276+ if (deltat[i] < minVal) {
277+ minVal = deltat[i];
278+ }
279+ if (deltat[i] > maxVal) {
280+ maxVal = deltat[i];
269281 }
270282 }
283+
284+ float recentering = (minVal + maxVal) * 0.5 ;
285+ (*(mGlobalOffset [sector]))[channel] = offsetold + recentering;
286+ (*(mChannelStart [sector]))[channel] = mTimeSlewing [sector]->size ();
287+
288+ for (int i = 0 ; i < deltat.size (); i++) {
289+ (*(mTimeSlewing [sector])).emplace_back (totFin[i], (short )(deltat[i] - recentering));
290+ }
271291}
272292// ______________________________________________
273293void CalibTimeSlewingParamTOF::addTimeSlewingInfo (int channel, float tot, float time)
0 commit comments