CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
vis_macros.cc
Go to the documentation of this file.
1 #include <list>
2 #include <cmath>
3 
4 #include <TEveElement.h>
5 #include <TEveGeoNode.h>
6 #include <TGeoNode.h>
7 
8 #include "vis_macros.h"
9 
10 // get the named from an object derived from both TEveElement and TNamed
11 const char* get_name( const TEveElement * element ) {
12  // try as a TEveGeoNode or TEveGeoShape
13  if (const TEveGeoNode * node = dynamic_cast<const TEveGeoNode *>( element ))
14  return node->GetName();
15  if (const TEveGeoShape * shape = dynamic_cast<const TEveGeoShape *>( element ))
16  return shape->GetName();
17 
18  // try to access the element as a named thingy
19  if (const TNamed * named = dynamic_cast<const TNamed *>( element ))
20  return named->GetName();
21 
22  return 0;
23 }
24 
25 // force a node to expand its internal reprsentation, so all children are actually present
26 void expand_node( TEveElement * element )
27 {
28  // force a TEveGeoNode to load all its children
29  if (TEveGeoNode * node = dynamic_cast<TEveGeoNode *>( element )) {
30  if (node->NumChildren() == 0 && node->GetNode()->GetVolume()->GetNdaughters() > 0) {
31  TIter next(node->GetNode()->GetVolume()->GetNodes());
32  TGeoNode* dnode;
33  while ((dnode = (TGeoNode*) next()) != 0) {
34  TEveGeoNode* node_re = new TEveGeoNode(dnode);
35  node->AddElement(node_re);
36  }
37  }
38  return;
39  }
40  // a TEveGeoShape is always exanded
41  //if (TEveGeoShape * shape __attribute__ ((unused)) = dynamic_cast<TEveGeoShape *>( element )) {
42  // return;
43  //}
44  // a generic TEveElement has no knwledge on children expansion
45  return;
46 }
47 
48 // retrieves a TShape from a TEveElement
49 const TGeoShape * get_shape( const TEveElement * element ) {
50  // a TEveGeoNode, can look into its TGeoNode and retrieve the shape
51  if (const TEveGeoNode * node = dynamic_cast<const TEveGeoNode *>( element )) {
52  return node->GetNode()->GetVolume()->GetShape();
53  }
54  // a TEveGeoShape owns its shape
55  if (const TEveGeoShape * shape = dynamic_cast<const TEveGeoShape *>( element )) {
56  TEveGeoShape * nc_shape = const_cast<TEveGeoShape *>( shape );
57  return const_cast<const TGeoShape *>( nc_shape->GetShape() );
58  }
59  // a TEveElement is too generic, no way to get a shape
60  return 0;
61 }
62 
63 // overloaded non-const TShape retrieval, allowed from a TGeoShape only
64 TGeoShape * get_shape( TEveElement * element ) {
65  // a TEveGeoNode cannot modify its shape
66  //if (const TEveGeoNode * node __attribute__ ((unused)) = dynamic_cast<const TEveGeoNode *>( element )) {
67  // return 0;
68  //}
69  // a TEveGeoShape owns its shape, and can modifiy it
70  if (TEveGeoShape * shape = dynamic_cast<TEveGeoShape *>( element )) {
71  return shape->GetShape();
72  }
73  // a TEveElement is too generic, no way to get a shape
74  return 0;
75 }
76 // set an element's color and alpha, and possibly its children's up to levels levels deep
77 void set_color( TEveElement * element, Color_t color, float alpha, unsigned int levels )
78 {
79  if (not element)
80  return;
81 
82  // set this node's color
83  element->SetMainColor( color );
84  if (alpha > 1.) alpha = 1.;
85  if (alpha < 0.) alpha = 0.;
86  unsigned char transparency = (unsigned char) roundf(100. - (alpha * 100.));
87  element->SetMainTransparency( transparency );
88 
89  if (levels > 0) {
90  // set the node's children's color
91  expand_node( element );
92  for (std::list<TEveElement*>::iterator i = element->BeginChildren(); i != element->EndChildren(); ++i)
93  set_color( *i, color, alpha, levels - 1);
94  }
95  // notify the element that it has changed
96  element->ElementChanged(true, true);
97 }
98 
99 // check if a node has any children or if it's a leaf node
100 bool is_leaf_node( const TEveElement * element )
101 {
102  // a TEveGeoNode can have unaccounted-for children
103  if (const TEveGeoNode * node = dynamic_cast<const TEveGeoNode *>( element )) {
104  return ((node->NumChildren() == 0) and (node->GetNode()->GetVolume()->GetNdaughters() == 0));
105  }
106  // a TEveGeoShape always knows its children
107  if (const TEveGeoShape * shape = dynamic_cast<const TEveGeoShape *>( element )) {
108  return (shape->NumChildren() == 0);
109  }
110  // default implementation
111  return (element->NumChildren() == 0);
112 }
113 
114 // toggle an elements's children visibility, based on their name
115 // names are checked only up to their length, so for example tec:TEC will match both tec:TEC_1 and tec:TEC_2
116 void set_children_visibility( TEveElement * element, const std::string & node_name, const std::vector<std::string> & children_name, bool visibility )
117 {
118  // try to access the element as a named thingy
119  const char * name = get_name( element );
120  if (not name or strncmp(name, node_name.c_str(), node_name.size()))
121  // unnamed node, or wrong node
122  return;
123 
124  for (std::list<TEveElement *>::iterator j = element->BeginChildren(); j != element->EndChildren(); ++j) {
125  TEveElement * child = *j;
126  name = get_name( child );
127  if (not name)
128  // unnamed node, ignore it
129  continue;
130 
131  for (unsigned int i = 0; i < children_name.size(); ++i)
132  if (not strncmp(name, children_name[i].c_str(), children_name[i].size())) {
133  // change this child visibility
134  if (is_leaf_node( child )) {
135  child->SetRnrSelf( visibility );
136  child->SetRnrChildren( false );
137  } else {
138  child->SetRnrSelf( false );
139  child->SetRnrChildren( visibility );
140  }
141  break;
142  }
143  }
144  // notify the element that is had changed
145  element->ElementChanged(true, true);
146 }
147 
148 // set Tracker's Endcaps visibility
149 void set_tracker_endcap_visibility( TEveElement * tracker, bool visibility )
150 {
151  std::vector<std::string> endcap;
152  endcap.push_back("tec:TEC");
153  endcap.push_back("tidf:TIDF");
154  endcap.push_back("tidb:TIDB");
155  endcap.push_back("pixfwd:PixelForwardZPlus");
156  endcap.push_back("pixfwd:PixelForwardZMinus");
157  set_children_visibility( tracker, "tracker:Tracker", endcap, visibility );
158 }
159 
160 // show Tracker's Endcaps
161 void show_tracker_endcap( TEveElement * tracker )
162 {
163  set_tracker_endcap_visibility( tracker, true );
164 }
165 
166 // hide Tracker's Endcaps
167 void hide_tracker_endcap( TEveElement * tracker )
168 {
169  set_tracker_endcap_visibility( tracker, false );
170 }
171 
int i
Definition: DBlmapReader.cc:9
float alpha
Definition: AMPTWrapper.h:95
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e< void, edm::EventIDconst &, edm::Timestampconst & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
Definition: Activities.doc:12
tuple node
Definition: Node.py:50
void set_children_visibility(TEveElement *element, const std::string &node_name, const std::vector< std::string > &children_name, bool visibility)
Definition: vis_macros.cc:116
const char * get_name(const TEveElement *element)
Definition: vis_macros.cc:11
void set_tracker_endcap_visibility(TEveElement *tracker, bool visibility)
Definition: vis_macros.cc:149
void show_tracker_endcap(TEveElement *tracker)
Definition: vis_macros.cc:161
int j
Definition: DBlmapReader.cc:9
bool is_leaf_node(const TEveElement *element)
Definition: vis_macros.cc:100
void expand_node(TEveElement *element)
Definition: vis_macros.cc:26
const TGeoShape * get_shape(const TEveElement *element)
Definition: vis_macros.cc:49
void set_color(TEveElement *element, Color_t color, float alpha, unsigned int levels)
Definition: vis_macros.cc:77
void hide_tracker_endcap(TEveElement *tracker)
Definition: vis_macros.cc:167
tuple size
Write out results.