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. ("
45 << Const::SVD <<
"=SVD, " << Const::CDC <<
"=CDC, " << Const::TOP <<
"=TOP, "
46 << Const::ARICH <<
"=ARICH, " << Const::ECL <<
"=ECL, " << Const::KLM <<
"=KLM)");
51 m_logl[index][part.
getIndex()] = logl;
60 it != Const::PIDDetectorSet::set().end(); ++it) {
62 result += m_logl[it.getIndex()][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);
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 const 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 const 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>";
244 it != Const::PIDDetectorSet::set().end(); ++it) {
246 stream <<
"<tr><td>" << detectorName[it.getIndex()] <<
"</td><td>";
247 if (!isAvailable(det)) {
248 stream <<
"</td></tr>";
251 stream <<
"<table cellspacing=1 border=0 width=100%><tr>";
252 for (
unsigned i = 0; i < Const::ChargedStable::c_SetSize; i++) {
253 double p = getProbability(Const::chargedStableSet.at(i), 0, det);
254 stream <<
"<td bgcolor=\"" << colour[i] <<
"\" width=" << p * 100 <<
"%></td>";
256 stream <<
"</tr></table></td></tr>";
258 stream <<
"</table>\n";
260 stream <<
"<b>Log-Likelihoods</b><br>";
262 stream <<
"<tr><th>PID / Detector</th>";
263 for (
unsigned k = 0; k < Const::PIDDetectors::c_size; k++)
264 stream <<
"<th>" << detectorName[k] <<
"</th>";
266 for (
unsigned i = 0; i < Const::ChargedStable::c_SetSize; i++) {
268 stream <<
"<td bgcolor=\"" << colour[i] <<
"\">" << Const::chargedStableSet.at(i).getParticlePDG()->GetName() <<
"</td>";
269 for (
unsigned k = 0; k < Const::PIDDetectors::c_size; k++) {
270 stream <<
"<td>" << m_logl[k][i] <<
"</td>";
274 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.
int getIndex() const
This particle's index in the associated 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.