302 TH1::AddDirectory(kFALSE);
311 art::ServiceHandle<geo::Geometry const> geom;
326 art::Handle< std::vector<recob::Wire> > wireVecHandle;
339 for(
size_t wireIter = 0; wireIter < wireVecHandle->size(); wireIter++)
344 art::Ptr<recob::Wire> wire(wireVecHandle, wireIter);
345 art::Ptr<raw::RawDigit> rawdigits = RawDigits.at(wireIter);
347 channel = wire->Channel();
349 std::vector<geo::WireID> wids = geom->ChannelToWire(channel);
358 std::cout <<
"-----------------------------------------------------------------------------------------------------------" << std::endl;
359 std::cout <<
"Channel: " << channel << std::endl;
373 for(
const auto& range : signalROI.
get_ranges())
378 const std::vector<float>& signal = range.data();
390 std::vector<float> timeAve;
402 if (timeValsVec.empty())
continue;
425 std::cout <<
"-------------------- ROI #" << CountROI <<
" -------------------- " << std::endl;
426 if(timeValsVec.size() == 1)
std::cout <<
"ROI #" << CountROI <<
" (" << timeValsVec.size() <<
" peak): ROIStartTick: " << range.offset <<
" ROIEndTick:" << range.offset+range.size() << std::endl;
427 else std::cout <<
"ROI #" << CountROI <<
" (" << timeValsVec.size() <<
" peaks): ROIStartTick: " << range.offset <<
" ROIEndTick:" << range.offset+range.size() << std::endl;
434 for(
auto const& timeValsTmp : timeValsVec )
436 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;
443 if (timeValsVec.empty())
continue;
458 int NumberOfPeaksBeforeFit=0;
459 unsigned int nExponentialsForFit=0;
460 double chi2PerNDF=0.;
463 unsigned int NumberOfMergedVecs = mergedVec.size();
469 for(
unsigned int j=0; j < NumberOfMergedVecs; j++)
471 int startT = std::get<0>(mergedVec.at(j));
472 int endT = std::get<1>(mergedVec.at(j));
473 int width = endT + 1 - startT;
476 int NFluctuations = std::get<3>(mergedVec.at(j));
481 if(peakVals.size() == 1)
std::cout <<
"- Group #" << j <<
" (" << peakVals.size() <<
" peak): GroupStartTick: " << range.offset + startT <<
" GroupEndTick: " << range.offset + endT << std::endl;
482 else std::cout <<
"- Group #" << j <<
" (" << peakVals.size() <<
" peaks): GroupStartTick: " << range.offset + startT <<
" GroupEndTick: " << range.offset + endT << std::endl;
483 std::cout <<
"Fluctuations in this group: " << NFluctuations << std::endl;
484 int CountPeakInGroup=0;
485 for(
auto const& peakValsTmp : peakVals )
487 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;
497 std::cout <<
"Delete this group of peaks because width, integral or width/intergral is too small." << std::endl;
506 NumberOfPeaksBeforeFit = peakVals.size();
507 nExponentialsForFit = peakVals.size();
521 std::cout <<
"--- First fit ---" << std::endl;
522 if (nExponentialsForFit == 1)
std::cout <<
"- Fitted " << nExponentialsForFit <<
" peak in group #" << j <<
":" << std::endl;
523 else std::cout <<
"- Fitted " << nExponentialsForFit <<
" peaks in group #" << j <<
":" << std::endl;
524 std::cout <<
"chi2/ndf = " << chi2PerNDF << std::endl;
528 std::cout <<
"tau1 [mus] = " << paramVec[0].first << std::endl;
529 std::cout <<
"tau2 [mus] = " << paramVec[1].first << std::endl;
531 for(
unsigned int i = 0; i < nExponentialsForFit; i++)
533 std::cout <<
"Peak #" << i <<
": A [ADC] = " << paramVec[2*(i+1)].
first << std::endl;
534 std::cout <<
"Peak #" << i <<
": t0 [ticks] = " << range.offset + paramVec[2*(i+1)+1].
first << std::endl;
539 for(
unsigned int i = 0; i < nExponentialsForFit; i++)
541 std::cout <<
"Peak #" << i <<
": A [ADC] = " << paramVec[4*i+2].first << std::endl;
542 std::cout <<
"Peak #" << i <<
": t0 [ticks] = " << range.offset + paramVec[4*i+3].first << std::endl;
543 std::cout <<
"Peak #" << i <<
": tau1 [mus] = " << paramVec[4*i].first << std::endl;
544 std::cout <<
"Peak #" << i <<
": tau2 [mus] = " << paramVec[4*i+1].first << std::endl;
550 if (!(chi2PerNDF < std::numeric_limits<double>::infinity()))
continue;
561 unsigned int nExponentialsBeforeRefit=nExponentialsForFit;
562 unsigned int nExponentialsAfterRefit=nExponentialsForFit;
563 double oldChi2PerNDF = chi2PerNDF;
568 while( (nExponentialsForFit == 1 && nExponentialsAfterRefit < 3*nExponentialsBeforeRefit && chi2PerNDF > fChi2NDFRetry) ||
569 (nExponentialsForFit > 1 && nExponentialsAfterRefit < 3*nExponentialsBeforeRefit && chi2PerNDF >
fChi2NDFRetryFactorMultiHits*fChi2NDFRetry) )
571 RefitSuccess =
false;
576 for(
auto& PeakDevCand : PeakDev)
581 peakValsTemp = peakVals;
583 AddPeak(PeakDevCand, peakValsTemp);
586 if (chi2PerNDF2 < chi2PerNDF)
588 paramVec = paramVecRefit;
589 peakVals = peakValsTemp;
590 nExponentialsForFit = peakVals.size();
591 chi2PerNDF = chi2PerNDF2;
593 nExponentialsAfterRefit++;
600 if(RefitSuccess ==
false)
602 for(
auto& PeakDevCand : PeakDev)
607 peakValsTemp=peakVals;
612 if (chi2PerNDF2 < chi2PerNDF)
614 paramVec = paramVecRefit;
615 peakVals = peakValsTemp;
616 nExponentialsForFit = peakVals.size();
617 chi2PerNDF = chi2PerNDF2;
619 nExponentialsAfterRefit++;
626 if(RefitSuccess ==
false)
635 std::cout <<
"--- Refit ---" << std::endl;
636 if( chi2PerNDF == oldChi2PerNDF)
std::cout <<
"chi2/ndf didn't improve. Keep first fit." << std::endl;
639 std::cout <<
"- Added peaks to group #" << j <<
". This group now has " << nExponentialsForFit <<
" peaks:" << std::endl;
640 std::cout <<
"- Group #" << j <<
" (" << peakVals.size() <<
" peaks): GroupStartTick: " << range.offset + startT <<
" GroupEndTick: " << range.offset + endT << std::endl;
642 int CountPeakInGroup=0;
643 for(
auto const& peakValsTmp : peakVals )
645 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;
649 std::cout <<
"chi2/ndf = " << chi2PerNDF << std::endl;
653 std::cout <<
"tau1 [mus] = " << paramVec[0].first << std::endl;
654 std::cout <<
"tau2 [mus] = " << paramVec[1].first << std::endl;
656 for(
unsigned int i = 0; i < nExponentialsForFit; i++)
658 std::cout <<
"Peak #" << i <<
": A [ADC] = " << paramVec[2*(i+1)].
first << std::endl;
659 std::cout <<
"Peak #" << i <<
": t0 [ticks] = " << range.offset + paramVec[2*(i+1)+1].
first << std::endl;
664 for(
unsigned int i = 0; i < nExponentialsForFit; i++)
666 std::cout <<
"Peak #" << i <<
": A [ADC] = " << paramVec[4*i+2].first << std::endl;
667 std::cout <<
"Peak #" << i <<
": t0 [ticks] = " << range.offset + paramVec[4*i+3].first << std::endl;
668 std::cout <<
"Peak #" << i <<
": tau1 [mus] = " << paramVec[4*i].first << std::endl;
669 std::cout <<
"Peak #" << i <<
": tau2 [mus] = " << paramVec[4*i+1].first << std::endl;
681 for(
unsigned int i = 0; i < nExponentialsForFit; i++)
691 peakTau1 = paramVec[0].first;
692 peakTau2 = paramVec[1].first;
693 peakAmp = paramVec[2*(i+1)].
first;
694 peakMean = paramVec[2*(i+1)+1].
first;
698 peakTau1 = paramVec[4*i].first;
699 peakTau2 = paramVec[4*i+1].first;
700 peakAmp = paramVec[4*i+2].first;
701 peakMean = paramVec[4*i+3].first;
705 double peakAmpTrue = signal[std::get<0>(peakVals.at(i))];
706 double peakAmpErr = 1.;
709 TF1 Exponentials(
"Exponentials",
"( [0] * exp(0.4*(x-[1])/[2]) / ( 1 + exp(0.4*(x-[1])/[3]) ) )",startT,endT);
710 Exponentials.SetParameter(0, peakAmp);
711 Exponentials.SetParameter(1, peakMean);
712 Exponentials.SetParameter(2, peakTau1);
713 Exponentials.SetParameter(3, peakTau2);
714 double peakMeanTrue = Exponentials.GetMaximumX(startT,endT);
715 Exponentials.Delete();
718 double peakWidth =
WidthFunc(peakMean, peakAmp, peakTau1, peakTau2, startT, endT, peakMeanTrue);
727 peakMeanErr = paramVec[2*(i+1)+1].
second;
731 peakMeanErr = paramVec[4*i+3].second;
733 double peakWidthErr = 0.1*peakWidth;
737 double chargeErr = std::sqrt(TMath::Pi()) * (peakAmpErr*peakWidthErr + peakWidthErr*peakAmpErr);
740 int startTthisHit = std::get<2>(peakVals.at(i));
741 int endTthisHit = std::get<3>(peakVals.at(i));
742 std::vector<float>::const_iterator sumStartItr = signal.begin() + startTthisHit;
743 std::vector<float>::const_iterator sumEndItr = signal.begin() + endTthisHit;
746 double sumADC = std::accumulate(sumStartItr, sumEndItr + 1, 0.);
755 std::cout <<
"WARNING: For peak #" << i <<
" in this group:" << std::endl;
756 if( peakWidth <= 0 || charge <= 0. || charge != charge)
std::cout <<
"Fit function returned width < 0 or charge < 0 or charge = nan." << std::endl;
757 if( ( nExponentialsForFit == 1 && chi2PerNDF > fChi2NDFMax ) || ( nExponentialsForFit >= 2 && chi2PerNDF >
fChi2NDFMaxFactorMultiHits*fChi2NDFMax ) )
760 std::cout <<
"WARNING: For fit of this group (" << NumberOfPeaksBeforeFit <<
" peaks before refit, " << nExponentialsForFit <<
" peaks after refit): " << std::endl;
761 if ( nExponentialsForFit == 1 && chi2PerNDF > fChi2NDFMax )
std::cout <<
"chi2/ndf of this fit (" << chi2PerNDF <<
") is higher than threshold (" << fChi2NDFMax <<
")." << std::endl;
764 std::cout <<
"---> DO NOT create hit object from fit parameters but use peak values instead." << std::endl;
765 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;
768 peakMeanErr=peakWidth/2;
770 peakMeanTrue = std::get<0>(peakVals.at(i));
773 peakMean = peakMeanTrue;
782 startTthisHit+roiFirstBinTick,
783 endTthisHit+roiFirstBinTick,
785 peakMeanTrue+roiFirstBinTick,
801 std::cout <<
"- Created hit object for peak #" << i <<
" in this group with the following parameters (obtained from fit):" << std::endl;
802 std::cout <<
"HitStartTick: " << startTthisHit+roiFirstBinTick << std::endl;
803 std::cout <<
"HitEndTick: " << endTthisHit+roiFirstBinTick << std::endl;
804 std::cout <<
"HitWidthTicks: " << peakWidth << std::endl;
805 std::cout <<
"HitMeanTick: " << peakMeanTrue+roiFirstBinTick <<
" +- " << peakMeanErr << std::endl;
806 std::cout <<
"HitAmplitude [ADC]: " << peakAmpTrue <<
" +- " << peakAmpErr << std::endl;
807 std::cout <<
"HitIntegral [ADC*ticks]: " << charge <<
" +- " << chargeErr << std::endl;
808 std::cout <<
"HitADCSum [ADC*ticks]: " << sumADC << std::endl;
809 std::cout <<
"HitMultiplicity: " << nExponentialsForFit << std::endl;
810 std::cout <<
"HitIndex in group: " << numHits << std::endl;
811 std::cout <<
"Hitchi2/ndf: " << chi2PerNDF << std::endl;
812 std::cout <<
"HitNDF: " << NDF << std::endl;
817 hcol.emplace_back(std::move(
hit), wire, rawdigits);
819 std::array<float, 4> fitParams;
820 fitParams[0] = peakMean+roiFirstBinTick;
821 fitParams[1] = peakTau1;
822 fitParams[2] = peakTau2;
823 fitParams[3] = peakAmp;
847 if (nHitsInThisGroup *
fLongPulseWidth < (endT - startT + 1) ) nHitsInThisGroup++;
849 int firstTick = startT;
857 std::cout <<
"WARNING: Number of peaks in this group (" << NumberOfPeaksBeforeFit <<
") is higher than threshold (" <<
fMaxMultiHit <<
")." << std::endl;
858 std::cout <<
"---> DO NOT fit. Split group of peaks into hits with equal length instead." << std::endl;
863 std::cout <<
"WARNING: group of peak is longer (" << width <<
" ticks) than threshold (" <<
fMaxGroupLength <<
" ticks)." << std::endl;
864 std::cout <<
"---> DO NOT fit. Split group of peaks into hits with equal length instead." << std::endl;
869 std::cout <<
"WARNING: fluctuations (" << NFluctuations <<
") higher than threshold (" <<
fMaxFluctuations <<
")." << std::endl;
870 std::cout <<
"---> DO NOT fit. Split group of peaks into hits with equal length instead." << std::endl;
881 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;
885 for(
int hitIdx = 0; hitIdx < nHitsInThisGroup; hitIdx++)
889 double peakMeanTrue = (firstTick + lastTick) / 2.;
890 if( NumberOfPeaksBeforeFit == 1 && nHitsInThisGroup == 1) peakMeanTrue = std::get<0>(peakVals.at(0));
891 double peakMeanErr = (lastTick - firstTick) / 2.;
892 double sumADC = std::accumulate(signal.begin() + firstTick, signal.begin() + lastTick + 1, 0.);
893 double charge = sumADC;
894 double chargeErr = 0.1*sumADC;
895 double peakAmpTrue = 0;
899 if(signal[
tick] > peakAmpTrue) peakAmpTrue = signal[
tick];
902 double peakAmpErr = 1.;
903 nExponentialsForFit = nHitsInThisGroup;
907 double peakMean = peakMeanTrue-2;
908 double peakTau1 = 0.008;
909 double peakTau2 = 0.0065;
910 double peakAmp = 20.;
914 firstTick+roiFirstBinTick,
915 lastTick+roiFirstBinTick,
917 peakMeanTrue+roiFirstBinTick,
934 std::cout <<
"- Created hit object for peak #" << hitIdx <<
" in this group with the following parameters (obtained from waveform):" << std::endl;
935 std::cout <<
"HitStartTick: " << firstTick+roiFirstBinTick << std::endl;
936 std::cout <<
"HitEndTick: " << lastTick+roiFirstBinTick << std::endl;
937 std::cout <<
"HitWidthTicks: " << peakWidth << std::endl;
938 std::cout <<
"HitMeanTick: " << peakMeanTrue+roiFirstBinTick <<
" +- " << peakMeanErr << std::endl;
939 std::cout <<
"HitAmplitude [ADC]: " << peakAmpTrue <<
" +- " << peakAmpErr << std::endl;
940 std::cout <<
"HitIntegral [ADC*ticks]: " << charge <<
" +- " << chargeErr << std::endl;
941 std::cout <<
"HitADCSum [ADC*ticks]: " << sumADC << std::endl;
942 std::cout <<
"HitMultiplicity: " << nExponentialsForFit << std::endl;
943 std::cout <<
"HitIndex in group: " << hitIdx << std::endl;
944 std::cout <<
"Hitchi2/ndf: " << chi2PerNDF << std::endl;
945 std::cout <<
"HitNDF: " << NDF << std::endl;
948 hcol.emplace_back(std::move(
hit), wire, rawdigits);
950 std::array<float, 4> fitParams;
951 fitParams[0] = peakMean+roiFirstBinTick;
952 fitParams[1] = peakTau1;
953 fitParams[2] = peakTau2;
954 fitParams[3] = peakAmp;
958 firstTick = lastTick+1;
963 fChi2->Fill(chi2PerNDF);
double std(const std::vector< short > &wf, const double ped_mean, size_t start, size_t nsample)
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
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
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
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.
auto begin(FixedBins< T, C > const &) noexcept
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
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)
BEGIN_PROLOG could also be cout