@@ -698,19 +698,52 @@ void TOFChannelCalibrator<T>::finalizeSlotWithTracks(Slot& slot)
698698 fitValues.fill (-99999999 );
699699 histoValues.clear ();
700700 // more efficient way
701+ int imax = nbins / 2 ;
702+ double maxval = 0 ;
703+ double binwidth = 2 * range / nbins;
704+ int binrange = int (1500 / binwidth) + 1 ;
705+ float minRange = -range;
706+ float maxRange = range;
707+ int nbinsUsed = 0 ;
701708 for (unsigned j = chinsector; j <= chinsector; ++j) {
709+ for (unsigned i = 0 ; i < nbins; ++i) { // find peak
710+ const auto & v = histo.at (i, j);
711+ if (v > maxval) {
712+ maxval = v;
713+ imax = i;
714+ }
715+ }
716+
717+ float renorm = 1 .; // to avoid fit problem when stats is too large (bad chi2)
718+ if (maxval > 10 ) {
719+ renorm = 10 . / maxval;
720+ }
721+
702722 for (unsigned i = 0 ; i < nbins; ++i) {
703723 const auto & v = histo.at (i, j);
704724 LOG (debug) << " channel = " << ich << " , in sector = " << sector << " (where it is channel = " << chinsector << " ) bin = " << i << " value = " << v;
705- histoValues.push_back (v);
725+ if (i >= imax - binrange && i < imax + binrange) {
726+ histoValues.push_back (v * renorm);
727+ nbinsUsed++;
728+ } // not count for entries far from the peak (fit optimization)
706729 }
707730 }
708731
709- double fitres = fitGaus (nbins, histoValues.data (), -range, range, fitValues, nullptr , 2 ., true );
732+ minRange = (imax - nbins / 2 - binrange) * binwidth;
733+ maxRange = (imax - nbins / 2 + binrange) * binwidth;
734+
735+ double fitres = fitGaus (nbinsUsed, histoValues.data (), minRange, maxRange, fitValues, nullptr , 2 ., false );
710736 LOG (info) << " channel = " << ich << " fitted by thread = " << ithread;
711- if (fitres >= 0 ) {
737+ if (fitres > - 3 ) {
712738 LOG (info) << " Channel " << ich << " :: Fit result " << fitres << " Mean = " << fitValues[1 ] << " Sigma = " << fitValues[2 ];
713739 } else {
740+ #ifdef DEBUGGING
741+ FILE * f = fopen (Form (" %d.cal" , ich), " w" );
742+ for (int i = 0 ; i < histoValues.size (); i++) {
743+ fprintf (f, " %d %f %f\n " , i, minRange + binwidth * i, histoValues[i]);
744+ }
745+ fclose (f);
746+ #endif
714747 LOG (info) << " Channel " << ich << " :: Fit failed with result = " << fitres;
715748 ts.setFractionUnderPeak (ich / Geo::NPADSXSECTOR , ich % Geo::NPADSXSECTOR , -1 );
716749 ts.setSigmaPeak (ich / Geo::NPADSXSECTOR , ich % Geo::NPADSXSECTOR , 99999 );
0 commit comments