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