43 static float sumDt = 0;
48 for (
int j = 0;
j <
data.size();
j++) {
49 float dtraw =
data[
j].getDeltaTimePi();
51 if (dt > -50000 && dt < 50000) {
62 if (ntf && (sumdt > 5000 + sumDt / ntf || sumdt < -5000 + sumDt / ntf)) {
68 for (
int i =
data.size();
i--;) {
77 auto ch =
data[
i].getTOFChIndex();
80 auto dt =
data[
i].getDeltaTimePi();
82 auto tot =
data[
i].getTot();
85 auto dtcorr = dt - corr;
99 mChannelDist->Fill(ch, dtcorr);
103 mHisto[sector](dtcorr, chInSect);
106 int istrip = ch / 96;
107 int istripInSector = chInSect / 96;
108 int halffea = (chInSect % 96) / 12;
109 int choffset = (istrip - istripInSector) * 96;
112 int minch = istripInSector * 96 + halffea * 12;
113 int maxch = minch + 12;
114 for (
int ich = minch; ich < maxch; ich++) {
115 mHisto[sector](dtcorr, ich);
116 mEntries[ich + choffset] += 1;
442 LOG(info) <<
"Finalize slot for calibration with cosmics " << slot.
getTFStart() <<
" <= TF <= " << slot.
getTFEnd();
445 std::map<std::string, std::string> md;
449 int nbins =
c->getNbins();
450 float range =
c->getRange();
451 std::vector<int> entriesPerChannel =
c->getEntriesPerChannel();
455 mNThreads = std::min(omp_get_max_threads(), NMAXTHREADS);
457 LOG(
debug) <<
"Number of threads that will be used = " << mNThreads;
458#pragma omp parallel for schedule(dynamic) num_threads(mNThreads)
466 ithread = omp_get_thread_num();
469 LOG(info) <<
"Processing sector " << sector <<
" with thread " << ithread;
470 double xp[NCOMBINSTRIP],
exp[NCOMBINSTRIP], deltat[NCOMBINSTRIP], edeltat[NCOMBINSTRIP];
472 std::array<double, 3> fitValues;
473 std::vector<float> histoValues;
475 auto& histo =
c->getHisto(sector);
480 LOG(info) <<
"Processing strip " << istrip;
482 bool isChON[96] = {
false};
483 int offsetstrip = istrip *
Geo::NPADS + offsetsector;
487 TLinearFitter localFitter(1, mStripOffsetFunction.c_str());
489 int offsetPairInStrip = istrip * NCOMBINSTRIP;
491 localFitter.StoreData(kFALSE);
492 localFitter.ClearPoints();
493 for (
int ipair = 0; ipair < NCOMBINSTRIP; ipair++) {
494 int chinsector = ipair + offsetPairInStrip;
495 int ich = chinsector + offsetPairInSector;
496 auto entriesInPair = entriesPerChannel.at(ich);
497 xp[allpoints] = ipair + 0.5;
499 if (entriesInPair == 0) {
500 localFitter.AddPoint(&(xp[allpoints]), 0.0, 1.0);
504 if (entriesInPair < mMinEntries) {
505 LOG(
debug) <<
"pair " << ich <<
" will not be calibrated since it has only " << entriesInPair <<
" entries (min = " << mMinEntries <<
")";
506 localFitter.AddPoint(&(xp[allpoints]), 0.0, 1.0);
510 fitValues.fill(-99999999);
514 for (
unsigned i = 0;
i < nbins; ++
i) {
515 const auto&
v = histo.at(
i, chinsector);
516 LOG(
debug) <<
"channel = " << ich <<
", in sector = " << sector <<
" (where it is channel = " << chinsector <<
") bin = " <<
i <<
" value = " <<
v;
517 histoValues.push_back(
v);
520 double fitres = entriesInPair - 1;
521 fitres = fitGaus(nbins, histoValues.data(), -
range,
range, fitValues,
nullptr, 2.,
true);
523 LOG(
debug) <<
"Pair " << ich <<
" :: Fit result " << fitres <<
" Mean = " << fitValues[1] <<
" Sigma = " << fitValues[2];
525 LOG(
debug) <<
"Pair " << ich <<
" :: Fit failed with result = " << fitres;
526 localFitter.AddPoint(&(xp[allpoints]), 0.0, 1.0);
531 if (fitValues[2] < 0) {
532 fitValues[2] = -fitValues[2];
535 float intmin = fitValues[1] - 5 * fitValues[2];
536 float intmax = fitValues[1] + 5 * fitValues[2];
538 if (intmin < -mRange) {
541 if (intmax < -mRange) {
544 if (intmin > mRange) {
547 if (intmax > mRange) {
551 xp[allpoints] = ipair + 0.5;
552 exp[allpoints] = 0.0;
553 deltat[allpoints] = fitValues[1];
554 float integral =
c->integral(ich, intmin, intmax);
555 edeltat[allpoints] = 20 + fitValues[2] / sqrt(integral);
556 localFitter.AddPoint(&(xp[allpoints]), deltat[allpoints], edeltat[allpoints]);
559 int ch1 = ipair % 96;
560 int ch2 = ipair / 96 ? ch1 + 48 : ch1 + 1;
564 float fractionUnderPeak = entriesInPair > 0 ? integral / entriesInPair : 0;
566 if (fracUnderPeak[ch1] < fractionUnderPeak) {
567 fracUnderPeak[ch1] = fractionUnderPeak;
569 if (fracUnderPeak[ch2] < fractionUnderPeak) {
570 fracUnderPeak[ch2] = fractionUnderPeak;
580 if (goodpoints == 0) {
583 LOG(
debug) <<
"We found " << goodpoints <<
" good points for strip " << istrip <<
" in sector " << sector <<
" --> we can fit the TGraph";
587 LOG(
debug) <<
"N parameters before fixing = " << localFitter.GetNumberFreeParameters();
590 for (
int i = 0;
i < 96; ++
i) {
593 localFitter.FixParameter(
i, 0.);
599 localFitter.FixParameter(
i, 0.);
603 LOG(
debug) <<
"Strip = " << istrip <<
" fitted by thread = " << ithread <<
", goodpoints = " << goodpoints <<
", number of free parameters = "
604 << localFitter.GetNumberFreeParameters() <<
", NDF = " << goodpoints - localFitter.GetNumberFreeParameters();
606 if (goodpoints <= localFitter.GetNumberFreeParameters()) {
611 LOG(
debug) <<
"N real params = " << nparams <<
", fitter has " << localFitter.GetNumberFreeParameters() <<
" free parameters, " << localFitter.GetNumberTotalParameters() <<
" total parameters, " << localFitter.GetNpoints() <<
" points";
612 LOG(info) <<
"Sector = " << sector <<
", strip = " << istrip <<
" fitted by thread = " << ithread <<
": ready to fit";
615 LOG(info) <<
"Sector = " << sector <<
", strip = " << istrip <<
" fitted by thread = " << ithread <<
" with Chi/NDF " << localFitter.GetChisquare() <<
"/" << goodpoints - localFitter.GetNumberFreeParameters();
616 LOG(
debug) <<
"Strip = " << istrip <<
" fitted by thread = " << ithread <<
" with Chi/NDF " << localFitter.GetChisquare() <<
"/" << goodpoints - localFitter.GetNumberFreeParameters();
623 for (
int ichLocal = 0; ichLocal <
Geo::NPADS; ichLocal++) {
624 int ich = ichLocal + offsetstrip;
627 mFitCal->Fill(ich, localFitter.GetParameter(ichLocal));
642 mInfoVector.emplace_back(
"TOF/Calib/ChannelCalib", clName, flName, md, startValidity, endValidity);
643 mTimeSlewingVector.emplace_back(ts);
646 TFile fout(
"debug_tof_cal.root",
"RECREATE");
648 mChannelDist->Write();
661 int nbins =
c->getNbins();
662 float range =
c->getRange();
663 std::vector<int> entriesPerChannel =
c->getEntriesPerChannel();
666 std::map<std::string, std::string> md;
672 mNThreads = std::min(omp_get_max_threads(), NMAXTHREADS);
674 LOG(
debug) <<
"Number of threads that will be used = " << mNThreads;
675#pragma omp parallel for schedule(dynamic) num_threads(mNThreads)
683 ithread = omp_get_thread_num();
685 LOG(info) <<
"Processing sector " << sector <<
" with thread " << ithread;
686 auto& histo =
c->getHisto(sector);
688 std::array<double, 3> fitValues;
689 std::vector<float> histoValues;
693 auto entriesInChannel = entriesPerChannel.at(ich);
694 if (entriesInChannel == 0) {
699 if (entriesInChannel < mMinEntries) {
700 LOG(
debug) <<
"channel " << ich <<
" will not be calibrated since it has only " << entriesInChannel <<
" entries (min = " << mMinEntries <<
")";
705 LOG(
debug) <<
"channel " << ich <<
" will be calibrated since it has " << entriesInChannel <<
" entries (min = " << mMinEntries <<
")";
706 fitValues.fill(-99999999);
709 int imax = nbins / 2;
711 double binwidth = 2 *
range / nbins;
712 int binrange =
int(1500 / binwidth) + 1;
713 float minRange = -
range;
714 float maxRange =
range;
716 for (
unsigned j = chinsector;
j <= chinsector; ++
j) {
717 for (
unsigned i = 0;
i < nbins; ++
i) {
718 const auto&
v = histo.at(
i,
j);
730 for (
unsigned i = 0;
i < nbins; ++
i) {
731 const auto&
v = histo.at(
i,
j);
732 LOG(
debug) <<
"channel = " << ich <<
", in sector = " << sector <<
" (where it is channel = " << chinsector <<
") bin = " <<
i <<
" value = " <<
v;
733 if (
i >= imax - binrange &&
i < imax + binrange) {
734 histoValues.push_back(
v *
renorm);
740 minRange = (imax - nbins / 2 - binrange) * binwidth;
741 maxRange = (imax - nbins / 2 + binrange) * binwidth;
743 double fitres = fitGaus(nbinsUsed, histoValues.data(), minRange, maxRange, fitValues,
nullptr, 2.,
false);
744 LOG(info) <<
"channel = " << ich <<
" fitted by thread = " << ithread;
746 LOG(info) <<
"Channel " << ich <<
" :: Fit result " << fitres <<
" Mean = " << fitValues[1] <<
" Sigma = " << fitValues[2];
749 FILE*
f = fopen(Form(
"%d.cal", ich),
"w");
750 for (
int i = 0;
i < histoValues.size();
i++) {
751 fprintf(
f,
"%d %f %f\n",
i, minRange + binwidth *
i, histoValues[
i]);
755 LOG(info) <<
"Channel " << ich <<
" :: Fit failed with result = " << fitres;
762 if (fitValues[2] < 0) {
763 fitValues[2] = -fitValues[2];
766 float fractionUnderPeak;
767 float intmin = fitValues[1] - 5 * fitValues[2];
768 float intmax = fitValues[1] + 5 * fitValues[2];
770 if (intmin < -mRange) {
773 if (intmax < -mRange) {
776 if (intmin > mRange) {
779 if (intmax > mRange) {
783 fractionUnderPeak = entriesInChannel > 0 ?
c->integral(ich, intmin, intmax) / entriesInChannel : 0;
791 if (std::abs(fitValues[1]) > mRange) {
805 mFitCal->Fill(ich, fitValues[1]);
807 LOG(
debug) <<
"udpdate channel " << ich <<
" with " << fitValues[1] <<
" offset in ps";
816 mInfoVector.emplace_back(
"TOF/Calib/ChannelCalib", clName, flName, md, startValidity, endValidity);
817 mTimeSlewingVector.emplace_back(ts);
820 TFile fout(
"debug_tof_cal.root",
"RECREATE");
822 mChannelDist->Write();