Skip to content

Commit

Permalink
Merge pull request #114 from rest-for-physics/lobis-fix-noise
Browse files Browse the repository at this point in the history
Prevent noise from going out of bounds
  • Loading branch information
lobis authored Aug 20, 2023
2 parents 5da09a6 + 7c34116 commit 1bef356
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 38 deletions.
25 changes: 14 additions & 11 deletions inc/TRestRawSignal.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@
#define RestCore_TRestRawSignal

#include <TGraph.h>
#include <TObject.h>
#include <TRandom.h>
#include <TString.h>
#include <TVector2.h>

#include <iostream>
#include <string>
#include <vector>

//! It defines a Short_t array with a physical parameter that evolves in time using a fixed time bin.
class TRestRawSignal : public TObject {
class TRestRawSignal {
private:
void CalculateThresholdIntegral();

Expand All @@ -51,6 +52,9 @@ class TRestRawSignal : public TObject {

Bool_t fShowWarnings = true;

/// Seed used for random number generation
UInt_t fSeed = gRandom->GetSeed();

public:
/// A TGraph pointer used to store the TRestRawSignal drawing
TGraph* fGraph; //!
Expand Down Expand Up @@ -112,10 +116,7 @@ class TRestRawSignal : public TObject {
inline TVector2 GetRange() const { return fRange; }

/// Returns false if the baseline and its baseline fluctuation was not initialized.
inline Bool_t isBaseLineInitialized() {
if (fBaseLineSigma == 0 && fBaseLine == 0) return false;
return true;
}
inline Bool_t isBaseLineInitialized() const { return !(fBaseLineSigma == 0 && fBaseLine == 0); }

Double_t GetData(Int_t n) const;

Expand Down Expand Up @@ -147,16 +148,16 @@ class TRestRawSignal : public TObject {

void Initialize();

void AddPoint(Short_t d);

void AddCharge(Short_t d);
void AddPoint(Short_t);

void AddDeposit(Short_t d);
void AddPoint(Double_t);

void IncreaseBinBy(Int_t bin, Double_t data);

void InitializePointsOverThreshold(const TVector2& thrPar, Int_t nPointsOver, Int_t nPointsFlat = 512);

UInt_t GetSeed() const { return fSeed; }

Double_t GetIntegral();

Double_t GetIntegralInRange(Int_t startBin, Int_t endBin);
Expand Down Expand Up @@ -211,12 +212,14 @@ class TRestRawSignal : public TObject {

void Print() const;

void SetSeed(UInt_t seed) { fSeed = seed; }

TGraph* GetGraph(Int_t color = 1);

TRestRawSignal();
TRestRawSignal(Int_t nBins);
~TRestRawSignal();

ClassDef(TRestRawSignal, 1);
ClassDef(TRestRawSignal, 2);
};
#endif
2 changes: 2 additions & 0 deletions inc/TRestRawSignalEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class TRestRawSignalEvent : public TRestEvent {
return true;
}

std::vector<TRestRawSignal> GetSignals() const { return fSignal; }

// Setters
void AddSignal(TRestRawSignal& s);

Expand Down
13 changes: 12 additions & 1 deletion pipeline/processes/fit/GeneralFit.C
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@

#include <TRestRawSignal.h>
#include <TRestRawSignalAddNoiseProcess.h>
#include <TRestRawSignalEvent.h>
#include <TRestRawSignalFittingProcess.h>
#include <TRestRawSignalShapingProcess.h>

using namespace std;

Int_t GeneralFit(Bool_t draw = false) {
TRestRawSignalEvent* ev = new TRestRawSignalEvent();

TRestRawSignal* sgnl = new TRestRawSignal();
for (int n = 0; n < 512; n++) sgnl->AddPoint(0);
for (int n = 0; n < 512; n++) {
sgnl->AddPoint((Double_t)0);
}
sgnl->IncreaseBinBy(70, 100);
ev->AddSignal(*sgnl);

Expand Down
9 changes: 9 additions & 0 deletions pipeline/processes/fit/fit.C
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@

#include <TRestRawSignal.h>
#include <TRestRawSignalAddNoiseProcess.h>
#include <TRestRawSignalEvent.h>
#include <TRestRawSignalFittingProcess.h>
#include <TRestRawSignalShapingProcess.h>

using namespace std;

Int_t fit(Bool_t draw = false) {
TRestRawSignalEvent* ev = new TRestRawSignalEvent();

Expand Down
12 changes: 6 additions & 6 deletions src/TRestRawMemoryBufferToSignalProcess.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -304,19 +304,19 @@ TRestEvent* TRestRawMemoryBufferToSignalProcess::ProcessEvent(TRestEvent* inputE
SemaphoreRed(fSemaphoreId);

for (unsigned int s = 0; s < fShMem_daqInfo->nSignals; s++) {
TRestRawSignal sgnl;
sgnl.SetSignalID(fShMem_Buffer[s * (maxSamples + 1)]);
TRestRawSignal signal;
signal.SetSignalID(fShMem_Buffer[s * (maxSamples + 1)]);

if (GetVerboseLevel() >= TRestStringOutput::REST_Verbose_Level::REST_Debug)
cout << "s : " << s << " id : " << sgnl.GetSignalID() << endl;
cout << "s : " << s << " id : " << signal.GetSignalID() << endl;

for (int n = 0; n < maxSamples; n++) {
sgnl.AddPoint(fShMem_Buffer[s * (maxSamples + 1) + 1 + n]);
signal.AddPoint((Short_t)fShMem_Buffer[s * (maxSamples + 1) + 1 + n]);
}
fOutputRawSignalEvent->AddSignal(sgnl);
fOutputRawSignalEvent->AddSignal(signal);

if (GetVerboseLevel() >= TRestStringOutput::REST_Verbose_Level::REST_Extreme) {
sgnl.Print();
signal.Print();
GetChar();
}
}
Expand Down
41 changes: 22 additions & 19 deletions src/TRestRawSignal.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,19 @@ void TRestRawSignal::Reset() {
///////////////////////////////////////////////
/// \brief Adds a new point to the end of the signal data array
///
void TRestRawSignal::AddPoint(Short_t d) { fSignalData.push_back(d); }
void TRestRawSignal::AddPoint(Short_t value) { fSignalData.push_back(value); }

///////////////////////////////////////////////
/// \brief Adds a new point to the end of the signal data array. Same as
/// AddPoint.
///
void TRestRawSignal::AddCharge(Short_t d) { AddPoint(d); }

///////////////////////////////////////////////
/// \brief Adds a new point to the end of the signal data array. Same as
/// AddPoint.
/// \brief Adds a new point to the end of the signal data array
///
void TRestRawSignal::AddDeposit(Short_t d) { AddPoint(d); }
void TRestRawSignal::AddPoint(Double_t value) {
if (value > numeric_limits<Short_t>::max()) {
value = numeric_limits<Short_t>::max();
} else if (value < numeric_limits<Short_t>::min()) {
value = numeric_limits<Short_t>::min();
}
AddPoint((Short_t)value);
}

///////////////////////////////////////////////
/// \brief It overloads the operator [] so that we can retrieve a particular
Expand Down Expand Up @@ -568,15 +568,19 @@ void TRestRawSignal::GetDifferentialSignal(TRestRawSignal* diffSignal, Int_t sme
if (smearPoints <= 0) smearPoints = 1;
diffSignal->Initialize();

for (int i = 0; i < smearPoints; i++) diffSignal->AddPoint(0);
for (int i = 0; i < smearPoints; i++) {
diffSignal->AddPoint((Short_t)0);
}

for (int i = smearPoints; i < this->GetNumberOfPoints() - smearPoints; i++) {
Double_t value = 0.5 * (this->GetData(i + smearPoints) - GetData(i - smearPoints)) / smearPoints;

diffSignal->AddPoint((Short_t)value);
}

for (int i = GetNumberOfPoints() - smearPoints; i < GetNumberOfPoints(); i++) diffSignal->AddPoint(0);
for (int i = GetNumberOfPoints() - smearPoints; i < GetNumberOfPoints(); i++) {
diffSignal->AddPoint((Short_t)0);
}
}

///////////////////////////////////////////////
Expand All @@ -588,15 +592,14 @@ void TRestRawSignal::GetDifferentialSignal(TRestRawSignal* diffSignal, Int_t sme
/// as its standard deviation.
///
void TRestRawSignal::GetWhiteNoiseSignal(TRestRawSignal* noiseSignal, Double_t noiseLevel) {
double* dd = new double();
uintptr_t seed = (uintptr_t)dd + (uintptr_t)this;
delete dd;
TRandom3* fRandom = new TRandom3(seed);
TRandom3 random(fSeed);

for (int i = 0; i < GetNumberOfPoints(); i++) {
noiseSignal->AddPoint(this->GetData(i) + (Short_t)fRandom->Gaus(0, noiseLevel));
Double_t value = this->GetData(i) + random.Gaus(0, noiseLevel);
// do not cast as short so that there are no problems with overflows
// (https://github.com/rest-for-physics/rawlib/issues/113)
noiseSignal->AddPoint(value);
}
delete fRandom;
}

///////////////////////////////////////////////
Expand Down Expand Up @@ -628,7 +631,7 @@ void TRestRawSignal::GetSignalSmoothed(TRestRawSignal* smoothedSignal, Int_t ave
///////////////////////////////////////////////
/// \brief It smoothes the existing signal and returns it in a vector of Float_t values
///
/// \param averagingPoints It defines the number of neightbour consecutive
/// \param averagingPoints It defines the number of neighbour consecutive
/// points used to average the signal
///
/// \param option If the option is set to "EXCLUDE OUTLIERS", points that are too far away from the median
Expand Down
4 changes: 3 additions & 1 deletion src/TRestRawSignalAddNoiseProcess.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ void TRestRawSignalAddNoiseProcess::Initialize() {
/// corresponding TRestGeant4AnalysisProcess section inside the RML.
///
void TRestRawSignalAddNoiseProcess::LoadConfig(const string& configFilename, const string& name) {
if (LoadConfigFromFile(configFilename, name) == -1) LoadDefaultConfig();
if (LoadConfigFromFile(configFilename, name) == -1) {
LoadDefaultConfig();
}
}

///////////////////////////////////////////////
Expand Down

0 comments on commit 1bef356

Please sign in to comment.