10 #include <ecl/utility/ECLDspUtilities.h>
11 #include <ecl/utility/ECLChannelMapper.h>
12 #include <ecl/dbobjects/ECLDspData.h>
13 #include <ecl/dbobjects/ECLCrystalCalib.h>
14 #include <ecl/dataobjects/ECLDigit.h>
16 #include <framework/logging/Logger.h>
17 #include <framework/database/DBObjPtr.h>
18 #include <framework/database/DBArray.h>
19 #include <framework/utilities/FileSystem.h>
28 const short ECLDSP_FORMAT_VERSION = 1;
38 void readEclDspCoefs(FILE* fl,
void* ptr,
int word_size,
int word_count)
40 int read_items = fread(ptr, word_size, word_count, fl);
41 if (read_items != word_count) {
42 B2ERROR(
"Error reading DSP coefficients");
49 fl = fopen(filename,
"rb");
51 B2ERROR(
"Can't open file " << filename);
57 int crateNum = (boardNumber - 1) / 12 + 1;
58 int shaperNum = (boardNumber - 1) % 12;
65 if (shaperNum >= 10) {
66 if (crateNum >= 37 && crateNum <= 44) {
78 int size = fread(
id, nsiz, nsiz1, fl);
80 B2ERROR(
"Error reading header of DSP file");
83 std::vector<short int> extraData;
84 extraData.push_back(ECLDSP_FORMAT_VERSION);
85 extraData.push_back(data->getPackerVersion());
86 data->setExtraData(extraData);
88 data->setverMaj(
id[8] & 0xFF);
89 data->setverMin(
id[8] >> 8);
90 data->setkb(
id[13] >> 8);
91 data->setka(
id[13] - 256 * data->getkb());
92 data->sety0Startr(
id[14] >> 8);
93 data->setkc(
id[14] - 256 * data->gety0Startr());
94 data->setchiThresh(
id[15]);
95 data->setk2(
id[16] >> 8);
96 data->setk1(
id[16] - 256 * data->getk2());
98 data->setlAT(
id[128]);
100 data->setaAT(
id[208]);
102 std::vector<short int> f(49152), f1(49152), f31(49152),
103 f32(49152), f33(49152), f41(6144), f43(6144);
105 for (
int i = 0; i < 16; ++i) {
107 readEclDspCoefs(fl, &(*(f41.begin() + i * nsiz1)), nsiz, nsiz1);
109 readEclDspCoefs(fl, &(*(f31.begin() + i * nsiz1)), nsiz, nsiz1);
110 readEclDspCoefs(fl, &(*(f32.begin() + i * nsiz1)), nsiz, nsiz1);
111 readEclDspCoefs(fl, &(*(f33.begin() + i * nsiz1)), nsiz, nsiz1);
113 readEclDspCoefs(fl, &(*(f43.begin() + i * nsiz1)), nsiz, nsiz1);
115 readEclDspCoefs(fl, &(*(f.begin() + i * nsiz1)), nsiz, nsiz1);
116 readEclDspCoefs(fl, &(*(f1.begin() + i * nsiz1)), nsiz, nsiz1);
135 const unsigned short DEFAULT_HEADER[256] {
137 0x4543, 0x4c44, 0x5350, 0x2046, 0x494c, 0x4500, 0x0000, 0x0000,
138 0xABCD, 0xffff, 0x0000, 0x0000, 0x0000, 0xABCD, 0xABCD, 0xABCD,
139 0xABCD, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
140 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
141 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
142 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
143 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
144 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
145 0xABCD, 0xABCD, 0xABCD, 0xABCD, 0xABCD, 0xABCD, 0xABCD, 0xABCD,
146 0xABCD, 0xABCD, 0xABCD, 0xABCD, 0xABCD, 0xABCD, 0xABCD, 0xABCD,
147 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
148 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
149 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
150 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
151 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
152 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
153 0xABCD, 0xABCD, 0xABCD, 0xABCD, 0xABCD, 0xABCD, 0xABCD, 0xABCD,
154 0xABCD, 0xABCD, 0xABCD, 0xABCD, 0xABCD, 0xABCD, 0xABCD, 0xABCD,
155 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
156 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
157 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
158 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
159 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
160 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
161 0xABCD, 0xABCD, 0xABCD, 0xABCD, 0xABCD, 0xABCD, 0xABCD, 0xABCD,
162 0xABCD, 0xABCD, 0xABCD, 0xABCD, 0xABCD, 0xABCD, 0xABCD, 0xABCD,
163 0xABCD, 0xABCD, 0xABCD, 0xABCD, 0xABCD, 0xABCD, 0xABCD, 0xABCD,
164 0xABCD, 0xABCD, 0xABCD, 0xABCD, 0xABCD, 0xABCD, 0xABCD, 0xABCD,
165 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
166 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
167 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
168 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
172 fl = fopen(filename,
"wb");
178 unsigned short header[256];
180 int format_version = data->getExtraData()[0];
181 if (format_version > ECLDSP_FORMAT_VERSION) {
182 B2WARNING(
"Format version " << format_version <<
" is not fully supported, some data might be discarded.");
185 for (
int i = 0; i < 256; i++) {
187 header[i] = (data->getverMin() << 8) | data->getverMaj();
188 }
else if (i == 13) {
189 header[i] = (data->getkb() << 8) | data->getka();
190 }
else if (i == 14) {
191 header[i] = (data->gety0Startr() << 8) | data->getkc();
192 }
else if (i == 15) {
193 header[i] = data->getchiThresh();
194 }
else if (i == 16) {
195 header[i] = (data->getk2() << 8) | data->getk1();
196 }
else if (i >= 64 && i < 80) {
197 header[i] = data->gethT();
198 }
else if (i >= 128 && i < 144) {
199 header[i] = data->getlAT();
200 }
else if (i >= 192 && i < 208) {
201 header[i] = data->getsT();
202 }
else if (i >= 208 && i < 224) {
203 header[i] = data->getaAT();
206 int high = (DEFAULT_HEADER[i] & 0xFF00) >> 8;
207 int low = (DEFAULT_HEADER[i] & 0x00FF);
208 header[i] = (low << 8) + high;
212 int size = fwrite(header, nsiz, nsiz1, fl);
214 B2FATAL(
"Error writing header of DSP file " << filename);
217 std::vector<short int> f(49152), f1(49152), f31(49152),
218 f32(49152), f33(49152), f41(6144), f43(6144);
227 for (
int i = 0; i < 16; ++i) {
229 fwrite(&(*(f41.begin() + i * nsiz1)), nsiz, nsiz1, fl);
231 fwrite(&(*(f31.begin() + i * nsiz1)), nsiz, nsiz1, fl);
232 fwrite(&(*(f32.begin() + i * nsiz1)), nsiz, nsiz1, fl);
233 fwrite(&(*(f33.begin() + i * nsiz1)), nsiz, nsiz1, fl);
235 fwrite(&(*(f43.begin() + i * nsiz1)), nsiz, nsiz1, fl);
237 fwrite(&(*(f.begin() + i * nsiz1)), nsiz, nsiz1, fl);
238 fwrite(&(*(f1.begin() + i * nsiz1)), nsiz, nsiz1, fl);
246 short int* vectorsplit(std::vector<short int>& vectorFrom,
int channel)
248 size_t size = vectorFrom.size();
249 if (size % 16) B2ERROR(
"Split is impossible!" <<
"Vector size" << size);
250 return (vectorFrom.data() + (size / 16) * (channel - 1));
254 bool adjusted_timing)
267 int dsp_group = (crate - 1) / 18;
268 int dsp_id = (crate - 1) % 18;
269 std::string payload_name =
"ECLDSPPars" + std::to_string(dsp_group);
271 const ECLDspData* data = dsp_data[dsp_id * 12 + shaper - 1];
273 std::vector<short> vec_f; data->getF(vec_f);
274 std::vector<short> vec_f1; data->getF1(vec_f1);
275 std::vector<short> vec_fg31; data->getF31(vec_fg31);
276 std::vector<short> vec_fg32; data->getF32(vec_fg32);
277 std::vector<short> vec_fg33; data->getF33(vec_fg33);
278 std::vector<short> vec_fg41; data->getF41(vec_fg41);
279 std::vector<short> vec_fg43; data->getF43(vec_fg43);
281 short* f = vectorsplit(vec_f, channel);
282 short* f1 = vectorsplit(vec_f1, channel);
283 short* fg31 = vectorsplit(vec_fg31, channel);
284 short* fg32 = vectorsplit(vec_fg32, channel);
285 short* fg33 = vectorsplit(vec_fg33, channel);
286 short* fg41 = vectorsplit(vec_fg41, channel);
287 short* fg43 = vectorsplit(vec_fg43, channel);
289 int k_a = data->getka();
290 int k_b = data->getkb();
291 int k_c = data->getkc();
292 int k_1 = data->getk1();
293 int k_2 = data->getk2();
294 int k_16 = data->gety0Startr();
295 int chi_thres = data->getchiThresh();
301 int ttrig2 = ttrig - 2 * (ttrig / 8);
308 int A0 = thr_LowAmp->getCalibVector()[cid - 1];
309 int Ahard = thr_HitThresh->getCalibVector()[cid - 1];
310 int Askip = thr_StoreDigit->getCalibVector()[cid - 1];
313 auto result = lftda_(f, f1, fg41, fg43, fg31, fg32, fg33, y, ttrig2, A0,
314 Ahard, Askip, k_a, k_b, k_c, k_16, k_1, k_2,
315 chi_thres, adjusted_timing);
323 TFile* file =
new TFile(path.c_str(),
"read");
324 if (!file->IsOpen()) {
325 B2FATAL(
"Unable to load coefficients for ECL pedestal fit");
327 TTree* tree = (TTree*)file->Get(
"dsp_coefs");
328 int nentries = tree->GetEntries();
329 float fg31_i, fg32_i;
330 tree->SetBranchAddress(
"fg31", &fg31_i);
331 tree->SetBranchAddress(
"fg32", &fg32_i);
333 for (
int i = 0; i < nentries; i++) {
356 for (
int i = 0; i < 16; i++) {
357 if (adc[i] > ped_max) {
364 if (ped_max_pos < 2 || ped_max_pos > 13) {
371 int time_index = ped_max_pos * 4 - 8;
374 for (
int iter = 0; iter < 2; iter++) {
377 for (
int j = 0; j < 16; j++) {
382 time_index -= tim * 4;
383 if (time_index > 47) time_index = 47;
384 if (time_index < 0) time_index = 0;
Class for accessing arrays of objects in the database.
Class for accessing objects in the database.
This object contains ECL DSP coefs – electromagnetic calorimeter digital signal processing coefficien...
This class provides access to ECL channel map that is either a) Loaded from the database (see ecl/dbo...
bool initFromDB()
Initialize channel mapper from the conditions database.
int getShaperChannel(int cellID)
Get number of DSP channel in the shaper by given number of CellId.
int getShaperPosition(int cellID)
Get position of the shaper in the crate by given CellId.
int getCrateID(int iCOPPERNode, int iFINESSE, bool pcie40=false)
Get crate number by given COPPER node number and FINESSE number.
static ECLDspData * readEclDsp(const char *raw_file, int boardNumber)
Convert ECLDspData from *.dat file to Root object.
static ECLShapeFit shapeFitter(int cid, std::vector< int > adc, int ttrig, bool adjusted_timing=true)
Emulate shape fitting algorithm from ShaperDSP using algorithm from ecl/utility/src/ECLDspEmulator....
static int pedestal_fit_initialized
Flag indicating whether arrays fg31,fg32 are filled.
static ECLPedestalFit pedestalFit(std::vector< int > adc)
Fit pedestal part of the signal waveform (first 16 samples) This method will fit the first 16 samples...
static void writeEclDsp(const char *raw_file, ECLDspData *obj)
Convert ECLDspData from Root object to *.dat file.
static float pedfit_fg32[768]
DSP coefficients used to determine time in pedestalFit.
static float pedfit_fg31[768]
DSP coefficients used to determine amplitude in pedestalFit.
static void initPedestalFit()
Load DSP coefficients used in the pedestal fit function.
static std::string findFile(const std::string &path, bool silent=false)
Search for given file or directory in local or central release directory, and return absolute path if...
Abstract base class for different kinds of events.
This struct is returned by the pedestalFit method that fits the first 16 samples of the waveform (ped...
ShaperDSP fit results from _lftda function.