3#include "BmnADCDigit.h"
4#include "BmnScWallDigi.h"
5#include "BmnSyncDigit.h"
8#include "UniDetectorParameter.h"
27 const std::string& mappingFile,
28 const std::string& calibrationFile)
34 auto logIfError = [
this](
const char* what,
BmnStatus st) {
36 LOG(error) <<
GetName() <<
": " << what <<
" was not loaded, decoder may misbehave!";
39 LOG(info) <<
Form(
"%s : Config file: %s",
GetName().Data(), mappingFile.c_str());
43 if (!calibrationFile.empty()) {
44 LOG(info) <<
Form(
"%s : Hand-set Calibration file: %s",
GetName().Data(), calibrationFile.c_str());
49 logIfError(
"calibration", calibStatus);
54 std::string fullPath = std::string(getenv(
"VMCWORKDIR")) +
"/parameters/scwall/" + file;
55 std::ifstream config_file(fullPath);
56 if (!config_file.is_open()) {
57 LOG(error) <<
GetName() <<
"::ParseConfig: Cannot open config file: " << fullPath;
64 }
catch (
const std::exception& e) {
65 LOG(error) <<
GetName() <<
"::ParseConfig: Failed to parse JSON: " << e.what();
69 LOG(debug) <<
GetName() <<
"::ParseConfig. Version: " << j.value(
"version",
"unknown");
70 LOG(debug) <<
GetName() <<
"::ParseConfig. Comment: " << j.value(
"comment",
"none");
73 for (
const auto& entry : j[
"mapping"]) {
74 std::string serial = entry[
"serial"];
75 int channel = entry[
"channel"];
76 int cell = entry[
"cell"];
78 auto key = std::make_pair(std::stoul(serial,
nullptr, 16), channel);
88 bool gotFromDb =
false;
92 if (pParam !=
nullptr) {
94 if (pValue !=
nullptr) {
103 LOG(info) <<
GetName() <<
" : Loaded calibration from DB for period " << period <<
", run " << run;
107 const std::string fallbackFile =
Form(
"SCWALL_calibration_period%d.json", period);
109 LOG(warn) <<
GetName() <<
" : No calibration in DB for period " << period <<
", run " << run
110 <<
". Falling back to file " << fallbackFile;
118 std::string fullPath = std::string(getenv(
"VMCWORKDIR")) +
"/parameters/scwall/" + file;
119 std::ifstream config_file(fullPath);
120 if (!config_file.is_open()) {
121 LOG(error) <<
GetName() <<
"::ParseCalibration: Cannot open config file: " << fullPath;
128 }
catch (
const std::exception& e) {
129 LOG(error) <<
GetName() <<
"::ParseCalibration: Failed to parse JSON: " << e.what();
138 LOG(debug) <<
GetName() <<
"::ParseCalibrationJson. Version: " << j.value(
"version",
"unknown");
139 LOG(debug) <<
GetName() <<
"::ParseCalibrationJson. Comment: " << j.value(
"comment",
"none");
143 const auto& par = j.at(
"parameters");
145 fdigiPars.isWriteWfm = par.value(
"isWriteWfm",
false);
146 fdigiPars.gateBegin = par.at(
"gateBegin");
148 fdigiPars.threshold = par.at(
"threshold");
149 fdigiPars.doInvert = par.value(
"doInvert",
false);
152 for (
const auto& ch : j.at(
"calibration")) {
153 int cell = ch.at(
"cell");
154 float calib = ch.at(
"ampl");
159 if (j.contains(
"fitParameters")) {
160 const auto& fitpar = j.at(
"fitParameters");
161 fdigiPars.isfit = fitpar.value(
"isfit",
false);
163 for (
const auto& pair : fitpar.at(
"harmonics")) {
164 float re = pair.at(0).get<
float>();
165 float im = pair.at(1).get<
float>();
166 fdigiPars.harmonics.emplace_back(re, im);
170 int model_order =
fdigiPars.harmonics.size() + 1;
172 auto maxElement = std::max_element(
fdigiPars.harmonics.begin(),
fdigiPars.harmonics.end(),
173 [](
const std::complex<float>& a,
const std::complex<float>& b) {
174 return std::abs(a) < std::abs(b);
176 if (maxElement !=
fdigiPars.harmonics.end()) {
178 int maxElementLength =
floor(5. / (-
log(real(*maxElement))));
183 fAZik =
new std::complex<float>*[model_order];
184 for (
int i = 0;
i < model_order;
i++) {
185 fAZik[
i] =
new std::complex<float>[model_order];
186 for (
int k = 0; k < model_order; k++)
196 }
catch (
const std::exception& e) {
197 LOG(error) <<
GetName() <<
"::ParseCalibrationJson: error while parsing: " << e.what();
208 }
catch (
const std::out_of_range& e) {
209 LOG(debug) <<
"BmnScWallRaw2Digit:: GetAddressFromBoard. Board serial " << std::hex << key.first << std::dec
210 <<
" channel " << key.second <<
" is not connected. Skipping it";
219 }
catch (
const std::out_of_range& e) {
220 LOG(debug) <<
"BmnScWallRaw2Digit:: GetCalibPairFromAddress " << std::bitset<32>(address)
221 <<
" is marked badly in calibration file.";
228 LOG(debug) <<
"BmnScWallRaw2Digit::fillEvent";
236 if (!catch_address.has_value())
238 auto address = catch_address.value();
243 if (!catch_calib.has_value())
245 auto calib_pair = catch_calib.value();
254 LOG(debug4) <<
"BmnScWallRaw2Digit::ProcessWfm Calibration";
char * Form(const char *fmt,...)
BmnStatus
Define status code.
uint32_t GetSerial() const
vector< IntType > & GetValue()
uint16_t GetChannel() const
void SetAddress(uint32_t address)
void SetSignal(double signal)
int fIntegral
ZeroLevel from waveform [adc counts].
static uint32_t GetAddress(uint32_t CellId)
Return address from system ID, Cell ID.
Class for experimental data at digi level.
void reset() override final
BmnStatus ParseCalibration(const std::string &file)
std::optional< uint32_t > GetAddressFromBoard(std::pair< size_t, size_t > key)
std::optional< std::pair< float, float > > GetCalibPairFromAddress(uint32_t address)
BmnStatus ParseCalibrationJson(const nlohmann::json &j)
std::unordered_map< std::pair< size_t, size_t >, uint32_t, boost::hash< std::pair< size_t, size_t > > > fuoChannelMap
BmnStatus ParseConfig(const std::string &file)
BmnStatus LoadCalibration(int period, int run)
std::unordered_map< uint32_t, std::pair< float, float > > fuoCalibMap
void FillEvent(TClonesArray *data, TClonesArray *ScWalldigit)
void SetExternalHarmonics(std::vector< std::complex< float > > harmonics)
void Initialize(int model_order, int exponent_number, int gate_beg, int gate_end)
void MakeInvHarmoMatrix(int signal_length, std::complex< float > **output)
Int_t GetEntriesFast() const
TObject * At(Int_t idx) const override
void Start(Bool_t reset=kTRUE)
int GetValue(vector< UniValue * > ¶meter_value)
get value of detector parameter presented by an array
static UniDetectorParameter * GetDetectorParameter(int value_id)
get detector parameter from the database
std::complex< float > ** fAZik
struct WfmProcessor::digiPars fdigiPars
void ProcessWfm(std::vector< float > &wfm, BmnDigiContainerTemplate *digi)
RVec< PromoteType< T > > floor(const RVec< T > &v)
RVec< PromoteType< T > > log(const RVec< T > &v)
basic_json<> json
default specialization