1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | #include <iostream> |
10 | #include <iomanip> |
11 | using namespace std; |
12 | |
13 | #include <TMath.h> |
14 | |
15 | #include "DNeutralParticleHypothesis_factory.h" |
16 | using namespace jana; |
17 | |
18 | |
19 | |
20 | |
21 | jerror_t DNeutralParticleHypothesis_factory::init(void) |
22 | { |
23 | return NOERROR; |
24 | } |
25 | |
26 | |
27 | |
28 | |
29 | jerror_t DNeutralParticleHypothesis_factory::brun(jana::JEventLoop *locEventLoop, int runnumber) |
30 | { |
31 | |
32 | DApplication *locApplication = dynamic_cast<DApplication*> (locEventLoop->GetJApplication()); |
33 | DGeometry *locGeometry = locApplication ? locApplication->GetDGeometry(runnumber):NULL__null; |
34 | dTargetLength = 30.0; |
35 | double locTargetCenterZ = 65.0; |
36 | dTargetRadius = 1.5; |
37 | if(locGeometry) |
38 | { |
39 | locGeometry->GetTargetZ(locTargetCenterZ); |
40 | locGeometry->GetTargetLength(dTargetLength); |
41 | } |
42 | dTargetCenter.SetXYZ(0.0, 0.0, locTargetCenterZ); |
43 | |
44 | |
45 | |
46 | vector<const DParticleID *> locPIDAlgorithms; |
47 | locEventLoop->Get(locPIDAlgorithms); |
48 | if(locPIDAlgorithms.size() < 1){ |
49 | _DBG_std::cerr<<"DNeutralParticleHypothesis_factory.cc"<< ":"<<49<<" "<<"Unable to get a DParticleID object! NO PID will be done!"<<endl; |
50 | return RESOURCE_UNAVAILABLE; |
51 | } |
52 | |
53 | dPIDAlgorithm = const_cast<DParticleID*>(locPIDAlgorithms[0]); |
54 | |
55 | |
56 | if(!dPIDAlgorithm){ |
57 | _DBG_std::cerr<<"DNeutralParticleHypothesis_factory.cc"<< ":"<<57<<" "<<"Unable to get a DParticleID object! NO PID will be done!"<<endl; |
58 | return RESOURCE_UNAVAILABLE; |
59 | } |
60 | |
61 | return NOERROR; |
62 | } |
63 | |
64 | |
65 | |
66 | |
67 | jerror_t DNeutralParticleHypothesis_factory::evnt(jana::JEventLoop *locEventLoop, int eventnumber) |
68 | { |
69 | unsigned int loc_i, loc_j, loc_k, locNDF = 1; |
70 | bool locShowerMatchFlag; |
71 | float locMass, locMomentum, locShowerEnergy, locParticleEnergy, locPathLength, locFlightTime, locProjectedTime, locTimeDifference; |
72 | float locParticleEnergyUncertainty, locShowerEnergyUncertainty, locTimeDifferenceVariance, locChiSq, locFOM; |
73 | DVector3 locPathVector; |
74 | |
75 | const DNeutralShower *locNeutralShower; |
76 | const DChargedTrackHypothesis *locChargedTrackHypothesis; |
77 | DNeutralParticleHypothesis *locNeutralParticleHypothesis; |
78 | DMatrixDSym locVariances, locErrorMatrix; |
79 | vector<const DBCALShower*> locAssociatedBCALShowers_NeutralShower; |
80 | vector<const DFCALShower*> locAssociatedFCALShowers_NeutralShower; |
81 | vector<const DBCALShower*> locAssociatedBCALShowers_ChargedTrack; |
82 | vector<const DFCALShower*> locAssociatedFCALShowers_ChargedTrack; |
83 | |
84 | vector<const DChargedTrack*> locChargedTracks; |
85 | vector<const DNeutralShower*> locNeutralShowers; |
86 | locEventLoop->Get(locChargedTracks); |
87 | locEventLoop->Get(locNeutralShowers); |
88 | |
89 | vector<Particle_t> locPIDHypotheses; |
90 | locPIDHypotheses.push_back(Gamma); |
91 | locPIDHypotheses.push_back(Neutron); |
92 | |
93 | double locStartTime = 0.0; |
94 | DLorentzVector locSpacetimeVertex(dTargetCenter, locStartTime); |
95 | double locVertexTimeUncertainty = 0.0; |
96 | |
97 | |
98 | for (loc_i = 0; loc_i < locNeutralShowers.size(); loc_i++){ |
| 1 | Loop condition is true. Entering loop body | |
|
| 5 | | Loop condition is true. Entering loop body | |
|
| 9 | | Loop condition is true. Entering loop body | |
|
| 13 | | Loop condition is true. Entering loop body | |
|
99 | locNeutralShower = locNeutralShowers[loc_i]; |
100 | locNeutralShower->GetT(locAssociatedBCALShowers_NeutralShower); |
101 | locNeutralShower->GetT(locAssociatedFCALShowers_NeutralShower); |
102 | |
103 | |
104 | locShowerMatchFlag = false; |
105 | for (loc_j = 0; loc_j < locChargedTracks.size(); loc_j++){ |
| 2 | | Loop condition is false. Execution continues on line 122 | |
|
| 6 | | Loop condition is false. Execution continues on line 122 | |
|
| 10 | | Loop condition is false. Execution continues on line 122 | |
|
| 14 | | Loop condition is false. Execution continues on line 122 | |
|
106 | locChargedTrackHypothesis = locChargedTracks[loc_j]->dChargedTrackHypotheses[0]; |
107 | locChargedTrackHypothesis->GetT(locAssociatedBCALShowers_ChargedTrack); |
108 | locChargedTrackHypothesis->GetT(locAssociatedFCALShowers_ChargedTrack); |
109 | if ((locAssociatedBCALShowers_ChargedTrack.size() > 0) && (locAssociatedBCALShowers_NeutralShower.size() > 0)){ |
110 | if (locAssociatedBCALShowers_ChargedTrack[0]->id == locAssociatedBCALShowers_NeutralShower[0]->id){ |
111 | locShowerMatchFlag = true; |
112 | break; |
113 | } |
114 | } |
115 | if ((locShowerMatchFlag == false) && (locAssociatedFCALShowers_ChargedTrack.size() > 0) && (locAssociatedFCALShowers_NeutralShower.size() > 0)){ |
116 | if (locAssociatedFCALShowers_ChargedTrack[0]->id == locAssociatedFCALShowers_NeutralShower[0]->id){ |
117 | locShowerMatchFlag = true; |
118 | break; |
119 | } |
120 | } |
121 | } |
122 | if (locShowerMatchFlag == true) |
| |
| |
| |
| |
123 | continue; |
124 | |
125 | |
126 | for (loc_k = 0; loc_k < locPIDHypotheses.size(); loc_k++){ |
| 4 | | Loop condition is false. Execution continues on line 98 | |
|
| 8 | | Loop condition is false. Execution continues on line 98 | |
|
| 12 | | Loop condition is false. Execution continues on line 98 | |
|
| 16 | | Loop condition is true. Entering loop body | |
|
127 | |
128 | |
129 | locMass = ParticleMass(locPIDHypotheses[loc_k]); |
| 17 | | Dereference of null pointer |
|
130 | locShowerEnergy = locNeutralShower->dEnergy; |
131 | locParticleEnergy = locShowerEnergy; |
132 | if (locParticleEnergy < locMass) |
133 | continue; |
134 | |
135 | locShowerEnergyUncertainty = locNeutralShower->dEnergyUncertainty; |
136 | locParticleEnergyUncertainty = locShowerEnergyUncertainty; |
137 | |
138 | locPathVector = locNeutralShower->dSpacetimeVertex.Vect() - locSpacetimeVertex.Vect(); |
139 | locPathLength = locPathVector.Mag(); |
140 | if(!(locPathLength > 0.0)) |
141 | continue; |
142 | locMomentum = sqrt(locParticleEnergy*locParticleEnergy - locMass*locMass); |
143 | locFlightTime = locPathLength*locParticleEnergy/(locMomentum*SPEED_OF_LIGHT29.9792); |
144 | locProjectedTime = locSpacetimeVertex.T() - locFlightTime; |
145 | |
146 | |
147 | locTimeDifference = locSpacetimeVertex.T() - locProjectedTime; |
148 | locTimeDifferenceVariance = 1.0; |
149 | locChiSq = locTimeDifference*locTimeDifference/locTimeDifferenceVariance; |
150 | locFOM = TMath::Prob(locChiSq, locNDF); |
151 | if(locPIDHypotheses[loc_k] == Neutron) |
152 | locFOM = -1.0; |
153 | |
154 | |
155 | locNeutralParticleHypothesis = new DNeutralParticleHypothesis; |
156 | locNeutralParticleHypothesis->AddAssociatedObject(locNeutralShower); |
157 | |
158 | locNeutralParticleHypothesis->setMass(locMass); |
159 | locNeutralParticleHypothesis->setCharge(0.0); |
160 | |
161 | Calc_Variances(locNeutralShower, locParticleEnergyUncertainty, locVariances); |
162 | Build_ErrorMatrix(locPathVector, locParticleEnergy, locVariances, locErrorMatrix); |
163 | |
164 | locNeutralParticleHypothesis->setErrorMatrix(locErrorMatrix); |
165 | locNeutralParticleHypothesis->clearTrackingErrorMatrix(); |
166 | locPathVector.SetMag(locMomentum); |
167 | locNeutralParticleHypothesis->setMomentum(locPathVector); |
168 | locNeutralParticleHypothesis->setPosition(locSpacetimeVertex.Vect()); |
169 | locNeutralParticleHypothesis->setT0(locSpacetimeVertex.T(), locVertexTimeUncertainty, SYS_NULL); |
170 | locNeutralParticleHypothesis->setT1(locNeutralShower->dSpacetimeVertex.T(), locNeutralShower->dSpacetimeVertexUncertainties.T(), locNeutralShower->dDetectorSystem); |
171 | locNeutralParticleHypothesis->setPathLength(locPathLength, 0.0); |
172 | |
173 | locNeutralParticleHypothesis->dPID = locPIDHypotheses[loc_k]; |
174 | locNeutralParticleHypothesis->dChiSq = locChiSq; |
175 | locNeutralParticleHypothesis->dNDF = locNDF; |
176 | locNeutralParticleHypothesis->dFOM = locFOM; |
177 | |
178 | _data.push_back(locNeutralParticleHypothesis); |
179 | } |
180 | } |
181 | |
182 | return NOERROR; |
183 | } |
184 | |
185 | |
186 | |
187 | |
188 | jerror_t DNeutralParticleHypothesis_factory::erun(void) |
189 | { |
190 | return NOERROR; |
191 | } |
192 | |
193 | |
194 | |
195 | |
196 | jerror_t DNeutralParticleHypothesis_factory::fini(void) |
197 | { |
198 | return NOERROR; |
199 | } |
200 | |
201 | #define DELTA(i,j)((i==j) ? 1 : 0) ((i==j) ? 1 : 0) |
202 | void DNeutralParticleHypothesis_factory::Calc_Variances(const DNeutralShower *locNeutralShower, double locParticleEnergyUncertainty, DMatrixDSym &locVariances){ |
203 | DLorentzVector locShowerPositionUncertainties = locNeutralShower->dSpacetimeVertexUncertainties; |
204 | |
205 | |
206 | |
207 | |
208 | |
209 | |
210 | |
211 | |
212 | locVariances.Clear(); |
213 | locVariances.ResizeTo(7, 7); |
214 | |
215 | locVariances(0,0) = pow(locShowerPositionUncertainties.X(), 2.0); |
216 | locVariances(1,1) = pow(locShowerPositionUncertainties.Y(), 2.0); |
217 | locVariances(2,2) = pow(locShowerPositionUncertainties.Z(), 2.0); |
218 | |
219 | locVariances[3][3] = pow(locParticleEnergyUncertainty, 2.0); |
220 | |
221 | locVariances[4][4] = pow(0.5*dTargetRadius, 2.0) ; |
222 | locVariances[5][5] = pow(0.5*dTargetRadius, 2.0) ; |
223 | locVariances[6][6] = pow(dTargetLength/sqrt(12.0), 2.0) ; |
224 | } |
225 | |
226 | void DNeutralParticleHypothesis_factory::Build_ErrorMatrix(const DVector3 &locPathVector, double locEnergy, const DMatrixDSym& locVariances, DMatrixDSym& locErrorMatrix) |
227 | { |
228 | unsigned int loc_i, loc_j, loc_ik, loc_jk; |
229 | double R = locPathVector.Mag(); |
230 | double R2= locPathVector.Mag2(); |
231 | double R3 = R*R2; |
232 | double E_R3 = locEnergy/R3; |
233 | |
234 | |
235 | DMatrix A(7, 7); |
236 | for (loc_i = 0; loc_i < 3; loc_i++) { |
237 | for (loc_j = 0; loc_j <3; loc_j++) { |
238 | A[loc_i][loc_j] = E_R3*( R2*DELTA(loc_i, loc_j)((loc_i==loc_j) ? 1 : 0) - locPathVector(loc_i) * locPathVector(loc_j) ); |
239 | A[loc_i][loc_j + 4] = - A[loc_i][loc_j]; |
240 | } |
241 | } |
242 | |
243 | |
244 | A[3][3] = 1.; |
245 | for (loc_j = 0; loc_j < 3; loc_j++) |
246 | A[loc_j][3] = locPathVector(loc_j)/R; |
247 | |
248 | |
249 | for (loc_i = 0; loc_i < 3; loc_i++) { |
250 | for ( loc_j = 0; loc_j < 3; loc_j++) { |
251 | loc_ik = loc_i + 4; |
252 | loc_jk = loc_j + 4; |
253 | A[loc_ik][loc_j] = DELTA(loc_i, loc_j)((loc_i==loc_j) ? 1 : 0); |
254 | A[loc_ik][loc_jk] = - A[loc_ik][loc_j]; |
255 | } |
256 | } |
257 | |
258 | locErrorMatrix.ResizeTo(7, 7); |
259 | locErrorMatrix = locVariances; |
260 | locErrorMatrix = locErrorMatrix.Similarity(A); |
261 | } |
262 | |