Public Member Functions | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
DuneDPhase3x1x1NoiseRemovalService Class Reference

#include <DuneDPhase3x1x1NoiseRemovalService.h>

Inheritance diagram for DuneDPhase3x1x1NoiseRemovalService:
AdcNoiseRemovalService

Public Member Functions

 DuneDPhase3x1x1NoiseRemovalService (fhicl::ParameterSet const &pset, art::ActivityRegistry &)
 
int update (AdcChannelDataMap &datamap) const
 
std::ostream & print (std::ostream &out=std::cout, std::string prefix="") const
 
- Public Member Functions inherited from AdcNoiseRemovalService
virtual ~AdcNoiseRemovalService ()=default
 

Private Types

using AdcChannelToolPtr = std::unique_ptr< const AdcChannelTool >
 

Private Member Functions

std::vector< float > getMeanCorrection (const std::vector< unsigned int > &channels, const AdcChannelDataMap &datamap) const
 
std::vector< float > getMedianCorrection (const std::vector< unsigned int > &channels, const AdcChannelDataMap &datamap) const
 
void fftFltInPlace (std::vector< float > &adc, const std::vector< float > &coeffs) const
 
std::vector< float > fftFlt (const std::vector< float > &adc, const std::vector< float > &coeffs) const
 
void removeCoherent (const GroupChannelMap &ch_groups, AdcChannelDataMap &datamap) const
 
void removeHighFreq (AdcChannelDataMap &datamap) const
 
void removeSlope (AdcChannelDataMap &datamap) const
 
void removeSlopePolynomial (AdcChannelDataMap &datamap) const
 
std::vector< boolroiMask (const AdcChannelData &adc) const
 
GroupChannelMap makeDaqGroups (size_t gsize, const std::vector< size_t > &gidx) const
 Make groups of channels using 3x1x1 DAQ numbering. Channels tagged as noisy are excluded at this stage. More...
 
GroupChannelMap makeGroups (size_t gsize, const std::vector< size_t > &gidx) const
 Make groups of channels using LArSoft numbering. Channels tagged as noisy are excluded at this stage. More...
 
bool has (const std::vector< size_t > &v, size_t idx) const
 
std::vector< double > GaussJordanSolv (std::vector< std::vector< long double > > matrix) const
 

Static Private Member Functions

static size_t get311Chan (size_t LAr_chan)
 Get 3x1x1 DAQ channel number from the LArSoft's channel index. More...
 

Private Attributes

bool fDoTwoPassFilter
 
bool fCoherent32
 
bool fCoherent16
 
bool fLowPassFlt
 
bool fLowPassFltSecondPass
 
bool fFlatten
 
bool fFlattenExtrapolate
 
float fLowPassFltFCut
 
float fLowPassFltExpo
 
std::vector< size_t > fCoherent32Groups
 
std::vector< size_t > fCoherent16Groups
 
std::vector< float > fLowPassCoeffs
 
bool fUseBasicROIForCNR
 
float fRoiStartThreshold
 
float fRoiEndThreshold
 
int fRoiPadLow
 
int fRoiPadHigh
 
int fMode
 
AdcIndex fBinsToSkip
 
std::string m_ROIBuilderToolFlattening
 
AdcChannelToolPtr m_pROIBuilderToolFlattening
 
std::string m_ROIBuilderToolCNR
 
AdcChannelToolPtr m_pROIBuilderToolCNR
 
std::string m_ROIBuilderToolFinal
 
AdcChannelToolPtr m_pROIBuilderToolFinal
 
const geo::GeometryfGeometry
 
util::LArFFTfFFT
 

Detailed Description

Definition at line 25 of file DuneDPhase3x1x1NoiseRemovalService.h.

Member Typedef Documentation

Definition at line 86 of file DuneDPhase3x1x1NoiseRemovalService.h.

Constructor & Destructor Documentation

DuneDPhase3x1x1NoiseRemovalService::DuneDPhase3x1x1NoiseRemovalService ( fhicl::ParameterSet const &  pset,
art::ActivityRegistry  
)

Definition at line 22 of file DuneDPhase3x1x1NoiseRemovalService_service.cc.

22  :
24  m_pROIBuilderToolCNR(nullptr),
25  m_pROIBuilderToolFinal(nullptr) {
26  fDoTwoPassFilter = pset.get<bool>("DoTwoPassFilter");
27  fCoherent32 = pset.get<bool>("Coherent32");
28  fCoherent16 = pset.get<bool>("Coherent16");
29  fLowPassFlt = pset.get<bool>("LowPassFlt");
30  fLowPassFltSecondPass = pset.get<bool>("LowPassFltSecondPass");
31  fLowPassFltFCut = pset.get<float>("LowPassFltFCut");
32  fLowPassFltExpo = pset.get<float>("LowPassFltExpo");
33  fFlatten = pset.get<bool>("Flatten");
34  fFlattenExtrapolate = pset.get<bool>("FlattenExtrapolate");
35  fCoherent32Groups = pset.get<std::vector<size_t>>("Coherent32Groups");
36  fCoherent16Groups = pset.get<std::vector<size_t>>("Coherent16Groups");
37  fUseBasicROIForCNR = pset.get<bool>("UseBasicROIForCNR");
38  fRoiStartThreshold = pset.get<float>("RoiStartThreshold");
39  fRoiEndThreshold = pset.get<float>("RoiEndThreshold");
40  fRoiPadLow = pset.get<int>("RoiPadLow");
41  fRoiPadHigh = pset.get<int>("RoiPadHigh");
42  fBinsToSkip = pset.get<AdcIndex>("BinsToSkip");
45 
46  // Retrieve tools
47  pset.get_if_present<std::string>("ROIBuilderToolFlattening", m_ROIBuilderToolFlattening);
48  pset.get_if_present<std::string>("ROIBuilderToolCNR", m_ROIBuilderToolCNR);
49  pset.get_if_present<std::string>("ROIBuilderToolFinal", m_ROIBuilderToolFinal);
51  if ( ptm == nullptr ) {
52  std::cout << "ERROR: Unable to retrieve tool manaager." << std::endl;
53  } else {
57  }
58  if (pset.get<std::string>("CorrMode") == "mean") { fMode = 1; }
59  else if (pset.get<std::string>("CorrMode") == "median") { fMode = 2; }
60  else
61  {
62  std::cout << "DuneDPhase3x1x1NoiseRemovalService WARNING: correction set to mean value." << std::endl;
63  fMode = 1;
64  }
65 
66  fLowPassCoeffs.resize(fFFT->FFTSize() / 2 + 1);
67 
68  for (size_t i = 0; i < fLowPassCoeffs.size(); ++i)
69  {
70  float f = 0.0015 * i; // [MHz]
71  fLowPassCoeffs[i] = 1.0 / sqrt(1.0 + pow(f/fLowPassFltFCut, fLowPassFltExpo));
72  }
73 }
std::string string
Definition: nybbler.cc:12
constexpr T pow(T x)
Definition: pow.h:72
unsigned int AdcIndex
Definition: AdcTypes.h:15
static DuneToolManager * instance(std::string fclname="", int dbg=1)
QTextStream & endl(QTextStream &s)

Member Function Documentation

std::vector< float > DuneDPhase3x1x1NoiseRemovalService::fftFlt ( const std::vector< float > &  adc,
const std::vector< float > &  coeffs 
) const
private

Definition at line 675 of file DuneDPhase3x1x1NoiseRemovalService_service.cc.

676 {
677  std::vector< TComplex > ch_spectrum(fFFT->FFTSize() / 2 + 1);
678  std::vector< float > ch_waveform(fFFT->FFTSize(), 0);
679 
680  size_t n_samples = adc.size();
681 
682  std::copy(adc.begin(), adc.end(), ch_waveform.begin());
683  for (size_t s = n_samples; s < ch_waveform.size(); ++s)
684  {
685  ch_waveform[s] = ch_waveform[2*n_samples - s - 1];
686  }
687  fFFT->DoFFT(ch_waveform, ch_spectrum);
688  for (size_t c = 0; c < coeffs.size(); ++c)
689  {
690  ch_spectrum[c] *= coeffs[c];
691  }
692  fFFT->DoInvFFT(ch_spectrum, ch_waveform);
693 
694  std::vector< float > flt_adc(n_samples);
695  std::copy(ch_waveform.begin(), ch_waveform.begin()+n_samples, flt_adc.begin());
696  return flt_adc;
697 }
void DoFFT(std::vector< T > &input, std::vector< TComplex > &output)
Definition: LArFFT.h:98
uint size() const
Definition: qcstring.h:201
void DoInvFFT(std::vector< TComplex > &input, std::vector< T > &output)
Definition: LArFFT.h:120
int FFTSize() const
Definition: LArFFT.h:69
T copy(T const &v)
static QCString * s
Definition: config.cpp:1042
void DuneDPhase3x1x1NoiseRemovalService::fftFltInPlace ( std::vector< float > &  adc,
const std::vector< float > &  coeffs 
) const
private

Definition at line 652 of file DuneDPhase3x1x1NoiseRemovalService_service.cc.

653 {
654  std::vector< TComplex > ch_spectrum(fFFT->FFTSize() / 2 + 1);
655  std::vector< float > ch_waveform(fFFT->FFTSize(), 0);
656 
657  size_t n_samples = adc.size();
658 
659  std::copy(adc.begin(), adc.end(), ch_waveform.begin());
660  for (size_t s = n_samples; s < ch_waveform.size(); ++s)
661  {
662  ch_waveform[s] = ch_waveform[2*n_samples - s - 1];
663  }
664  fFFT->DoFFT(ch_waveform, ch_spectrum);
665  for (size_t c = 0; c < coeffs.size(); ++c)
666  {
667  ch_spectrum[c] *= coeffs[c];
668  }
669  fFFT->DoInvFFT(ch_spectrum, ch_waveform);
670 
671  std::copy(ch_waveform.begin(), ch_waveform.begin()+n_samples, adc.begin());
672 }
void DoFFT(std::vector< T > &input, std::vector< TComplex > &output)
Definition: LArFFT.h:98
uint size() const
Definition: qcstring.h:201
void DoInvFFT(std::vector< TComplex > &input, std::vector< T > &output)
Definition: LArFFT.h:120
int FFTSize() const
Definition: LArFFT.h:69
T copy(T const &v)
static QCString * s
Definition: config.cpp:1042
std::vector< double > DuneDPhase3x1x1NoiseRemovalService::GaussJordanSolv ( std::vector< std::vector< long double > >  matrix) const
private

Definition at line 817 of file DuneDPhase3x1x1NoiseRemovalService_service.cc.

818 {
819 
820  int n = matrix.size();
821 
822  for (int i=0; i<n; i++) {
823  // Search for maximum in this column
824  double maxEl = std::abs(matrix[i][i]);
825  int maxRow = i;
826  for (int k=i+1; k<n; k++) {
827  if (std::abs(matrix[k][i]) > maxEl) {
828  maxEl = std::abs(matrix[k][i]);
829  maxRow = k;
830  }
831  }
832 
833  // Swap maximum row with current row (column by column)
834  for (int k=i; k<n+1;k++) {
835  double tmp = matrix[maxRow][k];
836  matrix[maxRow][k] = matrix[i][k];
837  matrix[i][k] = tmp;
838  }
839 
840  // Make all rows below this one 0 in current column
841  for (int k=i+1; k<n; k++) {
842  double c = -matrix[k][i]/matrix[i][i];
843  for (int j=i; j<n+1; j++) {
844  if (i==j) {
845  matrix[k][j] = 0;
846  } else {
847  matrix[k][j] += c * matrix[i][j];
848  }
849  }
850  }
851  }
852 
853  // Solve equation Ax=b for an upper triangular matrix A
854  std::vector<double> x(n);
855  for (int i=n-1; i>=0; i--) {
856  x[i] = matrix[i][n]/matrix[i][i];
857  for (int k=i-1;k>=0; k--) {
858  matrix[k][n] -= matrix[k][i] * x[i];
859  }
860  }
861  return x;
862 }
T abs(T value)
std::void_t< T > n
string tmp
Definition: languages.py:63
list x
Definition: train.py:276
size_t DuneDPhase3x1x1NoiseRemovalService::get311Chan ( size_t  LAr_chan)
staticprivate

Get 3x1x1 DAQ channel number from the LArSoft's channel index.

Definition at line 774 of file DuneDPhase3x1x1NoiseRemovalService_service.cc.

775 {
776  size_t crate = LAr_chan / 320;
777  size_t Chan311;
778 
779  LAr_chan = 8*(LAr_chan/8+1)-LAr_chan%8 -1;
780 
781  if(crate == 0)
782  {
783  LAr_chan = 32*(LAr_chan/32+1)-LAr_chan%32 -1;
784  size_t card = 4 - ((LAr_chan / 32) % 5);
785  if(LAr_chan > 159)
786  {
787  size_t shift = 31 - (LAr_chan % 32);
788  Chan311 = (2*card)*32 + shift;
789  }
790  else
791  {
792  size_t shift = 31 - (LAr_chan % 32);
793  Chan311 = (2*card + 1)*32 + shift;
794  }
795  }
796  else
797  {
798  size_t new_LAr_chan = LAr_chan - crate*320;
799  size_t card = ((new_LAr_chan / 32) % 5);
800  if(new_LAr_chan > 159)
801  {
802  size_t shift = new_LAr_chan % 32;
803  Chan311 = (2*card)*32 + shift;
804  }
805  else
806  {
807  size_t shift = new_LAr_chan % 32;
808  Chan311 = (2*card + 1)*32 + shift;
809  }
810  Chan311 = Chan311 + crate*320;
811  } // end of if/else statementi
812 
813  return Chan311;
814 }
struct dune::tde::crate crate
std::vector< float > DuneDPhase3x1x1NoiseRemovalService::getMeanCorrection ( const std::vector< unsigned int > &  channels,
const AdcChannelDataMap datamap 
) const
private

Definition at line 221 of file DuneDPhase3x1x1NoiseRemovalService_service.cc.

224 {
225  size_t n_samples = datamap.begin()->second.samples.size();
226  std::vector<size_t> ch_averaged(n_samples, 0);
227  std::vector<float> correction(n_samples, 0);
228 
229  for (unsigned int ch : channels)
230  {
231  auto iacd = datamap.find(ch);
232  if (iacd == datamap.end()) continue;
233 
234  const AdcChannelData & adc = iacd->second;
235 
237  {
238  auto mask = roiMask(adc);
239  for (size_t s = 0; s < n_samples; ++s)
240  {
241  if (!mask[s]) { continue; }
242 
243  AdcFlag flag = adc.flags.size() ? adc.flags[s] : AdcGood;
244  if (flag != AdcGood) { continue; }
245 
246  correction[s] += adc.samples[s];
247  ch_averaged[s]++;
248  }
249  }
250  else
251  {
252  for (size_t s = 0; s < n_samples; ++s)
253  {
254  if (adc.signal[s]) { continue; }
255 
256  AdcFlag flag = adc.flags.size() ? adc.flags[s] : AdcGood;
257  if (flag != AdcGood) { continue; }
258 
259  correction[s] += adc.samples[s];
260  ch_averaged[s]++;
261  }
262  }
263  }
264  for (size_t s = 0; s < n_samples; ++s)
265  {
266  if (ch_averaged[s] > 0) { correction[s] /= ch_averaged[s]; }
267  }
268  return correction;
269 }
short AdcFlag
Definition: AdcTypes.h:29
int16_t adc
Definition: CRTFragment.hh:202
const AdcFlag AdcGood
Definition: AdcTypes.h:32
AdcFilterVector signal
std::vector< bool > roiMask(const AdcChannelData &adc) const
static QCString * s
Definition: config.cpp:1042
AdcSignalVector samples
AdcFlagVector flags
std::vector< float > DuneDPhase3x1x1NoiseRemovalService::getMedianCorrection ( const std::vector< unsigned int > &  channels,
const AdcChannelDataMap datamap 
) const
private

Definition at line 271 of file DuneDPhase3x1x1NoiseRemovalService_service.cc.

274 {
275  size_t n_samples = datamap.begin()->second.samples.size();
276  std::vector< std::vector<float> > samples(n_samples);
277 
278  for (unsigned int ch : channels)
279  {
280  auto iacd = datamap.find(ch);
281  if (iacd == datamap.end()) continue;
282 
283  const AdcChannelData & adc = iacd->second;
284 
286  {
287  auto mask = roiMask(adc);
288  for (size_t s = 0; s < n_samples; ++s)
289  {
290  if (!mask[s]) { continue; }
291 
292  AdcFlag flag = adc.flags.size() ? adc.flags[s] : AdcGood;
293  if (flag != AdcGood) { continue; }
294 
295  samples[s].push_back(adc.samples[s]);
296  }
297  }
298  else
299  {
300  for (size_t s = 0; s < n_samples; ++s)
301  {
302  if (adc.signal[s]) { continue; }
303 
304  AdcFlag flag = adc.flags.size() ? adc.flags[s] : AdcGood;
305  if (flag != AdcGood) { continue; }
306 
307  samples[s].push_back(adc.samples[s]);
308  }
309  }
310 
311 
312  }
313 
314  std::vector<float> correction(n_samples);
315  for (size_t s = 0; s < n_samples; ++s)
316  {
317  size_t n = samples[s].size();
318  if (n < 2) { correction[s] = 0; continue; }
319 
320  std::sort(samples[s].begin(), samples[s].end());
321 
322  if ((n % 2) == 0) { correction[s] = 0.5 * (samples[s][n/2] + samples[s][(n/2)-1]); }
323  else { correction[s] = samples[s][(n-1)/2]; }
324  }
325  return correction;
326 }
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
short AdcFlag
Definition: AdcTypes.h:29
int16_t adc
Definition: CRTFragment.hh:202
const AdcFlag AdcGood
Definition: AdcTypes.h:32
std::void_t< T > n
AdcFilterVector signal
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:72
std::vector< bool > roiMask(const AdcChannelData &adc) const
static QCString * s
Definition: config.cpp:1042
AdcSignalVector samples
AdcFlagVector flags
bool DuneDPhase3x1x1NoiseRemovalService::has ( const std::vector< size_t > &  v,
size_t  idx 
) const
inlineprivate

Definition at line 60 of file DuneDPhase3x1x1NoiseRemovalService.h.

61  {
62  for (auto c : v) if (c == idx) return true;
63  return false;
64  }
GroupChannelMap DuneDPhase3x1x1NoiseRemovalService::makeDaqGroups ( size_t  gsize,
const std::vector< size_t > &  gidx 
) const
private

Make groups of channels using 3x1x1 DAQ numbering. Channels tagged as noisy are excluded at this stage.

Definition at line 732 of file DuneDPhase3x1x1NoiseRemovalService_service.cc.

733 {
734  GroupChannelMap groups;
735 
736  auto const & chStatus = art::ServiceHandle< lariov::ChannelStatusService >()->GetProvider();
737 
738  const unsigned int nchan = fGeometry->Nchannels();
739  for (unsigned int ch = 0; ch < nchan; ++ch)
740  {
741  size_t g = get311Chan(ch) / gsize;
742  //std::cout << "p:" << fGeometry->View(raw::ChannelID_t(ch)) << " g:" << g << " ch:" << ch << std::endl;
743  if (gidx.empty() || has(gidx, g))
744  {
745  if (chStatus.IsPresent(ch) && !chStatus.IsNoisy(ch)) { groups[g].push_back(ch); }
746  }
747  }
748 
749  return groups;
750 }
static constexpr double g
Definition: Units.h:144
static size_t get311Chan(size_t LAr_chan)
Get 3x1x1 DAQ channel number from the LArSoft&#39;s channel index.
unsigned int Nchannels() const
Returns the number of TPC readout channels in the detector.
std::unordered_map< unsigned int, std::vector< unsigned int > > GroupChannelMap
bool has(const std::vector< size_t > &v, size_t idx) const
GroupChannelMap DuneDPhase3x1x1NoiseRemovalService::makeGroups ( size_t  gsize,
const std::vector< size_t > &  gidx 
) const
private

Make groups of channels using LArSoft numbering. Channels tagged as noisy are excluded at this stage.

Definition at line 753 of file DuneDPhase3x1x1NoiseRemovalService_service.cc.

754 {
755  GroupChannelMap groups;
756 
757  auto const & chStatus = art::ServiceHandle< lariov::ChannelStatusService >()->GetProvider();
758 
759  const unsigned int nchan = fGeometry->Nchannels();
760  for (unsigned int ch = 0; ch < nchan; ++ch)
761  {
762  size_t g = ch / gsize;
763  //std::cout << "p:" << fGeometry->View(raw::ChannelID_t(ch)) << " g:" << g << " ch:" << ch << std::endl;
764  if (gidx.empty() || has(gidx, g))
765  {
766  if (chStatus.IsPresent(ch) && !chStatus.IsNoisy(ch)) { groups[g].push_back(ch); }
767  }
768  }
769 
770  return groups;
771 }
static constexpr double g
Definition: Units.h:144
unsigned int Nchannels() const
Returns the number of TPC readout channels in the detector.
std::unordered_map< unsigned int, std::vector< unsigned int > > GroupChannelMap
bool has(const std::vector< size_t > &v, size_t idx) const
std::ostream & DuneDPhase3x1x1NoiseRemovalService::print ( std::ostream &  out = std::cout,
std::string  prefix = "" 
) const
virtual

Implements AdcNoiseRemovalService.

Definition at line 864 of file DuneDPhase3x1x1NoiseRemovalService_service.cc.

865 {
866  out << prefix << "DuneDPhase3x1x1NoiseRemovalService: ...info" << std::endl;
867  return out;
868 }
QTextStream & endl(QTextStream &s)
void DuneDPhase3x1x1NoiseRemovalService::removeCoherent ( const GroupChannelMap ch_groups,
AdcChannelDataMap datamap 
) const
private

Definition at line 328 of file DuneDPhase3x1x1NoiseRemovalService_service.cc.

329 {
330  if (datamap.empty()) return;
331 
332  size_t n_samples = datamap.begin()->second.samples.size();
333  std::vector<double> correction(n_samples);
334 
335  for (const auto & entry : ch_groups)
336  {
337  const auto & channels = entry.second;
338  std::vector<float> correction;
339 
340  if (fMode == 1) // mean
341  {
342  correction = getMeanCorrection(channels, datamap);
343  }
344  else if (fMode == 2) // median
345  {
346  correction = getMedianCorrection(channels, datamap);
347  }
348 
349  for (unsigned int ch : channels)
350  {
351  auto iacd = datamap.find(ch);
352  if (iacd == datamap.end()) continue;
353 
354  AdcChannelData & adc = iacd->second;
355  for (size_t s = 0; s < n_samples; ++s)
356  {
357  adc.samples[s] -= correction[s];
358  }
359 
360  if (adc.samples[2] - adc.samples[1] > 20) // ugly fix of the two ticks in plane0
361  {
362  adc.samples[0] = adc.samples[2];
363  adc.samples[1] = adc.samples[2];
364  }
365  }
366  }
367 }
QList< Entry > entry
int16_t adc
Definition: CRTFragment.hh:202
std::vector< float > getMedianCorrection(const std::vector< unsigned int > &channels, const AdcChannelDataMap &datamap) const
std::vector< float > getMeanCorrection(const std::vector< unsigned int > &channels, const AdcChannelDataMap &datamap) const
static QCString * s
Definition: config.cpp:1042
AdcSignalVector samples
void DuneDPhase3x1x1NoiseRemovalService::removeHighFreq ( AdcChannelDataMap datamap) const
private

Definition at line 370 of file DuneDPhase3x1x1NoiseRemovalService_service.cc.

371 {
372  auto const & chStatus = art::ServiceHandle< lariov::ChannelStatusService >()->GetProvider();
373 
374  for (auto & entry : datamap)
375  {
376  if (chStatus.IsPresent(entry.first) && !chStatus.IsNoisy(entry.first)) { fftFltInPlace(entry.second.samples, fLowPassCoeffs); }
377  }
378 }
QList< Entry > entry
void fftFltInPlace(std::vector< float > &adc, const std::vector< float > &coeffs) const
QCollection::Item first()
Definition: qglist.cpp:807
void DuneDPhase3x1x1NoiseRemovalService::removeSlope ( AdcChannelDataMap datamap) const
private

Definition at line 381 of file DuneDPhase3x1x1NoiseRemovalService_service.cc.

382 {
383  size_t n_samples = datamap.begin()->second.samples.size();
384  std::vector< float > slope(n_samples);
385 
386  auto const & chStatus = art::ServiceHandle< lariov::ChannelStatusService >()->GetProvider();
387 
388  for (auto & entry : datamap)
389  {
390  if (!chStatus.IsPresent(entry.first) || chStatus.IsNoisy(entry.first)) { continue; }
391 
392  auto & adc = entry.second.samples;
393  auto mask = roiMask(entry.second);
394 
395  bool start = true;
396  float i = 0, s0 = 0, s1 = 0;
397  while (i < n_samples)
398  {
399  if (mask[i])
400  {
401  if (start) { s0 = adc[i]; start = false; }
402  else { s0 = 0.8 * s0 + 0.2 * adc[i]; } // average over some past adc
403  slope[i] = adc[i]; ++i;
404  }
405  else
406  {
407  size_t j = i;
408  while ((j < n_samples) && !mask[j]) { ++j; }
409  if (j < n_samples)
410  {
411  size_t k = 1;
412  float f = 1, g = f;
413  s1 = adc[j];
414  while ((k < 25) && (j+k < n_samples) && mask[j+k])
415  {
416  f *= 0.8; g += f; s1 += f * adc[j+k]; ++k;
417  }
418  s1 /= g; // average ofer following adc
419  }
420  else { s1 = s0; }
421 
422  float ds = (s1 - s0) / (j - i + 1);
423 
424  j = i;
425  while ((j < n_samples) && !mask[j])
426  {
427  slope[j] = s0 + (j - i + 1) * ds;
428  ++j;
429  }
430  start = true;
431  i = j;
432  }
433  }
434 
435  double y, sx = 0, sy = 0, sxy = 0, sx2 = 0, sy2 = 0;
436  for (size_t s = 0; s < n_samples; ++s)
437  {
438  y = slope[s]; sx += s; sy += y; sxy += s*y; sx2 += s*s; sy2 += y*y;
439  }
440 
441  double ssx = sx2 - ((sx * sx) / n_samples);
442  double c = sxy - ((sx * sy) / n_samples);
443  double mx = sx / n_samples;
444  double my = sy / n_samples;
445  double b = my - ((c / ssx) * mx);
446  double a = c / ssx;
447  for (size_t s = 0; s < n_samples; ++s) { adc[s] -= (a*s + b); }
448  }
449 }
QList< Entry > entry
static constexpr double g
Definition: Units.h:144
int16_t adc
Definition: CRTFragment.hh:202
QCollection::Item first()
Definition: qglist.cpp:807
const double a
static bool * b
Definition: config.cpp:1043
std::vector< bool > roiMask(const AdcChannelData &adc) const
static QCString * s
Definition: config.cpp:1042
void DuneDPhase3x1x1NoiseRemovalService::removeSlopePolynomial ( AdcChannelDataMap datamap) const
private

Definition at line 452 of file DuneDPhase3x1x1NoiseRemovalService_service.cc.

453 {
454  AdcIndex n_samples = datamap.begin()->second.samples.size(); //1667
455 // std::vector< float > slope(n_samples);
456 
457  auto const & chStatus = art::ServiceHandle< lariov::ChannelStatusService >()->GetProvider();
458 
459  for (auto & entry : datamap)
460  {
461  if (!chStatus.IsPresent(entry.first) || chStatus.IsNoisy(entry.first)) { continue; }
462 
463  auto & adc = entry.second.samples;
464  auto & signal = entry.second.signal;
465 // auto & channel = entry.second.channel;
466 
467  //preparation for polynomial fit
468  int fOrder = 3; //order of the polynomial to be fitted to baseline
469  std::vector<long double> line(fOrder+2,0.);
470  std::vector< std::vector< long double> > matrix(fOrder+1,line);
471 
472  for(int i = 0; i < fOrder+1; i++)
473  {
474  for(int j = 0; j < fOrder+2; j++)
475  {
476  matrix[i][j] = 0.;
477  }
478  }
479 
480 // double x, y;
481  std::vector<double> x, y;
482  x.resize(n_samples, 0.);
483  y.resize(n_samples, 0.);
484  AdcIndex np = 0;
485  std::vector<double> sol;
486  sol.resize(fOrder+1, 0.);
487 
489  {
490  // check if ROI at beginning of waveform
491  if(signal[fBinsToSkip])
492  {
493 // std::cout << "ROI at fBinsToSkip. Channel: " << channel << std::endl;
494  AdcIndex ROIStart = fBinsToSkip;
495  AdcIndex ROIEnd = fBinsToSkip;
496  for( AdcIndex a = ROIStart; a < n_samples; a++)
497  {
498  if(!signal[a])
499  {
500  ROIEnd = a-1;
501  break;
502  }
503  }
504 
505 // std::cout << "ROIStart: " << ROIStart << std::endl;
506 // std::cout << "ROIEnd: " << ROIEnd << std::endl;
507 
508  // do linear regression for following 200 bins
509  double sx = 0., sy = 0., sxy = 0., sx2 = 0.;
510  AdcIndex a = ROIEnd+1;
511  AdcIndex aCount = 0;
512  while( aCount < 200 && a < n_samples )
513  {
514  if(!signal[a])
515  {
516  sx += a; sy += adc[a]; sxy += a*adc[a]; sx2 += a*a;
517  aCount++;
518  }
519  a++;
520  }
521  double c = (sy*sx2 - sx*sxy)/((double)aCount*sx2 - sx*sx);
522  double d = ((double)aCount*sxy - sx*sy)/((double)aCount*sx2 - sx*sx);
523 
524 /*
525  std::cout << "sx: " << sx << std::endl;
526  std::cout << "sy: " << sy << std::endl;
527  std::cout << "sxy: " << sxy << std::endl;
528  std::cout << "sx2: " << sx2 << std::endl;
529  std::cout << "c: " << c << std::endl;
530  std::cout << "d: " << d << std::endl;
531  std::cout << std::endl;
532 */
533  for( AdcIndex a = ROIStart; a <= ROIEnd; a++)
534  {
535  x[a] = a;
536  y[a] = c + d*a;
537 // std::cout << "x: " << x[a] << "\t" << "y: " << y[a] << std::endl;
538  }
539  }
540 
541  // check if ROI at end of waveform
542  if(signal[n_samples-1])
543  {
544 // std::cout << "ROI at n_samples-1. Channel: " << channel << std::endl;
545  AdcIndex ROIStart = n_samples-1;
546  AdcIndex ROIEnd = n_samples-1;
547  for( AdcIndex a = ROIStart; a >= fBinsToSkip; a--)
548  {
549  if(!signal[a])
550  {
551  ROIStart = a+1;
552  break;
553  }
554  }
555 // std::cout << "ROIStart: " << ROIStart << std::endl;
556 // std::cout << "ROIEnd: " << ROIEnd << std::endl;
557 
558  // do linear regression for previous 200 bins
559  double sx = 0., sy = 0., sxy = 0., sx2 = 0.;
560  AdcIndex a = ROIEnd-1;
561  AdcIndex aCount = 0;
562  while( aCount < 200 && a >= fBinsToSkip)
563  {
564  if(!signal[a])
565  {
566  sx += a; sy += adc[a]; sxy += a*adc[a]; sx2 += a*a;
567  aCount++;
568  }
569  a--;
570  }
571 // std::cout << "aCount: " << aCount << std::endl;
572 
573  double c = (sy*sx2 - sx*sxy)/((double)aCount*sx2 - sx*sx);
574  double d = ((double)aCount*sxy - sx*sy)/((double)aCount*sx2 - sx*sx);
575 /*
576  std::cout << "sx: " << sx << std::endl;
577  std::cout << "sy: " << sy << std::endl;
578  std::cout << "sxy: " << sxy << std::endl;
579  std::cout << "sx2: " << sx2 << std::endl;
580  std::cout << "c: " << c << std::endl;
581  std::cout << "d: " << d << std::endl;
582  std::cout << std::endl;
583 */
584  for( AdcIndex a = ROIEnd; a >= ROIStart; a--)
585  {
586  x[a] = a;
587  y[a] = c + d*a;
588 // std::cout << "x: " << x[a] << "\t" << "y: " << y[a] << std::endl;
589  }
590  }
591  } //if fFlattenExtrapolate
592 
593  for(AdcIndex i = fBinsToSkip; i < n_samples; i++)
594  {
595  if(signal[i])
596  {
597  x[i] = i;
598  y[i] = 0;
599  }
600  else
601  {
602  x[i] = i;
603  y[i] = adc[i];
604  }
605 
606  int j = 0;
607  for( int l = 1; l <= fOrder+1; l++)
608  {
609  long double wt = pow(x[i], l-1);
610  int k = 0;
611 
612  for(int m=1; m <= l; m++)
613  {
614  matrix[j][k] += wt*pow(x[i],m-1);
615  k++;
616  }
617 
618  matrix[j][fOrder+1] += y[i]*wt;
619  j++;
620  }
621  np++;
622  }
623 
624 
625  for(int i = 1; i < fOrder+1; i++){
626  for(int j = 0; j<i; j++){
627  matrix[j][i] = matrix[i][j];
628  }
629  }
630 
631  //get parameters for polynomial with Gauss-Jordan
632  if(np > n_samples/10) //need minimum number of bins for fit
633  {
634  sol = GaussJordanSolv(matrix);
635 
636  //subtract fit from waveform
637  for(AdcIndex i = fBinsToSkip; i < n_samples; i++)
638  {
639  double corr = 0.;
640  for(size_t a = 0; a < sol.size(); a++)
641  {
642  corr += sol[a]*pow(i,a);
643  }
644  adc[i] -= corr;
645  }
646  }
647  }
648 }
QList< Entry > entry
constexpr T pow(T x)
Definition: pow.h:72
int16_t adc
Definition: CRTFragment.hh:202
static QStrList * l
Definition: config.cpp:1044
QCollection::Item first()
Definition: qglist.cpp:807
const double a
std::vector< double > GaussJordanSolv(std::vector< std::vector< long double > > matrix) const
unsigned int AdcIndex
Definition: AdcTypes.h:15
void line(double t, double *p, double &x, double &y, double &z)
list x
Definition: train.py:276
std::vector< bool > DuneDPhase3x1x1NoiseRemovalService::roiMask ( const AdcChannelData adc) const
private

Definition at line 700 of file DuneDPhase3x1x1NoiseRemovalService_service.cc.

701 {
702  std::vector<bool> mask(adc.samples.size(), true);
703 
704  auto adc_flt = fftFlt(adc.samples, fLowPassCoeffs);
705 
706  bool inroi = false;
707  for (int i = 0; i < (int)adc_flt.size(); ++i)
708  {
709  auto sig = adc_flt[i];
710  if (inroi)
711  {
712  if (sig > fRoiEndThreshold) { mask[i] = false; }
713  else
714  {
715  for (int p = 0; p <= fRoiPadHigh; ++p) { if ((i + p) < (int)mask.size()) { mask[i + p] = false; } }
716  inroi = false;
717  }
718  }
719  else
720  {
721  if (sig > fRoiStartThreshold )
722  {
723  for (int p = fRoiPadLow; p >= 0; --p) { if (i - p >= 0) { mask[i - p] = false; } }
724  inroi = true;
725  }
726  }
727  }
728  return mask;
729 }
p
Definition: test.py:223
std::vector< float > fftFlt(const std::vector< float > &adc, const std::vector< float > &coeffs) const
AdcSignalVector samples
int DuneDPhase3x1x1NoiseRemovalService::update ( AdcChannelDataMap datamap) const
virtual

Implements AdcNoiseRemovalService.

Definition at line 76 of file DuneDPhase3x1x1NoiseRemovalService_service.cc.

77 {
78  const std::string myname = "DuneDPhase3x1x1NoiseRemovalService:update: ";
79  if ( datamap.size() == 0 ) {
80  std::cout << myname << "WARNING: No channels found." << std::endl;
81  return 1;
82  }
83 
84  unsigned int nsig = 0;
85  bool first = true;
86  for (const AdcChannelDataMap::value_type& ent : datamap)
87  {
88  const AdcChannelData& data = ent.second;
89  if (first) { nsig = data.samples.size(); first = false; }
90  else if (data.samples.size() != nsig)
91  {
92  std::cout << myname << "WARNING: Channels have inconsistent sample counts." << std::endl;
93  return 2;
94  }
95  }
96 
97  if (nsig == 0)
98  {
99  std::cout << myname << "WARNING: No ADC samples found." << std::endl;
100  return 3;
101  }
102 
103  if(fDoTwoPassFilter)
104  {
105  //First pass
106 
107  //Use a copy of the data map to determine ROI. Actual datamap is gonna be used in second pass.
108  AdcChannelDataMap tempdatamap = datamap;
109 
110  if (fLowPassFlt)
111  {
112  removeHighFreq(tempdatamap);
113  }
114 
115 
116 
117  if (fFlatten)
118  {
119  removeSlopePolynomial(tempdatamap);
120  }
121 
122  //Rebuild ROI after low pass and flattening
123  for (auto & entry : tempdatamap)
124  {
125  auto & acd = entry.second;
126  m_pROIBuilderToolFlattening->update(acd);
127  }
128 
129  if (fCoherent16)
130  {
131  auto ch_groups = makeDaqGroups(16, fCoherent16Groups);
132  removeCoherent(ch_groups, tempdatamap);
133  }
134 
135  if (fCoherent32)
136  {
137  auto ch_groups = makeDaqGroups(32, fCoherent32Groups);
138  removeCoherent(ch_groups, tempdatamap);
139  }
140 
141  //Rebuild ROI after CNR
142  for (auto & entry : tempdatamap)
143  {
144  auto & acd = entry.second;
145  m_pROIBuilderToolCNR->update(acd);
146  }
147 
148  //copy the ROI found in tempdatampa to datamap
149  AdcChannelDataMap::iterator ittempdatamap = tempdatamap.begin();
150  for (auto & entry : datamap)
151  {
152  auto & acd = entry.second;
153  auto acdtemp = ittempdatamap->second;
154  acd.signal = acdtemp.signal;
155  acd.rois = acdtemp.rois;
156  ittempdatamap++;
157  }
158 
159  //Second pass
160 
162  {
163  removeHighFreq(datamap);
164  }
165 
166  if (fFlatten)
167  {
168  removeSlopePolynomial(datamap);
169  }
170 
171  if (fCoherent16)
172  {
173  auto ch_groups = makeDaqGroups(16, fCoherent16Groups);
174  removeCoherent(ch_groups, datamap);
175  }
176 
177  if (fCoherent32)
178  {
179  auto ch_groups = makeDaqGroups(32, fCoherent32Groups);
180  removeCoherent(ch_groups, datamap);
181  }
182 
183  //Rebuild ROI after second pass (final ROI)
184  for (auto & entry : datamap)
185  {
186  auto & acd = entry.second;
187  m_pROIBuilderToolFinal->update(acd);
188  }
189 
190  }
191  else
192  {
193  if (fCoherent32)
194  {
195  auto ch_groups = makeDaqGroups(32, fCoherent32Groups);
196  removeCoherent(ch_groups, datamap);
197  }
198 
199  if (fCoherent16)
200  {
201  auto ch_groups = makeDaqGroups(16, fCoherent16Groups);
202  removeCoherent(ch_groups, datamap);
203  }
204 
205  if (fLowPassFlt)
206  {
207  removeHighFreq(datamap);
208  }
209 
210  if (fFlatten)
211  {
212  removeSlope(datamap);
213  }
214  }
215 // std::cout << myname << "...done." << std::endl;
216 
217  return 0;
218 }
intermediate_table::iterator iterator
QList< Entry > entry
std::string string
Definition: nybbler.cc:12
void removeCoherent(const GroupChannelMap &ch_groups, AdcChannelDataMap &datamap) const
void removeHighFreq(AdcChannelDataMap &datamap) const
std::map< AdcChannel, AdcChannelData > AdcChannelDataMap
void removeSlopePolynomial(AdcChannelDataMap &datamap) const
GroupChannelMap makeDaqGroups(size_t gsize, const std::vector< size_t > &gidx) const
Make groups of channels using 3x1x1 DAQ numbering. Channels tagged as noisy are excluded at this stag...
AdcSignalVector samples
QTextStream & endl(QTextStream &s)

Member Data Documentation

AdcIndex DuneDPhase3x1x1NoiseRemovalService::fBinsToSkip
private

Definition at line 83 of file DuneDPhase3x1x1NoiseRemovalService.h.

bool DuneDPhase3x1x1NoiseRemovalService::fCoherent16
private

Definition at line 72 of file DuneDPhase3x1x1NoiseRemovalService.h.

std::vector< size_t > DuneDPhase3x1x1NoiseRemovalService::fCoherent16Groups
private

Definition at line 75 of file DuneDPhase3x1x1NoiseRemovalService.h.

bool DuneDPhase3x1x1NoiseRemovalService::fCoherent32
private

Definition at line 72 of file DuneDPhase3x1x1NoiseRemovalService.h.

std::vector< size_t > DuneDPhase3x1x1NoiseRemovalService::fCoherent32Groups
private

Definition at line 74 of file DuneDPhase3x1x1NoiseRemovalService.h.

bool DuneDPhase3x1x1NoiseRemovalService::fDoTwoPassFilter
private

Definition at line 72 of file DuneDPhase3x1x1NoiseRemovalService.h.

util::LArFFT* DuneDPhase3x1x1NoiseRemovalService::fFFT
mutableprivate

Definition at line 99 of file DuneDPhase3x1x1NoiseRemovalService.h.

bool DuneDPhase3x1x1NoiseRemovalService::fFlatten
private

Definition at line 72 of file DuneDPhase3x1x1NoiseRemovalService.h.

bool DuneDPhase3x1x1NoiseRemovalService::fFlattenExtrapolate
private

Definition at line 72 of file DuneDPhase3x1x1NoiseRemovalService.h.

const geo::Geometry* DuneDPhase3x1x1NoiseRemovalService::fGeometry
private

Definition at line 98 of file DuneDPhase3x1x1NoiseRemovalService.h.

std::vector< float > DuneDPhase3x1x1NoiseRemovalService::fLowPassCoeffs
private

Definition at line 76 of file DuneDPhase3x1x1NoiseRemovalService.h.

bool DuneDPhase3x1x1NoiseRemovalService::fLowPassFlt
private

Definition at line 72 of file DuneDPhase3x1x1NoiseRemovalService.h.

float DuneDPhase3x1x1NoiseRemovalService::fLowPassFltExpo
private

Definition at line 73 of file DuneDPhase3x1x1NoiseRemovalService.h.

float DuneDPhase3x1x1NoiseRemovalService::fLowPassFltFCut
private

Definition at line 73 of file DuneDPhase3x1x1NoiseRemovalService.h.

bool DuneDPhase3x1x1NoiseRemovalService::fLowPassFltSecondPass
private

Definition at line 72 of file DuneDPhase3x1x1NoiseRemovalService.h.

int DuneDPhase3x1x1NoiseRemovalService::fMode
private

Definition at line 82 of file DuneDPhase3x1x1NoiseRemovalService.h.

float DuneDPhase3x1x1NoiseRemovalService::fRoiEndThreshold
private

Definition at line 79 of file DuneDPhase3x1x1NoiseRemovalService.h.

int DuneDPhase3x1x1NoiseRemovalService::fRoiPadHigh
private

Definition at line 81 of file DuneDPhase3x1x1NoiseRemovalService.h.

int DuneDPhase3x1x1NoiseRemovalService::fRoiPadLow
private

Definition at line 80 of file DuneDPhase3x1x1NoiseRemovalService.h.

float DuneDPhase3x1x1NoiseRemovalService::fRoiStartThreshold
private

Definition at line 78 of file DuneDPhase3x1x1NoiseRemovalService.h.

bool DuneDPhase3x1x1NoiseRemovalService::fUseBasicROIForCNR
private

Definition at line 77 of file DuneDPhase3x1x1NoiseRemovalService.h.

AdcChannelToolPtr DuneDPhase3x1x1NoiseRemovalService::m_pROIBuilderToolCNR
private

Definition at line 92 of file DuneDPhase3x1x1NoiseRemovalService.h.

AdcChannelToolPtr DuneDPhase3x1x1NoiseRemovalService::m_pROIBuilderToolFinal
private

Definition at line 95 of file DuneDPhase3x1x1NoiseRemovalService.h.

AdcChannelToolPtr DuneDPhase3x1x1NoiseRemovalService::m_pROIBuilderToolFlattening
private

Definition at line 89 of file DuneDPhase3x1x1NoiseRemovalService.h.

std::string DuneDPhase3x1x1NoiseRemovalService::m_ROIBuilderToolCNR
private

Definition at line 91 of file DuneDPhase3x1x1NoiseRemovalService.h.

std::string DuneDPhase3x1x1NoiseRemovalService::m_ROIBuilderToolFinal
private

Definition at line 94 of file DuneDPhase3x1x1NoiseRemovalService.h.

std::string DuneDPhase3x1x1NoiseRemovalService::m_ROIBuilderToolFlattening
private

Definition at line 88 of file DuneDPhase3x1x1NoiseRemovalService.h.


The documentation for this class was generated from the following files: