From b945a0e86cdd38869f3475857fafec3fefc9ac38 Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Fri, 31 May 2024 10:29:48 +0200 Subject: [PATCH 01/10] TVector2 return --- inc/TRestDetectorSignal.h | 6 +----- src/TRestDetectorSignal.cxx | 14 ++++---------- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/inc/TRestDetectorSignal.h b/inc/TRestDetectorSignal.h index 2c1fbe67..90b4be2c 100644 --- a/inc/TRestDetectorSignal.h +++ b/inc/TRestDetectorSignal.h @@ -66,11 +66,7 @@ class TRestDetectorSignal { void SetSignalType(const std::string& type) { fType = type; } // Getters - TVector2 GetPoint(Int_t n) { - TVector2 vector2(GetTime(n), GetData(n)); - - return vector2; - } + TVector2 GetPoint(Int_t n) { return {GetTime(n), GetData(n)}; } inline Int_t GetSignalID() const { return fSignalID; } inline Int_t GetID() const { return fSignalID; } diff --git a/src/TRestDetectorSignal.cxx b/src/TRestDetectorSignal.cxx index 8ba623b6..b3e84aa5 100644 --- a/src/TRestDetectorSignal.cxx +++ b/src/TRestDetectorSignal.cxx @@ -305,7 +305,7 @@ TRestDetectorSignal::GetMaxGauss() // returns a 2vector with the time of the pe } } - // Find the upper limit: time where signal drops to 90% of the max after the peak + // Find the upper limit: time when signal drops to 90% of the max after the peak for (int i = maxRaw; i < GetNumberOfPoints(); ++i) { if (GetData(i) <= threshold) { upperLimit = GetTime(i); @@ -358,9 +358,7 @@ TRestDetectorSignal::GetMaxGauss() // returns a 2vector with the time of the pe */ } - TVector2 fitParam(time, energy); - - return fitParam; + return {time, energy}; } // z position by landau fit @@ -410,12 +408,10 @@ TRestDetectorSignal::GetMaxLandau() // returns a 2vector with the time of the p */ } - TVector2 fitParam(time, energy); - delete h1; delete landau; - return fitParam; + return {time, energy}; } // z position by aget fit @@ -473,12 +469,10 @@ TRestDetectorSignal::GetMaxAget() // returns a 2vector with the time of the pea << "Assigned fit parameters : energy = " << energy << ", time = " << time << endl; } - TVector2 fitParam(time, energy); - delete h1; delete aget; - return fitParam; + return {time, energy}; } Double_t TRestDetectorSignal::GetMaxPeakTime(Int_t from, Int_t to) { return GetTime(GetMaxIndex(from, to)); } From 23cba05a3cd97fa78da6c089e60fa31dbeff3e38 Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Fri, 31 May 2024 10:37:15 +0200 Subject: [PATCH 02/10] use optional --- inc/TRestDetectorSignal.h | 3 +- src/TRestDetectorSignal.cxx | 31 ++++++------- src/TRestDetectorSignalToHitsProcess.cxx | 55 +++++++++++++----------- 3 files changed, 45 insertions(+), 44 deletions(-) diff --git a/inc/TRestDetectorSignal.h b/inc/TRestDetectorSignal.h index 90b4be2c..59c30261 100644 --- a/inc/TRestDetectorSignal.h +++ b/inc/TRestDetectorSignal.h @@ -28,6 +28,7 @@ #include #include +#include class TRestDetectorSignal { private: @@ -55,7 +56,7 @@ class TRestDetectorSignal { // TODO other objects should probably skip using GetMaxIndex directly Int_t GetMaxIndex(Int_t from = 0, Int_t to = 0); - TVector2 GetMaxGauss(); + std::optional GetMaxGauss(); TVector2 GetMaxLandau(); TVector2 GetMaxAget(); diff --git a/src/TRestDetectorSignal.cxx b/src/TRestDetectorSignal.cxx index b3e84aa5..5173c84a 100644 --- a/src/TRestDetectorSignal.cxx +++ b/src/TRestDetectorSignal.cxx @@ -284,20 +284,19 @@ Int_t TRestDetectorSignal::GetMaxIndex(Int_t from, Int_t to) { // z position by gaussian fit -TVector2 +optional TRestDetectorSignal::GetMaxGauss() // returns a 2vector with the time of the peak time in us and the energy { Int_t maxRaw = GetMaxIndex(); // The bin where the maximum of the raw signal is found Double_t maxRawTime = GetTime(maxRaw); // The time of the bin where the maximum of the raw signal is found - Double_t energy = 0, time = 0; // Define fit limits Double_t threshold = GetData(maxRaw) * 0.9; // 90% of the maximum value Double_t lowerLimit = maxRawTime, upperLimit = maxRawTime; - // Find the lower limit: time where signal drops to 90% of the max before the peak + // Find the lower limit: time when signal drops to 90% of the max before the peak for (int i = maxRaw; i >= 0; --i) { if (GetData(i) <= threshold) { lowerLimit = GetTime(i); @@ -336,19 +335,17 @@ TRestDetectorSignal::GetMaxGauss() // returns a 2vector with the time of the pe // = save and return the fit result if (fitResult->IsValid()) { - energy = gaus.GetParameter(0); - time = gaus.GetParameter(1); + double energy = gaus.GetParameter(0); + double time = gaus.GetParameter(1); + + return TVector2(time, energy); } else { - // the fit failed, return -1 to indicate failure - energy = -1; - time = -1; + // The fit failed cout << endl << "WARNING: bad fit to signal with ID " << GetID() << " with maximum at time = " << maxRawTime - << " ns " - << "\n" + << " ns " << endl << "Failed fit parameters = " << gaus.GetParameter(0) << " || " << gaus.GetParameter(1) << " || " - << gaus.GetParameter(2) << "\n" - << "Assigned fit parameters : energy = " << energy << ", time = " << time << endl; + << gaus.GetParameter(2) << endl; /* TCanvas* c2 = new TCanvas("c2", "Signal fit", 200, 10, 1280, 720); h1->Draw(); @@ -356,9 +353,9 @@ TRestDetectorSignal::GetMaxGauss() // returns a 2vector with the time of the pe getchar(); delete c2; */ - } - return {time, energy}; + return nullopt; + } } // z position by landau fit @@ -394,8 +391,7 @@ TRestDetectorSignal::GetMaxLandau() // returns a 2vector with the time of the p time = -1; cout << endl << "WARNING: bad fit to signal with ID " << GetID() << " with maximum at time = " << maxRawTime - << " ns " - << "\n" + << " ns " << "\n" << "Failed fit parameters = " << landau->GetParameter(0) << " || " << landau->GetParameter(1) << " || " << landau->GetParameter(2) << "\n" << "Assigned fit parameters : energy = " << energy << ", time = " << time << endl; @@ -462,8 +458,7 @@ TRestDetectorSignal::GetMaxAget() // returns a 2vector with the time of the pea time = -1; cout << endl << "WARNING: bad fit to signal with ID " << GetID() << " with maximum at time = " << maxRawTime - << " ns " - << "\n" + << " ns " << "\n" << "Failed fit parameters = " << aget->GetParameter(0) << " || " << aget->GetParameter(1) << " || " << aget->GetParameter(2) << "\n" << "Assigned fit parameters : energy = " << energy << ", time = " << time << endl; diff --git a/src/TRestDetectorSignalToHitsProcess.cxx b/src/TRestDetectorSignalToHitsProcess.cxx index 43f132b4..15e13b54 100644 --- a/src/TRestDetectorSignalToHitsProcess.cxx +++ b/src/TRestDetectorSignalToHitsProcess.cxx @@ -379,33 +379,38 @@ TRestEvent* TRestDetectorSignalToHitsProcess::ProcessEvent(TRestEvent* inputEven } } else if (fMethod == "gaussFit") { - TVector2 gaussFit = signal->GetMaxGauss(); - - Double_t hitTime = 0; - Double_t z = -1.0; - if (gaussFit.X() >= 0.0) { - hitTime = gaussFit.X(); - Double_t distanceToPlane = hitTime * fDriftVelocity; - z = zPosition + fieldZDirection * distanceToPlane; - } - Double_t energy = -1.0; - if (gaussFit.Y() >= 0.0) { - energy = gaussFit.Y(); - } + const auto peak = signal->GetMaxGauss(); + if (peak) { + // unpack peak + const TVector2 gaussFit = peak.value(); + Double_t hitTime = 0; + Double_t z = -1.0; + if (gaussFit.X() >= 0.0) { + hitTime = gaussFit.X(); + Double_t distanceToPlane = hitTime * fDriftVelocity; + z = zPosition + fieldZDirection * distanceToPlane; + } + Double_t energy = -1.0; + if (gaussFit.Y() >= 0.0) { + energy = gaussFit.Y(); + } - if (GetVerboseLevel() >= TRestStringOutput::REST_Verbose_Level::REST_Debug) { - cout << "Signal event : " << signal->GetSignalID() - << "--------------------------------------------------------" << endl; - cout << "GausFit : time bin " << gaussFit.X() << " and energy : " << gaussFit.Y() << endl; - cout << "Signal to hit info : zPosition : " << zPosition - << "; fieldZDirection : " << fieldZDirection << " and driftV : " << fDriftVelocity - << endl; - cout << "Adding hit. Time : " << hitTime << " x : " << x << " y : " << y << " z : " << z - << " Energy : " << energy << endl; + if (GetVerboseLevel() >= TRestStringOutput::REST_Verbose_Level::REST_Debug) { + cout << "Signal event : " << signal->GetSignalID() + << "--------------------------------------------------------" << endl; + cout << "GausFit : time bin " << gaussFit.X() << " and energy : " << gaussFit.Y() << endl; + cout << "Signal to hit info : zPosition : " << zPosition + << "; fieldZDirection : " << fieldZDirection << " and driftV : " << fDriftVelocity + << endl; + cout << "Adding hit. Time : " << hitTime << " x : " << x << " y : " << y << " z : " << z + << " Energy : " << energy << endl; + } + } else { + // Perhaps we should just skip it + double energy = -1; + double z = -1; + fHitsEvent->AddHit(x, y, z, energy, 0, type); } - - fHitsEvent->AddHit(x, y, z, energy, 0, type); - } else if (fMethod == "landauFit") { TVector2 landauFit = signal->GetMaxLandau(); From 47154fa771787a9aea469f21a60e257c6fb43bf1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 31 May 2024 08:38:22 +0000 Subject: [PATCH 03/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/TRestDetectorSignal.cxx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/TRestDetectorSignal.cxx b/src/TRestDetectorSignal.cxx index 5173c84a..300b802a 100644 --- a/src/TRestDetectorSignal.cxx +++ b/src/TRestDetectorSignal.cxx @@ -391,7 +391,8 @@ TRestDetectorSignal::GetMaxLandau() // returns a 2vector with the time of the p time = -1; cout << endl << "WARNING: bad fit to signal with ID " << GetID() << " with maximum at time = " << maxRawTime - << " ns " << "\n" + << " ns " + << "\n" << "Failed fit parameters = " << landau->GetParameter(0) << " || " << landau->GetParameter(1) << " || " << landau->GetParameter(2) << "\n" << "Assigned fit parameters : energy = " << energy << ", time = " << time << endl; @@ -458,7 +459,8 @@ TRestDetectorSignal::GetMaxAget() // returns a 2vector with the time of the pea time = -1; cout << endl << "WARNING: bad fit to signal with ID " << GetID() << " with maximum at time = " << maxRawTime - << " ns " << "\n" + << " ns " + << "\n" << "Failed fit parameters = " << aget->GetParameter(0) << " || " << aget->GetParameter(1) << " || " << aget->GetParameter(2) << "\n" << "Assigned fit parameters : energy = " << energy << ", time = " << time << endl; From e63a79a74022bfe7b5c15e76827e26e1cfc91657 Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Fri, 31 May 2024 11:19:13 +0200 Subject: [PATCH 04/10] update fit --- inc/TRestDetectorSignal.h | 2 +- src/TRestDetectorSignal.cxx | 34 +++++++++++------------- src/TRestDetectorSignalToHitsProcess.cxx | 21 +++++---------- 3 files changed, 23 insertions(+), 34 deletions(-) diff --git a/inc/TRestDetectorSignal.h b/inc/TRestDetectorSignal.h index 59c30261..4b2c7d18 100644 --- a/inc/TRestDetectorSignal.h +++ b/inc/TRestDetectorSignal.h @@ -56,7 +56,7 @@ class TRestDetectorSignal { // TODO other objects should probably skip using GetMaxIndex directly Int_t GetMaxIndex(Int_t from = 0, Int_t to = 0); - std::optional GetMaxGauss(); + std::optional> GetMaxGauss(); TVector2 GetMaxLandau(); TVector2 GetMaxAget(); diff --git a/src/TRestDetectorSignal.cxx b/src/TRestDetectorSignal.cxx index 5173c84a..1ef824af 100644 --- a/src/TRestDetectorSignal.cxx +++ b/src/TRestDetectorSignal.cxx @@ -32,6 +32,7 @@ #include #include +#include #include using namespace std; @@ -284,38 +285,35 @@ Int_t TRestDetectorSignal::GetMaxIndex(Int_t from, Int_t to) { // z position by gaussian fit -optional +optional> TRestDetectorSignal::GetMaxGauss() // returns a 2vector with the time of the peak time in us and the energy { - Int_t maxRaw = GetMaxIndex(); // The bin where the maximum of the raw signal is found - Double_t maxRawTime = - GetTime(maxRaw); // The time of the bin where the maximum of the raw signal is found + const auto indexMax = + std::distance(fSignalCharge.begin(), std::max_element(fSignalCharge.begin(), fSignalCharge.end())); + const auto timeMax = fSignalTime[indexMax]; + const auto signalMax = fSignalCharge[indexMax]; // Define fit limits - Double_t threshold = GetData(maxRaw) * 0.9; // 90% of the maximum value + Double_t threshold = signalMax * 0.9; // 90% of the maximum value - Double_t lowerLimit = maxRawTime, upperLimit = maxRawTime; + Double_t lowerLimit = timeMax, upperLimit = timeMax; // Find the lower limit: time when signal drops to 90% of the max before the peak - for (int i = maxRaw; i >= 0; --i) { - if (GetData(i) <= threshold) { - lowerLimit = GetTime(i); + for (auto i = indexMax; i >= 0; --i) { + if (fSignalCharge[i] <= threshold) { + lowerLimit = fSignalTime[i]; break; } } // Find the upper limit: time when signal drops to 90% of the max after the peak - for (int i = maxRaw; i < GetNumberOfPoints(); ++i) { - if (GetData(i) <= threshold) { - upperLimit = GetTime(i); + for (auto i = indexMax; i < GetNumberOfPoints(); ++i) { + if (fSignalCharge[i] <= threshold) { + lowerLimit = fSignalTime[i]; break; } } - std::cout << "The max is " << maxRaw << " " << GetData(maxRaw) << " " << maxRawTime << std::endl; - std::cout << "The threshold is " << threshold << std::endl; - std::cout << "The range is " << lowerLimit << " " << upperLimit << std::endl; - TF1 gaus("gaus", "gaus", lowerLimit, upperLimit); TH1F h1("h1", "h1", GetNumberOfPoints(), GetTime(0), GetTime(GetNumberOfPoints() - 1)); @@ -338,11 +336,11 @@ TRestDetectorSignal::GetMaxGauss() // returns a 2vector with the time of the pe double energy = gaus.GetParameter(0); double time = gaus.GetParameter(1); - return TVector2(time, energy); + return make_pair(time, energy); } else { // The fit failed cout << endl - << "WARNING: bad fit to signal with ID " << GetID() << " with maximum at time = " << maxRawTime + << "WARNING: bad fit to signal with ID " << GetID() << " with maximum at time = " << timeMax << " ns " << endl << "Failed fit parameters = " << gaus.GetParameter(0) << " || " << gaus.GetParameter(1) << " || " << gaus.GetParameter(2) << endl; diff --git a/src/TRestDetectorSignalToHitsProcess.cxx b/src/TRestDetectorSignalToHitsProcess.cxx index 15e13b54..bf30fabe 100644 --- a/src/TRestDetectorSignalToHitsProcess.cxx +++ b/src/TRestDetectorSignalToHitsProcess.cxx @@ -381,28 +381,19 @@ TRestEvent* TRestDetectorSignalToHitsProcess::ProcessEvent(TRestEvent* inputEven } else if (fMethod == "gaussFit") { const auto peak = signal->GetMaxGauss(); if (peak) { - // unpack peak - const TVector2 gaussFit = peak.value(); - Double_t hitTime = 0; - Double_t z = -1.0; - if (gaussFit.X() >= 0.0) { - hitTime = gaussFit.X(); - Double_t distanceToPlane = hitTime * fDriftVelocity; - z = zPosition + fieldZDirection * distanceToPlane; - } - Double_t energy = -1.0; - if (gaussFit.Y() >= 0.0) { - energy = gaussFit.Y(); - } + const auto [time, energy] = peak.value(); + + const auto distanceToPlane = time * fDriftVelocity; + const auto z = zPosition + fieldZDirection * distanceToPlane; if (GetVerboseLevel() >= TRestStringOutput::REST_Verbose_Level::REST_Debug) { cout << "Signal event : " << signal->GetSignalID() << "--------------------------------------------------------" << endl; - cout << "GausFit : time bin " << gaussFit.X() << " and energy : " << gaussFit.Y() << endl; + cout << "GaussFit : time " << time << " ns and energy : " << energy << endl; cout << "Signal to hit info : zPosition : " << zPosition << "; fieldZDirection : " << fieldZDirection << " and driftV : " << fDriftVelocity << endl; - cout << "Adding hit. Time : " << hitTime << " x : " << x << " y : " << y << " z : " << z + cout << "Adding hit. Time : " << time << " ns x : " << x << " y : " << y << " z : " << z << " Energy : " << energy << endl; } } else { From f5b9db2dfbdddf20920bcc5b813c1c511673b901 Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Fri, 31 May 2024 11:30:27 +0200 Subject: [PATCH 05/10] simplify peak --- inc/TRestDetectorSignal.h | 6 +- src/TRestDetectorSignal.cxx | 95 +++++++++++------------- src/TRestDetectorSignalToHitsProcess.cxx | 83 +++++---------------- 3 files changed, 64 insertions(+), 120 deletions(-) diff --git a/inc/TRestDetectorSignal.h b/inc/TRestDetectorSignal.h index 4b2c7d18..2aadd0ce 100644 --- a/inc/TRestDetectorSignal.h +++ b/inc/TRestDetectorSignal.h @@ -56,9 +56,9 @@ class TRestDetectorSignal { // TODO other objects should probably skip using GetMaxIndex directly Int_t GetMaxIndex(Int_t from = 0, Int_t to = 0); - std::optional> GetMaxGauss(); - TVector2 GetMaxLandau(); - TVector2 GetMaxAget(); + std::optional> GetPeakGauss(); + std::optional> GetPeakLandau(); + std::optional> GetPeakAget(); std::string GetSignalName() const { return fName; } std::string GetSignalType() const { return fType; } diff --git a/src/TRestDetectorSignal.cxx b/src/TRestDetectorSignal.cxx index 1ef824af..d3bbc8e0 100644 --- a/src/TRestDetectorSignal.cxx +++ b/src/TRestDetectorSignal.cxx @@ -268,10 +268,16 @@ Int_t TRestDetectorSignal::GetMaxIndex(Int_t from, Int_t to) { Double_t max = std::numeric_limits::min(); Int_t index = 0; - if (from < 0) from = 0; - if (to > GetNumberOfPoints()) to = GetNumberOfPoints(); + if (from < 0) { + from = 0; + } + if (to > GetNumberOfPoints()) { + to = GetNumberOfPoints(); + } - if (to == 0) to = GetNumberOfPoints(); + if (to == 0) { + to = GetNumberOfPoints(); + } for (int i = from; i < to; i++) { if (this->GetData(i) > max) { @@ -286,7 +292,7 @@ Int_t TRestDetectorSignal::GetMaxIndex(Int_t from, Int_t to) { // z position by gaussian fit optional> -TRestDetectorSignal::GetMaxGauss() // returns a 2vector with the time of the peak time in us and the energy +TRestDetectorSignal::GetPeakGauss() // returns a 2vector with the time of the peak time in us and the energy { const auto indexMax = std::distance(fSignalCharge.begin(), std::max_element(fSignalCharge.begin(), fSignalCharge.end())); @@ -358,8 +364,8 @@ TRestDetectorSignal::GetMaxGauss() // returns a 2vector with the time of the pe // z position by landau fit -TVector2 -TRestDetectorSignal::GetMaxLandau() // returns a 2vector with the time of the peak time in us and the energy +optional> +TRestDetectorSignal::GetPeakLandau() // returns a 2vector with the time of the peak time in us and the energy { Int_t maxRaw = GetMaxIndex(); // The bin where the maximum of the raw signal is found Double_t maxRawTime = @@ -368,31 +374,29 @@ TRestDetectorSignal::GetMaxLandau() // returns a 2vector with the time of the p Double_t lowerLimit = maxRawTime - 0.2; // us Double_t upperLimit = maxRawTime + 0.4; // us - TF1* landau = new TF1("landau", "landau", lowerLimit, upperLimit); - TH1F* h1 = new TH1F("h1", "h1", 1000, 0, - 10); // Histogram to store the signal. For now the number of bins is fixed. + TF1 landau("landau", "landau", lowerLimit, upperLimit); + TH1F h("h1", "h1", 1000, 0, + 10); // Histogram to store the signal. For now the number of bins is fixed. // copying the signal peak to a histogram for (int i = 0; i < GetNumberOfPoints(); i++) { - h1->Fill(GetTime(i), GetData(i)); + h.Fill(GetTime(i), GetData(i)); } TFitResultPtr fitResult = - h1->Fit(landau, "QNRS"); // Q = quiet, no info in screen; N = no plot; R = fit in the function range; - // S = save and return the fit result + h.Fit(&landau, "QNRS"); // Q = quiet, no info in screen; N = no plot; R = fit in the function range; + // S = save and return the fit result if (fitResult->IsValid()) { - energy = landau->GetParameter(0); - time = landau->GetParameter(1); + energy = landau.GetParameter(0); + time = landau.GetParameter(1); + return make_pair(time, energy); } else { - // the fit failed, return -1 to indicate failure - energy = -1; - time = -1; + // the fit failed cout << endl << "WARNING: bad fit to signal with ID " << GetID() << " with maximum at time = " << maxRawTime - << " ns " << "\n" - << "Failed fit parameters = " << landau->GetParameter(0) << " || " << landau->GetParameter(1) - << " || " << landau->GetParameter(2) << "\n" - << "Assigned fit parameters : energy = " << energy << ", time = " << time << endl; + << " ns " << endl + << "Failed fit parameters = " << landau.GetParameter(0) << " || " << landau.GetParameter(1) + << " || " << landau.GetParameter(2) << endl; /* TCanvas* c2 = new TCanvas("c2", "Signal fit", 200, 10, 1280, 720); h1->Draw(); @@ -400,12 +404,8 @@ TRestDetectorSignal::GetMaxLandau() // returns a 2vector with the time of the p getchar(); delete c2; */ + return std::nullopt; } - - delete h1; - delete landau; - - return {time, energy}; } // z position by aget fit @@ -422,51 +422,44 @@ Double_t agetResponseFunction(Double_t* x, Double_t* par) { // x contains as ma return f; } -TVector2 -TRestDetectorSignal::GetMaxAget() // returns a 2vector with the time of the peak time in us and the energy +optional> +TRestDetectorSignal::GetPeakAget() // returns a 2vector with the time of the peak time in us and the energy { Int_t maxRaw = GetMaxIndex(); // The bin where the maximum of the raw signal is found Double_t maxRawTime = GetTime(maxRaw); // The time of the bin where the maximum of the raw signal is found - Double_t energy = 0, time = 0; // The intervals below are small because otherwise the function doesn't fit anymore. Double_t lowerLimit = maxRawTime - 0.2; // us Double_t upperLimit = maxRawTime + 0.7; // us - TF1* aget = new TF1("aget", agetResponseFunction, lowerLimit, upperLimit, 3); // - TH1F* h1 = new TH1F("h1", "h1", 1000, 0, - 10); // Histogram to store the signal. For now the number of bins is fixed. - aget->SetParameters(500, maxRawTime, 1.2); + TF1 aget("aget", agetResponseFunction, lowerLimit, upperLimit, 3); // + TH1F h("h1", "h1", 1000, 0, + 10); // Histogram to store the signal. For now the number of bins is fixed. + aget.SetParameters(500, maxRawTime, 1.2); // copying the signal peak to a histogram for (int i = 0; i < GetNumberOfPoints(); i++) { - h1->Fill(GetTime(i), GetData(i)); + h.Fill(GetTime(i), GetData(i)); } - TFitResultPtr fitResult = - h1->Fit(aget, "QNRS"); // Q = quiet, no info in screen; N = no plot; R = fit in - // the function range; S = save and return the fit result + TFitResultPtr fitResult = h.Fit(&aget, "QNRS"); // Q = quiet, no info in screen; N = no plot; R = fit in + // the function range; S = save and return the fit result if (fitResult->IsValid()) { - energy = aget->GetParameter(0); - time = aget->GetParameter(1); + double energy = aget.GetParameter(0); + double time = aget.GetParameter(1); + return make_pair(time, energy); } else { - // the fit failed, return -1 to indicate failure - energy = -1; - time = -1; + // the fit failed cout << endl << "WARNING: bad fit to signal with ID " << GetID() << " with maximum at time = " << maxRawTime - << " ns " << "\n" - << "Failed fit parameters = " << aget->GetParameter(0) << " || " << aget->GetParameter(1) - << " || " << aget->GetParameter(2) << "\n" - << "Assigned fit parameters : energy = " << energy << ", time = " << time << endl; + << " ns " << endl + << "Failed fit parameters = " << aget.GetParameter(0) << " || " << aget.GetParameter(1) << " || " + << aget.GetParameter(2) << endl; + return nullopt; } - - delete h1; - delete aget; - - return {time, energy}; } + Double_t TRestDetectorSignal::GetMaxPeakTime(Int_t from, Int_t to) { return GetTime(GetMaxIndex(from, to)); } Double_t TRestDetectorSignal::GetMinPeakValue() { return GetData(GetMinIndex()); } diff --git a/src/TRestDetectorSignalToHitsProcess.cxx b/src/TRestDetectorSignalToHitsProcess.cxx index bf30fabe..085a2da2 100644 --- a/src/TRestDetectorSignalToHitsProcess.cxx +++ b/src/TRestDetectorSignalToHitsProcess.cxx @@ -378,86 +378,37 @@ TRestEvent* TRestDetectorSignalToHitsProcess::ProcessEvent(TRestEvent* inputEven cout << "E1, E2, E3 = " << energy1 << ", " << energy2 << ", " << energy3 << endl; } - } else if (fMethod == "gaussFit") { - const auto peak = signal->GetMaxGauss(); - if (peak) { - const auto [time, energy] = peak.value(); - - const auto distanceToPlane = time * fDriftVelocity; - const auto z = zPosition + fieldZDirection * distanceToPlane; - - if (GetVerboseLevel() >= TRestStringOutput::REST_Verbose_Level::REST_Debug) { - cout << "Signal event : " << signal->GetSignalID() - << "--------------------------------------------------------" << endl; - cout << "GaussFit : time " << time << " ns and energy : " << energy << endl; - cout << "Signal to hit info : zPosition : " << zPosition - << "; fieldZDirection : " << fieldZDirection << " and driftV : " << fDriftVelocity - << endl; - cout << "Adding hit. Time : " << time << " ns x : " << x << " y : " << y << " z : " << z - << " Energy : " << energy << endl; - } + } else if (fMethod == "gaussFit" || fMethod == "landauFit" || fMethod == "agetFit") { + optional> peak; + if (fMethod == "gaussFit") { + peak = signal->GetPeakGauss(); + } else if (fMethod == "landauFit") { + peak = signal->GetPeakLandau(); + } else if (fMethod == "agetFit") { + peak = signal->GetPeakAget(); } else { - // Perhaps we should just skip it - double energy = -1; - double z = -1; - fHitsEvent->AddHit(x, y, z, energy, 0, type); + throw std::runtime_error("Invalid method"); } - } else if (fMethod == "landauFit") { - TVector2 landauFit = signal->GetMaxLandau(); - - Double_t hitTime = 0; - Double_t z = -1.0; - if (landauFit.X() >= 0.0) { - hitTime = landauFit.X(); - Double_t distanceToPlane = hitTime * fDriftVelocity; - z = zPosition + fieldZDirection * distanceToPlane; - } - Double_t energy = -1.0; - if (landauFit.Y() >= 0.0) { - energy = landauFit.Y(); - } - - if (GetVerboseLevel() >= TRestStringOutput::REST_Verbose_Level::REST_Debug) { - cout << "Signal event : " << signal->GetSignalID() - << "--------------------------------------------------------" << endl; - cout << "landauFit : time bin " << landauFit.X() << " and energy : " << landauFit.Y() << endl; - cout << "Signal to hit info : zPosition : " << zPosition - << "; fieldZDirection : " << fieldZDirection << " and driftV : " << fDriftVelocity - << endl; - cout << "Adding hit. Time : " << hitTime << " x : " << x << " y : " << y << " z : " << z - << " Energy : " << energy << endl; + if (!peak) { + continue; } + const auto [time, energy] = peak.value(); - fHitsEvent->AddHit(x, y, z, energy, 0, type); - - } else if (fMethod == "agetFit") { - TVector2 agetFit = signal->GetMaxAget(); - - Double_t hitTime = 0; - Double_t z = -1.0; - if (agetFit.X() >= 0.0) { - hitTime = agetFit.X(); - Double_t distanceToPlane = hitTime * fDriftVelocity; - z = zPosition + fieldZDirection * distanceToPlane; - } - Double_t energy = -1.0; - if (agetFit.Y() >= 0.0) { - energy = agetFit.Y(); - } + const auto distanceToPlane = time * fDriftVelocity; + const auto z = zPosition + fieldZDirection * distanceToPlane; if (GetVerboseLevel() >= TRestStringOutput::REST_Verbose_Level::REST_Debug) { cout << "Signal event : " << signal->GetSignalID() << "--------------------------------------------------------" << endl; - cout << "agetFit : time bin " << agetFit.X() << " and energy : " << agetFit.Y() << endl; + cout << "Method: " << fMethod << " : time " << time << " ns and energy : " << energy << endl; cout << "Signal to hit info : zPosition : " << zPosition << "; fieldZDirection : " << fieldZDirection << " and driftV : " << fDriftVelocity << endl; - cout << "Adding hit. Time : " << hitTime << " x : " << x << " y : " << y << " z : " << z + cout << "Adding hit. Time : " << time << " ns x : " << x << " y : " << y << " z : " << z << " Energy : " << energy << endl; } fHitsEvent->AddHit(x, y, z, energy, 0, type); - } else if (fMethod == "qCenter") { Double_t energy_signal = 0; Double_t distanceToPlane = 0; @@ -492,7 +443,7 @@ TRestEvent* TRestDetectorSignalToHitsProcess::ProcessEvent(TRestEvent* inputEven } } else if (fMethod == "intwindow") { Int_t nPoints = signal->GetNumberOfPoints(); - std::map > windowMap; + std::map> windowMap; for (int j = 0; j < nPoints; j++) { int index = signal->GetTime(j) / fIntWindow; auto it = windowMap.find(index); From 018d8a2b3d29ec003b9b0a4d5581e5257789a4ec Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Wed, 5 Jun 2024 09:58:07 +0200 Subject: [PATCH 06/10] print peak not found on process only --- src/TRestDetectorSignal.cxx | 75 +++++++----------------- src/TRestDetectorSignalToHitsProcess.cxx | 4 ++ 2 files changed, 25 insertions(+), 54 deletions(-) diff --git a/src/TRestDetectorSignal.cxx b/src/TRestDetectorSignal.cxx index cbc7e9dc..4a4cc73d 100644 --- a/src/TRestDetectorSignal.cxx +++ b/src/TRestDetectorSignal.cxx @@ -319,45 +319,25 @@ TRestDetectorSignal::GetPeakGauss() // returns a 2vector with the time of the p } } - TF1 gaus("gaus", "gaus", lowerLimit, upperLimit); + TF1 gauss("gaus", "gaus", lowerLimit, upperLimit); TH1F h("h", "h", GetNumberOfPoints(), GetTime(0), GetTime(GetNumberOfPoints() - 1)); // copying the signal peak to a histogram for (int i = 0; i < GetNumberOfPoints(); i++) { h.SetBinContent(i + 1, GetData(i)); } - /* - TCanvas* c = new TCanvas("c", "Signal fit", 200, 10, 1280, 720); - h->GetXaxis()->SetTitle("Time (us)"); - h->GetYaxis()->SetTitle("Amplitude"); - h->Draw(); - */ - TFitResultPtr fitResult = h.Fit(&gaus, "QNRS"); // Q = quiet, no info in screen; N = no plot; R = fit in + TFitResultPtr fitResult = h.Fit(&gauss, "QNRS"); // Q = quiet, no info in screen; N = no plot; R = fit in // the function range; S = save and return the fit result - if (fitResult->IsValid()) { - double energy = gaus.GetParameter(0); - double time = gaus.GetParameter(1); - - return make_pair(time, energy); - } else { - // The fit failed - cout << endl - << "WARNING: bad fit to signal with ID " << GetID() << " with maximum at time = " << timeMax - << " ns " << endl - << "Failed fit parameters = " << gaus.GetParameter(0) << " || " << gaus.GetParameter(1) << " || " - << gaus.GetParameter(2) << endl; - /* - TCanvas* c2 = new TCanvas("c2", "Signal fit", 200, 10, 1280, 720); - h->Draw(); - c2->Update(); - getchar(); - delete c2; - */ - + if (!fitResult->IsValid()) { return nullopt; } + + double energy = gauss.GetParameter(0); + double time = gauss.GetParameter(1); + + return make_pair(time, energy); } // z position by landau fit @@ -402,24 +382,14 @@ TRestDetectorSignal::GetPeakLandau() // returns a 2vector with the time of the TFitResultPtr fitResult = h.Fit(&landau, "QNRS"); // Q = quiet, no info in screen; N = no plot; R = fit in the function range; // S = save and return the fit result - if (fitResult->IsValid()) { - double energy = landau.GetParameter(0); - double time = landau.GetParameter(1); - return make_pair(time, energy); - } else { - // the fit failed - cout << endl - << "WARNING: bad fit to signal with ID " << GetID() << " with maximum at time = " << maxRawTime - << " us " << endl; - /* - TCanvas* c2 = new TCanvas("c2", "Signal fit", 200, 10, 1280, 720); - h->Draw(); - c2->Update(); - getchar(); - delete c2; - */ - return std::nullopt; + if (!fitResult->IsValid()) { + return nullopt; } + + double energy = landau.GetParameter(0); + double time = landau.GetParameter(1); + + return make_pair(time, energy); } // z position by aget fit @@ -477,17 +447,14 @@ TRestDetectorSignal::GetPeakAget() // returns a 2vector with the time of the pe TFitResultPtr fitResult = h.Fit(&aget, "QNRS"); // Q = quiet, no info in screen; N = no plot; R = fit in // the function range; S = save and return the fit result - if (fitResult->IsValid()) { - double energy = aget.GetParameter(0); - double time = aget.GetParameter(1); - return make_pair(time, energy); - } else { - // the fit failed - cout << endl - << "WARNING: bad fit to signal with ID " << GetID() << " with maximum at time = " << maxRawTime - << " ns " << endl; + if (!fitResult->IsValid()) { return nullopt; } + + double energy = aget.GetParameter(0); + double time = aget.GetParameter(1); + + return make_pair(time, energy); } Double_t TRestDetectorSignal::GetMaxPeakTime(Int_t from, Int_t to) { return GetTime(GetMaxIndex(from, to)); } diff --git a/src/TRestDetectorSignalToHitsProcess.cxx b/src/TRestDetectorSignalToHitsProcess.cxx index 085a2da2..b522c8b0 100644 --- a/src/TRestDetectorSignalToHitsProcess.cxx +++ b/src/TRestDetectorSignalToHitsProcess.cxx @@ -390,6 +390,10 @@ TRestEvent* TRestDetectorSignalToHitsProcess::ProcessEvent(TRestEvent* inputEven throw std::runtime_error("Invalid method"); } if (!peak) { + if (GetVerboseLevel() >= TRestStringOutput::REST_Verbose_Level::REST_Info) { + cout << "Unable to find peak for signal " << signal->GetSignalID() + << " with method: " << fMethod << endl; + } continue; } const auto [time, energy] = peak.value(); From b58964513afb77d690c2ad91d7534d88b9dcc941 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 5 Jun 2024 07:58:19 +0000 Subject: [PATCH 07/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/TRestDetectorSignal.cxx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/TRestDetectorSignal.cxx b/src/TRestDetectorSignal.cxx index 4a4cc73d..94df4207 100644 --- a/src/TRestDetectorSignal.cxx +++ b/src/TRestDetectorSignal.cxx @@ -327,8 +327,9 @@ TRestDetectorSignal::GetPeakGauss() // returns a 2vector with the time of the p h.SetBinContent(i + 1, GetData(i)); } - TFitResultPtr fitResult = h.Fit(&gauss, "QNRS"); // Q = quiet, no info in screen; N = no plot; R = fit in - // the function range; S = save and return the fit result + TFitResultPtr fitResult = + h.Fit(&gauss, "QNRS"); // Q = quiet, no info in screen; N = no plot; R = fit in + // the function range; S = save and return the fit result if (!fitResult->IsValid()) { return nullopt; From 16be34965f2909c5b38608096944a92b7b9ebbae Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Wed, 5 Jun 2024 13:31:30 +0200 Subject: [PATCH 08/10] code review --- src/TRestDetectorSignal.cxx | 2 -- src/TRestDetectorSignalToHitsProcess.cxx | 4 ++-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/TRestDetectorSignal.cxx b/src/TRestDetectorSignal.cxx index 94df4207..57ecf739 100644 --- a/src/TRestDetectorSignal.cxx +++ b/src/TRestDetectorSignal.cxx @@ -376,7 +376,6 @@ TRestDetectorSignal::GetPeakLandau() // returns a 2vector with the time of the // copying the signal peak to a histogram for (int i = 0; i < GetNumberOfPoints(); i++) { - h.Fill(GetTime(i), GetData(i)); h.SetBinContent(i + 1, GetData(i)); } @@ -441,7 +440,6 @@ TRestDetectorSignal::GetPeakAget() // returns a 2vector with the time of the pe // copying the signal peak to a histogram for (int i = 0; i < GetNumberOfPoints(); i++) { - h.Fill(GetTime(i), GetData(i)); h.SetBinContent(i + 1, GetData(i)); } diff --git a/src/TRestDetectorSignalToHitsProcess.cxx b/src/TRestDetectorSignalToHitsProcess.cxx index b522c8b0..4782aa07 100644 --- a/src/TRestDetectorSignalToHitsProcess.cxx +++ b/src/TRestDetectorSignalToHitsProcess.cxx @@ -404,11 +404,11 @@ TRestEvent* TRestDetectorSignalToHitsProcess::ProcessEvent(TRestEvent* inputEven if (GetVerboseLevel() >= TRestStringOutput::REST_Verbose_Level::REST_Debug) { cout << "Signal event : " << signal->GetSignalID() << "--------------------------------------------------------" << endl; - cout << "Method: " << fMethod << " : time " << time << " ns and energy : " << energy << endl; + cout << "Method: " << fMethod << " : time " << time << " us and energy : " << energy << endl; cout << "Signal to hit info : zPosition : " << zPosition << "; fieldZDirection : " << fieldZDirection << " and driftV : " << fDriftVelocity << endl; - cout << "Adding hit. Time : " << time << " ns x : " << x << " y : " << y << " z : " << z + cout << "Adding hit. Time : " << time << " us x : " << x << " y : " << y << " z : " << z << " Energy : " << energy << endl; } From 661e5754c319a43bf42c97a9586a8666aee05318 Mon Sep 17 00:00:00 2001 From: mariajmz Date: Thu, 6 Jun 2024 09:58:37 +0200 Subject: [PATCH 09/10] using TGraph to fit --- src/TRestDetectorSignal.cxx | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/src/TRestDetectorSignal.cxx b/src/TRestDetectorSignal.cxx index 57ecf739..fda69906 100644 --- a/src/TRestDetectorSignal.cxx +++ b/src/TRestDetectorSignal.cxx @@ -320,15 +320,11 @@ TRestDetectorSignal::GetPeakGauss() // returns a 2vector with the time of the p } TF1 gauss("gaus", "gaus", lowerLimit, upperLimit); - TH1F h("h", "h", GetNumberOfPoints(), GetTime(0), GetTime(GetNumberOfPoints() - 1)); - // copying the signal peak to a histogram - for (int i = 0; i < GetNumberOfPoints(); i++) { - h.SetBinContent(i + 1, GetData(i)); - } + auto signal_graph = std::unique_ptr(GetGraph()); TFitResultPtr fitResult = - h.Fit(&gauss, "QNRS"); // Q = quiet, no info in screen; N = no plot; R = fit in + signal_graph->Fit(&gauss, "QNRS"); // Q = quiet, no info in screen; N = no plot; R = fit in // the function range; S = save and return the fit result if (!fitResult->IsValid()) { @@ -337,7 +333,7 @@ TRestDetectorSignal::GetPeakGauss() // returns a 2vector with the time of the p double energy = gauss.GetParameter(0); double time = gauss.GetParameter(1); - + return make_pair(time, energy); } @@ -372,15 +368,11 @@ TRestDetectorSignal::GetPeakLandau() // returns a 2vector with the time of the } TF1 landau("landau", "landau", lowerLimit, upperLimit); - TH1F h("h", "h", GetNumberOfPoints(), GetTime(0), GetTime(GetNumberOfPoints() - 1)); - - // copying the signal peak to a histogram - for (int i = 0; i < GetNumberOfPoints(); i++) { - h.SetBinContent(i + 1, GetData(i)); - } + auto signal_graph = std::unique_ptr(GetGraph()); + TFitResultPtr fitResult = - h.Fit(&landau, "QNRS"); // Q = quiet, no info in screen; N = no plot; R = fit in the function range; + signal_graph->Fit(&landau, "QNRS"); // Q = quiet, no info in screen; N = no plot; R = fit in the function range; // S = save and return the fit result if (!fitResult->IsValid()) { return nullopt; @@ -435,15 +427,11 @@ TRestDetectorSignal::GetPeakAget() // returns a 2vector with the time of the pe } TF1 aget("aget", agetResponseFunction, lowerLimit, upperLimit, 3); // - TH1F h("h", "h", GetNumberOfPoints(), GetTime(0), GetTime(GetNumberOfPoints() - 1)); aget.SetParameters(500, maxRawTime, 1.2); - // copying the signal peak to a histogram - for (int i = 0; i < GetNumberOfPoints(); i++) { - h.SetBinContent(i + 1, GetData(i)); - } + auto signal_graph = std::unique_ptr(GetGraph()); - TFitResultPtr fitResult = h.Fit(&aget, "QNRS"); // Q = quiet, no info in screen; N = no plot; R = fit in + TFitResultPtr fitResult = signal_graph->Fit(&aget, "QNRS"); // Q = quiet, no info in screen; N = no plot; R = fit in // the function range; S = save and return the fit result if (!fitResult->IsValid()) { From edb1a13674f37d20e7c7ca204bf1e1cb5635492a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 6 Jun 2024 07:59:55 +0000 Subject: [PATCH 10/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/TRestDetectorSignal.cxx | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/TRestDetectorSignal.cxx b/src/TRestDetectorSignal.cxx index fda69906..362bdfff 100644 --- a/src/TRestDetectorSignal.cxx +++ b/src/TRestDetectorSignal.cxx @@ -325,7 +325,7 @@ TRestDetectorSignal::GetPeakGauss() // returns a 2vector with the time of the p TFitResultPtr fitResult = signal_graph->Fit(&gauss, "QNRS"); // Q = quiet, no info in screen; N = no plot; R = fit in - // the function range; S = save and return the fit result + // the function range; S = save and return the fit result if (!fitResult->IsValid()) { return nullopt; @@ -333,7 +333,7 @@ TRestDetectorSignal::GetPeakGauss() // returns a 2vector with the time of the p double energy = gauss.GetParameter(0); double time = gauss.GetParameter(1); - + return make_pair(time, energy); } @@ -370,10 +370,10 @@ TRestDetectorSignal::GetPeakLandau() // returns a 2vector with the time of the TF1 landau("landau", "landau", lowerLimit, upperLimit); auto signal_graph = std::unique_ptr(GetGraph()); - + TFitResultPtr fitResult = - signal_graph->Fit(&landau, "QNRS"); // Q = quiet, no info in screen; N = no plot; R = fit in the function range; - // S = save and return the fit result + signal_graph->Fit(&landau, "QNRS"); // Q = quiet, no info in screen; N = no plot; R = fit in the + // function range; S = save and return the fit result if (!fitResult->IsValid()) { return nullopt; } @@ -431,8 +431,9 @@ TRestDetectorSignal::GetPeakAget() // returns a 2vector with the time of the pe auto signal_graph = std::unique_ptr(GetGraph()); - TFitResultPtr fitResult = signal_graph->Fit(&aget, "QNRS"); // Q = quiet, no info in screen; N = no plot; R = fit in - // the function range; S = save and return the fit result + TFitResultPtr fitResult = + signal_graph->Fit(&aget, "QNRS"); // Q = quiet, no info in screen; N = no plot; R = fit in + // the function range; S = save and return the fit result if (!fitResult->IsValid()) { return nullopt;