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