Skip to content

Commit 9df8c2f

Browse files
authored
Merge pull request #119 from rest-for-physics/mariajmz_fits
Fixing problems with TRestDetectorSignal fitting methods
2 parents e47e9a5 + ea80a78 commit 9df8c2f

File tree

2 files changed

+104
-67
lines changed

2 files changed

+104
-67
lines changed

inc/TRestDetectorSignal.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ class TRestDetectorSignal {
138138
void ExponentialConvolution(Double_t fromTime, Double_t decayTime, Double_t offset = 0);
139139
void SignalAddition(TRestDetectorSignal* inSgnl);
140140

141-
Bool_t isSorted();
141+
Bool_t isSorted() const;
142142
void Sort();
143143

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

160-
void WriteSignalToTextFile(const TString& filename);
160+
void WriteSignalToTextFile(const TString& filename) const;
161161
void Print() const;
162162

163163
TGraph* GetGraph(Int_t color = 1);

src/TRestDetectorSignal.cxx

Lines changed: 102 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -291,31 +291,48 @@ TRestDetectorSignal::GetMaxGauss() // returns a 2vector with the time of the pe
291291
Double_t maxRawTime =
292292
GetTime(maxRaw); // The time of the bin where the maximum of the raw signal is found
293293
Double_t energy = 0, time = 0;
294-
Double_t lowerLimit = maxRawTime - 0.2; // us
295-
Double_t upperLimit = maxRawTime + 0.4; // us
296294

297-
TF1* gaus = new TF1("gaus", "gaus", lowerLimit, upperLimit);
298-
TH1F* h1 = new TH1F("h1", "h1", 1000, 0,
299-
10); // Histogram to store the signal. For now the number of bins is fixed.
295+
// Define fit limits
296+
Double_t threshold = GetData(maxRaw) * 0.9; // 90% of the maximum value
297+
298+
Double_t lowerLimit = maxRawTime, upperLimit = maxRawTime;
299+
300+
// Find the lower limit: time when signal drops to 90% of the max before the peak
301+
for (int i = maxRaw; i >= 0; --i) {
302+
if (GetData(i) <= threshold) {
303+
lowerLimit = GetTime(i);
304+
break;
305+
}
306+
}
307+
308+
// Find the upper limit: time when signal drops to 90% of the max after the peak
309+
for (int i = maxRaw; i < GetNumberOfPoints(); ++i) {
310+
if (GetData(i) <= threshold) {
311+
upperLimit = GetTime(i);
312+
break;
313+
}
314+
}
315+
316+
TF1 gaus("gaus", "gaus", lowerLimit, upperLimit);
317+
TH1F h("h", "h", GetNumberOfPoints(), GetTime(0), GetTime(GetNumberOfPoints() - 1));
300318

301319
// copying the signal peak to a histogram
302320
for (int i = 0; i < GetNumberOfPoints(); i++) {
303-
h1->Fill(GetTime(i), GetData(i));
321+
h.SetBinContent(i + 1, GetData(i));
304322
}
305323
/*
306324
TCanvas* c = new TCanvas("c", "Signal fit", 200, 10, 1280, 720);
307-
h1->GetXaxis()->SetTitle("Time (us)");
308-
h1->GetYaxis()->SetTitle("Amplitude");
309-
h1->Draw();
325+
h->GetXaxis()->SetTitle("Time (us)");
326+
h->GetYaxis()->SetTitle("Amplitude");
327+
h->Draw();
310328
*/
311329

312-
TFitResultPtr fitResult =
313-
h1->Fit(gaus, "QNRS"); // Q = quiet, no info in screen; N = no plot; R = fit in the function range; S
314-
// = save and return the fit result
330+
TFitResultPtr fitResult = h.Fit(&gaus, "QNRS"); // Q = quiet, no info in screen; N = no plot; R = fit in
331+
// the function range; S = save and return the fit result
315332

316333
if (fitResult->IsValid()) {
317-
energy = gaus->GetParameter(0);
318-
time = gaus->GetParameter(1);
334+
energy = gaus.GetParameter(0);
335+
time = gaus.GetParameter(1);
319336
} else {
320337
// the fit failed, return -1 to indicate failure
321338
energy = -1;
@@ -324,24 +341,19 @@ TRestDetectorSignal::GetMaxGauss() // returns a 2vector with the time of the pe
324341
<< "WARNING: bad fit to signal with ID " << GetID() << " with maximum at time = " << maxRawTime
325342
<< " ns "
326343
<< "\n"
327-
<< "Failed fit parameters = " << gaus->GetParameter(0) << " || " << gaus->GetParameter(1)
328-
<< " || " << gaus->GetParameter(2) << "\n"
344+
<< "Failed fit parameters = " << gaus.GetParameter(0) << " || " << gaus.GetParameter(1) << " || "
345+
<< gaus.GetParameter(2) << "\n"
329346
<< "Assigned fit parameters : energy = " << energy << ", time = " << time << endl;
330347
/*
331348
TCanvas* c2 = new TCanvas("c2", "Signal fit", 200, 10, 1280, 720);
332-
h1->Draw();
349+
h->Draw();
333350
c2->Update();
334351
getchar();
335352
delete c2;
336353
*/
337354
}
338355

339-
TVector2 fitParam(time, energy);
340-
341-
delete h1;
342-
delete gaus;
343-
344-
return fitParam;
356+
return {time, energy};
345357
}
346358

347359
// z position by landau fit
@@ -353,24 +365,42 @@ TRestDetectorSignal::GetMaxLandau() // returns a 2vector with the time of the p
353365
Double_t maxRawTime =
354366
GetTime(maxRaw); // The time of the bin where the maximum of the raw signal is found
355367
Double_t energy = 0, time = 0;
356-
Double_t lowerLimit = maxRawTime - 0.2; // us
357-
Double_t upperLimit = maxRawTime + 0.4; // us
358368

359-
TF1* landau = new TF1("landau", "landau", lowerLimit, upperLimit);
360-
TH1F* h1 = new TH1F("h1", "h1", 1000, 0,
361-
10); // Histogram to store the signal. For now the number of bins is fixed.
369+
// Define fit limits
370+
Double_t threshold = GetData(maxRaw) * 0.9; // 90% of the maximum value
371+
372+
Double_t lowerLimit = maxRawTime, upperLimit = maxRawTime;
373+
374+
// Find the lower limit: time when signal drops to 90% of the max before the peak
375+
for (int i = maxRaw; i >= 0; --i) {
376+
if (GetData(i) <= threshold) {
377+
lowerLimit = GetTime(i);
378+
break;
379+
}
380+
}
381+
382+
// Find the upper limit: time when signal drops to 90% of the max after the peak
383+
for (int i = maxRaw; i < GetNumberOfPoints(); ++i) {
384+
if (GetData(i) <= threshold) {
385+
upperLimit = GetTime(i);
386+
break;
387+
}
388+
}
389+
390+
TF1 landau("landau", "landau", lowerLimit, upperLimit);
391+
TH1F h("h", "h", GetNumberOfPoints(), GetTime(0), GetTime(GetNumberOfPoints() - 1));
362392

363393
// copying the signal peak to a histogram
364394
for (int i = 0; i < GetNumberOfPoints(); i++) {
365-
h1->Fill(GetTime(i), GetData(i));
395+
h.SetBinContent(i + 1, GetData(i));
366396
}
367397

368398
TFitResultPtr fitResult =
369-
h1->Fit(landau, "QNRS"); // Q = quiet, no info in screen; N = no plot; R = fit in the function range;
370-
// S = save and return the fit result
399+
h.Fit(&landau, "QNRS"); // Q = quiet, no info in screen; N = no plot; R = fit in the function range;
400+
// S = save and return the fit result
371401
if (fitResult->IsValid()) {
372-
energy = landau->GetParameter(0);
373-
time = landau->GetParameter(1);
402+
energy = landau.GetParameter(0);
403+
time = landau.GetParameter(1);
374404
} else {
375405
// the fit failed, return -1 to indicate failure
376406
energy = -1;
@@ -379,24 +409,19 @@ TRestDetectorSignal::GetMaxLandau() // returns a 2vector with the time of the p
379409
<< "WARNING: bad fit to signal with ID " << GetID() << " with maximum at time = " << maxRawTime
380410
<< " ns "
381411
<< "\n"
382-
<< "Failed fit parameters = " << landau->GetParameter(0) << " || " << landau->GetParameter(1)
383-
<< " || " << landau->GetParameter(2) << "\n"
412+
<< "Failed fit parameters = " << landau.GetParameter(0) << " || " << landau.GetParameter(1)
413+
<< " || " << landau.GetParameter(2) << "\n"
384414
<< "Assigned fit parameters : energy = " << energy << ", time = " << time << endl;
385415
/*
386416
TCanvas* c2 = new TCanvas("c2", "Signal fit", 200, 10, 1280, 720);
387-
h1->Draw();
417+
h->Draw();
388418
c2->Update();
389419
getchar();
390420
delete c2;
391421
*/
392422
}
393423

394-
TVector2 fitParam(time, energy);
395-
396-
delete h1;
397-
delete landau;
398-
399-
return fitParam;
424+
return {time, energy};
400425
}
401426

402427
// z position by aget fit
@@ -420,27 +445,43 @@ TRestDetectorSignal::GetMaxAget() // returns a 2vector with the time of the pea
420445
Double_t maxRawTime =
421446
GetTime(maxRaw); // The time of the bin where the maximum of the raw signal is found
422447
Double_t energy = 0, time = 0;
423-
// The intervals below are small because otherwise the function doesn't fit anymore.
424-
Double_t lowerLimit = maxRawTime - 0.2; // us
425-
Double_t upperLimit = maxRawTime + 0.7; // us
426448

427-
TF1* aget = new TF1("aget", agetResponseFunction, lowerLimit, upperLimit, 3); //
428-
TH1F* h1 = new TH1F("h1", "h1", 1000, 0,
429-
10); // Histogram to store the signal. For now the number of bins is fixed.
430-
aget->SetParameters(500, maxRawTime, 1.2);
449+
// Define fit limits
450+
Double_t threshold = GetData(maxRaw) * 0.9; // 90% of the maximum value
451+
452+
Double_t lowerLimit = maxRawTime, upperLimit = maxRawTime;
453+
454+
// Find the lower limit: time when signal drops to 90% of the max before the peak
455+
for (int i = maxRaw; i >= 0; --i) {
456+
if (GetData(i) <= threshold) {
457+
lowerLimit = GetTime(i);
458+
break;
459+
}
460+
}
461+
462+
// Find the upper limit: time when signal drops to 90% of the max after the peak
463+
for (int i = maxRaw; i < GetNumberOfPoints(); ++i) {
464+
if (GetData(i) <= threshold) {
465+
upperLimit = GetTime(i);
466+
break;
467+
}
468+
}
469+
470+
TF1 aget("aget", agetResponseFunction, lowerLimit, upperLimit, 3); //
471+
TH1F h("h", "h", GetNumberOfPoints(), GetTime(0), GetTime(GetNumberOfPoints() - 1));
472+
aget.SetParameters(500, maxRawTime, 1.2);
431473

432474
// copying the signal peak to a histogram
433475
for (int i = 0; i < GetNumberOfPoints(); i++) {
434-
h1->Fill(GetTime(i), GetData(i));
476+
h.SetBinContent(i + 1, GetData(i));
435477
}
436478

437-
TFitResultPtr fitResult =
438-
h1->Fit(aget, "QNRS"); // Q = quiet, no info in screen; N = no plot; R = fit in
439-
// the function range; S = save and return the fit result
479+
TFitResultPtr fitResult = h.Fit(&aget, "QNRS"); // Q = quiet, no info in screen; N = no plot; R = fit in
480+
// the function range; S = save and return the fit result
440481

441482
if (fitResult->IsValid()) {
442-
energy = aget->GetParameter(0);
443-
time = aget->GetParameter(1);
483+
energy = aget.GetParameter(0);
484+
time = aget.GetParameter(1);
444485
} else {
445486
// the fit failed, return -1 to indicate failure
446487
energy = -1;
@@ -449,18 +490,14 @@ TRestDetectorSignal::GetMaxAget() // returns a 2vector with the time of the pea
449490
<< "WARNING: bad fit to signal with ID " << GetID() << " with maximum at time = " << maxRawTime
450491
<< " ns "
451492
<< "\n"
452-
<< "Failed fit parameters = " << aget->GetParameter(0) << " || " << aget->GetParameter(1)
453-
<< " || " << aget->GetParameter(2) << "\n"
493+
<< "Failed fit parameters = " << aget.GetParameter(0) << " || " << aget.GetParameter(1) << " || "
494+
<< aget.GetParameter(2) << "\n"
454495
<< "Assigned fit parameters : energy = " << energy << ", time = " << time << endl;
455496
}
456497

457-
TVector2 fitParam(time, energy);
458-
459-
delete h1;
460-
delete aget;
461-
462-
return fitParam;
498+
return {time, energy};
463499
}
500+
464501
Double_t TRestDetectorSignal::GetMaxPeakTime(Int_t from, Int_t to) { return GetTime(GetMaxIndex(from, to)); }
465502

466503
Double_t TRestDetectorSignal::GetMinPeakValue() { return GetData(GetMinIndex()); }
@@ -518,7 +555,7 @@ Int_t TRestDetectorSignal::GetTimeIndex(Double_t t) {
518555
return -1;
519556
}
520557

521-
Bool_t TRestDetectorSignal::isSorted() {
558+
Bool_t TRestDetectorSignal::isSorted() const {
522559
for (int i = 0; i < GetNumberOfPoints() - 1; i++) {
523560
if (GetTime(i + 1) < GetTime(i)) {
524561
return false;
@@ -718,7 +755,7 @@ void TRestDetectorSignal::GetSignalGaussianConvolution(TRestDetectorSignal* conv
718755
cout << "Final charge of the pulse " << totChargeFinal << endl;
719756
}
720757

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

0 commit comments

Comments
 (0)