Skip to content

Commit

Permalink
Fix #202 (again)
Browse files Browse the repository at this point in the history
This revert the use of round() in Digitizer but adds a configuration option of the same name to allow the user to reenable, if they desire.  See #202 for examples of the implications of either choice.

The bug that orginally launched #202 is still fixed but, for now, AddNoise gains a configuration parameter to allow the user to activate this bug and at any desired level of damage.  Of course, default is no bug.  This is added for testing purposes and may be removed in the near future.

Change the "roundtrip" test to utilize all 10 levels of noise, each level across a contigous group of 256 channels.

The bats test is greatly expanded.  It currently requires the "bug202" option and an updated "wirecell-plot comp1d" which is to be committed shortly.
  • Loading branch information
brettviren committed Mar 25, 2023
1 parent 53003b3 commit 56aae07
Show file tree
Hide file tree
Showing 10 changed files with 337 additions and 84 deletions.
1 change: 1 addition & 0 deletions gen/inc/WireCellGen/Digitizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ namespace WireCell {
std::vector<double> m_fullscale, m_baselines;
std::string m_frame_tag;
size_t m_count{0};
bool m_round{false}; // apply round to FP ADC values
};

} // namespace Gen
Expand Down
1 change: 1 addition & 0 deletions gen/inc/WireCellGen/Noise.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ namespace WireCell::Gen {
double m_rep_percent{0.02};
size_t m_count{0};

double m_bug202{0.0}; // non-zero emulates issue #202
};

/// Incoherent and coherent noise each have their own type of
Expand Down
6 changes: 4 additions & 2 deletions gen/src/AddNoise.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ bool Gen::IncoherentAddNoise::operator()(const input_pointer& inframe, output_po
// array will start from a random location in the recycled buffer
// and a few percent will be "freshened". This results in a small
// amount of coherency between nearby channels.
auto rn = Normals::make_recycling(m_rng, 2*m_nsamples, 0, 1, 2*m_rep_percent);
const double BUG=m_bug202; // 0.0 is correct, passing rec. perc. 0.04 opened issue 202
auto rn = Normals::make_recycling(m_rng, 2*m_nsamples, BUG, 1, 2*m_rep_percent);
GeneratorN rwgen(m_dft, rn);

// Limit number of warnings below
Expand Down Expand Up @@ -121,7 +122,8 @@ bool Gen::CoherentAddNoise::operator()(const input_pointer& inframe, output_poin
// array will start from a random location in the recycled buffer
// and a few percent will be "freshened". This results in a small
// amount of coherency between nearby channels.
auto rn = Normals::make_recycling(m_rng, 2*m_nsamples, 0, 1, 2*m_rep_percent);
const double BUG=m_bug202; // 0.0 is correct, passing rec. perc. 0.04 opened issue 202
auto rn = Normals::make_recycling(m_rng, 2*m_nsamples, BUG, 1, 2*m_rep_percent);
GeneratorN rwgen(m_dft, rn);

// Look up the generated wave for a group.
Expand Down
11 changes: 9 additions & 2 deletions gen/src/Digitizer.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ WireCell::Configuration Gen::Digitizer::default_configuration() const
bl[ind] = m_baselines[ind];
}
cfg["baselines"] = bl;
cfg["round"] = m_round;

cfg["frame_tag"] = m_frame_tag;
return cfg;
Expand All @@ -67,6 +68,7 @@ void Gen::Digitizer::configure(const Configuration& cfg)
m_gain = get(cfg, "gain", m_gain);
m_fullscale = get(cfg, "fullscale", m_fullscale);
m_baselines = get(cfg, "baselines", m_baselines);
m_round = get(cfg, "round", m_round);
m_frame_tag = get(cfg, "frame_tag", m_frame_tag);

std::stringstream ss;
Expand All @@ -76,7 +78,8 @@ void Gen::Digitizer::configure(const Configuration& cfg)
<< "gain=" << m_gain << ", "
<< "fullscale=[" << m_fullscale[0] / units::mV << "," << m_fullscale[1] / units::mV << "] mV, "
<< "baselines=[" << m_baselines[0] / units::mV << "," << m_baselines[1] / units::mV << ","
<< m_baselines[2] / units::mV << "] mV";
<< m_baselines[2] / units::mV << "] mV "
<< "round=" << m_round;
log->debug(ss.str());
}

Expand All @@ -91,7 +94,11 @@ double Gen::Digitizer::digitize(double voltage)
return adcmaxval;
}
const double relvoltage = (voltage - m_fullscale[0]) / (m_fullscale[1] - m_fullscale[0]);
return round(relvoltage * adcmaxval);
const double fp_adc = relvoltage * adcmaxval;
if (m_round) {
return round(fp_adc);
}
return fp_adc;
}

bool Gen::Digitizer::operator()(const input_pointer& vframe, output_pointer& adcframe)
Expand Down
4 changes: 4 additions & 0 deletions gen/src/Noise.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,9 @@ void Gen::NoiseBase::configure(const WireCell::Configuration& cfg)

m_nsamples = get<int>(cfg, "nsamples", m_nsamples);
m_rep_percent = get<double>(cfg, "replacement_percentage", m_rep_percent);
m_bug202 = get<double>(cfg, "bug202", 0.0);
if (m_bug202>0) {
log->warn("BUG 202 IS ACTIVATED: {}", m_bug202);
}
}

21 changes: 21 additions & 0 deletions gen/test/check_noise_roundtrip.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,27 @@ def image(array, channels, cmap, format, output, aname, fname):



@cli.command("configured-spectra")
@click.option("-t","--type", default="GroupNoiseModel", help="component type name")
@click.option("-n","--name", default="inco", help="component instance name")
@click.argument("cfgfile")
@click.argument("output")
def configured_spectra(type, name, cfgfile, output):
from wirecell.util import jsio
from wirecell.sigproc.noise.plots import plot_many

got = None
for one in jsio.load(cfgfile):
if one['type'] == type and one['name'] == name:
got=one['data']['spectra']
break
if got is None:
raise click.BadParameter(f'failed to find node {type}:{name}')

zero_suppress = True
plot_many(got, output, zero_suppress)


def main():
cli(obj=dict())

Expand Down
33 changes: 16 additions & 17 deletions gen/test/test-noise-groups-incoherent.jsonnet
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
// Make an incoherent GroupNoiseModel "map file".
//
// Here we assume the test-noise-spectra.jsonnet with 10 groups and
// PDSP APA0 channel numbering.
// Here we assume the test-noise-spectra.jsonnet with 10 groups
// (numbered 1 to 10) and PDSP APA0 channel numbering. The particualr
// noise grouping is totally bogus for the real PDSP detector.
//
// We make each plane channel range use a different spectra.
// We make each "plane" channel range use a different spectra.
//
// Note at the lowest noise, quantization error dominates and the
// modeled noise adding a relatively large component. In this test,
// starting at about group 3, the modeled spectra are comparable to
// the input spectra in the peak but the modeled noise spectra will
// still have a fatter tail.
local step=256;
[
// Note the "plane" attribute is not standard and shoud/must be
// ignored.
{
plane: "u",
groupID: 9, // noisiest
channels: std.range(0, 800-1),
},
{
plane: "v",
groupID: 5,
channels: std.range(800, 800 + 800-1),
},
{
plane: "w",
groupID: 1, // quietest
channels: std.range(800 + 800, 800 + 800 + 960-1),
plane: "p%d"%n,
groupID: n,
channels: std.range(step*(n-1), (step*n)-1),
},
]
for n in std.range(1,10)
]
Loading

0 comments on commit 56aae07

Please sign in to comment.