00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include "TROOT.h"
00013 #include "THashList.h"
00014 #include "TObjArray.h"
00015 #include "TGeometry.h"
00016 #include "TNode.h"
00017 #include "TMaterial.h"
00018 #include "TBrowser.h"
00019 #include "TClass.h"
00020
00021 TGeometry *gGeometry = 0;
00022
00023 ClassImp(TGeometry)
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
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 TGeometry::TGeometry()
00085 {
00086
00087
00088 fMaterials = new THashList(100,3);
00089 fMatrices = new THashList(100,3);
00090 fShapes = new THashList(500,3);
00091 fNodes = new TList;
00092 fCurrentNode = 0;
00093 fMaterialPointer = 0;
00094 fMatrixPointer = 0;
00095 fShapePointer = 0;
00096 gGeometry = this;
00097 fBomb = 1;
00098 fMatrix = 0;
00099 fX=fY=fZ =0.0;
00100 fGeomLevel =0;
00101 fIsReflection[fGeomLevel] = kFALSE;
00102 }
00103
00104
00105
00106 TGeometry::TGeometry(const char *name,const char *title ) : TNamed (name, title)
00107 {
00108
00109
00110 fMaterials = new THashList(1000,3);
00111 fMatrices = new THashList(1000,3);
00112 fShapes = new THashList(5000,3);
00113 fNodes = new TList;
00114 fCurrentNode = 0;
00115 fMaterialPointer = 0;
00116 fMatrixPointer = 0;
00117 fShapePointer = 0;
00118 gGeometry = this;
00119 fBomb = 1;
00120 fMatrix = 0;
00121 fX=fY=fZ =0.0;
00122 gROOT->GetListOfGeometries()->Add(this);
00123 fGeomLevel =0;
00124 fIsReflection[fGeomLevel] = kFALSE;
00125 }
00126
00127
00128 TGeometry::TGeometry(const TGeometry& geo) :
00129 TNamed(geo),
00130 fMaterials(geo.fMaterials),
00131 fMatrices(geo.fMatrices),
00132 fShapes(geo.fShapes),
00133 fNodes(geo.fNodes),
00134 fMatrix(geo.fMatrix),
00135 fCurrentNode(geo.fCurrentNode),
00136 fMaterialPointer(geo.fMaterialPointer),
00137 fMatrixPointer(geo.fMatrixPointer),
00138 fShapePointer(geo.fShapePointer),
00139 fBomb(geo.fBomb),
00140 fGeomLevel(geo.fGeomLevel),
00141 fX(geo.fX),
00142 fY(geo.fY),
00143 fZ(geo.fZ)
00144 {
00145
00146 for(Int_t i=0; i<kMAXLEVELS; i++) {
00147 for(Int_t j=0; j<kVectorSize; j++)
00148 fTranslation[i][j]=geo.fTranslation[i][j];
00149 for(Int_t j=0; j<kMatrixSize; j++)
00150 fRotMatrix[i][j]=geo.fRotMatrix[i][j];
00151 fIsReflection[i]=geo.fIsReflection[i];
00152 }
00153 }
00154
00155
00156 TGeometry& TGeometry::operator=(const TGeometry& geo)
00157 {
00158
00159 if(this!=&geo) {
00160 TNamed::operator=(geo);
00161 fMaterials=geo.fMaterials;
00162 fMatrices=geo.fMatrices;
00163 fShapes=geo.fShapes;
00164 fNodes=geo.fNodes;
00165 fMatrix=geo.fMatrix;
00166 fCurrentNode=geo.fCurrentNode;
00167 fMaterialPointer=geo.fMaterialPointer;
00168 fMatrixPointer=geo.fMatrixPointer;
00169 fShapePointer=geo.fShapePointer;
00170 fBomb=geo.fBomb;
00171 fGeomLevel=geo.fGeomLevel;
00172 fX=geo.fX;
00173 fY=geo.fY;
00174 fZ=geo.fZ;
00175 for(Int_t i=0; i<kMAXLEVELS; i++) {
00176 for(Int_t j=0; j<kVectorSize; j++)
00177 fTranslation[i][j]=geo.fTranslation[i][j];
00178 for(Int_t j=0; j<kMatrixSize; j++)
00179 fRotMatrix[i][j]=geo.fRotMatrix[i][j];
00180 fIsReflection[i]=geo.fIsReflection[i];
00181 }
00182 }
00183 return *this;
00184 }
00185
00186
00187 TGeometry::~TGeometry()
00188 {
00189
00190
00191 if (!fMaterials) return;
00192 fMaterials->Delete();
00193 fMatrices->Delete();
00194 fShapes->Delete();
00195 fNodes->Delete();
00196 delete fMaterials;
00197 delete fMatrices;
00198 delete fShapes;
00199 delete fNodes;
00200 delete [] fMaterialPointer;
00201 delete [] fMatrixPointer;
00202 delete [] fShapePointer;
00203 fMaterials = 0;
00204 fMatrices = 0;
00205 fShapes = 0;
00206 fNodes = 0;
00207 fMaterialPointer = 0;
00208 fMatrixPointer = 0;
00209 fShapePointer = 0;
00210
00211 if (gGeometry == this) {
00212 gGeometry = (TGeometry*) gROOT->GetListOfGeometries()->First();
00213 if (gGeometry == this)
00214 gGeometry = (TGeometry*) gROOT->GetListOfGeometries()->After(gGeometry);
00215 }
00216 gROOT->GetListOfGeometries()->Remove(this);
00217 }
00218
00219
00220
00221 void TGeometry::Browse(TBrowser *b)
00222 {
00223
00224
00225 if( b ) {
00226 b->Add( fMaterials, "Materials" );
00227 b->Add( fMatrices, "Rotation Matrices" );
00228 b->Add( fShapes, "Shapes" );
00229 b->Add( fNodes, "Nodes" );
00230 }
00231 }
00232
00233
00234
00235 void TGeometry::cd(const char *)
00236 {
00237
00238
00239 gGeometry = this;
00240 }
00241
00242
00243
00244 void TGeometry::Draw(Option_t *option)
00245 {
00246
00247
00248 TNode *node1 = (TNode*)fNodes->First();
00249 if (node1) node1->Draw(option);
00250
00251 }
00252
00253
00254
00255 TObject *TGeometry::FindObject(const TObject *) const
00256 {
00257
00258
00259 Error("FindObject","Not yet implemented");
00260 return 0;
00261 }
00262
00263
00264
00265 TObject *TGeometry::FindObject(const char *name) const
00266 {
00267
00268
00269 TObjArray *loc = TGeometry::Get(name);
00270 if (loc) return loc->At(0);
00271 return 0;
00272 }
00273
00274
00275
00276 TObjArray *TGeometry::Get(const char *name)
00277 {
00278
00279
00280
00281
00282 static TObjArray *locs = 0;
00283 if (!locs) locs = new TObjArray(2);
00284 TObjArray &loc = *locs;
00285 loc[0] = 0;
00286 loc[1] = 0;
00287
00288 if (!gGeometry) return &loc;
00289
00290 TObject *temp;
00291 TObject *where;
00292
00293 temp = gGeometry->GetListOfMaterials()->FindObject(name);
00294 where = gGeometry->GetListOfMaterials();
00295
00296 if (!temp) {
00297 temp = gGeometry->GetListOfShapes()->FindObject(name);
00298 where = gGeometry->GetListOfShapes();
00299 }
00300 if (!temp) {
00301 temp = gGeometry->GetListOfMatrices()->FindObject(name);
00302 where = gGeometry->GetListOfMatrices();
00303 }
00304 if (!temp) {
00305 temp = gGeometry->GetNode(name);
00306 where = gGeometry;
00307 }
00308 loc[0] = temp;
00309 loc[1] = where;
00310
00311 return &loc;
00312 }
00313
00314
00315
00316 TMaterial *TGeometry::GetMaterial(const char *name) const
00317 {
00318
00319
00320 return (TMaterial*)fMaterials->FindObject(name);
00321 }
00322
00323
00324
00325 TMaterial *TGeometry::GetMaterialByNumber(Int_t number) const
00326 {
00327
00328
00329 TMaterial *mat;
00330 if (number < 0 || number >= fMaterials->GetSize()) return 0;
00331 if (fMaterialPointer) return fMaterialPointer[number];
00332 TIter next(fMaterials);
00333 while ((mat = (TMaterial*) next())) {
00334 if (mat->GetNumber() == number) return mat;
00335 }
00336 return 0;
00337 }
00338
00339
00340
00341 TNode *TGeometry::GetNode(const char *name) const
00342 {
00343
00344
00345 TNode *node= (TNode*)GetListOfNodes()->First();
00346 if (!node) return 0;
00347 if (node->TestBit(kNotDeleted)) return node->GetNode(name);
00348 return 0;
00349 }
00350
00351
00352
00353 TRotMatrix *TGeometry::GetRotMatrix(const char *name) const
00354 {
00355
00356
00357 return (TRotMatrix*)fMatrices->FindObject(name);
00358 }
00359
00360
00361
00362 TRotMatrix *TGeometry::GetRotMatrixByNumber(Int_t number) const
00363 {
00364
00365
00366 TRotMatrix *matrix;
00367 if (number < 0 || number >= fMatrices->GetSize()) return 0;
00368 if (fMatrixPointer) return fMatrixPointer[number];
00369 TIter next(fMatrices);
00370 while ((matrix = (TRotMatrix*) next())) {
00371 if (matrix->GetNumber() == number) return matrix;
00372 }
00373 return 0;
00374 }
00375
00376
00377
00378 TShape *TGeometry::GetShape(const char *name) const
00379 {
00380
00381
00382 return (TShape*)fShapes->FindObject(name);
00383 }
00384
00385
00386
00387 TShape *TGeometry::GetShapeByNumber(Int_t number) const
00388 {
00389
00390
00391 TShape *shape;
00392 if (number < 0 || number >= fShapes->GetSize()) return 0;
00393 if (fShapePointer) return fShapePointer[number];
00394 TIter next(fShapes);
00395 while ((shape = (TShape*) next())) {
00396 if (shape->GetNumber() == number) return shape;
00397 }
00398 return 0;
00399 }
00400
00401
00402
00403 void TGeometry::Local2Master(Double_t *local, Double_t *master)
00404 {
00405
00406
00407
00408
00409
00410
00411
00412 if (GeomLevel()) {
00413 Double_t x,y,z;
00414 Double_t bomb = GetBomb();
00415 Double_t *matrix = &fRotMatrix[GeomLevel()][0];
00416 x = bomb*fX
00417 + local[0]*matrix[0]
00418 + local[1]*matrix[3]
00419 + local[2]*matrix[6];
00420
00421 y = bomb*fY
00422 + local[0]*matrix[1]
00423 + local[1]*matrix[4]
00424 + local[2]*matrix[7];
00425
00426 z = bomb*fZ
00427 + local[0]*matrix[2]
00428 + local[1]*matrix[5]
00429 + local[2]*matrix[8];
00430 master[0] = x; master[1] = y; master[2] = z;
00431 }
00432 else
00433 for (Int_t i=0;i<3;i++) master[i] = local[i];
00434 }
00435
00436
00437
00438 void TGeometry::Local2Master(Float_t *local, Float_t *master)
00439 {
00440
00441
00442
00443
00444
00445
00446
00447 if (GeomLevel()) {
00448 Float_t x,y,z;
00449 Float_t bomb = GetBomb();
00450
00451 Double_t *matrix = &fRotMatrix[GeomLevel()][0];
00452
00453 x = bomb*fX
00454 + local[0]*matrix[0]
00455 + local[1]*matrix[3]
00456 + local[2]*matrix[6];
00457
00458 y = bomb*fY
00459 + local[0]*matrix[1]
00460 + local[1]*matrix[4]
00461 + local[2]*matrix[7];
00462
00463 z = bomb*fZ
00464 + local[0]*matrix[2]
00465 + local[1]*matrix[5]
00466 + local[2]*matrix[8];
00467
00468 master[0] = x; master[1] = y; master[2] = z;
00469 }
00470 else
00471 for (Int_t i=0;i<3;i++) master[i] = local[i];
00472 }
00473
00474
00475
00476 void TGeometry::ls(Option_t *option) const
00477 {
00478
00479
00480 TString opt = option;
00481 opt.ToLower();
00482 if (opt.Contains("m")) {
00483 Printf("=================List of Materials================");
00484 fMaterials->ls(option);
00485 }
00486 if (opt.Contains("r")) {
00487 Printf("=================List of RotationMatrices================");
00488 fMatrices->ls(option);
00489 }
00490 if (opt.Contains("s")) {
00491 Printf("=================List of Shapes==========================");
00492 fShapes->ls(option);
00493 }
00494 if (opt.Contains("n")) {
00495 Printf("=================List of Nodes===========================");
00496 fNodes->ls(option);
00497 }
00498 }
00499
00500
00501
00502 void TGeometry::Master2Local(Double_t *master, Double_t *local)
00503 {
00504
00505
00506
00507
00508
00509
00510
00511 if (GeomLevel()) {
00512 Double_t x,y,z;
00513 Double_t bomb = GetBomb();
00514 Double_t *matrix = &fRotMatrix[GeomLevel()][0];
00515
00516 Double_t xms = master[0] - bomb*fX;
00517 Double_t yms = master[1] - bomb*fY;
00518 Double_t zms = master[2] - bomb*fZ;
00519
00520 x = xms*matrix[0] + yms*matrix[1] + zms*matrix[2];
00521 y = xms*matrix[3] + yms*matrix[4] + zms*matrix[5];
00522 z = xms*matrix[6] + yms*matrix[7] + zms*matrix[8];
00523
00524 local[0] = x; local[1] = y; local[2] = z;
00525 }
00526 else
00527 memcpy(local,master,sizeof(Double_t)* kVectorSize);
00528 }
00529
00530
00531
00532 void TGeometry::Master2Local(Float_t *master, Float_t *local)
00533 {
00534
00535
00536
00537
00538
00539
00540
00541 if (GeomLevel()) {
00542 Float_t x,y,z;
00543 Float_t bomb = GetBomb();
00544
00545 Double_t *matrix = &fRotMatrix[GeomLevel()][0];
00546
00547 Double_t xms = master[0] - bomb*fX;
00548 Double_t yms = master[1] - bomb*fY;
00549 Double_t zms = master[2] - bomb*fZ;
00550
00551 x = xms*matrix[0] + yms*matrix[1] + zms*matrix[2];
00552 y = xms*matrix[3] + yms*matrix[4] + zms*matrix[5];
00553 z = xms*matrix[6] + yms*matrix[7] + zms*matrix[8];
00554
00555 local[0] = x; local[1] = y; local[2] = z;
00556 }
00557 else
00558 memcpy(local,master,sizeof(Float_t)* kVectorSize);
00559 }
00560
00561
00562
00563 void TGeometry::Node(const char *name, const char *title, const char *shapename, Double_t x, Double_t y, Double_t z, const char *matrixname, Option_t *option)
00564 {
00565
00566
00567 new TNode(name,title,shapename,x,y,z,matrixname,option);
00568 }
00569
00570
00571
00572 void TGeometry::RecursiveRemove(TObject *obj)
00573 {
00574
00575
00576 if (fNodes) fNodes->RecursiveRemove(obj);
00577 }
00578
00579
00580
00581 void TGeometry::Streamer(TBuffer &b)
00582 {
00583
00584
00585 if (b.IsReading()) {
00586 UInt_t R__s, R__c;
00587 Version_t R__v = b.ReadVersion(&R__s, &R__c);
00588 if (R__v > 1) {
00589 b.ReadClassBuffer(TGeometry::Class(), this, R__v, R__s, R__c);
00590 } else {
00591
00592 TNamed::Streamer(b);
00593 fMaterials->Streamer(b);
00594 fMatrices->Streamer(b);
00595 fShapes->Streamer(b);
00596 fNodes->Streamer(b);
00597 b >> fBomb;
00598 b.CheckByteCount(R__s, R__c, TGeometry::IsA());
00599
00600 }
00601
00602 Int_t i;
00603 TMaterial *onemat;
00604 TRotMatrix *onematrix;
00605 TShape *oneshape;
00606 Int_t nmat = fMaterials->GetSize();
00607 if (nmat) fMaterialPointer = new TMaterial* [nmat];
00608 TIter nextmat(fMaterials);
00609 i = 0;
00610 while ((onemat = (TMaterial*) nextmat())) {
00611 fMaterialPointer[i] = onemat;
00612 i++;
00613 }
00614
00615 Int_t nrot = fMatrices->GetSize();
00616 if (nrot) fMatrixPointer = new TRotMatrix* [nrot];
00617 TIter nextmatrix(fMatrices);
00618 i = 0;
00619 while ((onematrix = (TRotMatrix*) nextmatrix())) {
00620 fMatrixPointer[i] = onematrix;
00621 i++;
00622 }
00623
00624 Int_t nsha = fShapes->GetSize();
00625 if (nsha) fShapePointer = new TShape* [nsha];
00626 TIter nextshape(fShapes);
00627 i = 0;
00628 while ((oneshape = (TShape*) nextshape())) {
00629 fShapePointer[i] = oneshape;
00630 i++;
00631 }
00632
00633 gROOT->GetListOfGeometries()->Add(this);
00634
00635 fCurrentNode = (TNode*)GetListOfNodes()->First();
00636 } else {
00637 b.WriteClassBuffer(TGeometry::Class(),this);
00638 }
00639 }
00640
00641
00642
00643 void TGeometry::UpdateMatrix(TNode *node)
00644 {
00645
00646
00647
00648 TNode *nodes[kMAXLEVELS];
00649 for (Int_t i=0;i<kVectorSize;i++) fTranslation[0][i] = 0;
00650 for (Int_t i=0;i<kMatrixSize;i++) fRotMatrix[0][i] = 0;
00651 fRotMatrix[0][0] = 1; fRotMatrix[0][4] = 1; fRotMatrix[0][8] = 1;
00652
00653 fGeomLevel = 0;
00654
00655 while (node) {
00656 nodes[fGeomLevel] = node;
00657 node = node->GetParent();
00658 fGeomLevel++;
00659 }
00660 fGeomLevel--;
00661 Int_t saveGeomLevel = fGeomLevel;
00662
00663 for (fGeomLevel=1;fGeomLevel<=saveGeomLevel;fGeomLevel++) {
00664 node = nodes[fGeomLevel-1];
00665 UpdateTempMatrix(node->GetX(),node->GetY(),node->GetZ(),node->GetMatrix());
00666 }
00667 }
00668
00669
00670
00671 void TGeometry::UpdateTempMatrix(Double_t x, Double_t y, Double_t z, TRotMatrix *rotMatrix)
00672 {
00673
00674
00675 Double_t *matrix = 0;
00676 Bool_t isReflection = kFALSE;
00677 if (rotMatrix && rotMatrix->GetType()) {
00678 matrix = rotMatrix->GetMatrix();
00679 isReflection = rotMatrix->IsReflection();
00680 }
00681 UpdateTempMatrix( x,y,z, matrix,isReflection);
00682 }
00683
00684
00685
00686 void TGeometry::UpdateTempMatrix(Double_t x, Double_t y, Double_t z, Double_t *matrix,Bool_t isReflection)
00687 {
00688
00689
00690 Int_t i=GeomLevel();
00691 if (i) {
00692 if(matrix) {
00693 UpdateTempMatrix(&(fTranslation[i-1][0]),&fRotMatrix[i-1][0]
00694 ,x,y,z,matrix
00695 ,&fTranslation[i][0],&fRotMatrix[i][0]);
00696 fX = fTranslation[i][0];
00697 fY = fTranslation[i][1];
00698 fZ = fTranslation[i][2];
00699 fIsReflection[i] = fIsReflection[i-1] ^ isReflection;
00700 } else {
00701 fX = fTranslation[i][0] = fTranslation[i-1][0] + x;
00702 fY = fTranslation[i][1] = fTranslation[i-1][1] + y;
00703 fZ = fTranslation[i][2] = fTranslation[i-1][2] + z;
00704 }
00705 } else {
00706 fX=fY=fZ=0;
00707 fIsReflection[0] = kFALSE;
00708 for (i=0;i<kVectorSize;i++) fTranslation[0][i] = 0;
00709 for (i=0;i<kMatrixSize;i++) fRotMatrix[0][i] = 0;
00710 fRotMatrix[0][0] = 1; fRotMatrix[0][4] = 1; fRotMatrix[0][8] = 1;
00711 }
00712 }
00713
00714
00715
00716 void TGeometry::UpdateTempMatrix(Double_t *dx,Double_t *rmat
00717 , Double_t x, Double_t y, Double_t z, Double_t *matrix
00718 , Double_t *dxnew, Double_t *rmatnew)
00719 {
00720
00721
00722
00723
00724
00725
00726
00727
00728 dxnew[0] = dx[0] + x*rmat[0] + y*rmat[3] + z*rmat[6];
00729 dxnew[1] = dx[1] + x*rmat[1] + y*rmat[4] + z*rmat[7];
00730 dxnew[2] = dx[2] + x*rmat[2] + y*rmat[5] + z*rmat[8];
00731
00732 rmatnew[0] = rmat[0]*matrix[0] + rmat[3]*matrix[1] + rmat[6]*matrix[2];
00733 rmatnew[1] = rmat[1]*matrix[0] + rmat[4]*matrix[1] + rmat[7]*matrix[2];
00734 rmatnew[2] = rmat[2]*matrix[0] + rmat[5]*matrix[1] + rmat[8]*matrix[2];
00735 rmatnew[3] = rmat[0]*matrix[3] + rmat[3]*matrix[4] + rmat[6]*matrix[5];
00736 rmatnew[4] = rmat[1]*matrix[3] + rmat[4]*matrix[4] + rmat[7]*matrix[5];
00737 rmatnew[5] = rmat[2]*matrix[3] + rmat[5]*matrix[4] + rmat[8]*matrix[5];
00738 rmatnew[6] = rmat[0]*matrix[6] + rmat[3]*matrix[7] + rmat[6]*matrix[8];
00739 rmatnew[7] = rmat[1]*matrix[6] + rmat[4]*matrix[7] + rmat[7]*matrix[8];
00740 rmatnew[8] = rmat[2]*matrix[6] + rmat[5]*matrix[7] + rmat[8]*matrix[8];
00741 }