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