11 #include <mdst/dataobjects/PIDLikelihood.h>
12 #include <framework/logging/Logger.h>
16 #include <TParticlePDG.h>
25 PIDLikelihood::PIDLikelihood()
27 for (
unsigned short i = 0; i < Const::PIDDetectors::c_size; i++) {
28 for (
unsigned int k = 0; k < Const::ChargedStable::c_SetSize; k++) {
39 int index = Const::PIDDetectors::set().getIndex(det);
41 B2ERROR(
"PIDLikelihood::setLogLikelihood: detector is not a PID device");
44 if (logl != logl or logl == INFINITY) {
45 B2ERROR(
"PIDLikelihood::setLogLikelihood: log-likelihood for detector " << det <<
" is " << logl <<
46 " (i.e. +inf or NaN)! Ignoring this value. (" << Const::CDC <<
"=CDC, " << Const::TOP <<
"=TOP, " << Const::ARICH <<
"=ARICH, " <<
47 Const::ECL <<
"=ECL)");
52 m_logl[index][part.getIndex()] = logl;
60 for (
unsigned int index = 0; index < Const::PIDDetectorSet::set().size(); ++index) {
61 if (set.
contains(Const::PIDDetectorSet::set()[index]))
62 result += m_logl[index][part.getIndex()];
74 B2ERROR(
"PIDLikelihood::probability argument 'ratio' is given with negative value");
77 if (ratio == 0)
return 0;
79 double dlogl = getLogL(p2, set) - getLogL(p1, set);
82 double elogl = exp(dlogl);
83 res = ratio / (ratio + elogl);
85 double elogl = exp(-dlogl) * ratio;
86 res = elogl / (1.0 + elogl);
89 if (std::isfinite(res))
95 const double* fractions,
98 double prob[Const::ChargedStable::c_SetSize];
99 probability(prob, fractions, detSet);
101 int k = part.getIndex();
111 const unsigned int n = Const::ChargedStable::c_SetSize;
113 probability(prob, fractions, detSet);
116 double maxProb = prob[k];
117 for (
unsigned i = 0; i < n; ++i) {
118 if (prob[i] > maxProb) {maxProb = prob[i]; k = i;}
120 return Const::chargedStableSet.at(k);
125 void PIDLikelihood::probability(
double probabilities[],
126 const double* fractions,
129 const unsigned int n = Const::ChargedStable::c_SetSize;
132 for (
unsigned int i = 0; i < n; ++i) frac[i] = 1.0;
139 for (
unsigned i = 0; i < n; ++i) {
141 if (fractions[i] > 0) {
142 logL[i] = getLogL(Const::chargedStableSet.at(i), detSet);
143 if (!hasMax || logL[i] > logLmax) {
151 for (
unsigned i = 0; i < n; ++i) {
152 probabilities[i] = 0;
153 if (fractions[i] > 0) probabilities[i] = exp(logL[i] - logLmax) * fractions[i];
154 norm += probabilities[i];
156 if (norm == 0)
return;
158 for (
unsigned i = 0; i < n; ++i) {
159 probabilities[i] /= norm;
165 void PIDLikelihood::printArray()
const
168 string detectorName[Const::PIDDetectors::c_size] =
169 {
"SVD",
"CDC",
"TOP",
"ARICH",
"ECL",
"KLM"};
170 string hline(
"-------");
171 for (
unsigned i = 0; i < Const::ChargedStable::c_SetSize; i++)
172 hline +=
"-----------";
173 string Hline(
"=======");
174 for (
unsigned i = 0; i < Const::ChargedStable::c_SetSize; i++)
175 Hline +=
"===========";
177 cout << Hline << endl;
180 for (
unsigned i = 0; i < Const::ChargedStable::c_SetSize; i++)
181 cout << setw(10) << Const::chargedStableSet.at(i).getPDGCode() <<
" ";
184 cout << Hline << endl;
186 float sum_logl[Const::ChargedStable::c_SetSize];
187 for (
unsigned i = 0; i < Const::ChargedStable::c_SetSize; i++) sum_logl[i] = 0;
189 for (
unsigned k = 0; k < Const::PIDDetectors::c_size; k++) {
190 cout << setw(7) << detectorName[k];
191 for (
unsigned i = 0; i < Const::ChargedStable::c_SetSize; i++) {
192 cout << setw(10) << setprecision(4) << m_logl[k][i] <<
" ";
193 sum_logl[i] += m_logl[k][i];
198 cout << hline << endl;
200 cout << setw(7) <<
"sum";
201 for (
unsigned i = 0; i < Const::ChargedStable::c_SetSize; i++)
202 cout << setw(10) << setprecision(4) << sum_logl[i] <<
" ";
205 if (isAvailable(Const::SVD) or isAvailable(Const::CDC) or isAvailable(Const::TOP) or
206 isAvailable(Const::ARICH) or isAvailable(Const::ECL) or isAvailable(Const::KLM)) {
208 for (
unsigned i = 0; i < Const::ChargedStable::c_SetSize; i++)
209 if (sum_logl[i] > sum_logl[k]) k = i;
210 unsigned pos = 11 * (k + 1) + 3;
211 Hline.replace(pos, 1,
"^");
213 cout << Hline << endl;
217 std::string PIDLikelihood::getInfoHTML()
const
219 string detectorName[Const::PIDDetectors::c_size] = {
"SVD",
"CDC",
"TOP",
"ARICH",
"ECL",
"KLM"};
221 std::stringstream stream;
222 stream << std::setprecision(4);
225 std::string colour[Const::ChargedStable::c_SetSize];
226 colour[0] = gROOT->GetColor(kAzure)->AsHexString();
227 colour[1] = gROOT->GetColor(kCyan + 1)->AsHexString();
228 colour[2] = gROOT->GetColor(kGray + 1)->AsHexString();
229 colour[3] = gROOT->GetColor(kRed + 1)->AsHexString();
230 colour[4] = gROOT->GetColor(kOrange - 2)->AsHexString();
231 colour[5] = gROOT->GetColor(kMagenta)->AsHexString();
233 stream <<
"<b>Probabilities</b><br>";
234 stream <<
"<table border=0 width=100%>";
235 stream <<
"<tr><td>All</td><td>";
236 stream <<
"<table cellspacing=1 border=0 width=100%><tr>";
237 for (
unsigned i = 0; i < Const::ChargedStable::c_SetSize; i++) {
238 double p = getProbability(Const::chargedStableSet.at(i));
239 stream <<
"<td bgcolor=\"" << colour[i] <<
"\" width=" << p * 100 <<
"%></td>";
241 stream <<
"</tr></table></td></tr>";
243 for (
unsigned k = 0; k < Const::PIDDetectors::c_size; k++) {
244 auto det = Const::PIDDetectorSet::set()[k];
245 stream <<
"<tr><td>" << detectorName[k] <<
"</td><td>";
246 if (!isAvailable(det)) {
247 stream <<
"</td></tr>";
250 stream <<
"<table cellspacing=1 border=0 width=100%><tr>";
251 for (
unsigned i = 0; i < Const::ChargedStable::c_SetSize; i++) {
252 double p = getProbability(Const::chargedStableSet.at(i), 0, det);
253 stream <<
"<td bgcolor=\"" << colour[i] <<
"\" width=" << p * 100 <<
"%></td>";
255 stream <<
"</tr></table></td></tr>";
257 stream <<
"</table>\n";
259 stream <<
"<b>Log-Likelihoods</b><br>";
261 stream <<
"<tr><th>PID / Detector</th>";
262 for (
unsigned k = 0; k < Const::PIDDetectors::c_size; k++)
263 stream <<
"<th>" << detectorName[k] <<
"</th>";
265 for (
unsigned i = 0; i < Const::ChargedStable::c_SetSize; i++) {
267 stream <<
"<td bgcolor=\"" << colour[i] <<
"\">" << Const::chargedStableSet.at(i).getParticlePDG()->GetName() <<
"</td>";
268 for (
unsigned k = 0; k < Const::PIDDetectors::c_size; k++) {
269 stream <<
"<td>" << m_logl[k][i] <<
"</td>";
273 stream <<
"</table>";