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