File indexing completed on 2025-01-30 09:17:18
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <DD4hep/Objects.h>
0016 #include <DD4hep/DetElement.h>
0017 #include <DD4hep/Volumes.h>
0018 #include <DD4hep/Printout.h>
0019 #include <DDEve/Utilities.h>
0020 #include <DDEve/EventHandler.h>
0021 #include <DDEve/ElementList.h>
0022
0023 #include <TGeoNode.h>
0024 #include <TGeoShape.h>
0025 #include <TGeoVolume.h>
0026 #include <TGeoManager.h>
0027 #include <TGeoShapeAssembly.h>
0028
0029 #include <TEveGeoShape.h>
0030 #include <TEveGeoNode.h>
0031 #include <TEveElement.h>
0032 #include <TEveTrans.h>
0033
0034 using namespace dd4hep;
0035
0036
0037 void Utilities::SetRnrChildren(TEveElementList* l, bool b) {
0038 l->SetRnrChildren(b);
0039 for(TEveElementList::List_i i=l->BeginChildren(); i!=l->EndChildren(); ++i) {
0040 (*i)->SetRnrChildren(b);
0041 }
0042 }
0043
0044
0045 void Utilities::SetRnrAll(TEveElementList* l, bool b) {
0046 l->SetRnrSelfChildren(b,b);
0047 for(TEveElementList::List_i i=l->BeginChildren(); i!=l->EndChildren(); ++i) {
0048 TEveElement* e = *i;
0049 TEveElementList* ll = dynamic_cast<TEveElementList*>(e);
0050 if ( ll ) SetRnrAll(ll, b);
0051 else e->SetRnrSelfChildren(b,b);
0052 }
0053 }
0054
0055
0056 void Utilities::MakeNodesVisible(TEveElement* e, bool visible, int level) {
0057 TEveElementList* lst = dynamic_cast<TEveElementList*>(e);
0058 if ( !lst ) {
0059 return;
0060 }
0061 else if ( level == 1 ) {
0062 SetRnrChildren(lst, visible);
0063 lst->SetRnrSelf(true);
0064 }
0065 else if ( level == 0 ) {
0066 lst->SetRnrSelf(visible);
0067 }
0068 else {
0069 SetRnrAll(lst, visible);
0070 lst->SetRnrSelf(visible);
0071 }
0072 }
0073
0074 std::pair<bool,TEveElement*>
0075 Utilities::createEveShape(int level,
0076 int max_level,
0077 TEveElement* p,
0078 TGeoNode* n,
0079 const TGeoHMatrix& mat,
0080 const std::string& nam)
0081 {
0082 TGeoVolume* vol = n ? n->GetVolume() : 0;
0083 bool created = false;
0084
0085 if ( 0 == vol || level > max_level ) return std::make_pair(created,(TEveElement*)0);
0086
0087 VisAttr vis(Volume(vol).visAttributes());
0088 TGeoShape* geoShape = vol->GetShape();
0089 TEveElement* element = 0;
0090
0091 if ( p ) {
0092 TGeoNode* pn = (TGeoNode*)p->GetUserData();
0093 if ( pn == n ) {
0094 element = p;
0095 }
0096 if ( !element ) {
0097 element = (TEveGeoShape*)p->FindChild(n->GetName());
0098 }
0099 if ( !element && !nam.empty() ) {
0100 element = (TEveGeoShape*)p->FindChild(nam.c_str());
0101 }
0102 if ( element ) goto Daughters;
0103 }
0104
0105 if ( geoShape->IsA() == TGeoShapeAssembly::Class() ) {
0106
0107 ElementList* shape = new ElementList(n->GetName(),n->GetName(),true,true);
0108 shape->SetUserData(n);
0109 shape->SetMainTransparency(true);
0110 shape->SetMainAlpha(0.2);
0111 shape->SetPickable(kTRUE);
0112 if ( vis.isValid() ) {
0113 float red, green, blue;
0114 vis.rgb(red, green, blue);
0115 shape->SetMainColorRGB(red, green, blue);
0116 }
0117 element = shape;
0118 created = true;
0119 goto Daughters;
0120 }
0121 else if ( 0 == element ) {
0122 TEveGeoShape* shape = new TEveGeoShape(n->GetName());
0123
0124
0125 created = true;
0126 if ( vis.isValid() ) {
0127 float red, green, blue;
0128 vis.rgb(red, green, blue);
0129 shape->SetMainColorRGB(red, green, blue);
0130 }
0131 shape->SetMainTransparency(true);
0132 shape->SetMainAlpha(0.2);
0133 shape->SetPickable(kTRUE);
0134 shape->RefMainTrans().SetFrom(mat);
0135 shape->SetShape((TGeoShape*)geoShape->Clone());
0136 if ( level < max_level ) {
0137 shape->SetRnrSelfChildren(true,true);
0138 }
0139 else if ( level == max_level ) {
0140 shape->SetRnrSelfChildren(true,false);
0141 }
0142 else if ( level > max_level ) {
0143 shape->SetRnrSelfChildren(false,false);
0144 }
0145 shape->SetUserData(n);
0146 element = shape;
0147 }
0148
0149 Daughters:
0150 for (Int_t idau = 0, ndau = n->GetNdaughters(); idau < ndau; ++idau) {
0151 TGeoNode* daughter = n->GetDaughter(idau);
0152 TGeoHMatrix dau_mat(mat);
0153 TGeoMatrix* matrix = daughter->GetMatrix();
0154 dau_mat.Multiply(matrix);
0155 std::pair<bool,TEveElement*> dau_shape =
0156 createEveShape(level+1, max_level, element, daughter, dau_mat, daughter->GetName());
0157 if ( dau_shape.first ) {
0158 element->AddElement(dau_shape.second);
0159 }
0160 }
0161 return std::make_pair(created,element);
0162 }
0163
0164 int Utilities::findNodeWithMatrix(TGeoNode* p, TGeoNode* n, TGeoHMatrix* mat, std::string* sub_path) {
0165 if ( p == n ) return 1;
0166 TGeoHMatrix dau_mat;
0167 for (Int_t idau = 0, ndau = p->GetNdaughters(); idau < ndau; ++idau) {
0168 std::string spath;
0169 TGeoNode* daughter = p->GetDaughter(idau);
0170 TGeoHMatrix* daughter_matrix = 0;
0171 if ( mat ) {
0172 TGeoMatrix* matrix = daughter->GetMatrix();
0173 dau_mat = *mat;
0174 dau_mat.Multiply(matrix);
0175 daughter_matrix = &dau_mat;
0176 }
0177 int level = findNodeWithMatrix(daughter,n,daughter_matrix,sub_path ? &spath : 0);
0178 if ( level>0 ) {
0179 if ( sub_path ) {
0180 *sub_path += "/";
0181 *sub_path += spath;
0182 }
0183 if ( mat ) *mat = *daughter_matrix;
0184 return level+1;
0185 }
0186 }
0187 return 0;
0188 }
0189
0190 std::pair<bool,TEveElement*> Utilities::LoadDetElement(DetElement de,int levels, TEveElement* parent) {
0191 if ( !strstr(de.name(),"BeamCal") ) {
0192 PlacedVolume pv = de.placement();
0193 if (pv.isValid()) {
0194 TGeoNode* n = pv.ptr();
0195 TGeoMatrix* matrix = n->GetMatrix();
0196 std::pair<bool,TEveElement*> e = createEveShape(0, levels, parent, n, *matrix, de.name());
0197 TEveElementList* list = dynamic_cast<TEveElementList*>(e.second);
0198 if ( list ) {
0199 list->SetName(de.name());
0200 }
0201 return e;
0202 }
0203 }
0204 return std::make_pair(false,(TEveGeoShape*)0);
0205 }