9 #include <mdst/dataobjects/PIDLikelihood.h>
10 #include <framework/logging/Logger.h>
14 #include <TParticlePDG.h>
23 PIDLikelihood::PIDLikelihood()
25 for (
unsigned short i = 0; i < Const::PIDDetectors::c_size; i++) {
26 for (
unsigned int k = 0; k < Const::ChargedStable::c_SetSize; k++) {
37 int index = Const::PIDDetectors::set().getIndex(det);
39 B2ERROR(
"PIDLikelihood::setLogLikelihood: detector is not a PID device");
42 if (logl != logl or logl == INFINITY) {
43 B2ERROR(
"PIDLikelihood::setLogLikelihood: log-likelihood for detector " << det <<
" is " << logl <<
44 " (i.e. +inf or NaN)! Ignoring this value. (" << Const::CDC <<
"=CDC, " << Const::TOP <<
"=TOP, " << Const::ARICH <<
"=ARICH, " <<
45 Const::ECL <<
"=ECL)");
50 m_logl[index][part.getIndex()] = logl;
59 it != Const::PIDDetectorSet::set().end(); ++it) {
61 result += m_logl[it.getIndex()][part.getIndex()];
73 B2ERROR(
"PIDLikelihood::probability argument 'ratio' is given with negative value");
76 if (ratio == 0)
return 0;
78 double dlogl = getLogL(p2, set) - getLogL(p1, set);
81 double elogl = exp(dlogl);
82 res = ratio / (ratio + elogl);
84 double elogl = exp(-dlogl) * ratio;
85 res = elogl / (1.0 + elogl);
88 if (std::isfinite(res))
94 const double* fractions,
97 double prob[Const::ChargedStable::c_SetSize];
98 probability(prob, fractions, detSet);
100 int k = part.getIndex();
110 const unsigned int n = Const::ChargedStable::c_SetSize;
112 probability(prob, fractions, detSet);
115 double maxProb = prob[k];
116 for (
unsigned i = 0; i < n; ++i) {
117 if (prob[i] > maxProb) {maxProb = prob[i]; k = i;}
119 return Const::chargedStableSet.at(k);
124 void PIDLikelihood::probability(
double probabilities[],
125 const double* fractions,
128 const unsigned int n = Const::ChargedStable::c_SetSize;
131 for (
unsigned int i = 0; i < n; ++i) frac[i] = 1.0;
138 for (
unsigned i = 0; i < n; ++i) {
140 if (fractions[i] > 0) {
141 logL[i] = getLogL(Const::chargedStableSet.at(i), detSet);
142 if (!hasMax || logL[i] > logLmax) {
150 for (
unsigned i = 0; i < n; ++i) {
151 probabilities[i] = 0;
152 if (fractions[i] > 0) probabilities[i] = exp(logL[i] - logLmax) * fractions[i];
153 norm += probabilities[i];
155 if (norm == 0)
return;
157 for (
unsigned i = 0; i < n; ++i) {
158 probabilities[i] /= norm;
164 void PIDLikelihood::printArray()
const
167 const string detectorName[Const::PIDDetectors::c_size] =
168 {
"SVD",
"CDC",
"TOP",
"ARICH",
"ECL",
"KLM"};
169 string hline(
"-------");
170 for (
unsigned i = 0; i < Const::ChargedStable::c_SetSize; i++)
171 hline +=
"-----------";
172 string Hline(
"=======");
173 for (
unsigned i = 0; i < Const::ChargedStable::c_SetSize; i++)
174 Hline +=
"===========";
176 cout << Hline << endl;
179 for (
unsigned i = 0; i < Const::ChargedStable::c_SetSize; i++)
180 cout << setw(10) << Const::chargedStableSet.at(i).getPDGCode() <<
" ";
183 cout << Hline << endl;
185 float sum_logl[Const::ChargedStable::c_SetSize];
186 for (
unsigned i = 0; i < Const::ChargedStable::c_SetSize; i++) sum_logl[i] = 0;
188 for (
unsigned k = 0; k < Const::PIDDetectors::c_size; k++) {
189 cout << setw(7) << detectorName[k];
190 for (
unsigned i = 0; i < Const::ChargedStable::c_SetSize; i++) {
191 cout << setw(10) << setprecision(4) << m_logl[k][i] <<
" ";
192 sum_logl[i] += m_logl[k][i];
197 cout << hline << endl;
199 cout << setw(7) <<
"sum";
200 for (
unsigned i = 0; i < Const::ChargedStable::c_SetSize; i++)
201 cout << setw(10) << setprecision(4) << sum_logl[i] <<
" ";
204 if (isAvailable(Const::SVD) or isAvailable(Const::CDC) or isAvailable(Const::TOP) or
205 isAvailable(Const::ARICH) or isAvailable(Const::ECL) or isAvailable(Const::KLM)) {
207 for (
unsigned i = 0; i < Const::ChargedStable::c_SetSize; i++)
208 if (sum_logl[i] > sum_logl[k]) k = i;
209 unsigned pos = 11 * (k + 1) + 3;
210 Hline.replace(pos, 1,
"^");
212 cout << Hline << endl;
216 std::string PIDLikelihood::getInfoHTML()
const
218 const string detectorName[Const::PIDDetectors::c_size] = {
"SVD",
"CDC",
"TOP",
"ARICH",
"ECL",
"KLM"};
220 std::stringstream stream;
221 stream << std::setprecision(4);
224 std::string colour[Const::ChargedStable::c_SetSize];
225 colour[0] = gROOT->GetColor(kAzure)->AsHexString();
226 colour[1] = gROOT->GetColor(kCyan + 1)->AsHexString();
227 colour[2] = gROOT->GetColor(kGray + 1)->AsHexString();
228 colour[3] = gROOT->GetColor(kRed + 1)->AsHexString();
229 colour[4] = gROOT->GetColor(kOrange - 2)->AsHexString();
230 colour[5] = gROOT->GetColor(kMagenta)->AsHexString();
232 stream <<
"<b>Probabilities</b><br>";
233 stream <<
"<table border=0 width=100%>";
234 stream <<
"<tr><td>All</td><td>";
235 stream <<
"<table cellspacing=1 border=0 width=100%><tr>";
236 for (
unsigned i = 0; i < Const::ChargedStable::c_SetSize; i++) {
237 double p = getProbability(Const::chargedStableSet.at(i));
238 stream <<
"<td bgcolor=\"" << colour[i] <<
"\" width=" << p * 100 <<
"%></td>";
240 stream <<
"</tr></table></td></tr>";
243 it != Const::PIDDetectorSet::set().end(); ++it) {
245 stream <<
"<tr><td>" << detectorName[it.getIndex()] <<
"</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>";
Provides a type-safe way to pass members of the chargedStableSet set.
bool contains(const DetectorSet &set) const
Check whether this set contains another set.
A class for sets of detector IDs whose content is limited to restricted set of valid detector IDs.
EDetector
Enum for identifying the detector components (detector and subdetector).
Abstract base class for different kinds of events.