BmnRoot 26.10.0-233-gb9bbe585
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
BmnTrigRaw2Digit.cxx
Go to the documentation of this file.
1#include "BmnTrigRaw2Digit.h"
2// C++
3#include <climits>
4// Boost
5#include <boost/program_options.hpp>
6// ROOT
7#include <TStopwatch.h>
8// FairRoot
9#include <FairLogger.h>
10// BmnRoot
11#include "BmnTDCDigit.h"
12#include "BmnTQDCADCDigit.h"
13#include "BmnTrigDigit.h"
14#include "BmnTrigWaveDigit.h"
15
16namespace po = boost::program_options;
17using namespace std;
18using namespace TMath;
19
20BmnTrigRaw2Digit::BmnTrigRaw2Digit(uint32_t period, TString PlacementMapFile, TString StripMapFile, TTree* digiTree)
21 : fPeriodId(period)
23 // , fWorkArray{}
24 , fTrcSerial(0)
25{
26 // fWorkHist->SetDirectory(0);
27 ReadPlacementMap(PlacementMapFile);
28 if (ReadChannelMap(StripMapFile) == kBMNERROR) {
29 printf("Error! Unable to read the trigger channel map!\n");
30 return;
31 }
32 if (digiTree == NULL)
33 return;
34 // Create corresponding branches for each trigger. //
35 // different channels of the one trigger will be stored //
36 // in one branch with different mod ID //
37 map<TString, TClonesArray*> brName2ptr;
38 for (BmnTrigChannelData& record : fChannelVec) {
39 TString& detName = record.name;
40 TClass* cl = detName.Contains("TQDC") ? BmnTrigWaveDigit::Class() : BmnTrigDigit::Class();
41 auto it = brName2ptr.find(detName);
42 if (it == brName2ptr.end()) {
43 TClonesArray* ar = new TClonesArray(cl);
44 ar->SetName(detName.Data());
45 digiTree->Branch(detName.Data(), &ar);
46 trigArrays.push_back(ar);
47 record.branchArrayPtr = ar;
48 brName2ptr.insert(make_pair(detName, ar));
49 } else {
50 record.branchArrayPtr = it->second;
51 }
52 }
53 AssignMaps();
54}
55
56// BmnTrigRaw2Digit::BmnTrigRaw2Digit(TString PlacementMapFile, TString StripMapFile, FairRootManager* frm, bool
57// saveOutput) :
58
60 TString PlacementMapFile,
61 TString StripMapFile,
62 std::function<void(TString, TClonesArray*)> BranchRegFun)
63 : fPeriodId(period)
65 // , fWorkHist(make_unique<TH1I>(Form("workhist_%p", this), "wh", SHRT_MAX - SHRT_MIN, SHRT_MIN, SHRT_MAX))
66 , fTrcSerial(0)
67{
68 // fWorkHist->SetDirectory(0);
69 ReadPlacementMap(PlacementMapFile);
70 if (ReadChannelMap(StripMapFile) == kBMNERROR) {
71 printf("Error! Unable to read the trigger channel map!\n");
72 return;
73 }
74 // Create corresponding branches for each trigger. //
75 // different channels of the one trigger will be stored //
76 // in one branch with different mod ID //
77 map<TString, TClonesArray*> brName2ptr;
78 for (BmnTrigChannelData& record : fChannelVec) {
79 TString& detName = record.name;
80 TClass* cl = detName.Contains("TQDC") ? BmnTrigWaveDigit::Class() : BmnTrigDigit::Class();
81 auto it = brName2ptr.find(detName);
82 if (it == brName2ptr.end()) {
83 TClonesArray* ar = new TClonesArray(cl);
84 ar->SetName(detName.Data());
85 BranchRegFun(detName, ar);
86 // frm->Register(detName, "Trig", ar, saveOutput);
87 trigArrays.push_back(ar);
88 record.branchArrayPtr = ar;
89 brName2ptr.insert(make_pair(detName, ar));
90 } else
91 record.branchArrayPtr = it->second;
92 }
93 AssignMaps();
94}
95
97{
98 TString PlMapFileName = TString(getenv("VMCWORKDIR")) + TString("/input/") + mappingFile;
99 LOGF(info, "Reading Triggers placement mapping file %s...", PlMapFileName.Data());
100 ifstream pmFile;
101 pmFile.open(PlMapFileName.Data());
102 if (!pmFile.is_open()) {
103 cout << "Error opening map-file (" << PlMapFileName << ")!" << endl;
104 return kBMNERROR;
105 }
106 string dummy;
107 string name;
108 UInt_t crateSerial, boardSerial;
109 uint16_t slot;
110 uint16_t isT0;
111
112 pmFile >> dummy >> dummy >> dummy >> dummy >> dummy;
113 pmFile >> dummy;
114 while (pmFile >> name >> hex >> crateSerial >> dec >> slot >> hex >> boardSerial >> dec >> isT0) {
115 fPlacementVec.emplace_back(PlacementLine{
116 .BoardSerial = boardSerial, .CrateSerial = crateSerial, .Slot = (uint8_t)slot, .IsT0 = (bool)isT0});
117 auto [newIt, didInsert] = fTrigBoardMap.emplace(boardSerial, BmnTrigParameters{.name = name,
118 .BoardSerial = boardSerial,
119 .CrateSerial = crateSerial,
120 .Slot = (uint8_t)slot,
121 .IsT0 = (bool)isT0});
122 if (didInsert)
123 LOGF(debug, "Board 0x%08X inserted", newIt->second.BoardSerial);
124 else {
125 LOGF(error, "Board 0x%08X duplicate serial in the placement map!", newIt->second.BoardSerial);
126 return kBMNERROR;
127 }
128 }
129 pmFile.close();
130 return kBMNSUCCESS;
131}
132
134{
135 fMapFileName = TString(getenv("VMCWORKDIR")) + TString("/input/") + mappingFile;
136 LOGF(info, "Reading Triggers strip mapping file %s...", fMapFileName.Data());
137
138 vector<string> trc_trigger_bits;
139 vector<string> scalers;
140 vector<string> configuration;
141 vector<string> t0_raw;
142
143 // Setup options.
144 po::options_description desc("Options");
145 desc.add_options()("INPUTS.serial", po::value<string>(), "TRC serial")(
146 "INPUTS.channels", po::value<vector<string>>(&trc_trigger_bits)->multitoken(), "TRC trigger input signal bits")(
147 "SCALERS.channels", po::value<vector<string>>(&scalers)->multitoken(),
148 "Scaler channels")("TDC-CONFIGURATION.channels", po::value<vector<string>>(&configuration)->multitoken(),
149 "TDC configuration")("T0-RAW.virt_serial", po::value<string>(), "T0 virtual serial")(
150 "T0-RAW.channels", po::value<vector<string>>(&t0_raw)->multitoken(), "TDC configuration");
151
152 // Load config file.
153 po::variables_map vm;
154 ifstream config_file(fMapFileName.Data(), ifstream::in);
155 if (!config_file.is_open()) {
156 printf("%s - file open error!\n", mappingFile.Data());
157 return kBMNERROR;
158 }
159 po::store(po::parse_config_file(config_file, desc), vm);
160 config_file.close();
161 po::notify(vm);
162
163 stringstream hs(vm["INPUTS.serial"].as<string>());
164 hs >> std::hex >> fTrcSerial;
165 LOGF(info, "TRC serial %8X", fTrcSerial);
166
167 //========== read TRC trig bits map ==========//
168 string tr_name;
169 uint16_t bit;
170 for (auto it : trc_trigger_bits) {
171 istringstream ss(it);
172 ss >> tr_name >> bit;
173 fTrcBitMap[tr_name] = bit;
174 }
175 //========== read scalers channel map ==========//
176 UInt_t ser;
177 uint16_t ch;
178 uint16_t scalers_ar_size(0); // MSC list size
179 for (auto it : scalers) {
180 istringstream ss(it);
181 ss >> tr_name >> hex >> ser >> dec >> ch;
182 std::pair p_ser_ch = make_pair(ser, ch);
183 scalers2name_map.insert(make_pair(p_ser_ch, tr_name));
184 scalers2index_map.insert(make_pair(p_ser_ch, scalers_ar_size));
185 scalers_name2index_map.insert(make_pair(tr_name, scalers_ar_size));
186 scalers_index2name_map.push_back(tr_name);
187 // scalers_index2name_map.insert(make_pair(scalers_ar_size, tr_name));
188 name2scaler_map[tr_name] = p_ser_ch;
189 auto it_trc = fTrcBitMap.find(tr_name);
190 if (it_trc != fTrcBitMap.end())
191 trcIdx2scalerIdx.insert(make_pair(it_trc->second, scalers_ar_size));
192 scalers_ar_size++;
193 LOGF(debug, "msc[%15s] = (0x%08X : %2u)", tr_name.data(), name2scaler_map[tr_name].first,
194 name2scaler_map[tr_name].second);
195 }
196 //========== read TDC channel map ==========//
197 TString name;
198 uint16_t mod;
199 uint16_t neg;
200 for (auto it : configuration) {
201 istringstream ss(it);
202 ss >> name >> mod >> hex >> ser >> dec >> ch >> neg;
203 if (ch < 0)
204 continue;
205 BmnTrigChannelData record{};
206 record.branchArrayPtr = NULL;
207 record.name = name;
208 record.serial = ser; // from now serial of the board itself (not crate or any WR distributor)
209 record.module = mod;
210 record.channel = ch;
211 record.isNegative = neg > 0 ? kTRUE : kFALSE;
212 fChannelVec.push_back(record);
213 }
214 //========== read T0 raw signals from Sergeev's txt ==========//
215 stringstream vs(vm["T0-RAW.virt_serial"].as<string>());
216 UInt_t t0_virt_serial;
217 vs >> std::hex >> t0_virt_serial;
218 LOGF(info, "t0_virt_serial %0X", t0_virt_serial);
219 for (auto it : t0_raw) {
220 istringstream ss(it);
221 ss >> tr_name >> mod >> ch;
222 fT0Map[make_pair(tr_name, mod)] = ch;
223 LOGF(debug, "trig[%10s] mod %2u ch %3u", tr_name.data(), mod, ch);
224 }
225 return kBMNSUCCESS;
226}
227
229{
230 LOGF(info, "Assigning trigger maps...");
231 // Fill elements of placement map with channel->(strip, mod, branchRef) map //
232 for (BmnTrigChannelData& record : fChannelVec) {
233 uint32_t& boardSerial = record.serial;
234 BmnTrigParameters* par = nullptr;
235 auto itPar = fTrigBoardMap.find(boardSerial);
236 if (itPar == fTrigBoardMap.end()) {
237 LOGF(warning,
238 "BoardSeral %08X was not present in the placement map file!\n"
239 " Board sync time will be associated with it's own WR time!\n",
240 boardSerial);
241 auto [newIt, didInsert] =
242 fTrigBoardMap.emplace(boardSerial, BmnTrigParameters{
243 .name = record.name,
244 .BoardSerial = boardSerial,
245 .CrateSerial = boardSerial, // as no placement record found
246 .Slot = record.slot,
247 .IsT0 = false // we presume
248 });
249 if (didInsert)
250 LOGF(debug, "Board 0x%08X inserted", newIt->second.BoardSerial);
251 else {
252 LOGF(error, "Board 0x%08X duplicate serial in the placement map!", newIt->second.BoardSerial);
253 // return kBMNERROR;
254 }
255 par = &(newIt->second);
256 } else {
257 par = &(itPar->second);
258 }
259 par->ChannelMap[record.channel] = record.module;
260 par->branchArrayPtr[record.channel] = record.branchArrayPtr;
261 par->NegativeMap[record.channel] = record.isNegative;
262 }
263 for (auto& el : fTrigBoardMap) {
264 BmnTrigParameters& par = el.second;
265 ReadINLFromFile(&par);
266 }
267}
268
270{
271 fstream ff;
272 TString fINLFileName =
273 TString(getenv("VMCWORKDIR")) + TString("/input/") + Serial2FileName(par->name, par->BoardSerial);
274 ff.open((fINLFileName).Data(), ios::in);
275 if (!ff.is_open()) {
276 cout << "Error opening INL-file (" << fINLFileName << ")!" << endl;
277 return kBMNERROR;
278 }
279 LOGF(info, "Open INL file %s", fINLFileName.Data());
280 TPRegexp reInlHdr("\\[.*(inl_corr).*\\]"); // INL block header
281 // regex reInlHdr("\\‍[.*(inl_corr).*\\‍]"); // INL block header
282 // regex reInlChannel("\\s*(\\d+)=(.+)"); // chID=c0, c1, ...
283 Bool_t isInlHdr = kFALSE;
284 while (!ff.eof()) {
285 string line;
286 std::getline(ff, line, '\n');
287 if (reInlHdr.MatchB(line)) {
288 // if (regex_match(line, reInlHdr)) {
289 isInlHdr = kTRUE;
290 break;
291 }
292 }
293 if (!isInlHdr) {
294 printf("Incorrect INL file format!\n");
295 return kBMNERROR;
296 }
297 uint16_t channelID = 0;
298 while (!ff.eof()) {
299 string line;
300 std::getline(ff, line, '\n');
301 // printf("Read %lu \n", line.length());
302 // if (!regex_match(line, reInlChannel))
303 // continue;
304 // line = regex_replace(line, reInlChannel, "$1 $2");
305 // printf("%s\n", line.c_str());
306 istringstream ss(line);
307 ss >> channelID;
308 // printf("Channel ID = %u\n", channelID);
309 uint16_t i_bin = 0;
310 while (ss.tellg() != -1) {
311 if (i_bin > TDC_BIN_COUNT) {
312 printf("INL File contains too many bins in channel.\n");
313 ff.close();
314 return kBMNERROR;
315 }
316 if (ss.peek() == ',' || ss.peek() == '=') {
317 ss.ignore();
318 }
319 ss >> par->INL[channelID][i_bin];
320 i_bin++;
321 }
322 }
323 ff.close();
324 return kBMNSUCCESS;
325}
326
327void BmnTrigRaw2Digit::ProcessWave(vector<int16_t>& iValue, Bool_t& isNeg)
328{
329 // Double_t rtime;
330 // Double_t ctime;
331 // TStopwatch timer;
332 // timer.Start();
333 int16_t baseLine = GetBaselineByMap(iValue);
334 // timer.Stop();
335 // rtime = timer.RealTime();
336 // ctime = timer.CpuTime();
337 // printf("Real time %f s, CPU time %f s baseline\n", rtime, ctime);
338 // timer.Start();
339 for (auto& val : iValue)
340 val -= baseLine;
341 if (isNeg)
342 for (auto& val : iValue)
343 val = -val;
344 // timer.Stop();
345 // rtime = timer.RealTime();
346 // ctime = timer.CpuTime();
347 // printf("Real time %f s, CPU time %f s sub\n", rtime, ctime);
348}
349
350// int16_t BmnTrigRaw2Digit::GetBaseline(vector<int16_t>& iValue)
351//{
352// fWorkArray = {};
353// for (auto& val : iValue)
354// fWorkArray[val - SHRT_MIN]++;
355// uint16_t maxCount = 0;
356// size_t mode = 0;
357// for (size_t i = 0; i < ADC_RANGE; ++i)
358// if (fWorkArray[i] > maxCount) {
359// maxCount = fWorkArray[i];
360// mode = i;
361// }
362// return (mode + SHRT_MIN);
363// }
364
365int16_t BmnTrigRaw2Digit::GetBaselineByMap(vector<int16_t>& iValue)
366{
367 fValsMap.clear();
368 for (auto& val : iValue)
369 fValsMap[val]++;
370 uint16_t maxCount = 0;
371 int16_t mode = 0;
372 for (auto& [val, cnt] : fValsMap)
373 if (cnt > maxCount) {
374 maxCount = cnt;
375 mode = val;
376 }
377 return mode;
378}
379
380BmnStatus BmnTrigRaw2Digit::FillEvent(TClonesArray* tdc, TClonesArray* adc, unordered_map<UInt_t, Long64_t>& tsMap)
381{
382 std::vector<Double_t> times;
383 for (Int_t iAdc = 0; iAdc < adc->GetEntriesFast(); iAdc++) {
384 times.clear();
386 uint16_t iChannel = adcDig->GetChannel();
387 auto plIter = fTrigBoardMap.find(adcDig->GetSerial());
388 if (plIter == fTrigBoardMap.end())
389 continue;
390 BmnTrigParameters& par = plIter->second;
391 TClonesArray* trigAr = par.branchArrayPtr[iChannel];
392 if (trigAr) {
393 uint16_t iMod = par.ChannelMap[iChannel];
394 Double_t adcTimestamp = adcDig->GetAdcTimestamp() * ADC_CLOCK_TQDC16VS;
395 Double_t trgTimestamp = adcDig->GetTrigTimestamp() * ADC_CLOCK_TQDC16VS;
396
397 auto it = tsMap.find(adcDig->GetSerial());
398 Long64_t timeShift = (it != tsMap.end()) ? it->second : 0;
399 Double_t boardShift = par.IsT0 ? 0 : timeShift;
400
401 for (Int_t iTdc = 0; iTdc < tdc->GetEntriesFast(); ++iTdc) {
402 BmnTDCDigit* tdcDig = (BmnTDCDigit*)tdc->At(iTdc);
403 if (tdcDig->GetSerial() != adcDig->GetSerial() || tdcDig->GetSlot() != adcDig->GetSlot())
404 continue;
405 if (tdcDig->GetChannel() != iChannel)
406 continue;
407 Double_t time = (tdcDig->GetValue() + par.INL[iChannel][tdcDig->GetValue() % TDC_BIN_COUNT]) * TDC_CLOCK
409 + boardShift;
410 times.push_back(time);
411 }
412 BmnTrigWaveDigit* dig = new ((*trigAr)[trigAr->GetEntriesFast()])
413 BmnTrigWaveDigit(iMod, move(adcDig->GetValue()), trgTimestamp, adcTimestamp);
414 ProcessWave(dig->GetValue(), par.NegativeMap[iChannel]);
415 dig->TdcVector() = std::move(times);
416 }
417 }
418 return kBMNSUCCESS;
419}
420
421BmnStatus BmnTrigRaw2Digit::FillEvent(TClonesArray* tdc, unordered_map<UInt_t, Long64_t>& tsMap)
422{
423 for (auto& el : fTrigBoardMap)
424 std::fill_n(el.second.t, CHANNEL_COUNT_MAX, -1.0);
425 for (Int_t iTdc = 0; iTdc < tdc->GetEntriesFast(); ++iTdc) {
426 BmnTDCDigit* tdcDig = (BmnTDCDigit*)tdc->At(iTdc);
427 auto plIter = fTrigBoardMap.find(tdcDig->GetSerial());
428 if (plIter == fTrigBoardMap.end())
429 continue;
430 BmnTrigParameters& par = plIter->second;
431
432 auto it = tsMap.find(tdcDig->GetSerial());
433 Long64_t timeShift = (it != tsMap.end()) ? it->second : 0;
434 Double_t boardShift = par.IsT0 ? 0 : timeShift;
435
436 uint16_t rChannel = tdcDig->GetHptdcId() * kNCHANNELS + tdcDig->GetChannel();
437 Double_t time =
438 (tdcDig->GetValue() + par.INL[rChannel][tdcDig->GetValue() % TDC_BIN_COUNT]) * TDC_CLOCK / TDC_BIN_COUNT
439 + boardShift;
440 // printf("\tCrateSeral %08X slot %02u channel %02u time %+2.2f leading %d neg %d\n",
441 // tdcDig->GetSerial(), tdcDig->GetSlot(), rChannel, time,
442 // tdcDig->GetLeading(),par.NegativeMap[rChannel]);
443 if (tdcDig->GetLeading() ^ par.NegativeMap[rChannel]) {
444 par.t[rChannel] = time;
445 } else {
446 if (time < par.t[rChannel])
447 continue;
448 if (par.t[rChannel] < 0)
449 continue;
450 uint16_t iMod = par.ChannelMap[rChannel];
451 TClonesArray* trigAr = par.branchArrayPtr[rChannel];
452 if (trigAr == NULL)
453 continue;
454 Double_t tL = par.t[rChannel];
455 Double_t tT = time;
456 par.t[rChannel] = -1.0;
457 new ((*trigAr)[trigAr->GetEntriesFast()]) BmnTrigDigit(iMod, tL, tT - tL);
458 }
459 }
460 return kBMNSUCCESS;
461}
462
464{
465 for (TClonesArray* ar : trigArrays)
466 ar->Delete();
467 return kBMNSUCCESS;
468}
BmnStatus
Define status code.
Definition BmnEnums.h:93
@ kBMNERROR
Error.
Definition BmnEnums.h:95
@ kBMNSUCCESS
Success.
Definition BmnEnums.h:94
@ kBMNSETUP
Definition BmnEnums.h:166
const UShort_t kNCHANNELS
#define TDC_CLOCK
#define ADC_CLOCK_TQDC16VS
#define CHANNEL_COUNT_MAX
#define TDC_BIN_COUNT
bool Bool_t
int Int_t
constexpr Bool_t kFALSE
double Double_t
long long Long64_t
constexpr Bool_t kTRUE
UInt_t GetValue() const
Definition BmnTDCDigit.h:32
UInt_t GetSerial() const
Definition BmnTDCDigit.h:22
UChar_t GetHptdcId() const
Definition BmnTDCDigit.h:34
UChar_t GetSlot() const
Definition BmnTDCDigit.h:26
UChar_t GetChannel() const
Definition BmnTDCDigit.h:30
Bool_t GetLeading() const
Definition BmnTDCDigit.h:28
UChar_t GetSlot() const
UShort_t GetTrigTimestamp() const
UShort_t GetAdcTimestamp() const
int16_t GetBaselineByMap(vector< int16_t > &iValue)
BmnStatus ReadINLFromFile(BmnTrigParameters *par)
Name2SerCh name2scaler_map
map< std::pair< uint32_t, uint16_t >, uint16_t > scalers2index_map
BmnStatus ReadChannelMap(TString mappingFile)
unordered_map< uint32_t, BmnTrigParameters > fTrigBoardMap
vector< BmnTrigChannelData > fChannelVec
vector< PlacementLine > fPlacementVec
void ProcessWave(vector< int16_t > &iValue, Bool_t &isNeg)
map< uint16_t, uint16_t > trcIdx2scalerIdx
vector< string > scalers_index2name_map
unordered_map< int16_t, uint16_t > fValsMap
SerCh2Name scalers2name_map
vector< TClonesArray * > trigArrays
NameChMap scalers_name2index_map
TString Serial2FileName(TString boardName, UInt_t serial)
BmnStatus FillEvent(TClonesArray *tdc, unordered_map< UInt_t, Long64_t > &tsMap)
BmnStatus ReadPlacementMap(TString mappingFile)
BmnTrigRaw2Digit(uint32_t period, TString mappingFile, TString INLFile, TTree *digiTree=NULL)
class for TQDC data including waveform can be initialized both from an std::vector and raw pointer to...
vector< int16_t > & GetValue()
vector< Double_t > & TdcVector()
void SetName(const char *name)
static TClass * Class()
Int_t GetEntriesFast() const
TObject * At(Int_t idx) const override
static TClass * Class()
Bool_t MatchB(const TString &s, const TString &mods="", Int_t start=0, Int_t nMaxMatch=10)
const char * Data() const
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
virtual Int_t Branch(const char *folder, Int_t bufsize=32000, Int_t splitlevel=99)
Double_t t[CHANNEL_COUNT_MAX]
Double_t INL[CHANNEL_COUNT_MAX][TDC_BIN_COUNT]
TClonesArray * branchArrayPtr[CHANNEL_COUNT_MAX]
bool NegativeMap[CHANNEL_COUNT_MAX]
UShort_t ChannelMap[CHANNEL_COUNT_MAX]