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