CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
eve_filter.cc
Go to the documentation of this file.
1 #include <vector>
2 #include <string>
3 #include <iostream>
4 #include <iomanip>
5 #include <utility>
6 
7 #include "TPRegexp.h"
8 #include "TEveManager.h"
9 #include "TEveScene.h"
10 #include "TEveElement.h"
11 #include "TEveGeoNode.h"
12 #include "TGeoNode.h"
13 #include "TCollection.h"
14 #include "TObjString.h"
15 
16 #include "split.h"
17 #include "eve_filter.h"
18 #include "eve_macros.h"
19 
20 // global variables... FIXME all this stuff should be incapsulated ina a class !
21 TPRegexp parents;
22 std::vector<TPRegexp> filters;
23 std::vector<Color_t> colors;
24 unsigned int matching_nodes;
25 
26 void split_path(const std::string & path, std::string & name, std::vector<std::string> & parents) {
27  split( path, parents, '/', false );
28  if (parents.empty()) {
29  name = "";
30  } else {
31  name = parents.back();
32  parents.pop_back();
33  }
34 }
35 
36 static TPRegexp ns_name_index( "([[:alnum:]]+:[[:alnum:]-\\[\\]]+)(_[0-9]+)+" );
37 
38 // generalizes a node name of the form namespace:Name_NUM into a (regexp) filter that allows any number
39 TPRegexp make_filter(const std::string & token) {
41  filter += "(";
42  if (ns_name_index.MatchB( token ))
43  filter += ((TObjString*)(ns_name_index.MatchS( token )->At(1)))->GetString();
44  else
45  filter += token;
46  filter += ")_[0-9]+";
47  return TPRegexp( filter.c_str() );
48 }
49 
50 // generalizes a list of node names of the form namespace:Name_NUM into a (regexp) filter that allows any of the "namespace:Name" followd by any number
51 TPRegexp make_filter(const std::vector<std::string> & tokens) {
52  if (tokens.empty())
53  return TPRegexp();
54 
56  filter += "(";
57  if (ns_name_index.MatchB( tokens[0] ))
58  filter += ((TObjString*)(ns_name_index.MatchS( tokens[0] )->At(1)))->GetString();
59  else
60  filter += tokens[0];
61  for (unsigned int i = 1; i < tokens.size(); ++i) {
62  filter += "|";
63  if (ns_name_index.MatchB( tokens[i] ))
64  filter += ((TObjString*)(ns_name_index.MatchS( tokens[i] )->At(1)))->GetString();
65  else
66  filter += tokens[i];
67  }
68  filter += ")_[0-9]+";
69  return TPRegexp( filter.c_str() );
70 }
71 
72 // apply the filters to a node, specifying if the removed elements should be hidden (default), deleted (does not work) or ignored
73 void node_filter(TEveElement * node, int simplify /* = do_hide */, bool verbose /* = false */) {
74  static int indent = 0;
75  ++indent;
76 
77  expand_node(node);
78 
79  for (TEveElement::List_i i = node->BeginChildren(); i != node->EndChildren(); ++i)
80  {
81  bool found = false;
82  TEveGeoNode * child = (TEveGeoNode*)(*i);
83  for (unsigned int det = 0; det < filters.size(); ++det) {
84  if (filters[det].MatchB( child->GetName() )) {
85  // found a selcted leaf
86  if (verbose) {
87  for (int _t = 0; _t < indent; ++_t) std::cout << " ";
88  std::cout << child->GetName() << " (found)" << std::endl;
89  }
90  child->SetRnrSelf( true );
91  child->SetMainColor( colors[det] );
92  // simplify - hide or delete its children
93  switch (simplify) {
94  case do_hide:
95  child->SetRnrSelf( true );
96  child->SetRnrChildren( false );
97  break;
98  case do_remove:
99  child->Destroy();
100  break;
101  case do_nothing:
102  break;
103  }
104 
105  found = true;
106  ++matching_nodes;
107  break; // breaks out of the loop on "det"
108  }
109  }
110  if (found)
111  continue;
112  else if (parents.MatchB( child->GetName() )) {
113  // found a possible parent
114  if (verbose) {
115  for (int _t = 0; _t < indent; ++_t) std::cout << " ";
116  std::cout << child->GetName() << " (look inside...)" << std::endl;
117  }
118  child->SetRnrSelf( false );
119  child->SetRnrChildren( true );
120  node_filter( child );
121  } else {
122  // enything else
123  if (verbose) {
124  for (int _t = 0; _t < indent; ++_t) std::cout << " ";
125  std::cout << child->GetName() << " (unused)" << std::endl;
126  }
127  // simplify - hide or delete this
128  switch (simplify) {
129  case do_hide:
130  child->SetRnrSelf( false );
131  child->SetRnrChildren( false );
132  break;
133  case do_remove:
134  child->Destroy();
135  break;
136  case do_nothing:
137  break;
138  }
139  }
140  }
141 
142  --indent;
143 }
144 
145 // initializes the filter static variables from a list of elements to display and colors to use
146 void init_filter(const std::vector< std::pair< std::string, Color_t> > & elements) {
147  std::vector< std::string > all_parents;
148  std::vector< std::string > all_names;
149 
150  for (unsigned int i = 0; i < elements.size(); ++i) {
151  std::vector< std::string > s_parents;
152  std::string s_name;
153  split_path( elements[i].first, s_name, s_parents );
154  all_names.push_back( s_name );
155  all_parents.insert( all_parents.end(), s_parents.begin(), s_parents.end() );
156 
157  colors.push_back( elements[i].second );
158  }
159 
160  parents = make_filter( all_parents );
161  for (unsigned int i = 0; i < all_names.size(); ++i)
162  filters.push_back( make_filter( all_names[i] ) );
163 
164  matching_nodes = 0;
165 }
166 
167 // dump the filters
168 void dump(void) {
169  std::cout << parents.fPattern << std::endl;
170  for (unsigned int i = 0; i < filters.size(); ++i)
171  std::cout << '\t' << std::setw(32) << std::left << filters[i].fPattern << '\t' << colors[i] << std::endl;
172 }
173 
174 // apply the filters to a node, then notify it to update itself and its children for redrawing
175 void apply_filter(TEveElement * node, int simplify /* = do_hide */, bool verbose /* = false */) {
176  if (node == 0)
177  return;
178 
179  if (verbose)
180  std::cout << get_name(node) << " (look inside...)" << std::endl;
181 
182  node_filter( node, simplify );
183  node->ElementChanged( true, true );
184 
185  std::cout << "found " << matching_nodes << " matching nodes" << std::endl;
186 }
187 
188 // find a TEve root object (could be a TEveGeoRootNode or a TEveGeoShape) by its name
189 TEveElement * get_root_object(const char* name)
190 {
191  for (TEveElement::List_i i = gEve->GetScenes()->BeginChildren(); i != gEve->GetScenes()->EndChildren(); ++i) {
192  TEveScene * scene = dynamic_cast<TEveScene *> (*i);
193  if (not scene)
194  continue;
195  for (TEveElement::List_i n = scene->BeginChildren(); n != scene->EndChildren(); ++n) {
196  const char* obj_name = get_name( *n );
197  if (obj_name == 0 or strcmp(name, obj_name))
198  continue;
199 
200  return *n;
201  }
202  }
203 
204  return 0;
205 }
int i
Definition: DBlmapReader.cc:9
TPRegexp parents
Definition: eve_filter.cc:21
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
void node_filter(TEveElement *node, int simplify, bool verbose)
Definition: eve_filter.cc:73
unsigned int matching_nodes
Definition: eve_filter.cc:24
static TPRegexp ns_name_index("([[:alnum:]]+:[[:alnum:]-\\[\\]]+)(_[0-9]+)+")
void init_filter(const std::vector< std::pair< std::string, Color_t > > &elements)
Definition: eve_filter.cc:146
std::vector< TPRegexp > filters
Definition: eve_filter.cc:22
tuple node
Definition: Node.py:50
dictionary elements
U second(std::pair< T, U > const &p)
vector< Color_t > colors
TPRegexp make_filter(const std::string &token)
Definition: eve_filter.cc:39
TEveElement * get_root_object(const char *name)
Definition: eve_filter.cc:189
tuple cout
Definition: gather_cfg.py:121
void expand_node(TEveElement *element)
Definition: eve_macros.cc:43
double split
Definition: MVATrainer.cc:139
const char * get_name(const TEveElement *element)
Definition: eve_macros.cc:13