DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
Utilities.cpp
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 // Author : M.Frank
11 //
12 //==========================================================================
13 
14 // Framework include files
15 #include <DD4hep/Objects.h>
16 #include <DD4hep/DetElement.h>
17 #include <DD4hep/Volumes.h>
18 #include <DD4hep/Printout.h>
19 #include <DDEve/Utilities.h>
20 #include <DDEve/EventHandler.h>
21 #include <DDEve/ElementList.h>
22 
23 #include <TGeoNode.h>
24 #include <TGeoShape.h>
25 #include <TGeoVolume.h>
26 #include <TGeoManager.h>
27 #include <TGeoShapeAssembly.h>
28 
29 #include <TEveGeoShape.h>
30 #include <TEveGeoNode.h>
31 #include <TEveElement.h>
32 #include <TEveTrans.h>
33 
34 using namespace dd4hep;
35 
38  l->SetRnrChildren(b);
39  for(TEveElementList::List_i i=l->BeginChildren(); i!=l->EndChildren(); ++i) {
40  (*i)->SetRnrChildren(b);
41  }
42 }
43 
46  l->SetRnrSelfChildren(b,b);
47  for(TEveElementList::List_i i=l->BeginChildren(); i!=l->EndChildren(); ++i) {
48  TEveElement* e = *i;
49  TEveElementList* ll = dynamic_cast<TEveElementList*>(e);
50  if ( ll ) SetRnrAll(ll, b);
51  else e->SetRnrSelfChildren(b,b);
52  }
53 }
54 
56 void Utilities::MakeNodesVisible(TEveElement* e, bool visible, int level) {
57  TEveElementList* lst = dynamic_cast<TEveElementList*>(e);
58  if ( !lst ) {
59  return;
60  }
61  else if ( level == 1 ) {
62  SetRnrChildren(lst, visible);
63  lst->SetRnrSelf(true);
64  }
65  else if ( level == 0 ) {
66  lst->SetRnrSelf(visible);
67  }
68  else {
69  SetRnrAll(lst, visible);
70  lst->SetRnrSelf(visible);
71  }
72 }
73 
74 std::pair<bool,TEveElement*>
76  int max_level,
77  TEveElement* p,
78  TGeoNode* n,
79  const TGeoHMatrix& mat,
80  const std::string& nam)
81 {
82  TGeoVolume* vol = n ? n->GetVolume() : 0;
83  bool created = false;
84 
85  if ( 0 == vol || level > max_level ) return std::make_pair(created,(TEveElement*)0);
86 
87  VisAttr vis(Volume(vol).visAttributes());
88  TGeoShape* geoShape = vol->GetShape();
89  TEveElement* element = 0;
90 
91  if ( p ) {
92  TGeoNode* pn = (TGeoNode*)p->GetUserData();
93  if ( pn == n ) {
94  element = p;
95  }
96  if ( !element ) {
97  element = (TEveGeoShape*)p->FindChild(n->GetName());
98  }
99  if ( !element && !nam.empty() ) {
100  element = (TEveGeoShape*)p->FindChild(nam.c_str());
101  }
102  if ( element ) goto Daughters;
103  }
104 
105  if ( geoShape->IsA() == TGeoShapeAssembly::Class() ) {
106  //printout(INFO,"createEveShape","+++ Assembly Shape %s Userdata:%p.",n->GetName(),n);
107  ElementList* shape = new ElementList(n->GetName(),n->GetName(),true,true);
108  shape->SetUserData(n);
109  shape->SetMainTransparency(true);
110  shape->SetMainAlpha(0.2);
111  shape->SetPickable(kTRUE);
112  if ( vis.isValid() ) {
113  float red, green, blue;
114  vis.rgb(red, green, blue);
115  shape->SetMainColorRGB(red, green, blue);
116  }
117  element = shape;
118  created = true;
119  goto Daughters;
120  }
121  else if ( 0 == element ) {
122  TEveGeoShape* shape = new TEveGeoShape(n->GetName());
123  //printout(INFO,"createEveShape","+++ Create TEveGeoShape %s [%s] Userdata:%p.",
124  //n->GetName(),geoShape->IsA()->GetName(),n);
125  created = true;
126  if ( vis.isValid() ) {
127  float red, green, blue;
128  vis.rgb(red, green, blue);
129  shape->SetMainColorRGB(red, green, blue);
130  }
131  shape->SetMainTransparency(true);
132  shape->SetMainAlpha(0.2);
133  shape->SetPickable(kTRUE);
134  shape->RefMainTrans().SetFrom(mat);
135  shape->SetShape((TGeoShape*)geoShape->Clone());
136  if ( level < max_level ) {
137  shape->SetRnrSelfChildren(true,true);
138  }
139  else if ( level == max_level ) {
140  shape->SetRnrSelfChildren(true,false);
141  }
142  else if ( level > max_level ) {
143  shape->SetRnrSelfChildren(false,false);
144  }
145  shape->SetUserData(n);
146  element = shape;
147  }
148 
149  Daughters:
150  for (Int_t idau = 0, ndau = n->GetNdaughters(); idau < ndau; ++idau) {
151  TGeoNode* daughter = n->GetDaughter(idau);
152  TGeoHMatrix dau_mat(mat);
153  TGeoMatrix* matrix = daughter->GetMatrix();
154  dau_mat.Multiply(matrix);
155  std::pair<bool,TEveElement*> dau_shape =
156  createEveShape(level+1, max_level, element, daughter, dau_mat, daughter->GetName());
157  if ( dau_shape.first ) {
158  element->AddElement(dau_shape.second);
159  }
160  }
161  return std::make_pair(created,element);
162 }
163 
164 int Utilities::findNodeWithMatrix(TGeoNode* p, TGeoNode* n, TGeoHMatrix* mat, std::string* sub_path) {
165  if ( p == n ) return 1;
166  TGeoHMatrix dau_mat;
167  for (Int_t idau = 0, ndau = p->GetNdaughters(); idau < ndau; ++idau) {
168  std::string spath;
169  TGeoNode* daughter = p->GetDaughter(idau);
170  TGeoHMatrix* daughter_matrix = 0;
171  if ( mat ) {
172  TGeoMatrix* matrix = daughter->GetMatrix();
173  dau_mat = *mat;
174  dau_mat.Multiply(matrix);
175  daughter_matrix = &dau_mat;
176  }
177  int level = findNodeWithMatrix(daughter,n,daughter_matrix,sub_path ? &spath : 0);
178  if ( level>0 ) {
179  if ( sub_path ) {
180  *sub_path += "/";
181  *sub_path += spath;
182  }
183  if ( mat ) *mat = *daughter_matrix;
184  return level+1;
185  }
186  }
187  return 0;
188 }
189 
190 std::pair<bool,TEveElement*> Utilities::LoadDetElement(DetElement de,int levels, TEveElement* parent) {
191  if ( !strstr(de.name(),"BeamCal") ) {
192  PlacedVolume pv = de.placement();
193  if (pv.isValid()) {
194  TGeoNode* n = pv.ptr();
195  TGeoMatrix* matrix = n->GetMatrix();
196  std::pair<bool,TEveElement*> e = createEveShape(0, levels, parent, n, *matrix, de.name());
197  TEveElementList* list = dynamic_cast<TEveElementList*>(e.second);
198  if ( list ) {
199  list->SetName(de.name());
200  }
201  return e;
202  }
203  }
204  return std::make_pair(false,(TEveGeoShape*)0);
205 }
Objects.h
Volumes.h
dd4hep::Utilities::findNodeWithMatrix
int findNodeWithMatrix(TGeoNode *p, TGeoNode *n, TGeoHMatrix *mat, std::string *sub_path=0)
Definition: Utilities.cpp:164
dd4hep::PlacedVolume
Handle class holding a placed volume (also called physical volume)
Definition: Volumes.h:163
dd4hep::VisAttr
Handle class describing visualization attributes.
Definition: Objects.h:324
created
unsigned char created
Definition: AlignmentsCalculator.cpp:69
dd4hep::DetElement::placement
PlacedVolume placement() const
Access to the physical volume of this detector element.
Definition: DetElement.cpp:321
dd4hep::Utilities::SetRnrChildren
void SetRnrChildren(TEveElementList *l, bool b)
Set the rendering flags for the object and the next level children.
Definition: Utilities.cpp:37
dd4hep::Handle::isValid
bool isValid() const
Check the validity of the object held by the handle.
Definition: Handle.h:128
dd4hep::ElementList
Local implementation with overrides of the TEveElementList.
Definition: ElementList.h:35
dd4hep::Handle::name
const char * name() const
Access the object name (or "" if not supported by the object)
Utilities.h
TEveElementList
Class of the ROOT toolkit. See http://root.cern.ch/root/htmldoc/ClassIndex.html.
Definition: ROOTClasses.h:13
dd4hep::Utilities::createEveShape
std::pair< bool, TEveElement * > createEveShape(int level, int max_level, TEveElement *p, TGeoNode *n, const TGeoHMatrix &mat, const std::string &node_name)
Definition: Utilities.cpp:75
dd4hep::Utilities::SetRnrAll
void SetRnrAll(TEveElementList *l, bool b)
Recursively set the rendering flags for the object ans its children.
Definition: Utilities.cpp:45
dd4hep::DetElement
Handle class describing a detector element.
Definition: DetElement.h:188
dd4hep::Volume
Handle class holding a placed volume (also called physical volume)
Definition: Volumes.h:370
EventHandler.h
dd4hep::VisAttr::rgb
bool rgb(float &red, float &green, float &blue) const
Get RGB values of the color (if valid)
Definition: Objects.cpp:396
dd4hep::Utilities::LoadDetElement
std::pair< bool, TEveElement * > LoadDetElement(DetElement element, int levels, TEveElement *parent)
Definition: Utilities.cpp:190
ElementList.h
dd4hep::Handle::ptr
T * ptr() const
Access to the held object.
Definition: Handle.h:153
DetElement.h
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::Utilities::MakeNodesVisible
void MakeNodesVisible(TEveElement *e, bool visible, int level)
Make a set of nodes starting from a top element (in-)visible with a given depth.
Definition: Utilities.cpp:56
Printout.h