BOSS 8.0.0
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 "TrkBase/TrkRecoTrk.h"
12#include "CLHEP/Vector/ThreeVector.h"
13#include "MdcGeom/Constants.h"
14#include "MdcRecoUtil/BesPointErr.h"
15#include "MdcRecoUtil/BesVectorErr.h"
16#include "MdcRecoUtil/DifIndepPar.h"
17#include "MdcRecoUtil/DifPoint.h"
18#include "MdcRecoUtil/DifVector.h"
19#include "MdcRecoUtil/Pdt.h"
20#include "MdcRecoUtil/PdtPid.h"
21#include "PatBField/BField.h"
22#include "TrkBase/TrkContext.h"
23#include "TrkBase/TrkDifTraj.h"
24#include "TrkBase/TrkErrCode.h"
25#include "TrkBase/TrkExchangePar.h"
26#include "TrkBase/TrkExtInterface.h"
27#include "TrkBase/TrkFitStatus.h"
28#include "TrkBase/TrkFundHit.h"
29#include "TrkBase/TrkHitOnTrk.h"
30#include "TrkBase/TrkRep.h"
31#include "TrkBase/TrkRepIter.h"
32#include "boost/shared_ptr.hpp"
33#include <algorithm>
34#include <assert.h>
35#include <functional>
36#include <vector>
38using std::ostream;
39
40class TrkRecoTrkImpl {
41public:
42 friend class TrkRecoTrk;
43
44private:
45 TrkRecoTrkImpl()
46 : _reps( PdtPid::nPidType, boost::shared_ptr<TrkRep>( (TrkRep*)0 ) )
47 , _hitInterfaces( PdtPid::nPidType, boost::shared_ptr<TrkHitList>( (TrkHitList*)0 ) ) {}
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
61TrkRecoTrk::TrkRecoTrk( PdtPid::PidType defaultPart, const TrkContext& ctext, double t0 )
62 : _impl( new TrkRecoTrkImpl )
63 , _id( ctext.getId() )
64 , _fitNumber( PdtPid::nPidType, (int)0 )
65 , _defaultType( defaultPart )
66 , _trackT0( t0 )
67 , _bField( ctext.bField() ) {
68 // No TrkRep is defined here; must be created in appropriate FitMaker.
69 unsigned i = 0;
70 for ( TrkRecoTrkImpl::hitListIter iface = _impl->_hitInterfaces.begin();
71 iface != _impl->_hitInterfaces.end(); ++iface )
72 {
73 iface->reset( new TrkHitList( this, (PdtPid::PidType)i++ ) ); // cast
74 }
75}
76
77// persistence reconstitution. This sets a nul value for BField and IdManager
78TrkRecoTrk::TrkRecoTrk( PdtPid::PidType defaultPart, long idnum, double t0 )
79 : _impl( new TrkRecoTrkImpl )
80 , _id( idnum, 0 )
81 , _fitNumber( PdtPid::nPidType, (int)0 )
82 , _defaultType( defaultPart )
83 , _trackT0( t0 )
84 , _bField( 0 ) {
85 // No TrkRep is defined here; must be created in appropriate FitMaker.
86 unsigned i = 0;
87 for ( TrkRecoTrkImpl::hitListIter iface = _impl->_hitInterfaces.begin();
88 iface != _impl->_hitInterfaces.end(); ++iface )
89 {
90 iface->reset( new TrkHitList( this, (PdtPid::PidType)i++ ) ); // cast
91 }
92}
93
94//-- Copy constructor
96 : _impl( new TrkRecoTrkImpl )
97 , _id( rhs._id.idManager() )
98 , _fitNumber( PdtPid::nPidType, (int)0 )
99 , _storage( rhs._storage )
100 , _trackT0( rhs._trackT0 )
101 , _bField( rhs._bField ) {
102 _defaultType = rhs.defaultType();
103 unsigned i = 0;
104 for ( TrkRecoTrkImpl::hitListIter iface = _impl->_hitInterfaces.begin();
105 iface != _impl->_hitInterfaces.end(); ++iface )
106 {
107 iface->reset( new TrkHitList( this, (PdtPid::PidType)i++ ) ); // cast
108 }
109 copyReps( rhs );
110}
111
112TrkRecoTrk::~TrkRecoTrk() { delete _impl; }
113
115 if ( &right == this ) return *this;
116 _trackT0 = right._trackT0;
117 _defaultType = right.defaultType();
118 copyReps( right );
119 _bField = right._bField;
120 // AbsEvtObj::operator=(right);
121 _id.setNewValue( right._id );
122 _storage = right._storage;
123 return *this;
124}
125
126const TrkId& TrkRecoTrk::id() const { return _id; }
127
128double TrkRecoTrk::trackT0() const { return _trackT0; }
129
131 if ( _impl->_reps[hypo].get() == 0 ) { return hypo; }
132 // return _repPtr[hypo]->fitValid() ? _repPtr[hypo]->particleType()
133 // : PdtPid::null;
134 return _impl->_reps[hypo]->particleType();
135}
136
138 PdtPid::PidType used = whichFit( hypo );
139 if ( used == PdtPid::null ) return -1;
140 int index = used;
141 return _fitNumber[index];
142}
143
144void TrkRecoTrk::print( ostream& ostr ) const {
145 ostr << "Trk: " << id() << " def: " << Pdt::lookup( defaultType() )->name()
146 << " fitNumber:" << fitNumber( defaultType() );
147 const TrkFit* daFit = fitResult();
148 const TrkHitList* daList = hits();
149 const TrkFitStatus* daStatus = status();
150 if ( daList != 0 ) { ostr << " nhit: " << daList->nHit(); }
151 if ( daFit != 0 )
152 { ostr << " nactive: " << daFit->nActive() << " chisq: " << daFit->chisq(); }
153 if ( daStatus != 0 ) { ostr << " 3-d: " << ( daStatus->is2d() == 0 ); }
154 ostr << " t0: " << _trackT0 << "\n";
155 if ( daFit != 0 )
156 {
157 TrkExchangePar par = daFit->helix( 0. );
158 ostr << "phi0: " << par.phi0() << " om: " << par.omega() << " d0: " << par.d0()
159 << " z0: " << par.z0() << " ct: " << par.tanDip();
160 }
161 ostr << std::endl;
162}
163
164void TrkRecoTrk::printAll( ostream& ostr ) const {
165 // This should be expanded to print other hypotheses as well
166 ostr << "Trk: " << id() << " def: " << Pdt::lookup( defaultType() )->name()
167 << " fitNumber:" << fitNumber( defaultType() );
168 const TrkFit* daFit = fitResult();
169 const TrkHitList* daList = hits();
170 const TrkFitStatus* daStatus = status();
171 if ( daList != 0 ) { ostr << " nhit: " << daList->nHit(); }
172 if ( daFit != 0 )
173 { ostr << " nactive: " << daFit->nActive() << " chisq: " << daFit->chisq(); }
174 if ( daStatus != 0 ) { ostr << " 3-d: " << ( daStatus->is2d() == 0 ); }
175 ostr << " t0: " << _trackT0 << "\n";
176 getRep( defaultType() )->printAll( ostr );
177 // allReps();//yzhang debug
178 ostr << std::endl;
179}
180
182 // If there is no fit, create one. If hypo points to a fit for a different
183 // particle type, create a fit of type hypo, and point at that. Carry
184 // out the fit if needed.
185 if ( hits() == 0 )
186 {
187 // Unfittable rep
188 return TrkErrCode( TrkErrCode::fail, 11,
189 "TrkRecoTrk::addFit(): cannot add a fit to this track." );
190 }
191 if ( whichFit( hypo ) == hypo )
192 {
194 "TrkRecoTrk::addFit(): requested fit already exists." );
195 }
196 _impl->_reps[hypo].reset( _impl->_reps[defaultType()]->cloneNewHypo( hypo ) );
197 TrkErrCode fitErr( TrkErrCode::succeed, 1 );
198 if ( fit && !_impl->_reps[hypo]->fitCurrent() ) { fitErr = _impl->_reps[hypo]->fit(); }
199 ++_fitNumber[hypo];
200 return fitErr;
201}
202
203void TrkRecoTrk::resetT0( double t ) {
204 _trackT0 = t;
205 updateReps();
206}
207
209 std::pair<TrkRepIter, TrkRepIter> x = uniqueReps();
210 std::for_each( x.first, x.second, std::mem_fun_ref( &TrkRep::updateHots ) );
211 std::transform( _fitNumber.begin(), _fitNumber.end(), _fitNumber.begin(),
212 std::bind2nd( std::plus<int>(), 1 ) );
213}
214
215bool TrkRecoTrk::operator==( const TrkRecoTrk& other ) const { return _id == other._id; }
216
217bool TrkRecoTrk::operator<( const TrkRecoTrk& other ) const { return _id < other._id; }
218
219//***********************
220// Protected functions
221//***********************
222
224 assert( hypo >= PdtPid::electron && hypo <= PdtPid::proton );
225 TrkRep* theRep = _impl->_reps[hypo].get();
226 // insist the default rep exist
227 if ( hypo == defaultType() ) assert( 0 != theRep );
228 return theRep;
229}
230
232 assert( hypo >= PdtPid::electron && hypo <= PdtPid::proton );
233 const TrkRep* theRep = _impl->_reps[hypo].get();
234 if ( hypo == defaultType() ) assert( 0 != theRep );
235 return theRep;
236}
237
239 if ( newHypo == defaultType() ) return;
240 assert( whichFit( newHypo ) != PdtPid::null );
241
242 TrkHotList* oldList = getRep( defaultType() )->hotList();
243 std::for_each( oldList->begin(), oldList->end(),
244 std::mem_fun_ref( &TrkHitOnTrk::setUnusedHit ) );
245 assert( getRep( newHypo ) != 0 );
246 TrkHotList* newList = getRep( newHypo )->hotList();
247 std::for_each( newList->begin(), newList->end(),
248 std::mem_fun_ref( &TrkHitOnTrk::setUsedHit ) );
249 _defaultType = newHypo;
250}
251
253 TrkRecoTrkImpl::repIter lhs = _impl->_reps.begin();
254 for ( TrkRecoTrkImpl::repIter i = rhs._impl->_reps.begin(); i != rhs._impl->_reps.end();
255 ++i, ++lhs )
256 {
257 TrkRecoTrkImpl::repIter j = std::find( rhs._impl->_reps.begin(), i, *i );
258 if ( j == i )
259 { // first time this one is seen
260 lhs->reset( ( *i )->clone( this ) );
261 ( *lhs )->setValid( ( *i )->fitValid() );
262 ( *lhs )->setCurrent( ( *i )->fitCurrent() );
263 }
264 else { *lhs = *( _impl->_reps.begin() + ( j - rhs._impl->_reps.begin() ) ); }
265 }
266 assert( _fitNumber.size() == rhs._fitNumber.size() );
267 std::copy( rhs._fitNumber.begin(), rhs._fitNumber.end(), _fitNumber.begin() );
268}
269
271 // Sets the default rep to be r, clears out other reps., and sets
272 // non-default rep ptrs to point at default. Increments all fit numbers.
273 std::fill( _impl->_reps.begin(), _impl->_reps.end(), boost::shared_ptr<TrkRep>( r ) );
274 std::transform( _fitNumber.begin(), _fitNumber.end(), _fitNumber.begin(),
275 std::bind2nd( std::plus<int>(), 1 ) );
276}
277
279 // Do we have to do anything?
280 if ( fit == hypo || getRep( fit ) == getRep( hypo ) ) return;
281
282 if ( hypo == defaultType() )
283 {
284 std::cout << "ErrMsg(error) "
285 << "TrkRecoTrk: can't make default hypothesis point at different fit"
286 << std::endl;
287 return;
288 }
289 _impl->_reps[hypo] = _impl->_reps[fit];
290}
291
292bool TrkRecoTrk::attach( TrkExtInterface& interface, PdtPid::PidType hypo ) const {
293 const TrkRep* rp = getRep( hypo );
294 return rp != 0 ? interface.attach( rp ) : 0;
295}
296
298 TrkRep* rp = getRep( hypo );
299 return rp != 0 ? interface.attach( rp ) : 0;
300}
301
303 const TrkRep* rp = getRep( hypo );
304 return rp == 0 ? 0 : _impl->_hitInterfaces[hypo].get();
305}
306
308 const TrkRep* rp = getRep( hypo );
309 return rp == 0 ? 0 : _impl->_hitInterfaces[hypo].get();
310}
311
312const TrkFit* TrkRecoTrk::fitResult() const { return fitResult( defaultType() ); }
313
315 const TrkRep* rp = getRep( hypo );
316 return rp == 0 ? 0 : ( rp->fitValid() ? rp : 0 );
317}
318
319const TrkFitStatus* TrkRecoTrk::status() const { return status( defaultType() ); }
320
321const TrkFitStatus* TrkRecoTrk::status( PdtPid::PidType hypo ) const { return getRep( hypo ); }
322
324
326
327void TrkRecoTrk::setFitNumber( PdtPid::PidType hypo, int newNumber ) {
328 _fitNumber[hypo] = newNumber;
329}
330
332 _impl->_reps[hypo].reset( newRep );
333}
334
335void TrkRecoTrk::setIdManager( TrkIdManager* idMan ) { _id.setIdManager( idMan ); }
336
337ostream& operator<<( ostream& os, const TrkRecoTrk& tk ) {
338 tk.print( os );
339 return os;
340}
341
342void TrkRecoTrk::setBField( const BField* field ) { _bField = field; }
343
344void TrkRecoTrk::markForStore( PdtPid::PidType hypo, double fltlen, const char* listname ) {
345 // first, translate to the real hypo
346 PdtPid::PidType realhypo = whichFit( hypo );
347 // Then, make sure this hypo has a valid fit
348 if ( getRep( realhypo ) != 0 && getRep( realhypo )->fitValid() )
349 // add an entry in the toStore list (if it's unique)
350 _storage[std::string( listname )].insert( TrkStoreHypo( realhypo, fltlen ) );
351 else
352 // It's an error to try to store invalid fits
353 std::cout << "ErrMsg(error) "
354 << "Invalid fits cannot be marked for storage" << std::endl;
355}
356
357const std::set<TrkStoreHypo>& TrkRecoTrk::storageRequests( const char* listname ) const {
358 static std::set<TrkStoreHypo> empty; // empty set to return if list doesn't exist
359 std::map<std::string, std::set<TrkStoreHypo>>::const_iterator foundit =
360 _storage.find( std::string( listname ) );
361 if ( foundit != _storage.end() ) return foundit->second;
362 else return empty;
363}
364
365void TrkRecoTrk::clearStorageRequests( const char* listname ) {
366 _storage[std::string( listname )].clear();
367}
368
369void TrkRecoTrk::storageLists( std::set<std::string>& storage ) const {
370 // clear the output set
371 storage.clear();
372 // iterate over all the storage requests
373 std::map<std::string, std::set<TrkStoreHypo>>::const_iterator miter = _storage.begin();
374 while ( miter != _storage.end() )
375 {
376 storage.insert( miter->first );
377 miter++;
378 }
379}
380
382 TrkRep* rp = getRep( hypo );
383 return rp == 0 ? 0 : rp->hotList();
384}
385
387 const TrkRep* rp = getRep( hypo );
388 return rp == 0 ? 0 : rp->hotList();
389}
390
391std::pair<TrkRepIter, TrkRepIter> TrkRecoTrk::allReps() const {
392 typedef std::vector<TrkRep*> RPL;
393 boost::shared_ptr<RPL> x( new RPL );
394 // std::cout << " TrkRecoTrk::allReps" << std::endl;//yzhang debug
395
396 for ( TrkRecoTrkImpl::repConstIter i = _impl->_reps.begin(); i != _impl->_reps.end(); ++i )
397 {
398 x->push_back( i->get() );
399 /*
400 i->get()->printType(std::cout);//yzhang debug
401 i->get()->printAll(std::cout);//yzhang debug
402 */
403 }
404 // std::cout << "------ " << std::endl;//yzhang debug
405 return std::make_pair( TrkRepIter( x, 0 ), TrkRepIter( x, x->size() ) );
406}
407
408std::pair<TrkRepIter, TrkRepIter> TrkRecoTrk::uniqueReps() const {
409 typedef std::vector<TrkRep*> RPL;
410 boost::shared_ptr<RPL> x( new RPL );
411 for ( TrkRecoTrkImpl::repConstIter i = _impl->_reps.begin(); i != _impl->_reps.end(); ++i )
412 {
413 if ( std::find( x->begin(), x->end(), i->get() ) == x->end() ) x->push_back( i->get() );
414 }
415 return std::make_pair( TrkRepIter( x, 0 ), TrkRepIter( x, x->size() ) );
416}
Double_t x[10]
ostream & operator<<(ostream &os, const TrkRecoTrk &tk)
static PdtEntry * lookup(const std::string &name)
Definition Pdt.cxx:183
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()
friend class TrkRecoTrk
void repointHypo(PdtPid::PidType hypo, PdtPid::PidType fit)
virtual void print(std::ostream &) const
double trackT0() const
PdtPid::PidType whichFit(PdtPid::PidType hypo) const
void addHypoTo(TrkRep *newRep, PdtPid::PidType hypo)
void clearStorageRequests(const char *listname="Default")
void changeDefault(PdtPid::PidType newHypo)
int fitNumber(PdtPid::PidType hypo) const
void setFitNumber(PdtPid::PidType hypo, int newNumber)
void markForStore(PdtPid::PidType hypo, double fltlen, const char *listname="Default")
void setRep(TrkRep *)
const TrkRecoTrk & operator=(const TrkRecoTrk &right)
void resetT0(double time)
TrkRecoTrk(const TrkRecoTrk &right)
void updateReps()
std::pair< TrkRepIter, TrkRepIter > uniqueReps() const
bool operator<(const TrkRecoTrk &other) const
bool operator==(const TrkRecoTrk &other) const
virtual ~TrkRecoTrk()
void copyReps(const TrkRecoTrk &rhs)
const TrkId & id() const
void setIdManager(TrkIdManager *idMan)
TrkRep * getRep(PdtPid::PidType hypo)
std::pair< TrkRepIter, TrkRepIter > allReps() const
void storageLists(std::set< std::string > &storage) const
const TrkFit * fitResult() const
virtual void printAll(std::ostream &) const
const TrkFitStatus * status() const
bool attach(TrkExtInterface &, PdtPid::PidType hypo)
const std::set< TrkStoreHypo > & storageRequests(const char *listname="Default") const
void setBField(const MdcPatRec::BField *field)
TrkErrCode addFit(PdtPid::PidType hypo, bool fit=true)
virtual void updateHots()
Definition TrkRep.cxx:258
int t()
Definition t.c:1