Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 37 additions & 5 deletions app/tool/algorithm/get_toa_efficiencies.cxx
Original file line number Diff line number Diff line change
@@ -1,27 +1,59 @@
#include "get_toa_efficiencies.h"

#include "pflib/logging/Logging.h"
#include "pflib/utility/efficiency.h"

namespace pflib::algorithm {

std::array<double, 72> get_toa_efficiencies(

int i_roc, const pflib::packing::SingleECONDRocErxMapping& mapping,
const std::vector<pflib::packing::MultiSampleECONDEventPacket>& data) {
static auto the_log_{::pflib::logging::get("get_toa_efficiencies")};
std::array<double, 72> efficiencies;

/// reserve a vector of the appropriate size to avoid repeating allocation
/// time for all 72 channels

if (data.empty()) {
pflib_log(warn) << "[DEBUG TOA] data packet vector is EMPTY for ROC "
<< i_roc;
efficiencies.fill(0.0);
return efficiencies;
}

std::vector<int> toas(data.size());
for (int ch{0}; ch < 72; ch++) {
// TODO: 348
int i_link = (ch / 36);
int i_ch = ch % 36;
auto [i_erx, i_ch] = mapping.toErxChannel(i_roc, ch);
if (i_erx < 0 || i_ch < 0) {
pflib_log(error)
<< "[DEBUG MAP] Sanity Failure: Negative mapping values detected "
<< "ROC " << i_roc << " Ch " << ch
<< " resolved to eRx = " << (int)i_erx << ", eCh = " << (int)i_ch;
}

for (std::size_t i{0}; i < toas.size(); i++) {
toas[i] = data[i].samples[data[i].i_soi].channel(i_link, i_ch).toa();
toas[i] = data[i].soi().channel(i_erx, i_ch).toa();
}
/// we assume that the data provided is not empty otherwise the efficiency
/// calculation is meaningless

if (ch == 0 || ch == 36) {
pflib_log(trace) << "[DEBUG TOA] ROC " << i_roc << " Ch " << ch
<< " (eRx: " << (int)i_erx << ", eCh: " << (int)i_ch
<< ")"
<< " first 3 raw TOAs: [" << toas[0] << ", "
<< (toas.size() > 1 ? std::to_string(toas[1]) : "N/A")
<< ", "
<< (toas.size() > 2 ? std::to_string(toas[2]) : "N/A")
<< "]";
}
efficiencies[ch] = pflib::utility::efficiency(toas);
if (efficiencies[ch] > 0.0) {
pflib_log(debug) << "[DEBUG TOA] Found non-zero efficiency on ROC "
<< i_roc << " Ch " << ch << " = " << efficiencies[ch];
}
}
return efficiencies;
}

} // namespace pflib::algorithm
1 change: 1 addition & 0 deletions app/tool/algorithm/get_toa_efficiencies.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace pflib::algorithm {

// templated to match any event packet type
std::array<double, 72> get_toa_efficiencies(
int i_roc, const pflib::packing::SingleECONDRocErxMapping& mapping,
const std::vector<pflib::packing::MultiSampleECONDEventPacket>& data);

} // namespace pflib::algorithm
143 changes: 107 additions & 36 deletions app/tool/algorithm/toa_vref_scan.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
#include "get_toa_efficiencies.h"
#include "pflib/utility/efficiency.h"
#include "pflib/utility/string_format.h"

namespace pflib::algorithm {

std::map<std::string, std::map<std::string, uint64_t>> toa_vref_scan(
Target* tgt, ROC roc) {
std::map<int, std::map<std::string, std::map<std::string, uint64_t>>>
toa_vref_scan(Target* tgt) {
static auto the_log_{::pflib::logging::get("toa_vref_scan")};

/// do a run of 100 samples per toa_vref to measure the TOA
Expand All @@ -19,58 +18,130 @@ std::map<std::string, std::map<std::string, uint64_t>> toa_vref_scan(

tgt->setup_run(1, Target::DaqFormat::ECOND_SW_HEADERS, 1);

std::array<int, 2> target;
std::array<std::array<double, 256>, 2> final_effs;
std::map<int, std::array<int, 2>> target; // i_roc int for each variable
std::map<int, std::array<std::array<double, 256>, 2>> final_effs;
std::map<int, std::map<std::string, std::map<std::string, uint64_t>>>
settings;

// TODO 348
DecodeAndBuffer buffer{n_events, 2};

DecodeAndBuffer buffer{n_events, tgt->nrocs() * 2};

// loop over runs, from toa_vref = 0 to = 255

for (int toa_vref{0}; toa_vref < 256; toa_vref++) {
pflib_log(info) << "testing toa_vref = " << toa_vref;
auto test_handle = roc.testParameters()
.add("REFERENCEVOLTAGE_0", "TOA_VREF", toa_vref)
.add("REFERENCEVOLTAGE_1", "TOA_VREF", toa_vref)
.apply();

std::map<std::string, std::map<std::string, uint64_t>> parameters;
parameters["REFERENCEVOLTAGE_0"]["TOA_VREF"] = toa_vref;
parameters["REFERENCEVOLTAGE_1"]["TOA_VREF"] = toa_vref;
auto test_params = tgt->tempApplyAllROCs(parameters);

usleep(10);

daq_run(tgt, "PEDESTAL", buffer, n_events, 100);
pflib_log(trace) << "finished toa_vref = " << toa_vref
<< ", getting efficiencies";
auto efficiencies = get_toa_efficiencies(buffer.get_buffer());
pflib_log(trace)
<< "got channel efficiencies, getting max efficiency per link";
for (int i_link{0}; i_link < 2; i_link++) {
auto start = efficiencies.begin() +
36 * i_link; // start at 0 for link 0, 36 for link 1
auto end = start + 36;
final_effs[i_link][toa_vref] = *std::max_element(start, end);

for (int i_roc : tgt->roc_ids()) {
// Debug callout for mapping
const auto& mapping = tgt->getRocErxMapping();
try {
auto [first_erx, first_ch] = mapping.toErxChannel(i_roc, 0);
auto [last_erx, last_ch] = mapping.toErxChannel(i_roc, 71);

if (toa_vref == 0) {
pflib_log(info) << "[DEBUG MAP] Mapping Check for ROC " << i_roc
<< ":"
<< " Ch 0 -> eRx " << (int)first_erx << ", eCh "
<< (int)first_ch << " | Ch 71 -> eRx "
<< (int)last_erx << ", eCh " << (int)last_ch;
}
}

catch (const std::exception& e) {
pflib_log(error) << "[DEBUG MAP] Critical: Mapping lookup threw an "
"exception for ROC "
<< i_roc << ". Message: " << e.what();
}

auto efficiencies =
get_toa_efficiencies(i_roc, mapping, buffer.get_buffer());

pflib_log(trace) << "got channel efficiencies for ROC " << i_roc
<< ", getting max efficiency per link";

for (int i_link{0}; i_link < 2; i_link++) {
auto start = efficiencies.begin() + 36 * i_link;
auto end = start + 36;
double max_eff = *std::max_element(start, end);

final_effs[i_roc][i_link][toa_vref] = max_eff;

if (toa_vref % 32 == 0 || max_eff > 0.0) {
pflib_log(trace) << "[DEBUG SCAN] VREF " << toa_vref << " | ROC "
<< i_roc << " Link " << i_link
<< " | Max Efficiency: " << max_eff;
}
}

pflib_log(trace) << "got link efficiencies";
}
pflib_log(trace) << "got link efficiencies";
}

pflib_log(info) << "sample collections done, deducing settings";
// get the max toa_vref with non-zero efficiency? Iterate through the array
// from bottom up.
for (int i_link{0}; i_link < 2; i_link++) {
int highest_non_zero_eff = -1; // just a placeholder in case it's not found
for (int toa_vref = final_effs[0].size() - 1; toa_vref >= 0; toa_vref--) {
if (final_effs[i_link][toa_vref] > 0.0) {
highest_non_zero_eff =
toa_vref + 10; // need to add 10 since we don't want to overlap
// with highest pedestals!
break; // should break from link 0 into link 1

for (int i_roc : tgt->roc_ids()) {
for (int i_link{0}; i_link < 2; i_link++) {
int highest_non_zero_eff =
-1; // just a placeholder in case it's not found

for (int toa_vref = final_effs[i_roc][i_link].size() - 1; toa_vref >= 0;
toa_vref--) {
if (toa_vref == (int)final_effs[i_roc][i_link].size() - 1) {
pflib_log(trace)
<< "[DEBUG SEARCH] Starting backwards search for ROC " << i_roc
<< " Link " << i_link << ". Initial val at max VREF: "
<< final_effs[i_roc][i_link][toa_vref];
}

if (final_effs[i_roc][i_link][toa_vref] > 0.0) {
highest_non_zero_eff =
toa_vref + 10; // need to add 10 since we don't want to overlap
// with highest pedestals!
pflib_log(info)
<< "[DEBUG SEARCH] Success. Found threshold edge at VREF "
<< toa_vref << " (Setting target to " << highest_non_zero_eff
<< ")";
break; // should break from link 0 into link 1
}
}

if (highest_non_zero_eff < 0) {
pflib_log(warn) << "ROC " << i_roc << " link " << i_link
<< ": no non-zero TOA efficiency found, skipping";
continue;
}

if (highest_non_zero_eff > 255) {
pflib_log(warn) << "ROC " << i_roc << " link " << i_link
<< ": deduced TOA_VREF " << highest_non_zero_eff
<< " out of range, clamping to 255";
highest_non_zero_eff = 255;
}
target[i_roc][i_link] = highest_non_zero_eff; // store value
}
target[i_link] = highest_non_zero_eff; // store value
}
// toa_vref is a global parameter (1 value per link)

std::map<std::string, std::map<std::string, uint64_t>> settings;
for (int i_link{0}; i_link < 2; i_link++) {
std::string page{
pflib::utility::string_format("REFERENCEVOLTAGE_%d", i_link)};
settings[page]["TOA_VREF"] = target[i_link];
}
// toa_vref is a global parameter (1 value per link)

for (int i_link{0}; i_link < 2; i_link++) {
std::string page{
pflib::utility::string_format("REFERENCEVOLTAGE_%d", i_link)};
settings[i_roc][page]["TOA_VREF"] = target[i_roc][i_link];
}
}
return settings;
}

Expand Down
4 changes: 2 additions & 2 deletions app/tool/algorithm/toa_vref_scan.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace pflib::algorithm {
*
* @note Only functional for single-ROC targets
*/
std::map<std::string, std::map<std::string, uint64_t>> toa_vref_scan(
Target* tgt, ROC roc);
std::map<int, std::map<std::string, std::map<std::string, uint64_t>>>
toa_vref_scan(Target* tgt);

} // namespace pflib::algorithm
Loading