Bug Summary

File:programs/Analysis/plugins/trackeff_hists/DEventProcessor_trackeff_hists.cc
Location:line 282, column 7
Description:Dereference of null pointer

Annotated Source Code

1// $Id: DEventProcessor_trackeff_hists.cc 2774 2007-07-19 15:59:02Z davidl $
2//
3// File: DEventProcessor_trackeff_hists.cc
4// Created: Sun Apr 24 06:45:21 EDT 2005
5// Creator: davidl (on Darwin Harriet.local 7.8.0 powerpc)
6//
7
8#include <iostream>
9#include <cmath>
10using namespace std;
11
12#include <TThread.h>
13
14#include <TROOT.h>
15
16#include "DEventProcessor_trackeff_hists.h"
17
18#include <JANA/JApplication.h>
19#include <JANA/JEventLoop.h>
20
21#include <DANA/DApplication.h>
22#include <TRACKING/DMCTrajectoryPoint.h>
23#include <PID/DChargedTrack.h>
24#include <DVector2.h>
25#include <particleType.h>
26
27
28// Routine used to create our DEventProcessor
29extern "C"{
30void InitPlugin(JApplication *app){
31 InitJANAPlugin(app);
32 app->AddProcessor(new DEventProcessor_trackeff_hists());
33}
34} // "C"
35
36
37//------------------
38// DEventProcessor_trackeff_hists
39//------------------
40DEventProcessor_trackeff_hists::DEventProcessor_trackeff_hists()
41{
42 trk_ptr = &trk;
43 MAX_TRACKS = 10;
44
45 trkeff = NULL__null;
46
47 pthread_mutex_init(&mutex, NULL__null);
48 pthread_mutex_init(&rt_mutex, NULL__null);
49}
50
51//------------------
52// ~DEventProcessor_trackeff_hists
53//------------------
54DEventProcessor_trackeff_hists::~DEventProcessor_trackeff_hists()
55{
56
57}
58
59//------------------
60// init
61//------------------
62jerror_t DEventProcessor_trackeff_hists::init(void)
63{
64 pthread_mutex_lock(&mutex);
65
66 // Create TRACKING directory
67 TDirectory *dir = (TDirectory*)gROOT->FindObject("TRACKING");
68 if(!dir)dir = new TDirectoryFile("TRACKING","TRACKING");
69 dir->cd();
70
71 // Create Tree
72 if(!trkeff){
73 trkeff = new TTree("trkeff","Tracking Efficiency");
74 trkeff->Branch("F","track",&trk_ptr);
75 }
76
77 dir->cd("../");
78
79 pthread_mutex_unlock(&mutex);
80
81 return NOERROR;
82}
83
84//------------------
85// brun
86//------------------
87jerror_t DEventProcessor_trackeff_hists::brun(JEventLoop *loop, int runnumber)
88{
89
90 return NOERROR;
91}
92
93//------------------
94// erun
95//------------------
96jerror_t DEventProcessor_trackeff_hists::erun(void)
97{
98
99 return NOERROR;
100}
101
102//------------------
103// fini
104//------------------
105jerror_t DEventProcessor_trackeff_hists::fini(void)
106{
107 return NOERROR;
108}
109
110//------------------
111// evnt
112//------------------
113jerror_t DEventProcessor_trackeff_hists::evnt(JEventLoop *loop, int eventnumber)
114{
115 vector<const DCDCTrackHit*> cdctrackhits;
116 vector<const DFDCPseudo*> fdcpseudos;
117 vector<const DTrackCandidate*> trackcandidates;
118 vector<const DTrackWireBased*> trackWBs;
119 vector<const DChargedTrack*> trackTBs;
120 vector<const DTrackTimeBased*> throwns;
121 vector<const DTrackTimeBased*> locAssociatedTrackTimeBasedVector;
122 vector<const DMCTrajectoryPoint*> mctraj;
123
124 loop->Get(cdctrackhits);
125 loop->Get(fdcpseudos);
126 loop->Get(trackcandidates);
127 loop->Get(trackWBs);
128 loop->Get(trackTBs);
129 loop->Get(throwns, "THROWN");
130 loop->Get(mctraj);
131
132 // 1. Get number of CDC/FDC wires for each primary track
133 // 2. Get number of CDC/FDC wires and track number for a given DKinematicData object
134 // 3. Get number of DKinematicData objects of same type associated with each track number
135
136 // Get track info and track number for each reconstructed track
137 vector<track_info> ti_can(MAX_TRACKS);
138 vector<track_info> ti_trkwb(MAX_TRACKS);
139 vector<track_info> ti_trktb(MAX_TRACKS);
140 for(unsigned int i=0; i<trackcandidates.size(); i++)FillTrackInfo(trackcandidates[i], ti_can);
141 for(unsigned int i=0; i<trackWBs.size(); i++)FillTrackInfo(trackWBs[i], ti_trkwb);
142 for(unsigned int i=0; i<trackTBs.size(); i++){
143 trackTBs[i]->dChargedTrackHypotheses[0]->GetT(locAssociatedTrackTimeBasedVector);
144 FillTrackInfo(locAssociatedTrackTimeBasedVector[0], ti_trktb);
145 }
146
147 // The highest (and therefore, most interesting) GEANT mechansim for each track in the
148 // region before it gets to the BCAL.
149 vector<double> dtheta_mech(MAX_TRACKS);
150 vector<double> dp_mech(MAX_TRACKS);
151 vector<TVector3> last_p(MAX_TRACKS, TVector3(0.0, 0.0, 0.0));
152 vector<int> mech_max(MAX_TRACKS,0);
153 for(unsigned int i=0; i<mctraj.size(); i++){
154 int track = mctraj[i]->track;
155 int mech = mctraj[i]->mech;
156 double R = sqrt(pow((double)mctraj[i]->x, 2.0) + pow((double)mctraj[i]->y, 2.0));
157 if(track<0 || track>=MAX_TRACKS)continue;
158 if(R>60.0)continue;
159 TVector3 p(mctraj[i]->px, mctraj[i]->py, mctraj[i]->pz);
160 if(mech>mech_max[track]){
161 mech_max[track] = mech;
162 dtheta_mech[track] = p.Angle(last_p[track]);
163 dp_mech[track] = (p - last_p[track]).Mag();
164 }
165 last_p[track] = p;
166 }
167
168 // Lock mutex
169 pthread_mutex_lock(&mutex);
170
171 // Loop over thrown tracks
172 for(unsigned int i=0; i<throwns.size(); i++){
173 const DTrackTimeBased *thrown = throwns[i];
174
175 trk.pthrown.SetXYZ(thrown->momentum().x(),
176 thrown->momentum().y(),
177 thrown->momentum().z());
178
179 // Get info for thrown track
180 GetNhits(thrown, trk.Ncdc, trk.Nfdc, trk.track);
181 if(trk.track<=0 || trk.track>=MAX_TRACKS)continue;
182
183 // Copy best reconstructed track info
184 trk.can = ti_can[trk.track];
185 trk.trkwb = ti_trkwb[trk.track];
186 trk.trktb = ti_trktb[trk.track];
187
188 // Fill tree
189 trk.event = eventnumber;
190 trk.mech = mech_max[trk.track];
191 trk.dtheta_mech = dtheta_mech[trk.track];
192 trk.dp_mech = dp_mech[trk.track];
193 trkeff->Fill();
194 }
195
196
197 // Unlock mutex
198 pthread_mutex_unlock(&mutex);
199
200 return NOERROR;
201}
202
203//------------------
204// FillTrackInfo
205//------------------
206void DEventProcessor_trackeff_hists::FillTrackInfo(const DKinematicData *kd, vector<track_info> &vti)
207{
208 // Get track info and track number most closely matching this track
209 int track_no;
210 track_info ti;
211 GetTrackInfo(kd, ti, track_no);
212 if(track_no<0 || track_no>=MAX_TRACKS)return;
213
214 // Check if this track has more wires hit than the existing track_info
215 // and replace if needed.
216 int Nwires = ti.Ncdc + ti.Nfdc;
217 int Nwires_prev = vti[track_no].Ncdc + vti[track_no].Nfdc;
218 if(Nwires > Nwires_prev)vti[track_no] = ti;
219}
220
221//------------------
222// GetTrackInfo
223//------------------
224void DEventProcessor_trackeff_hists::GetTrackInfo(const DKinematicData *kd, track_info &ti, int &track_no)
225{
226 ti.p.SetXYZ(kd->momentum().x(),kd->momentum().y(),kd->momentum().z());
227 GetNhits(kd, ti.Ncdc, ti.Nfdc, track_no);
228
229 // Try dynamic casting DKinematicData into something that can be used to get
230 // at the chisq and Ndof.
231 const DTrackCandidate *can = dynamic_cast<const DTrackCandidate*>(kd);
232 const DTrackWireBased *track = dynamic_cast<const DTrackWireBased*>(kd);
233 const DTrackTimeBased *part = dynamic_cast<const DTrackTimeBased*>(kd);
234 if(can!=NULL__null){
235 ti.trk_chisq = can->chisq;
236 ti.trk_Ndof = can->Ndof;
237 }else if(track!=NULL__null){
238 ti.trk_chisq = track->chisq;
239 ti.trk_Ndof = track->Ndof;
240 }else if(part!=NULL__null){
241 ti.trk_chisq = part->chisq;
242 ti.trk_Ndof = part->Ndof;
243 }else{
244 ti.trk_chisq = 1.0E6;
245 ti.trk_Ndof = -1;
246 }
247}
248
249//------------------
250// GetNhits
251//------------------
252void DEventProcessor_trackeff_hists::GetNhits(const DKinematicData *kd, int &Ncdc, int &Nfdc, int &track)
253{
254 vector<const DCDCTrackHit*> cdctrackhits;
255 vector<const DFDCPseudo*> fdcpseudos;
256
257 // The DKinematicData object should be a DTrackCandidate, DTrackWireBased, or DParticle which
258 // has associated objects for the hits
259 kd->Get(cdctrackhits);
260 kd->Get(fdcpseudos);
261
262 // The track number is buried in the truth hit objects of type DMCTrackHit. These should be
263 // associated objects for the individual hit objects. We need to loop through them and
264 // keep track of how many hits for each track number we find
265
266 // CDC hits
267 vector<int> cdc_track_no(MAX_TRACKS, 0);
268 for(unsigned int i=0; i<cdctrackhits.size(); i++){
1
Loop condition is false. Execution continues on line 277
269 vector<const DMCTrackHit*> mctrackhits;
270 cdctrackhits[i]->Get(mctrackhits);
271 if(mctrackhits.size()==0)continue;
272 if(!mctrackhits[0]->primary)continue;
273 int track = mctrackhits[0]->track;
274 if(track>=0 && track<MAX_TRACKS)cdc_track_no[track]++;
275 }
276 // FDC hits
277 vector<int> fdc_track_no(MAX_TRACKS, 0);
278 for(unsigned int i=0; i<fdcpseudos.size(); i++){
2
Loop condition is true. Entering loop body
5
Loop condition is true. Entering loop body
8
Loop condition is true. Entering loop body
279 vector<const DMCTrackHit*> mctrackhits;
280 fdcpseudos[i]->Get(mctrackhits);
281 if(mctrackhits.size()==0)continue;
3
Taking true branch
4
Execution continues on line 278
6
Taking true branch
7
Execution continues on line 278
9
Taking false branch
282 if(!mctrackhits[0]->primary)continue;
10
Dereference of null pointer
283 int track = mctrackhits[0]->track;
284 if(track>=0 && track<MAX_TRACKS)fdc_track_no[track]++;
285 }
286
287 // Find track number with most wires hit
288 int track_with_max_hits = 0;
289 int tot_hits_max = cdc_track_no[0] + fdc_track_no[0];
290 for(int i=1; i<MAX_TRACKS; i++){
291 int tot_hits = cdc_track_no[i] + fdc_track_no[i];
292 if(tot_hits > tot_hits_max){
293 track_with_max_hits=i;
294 tot_hits_max = tot_hits;
295 }
296 }
297
298 Ncdc = cdc_track_no[track_with_max_hits];
299 Nfdc = fdc_track_no[track_with_max_hits];
300
301 // If there are no hits on this track, then we really should report
302 // a "non-track" (i.e. track=-1)
303 track = tot_hits_max>0 ? track_with_max_hits:-1;
304}
305