Belle II Software  release-05-01-25
CDCDatabaseImporter.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2016 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Makoto Uchida *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 // Own include
12 #include <cdc/calibration/CDCDatabaseImporter.h>
13 
14 // framework - Database
15 #include <framework/database/DBArray.h>
16 #include <framework/database/DBObjPtr.h>
17 #include <framework/database/IntervalOfValidity.h>
18 #include <framework/database/DBImportArray.h>
19 #include <framework/database/DBImportObjPtr.h>
20 
21 // framework aux
22 #include <framework/logging/Logger.h>
23 // framework timer
24 #include <framework/utilities/Utils.h>
25 
26 // DB objects
27 #include <cdc/dataobjects/WireID.h>
28 #include <cdc/dbobjects/CDCChannelMap.h>
29 #include <cdc/dbobjects/CDCTimeZeros.h>
30 #include <cdc/dbobjects/CDCBadWires.h>
31 #include <cdc/dbobjects/CDCPropSpeeds.h>
32 #include <cdc/dbobjects/CDCTimeWalks.h>
33 #include <cdc/dbobjects/CDCXtRelations.h>
34 #include <cdc/dbobjects/CDCSpaceResols.h>
35 #include <cdc/dbobjects/CDCFudgeFactorsForSigma.h>
36 #include <cdc/dbobjects/CDCDisplacement.h>
37 #include <cdc/dbobjects/CDCAlignment.h>
38 #include <cdc/dbobjects/CDCADCDeltaPedestals.h>
39 #include <cdc/dbobjects/CDCFEElectronics.h>
40 #include <cdc/dbobjects/CDCEDepToADCConversions.h>
41 #include <cdc/dbobjects/CDCWireHitRequirements.h>
42 #include <cdc/dbobjects/CDCCrossTalkLibrary.h>
43 
44 #include <cdc/geometry/CDCGeometryPar.h>
45 #include <TFile.h>
46 #include <TTreeReader.h>
47 #include <TH1F.h>
48 #include <iostream>
49 #include <fstream>
50 
51 #include <boost/iostreams/filtering_stream.hpp>
52 #include <boost/iostreams/device/file.hpp>
53 #include <boost/iostreams/filter/gzip.hpp>
54 
55 #include <boost/property_tree/ptree.hpp>
56 #include <boost/property_tree/json_parser.hpp>
57 
58 using namespace std;
59 using namespace Belle2;
60 
61 void CDCDatabaseImporter::importTimeZero(std::string fileName)
62 {
63  std::ifstream stream;
64  stream.open(fileName.c_str());
65  if (!stream) {
66  B2ERROR("openFile: " << fileName << " *** failed to open");
67  return;
68  }
69  B2INFO(fileName << ": open for reading");
70 
72  tz.construct();
73 
74  int iL(0);
75  int iC(0);
76  double t0(0.);
77  int nRead(0);
78 
79  while (true) {
80  stream >> iL >> iC >> t0;
81  if (stream.eof()) break;
82  ++nRead;
83  WireID wire(iL, iC);
84  tz->setT0(wire, t0);
85  // if (m_debug) {
86  // std::cout << iL << " " << iC << " " << t0 << std::endl;
87  // }
88  }
89  stream.close();
90 
91  if (nRead != nSenseWires) B2FATAL("#lines read-in (=" << nRead << ") is inconsistent with total #sense wires (=" << nSenseWires <<
92  ") !");
93 
94  IntervalOfValidity iov(m_firstExperiment, m_firstRun,
95  m_lastExperiment, m_lastRun);
96  tz.import(iov);
97 
98  B2RESULT("Time zero table imported to database.");
99 
100 }
101 
102 
103 void CDCDatabaseImporter::importChannelMap(std::string fileName)
104 {
105  std::ifstream stream;
106  stream.open(fileName.c_str());
107  if (!stream) {
108  B2ERROR("openFile: " << fileName << " *** failed to open");
109  return;
110  }
111  B2INFO(fileName << ": open for reading");
112 
114 
115  int isl;
116  int il;
117  int iw;
118  int iBoard;
119  int iCh;
120 
121  while (!stream.eof()) {
122  stream >> isl >> il >> iw >> iBoard >> iCh;
123  cm.appendNew(isl, il, iw, iBoard, iCh);
124  }
125  stream.close();
126 
127  IntervalOfValidity iov(m_firstExperiment, m_firstRun,
128  m_lastExperiment, m_lastRun);
129 
130  cm.import(iov);
131 
132  B2RESULT("Channel map imported to database.");
133 
134 }
135 
136 
137 void CDCDatabaseImporter::importFEElectronics(std::string fileName)
138 {
139  std::ifstream stream;
140  stream.open(fileName.c_str());
141  if (!stream) {
142  B2ERROR("openFile: " << fileName << " *** failed to open");
143  return;
144  }
145  B2INFO(fileName << ": open for reading");
146 
148 
149  // short width, delay, aTh, tThmV, tTheV, l1late;
150  short ib, width, delay, aTh, tThmV;
151 
152  // int i=-1;
153  while (stream >> ib) {
154  // stream >> delay >> aTh >> tThmV >> tTheV >> l1late;
155  stream >> width >> delay >> aTh >> tThmV;
156  // ++i;
157  // std::cout << i <<" "<< width << std::endl;
158  // cf.appendNew(width, delay, aTh, tThmV, tTheV, l1late);
159  cf.appendNew(ib, width, delay, aTh, tThmV);
160  }
161  stream.close();
162 
163  IntervalOfValidity iov(m_firstExperiment, m_firstRun,
164  m_lastExperiment, m_lastRun);
165 
166  cf.import(iov);
167 
168  B2RESULT("FEEElectronics imported to database.");
169 }
170 
171 
172 void CDCDatabaseImporter::importEDepToADC(std::string fileName)
173 {
174  std::ifstream stream;
175  stream.open(fileName.c_str());
176  if (!stream) {
177  B2FATAL("openFile: " << fileName << " *** failed to open");
178  return;
179  }
180  B2INFO(fileName << ": open for reading");
181 
183  etoa.construct();
184 
185  unsigned short paramMode(0), nParams(0);
186  stream >> paramMode >> nParams;
187  etoa->setParamMode(paramMode);
188 
189  unsigned short groupId(0);
190  stream >> groupId;
191  B2INFO(paramMode << " " << nParams << " " << groupId);
192  if (groupId > 1) B2FATAL("invalid groupId now !");
193  etoa->setGroupID(groupId);
194 
195  unsigned short id = 0;
196  std::vector<float> coeffs(nParams);
197  int nRead = 0;
198 
199  while (stream >> id) {
200  for (unsigned short i = 0; i < nParams; ++i) {
201  stream >> coeffs[i];
202  }
203  ++nRead;
204  etoa->setParams(id, coeffs);
205  }
206  stream.close();
207 
208  unsigned short nId = 9;
209  if (groupId == 1) {
210  nId = MAX_N_SLAYERS;
211  } else if (groupId == 2) {
212  nId = 14336;
213  }
214  if (nRead != nId) B2FATAL("#lines read-in (=" << nRead << ") is not equal #ids (=" << nId << ") !");
215 
216  IntervalOfValidity iov(m_firstExperiment, m_firstRun,
217  m_lastExperiment, m_lastRun);
218  etoa.import(iov);
219  B2RESULT("EDep-toADC table imported to database.");
220 }
221 
222 
223 void CDCDatabaseImporter::importBadWire(std::string fileName)
224 {
225  std::ifstream stream;
226  stream.open(fileName.c_str());
227  if (!stream) {
228  B2FATAL("openFile: " << fileName << " *** failed to open");
229  return;
230  }
231  B2INFO(fileName << ": open for reading");
232 
234  bw.construct();
235 
236  int iL(0), iC(0), nRead(0);
237  double effi(0.);
238 
239  while (true) {
240  stream >> iL >> iC >> effi;
241  if (stream.eof()) break;
242  ++nRead;
243  bw->setWire(WireID(iL, iC), effi);
244  // if (m_debug) {
245  // std::cout << iL << " " << iC << " " << effi << std::endl;
246  // }
247  }
248  stream.close();
249 
250  if (nRead > static_cast<int>(nSenseWires)) B2FATAL("#lines read-in (=" << nRead << ") is larger than #sense wires (=" << nSenseWires
251  << ") !");
252 
253  IntervalOfValidity iov(m_firstExperiment, m_firstRun,
254  m_lastExperiment, m_lastRun);
255  bw.import(iov);
256  B2RESULT("BadWire table imported to database.");
257 }
258 
259 
260 void CDCDatabaseImporter::importPropSpeed(std::string fileName)
261 {
262  std::ifstream stream;
263  stream.open(fileName.c_str());
264  if (!stream) {
265  B2FATAL("openFile: " << fileName << " *** failed to open");
266  return;
267  }
268  B2INFO(fileName << ": open for reading");
269 
271  ps.construct();
272 
273  int iCL(0), nRead(0);
274  double speed(0.);
275 
276  while (true) {
277  stream >> iCL >> speed;
278  if (stream.eof()) break;
279  ++nRead;
280  // ps->setSpeed(speed);
281  ps->setSpeed(iCL, speed);
282  // ps->setSpeed(iCL, speed);
283  // if (m_debug) {
284  // std::cout << iCL << " " << value << std::endl;
285  // }
286  }
287  stream.close();
288 
289  if (nRead != MAX_N_SLAYERS) B2FATAL("#lines read-in (=" << nRead << ") is no equal #sense layers (=" << MAX_N_SLAYERS << ") !");
290 
291  IntervalOfValidity iov(m_firstExperiment, m_firstRun,
292  m_lastExperiment, m_lastRun);
293  ps.import(iov);
294  B2RESULT("PropSpeed table imported to database.");
295 }
296 
297 
298 void CDCDatabaseImporter::importTimeWalk(std::string fileName)
299 {
300  std::ifstream stream;
301  stream.open(fileName.c_str());
302  if (!stream) {
303  B2FATAL("openFile: " << fileName << " *** failed to open");
304  return;
305  }
306  B2INFO(fileName << ": open for reading");
307 
309  tw.construct();
310 
311  unsigned short mode(0), nParams(0);
312  stream >> mode >> nParams;
313  tw->setTwParamMode(mode);
314 
315  unsigned short iBoard(0);
316  std::vector<float> coeffs(nParams);
317  int nRead(0);
318 
319  while (stream >> iBoard) {
320  for (unsigned short i = 0; i < nParams; ++i) {
321  stream >> coeffs[i];
322  }
323  ++nRead;
324  tw->setTimeWalkParams(iBoard, coeffs);
325  // if (m_debug) {
326  // std::cout << iBoard << " " << coeff << std::endl;
327  // }
328  }
329  stream.close();
330 
331  if (nRead != nBoards) B2FATAL("#lines read-in (=" << nRead << ") is not equal #boards (=" << nBoards << ") !");
332 
333  IntervalOfValidity iov(m_firstExperiment, m_firstRun,
334  m_lastExperiment, m_lastRun);
335  tw.import(iov);
336  B2RESULT("Time-walk coeff. table imported to database.");
337 }
338 
339 
340 void CDCDatabaseImporter::importXT(std::string fileName)
341 {
343  xt.construct();
344 
345  //read alpha bins
346  // std::ifstream ifs;
347  // ifs.open(fileName.c_str());
348  boost::iostreams::filtering_istream ifs;
349  if ((fileName.rfind(".gz") != string::npos) && (fileName.length() - fileName.rfind(".gz") == 3)) {
350  ifs.push(boost::iostreams::gzip_decompressor());
351  }
352  ifs.push(boost::iostreams::file_source(fileName));
353  if (!ifs) {
354  B2FATAL("openFile: " << fileName << " *** failed to open");
355  }
356  B2INFO(fileName << ": open for reading");
357 
358  const double degrad = M_PI / 180.;
359  const double raddeg = 180. / M_PI;
360 
361  unsigned short nAlphaBins = 0;
362  if (ifs >> nAlphaBins) {
363  if (nAlphaBins == 0 || nAlphaBins > maxNAlphaPoints) B2FATAL("Fail to read alpha bins !");
364  } else {
365  B2FATAL("Fail to read alpha bins !");
366  }
367  std::array<float, 3> alpha3;
368  for (unsigned short i = 0; i < nAlphaBins; ++i) {
369  for (unsigned short j = 0; j < 3; ++j) {
370  ifs >> alpha3[j];
371  alpha3[j] *= degrad;
372  }
373  xt->setAlphaBin(alpha3);
374  }
375 
376  //read theta bins
377  unsigned short nThetaBins = 0;
378  if (ifs >> nThetaBins) {
379  if (nThetaBins == 0 || nThetaBins > maxNThetaPoints) B2FATAL("Fail to read theta bins !");
380  } else {
381  B2FATAL("Fail to read theta bins !");
382  }
383  std::array<float, 3> theta3;
384 
385  for (unsigned short i = 0; i < nThetaBins; ++i) {
386  for (unsigned short j = 0; j < 3; ++j) {
387  ifs >> theta3[j];
388  theta3[j] *= degrad;
389  }
390  xt->setThetaBin(theta3);
391  }
392 
393 
394  //read xt params.
395  /* std::ifstream ifs;
396  ifs.open(fileName.c_str());
397  if (!ifs) {
398  B2FATAL("openFile: " << fileName << " *** failed to open");
399  return;
400  }
401  B2INFO(fileName << ": open for reading");
402  */
403  short xtParamMode, np;
404  unsigned short iCL, iLR;
405  const unsigned short npx = nXTParams - 1;
406  double xtc[npx];
407  double theta, alpha, dummy1;
408  unsigned nRead = 0;
409 
410  ifs >> xtParamMode >> np;
411  if (xtParamMode < 0 || xtParamMode > 1) B2FATAL("Invalid xt param mode read !");
412  if (np <= 0 || np > npx) B2FATAL("No. of xt-params. outside limits !");
413 
414  xt->setXtParamMode(xtParamMode);
415 
416  const double epsi = 0.1;
417 
418  while (ifs >> iCL) {
419  ifs >> theta >> alpha >> dummy1 >> iLR;
420  for (int i = 0; i < np; ++i) {
421  ifs >> xtc[i];
422  }
423  ++nRead;
424 
425  int ialpha = -99;
426  for (unsigned short i = 0; i < nAlphaBins; ++i) {
427  if (fabs(alpha - xt->getAlphaBin(i)[2]*raddeg) < epsi) {
428  ialpha = i;
429  break;
430  }
431  }
432  if (ialpha < 0) B2FATAL("alphas in xt.dat are inconsistent !");
433 
434  int itheta = -99;
435  for (unsigned short i = 0; i < nThetaBins; ++i) {
436  if (fabs(theta - xt->getThetaBin(i)[2]*raddeg) < epsi) {
437  itheta = i;
438  break;
439  }
440  }
441  if (itheta < 0) B2FATAL("thetas in xt.dat are inconsistent !");
442 
443  // std::vector<float> xtbuff = std::vector<float>(np);
444  std::vector<float> xtbuff;
445  for (int i = 0; i < np; ++i) {
446  xtbuff.push_back(xtc[i]);
447  }
448  // std::cout <<"iCL,iLR,ialpha,itheta= " << iCL <<" "<< iLR <<" "<< ialpha <<" "<< itheta << std::endl;
449  xt->setXtParams(iCL, iLR, ialpha, itheta, xtbuff);
450  }
451 
452  // ifs.close();
453  boost::iostreams::close(ifs);
454 
455  IntervalOfValidity iov(m_firstExperiment, m_firstRun,
456  m_lastExperiment, m_lastRun);
457  xt.import(iov);
458  B2RESULT("XT table imported to database.");
459 }
460 
461 void CDCDatabaseImporter::importSigma(std::string fileName)
462 {
464  sg.construct();
465 
466  //read alpha bins
467  std::ifstream ifs;
468  ifs.open(fileName.c_str());
469  if (!ifs) {
470  B2FATAL("openFile: " << fileName << " *** failed to open");
471  }
472  B2INFO(fileName << ": open for reading");
473 
474  const double degrad = M_PI / 180.;
475  const double raddeg = 180. / M_PI;
476 
477  unsigned short nAlphaBins = 0;
478  if (ifs >> nAlphaBins) {
479  if (nAlphaBins == 0 || nAlphaBins > maxNAlphaPoints) B2FATAL("Fail to read alpha bins !");
480  } else {
481  B2FATAL("Fail to read alpha bins !");
482  }
483  std::array<float, 3> alpha3;
484  for (unsigned short i = 0; i < nAlphaBins; ++i) {
485  for (unsigned short j = 0; j < 3; ++j) {
486  ifs >> alpha3[j];
487  alpha3[j] *= degrad;
488  }
489  sg->setAlphaBin(alpha3);
490  }
491 
492  //read theta bins
493  unsigned short nThetaBins = 0;
494  if (ifs >> nThetaBins) {
495  if (nThetaBins == 0 || nThetaBins > maxNThetaPoints) B2FATAL("Fail to read theta bins !");
496  } else {
497  B2FATAL("Fail to read theta bins !");
498  }
499  std::array<float, 3> theta3;
500 
501  for (unsigned short i = 0; i < nThetaBins; ++i) {
502  for (unsigned short j = 0; j < 3; ++j) {
503  ifs >> theta3[j];
504  theta3[j] *= degrad;
505  }
506  sg->setThetaBin(theta3);
507  }
508 
509 
510  //read sigma params.
511  short sgParamMode, np;
512  unsigned short iCL, iLR;
513  const unsigned short npx = nSigmaParams;
514  double sgm[npx];
515  double theta, alpha;
516  unsigned nRead = 0;
517 
518  ifs >> sgParamMode >> np;
519  if (sgParamMode < 0 || sgParamMode > 1) B2FATAL("Invalid sigma param mode read !");
520  if (np <= 0 || np > npx) B2FATAL("No. of sgm-params. outside limits !");
521 
522  sg->setSigmaParamMode(sgParamMode);
523 
524  const double epsi = 0.1;
525 
526  while (ifs >> iCL) {
527  ifs >> theta >> alpha >> iLR;
528  for (int i = 0; i < np; ++i) {
529  ifs >> sgm[i];
530  }
531  ++nRead;
532 
533  int ialpha = -99;
534  for (unsigned short i = 0; i < nAlphaBins; ++i) {
535  if (fabs(alpha - sg->getAlphaBin(i)[2]*raddeg) < epsi) {
536  ialpha = i;
537  break;
538  }
539  }
540  if (ialpha < 0) B2FATAL("alphas in sigma.dat are inconsistent !");
541 
542  int itheta = -99;
543  for (unsigned short i = 0; i < nThetaBins; ++i) {
544  if (fabs(theta - sg->getThetaBin(i)[2]*raddeg) < epsi) {
545  itheta = i;
546  break;
547  }
548  }
549  if (itheta < 0) B2FATAL("thetas in sigma.dat are inconsistent !");
550 
551  // std::vector<float> sgbuff = std::vector<float>(np);
552  std::vector<float> sgbuff;
553  for (int i = 0; i < np; ++i) {
554  sgbuff.push_back(sgm[i]);
555  }
556  // std::cout <<"iCL,iLR,ialpha,itheta= " << iCL <<" "<< iLR <<" "<< ialpha <<" "<< itheta << std::endl;
557  sg->setSigmaParams(iCL, iLR, ialpha, itheta, sgbuff);
558  }
559 
560  ifs.close();
561 
562  IntervalOfValidity iov(m_firstExperiment, m_firstRun,
563  m_lastExperiment, m_lastRun);
564  sg.import(iov);
565  B2RESULT("Sigma table imported to database.");
566 }
567 
568 
569 void CDCDatabaseImporter::importFFactor(std::string fileName)
570 {
571  std::ifstream stream;
572  stream.open(fileName.c_str());
573  if (!stream) {
574  B2FATAL("openFile: " << fileName << " *** failed to open");
575  return;
576  }
577  B2INFO(fileName << ": open for reading");
578 
580  etoa.construct();
581 
582  unsigned short groupId(0), nParams(0);
583  stream >> groupId >> nParams;
584  B2INFO(groupId << " " << nParams);
585  if (groupId != 0) B2FATAL("invalid groupId now !");
586  etoa->setGroupID(groupId);
587 
588  unsigned short id = 0;
589  std::vector<float> coeffs(nParams);
590  int nRead = 0;
591 
592  while (stream >> id) {
593  for (unsigned short i = 0; i < nParams; ++i) {
594  stream >> coeffs[i];
595  }
596  ++nRead;
597  etoa->setFactors(id, coeffs);
598  }
599  stream.close();
600 
601  unsigned short nId = 1;
602  if (nRead != nId) B2FATAL("#lines read-in (=" << nRead << ") is not equal #ids (=" << nId << ") !");
603 
604  IntervalOfValidity iov(m_firstExperiment, m_firstRun,
605  m_lastExperiment, m_lastRun);
606  etoa.import(iov);
607  B2RESULT("Fudge factor table imported to database.");
608 }
609 
610 
611 void CDCDatabaseImporter::importDisplacement(std::string fileName)
612 {
613  //read alpha bins
614  // std::ifstream ifs;
615  // ifs.open(fileName.c_str());
616  boost::iostreams::filtering_istream ifs;
617  if ((fileName.rfind(".gz") != string::npos) && (fileName.length() - fileName.rfind(".gz") == 3)) {
618  ifs.push(boost::iostreams::gzip_decompressor());
619  }
620  ifs.push(boost::iostreams::file_source(fileName));
621  if (!ifs) {
622  B2FATAL("openFile: " << fileName << " *** failed to open");
623  }
624  B2INFO(fileName << ": open for reading");
625 
627 
628  int iL(0), iC(0);
629  const int np = 3;
630  double back[np], fwrd[np];
631  double tension = 0.;
632  unsigned nRead = 0;
633 
634  while (true) {
635  ifs >> iL >> iC;
636  for (int i = 0; i < np; ++i) {
637  ifs >> back[i];
638  }
639  for (int i = 0; i < np; ++i) {
640  ifs >> fwrd[i];
641  }
642  ifs >> tension;
643 
644  if (ifs.eof()) break;
645 
646  ++nRead;
647  WireID wire(iL, iC);
648  TVector3 fwd(fwrd[0], fwrd[1], fwrd[2]);
649  TVector3 bwd(back[0], back[1], back[2]);
650  disp.appendNew(wire, fwd, bwd, tension);
651  }
652 
653  if (nRead != nSenseWires) B2FATAL("CDCDatabaseimporter::importDisplacement: #lines read-in (=" << nRead <<
654  ") is inconsistent with total #sense wires (=" << nSenseWires << ") !");
655 
656  // ifs.close();
657  boost::iostreams::close(ifs);
658 
659  IntervalOfValidity iov(m_firstExperiment, m_firstRun,
660  m_lastExperiment, m_lastRun);
661  disp.import(iov);
662  B2RESULT("Wire displasement table imported to database.");
663 }
664 
665 
666 void CDCDatabaseImporter::importWirPosAlign(std::string fileName)
667 {
668  // std::ifstream ifs;
669  // ifs.open(fileName.c_str());
670  boost::iostreams::filtering_istream ifs;
671  if ((fileName.rfind(".gz") != string::npos) && (fileName.length() - fileName.rfind(".gz") == 3)) {
672  ifs.push(boost::iostreams::gzip_decompressor());
673  }
674  ifs.push(boost::iostreams::file_source(fileName));
675  if (!ifs) {
676  B2FATAL("openFile: " << fileName << " *** failed to open");
677  return;
678  }
679  B2INFO(fileName << ": open for reading");
680 
682  al.construct();
683 
684  int iL(0), iC(0);
685  const int np = 3;
686  double back[np], fwrd[np], tension;
687  unsigned nRead = 0;
688 
689  while (true) {
690  ifs >> iL >> iC;
691  for (int i = 0; i < np; ++i) {
692  ifs >> back[i];
693  }
694  for (int i = 0; i < np; ++i) {
695  ifs >> fwrd[i];
696  }
697  ifs >> tension;
698  if (ifs.eof()) break;
699 
700  ++nRead;
701  WireID wire(iL, iC);
702 
703  for (int i = 0; i < np; ++i) {
704  al->set(wire, CDCAlignment::wireBwdX, back[0]);
705  al->set(wire, CDCAlignment::wireBwdY, back[1]);
706  al->set(wire, CDCAlignment::wireBwdZ, back[2]);
707  al->set(wire, CDCAlignment::wireFwdX, fwrd[0]);
708  al->set(wire, CDCAlignment::wireFwdY, fwrd[1]);
709  al->set(wire, CDCAlignment::wireFwdZ, fwrd[2]);
710  }
711  al->set(wire, CDCAlignment::wireTension, tension);
712  }
713 
714  if (nRead != nSenseWires) B2FATAL("CDCDatabaseimporter::importWirPosAlign: #lines read-in (=" << nRead <<
715  ") is inconsistent with total #sense wires (=" << nSenseWires << ") !");
716 
717  // ifs.close();
718  boost::iostreams::close(ifs);
719 
720  IntervalOfValidity iov(m_firstExperiment, m_firstRun,
721  m_lastExperiment, m_lastRun);
722  al.import(iov);
723  B2RESULT("Wire alignment table imported to database.");
724 }
725 
726 
727 void CDCDatabaseImporter::printChannelMap()
728 {
729 
730  DBArray<CDCChannelMap> channelMaps;
731 
732  for (const auto& cm : channelMaps) {
733  std::cout << cm.getISuperLayer() << " " << cm.getILayer()
734  << " " << cm.getIWire() << " "
735  << cm.getBoardID() << " " << cm.getBoardChannel() << std::endl;
736  }
737 
738 }
739 
740 void CDCDatabaseImporter::printFEElectronics()
741 {
742  DBArray<CDCFEElectronics> fEElectronics;
743  for (const auto& cf : fEElectronics) {
744  std::cout << cf.getBoardID() << " " << cf.getWidthOfTimeWindow() << " " << cf.getTrgDelay() << " " << cf.getADCThresh() << " " <<
745  cf.getTDCThreshInMV() << std::endl;
746  }
747  // << cf.getTDCThreshInMV() << " "
748  // << cf.getTDCThreshInEV() << " "
749  // << cf.getL1TrgLatency() << std::endl;
750 }
751 
752 void CDCDatabaseImporter::printEDepToADC()
753 {
755  etoa->dump();
756 }
757 
758 void CDCDatabaseImporter::printTimeZero()
759 {
760 
761  DBObjPtr<CDCTimeZeros> timeZeros;
762 
763  /* for (const auto& tz : timeZeros) {
764  std::cout << tz.getICLayer() << " " << tz.getIWire() << " "
765  << tz.getT0() << std::endl;
766  }
767  */
768  timeZeros->dump();
769 
770 }
771 
772 void CDCDatabaseImporter::printBadWire()
773 {
775  bw->dump();
776 }
777 
778 void CDCDatabaseImporter::printPropSpeed()
779 {
781  ps->dump();
782 }
783 
784 void CDCDatabaseImporter::printTimeWalk()
785 {
787  tw->dump();
788 }
789 
790 void CDCDatabaseImporter::printXT()
791 {
793  xt->dump();
794 }
795 
796 void CDCDatabaseImporter::printSigma()
797 {
799  sgm->dump();
800 }
801 
802 void CDCDatabaseImporter::printFFactor()
803 {
805  ff->dump();
806 }
807 
808 void CDCDatabaseImporter::printDisplacement()
809 {
810  DBArray<CDCDisplacement> displacements;
811  for (const auto& disp : displacements) {
812  B2INFO(disp.getICLayer() << " " << disp.getIWire() << " "
813  << disp.getXBwd() << " " << disp.getYBwd() << " " << disp.getZBwd() << " "
814  << disp.getXFwd() << " " << disp.getYFwd() << " " << disp.getZFwd() << " " << disp.getTension());
815  }
816 }
817 
818 void CDCDatabaseImporter::printWirPosAlign()
819 {
821  al->dump();
822 }
823 
824 void CDCDatabaseImporter::printWirPosMisalign()
825 {
827  mal->dump();
828 }
829 
830 
831 void CDCDatabaseImporter::importADCDeltaPedestal(std::string fileName)
832 {
833  std::ifstream stream;
834  stream.open(fileName.c_str());
835  if (!stream.is_open()) {
836  B2ERROR("openFile: " << fileName << " *** failed to open");
837  return;
838  }
839  B2INFO(fileName << ": open for reading");
840 
842  dbPed.construct();
843 
844  int iB(0);
845  int iC(0);
846  float ped(0);
847  int nRead(0);
848  int sample(0);
849 
850  while (true) {
851  if (nRead == 0) {
852  stream >> sample;
853  } else {
854  stream >> iB >> iC >> ped;
855  }
856  if (stream.eof()) break;
857  if (nRead == 0) {
858  if (sample == 0) {
859  B2FATAL("sample window is zero !");
860  }
861  dbPed->setSamplingWindow(sample);
862  } else {
863  dbPed->setPedestal(iB, iC, ped);
864  }
865  ++nRead;
866 
867  }
868  stream.close();
869 
870  IntervalOfValidity iov(m_firstExperiment, m_firstRun,
871  m_lastExperiment, m_lastRun);
872  dbPed.import(iov);
873 
874  B2RESULT("ADC delta pedestal table imported to database.");
875 }
876 
877 void CDCDatabaseImporter::importADCDeltaPedestal()
878 {
879 
881  dbPed.construct();
882 
883  IntervalOfValidity iov(m_firstExperiment, m_firstRun,
884  m_lastExperiment, m_lastRun);
885  dbPed.import(iov);
886 
887  B2RESULT("ADC delta pedestal w/ zero imported to database.");
888 }
889 
890 void CDCDatabaseImporter::printADCDeltaPedestal()
891 {
892 
894  dbPed->dump();
895 }
896 
897 void CDCDatabaseImporter::importCDCWireHitRequirements(const std::string& jsonFileName) const
898 {
899 
900  // Create a property tree
901  boost::property_tree::ptree tree;
902 
903  try {
904 
905  // Load the json file in this property tree.
906  B2INFO("Loading json file: " << jsonFileName);
907  boost::property_tree::read_json(jsonFileName, tree);
908 
909  } catch (boost::property_tree::ptree_error& e) {
910  B2FATAL("Error when loading json file: " << e.what());
911  }
912 
914  dbWireHitReq.construct(tree);
915 
916  IntervalOfValidity iov(m_firstExperiment, m_firstRun,
917  m_lastExperiment, m_lastRun);
918  dbWireHitReq.import(iov);
919 
920  B2RESULT("CDCWireHit requirements imported to database.");
921 }
922 
923 void CDCDatabaseImporter::printCDCWireHitRequirements() const
924 {
925 
927  if (dbWireHitReq.isValid()) {
928  dbWireHitReq->dump();
929  } else {
930  B2WARNING("DBObjPtr<CDCWireHitRequirements> not valid for the current run.");
931  }
932 }
933 
934 void CDCDatabaseImporter::importCDCCrossTalkLibrary(const std::string& rootFileName) const
935 {
936  DBImportObjPtr<CDCCrossTalkLibrary> dbCDCCrossTalkLibrary;
937  dbCDCCrossTalkLibrary.construct();
938 
939  TFile fIn = TFile(rootFileName.c_str());
940  TTreeReader reader("my_ttree", &fIn);
941  TTreeReaderValue<UChar_t> Board(reader, "Board");
942  TTreeReaderValue<UChar_t> Channel(reader, "Channel");
943  TTreeReaderValue<Short_t> Asic_ADC0(reader, "Asic_ADC0");
944  TTreeReaderValue<Short_t> Asic_TDC0(reader, "Asic_TDC0");
945  TTreeReaderValue<Short_t> Asic_TOT0(reader, "Asic_TOT0");
946  TTreeReaderValue<Short_t> Asic_ADC1(reader, "Asic_ADC1");
947  TTreeReaderValue<Short_t> Asic_TDC1(reader, "Asic_TDC1");
948  TTreeReaderValue<Short_t> Asic_TOT1(reader, "Asic_TOT1");
949  TTreeReaderValue<Short_t> Asic_ADC2(reader, "Asic_ADC2");
950  TTreeReaderValue<Short_t> Asic_TDC2(reader, "Asic_TDC2");
951  TTreeReaderValue<Short_t> Asic_TOT2(reader, "Asic_TOT2");
952  TTreeReaderValue<Short_t> Asic_ADC3(reader, "Asic_ADC3");
953  TTreeReaderValue<Short_t> Asic_TDC3(reader, "Asic_TDC3");
954  TTreeReaderValue<Short_t> Asic_TOT3(reader, "Asic_TOT3");
955  TTreeReaderValue<Short_t> Asic_ADC4(reader, "Asic_ADC4");
956  TTreeReaderValue<Short_t> Asic_TDC4(reader, "Asic_TDC4");
957  TTreeReaderValue<Short_t> Asic_TOT4(reader, "Asic_TOT4");
958  TTreeReaderValue<Short_t> Asic_ADC5(reader, "Asic_ADC5");
959  TTreeReaderValue<Short_t> Asic_TDC5(reader, "Asic_TDC5");
960  TTreeReaderValue<Short_t> Asic_TOT5(reader, "Asic_TOT5");
961  TTreeReaderValue<Short_t> Asic_ADC6(reader, "Asic_ADC6");
962  TTreeReaderValue<Short_t> Asic_TDC6(reader, "Asic_TDC6");
963  TTreeReaderValue<Short_t> Asic_TOT6(reader, "Asic_TOT6");
964  TTreeReaderValue<Short_t> Asic_ADC7(reader, "Asic_ADC7");
965  TTreeReaderValue<Short_t> Asic_TDC7(reader, "Asic_TDC7");
966  TTreeReaderValue<Short_t> Asic_TOT7(reader, "Asic_TOT7");
967 
968  while (reader.Next()) {
969  asicChannels record{
970  asicChannel{*Asic_TDC0, *Asic_ADC0, *Asic_TOT0},
971  asicChannel{*Asic_TDC1, *Asic_ADC1, *Asic_TOT1},
972  asicChannel{*Asic_TDC2, *Asic_ADC2, *Asic_TOT2},
973  asicChannel{*Asic_TDC3, *Asic_ADC3, *Asic_TOT3},
974  asicChannel{*Asic_TDC4, *Asic_ADC4, *Asic_TOT4},
975  asicChannel{*Asic_TDC5, *Asic_ADC5, *Asic_TOT5},
976  asicChannel{*Asic_TDC6, *Asic_ADC6, *Asic_TOT6},
977  asicChannel{*Asic_TDC7, *Asic_ADC7, *Asic_TOT7}
978  };
979  // Determine ADC of the signal
980  UChar_t asicCh = *Channel % 8;
981  Short_t ADC = record[asicCh].ADC;
982  dbCDCCrossTalkLibrary->addAsicRecord(asicCh, ADC, record);
983  }
984 
985  // Now also get the x-talk probability
986  double probs[8196];
987  TH1F* prob;
988  fIn.GetObject("ProbXTalk", prob);
989  for (size_t a = 1; a <= 8196; a += 1) {
990  probs[a - 1] = prob->GetBinContent(a);
991  }
992  fIn.Close();
993  dbCDCCrossTalkLibrary->setPCrossTalk(probs);
994 
995  dbCDCCrossTalkLibrary->dump(0);
996  IntervalOfValidity iov(m_firstExperiment, m_firstRun,
997  m_lastExperiment, m_lastRun);
998  dbCDCCrossTalkLibrary.import(iov);
999  B2RESULT("CDCCrossTalkLibrary requirements imported to database.");
1000 }
1001 
1002 void CDCDatabaseImporter::printCDCCrossTalkLibrary() const
1003 {
1004  DBObjPtr<CDCCrossTalkLibrary> dbCDCCrossTalkLib;
1005  if (dbCDCCrossTalkLib.isValid()) {
1006  dbCDCCrossTalkLib->dump(1);
1007  } else {
1008  B2ERROR("DBObjPtr<CDCCrossTalkLibrary> not valid for the current run.");
1009  }
1010 }
1011 
1012 void CDCDatabaseImporter::testCDCCrossTalkLibrary(bool spotChecks) const
1013 {
1014  DBObjPtr<CDCCrossTalkLibrary> dbCDCCrossTalkLib;
1015 
1016  if (dbCDCCrossTalkLib.isValid()) {
1017 
1018  if (! spotChecks) {
1019  B2INFO("Performing CDCCrossTalkLibrary checks");
1020  auto timer = new Utils::Timer("CDCCrossTalkLibrary checks took"); // use "new" to avoid cpp-check warning
1021  int counter = 0;
1022  int size = 0;
1023  for (Short_t ADC = 0; ADC < 8196; ADC += 1) {
1024  for (Short_t channel = 0; channel < 48; channel += 1) {
1025  for (size_t rep = 0; rep < 100; rep += 1) {
1026  auto xtalk = dbCDCCrossTalkLib->getLibraryCrossTalk(channel, 4999, ADC, 5, 0, false);
1027  counter += 1;
1028  size += xtalk.size();
1029  }
1030  }
1031  }
1032  B2INFO("CDCCrossTalkLibrary called " << counter << " times. Total number of cross talk hits " << size);
1033  delete timer;
1034  return;
1035  }
1036 
1037 
1038  Short_t ADC_spot_checks[5] = {2, 100, 500, 1000, 5000};
1039  for (auto ADC : ADC_spot_checks) {
1040  B2INFO("CHECK ADC=" << ADC);
1041 
1042  size_t NRep = ADC < 50 ? 100 : 10;
1043  for (size_t rep = 0; rep < NRep; rep += 1) {
1044  auto xtalk = dbCDCCrossTalkLib->getLibraryCrossTalk(0, 4999, ADC, 5, 0, true);
1045  B2INFO("Size = " << xtalk.size());
1046  for (auto [channel, rec] : xtalk) {
1047  B2INFO("Channel:" << channel << " TDC,ADC,TOT:" << rec.TDC << "," << rec.ADC << "," << rec.TOT);
1048  }
1049  }
1050  }
1051  } else {
1052  B2ERROR("DBObjPtr<CDCCrossTalkLibrary> not valid for the current run.");
1053  }
1054 }
1055 
1056 
1057 //Note; the following function is no longer needed
1058 #if 0
1059 void CDCDatabaseImporter::importWirPosMisalign(std::string fileName)
1060 {
1061  std::ifstream ifs;
1062  ifs.open(fileName.c_str());
1063  if (!ifs) {
1064  B2FATAL("openFile: " << fileName << " *** failed to open");
1065  return;
1066  }
1067  B2INFO(fileName << ": open for reading");
1068 
1070  mal.construct();
1071 
1072  int iL(0), iC(0);
1073  const int np = 3;
1074  double back[np], fwrd[np], tension;
1075  unsigned nRead = 0;
1076 
1077  while (true) {
1078  ifs >> iL >> iC;
1079  for (int i = 0; i < np; ++i) {
1080  ifs >> back[i];
1081  }
1082  for (int i = 0; i < np; ++i) {
1083  ifs >> fwrd[i];
1084  }
1085  ifs >> tension;
1086  if (ifs.eof()) break;
1087 
1088  ++nRead;
1089  WireID wire(iL, iC);
1090 
1091  for (int i = 0; i < np; ++i) {
1092  mal->set(wire, CDCMisalignment::wireBwdX, back[0]);
1093  mal->set(wire, CDCMisalignment::wireBwdY, back[1]);
1094  mal->set(wire, CDCMisalignment::wireBwdZ, back[2]);
1095  mal->set(wire, CDCMisalignment::wireFwdX, fwrd[0]);
1096  mal->set(wire, CDCMisalignment::wireFwdY, fwrd[1]);
1097  mal->set(wire, CDCMisalignment::wireFwdZ, fwrd[2]);
1098  }
1099  mal->set(wire, CDCMisalignment::wireTension, tension);
1100  }
1101 
1102  if (nRead != nSenseWires) B2FATAL("CDCDatabaseimporter::importWirPosMisalign: #lines read-in (=" << nRead <<
1103  ") is inconsistent with total #sense wires (=" << nSenseWires << ") !");
1104 
1105  ifs.close();
1106 
1107  IntervalOfValidity iov(m_firstExperiment, m_firstRun,
1108  m_lastExperiment, m_lastRun);
1109  mal.import(iov);
1110  B2RESULT("Wire misalignment table imported to database.");
1111 }
1112 #endif
1113 
Belle2::IntervalOfValidity
A class that describes the interval of experiments/runs for which an object in the database is valid.
Definition: IntervalOfValidity.h:35
Belle2::WireID
Class to identify a wire inside the CDC.
Definition: WireID.h:44
Belle2::CDCSpaceResols::getAlphaBin
const array3 & getAlphaBin(unsigned short i) const
Get i-th alpha-angle bin info.
Definition: CDCSpaceResols.h:172
Belle2::CDCEDepToADCConversions::setGroupID
void setGroupID(unsigned short mode)
Set group id (parameterized per group) id=0: superLayerID; =1: layerID; =1: wireID.
Definition: CDCEDepToADCConversions.h:55
Belle2::CDCCrossTalkLibrary::setPCrossTalk
void setPCrossTalk(const double *probs)
Store x-talk probability.
Definition: CDCCrossTalkLibrary.h:208
Belle2::DBImportArray::appendNew
T * appendNew()
Construct a new T object at the end of the array.
Definition: DBImportArray.h:77
prepareAsicCrosstalkSimDB.e
e
aux.
Definition: prepareAsicCrosstalkSimDB.py:53
Belle2::CDCXtRelations::setThetaBin
void setThetaBin(const array3 &theta)
Set theta-angle bin (rad)
Definition: CDCXtRelations.h:76
Belle2::DBObjCalibrationConstMapBase::set
virtual void set(baseType id, baseType param, double value)
Set parameter correction for given WireID and parameter number.
Definition: DBObjCalibrationConstMapBase.cc:31
Belle2::CDCXtRelations::setAlphaBin
void setAlphaBin(const array3 &alpha)
Set alpha-angle bin (rad)
Definition: CDCXtRelations.h:62
Belle2::CDCXtRelations::getThetaBin
const array3 & getThetaBin(unsigned short i) const
Get i-th theta-angle bin info.
Definition: CDCXtRelations.h:216
Belle2::CDCFudgeFactorsForSigma::setFactors
void setFactors(unsigned short id, const std::vector< float > &factors)
Set the factors in the list.
Definition: CDCFudgeFactorsForSigma.h:57
Belle2::CDCSpaceResols::setAlphaBin
void setAlphaBin(const array3 &alpha)
Set alpha-angle bin (rad)
Definition: CDCSpaceResols.h:60
Belle2::CDCSpaceResols::setSigmaParamMode
void setSigmaParamMode(unsigned short mode)
Set sigma parameterization mode.
Definition: CDCSpaceResols.h:95
Belle2::CDCTimeZeros::setT0
void setT0(unsigned short iCLayer, unsigned short iWire, float t0)
Set t0 in the list.
Definition: CDCTimeZeros.h:50
Belle2::DBArray
Class for accessing arrays of objects in the database.
Definition: DBArray.h:36
Belle2::CDCEDepToADCConversions::setParams
void setParams(unsigned short id, const std::vector< float > &params)
Set the conv.
Definition: CDCEDepToADCConversions.h:65
Belle2::DBImportObjPtr::construct
void construct(Args &&... params)
Construct an object of type T in this DBImportObjPtr using the provided constructor arguments.
Definition: DBImportObjPtr.h:57
Belle2::DBImportBase::import
bool import(const IntervalOfValidity &iov)
Import the object to database.
Definition: DBImportBase.cc:38
Belle2::asicChannel::ADC
Short_t ADC
ADC info
Definition: CDCCrossTalkClasses.h:25
Belle2::DBObjPtr
Class for accessing objects in the database.
Definition: DBObjPtr.h:31
Belle2::Utils::Timer
Small helper class that prints its lifetime when destroyed.
Definition: Utils.h:72
Belle2::CDCSpaceResols::getThetaBin
const array3 & getThetaBin(unsigned short i) const
Get i-th theta-angle bin info.
Definition: CDCSpaceResols.h:188
Belle2::asicChannels
array< asicChannel, 8 > asicChannels
fixed sized array of ASIC channels
Definition: CDCCrossTalkClasses.h:30
Belle2::CDCADCDeltaPedestals::setPedestal
void setPedestal(int board, int ch, float pedestal)
Set ADC pedestals in the list.
Definition: CDCADCDeltaPedestals.h:56
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::asicChannel
record to be used to store ASIC info
Definition: CDCCrossTalkClasses.h:23
Belle2::DBImportObjPtr
Class for importing a single object to the database.
Definition: DBImportObjPtr.h:33
Belle2::CDCBadWires::setWire
void setWire(const WireID &wid, double eff=0)
Set the wire in the list.
Definition: CDCBadWires.h:50
Belle2::CDCXtRelations::setXtParamMode
void setXtParamMode(unsigned short mode)
Set xt parameterization mode.
Definition: CDCXtRelations.h:97
Belle2::CDCXtRelations::setXtParams
void setXtParams(const XtID xtID, const std::vector< float > &params)
Set xt parameters for the specified id.
Definition: CDCXtRelations.h:105
Belle2::CDCADCDeltaPedestals::setSamplingWindow
void setSamplingWindow(unsigned short sample)
Set time window for sampling.
Definition: CDCADCDeltaPedestals.h:46
Belle2::CDCSpaceResols::setSigmaParams
void setSigmaParams(const SigmaID sigmaID, const std::vector< float > &params)
Set sigma parameters for the specified id.
Definition: CDCSpaceResols.h:103
Belle2::DBAccessorBase::isValid
bool isValid() const
Check whether a valid object was obtained from the database.
Definition: DBAccessorBase.h:75
Belle2::CDCCrossTalkLibrary::addAsicRecord
void addAsicRecord(const Short_t channel, const Short_t ADC, const asicChannels &asicInfo)
Add a new ASIC record to the library.
Definition: CDCCrossTalkLibrary.h:47
Belle2::CDCSpaceResols::setThetaBin
void setThetaBin(const array3 &theta)
Set theta-angle bin (rad)
Definition: CDCSpaceResols.h:74
Belle2::DBImportArray
Class for importing array of objects to the database.
Definition: DBImportArray.h:35
Belle2::CDCCrossTalkLibrary::dump
void dump(int verbosity) const
Print out contents of the library.
Definition: CDCCrossTalkLibrary.h:163
Belle2::CDCXtRelations::getAlphaBin
const array3 & getAlphaBin(unsigned short i) const
Get i-th alpha-angle bin info.
Definition: CDCXtRelations.h:200
Belle2::CDCFudgeFactorsForSigma::setGroupID
void setGroupID(unsigned short mode)
Set group id (factors per group) id=0: all-wires; >=1: not ready.
Definition: CDCFudgeFactorsForSigma.h:47
Belle2::CDCEDepToADCConversions::setParamMode
void setParamMode(unsigned short mode)
Set conversion parameterization mode.
Definition: CDCEDepToADCConversions.h:46