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;
58 for (
unsigned int index = 0; index < Const::PIDDetectorSet::set().size(); ++index) {
59 if (set.
contains(Const::PIDDetectorSet::set()[index]))
60 result += m_logl[index][part.getIndex()];
72 B2ERROR(
"PIDLikelihood::probability argument 'ratio' is given with negative value");
75 if (ratio == 0)
return 0;
77 double dlogl = getLogL(p2, set) - getLogL(p1, set);
80 double elogl = exp(dlogl);
81 res = ratio / (ratio + elogl);
83 double elogl = exp(-dlogl) * ratio;
84 res = elogl / (1.0 + elogl);
87 if (std::isfinite(res))
93 const double* fractions,
96 double prob[Const::ChargedStable::c_SetSize];
97 probability(prob, fractions, detSet);
99 int k = part.getIndex();
109 const unsigned int n = Const::ChargedStable::c_SetSize;
111 probability(prob, fractions, detSet);
114 double maxProb = prob[k];
115 for (
unsigned i = 0; i < n; ++i) {
116 if (prob[i] > maxProb) {maxProb = prob[i]; k = i;}
118 return Const::chargedStableSet.at(k);
123 void PIDLikelihood::probability(
double probabilities[],
124 const double* fractions,
127 const unsigned int n = Const::ChargedStable::c_SetSize;
130 for (
unsigned int i = 0; i < n; ++i) frac[i] = 1.0;
137 for (
unsigned i = 0; i < n; ++i) {
139 if (fractions[i] > 0) {
140 logL[i] = getLogL(Const::chargedStableSet.at(i), detSet);
141 if (!hasMax || logL[i] > logLmax) {
149 for (
unsigned i = 0; i < n; ++i) {
150 probabilities[i] = 0;
151 if (fractions[i] > 0) probabilities[i] = exp(logL[i] - logLmax) * fractions[i];
152 norm += probabilities[i];
154 if (norm == 0)
return;
156 for (
unsigned i = 0; i < n; ++i) {
157 probabilities[i] /= norm;
163 void PIDLikelihood::printArray()
const
166 string detectorName[Const::PIDDetectors::c_size] =
167 {
"SVD",
"CDC",
"TOP",
"ARICH",
"ECL",
"KLM"};
168 string hline(
"-------");
169 for (
unsigned i = 0; i < Const::ChargedStable::c_SetSize; i++)
170 hline +=
"-----------";
171 string Hline(
"=======");
172 for (
unsigned i = 0; i < Const::ChargedStable::c_SetSize; i++)
173 Hline +=
"===========";
175 cout << Hline << endl;
178 for (
unsigned i = 0; i < Const::ChargedStable::c_SetSize; i++)
179 cout << setw(10) << Const::chargedStableSet.at(i).getPDGCode() <<
" ";
182 cout << Hline << endl;
184 float sum_logl[Const::ChargedStable::c_SetSize];
185 for (
unsigned i = 0; i < Const::ChargedStable::c_SetSize; i++) sum_logl[i] = 0;
187 for (
unsigned k = 0; k < Const::PIDDetectors::c_size; k++) {
188 cout << setw(7) << detectorName[k];
189 for (
unsigned i = 0; i < Const::ChargedStable::c_SetSize; i++) {
190 cout << setw(10) << setprecision(4) << m_logl[k][i] <<
" ";
191 sum_logl[i] += m_logl[k][i];
196 cout << hline << endl;
198 cout << setw(7) <<
"sum";
199 for (
unsigned i = 0; i < Const::ChargedStable::c_SetSize; i++)
200 cout << setw(10) << setprecision(4) << sum_logl[i] <<
" ";
203 if (isAvailable(Const::SVD) or isAvailable(Const::CDC) or isAvailable(Const::TOP) or
204 isAvailable(Const::ARICH) or isAvailable(Const::ECL) or isAvailable(Const::KLM)) {
206 for (
unsigned i = 0; i < Const::ChargedStable::c_SetSize; i++)
207 if (sum_logl[i] > sum_logl[k]) k = i;
208 unsigned pos = 11 * (k + 1) + 3;
209 Hline.replace(pos, 1,
"^");
211 cout << Hline << endl;
215 std::string PIDLikelihood::getInfoHTML()
const
217 string detectorName[Const::PIDDetectors::c_size] = {
"SVD",
"CDC",
"TOP",
"ARICH",
"ECL",
"KLM"};
219 std::stringstream stream;
220 stream << std::setprecision(4);
223 std::string colour[Const::ChargedStable::c_SetSize];
224 colour[0] = gROOT->GetColor(kAzure)->AsHexString();
225 colour[1] = gROOT->GetColor(kCyan + 1)->AsHexString();
226 colour[2] = gROOT->GetColor(kGray + 1)->AsHexString();
227 colour[3] = gROOT->GetColor(kRed + 1)->AsHexString();
228 colour[4] = gROOT->GetColor(kOrange - 2)->AsHexString();
229 colour[5] = gROOT->GetColor(kMagenta)->AsHexString();
231 stream <<
"<b>Probabilities</b><br>";
232 stream <<
"<table border=0 width=100%>";
233 stream <<
"<tr><td>All</td><td>";
234 stream <<
"<table cellspacing=1 border=0 width=100%><tr>";
235 for (
unsigned i = 0; i < Const::ChargedStable::c_SetSize; i++) {
236 double p = getProbability(Const::chargedStableSet.at(i));
237 stream <<
"<td bgcolor=\"" << colour[i] <<
"\" width=" << p * 100 <<
"%></td>";
239 stream <<
"</tr></table></td></tr>";
241 for (
unsigned k = 0; k < Const::PIDDetectors::c_size; k++) {
242 auto det = Const::PIDDetectorSet::set()[k];
243 stream <<
"<tr><td>" << detectorName[k] <<
"</td><td>";
244 if (!isAvailable(det)) {
245 stream <<
"</td></tr>";
248 stream <<
"<table cellspacing=1 border=0 width=100%><tr>";
249 for (
unsigned i = 0; i < Const::ChargedStable::c_SetSize; i++) {
250 double p = getProbability(Const::chargedStableSet.at(i), 0, det);
251 stream <<
"<td bgcolor=\"" << colour[i] <<
"\" width=" << p * 100 <<
"%></td>";
253 stream <<
"</tr></table></td></tr>";
255 stream <<
"</table>\n";
257 stream <<
"<b>Log-Likelihoods</b><br>";
259 stream <<
"<tr><th>PID / Detector</th>";
260 for (
unsigned k = 0; k < Const::PIDDetectors::c_size; k++)
261 stream <<
"<th>" << detectorName[k] <<
"</th>";
263 for (
unsigned i = 0; i < Const::ChargedStable::c_SetSize; i++) {
265 stream <<
"<td bgcolor=\"" << colour[i] <<
"\">" << Const::chargedStableSet.at(i).getParticlePDG()->GetName() <<
"</td>";
266 for (
unsigned k = 0; k < Const::PIDDetectors::c_size; k++) {
267 stream <<
"<td>" << m_logl[k][i] <<
"</td>";
271 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.