StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StiDetectorContainer.cxx
1 //StiDetectorContainer.cxx
2 //M.L. Miller (Yale Software)
3 //02/02/01
4 
5 #include <cassert>
6 #include <Stiostream.h>
7 #include <math.h>
8 #include <stdio.h>
9 #include <dirent.h>
10 #include <sys/stat.h>
11 #include <string>
12 #include <stdexcept>
13 #include <algorithm>
14 using std::find_if;
15 using std::for_each;
16 using std::binary_search;
17 #include "Sti/Base/Filter.h"
18 #include "Sti/StiDetectorTreeBuilder.h"
19 #include "Sti/StlUtilities.h"
20 #include "Sti/StiCompositeLeafIterator.h"
21 #include "Sti/StiPlacement.h"
22 #include "Sti/StiDetectorContainer.h"
23 ostream& operator<<(ostream&, const NameMapKey&);
24 ostream& operator<<(ostream*, const StiDetector&);
25 
26 StiDetectorContainer::StiDetectorContainer(const string & name,
27  const string & description,
28  StiMasterDetectorBuilder* masterBuilder)
29 : Named(name),
30  Described(description),
31  mroot(0),
32  //mregion(0),
33  mLeafIt(0),
34  _masterDetectorBuilder(masterBuilder)
35 {
36  cout <<"StiDetectorContainer::StiDetectorContainer() -I- Started/Done"<<endl;
37 }
38 
39 StiDetectorContainer::~StiDetectorContainer()
40 {
41  cout <<"StiDetectorContainer::~StiDetectorContainer() -I- Started"<<endl;
42  if (mLeafIt) {
43  delete mLeafIt;
44  mLeafIt=0;
45  }
46  cout <<"StiDetectorContainer::~StiDetectorContainer() -I- Done"<<endl;
47 }
48 
49 void StiDetectorContainer::initialize()
50 {
51  cout << "StiDetectorContainer::initialize() -I- Started" << endl;
52  cout << "StiDetectorContainer::initialize() -I- Use master builder to build detectors" << endl;
53  build(_masterDetectorBuilder);
54  cout << "StiDetectorContainer::initialize() -I- Now extract detectors and add them to the _sortedDetectors" << endl;
55  _sortedDetectors.reserve(1000);
56  _sortedDetectors.clear();
57  _selectedDetectors.clear();
58  vector<StiDetectorBuilder*>::iterator bIter;
59  for (bIter=_masterDetectorBuilder->begin(); bIter!=_masterDetectorBuilder->end(); ++bIter)
60  {
61  string name = (*bIter)->getName();
62  ULong_t where = name.find("Tpc");
63  if (where==name.npos)
64  {
65  cout <<"StiDetectorContainer::initialize() -I- Skipping group: "<<name<<endl;
66  continue;
67  }
68  int nRows = (*bIter)->getNRows();
69  for (int row=0;row<nRows;row++)
70  {
71  int nSectors = (*bIter)->getNSectors(row);
72  for (int sector=0; sector<nSectors; sector++)
73  {
74  StiDetector* detector = (*bIter)->getDetector(row,sector);
75  if (!detector) continue;
76  if (detector->isActive())
77  add(detector);
78  else
79  cout <<"StiDetectorContainer::initialize() -I- Not Adding detector unit: "<< detector->getName()<<endl;
80  }
81  }
82  }
83  cout << "StiDetectorContainer::initialize() -I- Done" << endl;
84 }
85 
86 
93 {
94  assert(layer->getTreeNode() && "StiDetectorContainer::setToDetector(StiDetector*) layer->getTreeNode()==0");
95  setToLeaf( layer->getTreeNode() );
96 }
97 
101 {
102  SameOrderKey<StiDetector> mySameOrderKey;
103  StiOrderKey tempOrderKey;
104  tempOrderKey.key = static_cast<double>( StiPlacement::kMidRapidity );
105  mySameOrderKey.morderKey = tempOrderKey; //order is of type const StiOrderKey&
106  StiDetectorNodeVector::iterator where = find_if(mroot->begin(), mroot->end(), mySameOrderKey);
107  assert(where!=mroot->end() && "StiDetectorContainer::setToDetector(StiDetector*) mid-rapidity region not found - where==0");
108  mregion = where;
109  //This will seg fault if (*mregion)->begin()==(*mregion)->end() !!!!!!!
110  mradial_it = (*mregion)->begin(); //change (MLM)
111  mphi_it = (*mradial_it)->begin();
112 }
113 
115 {
116  assert(*mphi_it && "StiDetectorContainer::operator*() *mphi_it==0");
117  StiDetector * det = (*mphi_it)->getData();
118  assert(det && "StiDetectorContainer::operator*() *mphi_it==0");
119  return det;
120 }
121 
122 StiDetector* StiDetectorContainer::getCurrentDetector() const
123 {
124  assert(*mphi_it && "StiDetectorContainer::getCurrentDetector() *mphi_it==0");
125  StiDetector * det = (*mphi_it)->getData();
126  assert(det && "StiDetectorContainer::getCurrentDetector() det==0");
127  return det;
128 }
129 
143 bool StiDetectorContainer::moveIn(double phiCut, double zCut, double rMin)
144 {
145  //if (mradial_it == mregion->begin() ) { //change (MLM)
146  if (mradial_it == (*mregion)->begin() ) { //change (MLM)
147  // cout <<"StiDetectorContainer::moveIn():\t";
148  // cout <<"Nowhere to go. return false"<<endl;
149  return false;
150  }
151 
152  //remember where we started:
153  StiDetectorNodeVector::const_iterator oldPhiNodeP = mphi_it;
154  const StiDetectorNode* oldPhiNode = *mphi_it;
155  bool foundIt = false;
156 
157  --mradial_it;
158  if (rMin >=0 && (*(*mradial_it)->begin())->getData()->getPlacement()->getNormalRadius() < rMin)
159  return false;
160 
161  mphi_it = (*mradial_it)->begin();
162 
163  if ( (*mradial_it)->getChildCount() == oldPhiNode->getParent()->getChildCount()) {
164  // cout <<"Index into array"<<endl;
165  mphi_it = (*mradial_it)->begin()+oldPhiNode->getOrderKey().index;
166 
167  foundIt = true;
168  }
169  else {
170  // cout <<"Do linear search"<<endl;
171  foundIt = setPhi( oldPhiNode->getOrderKey() );
172  }
173  if (foundIt) {
174  if (phiCut >= 0) {
175  double phiDiff = TMath::Abs((*mphi_it)->getData()->getPlacement()->getNormalRefAngle() -
176  oldPhiNode->getData()->getPlacement()->getNormalRefAngle());
177  if (phiDiff > TMath::Pi()) phiDiff = TMath::TwoPi() - phiDiff;
178  foundIt = (phiDiff <= phiCut);
179  }
180  if (foundIt && zCut >= 0) {
181  double zDiff = TMath::Abs((*mphi_it)->getData()->getPlacement()->getZcenter() -
182  oldPhiNode->getData()->getPlacement()->getZcenter());
183  foundIt = (zDiff <= zCut);
184  }
185  if (!foundIt) {
186  // keep moving in with the new mradial_it
187  mphi_it = oldPhiNodeP;
188  return moveIn(phiCut,zCut);
189  }
190  }
191  return foundIt;
192 }
193 
194 bool StiDetectorContainer::setPhi(const StiOrderKey& oldOrder)
195 {
196  mphi_it = gFindClosestOrderKey((*mradial_it)->begin(),
197  (*mradial_it)->end(), oldOrder);
198  if (mphi_it == (*mradial_it)->end())
199  {
200  cout <<"StiDetectorContainer::setPhiIterator() -E- Find Phi failed"<<endl;
201  reset();
202  return false;
203  }
204  return true;
205 }
210 void
212 {
213  cout <<"StiDetectorContainer::build() -I- Starting"<<endl;
214  cout <<"StiDetectorContainer::build() -I- Building using builder:"<<builder->getName()<<endl;
215  // build the volumes
216  //builder->build();
217  // pass volumes to TreeBuilder before we make like a tree and leave...
218  StiDetectorTreeBuilder treeBuilder;
219  mroot = treeBuilder.build(builder);
220  assert(mroot && "StiDetectorContainer::build() mroot==0");
221 
222  //Don't need any of this!!!!!
223  /*
224  //Set region to midrapidity, hard-coded for now, update later to allow for other regions
225  SameName<StiDetector> mySameName;
226  mySameName.mname = "midrapidity";
227  StiDetectorNodeVector::iterator where = find_if(mroot->begin(), mroot->end(), mySameName);
228  if (where==mroot->end())
229  throw runtime_error("StiDetectorContainer::build() - ERROR - mid-rapidity region not found - where==0");
230  if (!(*where))
231  throw runtime_error("StiDetectorContainer::build() - ERROR - mid-rapidity region not found - *where==0");
232  cout <<"StiDetectorContainer::build() -I- Find Leaves and set mregion"<<endl;
233  //Sort by name for O(::log(n)) calls to setDetector()
234  //sort(mLeafIt->begin(), mLeafIt->end(), DataNameLessThan<StiDetector>() );
235 
236  mregion = (*where); //change (MLM)
237  */
238  mLeafIt = new StiCompositeLeafIterator<StiDetector>(mroot);
239  cout <<"StiDetectorContainer::build() -I- Done"<<endl;
240  return;
241 }
242 
243 /*
244  void StiDetectorContainer::print() const
245  {
246  RecursiveStreamNode<StiDetector> myStreamer;
247  myStreamer( mroot );
248  }*/
249 
250 //We assume that the node is a leaf in phi
251 void StiDetectorContainer::setToLeaf(StiDetectorNode* leaf)
252 {
253  //Now we try the new index-iterator scheme:
254  mphi_it = leaf->whereInParent();
255  if (mphi_it == leaf->end()) {
256  cout <<"StiDetectorContainer::setToLeaf(StiDetectorNode*). ERROR:\t"
257  <<"Node not found in parent. Abort"<<endl;
258  reset();
259  return;
260  }
261 
262  StiDetectorNode* parentInRadius = (*mphi_it)->getParent();
263  mradial_it = parentInRadius->whereInParent();
264  if (mradial_it == parentInRadius->end()) {
265  cout <<"StiDetectorContainer::setToLeaf(StiDetectorNode*) -E- Node not found in parent. Abort?"<<endl;
266  reset();
267  return;
268  }
269  //cout <<"\nleaf: "<<leaf->getName()<<" "<<leaf->getOrderKey()<<endl;
270  //cout <<"*mradial_it: "<<(*mradial_it)->getName()<<" "<<(*mradial_it)->getOrderKey()<<endl;
271  //cout <<"*mphi_it: "<<(*mphi_it)->getName()<<" "<<(*mphi_it)->getOrderKey()<<endl;
272 
273 }
274 
276 {
277  _sortedDetectors.push_back(det);
278  sort(_sortedDetectors.begin(), _sortedDetectors.end(), RPhiLessThan());
279 }
280 
281 
282 //Non members
283 
284 //sort in descending order in radius, and ascending order in phi
285 bool RPhiLessThan::operator()(const StiDetector* lhs, const StiDetector* rhs)
286 {
287  StiPlacement* lhsp = lhs->getPlacement();
288  StiPlacement* rhsp = rhs->getPlacement();
289  if (!lhsp || !rhsp)
290  {
291  cout << "RPhiLessThan::operator() -E- !lhsp || !rhsp "<<endl;
292  return false;
293  }
294  if (lhsp->getLayerRadius()<rhsp->getLayerRadius())
295  return false;
296  else if (lhsp->getLayerRadius()>rhsp->getLayerRadius())
297  return true;
298 //VP else
299 //VP return (lhsp->getLayerAngle()<rhsp->getLayerAngle());
300  double la = lhsp->getLayerAngle();
301  double ra = rhsp->getLayerAngle();
302  if ((la<0) != (ra<0)) { if (la<0) la+=2*M_PI;if (ra<0) ra+=2*M_PI;}
303  return (la<ra);
304 }
305 
306 
307 vector<StiDetector*> & StiDetectorContainer::getDetectors()
308 {
309  return _sortedDetectors;
310 }
311 
312 vector<StiDetector*> & StiDetectorContainer::getDetectors(Filter<StiDetector> & filter)
313 {
314  _selectedDetectors.clear();
315  StiDetector * detector;
316  for (vector<StiDetector*>::const_iterator i=_sortedDetectors.begin();
317  i!=_sortedDetectors.end();
318  ++i)
319  {
320  detector = *i;
321  if (filter.accept(detector))
322  _selectedDetectors.push_back(detector);
323  }
324  return _selectedDetectors;
325 }
326 
327 vector<StiDetector*>::iterator StiDetectorContainer::begin()
328 {
329  cout << "StiDetectorContainer::begin() -I- size:"<<_sortedDetectors.size()<<endl;
330  return _sortedDetectors.begin();
331 }
332 
333 vector<StiDetector*>::iterator StiDetectorContainer::end()
334 {
335  return _sortedDetectors.end();
336 }
337 
338 vector<StiDetector*>::const_iterator StiDetectorContainer::begin() const
339 {
340  cout << "StiDetectorContainer::begin() const -I- size:"<<_sortedDetectors.size()<<endl;
341  return _sortedDetectors.begin();
342 }
343 
344 vector<StiDetector*>::const_iterator StiDetectorContainer::end() const
345 {
346  return _sortedDetectors.end();
347 }
void add(StiDetector *det)
Add a detector element to the sorted vector.
bool moveIn(double phiCut=-1.0, double zCut=-1.0, double rMin=-1.0)
Step in radially in STAR TPC global coordinates.
T * getData() const
Return a (non-const!) pointer to the data hung on this node.
StiDetectorNode * build(StiDetectorBuilder *builder)
Build the Detector model.
void setToDetector(const StiDetector *layer)
Set iterators to the detector nearest to the passed StiDetector pointer.
Definition: Named.h:16
vec_type::iterator whereInParent()
Provide the iterator into the parent that can be dereferenced to get this node.
unsigned int getChildCount() const
Return the number of children that belong to this node.
StiDetector * operator*() const
Dereference current iterator and return a pointer to current StiDetector.
const StiOrderKey & getOrderKey() const
Return a reference to the the orderkey of this node.
virtual void build(StiDetectorBuilder *builder)
Builds the detector tree given a pointer to the detector builder.
void reset()
This performs a full internal reset of interator structure.
vec_type::iterator begin()
Provide random access iterator to the beginning of the vector of children.
vec_type::iterator end()
Provide random access iterator to the end of the vector of children.
StiCompositeTreeNode * getParent() const
Return a (non-const!) pointer to the parent of this node.
const string & getName() const
Get the name of the object.
Definition: Named.cxx:22