BOSS 7.0.1
BESIII Offline Software System
Loading...
Searching...
No Matches
TrkRecoTrk.cxx
Go to the documentation of this file.
1// File and Version Information:
2// $Id: TrkRecoTrk.cxx,v 1.9 2008/09/23 00:58:03 zhangy Exp $
3//
4// Description:
5// implementation for TrkRecoTrk
6//
7//
8// Author List: Stephen Schaffner
9//
10//------------------------------------------------------------------------
11#include <vector>
12#include <assert.h>
13#include <functional>
14#include <algorithm>
15#include "MdcGeom/Constants.h"
16#include "TrkBase/TrkRecoTrk.h"
17#include "TrkBase/TrkHitOnTrk.h"
18#include "TrkBase/TrkFundHit.h"
19#include "TrkBase/TrkDifTraj.h"
20#include "TrkBase/TrkExchangePar.h"
21#include "TrkBase/TrkErrCode.h"
22#include "TrkBase/TrkExtInterface.h"
23#include "TrkBase/TrkFitStatus.h"
24#include "TrkBase/TrkRepIter.h"
25#include "CLHEP/Vector/ThreeVector.h"
26#include "MdcRecoUtil/Pdt.h"
27#include "MdcRecoUtil/BesPointErr.h"
28#include "MdcRecoUtil/BesVectorErr.h"
29#include "MdcRecoUtil/DifPoint.h"
30#include "MdcRecoUtil/DifVector.h"
31#include "MdcRecoUtil/DifIndepPar.h"
32#include "TrkBase/TrkRep.h"
33#include "TrkBase/TrkContext.h"
34#include "MdcRecoUtil/PdtPid.h"
35#include "boost/shared_ptr.hpp"
36#include "BField/BField.h"
37using std::ostream;
38
39
41public:
42 friend class TrkRecoTrk;
43private:
45 : _reps(PdtPid::nPidType,boost::shared_ptr<TrkRep>((TrkRep *)0)),
46 _hitInterfaces(PdtPid::nPidType,boost::shared_ptr<TrkHitList>((TrkHitList *)0))
47 { }
48
49 // the reps live here; owned by trk;
50 // Pointers to the Reps, indexed by particle type; always has nPart entries,
51 // which may point to as few as one single distinct Rep...
52 typedef std::vector<boost::shared_ptr<TrkRep> > repList_t;
53 typedef repList_t::const_iterator repConstIter;
54 typedef repList_t::iterator repIter;
55 repList_t _reps;
56 typedef std::vector<boost::shared_ptr<TrkHitList> > hitList_t;
57 typedef hitList_t::iterator hitListIter;
58 hitList_t _hitInterfaces;
59
60};
61
63 double t0) :
64 _impl(new TrkRecoTrkImpl),
65 _id(ctext.getId()),
66 _fitNumber(PdtPid::nPidType,(int)0),
67 _defaultType(defaultPart),
68 _trackT0(t0),
69 _bField( ctext.bField() )
70{
71 // No TrkRep is defined here; must be created in appropriate FitMaker.
72 unsigned i=0;
73 for (TrkRecoTrkImpl::hitListIter iface = _impl->_hitInterfaces.begin();
74 iface!= _impl->_hitInterfaces.end(); ++iface) {
75 iface->reset( new TrkHitList(this, (PdtPid::PidType)i++) ); //cast
76 }
77}
78
79// persistence reconstitution. This sets a nul value for BField and IdManager
80TrkRecoTrk::TrkRecoTrk(PdtPid::PidType defaultPart,long idnum,double t0) :
81 _impl(new TrkRecoTrkImpl),
82 _id(idnum,0),
83 _fitNumber(PdtPid::nPidType,(int)0),
84 _defaultType(defaultPart),
85 _trackT0(t0),
86 _bField(0)
87{
88 // No TrkRep is defined here; must be created in appropriate FitMaker.
89 unsigned i=0;
90 for (TrkRecoTrkImpl::hitListIter iface = _impl->_hitInterfaces.begin();
91 iface!= _impl->_hitInterfaces.end(); ++iface) {
92 iface->reset( new TrkHitList(this, (PdtPid::PidType)i++) ); //cast
93 }
94}
95
96//-- Copy constructor
98 _impl(new TrkRecoTrkImpl),
99 _id(rhs._id.idManager()),
100 _fitNumber(PdtPid::nPidType,(int)0),
101 _storage(rhs._storage),
102 _trackT0( rhs._trackT0),
103 _bField(rhs._bField)
104{
105 _defaultType = rhs.defaultType();
106 unsigned i=0;
107 for (TrkRecoTrkImpl::hitListIter iface = _impl->_hitInterfaces.begin();
108 iface!= _impl->_hitInterfaces.end(); ++iface) {
109 iface->reset( new TrkHitList(this, (PdtPid::PidType)i++) ); //cast
110 }
111 copyReps(rhs);
112}
113
115{
116 delete _impl;
117}
118
119const TrkRecoTrk&
121{
122 if (&right == this) return *this;
123 _trackT0 = right._trackT0;
124 _defaultType = right.defaultType();
125 copyReps(right);
126 _bField = right._bField;
127// AbsEvtObj::operator=(right);
128 _id.setNewValue(right._id);
129 _storage = right._storage;
130 return *this;
131}
132
133const TrkId&
135{
136 return _id;
137}
138
139double
141{
142 return _trackT0;
143}
144
147{
148 if (_impl->_reps[hypo].get() == 0) {
149 return hypo;
150 }
151// return _repPtr[hypo]->fitValid() ? _repPtr[hypo]->particleType()
152// : PdtPid::null;
153 return _impl->_reps[hypo]->particleType();
154}
155
156int
158{
159 PdtPid::PidType used = whichFit(hypo);
160 if (used == PdtPid::null) return -1;
161 int index = used;
162 return _fitNumber[index];
163}
164
165void
166TrkRecoTrk::print(ostream& ostr) const
167{
168 ostr << "Trk: " << id() << " def: "
170 << " fitNumber:" << fitNumber(defaultType());
171 const TrkFit* daFit = fitResult();
172 const TrkHitList* daList = hits();
173 const TrkFitStatus* daStatus = status();
174 if (daList != 0) {
175 ostr << " nhit: " << daList->nHit();
176 }
177 if (daFit != 0) {
178 ostr << " nactive: " << daFit->nActive() << " chisq: " << daFit->chisq();
179 }
180 if (daStatus != 0) {
181 ostr << " 3-d: " << (daStatus->is2d() == 0);
182 }
183 ostr << " t0: " << _trackT0 << "\n";
184 if (daFit != 0) {
185 TrkExchangePar par = daFit->helix(0.);
186 ostr << "phi0: " << par.phi0()
187 << " om: "<< par.omega()
188 << " d0: " << par.d0() << " z0: " << par.z0()
189 << " ct: " << par.tanDip();
190 }
191 ostr << std::endl;
192}
193
194void
195TrkRecoTrk::printAll(ostream& ostr) const
196{
197 // This should be expanded to print other hypotheses as well
198 ostr << "Trk: " << id() << " def: "
200 << " fitNumber:" << fitNumber(defaultType());
201 const TrkFit* daFit = fitResult();
202 const TrkHitList* daList = hits();
203 const TrkFitStatus* daStatus = status();
204 if (daList != 0) {
205 ostr << " nhit: " << daList->nHit();
206 }
207 if (daFit != 0) {
208 ostr << " nactive: " << daFit->nActive() << " chisq: " << daFit->chisq();
209 }
210 if (daStatus != 0) {
211 ostr << " 3-d: " << (daStatus->is2d() == 0);
212 }
213 ostr << " t0: " << _trackT0 << "\n";
214 getRep(defaultType())->printAll(ostr);
215 // allReps();//yzhang debug
216 ostr << std::endl;
217}
218
221{
222 // If there is no fit, create one. If hypo points to a fit for a different
223 // particle type, create a fit of type hypo, and point at that. Carry
224 // out the fit if needed.
225 if (hits() == 0) {
226 // Unfittable rep
227 return TrkErrCode(TrkErrCode::fail, 11,
228 "TrkRecoTrk::addFit(): cannot add a fit to this track.");
229 }
230 if (whichFit(hypo) == hypo) {
232 "TrkRecoTrk::addFit(): requested fit already exists.");
233 }
234 _impl->_reps[hypo].reset( _impl->_reps[defaultType()]->cloneNewHypo(hypo) );
236 if (fit && !_impl->_reps[hypo]->fitCurrent()) {
237 fitErr = _impl->_reps[hypo]->fit();
238 }
239 ++_fitNumber[hypo];
240 return fitErr;
241}
242
243void
245{
246 _trackT0 = t;
247 updateReps();
248}
249
250void
252{
253 std::pair<TrkRepIter,TrkRepIter> x = uniqueReps();
254 std::for_each(x.first,x.second,std::mem_fun_ref(&TrkRep::updateHots));
255 std::transform(_fitNumber.begin(),_fitNumber.end(),
256 _fitNumber.begin(),
257 std::bind2nd(std::plus<int>(),1));
258}
259
260bool
262{
263 return _id == other._id;
264}
265
266bool
267TrkRecoTrk::operator<(const TrkRecoTrk &other) const
268{
269 return _id < other._id;
270}
271
272//***********************
273// Protected functions
274//***********************
275
276TrkRep*
278{
279 assert(hypo>=PdtPid::electron && hypo<= PdtPid::proton);
280 TrkRep* theRep = _impl->_reps[hypo].get();
281// insist the default rep exist
282 if(hypo == defaultType())assert(0 != theRep);
283 return theRep;
284}
285
286const TrkRep*
288{
289 assert(hypo>=PdtPid::electron && hypo<= PdtPid::proton);
290 const TrkRep* theRep = _impl->_reps[hypo].get();
291 if(hypo == defaultType())assert(0 != theRep);
292 return theRep;
293}
294
295void
297{
298 if (newHypo == defaultType()) return;
299 assert(whichFit(newHypo) != PdtPid::null);
300
301 TrkHotList *oldList = getRep(defaultType())->hotList();
302 std::for_each(oldList->begin(),
303 oldList->end(),
304 std::mem_fun_ref(&TrkHitOnTrk::setUnusedHit));
305 assert(getRep(newHypo) != 0);
306 TrkHotList *newList= getRep(newHypo)->hotList();
307 std::for_each(newList->begin(),
308 newList->end(),
309 std::mem_fun_ref(&TrkHitOnTrk::setUsedHit));
310 _defaultType = newHypo;
311}
312
313void
315{
316 TrkRecoTrkImpl::repIter lhs = _impl->_reps.begin();
317 for (TrkRecoTrkImpl::repIter i=rhs._impl->_reps.begin();i!=rhs._impl->_reps.end();++i,++lhs) {
318 TrkRecoTrkImpl::repIter j=std::find(rhs._impl->_reps.begin(),i,*i);
319 if (j==i) { // first time this one is seen
320 lhs->reset( (*i)->clone(this) );
321 (*lhs)->setValid((*i)->fitValid());
322 (*lhs)->setCurrent((*i)->fitCurrent());
323 } else {
324 *lhs = *(_impl->_reps.begin()+(j-rhs._impl->_reps.begin()));
325 }
326 }
327 assert(_fitNumber.size()==rhs._fitNumber.size());
328 std::copy(rhs._fitNumber.begin(),rhs._fitNumber.end(),_fitNumber.begin());
329}
330
331void
333{
334 // Sets the default rep to be r, clears out other reps., and sets
335 // non-default rep ptrs to point at default. Increments all fit numbers.
336 std::fill(_impl->_reps.begin(),_impl->_reps.end(),boost::shared_ptr<TrkRep>(r));
337 std::transform(_fitNumber.begin(),_fitNumber.end(),
338 _fitNumber.begin(),
339 std::bind2nd(std::plus<int>(),1));
340}
341
342void
344{
345 // Do we have to do anything?
346 if (fit == hypo || getRep(fit) == getRep(hypo)) return;
347
348 if (hypo == defaultType()) {
349 std::cout << "ErrMsg(error) "<<
350 "TrkRecoTrk: can't make default hypothesis point at different fit"
351 << std::endl;
352 return;
353 }
354 _impl->_reps[hypo] = _impl->_reps[fit];
355}
356
357bool
359{
360 const TrkRep* rp = getRep(hypo);
361 return rp!=0?interface.attach(rp):0;
362}
363
364
365bool
367{
368 TrkRep* rp = getRep(hypo);
369 return rp!=0?interface.attach(rp):0;
370}
371
374{
375 const TrkRep* rp = getRep(hypo);
376 return rp==0?0:_impl->_hitInterfaces[hypo].get();
377}
378
379const TrkHitList*
381{
382 const TrkRep* rp = getRep(hypo);
383 return rp==0?0:_impl->_hitInterfaces[hypo].get();
384}
385
386const TrkFit*
388{
389 return fitResult(defaultType());
390}
391
392const TrkFit*
394{
395 const TrkRep* rp = getRep(hypo);
396 return rp==0?0:(rp->fitValid()?rp:0);
397}
398
399const TrkFitStatus*
401{
402 return status(defaultType());
403}
404
405const TrkFitStatus*
407{
408 return getRep(hypo);
409}
410
413{
414 return status(defaultType());
415}
416
419{
420 return getRep(hypo);
421}
422
423void
425{
426 _fitNumber[hypo] = newNumber;
427}
428
429void
431{
432 _impl->_reps[hypo].reset( newRep );
433}
434
435void
437{
438 _id.setIdManager(idMan);
439}
440
441ostream&
442operator<<(ostream& os, const TrkRecoTrk& tk)
443{
444 tk.print(os);
445 return os;
446}
447
448void
450{
451 _bField = field;
452}
453
454void
456 const char* listname) {
457// first, translate to the real hypo
458 PdtPid::PidType realhypo = whichFit(hypo);
459// Then, make sure this hypo has a valid fit
460 if(getRep(realhypo)!= 0 && getRep(realhypo)->fitValid())
461// add an entry in the toStore list (if it's unique)
462 _storage[std::string(listname)].insert(TrkStoreHypo(realhypo,fltlen));
463 else
464// It's an error to try to store invalid fits
465 std::cout<<"ErrMsg(error) "<< "Invalid fits cannot be marked for storage" << std::endl;
466}
467
468const std::set<TrkStoreHypo>&
469TrkRecoTrk::storageRequests(const char* listname) const {
470 static std::set<TrkStoreHypo> empty; // empty set to return if list doesn't exist
471 std::map<std::string,std::set<TrkStoreHypo> >::const_iterator foundit =
472 _storage.find(std::string(listname));
473 if(foundit != _storage.end())
474 return foundit->second;
475 else
476 return empty;
477}
478
479void
480TrkRecoTrk::clearStorageRequests(const char* listname) {
481 _storage[std::string(listname)].clear();
482}
483
484void
485TrkRecoTrk::storageLists(std::set<std::string>& storage) const {
486 // clear the output set
487 storage.clear();
488 // iterate over all the storage requests
489 std::map<std::string,std::set<TrkStoreHypo> >::const_iterator miter = _storage.begin();
490 while(miter != _storage.end()){
491 storage.insert(miter->first);
492 miter++;
493 }
494}
495
498{
499 TrkRep* rp = getRep(hypo);
500 return rp==0?0:rp->hotList();
501}
502
503const TrkHotList*
505{
506 const TrkRep* rp = getRep(hypo);
507 return rp==0?0:rp->hotList();
508}
509
510std::pair<TrkRepIter,TrkRepIter>
512{
513 typedef std::vector<TrkRep*> RPL;
514 boost::shared_ptr<RPL> x( new RPL );
515 //std::cout << " TrkRecoTrk::allReps" << std::endl;//yzhang debug
516
517 for (TrkRecoTrkImpl::repConstIter i=_impl->_reps.begin();i!=_impl->_reps.end();++i) {
518 x->push_back(i->get());
519 /*
520 i->get()->printType(std::cout);//yzhang debug
521 i->get()->printAll(std::cout);//yzhang debug
522 */
523 }
524 //std::cout << "------ " << std::endl;//yzhang debug
525 return std::make_pair(TrkRepIter(x,0),TrkRepIter(x,x->size()));
526}
527
528std::pair<TrkRepIter,TrkRepIter>
530{
531 typedef std::vector<TrkRep*> RPL;
532 boost::shared_ptr<RPL> x( new RPL );
533 for (TrkRecoTrkImpl::repConstIter i=_impl->_reps.begin();i!=_impl->_reps.end();++i) {
534 if (std::find(x->begin(),x->end(),i->get())==x->end()) x->push_back(i->get());
535
536 }
537 return std::make_pair(TrkRepIter(x,0),TrkRepIter(x,x->size()));
538}
Double_t x[10]
ostream & operator<<(ostream &os, const TrkRecoTrk &tk)
Definition: TrkRecoTrk.cxx:442
static PdtEntry * lookup(const std::string &name)
Definition: Pdt.cxx:207
virtual void printAll(std::ostream &ostr) const =0
virtual double chisq() const =0
virtual bool attach(TrkRep *)
virtual int nActive() const =0
virtual TrkExchangePar helix(double fltL) const =0
void setUnusedHit()
void setUsedHit()
void setIdManager(TrkIdManager *idMan)
Definition: TrkId.cxx:93
void setNewValue(const TrkId &)
Definition: TrkId.cxx:77
void repointHypo(PdtPid::PidType hypo, PdtPid::PidType fit)
Definition: TrkRecoTrk.cxx:343
virtual void print(std::ostream &) const
double trackT0() const
Definition: TrkRecoTrk.cxx:140
PdtPid::PidType whichFit(PdtPid::PidType hypo) const
Definition: TrkRecoTrk.cxx:146
void addHypoTo(TrkRep *newRep, PdtPid::PidType hypo)
Definition: TrkRecoTrk.cxx:430
void clearStorageRequests(const char *listname="Default")
Definition: TrkRecoTrk.cxx:480
void changeDefault(PdtPid::PidType newHypo)
Definition: TrkRecoTrk.cxx:296
int fitNumber(PdtPid::PidType hypo) const
Definition: TrkRecoTrk.cxx:157
void setFitNumber(PdtPid::PidType hypo, int newNumber)
Definition: TrkRecoTrk.cxx:424
void markForStore(PdtPid::PidType hypo, double fltlen, const char *listname="Default")
Definition: TrkRecoTrk.cxx:455
void setRep(TrkRep *)
Definition: TrkRecoTrk.cxx:332
const TrkRecoTrk & operator=(const TrkRecoTrk &right)
Definition: TrkRecoTrk.cxx:120
void resetT0(double time)
Definition: TrkRecoTrk.cxx:244
TrkRecoTrk(const TrkRecoTrk &right)
Definition: TrkRecoTrk.cxx:97
void updateReps()
Definition: TrkRecoTrk.cxx:251
std::pair< TrkRepIter, TrkRepIter > uniqueReps() const
Definition: TrkRecoTrk.cxx:529
bool operator<(const TrkRecoTrk &other) const
Definition: TrkRecoTrk.cxx:267
bool operator==(const TrkRecoTrk &other) const
Definition: TrkRecoTrk.cxx:261
virtual ~TrkRecoTrk()
Definition: TrkRecoTrk.cxx:114
void copyReps(const TrkRecoTrk &rhs)
Definition: TrkRecoTrk.cxx:314
const TrkId & id() const
Definition: TrkRecoTrk.cxx:134
void setIdManager(TrkIdManager *idMan)
Definition: TrkRecoTrk.cxx:436
TrkRep * getRep(PdtPid::PidType hypo)
Definition: TrkRecoTrk.cxx:277
std::pair< TrkRepIter, TrkRepIter > allReps() const
Definition: TrkRecoTrk.cxx:511
void storageLists(std::set< std::string > &storage) const
Definition: TrkRecoTrk.cxx:485
const TrkFit * fitResult() const
Definition: TrkRecoTrk.cxx:387
virtual void printAll(std::ostream &) const
const TrkFitStatus * status() const
Definition: TrkRecoTrk.cxx:400
bool attach(TrkExtInterface &, PdtPid::PidType hypo)
Definition: TrkRecoTrk.cxx:366
const std::set< TrkStoreHypo > & storageRequests(const char *listname="Default") const
Definition: TrkRecoTrk.cxx:469
void setBField(const BField *field)
Definition: TrkRecoTrk.cxx:449
TrkErrCode addFit(PdtPid::PidType hypo, bool fit=true)
Definition: TrkRecoTrk.cxx:220
virtual void updateHots()
Definition: TrkRep.cxx:323
virtual TrkHotList * hotList()
int t()
Definition: t.c:1