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 = " << std::setprecision(2) << std::fixed << chi2PerNDF << std::endl;
561 std::cout <<
"tau1 [mus] = " << std::setprecision(3) << std::fixed << paramVec[0].first << std::endl;
562 std::cout <<
"tau2 [mus] = " << std::setprecision(3) << std::fixed << paramVec[1].first << std::endl;
564 for(
unsigned int i = 0; i < nExponentialsForFit; i++)
566 std::cout <<
"Peak #" << i <<
": A [ADC] = " << std::setprecision(1) << std::fixed << paramVec[2*(i+1)].first << std::endl;
567 std::cout <<
"Peak #" << i <<
": t0 [ticks] = " << std::setprecision(1) << std::fixed << 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] = " << std::setprecision(1) << std::fixed << paramVec[4*i+2].first << std::endl;
575 std::cout <<
"Peak #" << i <<
": t0 [ticks] = " << std::setprecision(1) << std::fixed << range.offset + paramVec[4*i+3].first << std::endl;
576 std::cout <<
"Peak #" << i <<
": tau1 [mus] = " << std::setprecision(3) << std::fixed << paramVec[4*i].first << std::endl;
577 std::cout <<
"Peak #" << i <<
": tau2 [mus] = " << std::setprecision(3) << std::fixed << 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 = " << std::setprecision(2) << std::fixed << chi2PerNDF << std::endl;
686 std::cout <<
"tau1 [mus] = " << std::setprecision(3) << std::fixed << paramVec[0].first << std::endl;
687 std::cout <<
"tau2 [mus] = " << std::setprecision(3) << std::fixed << paramVec[1].first << std::endl;
689 for(
unsigned int i = 0; i < nExponentialsForFit; i++)
691 std::cout <<
"Peak #" << i <<
": A [ADC] = " << std::setprecision(1) << std::fixed << paramVec[2*(i+1)].first << std::endl;
692 std::cout <<
"Peak #" << i <<
": t0 [ticks] = " << std::setprecision(1) << std::fixed << 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] = " << std::setprecision(1) << std::fixed << paramVec[4*i+2].first << std::endl;
700 std::cout <<
"Peak #" << i <<
": t0 [ticks] = " << std::setprecision(1) << std::fixed << range.offset + paramVec[4*i+3].first << std::endl;
701 std::cout <<
"Peak #" << i <<
": tau1 [mus] = " << std::setprecision(3) << std::fixed << paramVec[4*i].first << std::endl;
702 std::cout <<
"Peak #" << i <<
": tau2 [mus] = " << std::setprecision(3) << std::fixed << 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);
759 peakMeanErr = paramVec[2*(i+1)+1].second;
763 peakMeanErr = paramVec[4*i+3].second;
765 double peakWidthErr = 0.1*peakWidth;
769 double chargeErr = std::sqrt(TMath::Pi()) * (peakAmpErr*peakWidthErr + peakWidthErr*peakAmpErr);
772 int startTthisHit = std::get<2>(peakVals.at(i));
773 int endTthisHit = std::get<3>(peakVals.at(i));
778 double sumADC = std::accumulate(sumStartItr, sumEndItr + 1, 0.);
786 std::cout << std::endl;
787 std::cout <<
"WARNING: For peak #" << i <<
" in this group:" << std::endl;
788 if( peakWidth <= 0 || charge <= 0. || charge != charge) std::cout <<
"Fit function returned width < 0 or charge < 0 or charge = nan." << std::endl;
789 if( ( nExponentialsForFit == 1 && chi2PerNDF > fChi2NDFMax ) || ( nExponentialsForFit >= 2 && chi2PerNDF >
fChi2NDFMaxFactorMultiHits*fChi2NDFMax ) )
791 std::cout << std::endl;
792 std::cout <<
"WARNING: For fit of this group (" << NumberOfPeaksBeforeFit <<
" peaks before refit, " << nExponentialsForFit <<
" peaks after refit): " << std::endl;
793 if ( nExponentialsForFit == 1 && chi2PerNDF > fChi2NDFMax ) std::cout <<
"chi2/ndf of this fit (" << chi2PerNDF <<
") is higher than threshold (" << fChi2NDFMax <<
")." << std::endl;
796 std::cout <<
"---> DO NOT create hit object from fit parameters but use peak values instead." << std::endl;
797 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;
800 peakMeanErr=peakWidth/2;
802 peakMeanTrue = std::get<0>(peakVals.at(i));
805 peakMean = peakMeanTrue;
814 startT+roiFirstBinTick,
815 endT+roiFirstBinTick,
817 peakMeanTrue+roiFirstBinTick,
832 std::cout << std::endl;
833 std::cout <<
"- Created hit object for peak #" << i <<
" in this group with the following parameters (obtained from fit):" << std::endl;
834 std::cout <<
"HitStartTick: " << startT+roiFirstBinTick << std::endl;
835 std::cout <<
"HitEndTick: " << endT+roiFirstBinTick << std::endl;
836 std::cout <<
"HitWidthTicks: " << std::setprecision(2) << std::fixed << peakWidth << std::endl;
837 std::cout <<
"HitMeanTick: " << std::setprecision(2) << std::fixed << peakMeanTrue+roiFirstBinTick <<
" +- " << peakMeanErr << std::endl;
838 std::cout <<
"HitAmplitude [ADC]: " << std::setprecision(1) << std::fixed << peakAmpTrue <<
" +- " << peakAmpErr << std::endl;
839 std::cout <<
"HitIntegral [ADC*ticks]: " << std::setprecision(1) << std::fixed << charge <<
" +- " << chargeErr << std::endl;
840 std::cout <<
"HitADCSum [ADC*ticks]: " << std::setprecision(1) << std::fixed << sumADC << std::endl;
841 std::cout <<
"HitMultiplicity: " << nExponentialsForFit << std::endl;
842 std::cout <<
"HitIndex in group: " << numHits << std::endl;
843 std::cout <<
"Hitchi2/ndf: " << std::setprecision(2) << std::fixed << chi2PerNDF << std::endl;
844 std::cout <<
"HitNDF: " << NDF << std::endl;
849 hcol.emplace_back(std::move(
hit), wire, rawdigits);
851 std::array<float, 4> fitParams;
852 fitParams[0] = peakMean+roiFirstBinTick;
853 fitParams[1] = peakTau1;
854 fitParams[2] = peakTau2;
855 fitParams[3] = peakAmp;
879 if (nHitsInThisGroup *
fLongPulseWidth < (endT - startT + 1) ) nHitsInThisGroup++;
881 int firstTick = startT;
888 std::cout << std::endl;
889 std::cout <<
"WARNING: Number of peaks in this group (" << NumberOfPeaksBeforeFit <<
") is higher than threshold (" <<
fMaxMultiHit <<
")." << std::endl;
890 std::cout <<
"---> DO NOT fit. Split group of peaks into hits with equal length instead." << std::endl;
894 std::cout << std::endl;
895 std::cout <<
"WARNING: group of peak is longer (" << width <<
" ticks) than threshold (" <<
fMaxGroupLength <<
" ticks)." << std::endl;
896 std::cout <<
"---> DO NOT fit. Split group of peaks into hits with equal length instead." << std::endl;
900 std::cout << std::endl;
901 std::cout <<
"WARNING: fluctuations (" << NFluctuations <<
") higher than threshold (" <<
fMaxFluctuations <<
")." << std::endl;
902 std::cout <<
"---> DO NOT fit. Split group of peaks into hits with equal length instead." << std::endl;
913 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;
917 for(
int hitIdx = 0; hitIdx < nHitsInThisGroup; hitIdx++)
921 double peakMeanTrue = (firstTick + lastTick) / 2.;
922 if( NumberOfPeaksBeforeFit == 1 && nHitsInThisGroup == 1) peakMeanTrue = std::get<0>(peakVals.at(0));
923 double peakMeanErr = (lastTick - firstTick) / 2.;
924 double sumADC = std::accumulate(signal.begin() + firstTick, signal.begin() + lastTick + 1, 0.);
925 double charge = sumADC;
926 double chargeErr = 0.1*sumADC;
927 double peakAmpTrue = 0;
929 for(
int tick = firstTick; tick <= lastTick; tick++)
931 if(signal[tick] > peakAmpTrue) peakAmpTrue = signal[tick];
934 double peakAmpErr = 1.;
935 nExponentialsForFit = nHitsInThisGroup;
939 double peakMean = peakMeanTrue-2;
940 double peakTau1 = 0.008;
941 double peakTau2 = 0.0065;
942 double peakAmp = 20.;
946 firstTick+roiFirstBinTick,
947 lastTick+roiFirstBinTick,
949 peakMeanTrue+roiFirstBinTick,
965 std::cout << std::endl;
966 std::cout <<
"- Created hit object for peak #" << hitIdx <<
" in this group with the following parameters (obtained from waveform):" << std::endl;
967 std::cout <<
"HitStartTick: " << firstTick+roiFirstBinTick << std::endl;
968 std::cout <<
"HitEndTick: " << lastTick+roiFirstBinTick << std::endl;
969 std::cout <<
"HitWidthTicks: " << std::setprecision(2) << std::fixed << peakWidth << std::endl;
970 std::cout <<
"HitMeanTick: " << std::setprecision(2) << std::fixed << peakMeanTrue+roiFirstBinTick <<
" +- " << peakMeanErr << std::endl;
971 std::cout <<
"HitAmplitude [ADC]: " << std::setprecision(1) << std::fixed << peakAmpTrue <<
" +- " << peakAmpErr << std::endl;
972 std::cout <<
"HitIntegral [ADC*ticks]: " << std::setprecision(1) << std::fixed << charge <<
" +- " << chargeErr << std::endl;
973 std::cout <<
"HitADCSum [ADC*ticks]: " << std::setprecision(1) << std::fixed << sumADC << std::endl;
974 std::cout <<
"HitMultiplicity: " << nExponentialsForFit << std::endl;
975 std::cout <<
"HitIndex in group: " << hitIdx << std::endl;
976 std::cout <<
"Hitchi2/ndf: " << std::setprecision(2) << std::fixed << chi2PerNDF << std::endl;
977 std::cout <<
"HitNDF: " << NDF << std::endl;
980 hcol.emplace_back(std::move(
hit), wire, rawdigits);
982 std::array<float, 4> fitParams;
983 fitParams[0] = peakMean+roiFirstBinTick;
984 fitParams[1] = peakTau1;
985 fitParams[2] = peakTau2;
986 fitParams[3] = peakAmp;
990 firstTick = lastTick+1;
995 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)