00001
00002
00003
00004
00005 #include <Stiostream.h>
00006 #include <math.h>
00007 #include <stdio.h>
00008 #include <dirent.h>
00009 #include <sys/stat.h>
00010 #include <string>
00011 #include <stdexcept>
00012 #include <algorithm>
00013 using std::find_if;
00014 using std::for_each;
00015 using std::binary_search;
00016 #include "Sti/Base/Filter.h"
00017 #include "Sti/StiDetectorTreeBuilder.h"
00018 #include "Sti/StlUtilities.h"
00019 #include "Sti/StiCompositeLeafIterator.h"
00020 #include "Sti/StiPlacement.h"
00021 #include "Sti/StiDetectorContainer.h"
00022 ostream& operator<<(ostream&, const NameMapKey&);
00023 ostream& operator<<(ostream*, const StiDetector&);
00024
00025 StiDetectorContainer::StiDetectorContainer(const string & name,
00026 const string & description,
00027 StiMasterDetectorBuilder* masterBuilder)
00028 : Named(name),
00029 Described(description),
00030 mroot(0),
00031
00032 mLeafIt(0),
00033 _masterDetectorBuilder(masterBuilder)
00034 {
00035 cout <<"StiDetectorContainer::StiDetectorContainer() -I- Started/Done"<<endl;
00036 }
00037
00038 StiDetectorContainer::~StiDetectorContainer()
00039 {
00040 cout <<"StiDetectorContainer::~StiDetectorContainer() -I- Started"<<endl;
00041 if (mLeafIt) {
00042 delete mLeafIt;
00043 mLeafIt=0;
00044 }
00045 cout <<"StiDetectorContainer::~StiDetectorContainer() -I- Done"<<endl;
00046 }
00047
00048 void StiDetectorContainer::initialize()
00049 {
00050 cout << "StiDetectorContainer::initialize() -I- Started" << endl;
00051 cout << "StiDetectorContainer::initialize() -I- Use master builder to build detectors" << endl;
00052 build(_masterDetectorBuilder);
00053 cout << "StiDetectorContainer::initialize() -I- Now extract detectors and add them to the _sortedDetectors" << endl;
00054 _sortedDetectors.reserve(1000);
00055 _sortedDetectors.clear();
00056 _selectedDetectors.clear();
00057 vector<StiDetectorBuilder*>::iterator bIter;
00058 for (bIter=_masterDetectorBuilder->begin(); bIter!=_masterDetectorBuilder->end(); ++bIter)
00059 {
00060 string name = (*bIter)->getName();
00061
00062 ULong_t where = name.find("Tpc");
00063 if (where==name.npos)
00064 {
00065 cout <<"StiDetectorContainer::initialize() -I- Skipping group: "<<name<<endl;
00066 continue;
00067 }
00068 int nRows = (*bIter)->getNRows();
00069 for (int row=0;row<nRows;row++)
00070 {
00071 int nSectors = (*bIter)->getNSectors(row);
00072 for (int sector=0; sector<nSectors; sector++)
00073 {
00074 StiDetector* detector = (*bIter)->getDetector(row,sector);
00075 if (!detector) throw runtime_error("StiDetectorContainer::build() -F- detector==0 ");
00076 if (detector->isActive())
00077 add(detector);
00078 else
00079 cout <<"StiDetectorContainer::initialize() -I- Not Adding detector unit: "<< detector->getName()<<endl;
00080 }
00081 }
00082 }
00083 cout << "StiDetectorContainer::initialize() -I- Done" << endl;
00084 }
00085
00086
00092 void StiDetectorContainer::setToDetector(const StiDetector* layer)
00093 {
00094
00095 if (!layer->getTreeNode())
00096 throw runtime_error("StiDetectorContainer::setToDetector(StiDetector*) -E- layer->getTreeNode()==0");
00097 setToLeaf( layer->getTreeNode() );
00098 }
00099
00102 void StiDetectorContainer::reset()
00103 {
00104 SameOrderKey<StiDetector> mySameOrderKey;
00105 StiOrderKey tempOrderKey;
00106 tempOrderKey.key = static_cast<double>( StiPlacement::kMidRapidity );
00107 mySameOrderKey.morderKey = tempOrderKey;
00108 StiDetectorNodeVector::iterator where = find_if(mroot->begin(), mroot->end(), mySameOrderKey);
00109 if (where==mroot->end())
00110 throw runtime_error("StiDetectorContainer::setToDetector(StiDetector*) -E- mid-rapidity region not found - where==0");
00111 mregion = where;
00112
00113 mradial_it = (*mregion)->begin();
00114 mphi_it = (*mradial_it)->begin();
00115 }
00116
00117 StiDetector* StiDetectorContainer::operator*() const
00118 {
00119 if (!(*mphi_it))
00120 throw runtime_error("StiDetectorContainer::operator*() const -E- *mphi_it==0");
00121 StiDetector * det = (*mphi_it)->getData();
00122 if (!det)
00123 throw runtime_error("StiDetectorContainer::operator*() const -E- *mphi_it==0");
00124 return det;
00125 }
00126
00127 StiDetector* StiDetectorContainer::getCurrentDetector() const
00128 {
00129 if (!(*mphi_it))
00130 throw runtime_error("StiDetectorContainer::getCurrentDetector() const -E- *mphi_it==0");
00131 StiDetector * det = (*mphi_it)->getData();
00132 if (!det)
00133 throw runtime_error("StiDetectorContainer::getCurrentDetector() const -E- *mphi_it==0");
00134 return det;
00135 }
00136
00137 #if 0
00138 bool StiDetectorContainer::moveToNextRegion()
00139 {
00140
00141 StiDetectorNodeVector::const_iterator region = mregion;
00142 if ( ++region == mroot->end() )
00143 {
00144
00145 return false;
00146 }
00147
00148
00149 StiDetectorNodeVector::const_iterator radial_it = (*mregion)->begin();
00150
00151 if (radial_it==(*mregion)->end())
00152 return false;
00153 StiDetectorNodeVector::const_iterator phi_it = (*radial_it)->begin();
00154
00155 if (phi_it==(*radial_it)->end())
00156 return false;
00157
00158
00159 mregion = region;
00160 mradial_it = radial_it;
00161 mphi_it = phi_it;
00162
00163 return true;
00164 }
00165
00166 bool StiDetectorContainer::moveToPreviousRegion()
00167 {
00168 if (mregion == mroot->begin() )
00169 {
00170
00171 return false;
00172 }
00173
00174 --mregion;
00175
00176 mradial_it = (*mregion)->begin();
00177 mphi_it = (*mradial_it)->begin();
00178 return true;
00179 }
00180 #endif
00181
00194 bool StiDetectorContainer::moveIn()
00195 {
00196
00197 if (mradial_it == (*mregion)->begin() ) {
00198
00199
00200 return false;
00201 }
00202
00203
00204 const StiDetectorNode* oldPhiNode = *mphi_it;
00205
00206 --mradial_it;
00207 mphi_it = (*mradial_it)->begin();
00208
00209 if ( (*mradial_it)->getChildCount() == oldPhiNode->getParent()->getChildCount()) {
00210
00211 mphi_it = (*mradial_it)->begin()+oldPhiNode->getOrderKey().index;
00212 return true;
00213 }
00214 else {
00215
00216 return setPhi( oldPhiNode->getOrderKey() );
00217 }
00218 }
00219
00220 bool StiDetectorContainer::setPhi(const StiOrderKey& oldOrder)
00221 {
00222 mphi_it = gFindClosestOrderKey((*mradial_it)->begin(),
00223 (*mradial_it)->end(), oldOrder);
00224 if (mphi_it == (*mradial_it)->end())
00225 {
00226 cout <<"StiDetectorContainer::setPhiIterator() -E- Find Phi failed"<<endl;
00227 reset();
00228 return false;
00229 }
00230 return true;
00231 }
00232 #if 0
00233
00246 bool StiDetectorContainer::moveOut()
00247 {
00248
00249 const StiDetectorNode* oldPhiNode = *mphi_it;
00250
00251
00252
00253
00254
00255 if ( (++mradial_it<(*mregion)->end())==false) {
00256
00257
00258
00259 --mradial_it;
00260 return false;
00261 }
00262
00263 if ( (*mradial_it)->getChildCount() == oldPhiNode->getParent()->getChildCount()) {
00264
00265 mphi_it = (*mradial_it)->begin()+oldPhiNode->getOrderKey().index;
00266 return true;
00267 }
00268 else {
00269
00270 return setPhi( oldPhiNode->getOrderKey() );
00271 }
00272 }
00273
00278 void StiDetectorContainer::movePlusPhi()
00279 {
00280 ++mphi_it;
00281 if (mphi_it == (*mradial_it)->end()) {
00282 mphi_it = (*mradial_it)->begin();
00283 }
00284 }
00285
00291 void StiDetectorContainer::moveMinusPhi()
00292 {
00293 if (mphi_it == (*mradial_it)->begin()) {
00294 mphi_it = (*mradial_it)->end();
00295 }
00296 --mphi_it;
00297 }
00298 #endif
00299
00303 void
00304 StiDetectorContainer::build(StiDetectorBuilder * builder)
00305 {
00306 cout <<"StiDetectorContainer::build() -I- Starting"<<endl;
00307 cout <<"StiDetectorContainer::build() -I- Building using builder:"<<builder->getName()<<endl;
00308
00309
00310
00311 StiDetectorTreeBuilder treeBuilder;
00312 mroot = treeBuilder.build(builder);
00313 if (!mroot)
00314 throw runtime_error("StiDetectorContainer::build() - ERROR - mroot==0");
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332 mLeafIt = new StiCompositeLeafIterator<StiDetector>(mroot);
00333 cout <<"StiDetectorContainer::build() -I- Done"<<endl;
00334 return;
00335 }
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345 void StiDetectorContainer::setToLeaf(StiDetectorNode* leaf)
00346 {
00347
00348 mphi_it = leaf->whereInParent();
00349 if (mphi_it == leaf->end()) {
00350 cout <<"StiDetectorContainer::setToLeaf(StiDetectorNode*). ERROR:\t"
00351 <<"Node not found in parent. Abort"<<endl;
00352 reset();
00353 return;
00354 }
00355
00356 StiDetectorNode* parentInRadius = (*mphi_it)->getParent();
00357 mradial_it = parentInRadius->whereInParent();
00358 if (mradial_it == parentInRadius->end()) {
00359 cout <<"StiDetectorContainer::setToLeaf(StiDetectorNode*) -E- Node not found in parent. Abort?"<<endl;
00360 reset();
00361 return;
00362 }
00363
00364
00365
00366
00367 }
00368
00369 void StiDetectorContainer::add(StiDetector* det)
00370 {
00371 _sortedDetectors.push_back(det);
00372 sort(_sortedDetectors.begin(), _sortedDetectors.end(), RPhiLessThan());
00373 }
00374
00375
00376
00377
00378
00379 bool RPhiLessThan::operator()(const StiDetector* lhs, const StiDetector* rhs)
00380 {
00381 StiPlacement* lhsp = lhs->getPlacement();
00382 StiPlacement* rhsp = rhs->getPlacement();
00383 if (!lhsp || !rhsp)
00384 {
00385 cout << "RPhiLessThan::operator() -E- !lhsp || !rhsp "<<endl;
00386 return false;
00387 }
00388 if (lhsp->getLayerRadius()<rhsp->getLayerRadius())
00389 return false;
00390 else if (lhsp->getLayerRadius()>rhsp->getLayerRadius())
00391 return true;
00392
00393
00394 double la = lhsp->getLayerAngle();
00395 double ra = rhsp->getLayerAngle();
00396 if ((la<0) != (ra<0)) { if (la<0) la+=2*M_PI;if (ra<0) ra+=2*M_PI;}
00397 return (la<ra);
00398 }
00399
00400
00401 vector<StiDetector*> & StiDetectorContainer::getDetectors()
00402 {
00403 return _sortedDetectors;
00404 }
00405
00406 vector<StiDetector*> & StiDetectorContainer::getDetectors(Filter<StiDetector> & filter)
00407 {
00408 _selectedDetectors.clear();
00409 StiDetector * detector;
00410 for (vector<StiDetector*>::const_iterator i=_sortedDetectors.begin();
00411 i!=_sortedDetectors.end();
00412 ++i)
00413 {
00414 detector = *i;
00415 if (filter.accept(detector))
00416 _selectedDetectors.push_back(detector);
00417 }
00418 return _selectedDetectors;
00419 }
00420
00421 vector<StiDetector*>::iterator StiDetectorContainer::begin()
00422 {
00423 cout << "StiDetectorContainer::begin() -I- size:"<<_sortedDetectors.size()<<endl;
00424 return _sortedDetectors.begin();
00425 }
00426
00427 vector<StiDetector*>::iterator StiDetectorContainer::end()
00428 {
00429 return _sortedDetectors.end();
00430 }
00431
00432 vector<StiDetector*>::const_iterator StiDetectorContainer::begin() const
00433 {
00434 cout << "StiDetectorContainer::begin() const -I- size:"<<_sortedDetectors.size()<<endl;
00435 return _sortedDetectors.begin();
00436 }
00437
00438 vector<StiDetector*>::const_iterator StiDetectorContainer::end() const
00439 {
00440 return _sortedDetectors.end();
00441 }