296 TH1::AddDirectory(kFALSE);
334 for (
size_t wireIter = 0; wireIter < wireVecHandle->size(); wireIter++) {
343 std::vector<geo::WireID> wids = geom->
ChannelToWire(channel);
348 std::cout << std::endl;
349 std::cout << std::endl;
350 std::cout << std::endl;
351 std::cout <<
"-----------------------------------------------------------------------------" 352 "------------------------------" 354 std::cout <<
"Channel: " << channel << std::endl;
355 std::cout <<
"Cryostat: " << wid.
Cryostat << std::endl;
356 std::cout <<
"TPC: " << wid.
TPC << std::endl;
357 std::cout <<
"Plane: " << wid.
Plane << std::endl;
358 std::cout <<
"Wire: " << wid.
Wire << std::endl;
368 for (
const auto& range : signalROI.
get_ranges()) {
372 const std::vector<float>& signal = range.data();
383 std::vector<float> timeAve;
395 if (timeValsVec.empty())
continue;
414 std::cout << std::endl;
415 std::cout << std::endl;
416 std::cout <<
"-------------------- ROI #" << CountROI <<
" -------------------- " 418 if (timeValsVec.size() == 1)
419 std::cout <<
"ROI #" << CountROI <<
" (" << timeValsVec.size()
420 <<
" peak): ROIStartTick: " << range.offset
421 <<
" ROIEndTick:" << range.offset + range.size() << std::endl;
423 std::cout <<
"ROI #" << CountROI <<
" (" << timeValsVec.size()
424 <<
" peaks): ROIStartTick: " << range.offset
425 <<
" ROIEndTick:" << range.offset + range.size() << std::endl;
431 for (
auto const& timeValsTmp : timeValsVec) {
432 std::cout <<
"Peak #" << CountPeak
433 <<
": PeakStartTick: " << range.offset + std::get<0>(timeValsTmp)
434 <<
" PeakMaxTick: " << range.offset + std::get<1>(timeValsTmp)
435 <<
" PeakEndTick: " << range.offset + std::get<2>(timeValsTmp)
443 if (timeValsVec.empty())
continue;
457 int NumberOfPeaksBeforeFit = 0;
458 unsigned int nExponentialsForFit = 0;
459 double chi2PerNDF = 0.;
462 unsigned int NumberOfMergedVecs = mergedVec.size();
468 for (
unsigned int j = 0; j < NumberOfMergedVecs; j++) {
469 int startT = std::get<0>(mergedVec.at(j));
470 int endT = std::get<1>(mergedVec.at(j));
471 int width = endT + 1 - startT;
474 int NFluctuations = std::get<3>(mergedVec.at(j));
477 std::cout << std::endl;
478 if (peakVals.size() == 1)
479 std::cout <<
"- Group #" << j <<
" (" << peakVals.size()
480 <<
" peak): GroupStartTick: " << range.offset + startT
481 <<
" GroupEndTick: " << range.offset + endT << std::endl;
483 std::cout <<
"- Group #" << j <<
" (" << peakVals.size()
484 <<
" peaks): GroupStartTick: " << range.offset + startT
485 <<
" GroupEndTick: " << range.offset + endT << std::endl;
486 std::cout <<
"Fluctuations in this group: " << NFluctuations << std::endl;
487 int CountPeakInGroup = 0;
488 for (
auto const& peakValsTmp : peakVals) {
489 std::cout <<
"Peak #" << CountPeakInGroup <<
" in group #" << j
490 <<
": PeakInGroupStartTick: " << range.offset + std::get<2>(peakValsTmp)
491 <<
" PeakInGroupMaxTick: " << range.offset + std::get<0>(peakValsTmp)
492 <<
" PeakInGroupEndTick: " << range.offset + std::get<3>(peakValsTmp)
500 (
double)std::accumulate(signal.begin() + startT, signal.begin() + endT + 1, 0) <
502 (
double)
std::accumulate(signal.
begin() + startT, signal.
begin() + endT + 1, 0) /
506 std::cout <<
"Delete this group of peaks because width, integral or width/intergral " 516 NumberOfPeaksBeforeFit = peakVals.size();
517 nExponentialsForFit = peakVals.size();
529 std::cout << std::endl;
530 std::cout <<
"--- First fit ---" << std::endl;
531 if (nExponentialsForFit == 1)
532 std::cout <<
"- Fitted " << nExponentialsForFit <<
" peak in group #" << j <<
":" 535 std::cout <<
"- Fitted " << nExponentialsForFit <<
" peaks in group #" << j <<
":" 537 std::cout <<
"chi2/ndf = " << chi2PerNDF << std::endl;
540 std::cout <<
"tau1 [mus] = " << paramVec[0].first << std::endl;
541 std::cout <<
"tau2 [mus] = " << paramVec[1].first << std::endl;
543 for (
unsigned int i = 0; i < nExponentialsForFit; i++) {
544 std::cout <<
"Peak #" << i <<
": A [ADC] = " << paramVec[2 * (i + 1)].first
546 std::cout <<
"Peak #" << i
547 <<
": t0 [ticks] = " << range.offset + paramVec[2 * (i + 1) + 1].first
552 for (
unsigned int i = 0; i < nExponentialsForFit; i++) {
553 std::cout <<
"Peak #" << i <<
": A [ADC] = " << paramVec[4 * i + 2].first
555 std::cout <<
"Peak #" << i
556 <<
": t0 [ticks] = " << range.offset + paramVec[4 * i + 3].first
558 std::cout <<
"Peak #" << i <<
": tau1 [mus] = " << paramVec[4 * i].first
560 std::cout <<
"Peak #" << i <<
": tau2 [mus] = " << paramVec[4 * i + 1].first
567 if (!(chi2PerNDF < std::numeric_limits<double>::infinity()))
continue;
578 unsigned int nExponentialsBeforeRefit = nExponentialsForFit;
579 unsigned int nExponentialsAfterRefit = nExponentialsForFit;
580 double oldChi2PerNDF = chi2PerNDF;
585 while ((nExponentialsForFit == 1 &&
586 nExponentialsAfterRefit < 3 * nExponentialsBeforeRefit &&
587 chi2PerNDF > fChi2NDFRetry) ||
588 (nExponentialsForFit > 1 &&
589 nExponentialsAfterRefit < 3 * nExponentialsBeforeRefit &&
591 RefitSuccess =
false;
603 for (
auto& PeakDevCand : PeakDev) {
607 peakValsTemp = peakVals;
609 AddPeak(PeakDevCand, peakValsTemp);
619 if (chi2PerNDF2 < chi2PerNDF) {
620 paramVec = paramVecRefit;
621 peakVals = peakValsTemp;
622 nExponentialsForFit = peakVals.size();
623 chi2PerNDF = chi2PerNDF2;
625 nExponentialsAfterRefit++;
632 if (RefitSuccess ==
false) {
633 for (
auto& PeakDevCand : PeakDev) {
637 peakValsTemp = peakVals;
649 if (chi2PerNDF2 < chi2PerNDF) {
650 paramVec = paramVecRefit;
651 peakVals = peakValsTemp;
652 nExponentialsForFit = peakVals.size();
653 chi2PerNDF = chi2PerNDF2;
655 nExponentialsAfterRefit++;
662 if (RefitSuccess ==
false) {
break; }
666 std::cout << std::endl;
667 std::cout <<
"--- Refit ---" << std::endl;
668 if (chi2PerNDF == oldChi2PerNDF)
669 std::cout <<
"chi2/ndf didn't improve. Keep first fit." << std::endl;
671 std::cout <<
"- Added peaks to group #" << j <<
". This group now has " 672 << nExponentialsForFit <<
" peaks:" << std::endl;
673 std::cout <<
"- Group #" << j <<
" (" << peakVals.size()
674 <<
" peaks): GroupStartTick: " << range.offset + startT
675 <<
" GroupEndTick: " << range.offset + endT << std::endl;
677 int CountPeakInGroup = 0;
678 for (
auto const& peakValsTmp : peakVals) {
679 std::cout <<
"Peak #" << CountPeakInGroup <<
" in group #" << j
680 <<
": PeakInGroupStartTick: " 681 << range.offset + std::get<2>(peakValsTmp)
682 <<
" PeakInGroupMaxTick: " 683 << range.offset + std::get<0>(peakValsTmp)
684 <<
" PeakInGroupEndTick: " 685 << range.offset + std::get<3>(peakValsTmp) << std::endl;
689 std::cout <<
"chi2/ndf = " << chi2PerNDF << std::endl;
692 std::cout <<
"tau1 [mus] = " << paramVec[0].first << std::endl;
693 std::cout <<
"tau2 [mus] = " << paramVec[1].first << std::endl;
695 for (
unsigned int i = 0; i < nExponentialsForFit; i++) {
696 std::cout <<
"Peak #" << i <<
": A [ADC] = " << paramVec[2 * (i + 1)].first
698 std::cout <<
"Peak #" << i <<
": t0 [ticks] = " 699 << range.offset + paramVec[2 * (i + 1) + 1].first << std::endl;
703 for (
unsigned int i = 0; i < nExponentialsForFit; i++) {
704 std::cout <<
"Peak #" << i <<
": A [ADC] = " << paramVec[4 * i + 2].first
706 std::cout <<
"Peak #" << i
707 <<
": t0 [ticks] = " << range.offset + paramVec[4 * i + 3].first
709 std::cout <<
"Peak #" << i <<
": tau1 [mus] = " << paramVec[4 * i].first
711 std::cout <<
"Peak #" << i <<
": tau2 [mus] = " << paramVec[4 * i + 1].first
724 for (
unsigned int i = 0; i < nExponentialsForFit; i++) {
732 peakTau1 = paramVec[0].first;
733 peakTau2 = paramVec[1].first;
734 peakAmp = paramVec[2 * (i + 1)].first;
735 peakMean = paramVec[2 * (i + 1) + 1].first;
738 peakTau1 = paramVec[4 * i].first;
739 peakTau2 = paramVec[4 * i + 1].first;
740 peakAmp = paramVec[4 * i + 2].first;
741 peakMean = paramVec[4 * i + 3].first;
745 double peakAmpTrue = signal[std::get<0>(peakVals.at(i))];
746 double peakAmpErr = 1.;
749 TF1 Exponentials(
"Exponentials",
750 "( [0] * exp(0.4*(x-[1])/[2]) / ( 1 + exp(0.4*(x-[1])/[3]) ) )",
753 Exponentials.SetParameter(0, peakAmp);
754 Exponentials.SetParameter(1, peakMean);
755 Exponentials.SetParameter(2, peakTau1);
756 Exponentials.SetParameter(3, peakTau2);
757 double peakMeanTrue = Exponentials.GetMaximumX(startT, endT);
758 Exponentials.Delete();
762 WidthFunc(peakMean, peakAmp, peakTau1, peakTau2, startT, endT, peakMeanTrue);
771 peakMeanErr = paramVec[4 * i + 3].second;
773 double peakWidthErr = 0.1 * peakWidth;
779 std::sqrt(TMath::Pi()) * (peakAmpErr * peakWidthErr + peakWidthErr * peakAmpErr);
782 int startTthisHit = std::get<2>(peakVals.at(i));
783 int endTthisHit = std::get<3>(peakVals.at(i));
788 double sumADC = std::accumulate(sumStartItr, sumEndItr + 1, 0.);
791 if (peakWidth <= 0 || charge <= 0. || charge != charge ||
792 (nExponentialsForFit == 1 && chi2PerNDF >
fChi2NDFMax) ||
793 (nExponentialsForFit >= 2 &&
796 std::cout << std::endl;
797 std::cout <<
"WARNING: For peak #" << i <<
" in this group:" << std::endl;
798 if (peakWidth <= 0 || charge <= 0. || charge != charge)
799 std::cout <<
"Fit function returned width < 0 or charge < 0 or charge = nan." 801 if ((nExponentialsForFit == 1 && chi2PerNDF > fChi2NDFMax) ||
802 (nExponentialsForFit >= 2 &&
804 std::cout << std::endl;
805 std::cout <<
"WARNING: For fit of this group (" << NumberOfPeaksBeforeFit
806 <<
" peaks before refit, " << nExponentialsForFit
807 <<
" peaks after refit): " << std::endl;
808 if (nExponentialsForFit == 1 && chi2PerNDF > fChi2NDFMax)
809 std::cout <<
"chi2/ndf of this fit (" << chi2PerNDF
810 <<
") is higher than threshold (" << fChi2NDFMax <<
")." 812 if (nExponentialsForFit >= 2 &&
814 std::cout <<
"chi2/ndf of this fit (" << chi2PerNDF
815 <<
") is higher than threshold (" 818 std::cout <<
"---> DO NOT create hit object from fit parameters but use peak " 821 std::cout <<
"---> Set fit parameter so that a sharp peak with a width of 1 tick " 822 "is shown in the event display. This indicates that the fit failed." 826 (((double)endTthisHit - (
double)startTthisHit) / 4.) /
828 peakMeanErr = peakWidth / 2;
830 peakMeanTrue = std::get<0>(peakVals.at(i));
833 peakMean = peakMeanTrue;
843 startTthisHit + roiFirstBinTick,
844 endTthisHit + roiFirstBinTick,
846 peakMeanTrue + roiFirstBinTick,
860 std::cout << std::endl;
861 std::cout <<
"- Created hit object for peak #" << i
862 <<
" in this group with the following parameters (obtained from fit):" 864 std::cout <<
"HitStartTick: " << startTthisHit + roiFirstBinTick << std::endl;
865 std::cout <<
"HitEndTick: " << endTthisHit + roiFirstBinTick << std::endl;
866 std::cout <<
"HitWidthTicks: " << peakWidth << std::endl;
867 std::cout <<
"HitMeanTick: " << peakMeanTrue + roiFirstBinTick <<
" +- " 868 << peakMeanErr << std::endl;
869 std::cout <<
"HitAmplitude [ADC]: " << peakAmpTrue <<
" +- " << peakAmpErr
871 std::cout <<
"HitIntegral [ADC*ticks]: " << charge <<
" +- " << chargeErr
873 std::cout <<
"HitADCSum [ADC*ticks]: " << sumADC << std::endl;
874 std::cout <<
"HitMultiplicity: " << nExponentialsForFit << std::endl;
875 std::cout <<
"HitIndex in group: " << numHits << std::endl;
876 std::cout <<
"Hitchi2/ndf: " << chi2PerNDF << std::endl;
877 std::cout <<
"HitNDF: " << NDF << std::endl;
882 hcol.emplace_back(std::move(
hit), wire, rawdigits);
884 std::array<float, 4> fitParams;
885 fitParams[0] = peakMean + roiFirstBinTick;
886 fitParams[1] = peakTau1;
887 fitParams[2] = peakTau2;
888 fitParams[3] = peakAmp;
911 if (nHitsInThisGroup *
fLongPulseWidth < (endT - startT + 1)) nHitsInThisGroup++;
913 int firstTick = startT;
918 std::cout << std::endl;
919 std::cout <<
"WARNING: Number of peaks in this group (" << NumberOfPeaksBeforeFit
920 <<
") is higher than threshold (" <<
fMaxMultiHit <<
")." << std::endl;
922 <<
"---> DO NOT fit. Split group of peaks into hits with equal length instead." 926 std::cout << std::endl;
927 std::cout <<
"WARNING: group of peak is longer (" << width
931 <<
"---> DO NOT fit. Split group of peaks into hits with equal length instead." 935 std::cout << std::endl;
936 std::cout <<
"WARNING: fluctuations (" << NFluctuations
939 <<
"---> DO NOT fit. Split group of peaks into hits with equal length instead." 951 std::cout <<
"---> Group goes from tick " << roiFirstBinTick + startT <<
" to " 952 << roiFirstBinTick + endT <<
". Split group into (" 953 << roiFirstBinTick + endT <<
" - " << roiFirstBinTick + startT <<
")/" 956 <<
" = LongPulseWidth), or maximum LongMaxHits = " <<
fLongMaxHits 957 <<
" peaks." << std::endl;
960 for (
int hitIdx = 0; hitIdx < nHitsInThisGroup; hitIdx++) {
963 ((lastTick - firstTick) / 4.) /
965 double peakMeanTrue = (firstTick + lastTick) / 2.;
966 if (NumberOfPeaksBeforeFit == 1 && nHitsInThisGroup == 1)
967 peakMeanTrue = std::get<0>(peakVals.at(
969 double peakMeanErr = (lastTick - firstTick) / 2.;
971 std::accumulate(signal.begin() + firstTick, signal.begin() + lastTick + 1, 0.);
972 double charge = sumADC;
973 double chargeErr = 0.1 * sumADC;
974 double peakAmpTrue = 0;
977 if (signal[
tick] > peakAmpTrue) peakAmpTrue = signal[
tick];
980 double peakAmpErr = 1.;
981 nExponentialsForFit = nHitsInThisGroup;
985 double peakMean = peakMeanTrue - 2;
986 double peakTau1 = 0.008;
987 double peakTau2 = 0.0065;
988 double peakAmp = 20.;
993 firstTick + roiFirstBinTick,
994 lastTick + roiFirstBinTick,
996 peakMeanTrue + roiFirstBinTick,
1003 nExponentialsForFit,
1010 std::cout << std::endl;
1012 <<
"- Created hit object for peak #" << hitIdx
1013 <<
" in this group with the following parameters (obtained from waveform):" 1015 std::cout <<
"HitStartTick: " << firstTick + roiFirstBinTick << std::endl;
1016 std::cout <<
"HitEndTick: " << lastTick + roiFirstBinTick << std::endl;
1017 std::cout <<
"HitWidthTicks: " << peakWidth << std::endl;
1018 std::cout <<
"HitMeanTick: " << peakMeanTrue + roiFirstBinTick <<
" +- " 1019 << peakMeanErr << std::endl;
1020 std::cout <<
"HitAmplitude [ADC]: " << peakAmpTrue <<
" +- " << peakAmpErr
1022 std::cout <<
"HitIntegral [ADC*ticks]: " << charge <<
" +- " << chargeErr
1024 std::cout <<
"HitADCSum [ADC*ticks]: " << sumADC << std::endl;
1025 std::cout <<
"HitMultiplicity: " << nExponentialsForFit << std::endl;
1026 std::cout <<
"HitIndex in group: " << hitIdx << std::endl;
1027 std::cout <<
"Hitchi2/ndf: " << chi2PerNDF << std::endl;
1028 std::cout <<
"HitNDF: " << NDF << std::endl;
1031 hcol.emplace_back(std::move(
hit), wire, rawdigits);
1033 std::array<float, 4> fitParams;
1034 fitParams[0] = peakMean + roiFirstBinTick;
1035 fitParams[1] = peakTau1;
1036 fitParams[2] = peakTau2;
1037 fitParams[3] = peakAmp;
1041 firstTick = lastTick + 1;
1046 fChi2->Fill(chi2PerNDF);
void findCandidatePeaks(std::vector< float >::const_iterator startItr, std::vector< float >::const_iterator stopItr, TimeValsVec &timeValsVec, float &PeakMin, int firstTick) const
std::vector< WireID > ChannelToWire(raw::ChannelID_t const channel) const
Returns a list of wires connected to the specified TPC channel.
void mergeCandidatePeaks(const std::vector< float > signalVec, TimeValsVec, MergedTimeWidVec &)
ChannelID_t Channel() const
DAQ channel this raw data was read from.
void FindPeakWithMaxDeviation(const std::vector< float > fSignalVector, int fNPeaks, int fStartTime, int fEndTime, bool fSameShape, ParameterVec fparamVec, PeakTimeWidVec fpeakVals, PeakDevVec &fPeakDev)
CryostatID_t Cryostat
Index of cryostat.
const range_list_t & get_ranges() const
Returns the internal list of non-void ranges.
WireID_t Wire
Index of the wire within its plane.
void SplitPeak(std::tuple< double, int, int, int > fPeakDevCand, PeakTimeWidVec &fpeakValsTemp)
double fChi2NDFMaxFactorMultiHits
void addVector(FVector_ID id, std::array< float, N > const &values)
std::vector< std::tuple< int, int, int >> TimeValsVec
int TDCtick_t
Type representing a TDC tick.
Class managing the creation of a new recob::Hit object.
FVector_ID initOutputs(std::string const &dataTag, size_t dataSize, std::vector< std::string > const &names=std::vector< std::string >(N,""))
constexpr ChannelID_t InvalidChannelID
ID of an invalid channel.
void AddPeak(std::tuple< double, int, int, int > fPeakDevCand, PeakTimeWidVec &fpeakValsTemp)
double fChi2NDFRetryFactorMultiHits
A class handling a collection of hits and its associations.
double fWidthNormalization
anab::FVectorWriter< 4 > fHitParamWriter
std::vector< std::pair< double, double >> ParameterVec
std::vector< std::tuple< int, int, int, int >> PeakTimeWidVec
std::vector< std::tuple< double, int, int, int >> PeakDevVec
tick_as<> tick
Tick number, represented by std::ptrdiff_t.
double ChargeFunc(double fPeakMean, double fPeakAmp, double fPeakTau1, double fPeakTau2, double fChargeNormFactor, double fPeakMeanTrue)
double fMinADCSumOverWidth
PlaneID_t Plane
Index of the plane within its TPC.
void saveOutputs(art::Event &evt)
Check consistency and save all the results in the event.
Detector simulation of raw signals on wires.
void FitExponentials(const std::vector< float > fSignalVector, const PeakTimeWidVec fPeakVals, int fStartTime, int fEndTime, ParameterVec &fparamVec, double &fchi2PerNDF, int &fNDF, bool fSameShape)
std::string fCalDataModuleLabel
bool getByLabel(std::string const &label, std::string const &instance, Handle< PROD > &result) const
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
void doBinAverage(const std::vector< float > &inputVec, std::vector< float > &outputVec, size_t binsToAverage) const
2D representation of charge deposited in the TDC/wire plane
unsigned int ChannelID_t
Type representing the ID of a readout channel.
TPCID_t TPC
Index of the TPC within its cryostat.
std::vector< std::tuple< int, int, PeakTimeWidVec, int >> MergedTimeWidVec
second_as<> second
Type of time stored in seconds, in double precision.
art::InputTag fNewHitsTag
double WidthFunc(double fPeakMean, double fPeakAmp, double fPeakTau1, double fPeakTau2, double fStartTime, double fEndTime, double fPeakMeanTrue)