StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StiHitContainer.cxx
1 //StiHitContainer.cxx
2 //M.L. Miller (Yale Software)
3 //03/01
4 
5 #include "Stiostream.h"
6 #include <fstream>
7 #include <math.h>
8 #include <algorithm>
9 #include "Sti/Base/Filter.h"
10 #include "StiKalmanTrackNode.h"
11 #include "StiHit.h"
12 #include "StiPlacement.h"
13 #include "StiDetector.h"
14 #include "StiHitContainer.h"
15 #include <float.h>
16 
17 using std::sort;
18 using std::find;
19 using std::lower_bound;
20 using std::upper_bound;
21 using std::stable_partition;
22 ostream& operator<<(ostream& os, const StiHit& hit);
23 ostream& operator<<(ostream&, const HitMapKey&);
24 
25 int VectorAndEnd::fIdCounter = 0;
26 //________________________________________
27 VectorAndEnd::VectorAndEnd(): fEffectiveEndValid(false)
28 {
29  invalidateEnd();
30  fId=fIdCounter++;
31  theEffectiveEnd=theHitVec.end();
32  TestId(568);
33 }
34 
35 //________________________________________
36 void VectorAndEnd::TestId(int id)
37 {
38 // if ( fId == id) {
39 // printf(" Id = %d \n",fId);
40 // }
41 }
42 //________________________________________
43 void VectorAndEnd:: clear()
44 {
45  theHitVec.clear();
46  invalidateEnd();
47 }
48 //________________________________________
49 StiHitContainer::StiHitContainer(const string & name,
50  const string & description,
51  Factory<StiHit> *hitFactory)
52  : Named(name),
53  Described(description),
54  _hitFactory(hitFactory)
55 {
56  cout <<"StiHitContainer::StiHitContainer() -I- Started with name:"<<name<<endl;
57  //_minPoint = new StiHit();
58  //_maxPoint = new StiHit();
59 }
60 
61 //________________________________________________________________________________
62 StiHitContainer::~StiHitContainer()
63 {
64  cout <<"StiHitContainer::~StiHitContainer()"<<endl;
65 }
66 
73 //void StiHitContainer::update()
74 //{}
75 
76 //________________________________________________________________________________
90 {
91  const StiDetector* det = hit->detector();
92  assert(det);
93  _key.refangle = det->getPlacement()->getLayerAngle();
94  _key.position = det->getPlacement()->getLayerRadius();
95  _map[_key].push_back(hit);
96  return;
97 }
98 
99 //________________________________________________________________________________
100 void StiHitContainer::reset()
101 {
102  HitMapToVectorAndEndType::iterator it;
103  vector<StiHit*>::iterator iter;
104  //cout << "StiHitContainer::reset() -i- XXXXXXXXXXXXXXXXXXXXXXXX _map.size() ="<<_map.size()<<endl;
105  for (it=_map.begin(); it!=_map.end(); it++)
106  {
107  vector<StiHit*> &hits = (*it).second.hits();
108  //cout << ":"<<hits.size();
109  for (iter=hits.begin();iter!=hits.end();iter++)
110  {
111  (*iter)->setTimesUsed(0);
112  }
113  }
114 }
115 
116 //________________________________________________________________________________
123 {
124 // cout<<"StiHitContainer::clear() -I- Started"<<endl;
125  HitMapToVectorAndEndType::iterator it;
126  for (it=_map.begin(); it!=_map.end(); it++)
127  {
128  (*it).second.clear();
129  }
130 // cout<<"StiHitContainer::clear() -I- Done"<<endl;
131 }
132 
133 //________________________________________________________________________________
138 unsigned int StiHitContainer::size() const
139 {
140  unsigned int thesize = 0;
141  HitMapToVectorAndEndType::const_iterator it;
142  for (it=_map.begin(); it!=_map.end(); it++) {
143  thesize+=(*it).second.size();
144  }
145  return thesize;
146 }
147 
148 
149 //________________________________________________________________________________
150 //________________________________________________________________________________
151 vector<StiHit*>::iterator StiHitContainer::hitsBegin(const StiDetector* layer)
152 {
153  //_key.refangle = layer->getPlacement()->getNormalRefAngle();
154  _key.refangle = layer->getPlacement()->getLayerAngle();
155  //_key.position = layer->getPlacement()->getNormalRadius();
156  _key.position = layer->getPlacement()->getLayerRadius();
157  assert(_map.find(_key) != _map.end());
158  return _map[_key].begin();
159 }
160 
161 //________________________________________________________________________________
162 vector<StiHit*>::iterator StiHitContainer::hitsEnd(const StiDetector* layer)
163 {
164  //_key.refangle = layer->getPlacement()->getNormalRefAngle();
165  _key.refangle = layer->getPlacement()->getLayerAngle();
166  //_key.position = layer->getPlacement()->getNormalRadius();
167  _key.position = layer->getPlacement()->getLayerRadius();
168  assert(_map.find(_key) != _map.end());
169  return _map[_key].TheEffectiveEnd();
170 }
171 
172 
173 //________________________________________________________________________________
199 vector<StiHit*> & StiHitContainer::getHits(StiHit& ref, double dY, double dZ, bool fetchAll)
200 {
201  _selectedHits.clear();
202  _key.refangle = ref.refangle();
203  _key.position = ref.position();
204  // cout << "StiHitContainer::getHits(StiHit& ref, double dY, double dZ, bool fetchAll) -I- " << endl;
205  //cout << " dY:"<<dY<<endl
206  //<< " dZ:"<<dZ<<endl;
207  _minPoint.set(ref.position(),ref.refangle(),ref.y()-dY,ref.z()-dZ );
208  _maxPoint.set(ref.position(),ref.refangle(),ref.y()+dY,ref.z()+dZ );
209 
210  static int id = 0;
211  if (_map.find(_key) != _map.end()) {
212  vector<StiHit*>& tempvec = _map[_key].hits();
213  if (tempvec.size())
214  {
215  id = _map[_key].fId;
216 
217  vector<StiHit*>::iterator tempend = _map[_key].TheEffectiveEnd();
218 
219  //sanity check block
220  vector<StiHit*>::iterator tmptest = tempvec.begin();
221  vector<StiHit*>::iterator tmpend = tempvec.end();
222  if (!tempvec.size() ) {
223  cout << "-- Doing tmptest for id:" << id<< " " << ( tmpend != tmptest )
224  << " cmp " << ( tempend != tmptest )
225  << " " << tempvec.size() << " --> " << endl;
226  assert(0);
227  }
228 
229  //Search first by distance along z
230  _start = lower_bound(tempvec.begin(), tempend, &_minPoint, StizHitLessThan());
231  if (_start!=tempend)
232  _stop = upper_bound(tempvec.begin(), tempend, &_maxPoint, StizHitLessThan());
233  else
234  {
235  _start = tempend;
236  _stop = _start;
237  }
238  //Now search over distance along d
239  StiHit * hit;
240  for (vector<StiHit*>::iterator cit=_start; cit!=_stop; cit++)
241  {
242  hit = *cit;
243  if (fabs( hit->y() - ref.y() ) < dY)
244  {
245  if (fetchAll || (hit->isUsed()==0 && hit->detector()->isActive()) )
246  _selectedHits.push_back(hit);
247  }
248  }
249  } else { // StiHit vector size ==0; This is workround.
250  // we do not know why do we need this proptection yet
251  // cout << "Warning: Fix me, please !" << endl;
252  }
253  } // NO _key was provided
254  return _selectedHits;
255 }
256 
257 //________________________________________________________________________________
273 {
274  HitMapToVectorAndEndType::iterator it;
275  for (it=_map.begin(); it!=_map.end(); ++it)
276  {
277  vector<StiHit*>& tempvec = (*it).second.hits();
278  sort(tempvec.begin(), tempvec.end(), StizHitLessThan());
279  (*it).second.invalidateEnd();
280  }
281  return;
282 }
283 
284 //________________________________________________________________________________
285 ostream& operator<<(ostream& os, const vector<StiHit*>& vec)
286 {
287  for (vector<StiHit*>::const_iterator vit=vec.begin(); vit!=vec.end(); vit++) {
288  os<<*(*vit)<<endl;
289  }
290  return os;
291 }
292 
293 //________________________________________________________________________________
294 ostream& operator<<(ostream& os, const StiHitContainer& store)
295 {
296  for (HitMapToVectorAndEndType::const_iterator it=store._map.begin(); it!=store._map.end(); it++) {
297  os <<endl;
298  os <<(*it).second.hits();
299  }
300  return os;
301 }
302 
303 
304 //________________________________________________________________________________
306 // then return all hits.
307 vector<StiHit*> & StiHitContainer::getHits()
308 {
309  _selectedHits.clear();
310  for(HitMapToVectorAndEndType::const_iterator iter= _map.begin(); iter !=_map.end(); iter++)
311  {
312  const vector<StiHit*> & t_hits = (*iter).second.hits();
313  for (vector<StiHit*>::const_iterator it=t_hits.begin();it!=t_hits.end();++it)
314  _selectedHits.push_back(*it);
315  }
316  return _selectedHits;
317 }
318 
319 //________________________________________________________________________________
321 // then return all hits.
322 vector<StiHit*> & StiHitContainer::getHits(Filter<StiHit> & filter)
323 {
324  //cout << "StiHitContainer::getHits(Filter<StiHit> * filter) -I- Started"<<endl;
325  _selectedHits.clear();
326  StiHit * hit;
327  for(HitMapToVectorAndEndType::const_iterator iter= _map.begin(); iter !=_map.end(); iter++)
328  {
329  const vector<StiHit*> & t_hits = (*iter).second.hits();
330  for (vector<StiHit*>::const_iterator it=t_hits.begin();
331  it!=t_hits.end();
332  ++it)
333  {
334  hit = *it;
335  if (filter.accept(hit)) _selectedHits.push_back(hit);
336  }
337  }
338  return _selectedHits;
339 }
340 
341 
342 //________________________________________________________________________________
343 StiHit * StiHitContainer::getNearestHit(StiHit& ref, double dY, double dZ, bool fetchAll)
344 {
345  StiHit* hit = 0;
346  StiHit* closestHit = 0;
347  double dMax = DBL_MAX;
348  double dy, dz, d;
349  vector<StiHit*> & hits = getHits(ref,dY,dZ,fetchAll);
350  for (vector<StiHit*>::iterator iter=hits.begin();iter!=hits.end();++iter)
351  {
352  hit = *iter;
353  dy = hit->y() - ref.y();
354  dz = hit->z() - ref.z();
355  d = dy*dy + dz*dz;
356  if ( d<dMax)
357  {
358  closestHit = hit;
359  dMax = d;
360  }
361  }
362  return closestHit;
363 }
364 //________________________________________________________________________________
367 {
368  for(HitMapToVectorAndEndType::const_iterator iter= _map.begin(); iter !=_map.end(); iter++)
369  {
370  const vector<StiHit*> & t_hits = (*iter).second.hits();
371  for (vector<StiHit*>::const_iterator it=t_hits.begin();it!=t_hits.end();++it)
372  (*it)->setMaxTimes(nTimes);
373  }
374 }
375 
virtual void add(StiHit *)
vector< StiHit * > & getHits()
Get hits selected by the given filter. If no filter is given (i.e. filter==0)
Definition: StiHit.h:51
int isUsed() const
Definition: StiHit.h:148
Definition: Named.h:16
const StiDetector * detector() const
Definition: StiHit.h:96
virtual void clear()
virtual void sortHits()
Float_t refangle() const
Return the refAngle of the detector plane from which the hit arose.
Definition: StiHit.h:91
virtual unsigned int size() const
Float_t position() const
Return the position of the detector plane from which the hit arose.
Definition: StiHit.h:93
void setMaxTimes(int nTimes)
Set max time for all hits.