Skip to content

Commit

Permalink
code review
Browse files Browse the repository at this point in the history
  • Loading branch information
lobis committed Jun 4, 2024
1 parent 06826f3 commit ef37ea2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 44 deletions.
4 changes: 2 additions & 2 deletions inc/TRestDetectorSignal.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class TRestDetectorSignal {
void ExponentialConvolution(Double_t fromTime, Double_t decayTime, Double_t offset = 0);
void SignalAddition(TRestDetectorSignal* inSgnl);

Bool_t isSorted();
Bool_t isSorted() const;
void Sort();

void GetDifferentialSignal(TRestDetectorSignal* diffSgnl, Int_t smearPoints = 5);
Expand All @@ -157,7 +157,7 @@ class TRestDetectorSignal {
fSignalCharge.clear();
}

void WriteSignalToTextFile(const TString& filename);
void WriteSignalToTextFile(const TString& filename) const;
void Print() const;

TGraph* GetGraph(Int_t color = 1);
Expand Down
74 changes: 32 additions & 42 deletions src/TRestDetectorSignal.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -297,15 +297,15 @@ TRestDetectorSignal::GetMaxGauss() // returns a 2vector with the time of the pe

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);
break;
}
}

// 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);
Expand All @@ -314,22 +314,21 @@ TRestDetectorSignal::GetMaxGauss() // returns a 2vector with the time of the pe
}

TF1 gaus("gaus", "gaus", lowerLimit, upperLimit);
TH1F h1("h1", "h1", GetNumberOfPoints(), GetTime(0), GetTime(GetNumberOfPoints() - 1));
TH1F h("h", "h", GetNumberOfPoints(), GetTime(0), GetTime(GetNumberOfPoints() - 1));

// copying the signal peak to a histogram
for (int i = 0; i < GetNumberOfPoints(); i++) {
h1.SetBinContent(i + 1, GetData(i));
h.SetBinContent(i + 1, GetData(i));
}
/*
TCanvas* c = new TCanvas("c", "Signal fit", 200, 10, 1280, 720);
h1->GetXaxis()->SetTitle("Time (us)");
h1->GetYaxis()->SetTitle("Amplitude");
h1->Draw();
h->GetXaxis()->SetTitle("Time (us)");
h->GetYaxis()->SetTitle("Amplitude");
h->Draw();
*/

TFitResultPtr fitResult =
h1.Fit(&gaus, "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(&gaus, "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 = gaus.GetParameter(0);
Expand All @@ -340,23 +339,20 @@ TRestDetectorSignal::GetMaxGauss() // returns a 2vector with the time of the pe
time = -1;
cout << endl
<< "WARNING: bad fit to signal with ID " << GetID() << " with maximum at time = " << maxRawTime
<< " ns "
<< "\n"
<< " ns " << "\n"
<< "Failed fit parameters = " << gaus.GetParameter(0) << " || " << gaus.GetParameter(1) << " || "
<< gaus.GetParameter(2) << "\n"
<< "Assigned fit parameters : energy = " << energy << ", time = " << time << endl;
/*
TCanvas* c2 = new TCanvas("c2", "Signal fit", 200, 10, 1280, 720);
h1->Draw();
h->Draw();
c2->Update();
getchar();
delete c2;
*/
}

TVector2 fitParam(time, energy);

return fitParam;
return {time, energy};
}

// z position by landau fit
Expand All @@ -374,15 +370,15 @@ TRestDetectorSignal::GetMaxLandau() // returns a 2vector with the time of the p

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);
break;
}
}

// 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);
Expand All @@ -391,16 +387,16 @@ TRestDetectorSignal::GetMaxLandau() // returns a 2vector with the time of the p
}

TF1 landau("landau", "landau", lowerLimit, upperLimit);
TH1F h1("h1", "h1", GetNumberOfPoints(), GetTime(0), GetTime(GetNumberOfPoints() - 1));
TH1F h("h", "h", GetNumberOfPoints(), GetTime(0), GetTime(GetNumberOfPoints() - 1));

// copying the signal peak to a histogram
for (int i = 0; i < GetNumberOfPoints(); i++) {
h1.SetBinContent(i + 1, GetData(i));
h.SetBinContent(i + 1, 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);
Expand All @@ -410,23 +406,20 @@ 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;
/*
TCanvas* c2 = new TCanvas("c2", "Signal fit", 200, 10, 1280, 720);
h1->Draw();
h->Draw();
c2->Update();
getchar();
delete c2;
*/
}

TVector2 fitParam(time, energy);

return fitParam;
return {time, energy};
}

// z position by aget fit
Expand Down Expand Up @@ -456,15 +449,15 @@ TRestDetectorSignal::GetMaxAget() // returns a 2vector with the time of the pea

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);
break;
}
}

// 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);
Expand All @@ -473,17 +466,16 @@ TRestDetectorSignal::GetMaxAget() // returns a 2vector with the time of the pea
}

TF1 aget("aget", agetResponseFunction, lowerLimit, upperLimit, 3); //
TH1F h1("h1", "h1", GetNumberOfPoints(), GetTime(0), GetTime(GetNumberOfPoints() - 1));
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++) {
h1.SetBinContent(i + 1, GetData(i));
h.SetBinContent(i + 1, 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);
Expand All @@ -494,17 +486,15 @@ 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;
}

TVector2 fitParam(time, energy);

return fitParam;
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()); }
Expand Down Expand Up @@ -562,7 +552,7 @@ Int_t TRestDetectorSignal::GetTimeIndex(Double_t t) {
return -1;
}

Bool_t TRestDetectorSignal::isSorted() {
Bool_t TRestDetectorSignal::isSorted() const {
for (int i = 0; i < GetNumberOfPoints() - 1; i++) {
if (GetTime(i + 1) < GetTime(i)) {
return false;
Expand Down Expand Up @@ -762,7 +752,7 @@ void TRestDetectorSignal::GetSignalGaussianConvolution(TRestDetectorSignal* conv
cout << "Final charge of the pulse " << totChargeFinal << endl;
}

void TRestDetectorSignal::WriteSignalToTextFile(const TString& filename) {
void TRestDetectorSignal::WriteSignalToTextFile(const TString& filename) const {
FILE* fff = fopen(filename.Data(), "w");
for (int i = 0; i < GetNumberOfPoints(); i++) {
fprintf(fff, "%e\t%e\n", GetTime(i), GetData(i));
Expand Down

0 comments on commit ef37ea2

Please sign in to comment.