00001
00002
00003
00004
00005 #include "Stiostream.h"
00006 #include <fstream>
00007 #include <math.h>
00008 #include <algorithm>
00009 #include "Sti/Base/Filter.h"
00010 #include "StiKalmanTrackNode.h"
00011 #include "StiHit.h"
00012 #include "StiPlacement.h"
00013 #include "StiDetector.h"
00014 #include "StiHitContainer.h"
00015 #include <float.h>
00016
00017 using std::sort;
00018 using std::find;
00019 using std::lower_bound;
00020 using std::upper_bound;
00021 using std::stable_partition;
00022 ostream& operator<<(ostream& os, const StiHit& hit);
00023 ostream& operator<<(ostream&, const HitMapKey&);
00024
00025 int VectorAndEnd::fIdCounter = 0;
00026
00027 VectorAndEnd::VectorAndEnd(): fEffectiveEndValid(false)
00028 {
00029 invalidateEnd();
00030 fId=fIdCounter++;
00031 theEffectiveEnd=theHitVec.end();
00032 TestId(568);
00033 }
00034
00035
00036 void VectorAndEnd::TestId(int id)
00037 {
00038 if ( fId == id) {
00039 printf(" Id = %d \n",fId);
00040 }
00041 }
00042
00043 void VectorAndEnd:: clear()
00044 {
00045 theHitVec.clear();
00046 invalidateEnd();
00047 }
00048
00049 StiHitContainer::StiHitContainer(const string & name,
00050 const string & description,
00051 Factory<StiHit> *hitFactory)
00052 : Named(name),
00053 Described(description),
00054 _hitFactory(hitFactory)
00055 {
00056 cout <<"StiHitContainer::StiHitContainer() -I- Started with name:"<<name<<endl;
00057
00058
00059 }
00060
00061 StiHitContainer::~StiHitContainer()
00062 {
00063 cout <<"StiHitContainer::~StiHitContainer()"<<endl;
00064 }
00065
00072
00073
00074
00087 void StiHitContainer::add(StiHit* hit)
00088 {
00089 const StiDetector* det = hit->detector();
00090 if (!det)
00091 throw runtime_error("StiHitContainer::add() -E- Given hit has no associated detector");
00092
00093 _key.refangle = det->getPlacement()->getLayerAngle();
00094
00095 _key.position = det->getPlacement()->getLayerRadius();
00096 _map[_key].push_back(hit);
00097 return;
00098 }
00099
00100 void StiHitContainer::reset()
00101 {
00102 HitMapToVectorAndEndType::iterator it;
00103 vector<StiHit*>::iterator iter;
00104
00105 for (it=_map.begin(); it!=_map.end(); it++)
00106 {
00107 vector<StiHit*> &hits = (*it).second.hits();
00108
00109 for (iter=hits.begin();iter!=hits.end();iter++)
00110 {
00111 (*iter)->setTimesUsed(0);
00112 }
00113 }
00114 }
00115
00121 void StiHitContainer::clear()
00122 {
00123 cout<<"StiHitContainer::clear() -I- Started"<<endl;
00124 HitMapToVectorAndEndType::iterator it;
00125 for (it=_map.begin(); it!=_map.end(); it++)
00126 {
00127 (*it).second.clear();
00128 }
00129 cout<<"StiHitContainer::clear() -I- Done"<<endl;
00130 }
00131
00136 unsigned int StiHitContainer::size() const
00137 {
00138 unsigned int thesize = 0;
00139 HitMapToVectorAndEndType::const_iterator it;
00140 for (it=_map.begin(); it!=_map.end(); it++) {
00141 thesize+=(*it).second.size();
00142 }
00143 return thesize;
00144 }
00145
00146
00147 vector<StiHit*>::iterator StiHitContainer::hitsBegin(const StiDetector* layer)
00148 {
00149
00150 _key.refangle = layer->getPlacement()->getLayerAngle();
00151
00152 _key.position = layer->getPlacement()->getLayerRadius();
00153 assert(_map.find(_key) != _map.end());
00154 return _map[_key].begin();
00155 }
00156
00157 vector<StiHit*>::iterator StiHitContainer::hitsEnd(const StiDetector* layer)
00158 {
00159
00160 _key.refangle = layer->getPlacement()->getLayerAngle();
00161
00162 _key.position = layer->getPlacement()->getLayerRadius();
00163 assert(_map.find(_key) != _map.end());
00164 return _map[_key].TheEffectiveEnd();
00165 }
00166
00167
00193 vector<StiHit*> & StiHitContainer::getHits(StiHit& ref, double dY, double dZ, bool fetchAll)
00194 {
00195 _selectedHits.clear();
00196 _key.refangle = ref.refangle();
00197 _key.position = ref.position();
00198
00199
00200
00201 _minPoint.set(ref.position(),ref.refangle(),ref.y()-dY,ref.z()-dZ );
00202 _maxPoint.set(ref.position(),ref.refangle(),ref.y()+dY,ref.z()+dZ );
00203
00204 static int id = 0;
00205 if (_map.find(_key) != _map.end()) {
00206 vector<StiHit*>& tempvec = _map[_key].hits();
00207 if (tempvec.size())
00208 {
00209 id = _map[_key].fId;
00210
00211 vector<StiHit*>::iterator tempend = _map[_key].TheEffectiveEnd();
00212
00213 #if 1
00214
00215 vector<StiHit*>::iterator tmptest = tempvec.begin();
00216 vector<StiHit*>::iterator tmpend = tempvec.end();
00217 if (!tempvec.size() ) {
00218 cout << "-- Doing tmptest for id:" << id<< " " << ( tmpend != tmptest )
00219 << " cmp " << ( tempend != tmptest )
00220 << " " << tempvec.size() << " --> " << endl;
00221 assert(0);
00222 }
00223 #endif
00224
00225
00226 _start = lower_bound(tempvec.begin(), tempend, &_minPoint, StizHitLessThan());
00227 if (_start!=tempend)
00228 _stop = upper_bound(tempvec.begin(), tempend, &_maxPoint, StizHitLessThan());
00229 else
00230 {
00231 _start = tempend;
00232 _stop = _start;
00233 }
00234
00235 StiHit * hit;
00236 for (vector<StiHit*>::iterator cit=_start; cit!=_stop; cit++)
00237 {
00238 hit = *cit;
00239 if (fabs( hit->y() - ref.y() ) < dY)
00240 {
00241 if (fetchAll || (hit->timesUsed()==0 && hit->detector()->isActive()) )
00242 _selectedHits.push_back(hit);
00243 }
00244 }
00245 } else {
00246
00247
00248 }
00249 }
00250 #if 0
00251 else {
00252 cout << "-- Go ahead to test thing ------------------ last id =" << id << endl;
00253 id = _map[_key].fId;
00254 cout << "-- Go ahead to test thing ------------------ next id =" << id << endl;
00255 }
00256 #endif
00257 return _selectedHits;
00258 }
00259
00274 void StiHitContainer::sortHits()
00275 {
00276 HitMapToVectorAndEndType::iterator it;
00277 for (it=_map.begin(); it!=_map.end(); ++it)
00278 {
00279 vector<StiHit*>& tempvec = (*it).second.hits();
00280 sort(tempvec.begin(), tempvec.end(), StizHitLessThan());
00281 (*it).second.invalidateEnd();
00282 }
00283 return;
00284 }
00285
00286 void StiHitContainer::partitionUsedHits()
00287 {
00288 for (HitMapToVectorAndEndType::iterator it=_map.begin(); it!=_map.end(); ++it)
00289 {
00290 vector<StiHit*>& tempvec = (*it).second.hits();
00291 vector<StiHit*>::iterator where =
00292 stable_partition(tempvec.begin(), tempvec.end(), StiHitIsUsed() );
00293 (*it).second.setEnd(where);
00294 }
00295 }
00296
00297 ostream& operator<<(ostream& os, const vector<StiHit*>& vec)
00298 {
00299 for (vector<StiHit*>::const_iterator vit=vec.begin(); vit!=vec.end(); vit++) {
00300 os<<*(*vit)<<endl;
00301 }
00302 return os;
00303 }
00304
00305 ostream& operator<<(ostream& os, const StiHitContainer& store)
00306 {
00307 for (HitMapToVectorAndEndType::const_iterator it=store._map.begin(); it!=store._map.end(); it++) {
00308 os <<endl;
00309 os <<(*it).second.hits();
00310 }
00311 return os;
00312 }
00313
00314
00316
00317 vector<StiHit*> & StiHitContainer::getHits()
00318 {
00319 _selectedHits.clear();
00320 for(HitMapToVectorAndEndType::const_iterator iter= _map.begin(); iter !=_map.end(); iter++)
00321 {
00322 const vector<StiHit*> & t_hits = (*iter).second.hits();
00323 for (vector<StiHit*>::const_iterator it=t_hits.begin();it!=t_hits.end();++it)
00324 _selectedHits.push_back(*it);
00325 }
00326 return _selectedHits;
00327 }
00328
00330
00331 vector<StiHit*> & StiHitContainer::getHits(Filter<StiHit> & filter)
00332 {
00333
00334 _selectedHits.clear();
00335 StiHit * hit;
00336 for(HitMapToVectorAndEndType::const_iterator iter= _map.begin(); iter !=_map.end(); iter++)
00337 {
00338 const vector<StiHit*> & t_hits = (*iter).second.hits();
00339 for (vector<StiHit*>::const_iterator it=t_hits.begin();
00340 it!=t_hits.end();
00341 ++it)
00342 {
00343 hit = *it;
00344 if (filter.accept(hit)) _selectedHits.push_back(hit);
00345 }
00346 }
00347 return _selectedHits;
00348 }
00349
00350
00351 StiHit * StiHitContainer::getNearestHit(StiHit& ref, double dY, double dZ, bool fetchAll)
00352 {
00353 StiHit* hit = 0;
00354 StiHit* closestHit = 0;
00355 double dMax = DBL_MAX;
00356 double dy, dz, d;
00357 vector<StiHit*> & hits = getHits(ref,dY,dZ,fetchAll);
00358 for (vector<StiHit*>::iterator iter=hits.begin();iter!=hits.end();++iter)
00359 {
00360 hit = *iter;
00361 dy = hit->y() - ref.y();
00362 dz = hit->z() - ref.z();
00363 d = dy*dy + dz*dz;
00364 if ( d<dMax)
00365 {
00366 closestHit = hit;
00367 dMax = d;
00368 }
00369 }
00370 return closestHit;
00371 }
00372