330 TH1::AddDirectory(kFALSE);
367 for(
size_t wireIter = 0; wireIter < wireVecHandle->size(); wireIter++)
377 std::vector<geo::WireID> wids = geom->
ChannelToWire(channel);
383 std::cout << std::endl;
384 std::cout << std::endl;
385 std::cout << std::endl;
386 std::cout <<
"-----------------------------------------------------------------------------------------------------------" << std::endl;
387 std::cout <<
"Channel: " << channel << std::endl;
388 std::cout <<
"Cryostat: " << wid.
Cryostat << std::endl;
389 std::cout <<
"TPC: " << wid.
TPC << std::endl;
390 std::cout <<
"Plane: " << wid.
Plane << std::endl;
391 std::cout <<
"Wire: " << wid.
Wire << std::endl;
401 for(
const auto& range : signalROI.
get_ranges())
406 const std::vector<float>& signal = range.data();
423 std::vector<float> timeAve;
435 if (timeValsVec.empty())
continue;
456 std::cout << std::endl;
457 std::cout << std::endl;
458 std::cout <<
"-------------------- ROI #" << CountROI <<
" -------------------- " << std::endl;
459 if(timeValsVec.size() == 1) std::cout <<
"ROI #" << CountROI <<
" (" << timeValsVec.size() <<
" peak): ROIStartTick: " << range.offset <<
" ROIEndTick:" << range.offset+range.size() << std::endl;
460 else std::cout <<
"ROI #" << CountROI <<
" (" << timeValsVec.size() <<
" peaks): ROIStartTick: " << range.offset <<
" ROIEndTick:" << range.offset+range.size() << std::endl;
467 for(
auto const& timeValsTmp : timeValsVec )
469 std::cout <<
"Peak #" << CountPeak <<
": PeakStartTick: " << range.offset + std::get<0>(timeValsTmp) <<
" PeakMaxTick: " << range.offset + std::get<1>(timeValsTmp) <<
" PeakEndTick: " << range.offset + std::get<2>(timeValsTmp) << std::endl;
476 if (timeValsVec.empty())
continue;
491 int NumberOfPeaksBeforeFit=0;
492 unsigned int nExponentialsForFit=0;
493 double chi2PerNDF=0.;
496 unsigned int NumberOfMergedVecs = mergedVec.size();
502 for(
unsigned int j=0; j < NumberOfMergedVecs; j++)
504 int startT = std::get<0>(mergedVec.at(j));
505 int endT = std::get<1>(mergedVec.at(j));
506 int width = endT + 1 - startT;
509 int NFluctuations = std::get<3>(mergedVec.at(j));
513 std::cout << std::endl;
514 if(peakVals.size() == 1) std::cout <<
"- Group #" << j <<
" (" << peakVals.size() <<
" peak): GroupStartTick: " << range.offset + startT <<
" GroupEndTick: " << range.offset + endT << std::endl;
515 else std::cout <<
"- Group #" << j <<
" (" << peakVals.size() <<
" peaks): GroupStartTick: " << range.offset + startT <<
" GroupEndTick: " << range.offset + endT << std::endl;
516 std::cout <<
"Fluctuations in this group: " << NFluctuations << std::endl;
517 int CountPeakInGroup=0;
518 for(
auto const& peakValsTmp : peakVals )
520 std::cout <<
"Peak #" << CountPeakInGroup <<
" in group #" << j <<
": PeakInGroupStartTick: " << range.offset + std::get<2>(peakValsTmp) <<
" PeakInGroupMaxTick: " << range.offset + std::get<0>(peakValsTmp) <<
" PeakInGroupEndTick: " << range.offset + std::get<3>(peakValsTmp) << std::endl;
530 std::cout <<
"Delete this group of peaks because width, integral or width/intergral is too small." << std::endl;
539 NumberOfPeaksBeforeFit = peakVals.size();
540 nExponentialsForFit = peakVals.size();
553 std::cout << std::endl;
554 std::cout <<
"--- First fit ---" << std::endl;
555 if (nExponentialsForFit == 1) std::cout <<
"- Fitted " << nExponentialsForFit <<
" peak in group #" << j <<
":" << std::endl;
556 else std::cout <<
"- Fitted " << nExponentialsForFit <<
" peaks in group #" << j <<
":" << std::endl;
557 std::cout <<
"chi2/ndf = " << chi2PerNDF << std::endl;
561 std::cout <<
"tau1 [mus] = " << paramVec[0].first << std::endl;
562 std::cout <<
"tau2 [mus] = " << paramVec[1].first << std::endl;
564 for(
unsigned int i = 0; i < nExponentialsForFit; i++)
566 std::cout <<
"Peak #" << i <<
": A [ADC] = " << paramVec[2*(i+1)].first << std::endl;
567 std::cout <<
"Peak #" << i <<
": t0 [ticks] = " << range.offset + paramVec[2*(i+1)+1].first << std::endl;
572 for(
unsigned int i = 0; i < nExponentialsForFit; i++)
574 std::cout <<
"Peak #" << i <<
": A [ADC] = " << paramVec[4*i+2].first << std::endl;
575 std::cout <<
"Peak #" << i <<
": t0 [ticks] = " << range.offset + paramVec[4*i+3].first << std::endl;
576 std::cout <<
"Peak #" << i <<
": tau1 [mus] = " << paramVec[4*i].first << std::endl;
577 std::cout <<
"Peak #" << i <<
": tau2 [mus] = " << paramVec[4*i+1].first << std::endl;
583 if (!(chi2PerNDF < std::numeric_limits<double>::infinity()))
continue;
594 unsigned int nExponentialsBeforeRefit=nExponentialsForFit;
595 unsigned int nExponentialsAfterRefit=nExponentialsForFit;
596 double oldChi2PerNDF = chi2PerNDF;
601 while( (nExponentialsForFit == 1 && nExponentialsAfterRefit < 3*nExponentialsBeforeRefit && chi2PerNDF > fChi2NDFRetry) ||
602 (nExponentialsForFit > 1 && nExponentialsAfterRefit < 3*nExponentialsBeforeRefit && chi2PerNDF >
fChi2NDFRetryFactorMultiHits*fChi2NDFRetry) )
604 RefitSuccess =
false;
609 for(
auto& PeakDevCand : PeakDev)
614 peakValsTemp = peakVals;
616 AddPeak(PeakDevCand, peakValsTemp);
619 if (chi2PerNDF2 < chi2PerNDF)
621 paramVec = paramVecRefit;
622 peakVals = peakValsTemp;
623 nExponentialsForFit = peakVals.size();
624 chi2PerNDF = chi2PerNDF2;
626 nExponentialsAfterRefit++;
633 if(RefitSuccess ==
false)
635 for(
auto& PeakDevCand : PeakDev)
640 peakValsTemp=peakVals;
645 if (chi2PerNDF2 < chi2PerNDF)
647 paramVec = paramVecRefit;
648 peakVals = peakValsTemp;
649 nExponentialsForFit = peakVals.size();
650 chi2PerNDF = chi2PerNDF2;
652 nExponentialsAfterRefit++;
659 if(RefitSuccess ==
false)
667 std::cout << std::endl;
668 std::cout <<
"--- Refit ---" << std::endl;
669 if( chi2PerNDF == oldChi2PerNDF) std::cout <<
"chi2/ndf didn't improve. Keep first fit." << std::endl;
672 std::cout <<
"- Added peaks to group #" << j <<
". This group now has " << nExponentialsForFit <<
" peaks:" << std::endl;
673 std::cout <<
"- Group #" << j <<
" (" << peakVals.size() <<
" peaks): GroupStartTick: " << range.offset + startT <<
" GroupEndTick: " << range.offset + endT << std::endl;
675 int CountPeakInGroup=0;
676 for(
auto const& peakValsTmp : peakVals )
678 std::cout <<
"Peak #" << CountPeakInGroup <<
" in group #" << j <<
": PeakInGroupStartTick: " << range.offset + std::get<2>(peakValsTmp) <<
" PeakInGroupMaxTick: " << range.offset + std::get<0>(peakValsTmp) <<
" PeakInGroupEndTick: " << range.offset + std::get<3>(peakValsTmp) << std::endl;
682 std::cout <<
"chi2/ndf = " << chi2PerNDF << std::endl;
686 std::cout <<
"tau1 [mus] = " << paramVec[0].first << std::endl;
687 std::cout <<
"tau2 [mus] = " << paramVec[1].first << std::endl;
689 for(
unsigned int i = 0; i < nExponentialsForFit; i++)
691 std::cout <<
"Peak #" << i <<
": A [ADC] = " << paramVec[2*(i+1)].first << std::endl;
692 std::cout <<
"Peak #" << i <<
": t0 [ticks] = " << range.offset + paramVec[2*(i+1)+1].first << std::endl;
697 for(
unsigned int i = 0; i < nExponentialsForFit; i++)
699 std::cout <<
"Peak #" << i <<
": A [ADC] = " << paramVec[4*i+2].first << std::endl;
700 std::cout <<
"Peak #" << i <<
": t0 [ticks] = " << range.offset + paramVec[4*i+3].first << std::endl;
701 std::cout <<
"Peak #" << i <<
": tau1 [mus] = " << paramVec[4*i].first << std::endl;
702 std::cout <<
"Peak #" << i <<
": tau2 [mus] = " << paramVec[4*i+1].first << std::endl;
714 for(
unsigned int i = 0; i < nExponentialsForFit; i++)
724 peakTau1 = paramVec[0].first;
725 peakTau2 = paramVec[1].first;
726 peakAmp = paramVec[2*(i+1)].first;
727 peakMean = paramVec[2*(i+1)+1].first;
731 peakTau1 = paramVec[4*i].first;
732 peakTau2 = paramVec[4*i+1].first;
733 peakAmp = paramVec[4*i+2].first;
734 peakMean = paramVec[4*i+3].first;
738 double peakAmpTrue = signal[std::get<0>(peakVals.at(i))];
739 double peakAmpErr = 1.;
742 TF1 Exponentials(
"Exponentials",
"( [0] * exp(0.4*(x-[1])/[2]) / ( 1 + exp(0.4*(x-[1])/[3]) ) )",startT,endT);
743 Exponentials.SetParameter(0, peakAmp);
744 Exponentials.SetParameter(1, peakMean);
745 Exponentials.SetParameter(2, peakTau1);
746 Exponentials.SetParameter(3, peakTau2);
747 double peakMeanTrue = Exponentials.GetMaximumX(startT,endT);
748 Exponentials.Delete();
751 double peakWidth =
WidthFunc(peakMean, peakAmp, peakTau1, peakTau2, startT, endT, peakMeanTrue);
760 peakMeanErr = paramVec[2*(i+1)+1].second;
764 peakMeanErr = paramVec[4*i+3].second;
766 double peakWidthErr = 0.1*peakWidth;
770 double chargeErr = std::sqrt(TMath::Pi()) * (peakAmpErr*peakWidthErr + peakWidthErr*peakAmpErr);
773 int startTthisHit = std::get<2>(peakVals.at(i));
774 int endTthisHit = std::get<3>(peakVals.at(i));
779 double sumADC = std::accumulate(sumStartItr, sumEndItr + 1, 0.);
787 std::cout << std::endl;
788 std::cout <<
"WARNING: For peak #" << i <<
" in this group:" << std::endl;
789 if( peakWidth <= 0 || charge <= 0. || charge != charge) std::cout <<
"Fit function returned width < 0 or charge < 0 or charge = nan." << std::endl;
790 if( ( nExponentialsForFit == 1 && chi2PerNDF > fChi2NDFMax ) || ( nExponentialsForFit >= 2 && chi2PerNDF >
fChi2NDFMaxFactorMultiHits*fChi2NDFMax ) )
792 std::cout << std::endl;
793 std::cout <<
"WARNING: For fit of this group (" << NumberOfPeaksBeforeFit <<
" peaks before refit, " << nExponentialsForFit <<
" peaks after refit): " << std::endl;
794 if ( nExponentialsForFit == 1 && chi2PerNDF > fChi2NDFMax ) std::cout <<
"chi2/ndf of this fit (" << chi2PerNDF <<
") is higher than threshold (" << fChi2NDFMax <<
")." << std::endl;
797 std::cout <<
"---> DO NOT create hit object from fit parameters but use peak values instead." << std::endl;
798 std::cout <<
"---> Set fit parameter so that a sharp peak with a width of 1 tick is shown in the event display. This indicates that the fit failed." << std::endl;
801 peakMeanErr=peakWidth/2;
803 peakMeanTrue = std::get<0>(peakVals.at(i));
806 peakMean = peakMeanTrue;
815 startTthisHit+roiFirstBinTick,
816 endTthisHit+roiFirstBinTick,
818 peakMeanTrue+roiFirstBinTick,
833 std::cout << std::endl;
834 std::cout <<
"- Created hit object for peak #" << i <<
" in this group with the following parameters (obtained from fit):" << std::endl;
835 std::cout <<
"HitStartTick: " << startTthisHit+roiFirstBinTick << std::endl;
836 std::cout <<
"HitEndTick: " << endTthisHit+roiFirstBinTick << std::endl;
837 std::cout <<
"HitWidthTicks: " << peakWidth << std::endl;
838 std::cout <<
"HitMeanTick: " << peakMeanTrue+roiFirstBinTick <<
" +- " << peakMeanErr << std::endl;
839 std::cout <<
"HitAmplitude [ADC]: " << peakAmpTrue <<
" +- " << peakAmpErr << std::endl;
840 std::cout <<
"HitIntegral [ADC*ticks]: " << charge <<
" +- " << chargeErr << std::endl;
841 std::cout <<
"HitADCSum [ADC*ticks]: " << sumADC << std::endl;
842 std::cout <<
"HitMultiplicity: " << nExponentialsForFit << std::endl;
843 std::cout <<
"HitIndex in group: " << numHits << std::endl;
844 std::cout <<
"Hitchi2/ndf: " << chi2PerNDF << std::endl;
845 std::cout <<
"HitNDF: " << NDF << std::endl;
850 hcol.emplace_back(std::move(
hit), wire, rawdigits);
852 std::array<float, 4> fitParams;
853 fitParams[0] = peakMean+roiFirstBinTick;
854 fitParams[1] = peakTau1;
855 fitParams[2] = peakTau2;
856 fitParams[3] = peakAmp;
880 if (nHitsInThisGroup *
fLongPulseWidth < (endT - startT + 1) ) nHitsInThisGroup++;
882 int firstTick = startT;
889 std::cout << std::endl;
890 std::cout <<
"WARNING: Number of peaks in this group (" << NumberOfPeaksBeforeFit <<
") is higher than threshold (" <<
fMaxMultiHit <<
")." << std::endl;
891 std::cout <<
"---> DO NOT fit. Split group of peaks into hits with equal length instead." << std::endl;
895 std::cout << std::endl;
896 std::cout <<
"WARNING: group of peak is longer (" << width <<
" ticks) than threshold (" <<
fMaxGroupLength <<
" ticks)." << std::endl;
897 std::cout <<
"---> DO NOT fit. Split group of peaks into hits with equal length instead." << std::endl;
901 std::cout << std::endl;
902 std::cout <<
"WARNING: fluctuations (" << NFluctuations <<
") higher than threshold (" <<
fMaxFluctuations <<
")." << std::endl;
903 std::cout <<
"---> DO NOT fit. Split group of peaks into hits with equal length instead." << std::endl;
914 std::cout <<
"---> Group goes from tick " << roiFirstBinTick+startT <<
" to " << roiFirstBinTick+endT <<
". Split group into (" << roiFirstBinTick+endT <<
" - " << roiFirstBinTick+startT <<
")/" <<
fLongPulseWidth <<
" = " << (endT - startT) <<
"/" <<
fLongPulseWidth <<
" = " << nHitsInThisGroup <<
" peaks (" <<
fLongPulseWidth <<
" = LongPulseWidth), or maximum LongMaxHits = " <<
fLongMaxHits <<
" peaks." << std::endl;
918 for(
int hitIdx = 0; hitIdx < nHitsInThisGroup; hitIdx++)
922 double peakMeanTrue = (firstTick + lastTick) / 2.;
923 if( NumberOfPeaksBeforeFit == 1 && nHitsInThisGroup == 1) peakMeanTrue = std::get<0>(peakVals.at(0));
924 double peakMeanErr = (lastTick - firstTick) / 2.;
925 double sumADC = std::accumulate(signal.begin() + firstTick, signal.begin() + lastTick + 1, 0.);
926 double charge = sumADC;
927 double chargeErr = 0.1*sumADC;
928 double peakAmpTrue = 0;
930 for(
int tick = firstTick; tick <= lastTick; tick++)
932 if(signal[tick] > peakAmpTrue) peakAmpTrue = signal[tick];
935 double peakAmpErr = 1.;
936 nExponentialsForFit = nHitsInThisGroup;
940 double peakMean = peakMeanTrue-2;
941 double peakTau1 = 0.008;
942 double peakTau2 = 0.0065;
943 double peakAmp = 20.;
947 firstTick+roiFirstBinTick,
948 lastTick+roiFirstBinTick,
950 peakMeanTrue+roiFirstBinTick,
966 std::cout << std::endl;
967 std::cout <<
"- Created hit object for peak #" << hitIdx <<
" in this group with the following parameters (obtained from waveform):" << std::endl;
968 std::cout <<
"HitStartTick: " << firstTick+roiFirstBinTick << std::endl;
969 std::cout <<
"HitEndTick: " << lastTick+roiFirstBinTick << std::endl;
970 std::cout <<
"HitWidthTicks: " << peakWidth << std::endl;
971 std::cout <<
"HitMeanTick: " << peakMeanTrue+roiFirstBinTick <<
" +- " << peakMeanErr << std::endl;
972 std::cout <<
"HitAmplitude [ADC]: " << peakAmpTrue <<
" +- " << peakAmpErr << std::endl;
973 std::cout <<
"HitIntegral [ADC*ticks]: " << charge <<
" +- " << chargeErr << std::endl;
974 std::cout <<
"HitADCSum [ADC*ticks]: " << sumADC << std::endl;
975 std::cout <<
"HitMultiplicity: " << nExponentialsForFit << std::endl;
976 std::cout <<
"HitIndex in group: " << hitIdx << std::endl;
977 std::cout <<
"Hitchi2/ndf: " << chi2PerNDF << std::endl;
978 std::cout <<
"HitNDF: " << NDF << std::endl;
981 hcol.emplace_back(std::move(
hit), wire, rawdigits);
983 std::array<float, 4> fitParams;
984 fitParams[0] = peakMean+roiFirstBinTick;
985 fitParams[1] = peakTau1;
986 fitParams[2] = peakTau2;
987 fitParams[3] = peakAmp;
991 firstTick = lastTick+1;
996 fChi2->Fill(chi2PerNDF);
void findCandidatePeaks(std::vector< float >::const_iterator startItr, std::vector< float >::const_iterator stopItr, TimeValsVec &timeValsVec, float &PeakMin, int firstTick) const
void mergeCandidatePeaks(const std::vector< float > signalVec, TimeValsVec, MergedTimeWidVec &)
std::vector< std::tuple< int, int, PeakTimeWidVec, int >> 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)
std::vector< std::tuple< double, int, int, int >> PeakDevVec
std::vector< geo::WireID > ChannelToWire(raw::ChannelID_t const channel) const
Returns a list of wires connected to the specified TPC channel.
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)
int TDCtick_t
Type representing a TDC tick.
std::vector< std::tuple< int, int, int, int >> PeakTimeWidVec
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.
std::vector< std::pair< double, double >> ParameterVec
double fWidthNormalization
anab::FVectorWriter< 4 > fHitParamWriter
std::vector< evd::details::RawDigitInfo_t >::const_iterator begin(RawDigitCacheDataClass const &cache)
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
std::vector< std::tuple< int, int, int >> TimeValsVec
bool getByLabel(std::string const &label, std::string const &productInstanceName, Handle< PROD > &result) const
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.
art::InputTag fNewHitsTag
double WidthFunc(double fPeakMean, double fPeakAmp, double fPeakTau1, double fPeakTau2, double fStartTime, double fEndTime, double fPeakMeanTrue)