Hall A ROOT/C++ Analyzer (podd)
Loading...
Searching...
No Matches
THaPIDinfo.cxx
Go to the documentation of this file.
1//*-- Author : Ole Hansen 5 April 2001
2
4//
5// THaPIDinfo
6//
7// Implements a basic Baysian likelihood analysis.
8//
9// One can define an arbitrary number of detectors (= measurements) and
10// particle types (hypotheses). Each detector is expected to calculate
11// the probability that the event or track to which this object is
12// attached corresponds to each of the particle types.
13//
14// After the per-detector/per-particle type probabilities are set,
15// CombinePID() calculates the overall conditional (posterior)
16// probabilities for each particle hypothesis.
17//
18// For accurate results, prior probabilities (prevalences) should be set
19// for each particle type via SetPrior(). Such numbers will typically
20// come from simulations. By default, all prior probabilities are assumed
21// equal (=no prior knowledge).
22//
23// Prior probabilities may be different for each event or track,
24// especially for large-acceptance detectors, as they usually depend on
25// the reaction kinematics and thus on track 4-momentum. For small
26// acceptance spectrometers, one may get away with constant values
27// (averages) calculated for a given spectrometer setting (angle,
28// momentum).
29//
31
32#include "THaPIDinfo.h"
33#include "THaTrack.h"
34#include "THaSpectrometer.h"
35#include "THaTrackingDetector.h"
36#include <iostream>
37
38using namespace std;
39
40//_____________________________________________________________________________
41THaPIDinfo::THaPIDinfo() : fNdet{0}, fNpart{0}
42{
43 // Default constructor
44}
45
46//_____________________________________________________________________________
48 fPrior(npart), fProb(ndet*npart), fCombinedProb(npart),
49 fNdet{ndet}, fNpart{npart}
50{
51 // Normal constructor
53}
54
55//_____________________________________________________________________________
56THaPIDinfo::THaPIDinfo( const THaTrack* track ) : fNdet{0}, fNpart{0}
57{
58 // Normal constructor from a track. Retrieves array dimensions from
59 // the size of the detector and particle arrays of the track's spectrometer.
60
61 if( track ) {
62 if( const auto* td = track->GetCreator() ) {
63 if( const auto* sp = dynamic_cast<THaSpectrometer*>(td->GetApparatus()) ) {
64 UInt_t ndet = sp->GetNpidDetectors();
65 UInt_t npart = sp->GetNpidParticles();
66 SetSize(ndet, npart);
67 }
68 }
69 }
70}
71
72//_____________________________________________________________________________
74{
75 // Zero contents of the arrays
76
77 fProb.assign(fProb.size(), 0.0);
78 fCombinedProb.assign(fCombinedProb.size(), 0.0);
79}
80
81//_____________________________________________________________________________
83{
84 // Compute combined PID of all detectors
85
86 // Likelihood products per particle hypothesis across all detectors
87 for( UInt_t p = 0; p < fNpart; p++ ) {
88 fCombinedProb[p] = 1.0;
89 for( UInt_t d = 0; d < fNdet; d++ ) {
90 fCombinedProb[p] *= fProb[idx(d, p)];
91 }
92 }
93
94 // Weigh above results with the priors to get the conditional
95 // probabilities for each particle hypothesis
96 Double_t sum = 0.0;
97 for( UInt_t p = 0; p < fNpart; p++ ) {
98 sum += fPrior[p] * fCombinedProb[p];
99 }
100 if( sum != 0.0 ) {
101 for( UInt_t p = 0; p < fNpart; p++ ) {
102 fCombinedProb[p] *= fPrior[p] / sum;
103 }
104 }
105
106}
107
108//_____________________________________________________________________________
110{
111 // Display contents of arrays
112 // The first line shows the combined probabilities, followed by the
113 // probabilities for each detector
114 for( UInt_t d = 0; d <= fNdet; d++ ) {
115 for( UInt_t p = 0; p < fNpart; p++ ) {
116 if( d == 0 )
117 cout << fCombinedProb[p];
118 else
119 cout << fProb[idx(d-1,p)];
120 if( p != fNpart-1 ) cout << " ";
121 }
122 cout << endl;
123 }
124}
125
126//_____________________________________________________________________________
128{
129 // Set prior probabilities equal to 1/fNpart (= no prior knowledge).
130
131 for( UInt_t p = 0; p < fNpart; p++ ) {
132 fPrior[p] = 1.0/fNpart;
133 }
134}
135
136//_____________________________________________________________________________
138{
139 // Resize the arrays. Creates new arrays if they don't yet exist,
140 // Old contents are preserved as much as possible.
141
142 if( ndet == fNdet && npart == fNpart ) return;
143 if( ndet == 0 || npart == 0 ) {
144 fPrior.clear();
145 fProb.clear();
146 fCombinedProb.clear();
147 fNdet = fNpart = 0;
148 return;
149 }
150 if( ndet != fNdet || npart != fNpart )
151 fProb.resize(ndet * npart);
152
153 if( npart != fNpart ) {
154 fPrior.resize(npart);
155 fCombinedProb.resize(npart);
156 if( fNpart == 0 )
158 }
159
160 fNdet = ndet;
161 fNpart = npart;
162}
163
164//_____________________________________________________________________________
unsigned int UInt_t
#define d(i)
double Double_t
const char Option_t
winID h TVirtualViewer3D TVirtualGLPainter p
UInt_t fNpart
Definition THaPIDinfo.h:48
std::vector< Double_t > fPrior
Definition THaPIDinfo.h:40
void SetDefaultPriors()
void SetSize(UInt_t ndet, UInt_t prob)
virtual void CombinePID()
UInt_t fNdet
Definition THaPIDinfo.h:46
std::vector< Double_t > fProb
Definition THaPIDinfo.h:42
virtual void Print(Option_t *opt="") const
virtual void Clear(Option_t *opt="")
std::vector< Double_t > fCombinedProb
Definition THaPIDinfo.h:44
UInt_t idx(UInt_t detector, UInt_t particle) const
Definition THaPIDinfo.h:56
THaTrackingDetector * GetCreator() const
Definition THaTrack.h:77
STL namespace.
ClassImp(TPyArg)