23 m_pROIBuilderToolFlattening(nullptr),
24 m_pROIBuilderToolCNR(nullptr),
25 m_pROIBuilderToolFinal(nullptr) {
51 if ( ptm ==
nullptr ) {
52 std::cout <<
"ERROR: Unable to retrieve tool manaager." <<
std::endl;
62 std::cout <<
"DuneDPhase3x1x1NoiseRemovalService WARNING: correction set to mean value." <<
std::endl;
78 const std::string myname =
"DuneDPhase3x1x1NoiseRemovalService:update: ";
79 if ( datamap.size() == 0 ) {
80 std::cout << myname <<
"WARNING: No channels found." <<
std::endl;
84 unsigned int nsig = 0;
86 for (
const AdcChannelDataMap::value_type& ent : datamap)
89 if (first) { nsig = data.
samples.size(); first =
false; }
90 else if (data.
samples.size() != nsig)
92 std::cout << myname <<
"WARNING: Channels have inconsistent sample counts." <<
std::endl;
99 std::cout << myname <<
"WARNING: No ADC samples found." <<
std::endl;
123 for (
auto &
entry : tempdatamap)
125 auto & acd =
entry.second;
142 for (
auto &
entry : tempdatamap)
144 auto & acd =
entry.second;
150 for (
auto &
entry : datamap)
152 auto & acd =
entry.second;
153 auto acdtemp = ittempdatamap->second;
154 acd.signal = acdtemp.signal;
155 acd.rois = acdtemp.rois;
184 for (
auto &
entry : datamap)
186 auto & acd =
entry.second;
222 const std::vector<unsigned int> &
channels,
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);
229 for (
unsigned int ch : channels)
231 auto iacd = datamap.find(ch);
232 if (iacd == datamap.end())
continue;
239 for (
size_t s = 0;
s < n_samples; ++
s)
241 if (!
mask[
s]) {
continue; }
244 if (flag !=
AdcGood) {
continue; }
252 for (
size_t s = 0;
s < n_samples; ++
s)
254 if (adc.
signal[
s]) {
continue; }
257 if (flag !=
AdcGood) {
continue; }
264 for (
size_t s = 0;
s < n_samples; ++
s)
266 if (ch_averaged[
s] > 0) { correction[
s] /= ch_averaged[
s]; }
272 const std::vector<unsigned int> &
channels,
275 size_t n_samples = datamap.begin()->second.samples.size();
276 std::vector< std::vector<float> > samples(n_samples);
278 for (
unsigned int ch : channels)
280 auto iacd = datamap.find(ch);
281 if (iacd == datamap.end())
continue;
288 for (
size_t s = 0;
s < n_samples; ++
s)
290 if (!
mask[
s]) {
continue; }
293 if (flag !=
AdcGood) {
continue; }
295 samples[
s].push_back(adc.
samples[s]);
300 for (
size_t s = 0;
s < n_samples; ++
s)
302 if (adc.
signal[
s]) {
continue; }
305 if (flag !=
AdcGood) {
continue; }
314 std::vector<float> correction(n_samples);
315 for (
size_t s = 0;
s < n_samples; ++
s)
317 size_t n = samples[
s].size();
318 if (n < 2) { correction[
s] = 0;
continue; }
320 std::sort(samples[
s].
begin(), samples[
s].
end());
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]; }
330 if (datamap.empty())
return;
332 size_t n_samples = datamap.begin()->second.samples.size();
333 std::vector<double> correction(n_samples);
335 for (
const auto &
entry : ch_groups)
338 std::vector<float> correction;
351 auto iacd = datamap.find(ch);
352 if (iacd == datamap.end())
continue;
355 for (
size_t s = 0;
s < n_samples; ++
s)
374 for (
auto &
entry : datamap)
383 size_t n_samples = datamap.begin()->second.samples.size();
384 std::vector< float > slope(n_samples);
388 for (
auto &
entry : datamap)
396 float i = 0, s0 = 0, s1 = 0;
397 while (i < n_samples)
401 if (start) { s0 =
adc[i]; start =
false; }
402 else { s0 = 0.8 * s0 + 0.2 *
adc[i]; }
403 slope[i] =
adc[i]; ++i;
408 while ((j < n_samples) && !
mask[j]) { ++j; }
414 while ((k < 25) && (j+k < n_samples) &&
mask[j+k])
416 f *= 0.8;
g +=
f; s1 += f *
adc[j+
k]; ++
k;
422 float ds = (s1 - s0) / (j - i + 1);
425 while ((j < n_samples) && !
mask[j])
427 slope[j] = s0 + (j - i + 1) * ds;
435 double y, sx = 0, sy = 0, sxy = 0, sx2 = 0, sy2 = 0;
436 for (
size_t s = 0;
s < n_samples; ++
s)
438 y = slope[
s]; sx +=
s; sy +=
y; sxy +=
s*
y; sx2 +=
s*
s; sy2 += y*
y;
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);
447 for (
size_t s = 0;
s < n_samples; ++
s) {
adc[
s] -= (a*
s +
b); }
454 AdcIndex n_samples = datamap.begin()->second.samples.size();
459 for (
auto &
entry : datamap)
464 auto & signal =
entry.second.signal;
469 std::vector<long double>
line(fOrder+2,0.);
470 std::vector< std::vector< long double> > matrix(fOrder+1,line);
472 for(
int i = 0; i < fOrder+1; i++)
474 for(
int j = 0; j < fOrder+2; j++)
481 std::vector<double>
x,
y;
482 x.resize(n_samples, 0.);
483 y.resize(n_samples, 0.);
485 std::vector<double> sol;
486 sol.resize(fOrder+1, 0.);
509 double sx = 0., sy = 0., sxy = 0., sx2 = 0.;
512 while( aCount < 200 && a < n_samples )
516 sx +=
a; sy +=
adc[
a]; sxy += a*
adc[
a]; sx2 += a*
a;
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);
533 for(
AdcIndex a = ROIStart; a <= ROIEnd; a++)
542 if(signal[n_samples-1])
559 double sx = 0., sy = 0., sxy = 0., sx2 = 0.;
562 while( aCount < 200 && a >= fBinsToSkip)
566 sx +=
a; sy +=
adc[
a]; sxy += a*
adc[
a]; sx2 += a*
a;
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);
584 for(
AdcIndex a = ROIEnd; a >= ROIStart; a--)
607 for(
int l = 1;
l <= fOrder+1;
l++)
609 long double wt =
pow(x[i],
l-1);
612 for(
int m=1;
m <=
l;
m++)
614 matrix[j][
k] += wt*
pow(x[i],
m-1);
618 matrix[j][fOrder+1] += y[i]*wt;
625 for(
int i = 1; i < fOrder+1; i++){
626 for(
int j = 0; j<i; j++){
627 matrix[j][i] = matrix[i][j];
632 if(np > n_samples/10)
640 for(
size_t a = 0;
a < sol.size();
a++)
642 corr += sol[
a]*
pow(i,
a);
654 std::vector< TComplex > ch_spectrum(
fFFT->
FFTSize() / 2 + 1);
655 std::vector< float > ch_waveform(
fFFT->
FFTSize(), 0);
657 size_t n_samples = adc.size();
659 std::copy(adc.begin(), adc.end(), ch_waveform.begin());
660 for (
size_t s = n_samples;
s < ch_waveform.
size(); ++
s)
662 ch_waveform[
s] = ch_waveform[2*n_samples -
s - 1];
665 for (
size_t c = 0;
c < coeffs.size(); ++
c)
667 ch_spectrum[
c] *= coeffs[
c];
671 std::copy(ch_waveform.begin(), ch_waveform.begin()+n_samples, adc.begin());
677 std::vector< TComplex > ch_spectrum(
fFFT->
FFTSize() / 2 + 1);
678 std::vector< float > ch_waveform(
fFFT->
FFTSize(), 0);
680 size_t n_samples = adc.size();
682 std::copy(adc.begin(), adc.end(), ch_waveform.begin());
683 for (
size_t s = n_samples;
s < ch_waveform.
size(); ++
s)
685 ch_waveform[
s] = ch_waveform[2*n_samples -
s - 1];
688 for (
size_t c = 0;
c < coeffs.size(); ++
c)
690 ch_spectrum[
c] *= coeffs[
c];
694 std::vector< float > flt_adc(n_samples);
695 std::copy(ch_waveform.begin(), ch_waveform.begin()+n_samples, flt_adc.begin());
707 for (
int i = 0; i < (
int)adc_flt.size(); ++i)
709 auto sig = adc_flt[i];
739 for (
unsigned int ch = 0; ch < nchan; ++ch)
743 if (gidx.empty() ||
has(gidx, g))
745 if (
chStatus.IsPresent(ch) && !
chStatus.IsNoisy(ch)) { groups[
g].push_back(ch); }
760 for (
unsigned int ch = 0; ch < nchan; ++ch)
762 size_t g = ch / gsize;
764 if (gidx.empty() ||
has(gidx, g))
766 if (
chStatus.IsPresent(ch) && !
chStatus.IsNoisy(ch)) { groups[
g].push_back(ch); }
776 size_t crate = LAr_chan / 320;
779 LAr_chan = 8*(LAr_chan/8+1)-LAr_chan%8 -1;
783 LAr_chan = 32*(LAr_chan/32+1)-LAr_chan%32 -1;
784 size_t card = 4 - ((LAr_chan / 32) % 5);
787 size_t shift = 31 - (LAr_chan % 32);
788 Chan311 = (2*card)*32 + shift;
792 size_t shift = 31 - (LAr_chan % 32);
793 Chan311 = (2*card + 1)*32 + shift;
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)
802 size_t shift = new_LAr_chan % 32;
803 Chan311 = (2*card)*32 + shift;
807 size_t shift = new_LAr_chan % 32;
808 Chan311 = (2*card + 1)*32 + shift;
810 Chan311 = Chan311 + crate*320;
820 int n = matrix.size();
822 for (
int i=0; i<
n; i++) {
824 double maxEl =
std::abs(matrix[i][i]);
826 for (
int k=i+1;
k<
n;
k++) {
834 for (
int k=i;
k<n+1;
k++) {
835 double tmp = matrix[maxRow][
k];
836 matrix[maxRow][
k] = matrix[i][
k];
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++) {
847 matrix[
k][j] += c * matrix[i][j];
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];
866 out << prefix <<
"DuneDPhase3x1x1NoiseRemovalService: ...info" <<
std::endl;
std::string m_ROIBuilderToolFinal
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
AdcChannelToolPtr m_pROIBuilderToolCNR
static constexpr double g
int update(AdcChannelDataMap &datamap) const
static size_t get311Chan(size_t LAr_chan)
Get 3x1x1 DAQ channel number from the LArSoft's channel index.
void DoFFT(std::vector< T > &input, std::vector< TComplex > &output)
void removeCoherent(const GroupChannelMap &ch_groups, AdcChannelDataMap &datamap) const
std::vector< float > getMedianCorrection(const std::vector< unsigned int > &channels, const AdcChannelDataMap &datamap) const
art framework interface to geometry description
std::vector< size_t > fCoherent16Groups
void DoInvFFT(std::vector< TComplex > &input, std::vector< T > &output)
unsigned int Nchannels() const
Returns the number of TPC readout channels in the detector.
void fftFltInPlace(std::vector< float > &adc, const std::vector< float > &coeffs) const
QCollection::Item first()
AdcChannelToolPtr m_pROIBuilderToolFlattening
struct dune::tde::crate crate
T get(std::string const &key) const
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...
std::vector< double > GaussJordanSolv(std::vector< std::vector< long double > > matrix) const
const geo::Geometry * fGeometry
std::vector< float > getMeanCorrection(const std::vector< unsigned int > &channels, const AdcChannelDataMap &datamap) const
void removeHighFreq(AdcChannelDataMap &datamap) const
void removeSlope(AdcChannelDataMap &datamap) const
std::unordered_map< unsigned int, std::vector< unsigned int > > GroupChannelMap
DuneDPhase3x1x1NoiseRemovalService(fhicl::ParameterSet const &pset, art::ActivityRegistry &)
std::optional< T > get_if_present(std::string const &key) const
void line(double t, double *p, double &x, double &y, double &z)
std::string m_ROIBuilderToolCNR
std::vector< float > fftFlt(const std::vector< float > &adc, const std::vector< float > &coeffs) const
bool has(const std::vector< size_t > &v, size_t idx) const
Interface for experiment-specific channel quality info provider.
std::map< AdcChannel, AdcChannelData > AdcChannelDataMap
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
std::vector< size_t > fCoherent32Groups
std::vector< bool > roiMask(const AdcChannelData &adc) const
std::string m_ROIBuilderToolFlattening
Interface for experiment-specific service for channel quality info.
void removeSlopePolynomial(AdcChannelDataMap &datamap) const
std::ostream & print(std::ostream &out=std::cout, std::string prefix="") 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...
QTextStream & endl(QTextStream &s)
AdcChannelToolPtr m_pROIBuilderToolFinal
std::vector< float > fLowPassCoeffs
bool fLowPassFltSecondPass
#define DEFINE_ART_SERVICE_INTERFACE_IMPL(svc, iface)