Belle II Software  release-05-02-19
EveGeometry.cc
1 #include <display/EveGeometry.h>
2 
3 #include <geometry/GeometryManager.h>
4 #include <framework/logging/Logger.h>
5 #include <framework/utilities/FileSystem.h>
6 #include <framework/utilities/ColorPalette.h>
7 
8 #include <TGeoManager.h>
9 #include <TPRegexp.h>
10 #include <TEveManager.h>
11 #include <TEveGeoNode.h>
12 #include <TEveGeoShapeExtract.h>
13 #include <TFile.h>
14 
15 #include <cassert>
16 
17 using namespace Belle2;
18 using namespace Belle2::TangoPalette;
19 
20 namespace {
21  static TEveGeoTopNode* s_eveTopNode = nullptr;
22  static TEveGeoShape* s_simplifiedShape = nullptr;
23  static std::vector<std::string> s_hideVolumes = {};
24  static std::vector<std::string> s_deleteVolumes = {};
25  static std::string s_eveGeometryExtractPath = "/data/display/geometry_extract.root";
26 }
27 
29 {
30  B2DEBUG(100, "Setting up geometry for TEve...");
31  if (!gEve)
32  B2FATAL("gEve must be set up before EveGeometry!");
33 
34  if (visMode == c_Full) {
35  if (!gGeoManager) { //TGeo geometry not initialized, do it ourselves
36  //convert geant4 geometry to TGeo geometry
38  geoManager.createTGeoRepresentation();
39  if (!gGeoManager) {
40  B2FATAL("Couldn't create TGeo geometry! Please make sure you add the Geometry module before Display");
41  return;
42  }
43  }
44  //set colours by atomic mass number
45  gGeoManager->DefaultColors();
46 
47  setTransparency(90);
48  //set some nicer colors (at top level only)
49  setVolumeColor("PXD.Envelope", getTColorID("Chameleon", 2));
50  setVolumeColor("SVD.Envelope", getTColorID("Orange", 3));
51  setVolumeColor("logical_ecl", getTColorID("Orange", 1));
52  setVolumeColor("BKLM.EnvelopeLogical", getTColorID("Chameleon", 1));
53  setVolumeColor("Endcap_1", getTColorID("Chameleon", 1));
54  setVolumeColor("Endcap_2", getTColorID("Chameleon", 1));
55 
56  for (auto& volume : s_hideVolumes)
57  disableVolume(volume.c_str(), false);
58 
59  TGeoNode* top_node = gGeoManager->GetTopNode();
60  assert(top_node != NULL);
61  s_eveTopNode = new TEveGeoTopNode(gGeoManager, top_node);
62  s_eveTopNode->IncDenyDestroy();
63  s_eveTopNode->SetVisLevel(2);
64 
65  gEve->AddGlobalElement(s_eveTopNode);
66 
67  //expand geometry in eve list (otherwise one needs three clicks to see that it has children)
68  s_eveTopNode->ExpandIntoListTreesRecursively();
69 
70  }
71  B2DEBUG(100, "Loading geometry projections...");
72 
73  const std::string extractPath = FileSystem::findFile(s_eveGeometryExtractPath);
74  TFile* f = TFile::Open(extractPath.c_str(), "READ");
75  TEveGeoShapeExtract* gse = dynamic_cast<TEveGeoShapeExtract*>(f->Get("Extract"));
76  s_simplifiedShape = TEveGeoShape::ImportShapeExtract(gse, 0);
77  s_simplifiedShape->SetRnrSelf(false);
78  s_simplifiedShape->IncDenyDestroy();
79  s_simplifiedShape->SetName("Minimal geometry extract");
80  delete f;
81 
82  //I want to show full geo in unprojected view,
83  //but I still need to add the extract to the geometry scene...
84  gEve->AddGlobalElement(s_simplifiedShape);
85 
86  setVisualisationMode(visMode);
87 
88  // Allow deletion only for full geometry
89  if (s_eveTopNode) {
90  for (auto& volumeRegExp : s_deleteVolumes) {
91  removeChildrenByRegExp(s_eveTopNode, volumeRegExp);
92  }
93  }
94 
95  B2DEBUG(100, "Done.");
96 }
97 
98 void EveGeometry::removeChildrenByRegExp(TEveElement* parent, const std::string& pattern)
99 {
100  TPRegexp reAll(".*");
101  bool onlyChildren = false;
102  std::string regexp = pattern;
103 
104  // For patterns with leading '#', do delete only
105  // children elements (and remove the '#' for regexp)
106  if (pattern.substr(0, 1) == std::string("#")) {
107  regexp = pattern.substr(1);
108  onlyChildren = true;
109  }
110 
111  TPRegexp reMatch(regexp.c_str());
112 
113  std::list<TEveElement*> children;
114  parent->FindChildren(children, reAll);
115 
116  for (TEveElement* child : children) {
117  if (reMatch.MatchB(child->GetElementName())) {
118  if (onlyChildren) {
119  B2INFO("Removing children of " << child->GetElementName());
120  child->DestroyElements();
121  } else {
122  B2INFO("Removing " << child->GetElementName());
123  child->Destroy();
124  }
125  } else {
126  removeChildrenByRegExp(child, pattern);
127  }
128  }
129 }
130 
132 {
133  bool fullgeo = (visMode == c_Full);
134  if (s_eveTopNode)
135  s_eveTopNode->SetRnrSelfChildren(fullgeo, fullgeo);
136  s_simplifiedShape->SetRnrSelfChildren(false, !fullgeo);
137 }
138 
139 void EveGeometry::enableVolume(const char* name, bool only_daughters, bool enable)
140 {
141  if (!gGeoManager) return;
142  TGeoVolume* vol = gGeoManager->FindVolumeFast(name);
143  if (vol) {
144  if (!only_daughters)
145  vol->SetVisibility(enable);
146  vol->SetVisDaughters(enable);
147  } else {
148  B2DEBUG(100, "Volume " << name << " not found?");
149  }
150 }
151 
152 void EveGeometry::disableVolume(const char* name, bool only_daughters) { enableVolume(name, only_daughters, false); }
153 
154 void EveGeometry::setVolumeColor(const char* name, Color_t col)
155 {
156  if (!gGeoManager) return;
157  TGeoVolume* vol = gGeoManager->FindVolumeFast(name);
158  if (vol) {
159  //while TGeoVolume derives from TAttFill, the line color actually is important here
160  vol->SetLineColor(col);
161  } else {
162  B2DEBUG(100, "Volume " << name << " not found?");
163  }
164 }
165 
167 {
168  if (!gGeoManager) return;
169  TObjArray* volumes = gGeoManager->GetListOfVolumes();
170  for (int i = 0; i < volumes->GetEntriesFast(); i++) {
171  TGeoVolume* volume = static_cast<TGeoVolume*>(volumes->At(i));
172  volume->SetTransparency(percent);
173  }
174 }
175 
177 {
178  if (gGeoManager && gGeoManager->GetTopNode()) {
179  TGeoVolume* top_node = gGeoManager->GetTopNode()->GetVolume();
180  double p[3] = { 380.0, 0.0, 0.0 }; //ok for normal Belle II geometry
181  while (!top_node->Contains(p)) {
182  p[0] *= 0.8;
183  }
184  return p[0];
185  } else if (s_simplifiedShape) {
186  s_simplifiedShape->ComputeBBox();
187  const float* bbox = s_simplifiedShape->GetBBox();
188  return std::min(380.f, std::max({std::abs(bbox[0]), bbox[1], std::abs(bbox[2]), bbox[2]}));
189  }
190  // fallback to something reasonable
191  return 380.;
192 }
193 
195 {
196  TGeoManager* my_tgeomanager = gGeoManager;
197  if (!s_eveTopNode) {
198  B2ERROR("Couldn't find TEveGeoTopNode");
199  return;
200  }
201  s_eveTopNode->ExpandIntoListTrees();
202  s_eveTopNode->SaveExtract("geometry_extract.root", "Extract", false);
203 
204  //this doesn't work too well (i.e. crashes when geometry is drawn)
205  //s_eveTopNode->ExpandIntoListTreesRecursively();
206  //s_eveTopNode->SaveExtract("display_geometry_full.root", "Extract", false);
207  gGeoManager = my_tgeomanager;
208 }
209 
210 void EveGeometry::setCustomExtractPath(const std::string& extractPath)
211 {
212  s_eveGeometryExtractPath = extractPath;
213 }
214 
215 void EveGeometry::setHideVolumes(const std::vector<std::string>& volumes)
216 {
217  s_hideVolumes = volumes;
218 }
219 
220 void EveGeometry::setDeleteVolumes(const std::vector<std::string>& volumes)
221 {
222  s_deleteVolumes = volumes;
223 }
Belle2::EveGeometry::setHideVolumes
void setHideVolumes(const std::vector< std::string > &volumes)
List of volumes to be hidden (can be re-enabled in Eve panel / Geometry scene.
Definition: EveGeometry.cc:215
Belle2::EveGeometry::setVolumeColor
void setVolumeColor(const char *name, Color_t col)
set fill color of the volume 'name' to 'col'.
Definition: EveGeometry.cc:154
Belle2::EveGeometry::getMaxR
double getMaxR()
find a point that is inside the top node.
Definition: EveGeometry.cc:176
Belle2::EveGeometry::EType
EType
Type of geometry shown.
Definition: EveGeometry.h:18
Belle2::EveGeometry::saveExtract
void saveExtract()
Save a geometry extract from the current state of the TGeo geometry.
Definition: EveGeometry.cc:194
Belle2::EveGeometry::enableVolume
void enableVolume(const char *name, bool only_daughters=false, bool enable=true)
enable/disable rendering of the volume 'name', or only its daughters if only_daughters is set.
Definition: EveGeometry.cc:139
Belle2::geometry::GeometryManager::getInstance
static GeometryManager & getInstance()
Return a reference to the instance.
Definition: GeometryManager.cc:98
Belle2::EveGeometry::addGeometry
void addGeometry(EType visMode)
Add TGeo geometry to Eve (only needs to be done once.)
Definition: EveGeometry.cc:28
Belle2::geometry::GeometryManager::createTGeoRepresentation
void createTGeoRepresentation()
Create a TGeo representation of the native geometry description.
Definition: GeometryManager.cc:351
Belle2::EveGeometry::setVisualisationMode
void setVisualisationMode(EType visMode)
switch to given visualisation mode.
Definition: EveGeometry.cc:131
Belle2::EveGeometry::setDeleteVolumes
void setDeleteVolumes(const std::vector< std::string > &volumes)
List of volumes to be removed.
Definition: EveGeometry.cc:220
Belle2::geometry::GeometryManager
Class to manage the creation and conversion of the geometry.
Definition: GeometryManager.h:50
Belle2::TangoPalette::getTColorID
int getTColorID(const std::string &tangoName, int tangoId=1)
Get TColor ID for given name in tango colour palette.
Definition: ColorPalette.cc:31
Belle2::EveGeometry::setCustomExtractPath
void setCustomExtractPath(const std::string &extractPath)
Set custom path to the geometry extract (to change originally hard-coded value)
Definition: EveGeometry.cc:210
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::EveGeometry::disableVolume
void disableVolume(const char *name, bool only_daughters=false)
disable rendering of the volume 'name', or only its daughters if only_daughters is set.
Definition: EveGeometry.cc:152
Belle2::EveGeometry::removeChildrenByRegExp
void removeChildrenByRegExp(TEveElement *parent, const std::string &pattern)
Recursive removal of volumes based on regular expression pattern.
Definition: EveGeometry.cc:98
Belle2::EveGeometry::c_Full
@ c_Full
Full geometry converted from Geant4 (use this for non-standard Belle II setups!).
Definition: EveGeometry.h:19
Belle2::EveGeometry::setTransparency
void setTransparency(int percent)
Recursively set transparency of geometry (0: opaque, 100: fully transparent).
Definition: EveGeometry.cc:166
Belle2::FileSystem::findFile
static std::string findFile(const std::string &path, bool silent=false)
Search for given file or directory in local or central release directory, and return absolute path if...
Definition: FileSystem.cc:147
Belle2::TangoPalette
Implements a colour palette, see http://sobac.com/sobac/tangocolors.htm.
Definition: ColorPalette.h:16