Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DTrackTimeBased_factory_THROWN.cc
Go to the documentation of this file.
1 // $Id$
2 //
3 // File: DTrackTimeBased_factory_THROWN.cc
4 // Created: Wed Nov 18 06:25:19 EST 2009
5 // Creator: davidl (on Darwin Amelia.local 9.8.0 i386)
6 //
7 
8 #include <cmath>
9 using namespace std;
10 
11 #include <DANA/DApplication.h>
12 
13 #include <CDC/DCDCTrackHit.h>
14 #include <FDC/DFDCPseudo.h>
16 
18 #include "DMCThrown.h"
19 #include "DReferenceTrajectory.h"
20 #include "DRandom.h"
21 #include "DMatrix.h"
22 #include "DTrackHitSelector.h"
23 
24 
25 //------------------
26 // DTrackTimeBased_factory_THROWN
27 //------------------
29 {
30  fitter = NULL;
31  hitselector=NULL;
32 
33  RootGeom = NULL;
34  geom = NULL;
35 }
36 
37 //------------------
38 // brun
39 //------------------
40 jerror_t DTrackTimeBased_factory_THROWN::brun(jana::JEventLoop *loop, int32_t runnumber)
41 {
42  // Get pointer to DTrackFitter object that actually fits a track
43  vector<const DTrackFitter *> fitters;
44  loop->Get(fitters);
45  if(fitters.size()<1){
46  _DBG_<<"Unable to get a DTrackFitter object! NO Charged track fitting will be done!"<<endl;
47  return RESOURCE_UNAVAILABLE;
48  }
49 
50  // Drop the const qualifier from the DTrackFitter pointer (I'm surely going to hell for this!)
51  fitter = const_cast<DTrackFitter*>(fitters[0]);
52 
53  // Warn user if something happened that caused us NOT to get a fitter object pointer
54  if(!fitter){
55  _DBG_<<"ERROR: Unable to get a DTrackFitter object! Chisq for DTrackTimeBased:THROWN will NOT be calculated!"<<endl;
56  return RESOURCE_UNAVAILABLE;
57  }
58 
59  // Get pointer to DTrackHitSelector object
60  vector<const DTrackHitSelector *> hitselectors;
61  loop->Get(hitselectors);
62  if(hitselectors.size()<1){
63  _DBG_<<"ERROR: Unable to get a DTrackHitSelector object! NO DTrackTimeBased:THROWN objects will be created!"<<endl;
64  return RESOURCE_UNAVAILABLE;
65  }
66  hitselector = hitselectors[0];
67 
68  // Set DGeometry pointers so it can be used by the DReferenceTrajectory class
69  DApplication* dapp = dynamic_cast<DApplication*>(loop->GetJApplication());
70  geom = dapp->GetDGeometry(runnumber);
71 
72  // Set magnetic field pointer
73  bfield = dapp->GetBfield();
74 
75  // Get the particle ID algorithms
76  loop->GetSingle(dParticleID);
77 
78  return NOERROR;
79 }
80 
81 //------------------
82 // evnt
83 //------------------
84 jerror_t DTrackTimeBased_factory_THROWN::evnt(JEventLoop *loop, uint64_t eventnumber)
85 {
86  vector<const DMCThrown*> mcthrowns;
87  vector<const DCDCTrackHit*> cdctrackhits;
88  vector<const DFDCPseudo*> fdcpseudos;
89  vector<const DTrackWireBased*> wbtracks;
90  loop->Get(mcthrowns);
91  loop->Get(cdctrackhits);
92  loop->Get(fdcpseudos);
93  loop->Get(wbtracks, "THROWN");
94 
95  for(unsigned int i=0; i< mcthrowns.size(); i++){
96  const DMCThrown *thrown = mcthrowns[i];
97 
98  if(fabs(thrown->charge())<1)continue;
99 
100  // First, copy over the DKinematicData part
102  *static_cast<DKinematicData*>(track) = *static_cast<const DKinematicData*>(thrown);
103 
104  // Set PID
105  track->setPID(thrown->PID());
106 
107  // Add DMCThrown as associated object
108  track->AddAssociatedObject(thrown);
109 
110  // We need to swim a reference trajectory here. To avoid the overhead
111  // of allocating/deallocating them every event, we keep a pool and
112  // re-use them. If the pool is not big enough, then add one to the
113  // pool.
114  unsigned int locNumInitialReferenceTrajectories = rt_pool.size();
115  if(rt_pool.size()<=_data.size()){
116  // This is a little ugly, but only gets called a few times throughout the life of the process
117  // Note: these never get deleted, even at the end of process.
118  rt_pool.push_back(new DReferenceTrajectory(bfield));
119  }
120 
121  DReferenceTrajectory *rt = rt_pool[_data.size()];
122  if(locNumInitialReferenceTrajectories == rt_pool.size()) //didn't create a new one
123  rt->Reset();
124  rt->q = track->charge();
125  // track->rt = rt;
126  DVector3 pos = track->position();
127  DVector3 mom = track->momentum();
128  rt->SetMass(thrown->mass());
129  rt->SetDGeometry(geom);
130  rt->SetDRootGeom(RootGeom);
131  rt->Swim(pos, mom, track->charge());
132 
133  // Find hits that should be on this track and add them as associated objects
134  vector<const DCDCTrackHit*> cdchits;
135  vector<const DFDCPseudo*> fdchits;
136  if(hitselector && cdctrackhits.size()>0)hitselector->GetCDCHits(DTrackHitSelector::kHelical, rt, cdctrackhits, cdchits);
137  if(hitselector && fdcpseudos.size()>0)hitselector->GetFDCHits(DTrackHitSelector::kHelical, rt, fdcpseudos, fdchits);
138  for(unsigned int j=0; j<cdchits.size(); ++j)track->AddAssociatedObject(cdchits[j]);
139  for(unsigned int j=0; j<fdchits.size(); ++j)track->AddAssociatedObject(fdchits[j]);
140 
141  // We want to get chisq and Ndof values for this track using the hits from above.
142  // We do this using the DTrackFitter object. This more or less guarantees that the
143  // chisq calculation is done in the same way as it is for track fitting. Note
144  // that no fitting is actually done here so this should be reasonably fast
145  if(fitter){
146  fitter->Reset();
147  fitter->AddHits(cdchits);
148  fitter->AddHits(fdchits);
149  double chisq;
150  int Ndof;
151  vector<DTrackFitter::pull_t> pulls;
152  fitter->ChiSq(DTrackFitter::kTimeBased, rt, &chisq, &Ndof, &pulls);
153  track->chisq = chisq;
154  track->Ndof = Ndof;
155  track->pulls = pulls;
156  }else{
157  track->chisq = 0.0;
158  track->Ndof = 0;
159  }
160 
161  // For this to work properly with DChargedTrack, we need to put something
162  // in for the candidateid and the FOM (figure of merit) used to decide if
163  // this is the right mass hypothesis for the candidate. Since there is no
164  // candidate and we *know* it's the right hypothesis, we set the candidateid
165  // to the thrown object's oid and set the FOM to 1.
166  track->candidateid = thrown->id;
167  track->trackid = thrown->id;
168  track->FOM = 1.0;
169 
170  // Add wire-based track as associated object. Even though they should
171  // be in the same order, we verify it is the correct one by checking
172  // that the candidateid is the same as ours (i.e. the same thrown track)
173  for(unsigned int j=0; j<wbtracks.size(); j++){
174  if(wbtracks[j]->candidateid == track->candidateid){
175  track->AddAssociatedObject(wbtracks[j]);
176  break;
177  }
178  }
179 
180  // Set MC Hit-matching information
181  track->dMCThrownMatchMyID = thrown->myid;
182  track->dNumHitsMatchedToThrown = track->Ndof + 5;
183 
184  _data.push_back(track);
185  }
186 
187  // Set CDC ring & FDC plane hit patterns
188  for(size_t loc_i = 0; loc_i < _data.size(); ++loc_i)
189  {
190  vector<const DCDCTrackHit*> locCDCTrackHits;
191  _data[loc_i]->Get(locCDCTrackHits);
192 
193  vector<const DFDCPseudo*> locFDCPseudos;
194  _data[loc_i]->Get(locFDCPseudos);
195 
196  _data[loc_i]->dCDCRings = dParticleID->Get_CDCRingBitPattern(locCDCTrackHits);
197  _data[loc_i]->dFDCPlanes = dParticleID->Get_FDCPlaneBitPattern(locFDCPseudos);
198  }
199 
200  return NOERROR;
201 }
202 
Definition: track.h:16
DApplication * dapp
float chisq
Chi-squared for the track (not chisq/dof!)
The DTrackFitter class is a base class for different charged track fitting algorithms. It does not actually fit the track itself, but provides the interface and some common support features most algorthims will need to implement.
Definition: DTrackFitter.h:61
TVector3 DVector3
Definition: DVector3.h:14
oid_t trackid
id of DTrackWireBased corresponding to this track
void AddHits(vector< const DCDCTrackHit * > cdchits)
DMagneticFieldMap * GetBfield(unsigned int run_number=1)
void SetMass(double mass)
const DVector3 & position(void) const
void Swim(const DVector3 &pos, const DVector3 &mom, double q=-1000.0, const TMatrixFSym *cov=NULL, double smax=2000.0, const DCoordinateSystem *wire=NULL)
jerror_t brun(JEventLoop *eventLoop, int32_t runnumber)
Called everytime a new run number is detected.
oid_t candidateid
id of DTrackCandidate corresponding to this track
void SetDGeometry(const DGeometry *geom)
virtual double ChiSq(fit_type_t fit_type, DReferenceTrajectory *rt, double *chisq_ptr=NULL, int *dof_ptr=NULL, vector< pull_t > *pulls_ptr=NULL)=0
void Reset(void)
Definition: DTrackFitter.cc:94
DGeometry * GetDGeometry(unsigned int run_number)
#define _DBG_
Definition: HDEVIO.h:12
void SetDRootGeom(const DRootGeom *RootGeom)
int Ndof
Number of degrees of freedom in the fit.
double charge(void) const
void setPID(Particle_t locPID)
vector< DTrackFitter::pull_t > pulls
Holds pulls used in chisq calc. (not including off-diagonals)
const DVector3 & momentum(void) const
jerror_t evnt(JEventLoop *eventLoop, uint64_t eventnumber)
Called every event.
const DTrackFitter * fitter
int myid
id of this particle from original generator
Definition: DMCThrown.h:22
Particle_t PID(void) const
double mass(void) const