Belle II Software  release-05-01-25
KLMLikelihoodParametersImporter.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2015 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Yinghui Guan *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 /* Own header. */
12 #include <klm/calibration/KLMLikelihoodParametersImporter.h>
13 
14 /* KLM headers. */
15 #include <klm/dbobjects/KLMLikelihoodParameters.h>
16 #include <klm/muid/MuidElementNumbers.h>
17 
18 /* Belle 2 headers. */
19 #include <framework/database/DBImportObjPtr.h>
20 #include <framework/database/DBObjPtr.h>
21 #include <framework/gearbox/GearDir.h>
22 #include <framework/database/IntervalOfValidity.h>
23 #include <framework/logging/Logger.h>
24 
25 /* C++ headers. */
26 #include <fstream>
27 #include <string>
28 #include <vector>
29 
30 using namespace std;
31 using namespace Belle2;
32 
33 void KLMLikelihoodParametersImporter::writeLikelihoodParameters()
34 {
35  B2WARNING("The method KLMLikelihoodParametersImporter::writeMuidParameters() is temporary unavailable, sorry! :(");
36  return;
38  DBImportObjPtr<KLMLikelihoodParameters> likelihoodParameters;
39  likelihoodParameters.construct();
40  vector<string> const hypotheses = {"Positron", "Electron" , "Deuteron", "Antideuteron", "Proton", "Antiproton", "PionPlus", "PionMinus", "KaonPlus", "KaonMinus", "MuonPlus", "MuonMinus" };
41  for (unsigned int hypothesis = 0; hypothesis < hypotheses.size(); hypothesis++) {
42  GearDir content("/Detector/Muid/MuidParameters//Experiment[@exp=\"0\"]/");
43  content.append(hypotheses[hypothesis]);
44  for (int outcome = 1; outcome <= MuidElementNumbers::getMaximalOutcome(); ++outcome) {
45  GearDir outcomeContent(content);
46  outcomeContent.append((boost::format("/LayerProfile/Outcome[@outcome=\"%1%\"]/") % (outcome)).str());
47  for (int lastLayer = 0; lastLayer <= MuidElementNumbers::getMaximalBarrelLayer(); ++lastLayer) {
48  if (!(MuidElementNumbers::checkExtrapolationOutcome(outcome, lastLayer)))
49  break;
50  std::vector<double> layerPDF = outcomeContent.getArray((boost::format("LastLayer[@layer=\"%1%\"]") % (lastLayer)).str());
51  likelihoodParameters->setLongitudinalPDF(hypothesis, outcome, lastLayer, layerPDF);
52  }
53  }
54  for (int detector = 0; detector <= MuidElementNumbers::getMaximalDetector(); ++detector) {
55  GearDir detectorContent(content);
56  if (detector == 0) detectorContent.append("/TransversePDF/BarrelAndEndcap");
57  if (detector == 1) detectorContent.append("/TransversePDF/BarrelOnly");
58  if (detector == 2) detectorContent.append("/TransversePDF/EndcapOnly");
59  for (int halfNdof = 1; halfNdof <= MuidElementNumbers::getMaximalHalfNdof(); ++halfNdof) {
60  double reducedChiSquaredThreshold = detectorContent.getDouble((boost::format("DegreesOfFreedom[@ndof=\"%1%\"]/Tail/Threshold") %
61  (2 * halfNdof)).str());
62  double reducedChiSquaredScaleY = detectorContent.getDouble((boost::format("DegreesOfFreedom[@ndof=\"%1%\"]/Tail/ScaleY") %
63  (2 * halfNdof)).str());
64  double reducedChiSquaredScaleX = detectorContent.getDouble((boost::format("DegreesOfFreedom[@ndof=\"%1%\"]/Tail/ScaleX") %
65  (2 * halfNdof)).str());
66  std::vector<double> reducedChiSquaredPDF = detectorContent.getArray((boost::format("DegreesOfFreedom[@ndof=\"%1%\"]/Histogram") %
67  (2 * halfNdof)).str());
68  likelihoodParameters->setTransversePDF(hypothesis, detector, halfNdof * 2, reducedChiSquaredPDF);
69  likelihoodParameters->setTransverseThreshold(hypothesis, detector, halfNdof * 2, reducedChiSquaredThreshold);
70  likelihoodParameters->setTransverseScaleY(hypothesis, detector, halfNdof * 2, reducedChiSquaredScaleY);
71  likelihoodParameters->setTransverseScaleX(hypothesis, detector, halfNdof * 2, reducedChiSquaredScaleX);
72  }
73  }
74  }
75  IntervalOfValidity Iov(0, 0, -1, -1);
76  likelihoodParameters.import(Iov);
77 }
78 
79 void KLMLikelihoodParametersImporter::readLikelihoodParameters()
80 {
81  DBObjPtr<KLMLikelihoodParameters> likelihoodParameters;
82  vector<string> const hypotheses = {"Positron", "Electron" , "Deuteron", "Antideuteron", "Proton", "Antiproton", "PionPlus", "PionMinus", "KaonPlus", "KaonMinus", "MuonPlus", "MuonMinus" };
83  for (unsigned int hypothesis = 0; hypothesis < hypotheses.size(); hypothesis++) {
84  B2INFO(" hypothesisName " << hypotheses[hypothesis]);
85  for (int outcome = 1; outcome <= MuidElementNumbers::getMaximalOutcome(); ++outcome) {
86  B2INFO(" outcome " << outcome);
87  for (int lastLayer = 0; lastLayer <= MuidElementNumbers::getMaximalBarrelLayer(); ++lastLayer) {
88  B2INFO(" lastLayer " << lastLayer);
89  if (!(MuidElementNumbers::checkExtrapolationOutcome(outcome, lastLayer)))
90  break;
91  std::vector<double> layerPDF = likelihoodParameters->getLongitudinalPDF(hypothesis, outcome, lastLayer);
92  B2INFO(" layerPDF: ");
93  for (unsigned int layer = 0; layer < layerPDF.size(); ++layer) {
94  B2INFO(layerPDF[layer] << " , ");
95  }
96  }
97  }
98  const char* detectorNames[] = {"BarrelAndEndcap", "BarrelOnly", "EndcapOnly"};
99  for (int detector = 0; detector <= MuidElementNumbers::getMaximalDetector(); ++detector) {
100  B2INFO(" detectorName " << detectorNames[detector]);
101  for (int halfNdof = 1; halfNdof <= MuidElementNumbers::getMaximalHalfNdof(); ++halfNdof) {
102  B2INFO(" Ndof " << halfNdof * 2);
103  B2INFO(" ReducedChiSquaredThreshold " << likelihoodParameters->getTransverseThreshold(hypothesis, detector, halfNdof * 2));
104  B2INFO(" ReducedChiSquaredScaleY " << likelihoodParameters->getTransverseScaleY(hypothesis, detector, halfNdof * 2));
105  B2INFO(" ReducedChiSquaredScaleX " << likelihoodParameters->getTransverseScaleX(hypothesis, detector, halfNdof * 2));
106  std::vector<double> reducedChiSquaredPDF = likelihoodParameters->getTransversePDF(hypothesis, detector, halfNdof * 2);
107  if (reducedChiSquaredPDF.size() != MuidElementNumbers::getSizeReducedChiSquared()) {
108  B2ERROR("KLMLikelihoodParametersImporter::TransversePDF vector for hypothesis " << hypotheses[hypothesis] << " detector " <<
109  detectorNames[detector]
110  << " has " << reducedChiSquaredPDF.size() << " entries; should be " << MuidElementNumbers::getSizeReducedChiSquared());
111  } else {
112  for (int i = 0; i < MuidElementNumbers::getSizeReducedChiSquared(); ++i) {
113  B2INFO(" PDF " << reducedChiSquaredPDF[i]);
114  }
115  }
116  }
117  }
118  }
119 }
120 
Belle2::IntervalOfValidity
A class that describes the interval of experiments/runs for which an object in the database is valid.
Definition: IntervalOfValidity.h:35
Belle2::KLMLikelihoodParameters::setTransverseThreshold
void setTransverseThreshold(int hypothesis, int detector, int degreesOfFreedom, const double threshold)
Set the transverse probability density function (analytical): threshold for specific hypothesis,...
Definition: KLMLikelihoodParameters.h:89
Belle2::DBImportObjPtr::construct
void construct(Args &&... params)
Construct an object of type T in this DBImportObjPtr using the provided constructor arguments.
Definition: DBImportObjPtr.h:57
Belle2::DBImportBase::import
bool import(const IntervalOfValidity &iov)
Import the object to database.
Definition: DBImportBase.cc:38
Belle2::DBObjPtr
Class for accessing objects in the database.
Definition: DBObjPtr.h:31
Belle2::gearbox::Interface::getDouble
double getDouble(const std::string &path="") const noexcept(false)
Get the parameter path as a double.
Definition: Interface.cc:51
Belle2::KLMLikelihoodParameters::setTransversePDF
void setTransversePDF(int hypothesis, int detector, int degreesOfFreedom, const std::vector< double > &params)
Set the transverse probability density function for specific hypothesis, detector and degrees of free...
Definition: KLMLikelihoodParameters.h:76
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::GearDir
GearDir is the basic class used for accessing the parameter store.
Definition: GearDir.h:41
Belle2::GearDir::append
void append(const std::string &path)
Append something to the current path, modifying the GearDir in place.
Definition: GearDir.h:62
Belle2::DBImportObjPtr
Class for importing a single object to the database.
Definition: DBImportObjPtr.h:33
Belle2::gearbox::Interface::getArray
std::vector< double > getArray(const std::string &path) const noexcept(false)
Get the parameter path as a list of double values converted to the standard unit.
Definition: Interface.cc:133
Belle2::KLMLikelihoodParameters::setLongitudinalPDF
void setLongitudinalPDF(int hypothesis, int outcome, int lastLayer, const std::vector< double > &params)
Set the longitudinal probability density function for specific hypothesis, outcome and last layer.
Definition: KLMLikelihoodParameters.h:63
Belle2::KLMLikelihoodParameters::setTransverseScaleX
void setTransverseScaleX(int hypothesis, int detector, int degreesOfFreedom, const double scaleX)
Set the transverse probability density function (analytical): horizontal scale for specific hypothesi...
Definition: KLMLikelihoodParameters.h:102
Belle2::KLMLikelihoodParameters::setTransverseScaleY
void setTransverseScaleY(int hypothesis, int detector, int degreesOfFreedom, const double scaleY)
Set the transverse probability density function (analytical): vertical scale for specific hypothesis,...
Definition: KLMLikelihoodParameters.h:115