00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 #include "Riostream.h"
00069
00070 #include "TBrowser.h"
00071 #include "TObjArray.h"
00072 #include "TStyle.h"
00073
00074 #include "TGeoManager.h"
00075 #include "TGeoMatrix.h"
00076 #include "TGeoShape.h"
00077 #include "TGeoVolume.h"
00078 #include "TVirtualGeoPainter.h"
00079 #include "TGeoVoxelFinder.h"
00080 #include "TGeoNode.h"
00081 #include "TMath.h"
00082 #include "TStopwatch.h"
00083
00084
00085
00086 ClassImp(TGeoNode)
00087
00088
00089 TGeoNode::TGeoNode()
00090 {
00091
00092 fVolume = 0;
00093 fMother = 0;
00094 fNumber = 0;
00095 fOverlaps = 0;
00096 fNovlp = 0;
00097 }
00098
00099
00100 TGeoNode::TGeoNode(const TGeoVolume *vol)
00101 {
00102
00103 if (!vol) {
00104 Error("ctor", "volume not specified");
00105 return;
00106 }
00107 fVolume = (TGeoVolume*)vol;
00108 if (fVolume->IsAdded()) fVolume->SetReplicated();
00109 fVolume->SetAdded();
00110 fMother = 0;
00111 fNumber = 0;
00112 fOverlaps = 0;
00113 fNovlp = 0;
00114 }
00115
00116
00117 TGeoNode::TGeoNode(const TGeoNode& gn) :
00118 TNamed(gn),
00119 TGeoAtt(gn),
00120 fVolume(gn.fVolume),
00121 fMother(gn.fMother),
00122 fNumber(gn.fNumber),
00123 fNovlp(gn.fNovlp),
00124 fOverlaps(gn.fOverlaps)
00125 {
00126
00127 }
00128
00129
00130 TGeoNode& TGeoNode::operator=(const TGeoNode& gn)
00131 {
00132
00133 if(this!=&gn) {
00134 TNamed::operator=(gn);
00135 TGeoAtt::operator=(gn);
00136 fVolume=gn.fVolume;
00137 fMother=gn.fMother;
00138 fNumber=gn.fNumber;
00139 fNovlp=gn.fNovlp;
00140 fOverlaps=gn.fOverlaps;
00141 }
00142 return *this;
00143 }
00144
00145
00146 TGeoNode::~TGeoNode()
00147 {
00148
00149 if (fOverlaps) delete [] fOverlaps;
00150 }
00151
00152
00153 void TGeoNode::Browse(TBrowser *b)
00154 {
00155
00156 if (!b) return;
00157 if (!GetNdaughters()) return;
00158 TGeoNode *daughter;
00159 TString title;
00160 for (Int_t i=0; i<GetNdaughters(); i++) {
00161 daughter = GetDaughter(i);
00162 b->Add(daughter, daughter->GetName(), daughter->IsVisible());
00163 }
00164 }
00165
00166
00167 Int_t TGeoNode::CountDaughters(Bool_t unique_volumes)
00168 {
00169
00170
00171 static Int_t icall = 0;
00172 Int_t counter = 0;
00173
00174 if (unique_volumes) {
00175 if (!fVolume->IsSelected()) {
00176 counter++;
00177 fVolume->SelectVolume(kFALSE);
00178 }
00179 } else counter++;
00180 icall++;
00181 Int_t nd = fVolume->GetNdaughters();
00182
00183 for (Int_t i=0; i<nd; i++) counter += GetDaughter(i)->CountDaughters(unique_volumes);
00184 icall--;
00185
00186 if (icall == 0) fVolume->SelectVolume(kTRUE);
00187 return counter;
00188 }
00189
00190
00191 void TGeoNode::CheckOverlaps(Double_t ovlp, Option_t *option)
00192 {
00193
00194 Int_t icheck = 0;
00195 Int_t ncheck = 0;
00196 TStopwatch *timer;
00197 Int_t i;
00198 Bool_t sampling = kFALSE;
00199 TString opt(option);
00200 opt.ToLower();
00201 if (opt.Contains("s")) sampling = kTRUE;
00202
00203 TGeoManager *geom = fVolume->GetGeoManager();
00204 ncheck = CountDaughters(kFALSE);
00205 timer = new TStopwatch();
00206 geom->ClearOverlaps();
00207 geom->SetCheckingOverlaps(kTRUE);
00208 Info("CheckOverlaps", "Checking overlaps for %s and daughters within %g", fVolume->GetName(),ovlp);
00209 if (sampling) {
00210 Info("CheckOverlaps", "Checking overlaps by sampling <%s> for %s and daughters", option, fVolume->GetName());
00211 Info("CheckOverlaps", "=== NOTE: Extrusions NOT checked with sampling option ! ===");
00212 }
00213 timer->Start();
00214 geom->GetGeomPainter()->OpProgress(fVolume->GetName(),icheck,ncheck,timer,kFALSE);
00215 fVolume->CheckOverlaps(ovlp,option);
00216 icheck++;
00217 TGeoIterator next(fVolume);
00218 TGeoNode *node;
00219 TString path;
00220 while ((node=next())) {
00221 next.GetPath(path);
00222 icheck++;
00223 if (!node->GetVolume()->IsSelected()) {
00224 geom->GetGeomPainter()->OpProgress(node->GetVolume()->GetName(),icheck,ncheck,timer,kFALSE);
00225 node->GetVolume()->SelectVolume(kFALSE);
00226 node->GetVolume()->CheckOverlaps(ovlp,option);
00227 }
00228 }
00229 fVolume->SelectVolume(kTRUE);
00230 geom->SetCheckingOverlaps(kFALSE);
00231 geom->SortOverlaps();
00232 TObjArray *overlaps = geom->GetListOfOverlaps();
00233 Int_t novlps = overlaps->GetEntriesFast();
00234 TNamed *obj;
00235 for (i=0; i<novlps; i++) {
00236 obj = (TNamed*)overlaps->At(i);
00237 obj->SetName(TString::Format("ov%05d",i));
00238 }
00239 geom->GetGeomPainter()->OpProgress("Check overlaps:",icheck,ncheck,timer,kTRUE);
00240 Info("CheckOverlaps", "Number of illegal overlaps/extrusions : %d\n", novlps);
00241 delete timer;
00242 }
00243
00244
00245 Int_t TGeoNode::DistancetoPrimitive(Int_t px, Int_t py)
00246 {
00247
00248 Int_t dist = 9999;
00249 if (!fVolume) return dist;
00250 if (gGeoManager != fVolume->GetGeoManager()) gGeoManager = fVolume->GetGeoManager();
00251 TVirtualGeoPainter *painter = gGeoManager->GetPainter();
00252 if (!painter) return dist;
00253 dist = painter->DistanceToPrimitiveVol(fVolume, px, py);
00254 return dist;
00255 }
00256
00257
00258 void TGeoNode::ExecuteEvent(Int_t event, Int_t px, Int_t py)
00259 {
00260
00261 if (!fVolume) return;
00262 TVirtualGeoPainter *painter = fVolume->GetGeoManager()->GetPainter();
00263 if (!painter) return;
00264 painter->ExecuteVolumeEvent(fVolume, event, px, py);
00265 }
00266
00267
00268 char *TGeoNode::GetObjectInfo(Int_t px, Int_t py) const
00269 {
00270
00271 if (!fVolume) return 0;
00272 TVirtualGeoPainter *painter = fVolume->GetGeoManager()->GetPainter();
00273 if (!painter) return 0;
00274 return (char*)painter->GetVolumeInfo(fVolume, px, py);
00275 }
00276
00277
00278 Bool_t TGeoNode::IsOnScreen() const
00279 {
00280
00281
00282 if (fVolume->TestAttBit(TGeoAtt::kVisOnScreen)) return kTRUE;
00283 return kFALSE;
00284 }
00285
00286
00287 void TGeoNode::InspectNode() const
00288 {
00289
00290 Info("InspectNode","Inspecting node %s", GetName());
00291 if (IsOverlapping()) Info("InspectNode","node is MANY");
00292 if (fOverlaps && fMother) {
00293 Info("InspectNode","possibly overlaping with :");
00294 for (Int_t i=0; i<fNovlp; i++)
00295 Info("InspectNode"," node %s", fMother->GetNode(fOverlaps[i])->GetName());
00296 }
00297 Info("InspectNode","Transformation matrix:\n");
00298 TGeoMatrix *matrix = GetMatrix();
00299 if (matrix) matrix->Print();
00300 if (fMother)
00301 Info("InspectNode","Mother volume %s\n", fMother->GetName());
00302 fVolume->InspectShape();
00303 }
00304
00305
00306 void TGeoNode::CheckShapes()
00307 {
00308
00309 fVolume->CheckShapes();
00310 Int_t nd = GetNdaughters();
00311 if (!nd) return;
00312 for (Int_t i=0; i<nd; i++) fVolume->GetNode(i)->CheckShapes();
00313 }
00314
00315
00316 void TGeoNode::DrawOnly(Option_t *option)
00317 {
00318
00319 fVolume->DrawOnly(option);
00320 }
00321
00322
00323 void TGeoNode::Draw(Option_t *option)
00324 {
00325
00326 gGeoManager->FindNode();
00327 gGeoManager->CdUp();
00328 Double_t point[3];
00329 gGeoManager->MasterToLocal(gGeoManager->GetCurrentPoint(), &point[0]);
00330 gGeoManager->SetCurrentPoint(&point[0]);
00331 gGeoManager->GetCurrentVolume()->Draw(option);
00332 }
00333
00334
00335 void TGeoNode::DrawOverlaps()
00336 {
00337
00338 if (!fNovlp) {printf("node %s is ONLY\n", GetName()); return;}
00339 if (!fOverlaps) {printf("node %s no overlaps\n", GetName()); return;}
00340 TGeoNode *node;
00341 Int_t i;
00342 Int_t nd = fMother->GetNdaughters();
00343 for (i=0; i<nd; i++) {
00344 node = fMother->GetNode(i);
00345 node->GetVolume()->SetVisibility(kFALSE);
00346 }
00347 fVolume->SetVisibility(kTRUE);
00348 for (i=0; i<fNovlp; i++) {
00349 node = fMother->GetNode(fOverlaps[i]);
00350 node->GetVolume()->SetVisibility(kTRUE);
00351 }
00352 gGeoManager->SetVisLevel(1);
00353 fMother->Draw();
00354 }
00355
00356
00357 void TGeoNode::FillIdArray(Int_t &ifree, Int_t &nodeid, Int_t *array) const
00358 {
00359
00360 Int_t nd = GetNdaughters();
00361 if (!nd) return;
00362 TGeoNode *daughter;
00363 Int_t istart = ifree;
00364 ifree += nd;
00365 for (Int_t id=0; id<nd; id++) {
00366 daughter = GetDaughter(id);
00367 array[istart+id] = ifree;
00368 array[ifree++] = ++nodeid;
00369 daughter->FillIdArray(ifree, nodeid, array);
00370 }
00371 }
00372
00373
00374
00375 Int_t TGeoNode::FindNode(const TGeoNode *node, Int_t level)
00376 {
00377
00378 Int_t nd = GetNdaughters();
00379 if (!nd) return -1;
00380 TIter next(fVolume->GetNodes());
00381 TGeoNode *daughter;
00382 while ((daughter=(TGeoNode*)next())) {
00383 if (daughter==node) {
00384 gGeoManager->GetListOfNodes()->AddAt(daughter,level+1);
00385 return (level+1);
00386 }
00387 }
00388 next.Reset();
00389 Int_t new_level;
00390 while ((daughter=(TGeoNode*)next())) {
00391 new_level = daughter->FindNode(node, level+1);
00392 if (new_level>=0) {
00393 gGeoManager->GetListOfNodes()->AddAt(daughter, level+1);
00394 return new_level;
00395 }
00396 }
00397 return -1;
00398 }
00399
00400
00401 void TGeoNode::SaveAttributes(ostream &out)
00402 {
00403
00404 if (IsVisStreamed()) return;
00405 SetVisStreamed(kTRUE);
00406 char quote='"';
00407 Bool_t voldef = kFALSE;
00408 if ((fVolume->IsVisTouched()) && (!fVolume->IsVisStreamed())) {
00409 fVolume->SetVisStreamed(kTRUE);
00410 out << " vol = gGeoManager->GetVolume("<<quote<<fVolume->GetName()<<quote<<");"<<endl;
00411 voldef = kTRUE;
00412 if (!fVolume->IsVisDaughters())
00413 out << " vol->SetVisDaughters(kFALSE);"<<endl;
00414 if (fVolume->IsVisible()) {
00415
00416
00417
00418
00419
00420
00421
00422
00423 } else {
00424 out <<" vol->SetVisibility(kFALSE);"<<endl;
00425 }
00426 }
00427 if (!IsVisDaughters()) return;
00428 Int_t nd = GetNdaughters();
00429 if (!nd) return;
00430 TGeoNode *node;
00431 for (Int_t i=0; i<nd; i++) {
00432 node = GetDaughter(i);
00433 if (node->IsVisStreamed()) continue;
00434 if (node->IsVisTouched()) {
00435 if (!voldef)
00436 out << " vol = gGeoManager->GetVolume("<<quote<<fVolume->GetName()<<quote<<");"<<endl;
00437 out<<" node = vol->GetNode("<<i<<");"<<endl;
00438 if (!node->IsVisDaughters()) {
00439 out<<" node->VisibleDaughters(kFALSE);"<<endl;
00440 node->SetVisStreamed(kTRUE);
00441 continue;
00442 }
00443 if (!node->IsVisible())
00444 out<<" node->SetVisibility(kFALSE);"<<endl;
00445 }
00446 node->SaveAttributes(out);
00447 node->SetVisStreamed(kTRUE);
00448 }
00449 }
00450
00451
00452 Bool_t TGeoNode::MayOverlap(Int_t iother) const
00453 {
00454
00455
00456 if (!fOverlaps) return kFALSE;
00457 for (Int_t i=0; i<fNovlp; i++) if (fOverlaps[i]==iother) return kTRUE;
00458 return kFALSE;
00459 }
00460
00461
00462 void TGeoNode::MasterToLocal(const Double_t *master, Double_t *local) const
00463 {
00464
00465 GetMatrix()->MasterToLocal(master, local);
00466 }
00467
00468
00469 void TGeoNode::MasterToLocalVect(const Double_t *master, Double_t *local) const
00470 {
00471
00472 GetMatrix()->MasterToLocalVect(master, local);
00473 }
00474
00475
00476 void TGeoNode::LocalToMaster(const Double_t *local, Double_t *master) const
00477 {
00478
00479 GetMatrix()->LocalToMaster(local, master);
00480 }
00481
00482
00483 void TGeoNode::LocalToMasterVect(const Double_t *local, Double_t *master) const
00484 {
00485
00486 GetMatrix()->LocalToMasterVect(local, master);
00487 }
00488
00489
00490 void TGeoNode::ls(Option_t * ) const
00491 {
00492
00493 }
00494
00495
00496 void TGeoNode::Paint(Option_t *option)
00497 {
00498
00499 TVirtualGeoPainter *painter = gGeoManager->GetGeomPainter();
00500 if (!painter) return;
00501 painter->PaintNode(this, option);
00502 }
00503
00504
00505 void TGeoNode::PrintCandidates() const
00506 {
00507
00508
00509 Double_t point[3];
00510 gGeoManager->MasterToLocal(gGeoManager->GetCurrentPoint(), &point[0]);
00511 printf(" Local : %g, %g, %g\n", point[0], point[1], point[2]);
00512 if (!fVolume->Contains(&point[0])) {
00513 printf("current point not inside this\n");
00514 return;
00515 }
00516 TGeoPatternFinder *finder = fVolume->GetFinder();
00517 TGeoNode *node;
00518 if (finder) {
00519 printf("current node divided\n");
00520 node = finder->FindNode(&point[0]);
00521 if (!node) {
00522 printf("point not inside division element\n");
00523 return;
00524 }
00525 printf("inside division element %s\n", node->GetName());
00526 return;
00527 }
00528 TGeoVoxelFinder *voxels = fVolume->GetVoxels();
00529 if (!voxels) {
00530 printf("volume not voxelized\n");
00531 return;
00532 }
00533 Int_t ncheck = 0;
00534 Int_t *check_list = voxels->GetCheckList(&point[0], ncheck);
00535 voxels->PrintVoxelLimits(&point[0]);
00536 if (!check_list) {
00537 printf("no candidates for current point\n");
00538 return;
00539 }
00540 TString overlap = "ONLY";
00541 for (Int_t id=0; id<ncheck; id++) {
00542 node = fVolume->GetNode(check_list[id]);
00543 if (node->IsOverlapping()) overlap = "MANY";
00544 else overlap = "ONLY";
00545 printf("%i %s %s\n", check_list[id], node->GetName(), overlap.Data());
00546 }
00547 PrintOverlaps();
00548 }
00549
00550
00551 void TGeoNode::PrintOverlaps() const
00552 {
00553
00554
00555 if (!fOverlaps) {printf("node %s no overlaps\n", GetName()); return;}
00556 printf("Overlaps for node %s :\n", GetName());
00557 TGeoNode *node;
00558 for (Int_t i=0; i<fNovlp; i++) {
00559 node = fMother->GetNode(fOverlaps[i]);
00560 printf(" %s\n", node->GetName());
00561 }
00562 }
00563
00564
00565 Double_t TGeoNode::Safety(Double_t *point, Bool_t in) const
00566 {
00567
00568
00569 Double_t local[3];
00570 GetMatrix()->MasterToLocal(point,local);
00571 return fVolume->GetShape()->Safety(local,in);
00572 }
00573
00574
00575 void TGeoNode::SetOverlaps(Int_t *ovlp, Int_t novlp)
00576 {
00577
00578 if (fOverlaps) delete [] fOverlaps;
00579 fOverlaps = ovlp;
00580 fNovlp = novlp;
00581 }
00582
00583
00584 void TGeoNode::SetVisibility(Bool_t vis)
00585 {
00586
00587 if (gGeoManager->IsClosed()) SetVisTouched(kTRUE);
00588 TGeoAtt::SetVisibility(vis);
00589 if (vis && !fVolume->IsVisible()) fVolume->SetVisibility(vis);
00590 gGeoManager->ModifiedPad();
00591 }
00592
00593
00594 void TGeoNode::VisibleDaughters(Bool_t vis)
00595 {
00596
00597 if (gGeoManager->IsClosed()) SetVisTouched(kTRUE);
00598 SetVisDaughters(vis);
00599 gGeoManager->ModifiedPad();
00600 }
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614 ClassImp(TGeoNodeMatrix)
00615
00616
00617
00618 TGeoNodeMatrix::TGeoNodeMatrix()
00619 {
00620
00621 fMatrix = 0;
00622 }
00623
00624
00625 TGeoNodeMatrix::TGeoNodeMatrix(const TGeoVolume *vol, const TGeoMatrix *matrix) :
00626 TGeoNode(vol)
00627 {
00628
00629 fMatrix = (TGeoMatrix*)matrix;
00630 if (!fMatrix) fMatrix = gGeoIdentity;
00631 }
00632
00633
00634 TGeoNodeMatrix::TGeoNodeMatrix(const TGeoNodeMatrix& gnm)
00635 :TGeoNode(gnm),
00636 fMatrix(gnm.fMatrix)
00637 {
00638
00639 }
00640
00641
00642 TGeoNodeMatrix& TGeoNodeMatrix::operator=(const TGeoNodeMatrix& gnm)
00643 {
00644
00645 if (this!=&gnm) {
00646 TGeoNode::operator=(gnm);
00647 fMatrix=gnm.fMatrix;
00648 }
00649 return *this;
00650 }
00651
00652
00653 TGeoNodeMatrix::~TGeoNodeMatrix()
00654 {
00655
00656 }
00657
00658
00659 Int_t TGeoNodeMatrix::GetByteCount() const
00660 {
00661
00662 Int_t count = 40 + 4;
00663
00664 return count;
00665 }
00666
00667
00668 Int_t TGeoNodeMatrix::GetOptimalVoxels() const
00669 {
00670
00671
00672
00673 Bool_t type = fVolume->GetShape()->IsCylType();
00674 if (!type) return 0;
00675 if (!fMatrix->IsRotAboutZ()) return 0;
00676 const Double_t *transl = fMatrix->GetTranslation();
00677 if (TMath::Abs(transl[0])>1E-10) return 0;
00678 if (TMath::Abs(transl[1])>1E-10) return 0;
00679 return 1;
00680 }
00681
00682
00683 TGeoNode *TGeoNodeMatrix::MakeCopyNode() const
00684 {
00685
00686 TGeoNodeMatrix *node = new TGeoNodeMatrix(fVolume, fMatrix);
00687 node->SetName(GetName());
00688
00689 node->SetMotherVolume(fMother);
00690
00691 node->SetNumber(fNumber);
00692
00693 if (fNovlp>0) {
00694 if (fOverlaps) {
00695 Int_t *ovlps = new Int_t[fNovlp];
00696 memcpy(ovlps, fOverlaps, fNovlp*sizeof(Int_t));
00697 node->SetOverlaps(ovlps, fNovlp);
00698 } else {
00699 node->SetOverlaps(fOverlaps, fNovlp);
00700 }
00701 }
00702
00703 if (IsVirtual()) node->SetVirtual();
00704 return node;
00705 }
00706
00707
00708 void TGeoNodeMatrix::SetMatrix(const TGeoMatrix *matrix)
00709 {
00710
00711 fMatrix = (TGeoMatrix*)matrix;
00712 if (!fMatrix) fMatrix = gGeoIdentity;
00713 }
00714
00715
00716
00717
00718
00719 ClassImp(TGeoNodeOffset)
00720
00721
00722
00723 TGeoNodeOffset::TGeoNodeOffset()
00724 {
00725
00726 TObject::SetBit(kGeoNodeOffset);
00727 fOffset = 0;
00728 fIndex = 0;
00729 fFinder = 0;
00730 }
00731
00732
00733 TGeoNodeOffset::TGeoNodeOffset(const TGeoVolume *vol, Int_t index, Double_t offset) :
00734 TGeoNode(vol)
00735 {
00736
00737 TObject::SetBit(kGeoNodeOffset);
00738 fOffset = offset;
00739 fIndex = index;
00740 fFinder = 0;
00741 }
00742
00743
00744 TGeoNodeOffset::TGeoNodeOffset(const TGeoNodeOffset& gno) :
00745 TGeoNode(gno),
00746 fOffset(gno.fOffset),
00747 fIndex(gno.fIndex),
00748 fFinder(gno.fFinder)
00749 {
00750
00751 }
00752
00753
00754 TGeoNodeOffset& TGeoNodeOffset::operator=(const TGeoNodeOffset& gno)
00755 {
00756
00757 if(this!=&gno) {
00758 TGeoNode::operator=(gno);
00759 fOffset=gno.fOffset;
00760 fIndex=gno.fIndex;
00761 fFinder=gno.fFinder;
00762 }
00763 return *this;
00764 }
00765
00766
00767 TGeoNodeOffset::~TGeoNodeOffset()
00768 {
00769
00770 }
00771
00772
00773 Int_t TGeoNodeOffset::GetIndex() const
00774 {
00775
00776 return (fIndex+fFinder->GetDivIndex());
00777 }
00778
00779
00780 TGeoNode *TGeoNodeOffset::MakeCopyNode() const
00781 {
00782
00783 TGeoNodeOffset *node = new TGeoNodeOffset(fVolume, GetIndex(), fOffset);
00784 node->SetName(GetName());
00785
00786 node->SetMotherVolume(fMother);
00787
00788 node->SetNumber(fNumber);
00789 if (IsVirtual()) node->SetVirtual();
00790
00791 node->SetFinder(GetFinder());
00792 return node;
00793 }
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865 ClassImp(TGeoIteratorPlugin)
00866 ClassImp(TGeoIterator)
00867
00868 TGeoIterator::TGeoIterator(TGeoVolume *top)
00869 {
00870
00871 fTop = top;
00872 fLevel = 0;
00873 fMustResume = kFALSE;
00874 fMustStop = kFALSE;
00875 fType = 0;
00876 fArray = new Int_t[30];
00877 fMatrix = new TGeoHMatrix();
00878 fTopName = fTop->GetName();
00879 fPlugin = 0;
00880 fPluginAutoexec = kFALSE;
00881 }
00882
00883
00884 TGeoIterator::TGeoIterator(const TGeoIterator &iter)
00885 {
00886
00887 fTop = iter.GetTopVolume();
00888 fLevel = iter.GetLevel();
00889 fMustResume = kFALSE;
00890 fMustStop = kFALSE;
00891 fType = iter.GetType();
00892 fArray = new Int_t[30+ 30*Int_t(fLevel/30)];
00893 for (Int_t i=0; i<fLevel+1; i++) fArray[i] = iter.GetIndex(i);
00894 fMatrix = new TGeoHMatrix(*iter.GetCurrentMatrix());
00895 fTopName = fTop->GetName();
00896 fPlugin = iter.fPlugin;
00897 fPluginAutoexec = iter.fPluginAutoexec;;
00898 }
00899
00900
00901 TGeoIterator::~TGeoIterator()
00902 {
00903
00904 if (fArray) delete [] fArray;
00905 delete fMatrix;
00906 }
00907
00908
00909 TGeoIterator &TGeoIterator::operator=(const TGeoIterator &iter)
00910 {
00911
00912 if (&iter == this) return *this;
00913 fTop = iter.GetTopVolume();
00914 fLevel = iter.GetLevel();
00915 fMustResume = kFALSE;
00916 fMustStop = kFALSE;
00917 fType = iter.GetType();
00918 if (fArray) delete [] fArray;
00919 fArray = new Int_t[30+ 30*Int_t(fLevel/30)];
00920 for (Int_t i=0; i<fLevel+1; i++) fArray[i] = iter.GetIndex(i);
00921 if (!fMatrix) fMatrix = new TGeoHMatrix();
00922 *fMatrix = *iter.GetCurrentMatrix();
00923 fTopName = fTop->GetName();
00924 fPlugin = iter.fPlugin;
00925 fPluginAutoexec = iter.fPluginAutoexec;;
00926 return *this;
00927 }
00928
00929
00930 TGeoNode *TGeoIterator::Next()
00931 {
00932
00933 if (fMustStop) return 0;
00934 TGeoNode *mother = 0;
00935 TGeoNode *next = 0;
00936 Int_t i;
00937 Int_t nd = fTop->GetNdaughters();
00938 if (!nd) {
00939 fMustStop = kTRUE;
00940 return 0;
00941 }
00942 if (!fLevel) {
00943 fArray[++fLevel] = 0;
00944 next = fTop->GetNode(0);
00945 if (fPlugin && fPluginAutoexec) fPlugin->ProcessNode();
00946 return next;
00947 }
00948 next = fTop->GetNode(fArray[1]);
00949
00950 for (i=2; i<fLevel+1; i++) {
00951 mother = next;
00952 next = mother->GetDaughter(fArray[i]);
00953 }
00954 if (fMustResume) {
00955 fMustResume = kFALSE;
00956 if (fPlugin && fPluginAutoexec) fPlugin->ProcessNode();
00957 return next;
00958 }
00959
00960 switch (fType) {
00961 case 0:
00962 nd = next->GetNdaughters();
00963 if (nd) {
00964
00965 fLevel++;
00966 if ((fLevel%30)==0) IncreaseArray();
00967 fArray[fLevel] = 0;
00968 if (fPlugin && fPluginAutoexec) fPlugin->ProcessNode();
00969 return next->GetDaughter(0);
00970 }
00971
00972 while (next) {
00973 next = GetNode(fLevel-1);
00974 if (!next) {
00975 nd = fTop->GetNdaughters();
00976 if (fArray[fLevel]<nd-1) {
00977 fArray[fLevel]++;
00978 if (fPlugin && fPluginAutoexec) fPlugin->ProcessNode();
00979 return fTop->GetNode(fArray[fLevel]);
00980 }
00981 fMustStop = kTRUE;
00982 return 0;
00983 } else {
00984 nd = next->GetNdaughters();
00985 if (fArray[fLevel]<nd-1) {
00986 fArray[fLevel]++;
00987 if (fPlugin && fPluginAutoexec) fPlugin->ProcessNode();
00988 return next->GetDaughter(fArray[fLevel]);
00989 }
00990 }
00991 fLevel--;
00992 }
00993 break;
00994 case 1:
00995 if (mother) nd = mother->GetNdaughters();
00996 if (fArray[fLevel]<nd-1) {
00997 fArray[fLevel]++;
00998 if (fPlugin && fPluginAutoexec) fPlugin->ProcessNode();
00999 if (!mother) return fTop->GetNode(fArray[fLevel]);
01000 else return mother->GetDaughter(fArray[fLevel]);
01001 }
01002 }
01003 fMustStop = kTRUE;
01004 return 0;
01005 }
01006
01007
01008 TGeoNode *TGeoIterator::operator()()
01009 {
01010
01011 return Next();
01012 }
01013
01014
01015 const TGeoMatrix *TGeoIterator::GetCurrentMatrix() const
01016 {
01017
01018 fMatrix->Clear();
01019 if (!fLevel) return fMatrix;
01020 TGeoNode *node = fTop->GetNode(fArray[1]);
01021 fMatrix->Multiply(node->GetMatrix());
01022 for (Int_t i=2; i<fLevel+1; i++) {
01023 node = node->GetDaughter(fArray[i]);
01024 fMatrix->Multiply(node->GetMatrix());
01025 }
01026 return fMatrix;
01027 }
01028
01029
01030 TGeoNode *TGeoIterator::GetNode(Int_t level) const
01031 {
01032
01033 if (!level || level>fLevel) return 0;
01034 TGeoNode *node = fTop->GetNode(fArray[1]);
01035 for (Int_t i=2; i<level+1; i++) node = node->GetDaughter(fArray[i]);
01036 return node;
01037 }
01038
01039
01040 void TGeoIterator::GetPath(TString &path) const
01041 {
01042
01043 path = fTopName;
01044 if (!fLevel) return;
01045 TGeoNode *node = fTop->GetNode(fArray[1]);
01046 path += "/";
01047 path += node->GetName();
01048 for (Int_t i=2; i<fLevel+1; i++) {
01049 node = node->GetDaughter(fArray[i]);
01050 path += "/";
01051 path += node->GetName();
01052 }
01053 }
01054
01055
01056 void TGeoIterator::IncreaseArray()
01057 {
01058
01059 Int_t *array = new Int_t[fLevel+30];
01060 memcpy(array, fArray, fLevel*sizeof(Int_t));
01061 delete [] fArray;
01062 fArray = array;
01063 }
01064
01065
01066 void TGeoIterator::Reset(TGeoVolume *top)
01067 {
01068
01069 if (top) fTop = top;
01070 fLevel = 0;
01071 fMustResume = kFALSE;
01072 fMustStop = kFALSE;
01073 }
01074
01075
01076 void TGeoIterator::SetTopName(const char *name)
01077 {
01078
01079 fTopName = name;
01080 }
01081
01082
01083 void TGeoIterator::Skip()
01084 {
01085
01086
01087 fMustResume = kTRUE;
01088 TGeoNode *next = GetNode(fLevel);
01089 if (!next) return;
01090 Int_t nd;
01091 switch (fType) {
01092 case 0:
01093
01094 while (next) {
01095 next = GetNode(fLevel-1);
01096 nd = (next==0)?fTop->GetNdaughters():next->GetNdaughters();
01097 if (fArray[fLevel]<nd-1) {
01098 ++fArray[fLevel];
01099 return;
01100 }
01101 fLevel--;
01102 if (!fLevel) {
01103 fMustStop = kTRUE;
01104 return;
01105 }
01106 }
01107 break;
01108 case 1:
01109 next = GetNode(fLevel-1);
01110 nd = (next==0)?fTop->GetNdaughters():next->GetNdaughters();
01111 if (fArray[fLevel]<nd-1) {
01112 ++fArray[fLevel];
01113 return;
01114 }
01115 fMustStop = kTRUE;
01116 break;
01117 }
01118 }
01119
01120
01121 void TGeoIterator::SetUserPlugin(TGeoIteratorPlugin *plugin)
01122 {
01123
01124 fPlugin = plugin;
01125 if (plugin) plugin->SetIterator(this);
01126 }