BmnRoot 26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
BmnTof1Raw2Digit.cxx
Go to the documentation of this file.
1#include "BmnTof1Raw2Digit.h"
2
3// Written by JINR summer programme 2016 student Kurganov Alexander
4
5// Supervisors:
6// Vadim Babkin
7// Mikhail Rumyantsev
8// Vyatcheslav Golovatjuk
9
10typedef std::map<std::pair<UInt_t, UChar_t>, UInt_t>::iterator Tof1PlMapIter;
11typedef std::map<UInt_t, BmnTof1TDCParameters>::iterator Tof1TDCMapIter;
12typedef std::pair<UInt_t, UChar_t> Tof1PlMapKey;
13typedef std::pair<UInt_t, BmnTof1TDCParameters> Tof1TDCMapElem;
14
15using namespace std;
16
18
19//----------------------------------------------------
20// Simple constructors for the BmnTof1Map2 element class
22{
23 plane = p;
24 strip = s;
25 side = sd;
26}
27
28//----------------------------------------------------
30{
31 plane = 0;
32 strip = 0;
33 side = false;
34}
35
36//----------------------------------------------------
37// Main constructor for the BmnTof1TDCParameters()
39{
40 for (int i = 0; i < TOF1_CHANNEL_NUMBER; i++) {
41 for (int j = 0; j < TOF1_BIN_NUMBER; j++) {
42 INL[i][j] = 0;
43 }
44 ChannelMap[i] = BmnTof1Map2(0, 0, false);
45 t[i] = -1;
46 }
47}
48
49//----------------------------------------------------
50// A small functional class, which is used to compare two BmnTDCDigits
51// It is used to sort by time the TDCDigits in the BmnTof1Raw2Digit::FillEvent()
53{
54 return a.GetValue() < b.GetValue();
55}
56
57//----------------------------------------------------
58// A small function, which inserts in the placement map a key-value pair
59// Just to make everything more clear
60void BmnTof1Raw2Digit::plmap_insert(UInt_t Serial, UChar_t Slot, UInt_t TDCSerial)
61{
62 PlacementMap.insert(std::pair<Tof1PlMapKey, UInt_t>(Tof1PlMapKey(Serial, Slot), TDCSerial));
63}
64
65//----------------------------------------------------
66// BmnTof1Raw2Digit main constructor
71
72//----------------------------------------------------
73// BmnTof1Raw2Digit constructor, which also loads mapping from the DB
75{
76 fVerbose = verbose;
77 init();
78 setRun(nPeriod, nRun);
79}
80
81//----------------------------------------------------
82// BmnTof1Raw2Digit destructor
84
85//----------------------------------------------------
86// BmnTof1Raw2Digit init function (called in BmnTof1Raw2Digit constructors)
88{
89 // Nothing here
90}
91
92//----------------------------------------------------
93// A simple function to convert Hptdcid (0 -- 9) and Hptdc's channel (0 -- 8) to "global" tdc channel (0 -- 72)
95{
96 return HptdcId * 8 + channel;
97}
98
99//----------------------------------------------------
100// Loads mapping from the DB
102{
103
104 Bool_t flag_temp = kTRUE;
105 LOGF(info, ANSI_COLOR_BLUE "Loading the TOF400 Mapping and INL from DB: Period %d, Run %d ..." ANSI_COLOR_RESET,
106 nPeriod, nRun);
107 PeriodIndex = nPeriod;
108 RunIndex = nRun;
109 // return kFALSE;
110 //--------------------------------------------------------------------------
111 // Load the TDC's map (placement map) from the DB
112 LOGF(debug, "Loading the placement map...");
113 UniDetectorParameter* pLoadedPlacement =
114 UniDetectorParameter::GetDetectorParameter("TOF1", "TOF1_TdcMap", nPeriod, nRun);
115 if (pLoadedPlacement == 0) {
116 LOGF(error, ANSI_COLOR_RED "\tTOF400 Crucial error: failed to load the placement map from DB" ANSI_COLOR_RESET);
117 flag_temp = kFALSE;
118 } else {
119 // Get the placement map
120 vector<UniValue*> tdcmap_array;
121 pLoadedPlacement->GetValue(tdcmap_array);
122 MapIntValue* temptdcMap = nullptr;
123 // loop over TDCs stored in the DB
124 for (size_t i = 0; i < tdcmap_array.size(); i++) {
125 temptdcMap = (MapIntValue*)tdcmap_array.at(i);
126 plmap_insert(temptdcMap->serial, temptdcMap->channel, temptdcMap->value);
127 // create TDC in the map
128 TDCMap.insert(Tof1TDCMapElem(temptdcMap->value, BmnTof1TDCParameters()));
129 }
130
131 // Print the result
132 LOGF(debug, "\t\tLoaded %d TDCs", (int)tdcmap_array.size());
133
134 // Free the memory
135 if (!tdcmap_array.empty())
136 for (size_t i = 0; i < tdcmap_array.size(); i++)
137 delete tdcmap_array[i];
138 delete pLoadedPlacement;
139 // delete temptdcMap; //???
140 }
141
142 // Tof1PlMapIter parPair = PlacementMap.begin();
143 // while (parPair != PlacementMap.end()){
144 // printf ("\t%08X \t %08X \t %08X \n", parPair->first.first, parPair->first.second, parPair->second);
145 // parPair++;
146 // }
147 // getchar();
148
149 //--------------------------------------------------------------------------
150 // Load the strip mapping from the DB (TDCs channel -> plane, strip, side).
151 LOGF(debug, "Loading strip map...");
152 UniDetectorParameter* pLoadedStr =
153 UniDetectorParameter::GetDetectorParameter("TOF1", "TOF1_StripMap", nPeriod, nRun);
154 if (pLoadedStr == 0) {
155 LOGF(error, ANSI_COLOR_RED "\tTOF400 Crucial error: failed to load the strip map from DB" ANSI_COLOR_RESET);
156 flag_temp = kFALSE;
157 } else {
158 // Get the str map
159 vector<UniValue*> stripmap_array;
160 pLoadedStr->GetValue(stripmap_array);
161 MapDVectorValue* tempstrMap = nullptr;
162 for (size_t i = 0; i < stripmap_array.size(); i++) {
163 tempstrMap = (MapDVectorValue*)stripmap_array.at(i);
164 // Looking for TDC in the map
165 Tof1TDCMapIter TDCPair = TDCMap.find(tempstrMap->serial);
166 if (TDCPair == TDCMap.end()) {
167 LOGF(error, ANSI_COLOR_RED "\t\tError: TDC # 0x%08x are not found in the strip map" ANSI_COLOR_RESET,
168 tempstrMap->serial);
169 // fill map for this TDC by under range value
170 BmnTof1Map2* elem = &((TDCPair->second).ChannelMap[tempstrMap->channel]);
171 elem->plane = -1;
172 elem->strip = -1;
173 elem->side = TOF1_LEFT;
174 }
175 // get the channel map from
176 BmnTof1Map2* elem = &((TDCPair->second).ChannelMap[tempstrMap->channel]);
177 elem->plane = tempstrMap->value.at(0);
178 elem->strip = tempstrMap->value.at(1);
179 if (tempstrMap->value.at(2) == 0)
180 elem->side = TOF1_LEFT;
181 else if (tempstrMap->value.at(2) == 1)
182 elem->side = TOF1_RIGHT;
183 // cout << std::hex << tempstrMap->serial << std::dec << " ch " << tempstrMap->channel << " -> " <<
184 // tempstrMap->value.at(0) << " " << tempstrMap->value.at(1) << " " << tempstrMap->value.at(2) <<
185 // endl;
186 }
187
188 // Print the result
189 LOGF(debug, "\t\tLoaded %d strips", (int)stripmap_array.size());
190 // Free the memory
191 if (!stripmap_array.empty())
192 for (size_t i = 0; i < stripmap_array.size(); i++)
193 delete stripmap_array[i];
194 delete pLoadedStr;
195 // delete tempstrMap; // ???
196 }
197
198 // printf("Check map of TDCs\n");
199 // Tof1TDCMapIter TDCPair = TDCMap.begin();
200 // while (TDCPair != TDCMap.end()) {
201 // printf("TDC %08X; Det = %d;\n", TDCPair->first, TDCPair->second.ChannelMap[0].plane);
202 // ++TDCPair;
203 // }
204 // getchar();
205
206 //--------------------------------------------------------------------------
207 // Load Inl from DB
208 LOGF(debug, "Loading the INL correction from DB... ");
210 "TOF1", "TOF1_inl", nPeriod, nRun); //(detector_name, parameter_name, period_number, run_number)
211 if (pLoadedInl == NULL) {
212 LOGF(error, ANSI_COLOR_RED "\tINL are not exist in DB\n\t\tTime information will be wrong !!" ANSI_COLOR_RESET);
213 flag_temp = kFALSE; //
214 } else {
215 // get INL from DB to vector
216 vector<UniValue*> inl_array;
217 pLoadedInl->GetValue(inl_array);
218 TdcInlValue* tempInl = nullptr;
219 UInt_t serialtemp;
220 Bool_t flagtemp = false;
221 Tof1TDCMapIter tdcs = TDCMap.begin();
222 // loop over TDCs in the map
223 while (tdcs != TDCMap.end()) {
224 serialtemp = tdcs->first;
225 flagtemp = false;
226
227 // looking for INL for current TDC in the INL_vector from DB
228 for (size_t iv = 0; iv < inl_array.size(); iv++) {
229 tempInl = (TdcInlValue*)inl_array.at(iv);
230 if (tempInl->serial == serialtemp) {
231 LOGF(debug, "\t\tINL for %08X are found", serialtemp);
232 flagtemp = true;
233 for (Int_t ich = 0; ich < TOF1_CHANNEL_NUMBER; ich++)
234 for (Int_t ibin = 0; ibin < TOF1_BIN_NUMBER; ibin++)
235 (tdcs->second).INL[ich][ibin] = tempInl->inl[ich][ibin];
236 }
237 }
238 if (flagtemp == false) {
239 LOGF(error, ANSI_COLOR_RED "\t\tError: INL for TDC # 0x%08X are not stored in DB" ANSI_COLOR_RESET,
240 serialtemp);
241 // fill INL for this TDC by zero
242 for (Int_t ich = 0; ich < TOF1_CHANNEL_NUMBER; ich++)
243 for (Int_t ibin = 0; ibin < TOF1_BIN_NUMBER; ibin++)
244 (tdcs->second).INL[ich][ibin] = 0.;
245 }
246 // increase iterator
247 tdcs++;
248 }
249
250 LOGF(debug, "\t\tLoading INL for TOF400 complete. %d records uploaded", inl_array.size());
251 // Free the memory
252 if (!inl_array.empty())
253 for (size_t i = 0; i < inl_array.size(); i++)
254 delete inl_array[i];
255 delete pLoadedInl;
256 }
257
258 //--------------------------------------------------------------------------
259 return flag_temp;
260}
261
262//----------------------------------------------------
263// Load mapping from the file
265{
266 LOGF(info, ANSI_COLOR_BLUE "\tLoad TOF400 mapping from files:\n\t%s \n\t%s" ANSI_COLOR_RESET,
267 placementMapFile.Data(), mapFile.Data());
268 UInt_t fcrate, fserial, fslot, fchan, fplane, fstrip;
269 char side_c;
270 Bool_t flag_temp = kTRUE;
271
272 // 1. Placement map
273 fstream ff;
274 ff.open(placementMapFile.Data(), std::fstream::in);
275 if (ff.fail()) {
276 LOGF(error, ANSI_COLOR_RED "\t\tError: Cannot open the file %s" ANSI_COLOR_RESET, placementMapFile.Data());
277 flag_temp = kFALSE;
278 } else {
279 LOGF(debug, "Reading TOF400 PlacementMap file %s", placementMapFile.Data());
280 while (!ff.eof()) {
281 ff >> std::hex >> fcrate >> std::dec >> fslot >> std::hex >> fserial >> std::dec;
282 if (ff.eof()) {
283 break;
284 }
285 plmap_insert(fcrate, fslot, fserial);
286 TDCMap.insert(Tof1TDCMapElem(fserial, BmnTof1TDCParameters()));
287 }
288 LOGF(debug, "\t\tLoaded %d TDCs", (int)TDCMap.size());
289 }
290 ff.close();
291
292 // printf ("Check map of TDCs\n");
293 // Tof1PlMapIter plit = PlacementMap.begin();
294 // while (plit != PlacementMap.end()) {
295 // printf ("TTVXS %08X; Slot %d; TDC %08X\n", plit->first.first, plit->first.second, plit->second);
296 // ++plit;
297 // }
298 // getchar();
299
300 // 2. Main map
301 Int_t nLines = 0;
302 ff.open(mapFile.Data(), std::fstream::in);
303 if (ff.fail()) {
304 LOGF(error, ANSI_COLOR_RED "\t\tError: Cannot open the file %s" ANSI_COLOR_RESET, mapFile.Data());
305 flag_temp = kFALSE;
306 } else {
307 LOGF(debug, "Reading TOF400 StripMap file %s", mapFile.Data());
308 while (!ff.eof()) {
309 ff >> std::hex >> fserial >> std::dec >> fchan >> fplane >> fstrip >> side_c;
310 if (ff.eof()) {
311 break;
312 }
313 nLines++;
314 Tof1TDCMapIter TDCPair = TDCMap.find(fserial);
315 if (TDCPair == TDCMap.end()) {
316 LOGF(error, ANSI_COLOR_RED "\t\tError: TDC # 0x%08x not found" ANSI_COLOR_RESET, fserial);
317 }
318 BmnTof1Map2* elem = &((TDCPair->second).ChannelMap[fchan]);
319 elem->plane = fplane;
320 elem->strip = fstrip;
321 if (side_c == 'L') {
322 elem->side = TOF1_LEFT;
323 } else if (side_c == 'R') {
324 elem->side = TOF1_RIGHT;
325 } else {
326 // cerr << "ToF1 Error: side is not L or R" << endl;
327 continue;
328 }
329 }
330 LOGF(debug, "\t\tLoaded %d strips", nLines);
331 }
332 ff.close();
333
334 return flag_temp;
335}
336
337//----------------------------------------------------
338// Save mapping to file
339void BmnTof1Raw2Digit::saveMapToFile(string placementMapFile, string mapFile)
340{
341 fstream ff;
342
343 // 1.Placement map
344 ff.open(placementMapFile.c_str(), std::fstream::out);
345 if (ff.fail()) {
346 cerr << "Cannot open the file " << placementMapFile << endl;
347 return;
348 }
349
350 Tof1PlMapIter plit = PlacementMap.begin();
351 while (plit != PlacementMap.end()) {
352 ff << std::hex << std::setfill('0') << std::setw(8) << plit->first.first << '\t';
353 ff << std::dec << int(plit->first.second) << '\t';
354 ff << std::hex << std::setfill('0') << std::setw(8) << plit->second << endl;
355 ++plit;
356 }
357 ff.close();
358
359 // 2. Main map
360 ff.open(mapFile.c_str(), std::fstream::out);
361 if (ff.fail()) {
362 cerr << "Cannot open the file " << mapFile << endl;
363 return;
364 }
365
366 Tof1TDCMapIter mapit = TDCMap.begin();
367 while (mapit != TDCMap.end()) {
368 for (int i = 0; i < TOF1_CHANNEL_NUMBER; i++) {
369 ff << std::hex << std::setfill('0') << std::setw(8) << mapit->first << std::setfill(' ') << '\t';
370 ff << std::dec << i << '\t';
371 ff << mapit->second.ChannelMap[i].plane << '\t';
372 ff << mapit->second.ChannelMap[i].strip << '\t';
373 bool side = mapit->second.ChannelMap[i].side;
374 if (side == TOF1_LEFT) {
375 ff << 'L';
376 } else if (side == TOF1_RIGHT) {
377 ff << 'R';
378 } else {
379 cerr << "Side is not bool." << endl;
380 } // I don't think this can even occur
381 ff << endl;
382 }
383 ++mapit;
384 }
385}
386
387//----------------------------------------------------
388// Load INL to file
389void BmnTof1Raw2Digit::setINLFromFile(string INLFile)
390{
391 fstream ff(INLFile.c_str(), std::fstream::in);
392 if (ff.fail()) {
393 cerr << "Failed to open " << INLFile << endl;
394 return;
395 }
396 unsigned int TDCSerial;
397
398 // Read the header from the file
399 // The format of the header seems to be [TDC-THESERIAL-inl_corr]
400 ff.ignore(10, '-');
401 ff >> std::hex >> TDCSerial >> std::dec;
402 ff.ignore(1000, '\n');
403
404 // Find the TDC
405 Tof1TDCMapIter TDCPair = TDCMap.find(TDCSerial);
406 if (TDCPair == TDCMap.end()) {
407 cerr << "Tof400: TDC " << std::setfill('0') << std::setw(8) << std::hex << TDCSerial << std::setfill(' ')
408 << std::dec;
409 cerr << " isn't in the placement map." << endl;
410 ff.close();
411 return;
412 }
413
414 unsigned int chan_id = 0;
415 unsigned int lines_num = 0;
416
417 while (!ff.eof()) {
418 string line;
419 char dummy;
420
421 std::getline(ff, line, '\n');
422 if (ff.eof()) {
423 break;
424 }
425 if (line == "") {
426 continue;
427 }
428 istringstream ss(line);
429
430 ss >> chan_id >> dummy;
431 if (dummy != '=') {
432 cerr << "Tof400: Wrong INL file format." << endl;
433 ff.close();
434 return;
435 }
436 if (chan_id > TOF1_CHANNEL_NUMBER) {
437 cerr << "Tof400: Wrong channel in in the INL file" << endl;
438 ff.close();
439 return;
440 }
441
442 double* INLelem = (TDCPair->second).INL[chan_id];
443
444 unsigned int i_bin = 0;
445 while (ss.tellg() != -1) {
446 if (i_bin > TOF1_BIN_NUMBER) {
447 cerr << "Tof400: INL File contains too many bins in channel." << endl;
448 ff.close();
449 return;
450 }
451 if (ss.peek() == ',') {
452 ss.ignore();
453 }
454 ss >> INLelem[i_bin];
455 i_bin++;
456 }
457 if (i_bin != TOF1_BIN_NUMBER) {
458 cout << "Warning: wrong number of bins in the INL file for channel " << chan_id << " (" << i_bin << ")"
459 << endl;
460 }
461 lines_num++;
462 }
463
464 if (lines_num != TOF1_CHANNEL_NUMBER) {
465 cout << "Warning: wrong number of lines in the INL file (" << lines_num << endl;
466 }
467 cout << "Tof400: INL for TDC " << std::setfill('0') << std::setw(8) << std::hex << TDCSerial << std::setfill(' ')
468 << std::dec << " loaded succesfully from INL file." << endl;
469}
470
471//----------------------------------------------------
472// Save INL from file
473void BmnTof1Raw2Digit::saveINLToFile(string INLFile, unsigned int TDCSerial)
474{
475 // Find the TDC
476 Tof1TDCMapIter TDCPair = TDCMap.find(TDCSerial);
477 if (TDCPair == TDCMap.end()) {
478 cerr << "Tof400: TDC " << std::setfill('0') << std::setw(8) << std::hex << TDCSerial << std::setfill(' ')
479 << std::dec;
480 cerr << " isn't in the placement map." << endl;
481 return;
482 }
483
484 fstream ff(INLFile.c_str(), std::fstream::out);
485 ff << "[TDC-" << std::setfill('0') << std::setw(8) << std::hex << TDCSerial << std::dec << std::setfill(' ')
486 << "-inl_corr]" << endl;
487
488 for (int chan = 0; chan < TOF1_CHANNEL_NUMBER; chan++) {
489 ff << chan << "=";
490 double* INLelem = (TDCPair->second).INL[chan];
491 for (int bin = 0; bin < TOF1_BIN_NUMBER; bin++) {
492 ff << INLelem[bin];
493 if (bin != TOF1_BIN_NUMBER - 1) {
494 ff << ", ";
495 }
496 }
497 if (chan != TOF1_CHANNEL_NUMBER - 1) {
498 ff << endl;
499 }
500 }
501
502 ff.close();
503}
504
505//----------------------------------------------------
506// Main function. "Converts" the TObjArray *data of BmnTDCDigit to the TObjArray *output of BmnTof1Digit
507void BmnTof1Raw2Digit::FillEvent(TClonesArray* data, unordered_map<UInt_t, Long64_t>* mapTS, TClonesArray* output)
508{
509 // 0. Initialize: clear all the tempory times in the BmnTof1TDCParameters
510 Tof1TDCMapIter tdci = TDCMap.begin();
511 while (tdci != TDCMap.end()) {
512 for (int i = 0; i < TOF1_CHANNEL_NUMBER; i++) {
513 (tdci->second).t[i] = -1;
514 }
515 tdci++;
516 }
517
518 // 1. Filter and sort the TDC Data
519 // TODO: Well.. maybe it's better to do it NOT here?
520 // Maybe both of these should be done in the BmnRawDataDecoder?
521
522 int nOut = output->GetEntries();
523 // nOut variable is needed. GetEntries is quite a slow function,
524 // so storing the number of entries before filling the *output TObjArray works much faster
525
526 multiset<BmnTDCDigit, _Tof1TDCCompare> temp; // Create a multiset
527 // Multiset automaticly sorts the elements in it.
528 //_Tof1TDCCompare functional class is used to compare two BmnTDCDigit (see above)
529
530 // Fill the multiset from the input array (it sorts the input array)
531 TIter it = data->begin();
532 while (it != data->end()) {
533 BmnTDCDigit* obj = static_cast<BmnTDCDigit*>(*it);
534 if (obj->GetType() != TOF1_TDC_TYPE && obj->GetType() != TOF1_TDCVXS_TYPE) {
535 ++it;
536 continue;
537 } // Just skip it in case if TDC is not TDC72VHL (see TOF1_TDC_TYPE define in the .h file)
538 temp.insert(*obj);
539 // cout << std::hex << obj->GetSerial() << " " << std::dec << int(obj->GetSlot()) << " " <<
540 // int(obj->GetHptdcId()) << " " << int(obj->GetChannel()) << " " << (obj->GetLeading()?"L":"T") <<
541 // obj->GetValue() << endl;
542 ++it;
543 }
544
545 // Now a loop over the temp multiset using iterator si
546 multiset<BmnTDCDigit, _Tof1TDCCompare>::iterator si = temp.begin();
547
548 while (si != temp.end()) {
549 // Look for the crate serial and slot in the placement map
550 Tof1PlMapIter parPair = PlacementMap.begin();
551
552 // befor RUN8 BMN TOF400 are used FVME, so in the date we see serial of the trigger modules only and slot number
553 // of the TDC So pair of FVME+slot is key to get serial of TDC (for INL correction and mapping)
554 if (PeriodIndex <= 8 && RunIndex <= 5300) {
555 parPair = PlacementMap.find(std::make_pair<UInt_t, UChar_t>(si->GetSerial(), si->GetSlot()));
556 }
557
558 // Since RUN8 BMN TOF400 use TDC72VXS, so in the date we see serial of the TDC (second value of the map)
559 // But for WR correction we should to know Serial of TTVXS module in the current crate (First.first value of the
560 // map)
561 if (PeriodIndex >= 8 && RunIndex > 5300) {
562 // printf("Looking for TDC %08X in placement map\n", si->GetSerial());
563 while (parPair != PlacementMap.end()) {
564 // printf("Serial of TDC = %08X, Serial of TTVXS = %08X \n", parPair->second,
565 // parPair->first.first);
566 if (parPair->second == si->GetSerial())
567 break;
568 parPair++;
569 }
570 // getchar();
571 }
572 // In case if nothing was found
573 if (parPair == PlacementMap.end()) {
574 // Just skip it
575 si++;
576 continue;
577 // printf(" TDC are not in DB\n");
578 }
579
580 if (PeriodIndex <= 8 && RunIndex <= 5300)
581 fSerialForTS = si->GetSerial();
582 if (PeriodIndex >= 8 && RunIndex > 5300)
583 fSerialForTS = parPair->first.first;
584 auto itTS = mapTS->find(fSerialForTS);
585 if (itTS == mapTS->end()) {
586 si++;
587 continue;
588 }
589 Long64_t TimeShift = itTS->second;
590 // printf(" Time Shift = %lld\n", TimeShift);
591
592 // Get the TDC Serial for this crate serial and slot
593 UInt_t TDC_Serial = parPair->second;
594 // And look for this TDC Serial in the TDCMap to get the BmnTof1TDCParameters for this TDC
595 Tof1TDCMapIter TDCPair = TDCMap.find(TDC_Serial);
596 // In case if TDC Serial isn't in the TDCMap
597 if (TDCPair == TDCMap.end()) {
598 // Display a warning and skip. This is quite a serious error, actually
599 if (fVerbose >= 2)
600 cerr << "Warning: TDC with ID " << std::hex << TDC_Serial << std::dec << ": no data" << endl;
601 si++;
602 continue;
603 }
604
605 // Finally, get the BmnTof1TDCParameters instance for this TDC Serial
606 BmnTof1TDCParameters* par = &(TDCPair->second);
607 // And get it's channel using BmnTof1Raw2Digit::ToGlobalChannel (see above)
608 UShort_t rchan = ToGlobalChannel(si->GetHptdcId(), si->GetChannel());
609
610 // Int_t pl = par->ChannelMap[rchan].plane;
611 // if (si->GetSerial() == 0x07a9b1d6) {
612 // printf("Plane = %d\n", pl);
613 // getchar();
614 // }
615
616 // Get the time from the TDC value
617 // t = (val + INL[channel][val % 1024]) * (24ns / 1024)
618 // See TOF1_BIN_NUMBER and TOF1_MAX_TIME defines in the .h file
619 /*
620 if(par->INL[rchan][(si->GetValue()) % TOF1_BIN_NUMBER] == 0) {
621 cout << std::hex << TDC_Serial << std::dec << rchan << ":" << ((si->GetValue()) % TOF1_BIN_NUMBER) << "
622 - " << par->INL[rchan][(si->GetValue()) % TOF1_BIN_NUMBER] << endl;
623 }
624 */
625 double timeFromDigit = (si->GetValue() + par->INL[rchan][(si->GetValue()) % TOF1_BIN_NUMBER]) * TOF1_MAX_TIME
626 / double(TOF1_BIN_NUMBER);
627
628 if (si->GetLeading()) {
629 // If this is a leading TDC digit, just fill the temporary time in the BmnTof1TDCParameters.
630 par->t[rchan] = timeFromDigit;
631 } else {
632 // If this is NOT a leading TDC Digit...
633 if (par->t[rchan] != -1) {
634 // BmbTof1TDCParameters' temporary time (par->t[rchan]) has been already set
635 // This temporary time should store the leading time
636 if (timeFromDigit < (par->t[rchan]) && fVerbose >= 2) {
637 // This is impossible:
638 // Leading value is larger than the trailing time
639 // The BmnTDCDigit array has already been sorted, so this shouldn't happen
640 // But let's just make sure, this could be a serious error
641 cout << "Error: negative dt" << endl;
642
643 // Print all the TDC Digits we have loaded in the sorted order
644 multiset<BmnTDCDigit, _Tof1TDCCompare>::iterator si2 = temp.begin();
645 while (si2 != temp.end()) {
646 cout << std::hex << si2->GetSerial() << " " << int(si2->GetSlot()) << " " << std::dec
647 << int(si2->GetHptdcId()) << " " << int(si2->GetChannel()) << " "
648 << (si2->GetLeading() ? "L" : "T") << si2->GetValue() << endl;
649 si2++;
650 }
651 // Print where exactly the error has occured
652 cout << "Error at: " << endl;
653 cout << std::hex << si->GetSerial() << " " << int(si->GetSlot()) << " " << std::dec
654 << int(si->GetHptdcId()) << " " << int(si->GetChannel()) << " "
655 << (si->GetLeading() ? "L" : "T") << si->GetValue() << endl;
656 cout << std::hex << TDC_Serial << std::dec << " " << rchan << " " << (par->t[rchan]) << "--"
657 << timeFromDigit << endl;
658 } else {
659 // So we've got the Leading and Trail times and everything seems to be okay
660 // Find the BmnTof1Map2 mapping element, which stores the plane, strip and side.
661 BmnTof1Map2* elem = &(par->ChannelMap[rchan]);
662
663 // Create a new BmnTof1Digit:
664 // Plane, strip, and side are loaded from the found BmnTof1Map2
665 // Leading time is loaded from the temporary storage in the BmnTof1TDCParameters class
666 // Trailing time is just the time in current TDC Digit
667 //(See above: this piece of code is executed only if the current BmnTDCDigit is NOT leading)
668
669 new ((*output)[nOut]) BmnTof1Digit(elem->plane, elem->strip, elem->side, par->t[rchan] + TimeShift,
670 timeFromDigit - (par->t[rchan]));
671 nOut++;
672 }
673 }
674 // We don't need to do anything in case if no leading time has occured yet (if par->t[rchan] == -1)
675
676 // Finally, reset the temporary storage to prevent errors
677 par->t[rchan] = -1;
678 }
679 si++;
680 }
681 LOGF(debug, "TOF400 : %d digits", nOut);
682}
std::map< std::pair< UInt_t, UChar_t >, UInt_t >::iterator Tof1PlMapIter
std::pair< UInt_t, UChar_t > Tof1PlMapKey
int Tof1GlobalNameCounter
std::map< UInt_t, BmnTof1TDCParameters >::iterator Tof1TDCMapIter
std::pair< UInt_t, BmnTof1TDCParameters > Tof1TDCMapElem
int i
Definition P4_F32vec4.h:22
bool Bool_t
unsigned short UShort_t
int Int_t
unsigned char UChar_t
unsigned int UInt_t
short Short_t
constexpr Bool_t kFALSE
long long Long64_t
constexpr Bool_t kTRUE
#define ANSI_COLOR_RED
Definition BmnMath.h:16
#define ANSI_COLOR_RESET
Definition BmnMath.h:18
#define ANSI_COLOR_BLUE
Definition BmnMath.h:17
UChar_t GetType() const
Definition BmnTDCDigit.h:24
std::map< std::pair< UInt_t, UChar_t >, UInt_t > PlacementMap
Bool_t setRun(Int_t nPerion, Int_t nRun)
void saveINLToFile(std::string INLFile, unsigned int TDCSerial)
static UShort_t ToGlobalChannel(UChar_t HptdcId, UChar_t channel)
Bool_t setMapFromFile(TString placementMapFile, TString mapFile)
void plmap_insert(UInt_t Serial, UChar_t Slot, UInt_t TDCSerial)
void setINLFromFile(std::string INLFile)
void FillEvent(TClonesArray *data, unordered_map< UInt_t, Long64_t > *mapTS, TClonesArray *tof1digit)
std::map< UInt_t, BmnTof1TDCParameters > TDCMap
void saveMapToFile(std::string placementMapFile, std::string mapFile)
TIter end() const
TIter begin() const
Int_t GetEntries() const override
const char * Data() const
int GetValue(vector< UniValue * > &parameter_value)
get value of detector parameter presented by an array
static UniDetectorParameter * GetDetectorParameter(int value_id)
get detector parameter from the database
#define TOF1_TDC_TYPE
#define TOF1_RIGHT
#define TOF1_CHANNEL_NUMBER
#define TOF1_MAX_TIME
#define TOF1_TDCVXS_TYPE
#define TOF1_BIN_NUMBER
#define TOF1_LEFT
Definition json.hpp:4446
BmnTof1Map2 ChannelMap[TOF1_CHANNEL_NUMBER]
double t[TOF1_CHANNEL_NUMBER]
double INL[TOF1_CHANNEL_NUMBER][TOF1_BIN_NUMBER]
vector< double > value
double inl[72][1024]
bool operator()(const BmnTDCDigit &a, const BmnTDCDigit &b) const