DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
teve_display.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/Factories.h>
16 #include <DD4hep/Detector.h>
17 #include <DDRec/SurfaceHelper.h>
18 
19 #include "EvNavHandler.h"
20 #include "MultiView.h"
21 
22 #include "run_plugin.h"
23 #include <TRint.h>
24 
25 #include <TROOT.h>
26 #include <TEveGeoNode.h>
27 #include <TEveBrowser.h>
28 #include <TGNumberEntry.h>
29 #include <TGButton.h>
30 #include <TGLabel.h>
31 #include <TStyle.h>
32 #include <TGComboBox.h>
33 #include <TEveManager.h>
34 #include <TSystem.h>
35 #include <TGLViewer.h>
36 #include <TEveViewer.h>
37 #include <TGLPerspectiveCamera.h>
38 #include <TGLCamera.h>
39 #include <TEveStraightLineSet.h>
40 #include <TSysEvtHandler.h>
41 #include <TEveScene.h>
42 #include <TEveProjectionManager.h>
43 #include <TEveProjectionAxes.h>
44 #include <TEveWindow.h>
45 
46 #include <TGeoManager.h>
47 #include <TGLClip.h>
48 #include <TMap.h>
49 #include <TObjString.h>
50 #include <TGeoShape.h>
51 #include <TGLScenePad.h>
52 
53 
54 using namespace dd4hep ;
55 using namespace rec ;
56 using namespace detail ;
57 
58 //=====================================================================================
59 // function declarations:
60 void next_event();
61 void make_gui();
62 
63 TEveStraightLineSet* getSurfaces(int col=kRed, const SurfaceType& type=SurfaceType(), TString name="Surfaces" ) ;
64 TEveStraightLineSet* getSurfaceVectors(bool addO=true, bool addU= true, bool addV=true, bool addN=true,TString name="SurfaceVectors",int color=kGreen) ;
65 
66 //=====================================================================================
67 static void print_teve_help() {
68  std::cout <<
69  "Usage: teveDisplay -arg [-arg] \n\n"
70  " Invoke TEve display using the factory mechanism. \n\n"
71  " -input <file> [OPTIONAL] Specify geometry input file. \n";
72  print_default_args() << std::endl <<
73  " -level <number> Visualization level [TGeoManager::SetVisLevel] Default: 4 \n"
74  " -visopt <number> Visualization option [TGeoManager::SetVisOption] Default: 0 \n"
75  " -help Print this help output" << std::endl << std::flush;
76  ::exit(EINVAL);
77 }
78 
79 //=====================================================================================
80 
81 static long teve_display(Detector& description, int argc, char** argv) {
82  int level = 4, visopt = 0, help = 0;
83  for( int i=0; i<argc; ++i ) {
84  if ( strncmp(argv[i],"-visopt",4) == 0 ) visopt = ::atol(argv[++i]);
85  else if ( strncmp(argv[i],"-level", 4) == 0 ) level = ::atol(argv[++i]);
86  else help = 1;
87  }
88  if ( help ) {
89  print_teve_help();
90  }
91 
92  TGeoManager* mgr = &description.manager();
93  mgr->SetNsegments(100); // Increase the visualization resolution.
94  TEveManager::Create();
95 
96  TEveGeoTopNode* tn = new TEveGeoTopNode(mgr, mgr->GetTopNode());
97  // option 0 in TEve seems to correspond to option 1 in TGeo ( used in geoDisplay ...)
98  tn->SetVisOption(visopt) ;
99  tn->SetVisLevel(level);
100 
101  // // ---- try to set transparency - does not seem to work ...
102  // TGeoNode* node1 = gGeoManager->GetTopNode();
103  // int k_nodes = node1->GetNdaughters();
104  // for(int i=0; i<k_nodes; i++) {
105  // TGeoNode * CurrentNode = node1->GetDaughter(i);
106  // CurrentNode->GetMedium()->GetMaterial()->SetTransparency(80);
107  // // TEveGeoNode CurrentNode( node1->GetDaughter(i) ) ;
108  // // CurrentNode.SetMainTransparency( 80 ) ;
109  // }
110  // this code seems to have no effect either ...
111  // tn->CSCApplyMainTransparencyToAllChildren() ;
112  // tn->SetMainTransparency( 80 ) ;
113 
114 
115  gEve->AddGlobalElement( tn );
116 
117  TEveElement* surfaces = getSurfaces( kRed, SurfaceType( SurfaceType::Sensitive ), "SensitiveSurfaces" ) ;
118  TEveElement* helperSurfaces = getSurfaces( kGray, SurfaceType( SurfaceType::Helper ),"HelperSurfaces" ) ;
119  TEveElement* surfaceVectors = getSurfaceVectors(1,0,0,1,"SurfaceVectorsN",kGreen) ;
120 
121  gEve->AddGlobalElement( surfaces ) ;
122  gEve->AddGlobalElement( helperSurfaces ) ;
123  gEve->AddGlobalElement( surfaceVectors ) ;
124 
125 
126  TEveElement* surfaceVectors_u = getSurfaceVectors(0,1,0,0,"SurfaceVectorsU",kMagenta) ;
127 
128  gEve->AddGlobalElement( surfaceVectors_u ) ;
129 
130  TEveElement* surfaceVectors_v = getSurfaceVectors(0,0,1,0,"SurfaceVectorsV",kBlack) ;
131 
132  gEve->AddGlobalElement( surfaceVectors_v ) ;
133 
134  TGLViewer *v = gEve->GetDefaultGLViewer();
135  // v->GetClipSet()->SetClipType(TGLClip::kClipPlane);
136  // v->ColorSet().Background().SetColor(kMagenta+4);
137  v->ColorSet().Background().SetColor(kWhite);
138  v->SetGuideState(TGLUtil::kAxesEdge, kTRUE, kFALSE, 0);
139  v->RefreshPadEditor(v);
140  // v->CurrentCamera().RotateRad(-1.2, 0.5);
141 
142  gEve->GetGlobalScene()->GetGLScene()->SetSelectable(kFALSE) ; //change for debugging (kTRUE);
143 
144 
145  MultiView::instance()->ImportGeomRPhi( surfaces );
146  MultiView::instance()->ImportGeomRhoZ( surfaces ) ;
147  MultiView::instance()->ImportGeomRPhi( helperSurfaces );
148  MultiView::instance()->ImportGeomRhoZ( helperSurfaces ) ;
149 
150  make_gui();
151  next_event();
152  gEve->FullRedraw3D(kTRUE);
153  return 1;
154 }
155 DECLARE_APPLY(DD4hepTEveDisplay,teve_display)
156 
157 //=====================================================================================================================
158 int main(int argc,char** argv) {
159  std::vector<const char*> av;
160  std::string level, visopt, opt;
161  bool help = (argc == 1);
162 
163  for( int i=0; i < argc; ++i ) {
164  if ( i == 1 && argv[i][0] != '-' ) av.emplace_back("-input");
165  else if ( strncmp(argv[i],"-help", 4) == 0 ) help = true;
166  else if ( strncmp(argv[i],"--help", 5) == 0 ) help = true;
167  else if ( strncmp(argv[i],"-visopt", 4) == 0 ) visopt = argv[++i];
168  else if ( strncmp(argv[i],"--visopt",5) == 0 ) visopt = argv[++i];
169  else if ( strncmp(argv[i],"-level", 4) == 0 ) level = argv[++i];
170  else if ( strncmp(argv[i],"--level", 5) == 0 ) level = argv[++i];
171  else if ( strncmp(argv[i],"-option", 4) == 0 ) opt = argv[++i];
172  else if ( strncmp(argv[i],"--option",5) == 0 ) opt = argv[++i];
173  else if ( argc > 2 ) av.emplace_back(argv[i]);
174  }
175  if ( !help && argc == 2 ) { // Single argument given --> geometry file
176  av.emplace_back("-input");
177  av.emplace_back(argv[1]);
178  }
179  av.emplace_back("-interactive");
180  av.emplace_back("-plugin");
181  av.emplace_back("DD4hepTEveDisplay");
182  if ( help ) av.emplace_back("-help");
183  if ( !opt.empty() ) av.emplace_back("-opt"), av.emplace_back(opt.c_str());
184  if ( !level.empty() ) av.emplace_back("-level"), av.emplace_back(level.c_str());
185  if ( !visopt.empty() ) av.emplace_back("-visopt"), av.emplace_back(visopt.c_str());
186  if ( help ) {
187  print_teve_help();
188  }
189  return dd4hep::execute::main_plugins("DD4hepTEveDisplay", av.size(), (char**)&av[0]);
190 }
191 
192 //=====================================================================================================================
193 TEveStraightLineSet* getSurfaceVectors(bool addO, bool addU, bool addV, bool addN, TString name,int color) {
194  TEveStraightLineSet* ls = new TEveStraightLineSet(name);
195  Detector& description = Detector::getInstance();
196  DetElement world = description.world();
197 
198  // create a list of all surfaces in the detector:
199  SurfaceHelper surfMan( world );
200  const SurfaceList& sL = surfMan.surfaceList() ;
201  for( SurfaceList::const_iterator it = sL.begin() ; it != sL.end() ; ++it ){
202  ISurface* surf = *it ;
203  const Vector3D& u = surf->u() ;
204  const Vector3D& v = surf->v() ;
205  const Vector3D& n = surf->normal() ;
206  const Vector3D& o = surf->origin() ;
207 
208  Vector3D ou = o + u ;
209  Vector3D ov = o + v ;
210  Vector3D on = o + n ;
211 
212  if (addU) ls->AddLine( o.x(), o.y(), o.z(), ou.x() , ou.y() , ou.z() );
213 
214 // TEveStraightLineSet::Marker_t *m = ls->AddMarker(id,1.);
215 
216  if (addV) ls->AddLine( o.x(), o.y(), o.z(), ov.x() , ov.y() , ov.z() );
217  if (addN) ls->AddLine( o.x(), o.y(), o.z(), on.x() , on.y() , on.z() );
218  if (addO) ls->AddMarker( o.x(), o.y(), o.z() );
219  }
220  ls->SetLineColor( color ) ;
221  ls->SetMarkerColor( kBlue ) ;
222  ls->SetMarkerSize(1);
223  ls->SetMarkerStyle(4);
224 
225  // gEve->AddElement(ls);
226 
227  return ls;
228 }
229 //=====================================================================================
230 TEveStraightLineSet* getSurfaces(int col, const SurfaceType& type, TString name) {
231 
232  TEveStraightLineSet* ls = new TEveStraightLineSet(name);
233 
234  Detector& description = Detector::getInstance();
235 
236  DetElement world = description.world() ;
237 
238  // create a list of all surfaces in the detector:
239  SurfaceHelper surfMan( world ) ;
240 
241  const SurfaceList& sL = surfMan.surfaceList() ;
242 
243 // std::cout << " getSurfaces() for "<<name<<" - #surfaces : " << sL.size() << std::endl ;
244 
245  for( SurfaceList::const_iterator it = sL.begin() ; it != sL.end() ; ++it ){
246 
247  Surface* surf = dynamic_cast< Surface*> ( *it ) ;
248 
249  if( ! surf )
250  continue ;
251 
252  if( ! ( surf->type().isVisible() && ( surf->type().isSimilar( type ) ) ) )
253  continue ;
254 
255 // std::cout << " **** drawSurfaces() : empty lines vector for surface " << *surf << std::endl ;
256 
257  const std::vector< std::pair<Vector3D,Vector3D> > lines = surf->getLines() ;
258 
259  if( lines.empty() )
260  std::cout << " **** drawSurfaces() : empty lines vector for surface " << *surf << std::endl ;
261 
262  unsigned nL = lines.size() ;
263 
264  for( unsigned i=0 ; i<nL ; ++i){
265 
266  // std::cout << " **** drawSurfaces() : draw line for surface " << lines[i].first << " - " << lines[i].second << std::endl ;
267 
268  ls->AddLine( lines[i].first.x(), lines[i].first.y(), lines[i].first.z(),
269  lines[i].second.x(), lines[i].second.y(), lines[i].second.z() ) ;
270  }
271 
272  ls->SetLineColor( col ) ;
273  ls->SetMarkerColor( col ) ;
274  ls->SetMarkerSize(.1);
275  ls->SetMarkerStyle(4);
276 
277  }
278 
279  return ls;
280 }
281 
282 //=====================================================================================
283 
284 void make_gui() {
285 
286  // Create minimal GUI for event navigation.
287 
288  TEveBrowser* browser = gEve->GetBrowser();
289  browser->StartEmbedding(TRootBrowser::kLeft);
290 
291  TGMainFrame* frmMain = new TGMainFrame(gClient->GetRoot(), 1000, 600);
292  frmMain->SetWindowName("dd4hep GUI");
293  frmMain->SetCleanup(kDeepCleanup);
294 
295  TGHorizontalFrame* hf = new TGHorizontalFrame(frmMain);
296  {
297  TString icondir( Form("%s/", TROOT::GetIconPath().Data()) );
298  TGPictureButton* b = 0;
299  EvNavHandler *fh = new EvNavHandler;
300 
301  b = new TGPictureButton(hf, gClient->GetPicture(icondir+"GoBack.gif"));
302  b->SetEnabled(kFALSE);
303  b->SetToolTipText("Go to previous event - not supported.");
304  hf->AddFrame(b);
305  b->Connect("Clicked()", "EvNavHandler", fh, "Bck()");
306 
307  b = new TGPictureButton(hf, gClient->GetPicture(icondir+"GoForward.gif"));
308  b->SetToolTipText("Generate new event.");
309  hf->AddFrame(b);
310  b->Connect("Clicked()", "EvNavHandler", fh, "Fwd()");
311  }
312  frmMain->AddFrame(hf);
313 
314  frmMain->MapSubwindows();
315  frmMain->Resize();
316  frmMain->MapWindow();
317 
318  browser->StopEmbedding();
319  browser->SetTabTitle("Event Control", 0);
320 }
321 //=====================================================================================
322 
dd4hep::Detector::manager
virtual TGeoManager & manager() const =0
Access the geometry manager of this instance.
main
int main(int argc, char **argv)
Definition: teve_display.cpp:158
dd4hep::Detector::world
virtual DetElement world() const =0
Return reference to the top-most (world) detector element.
dd4hep::rec::SurfaceType::isSimilar
bool isSimilar(const SurfaceType &otherType) const
true if all properties of otherType are also true for this type.
Definition: ISurface.h:243
TGMainFrame
Class of the ROOT toolkit. See http://root.cern.ch/root/htmldoc/ClassIndex.html.
Definition: ROOTClasses.h:33
dd4hep::rec::Vector3D::z
double z() const
Definition: Vector3D.h:109
dd4hep::rec::ISurface::normal
virtual Vector3D normal(const Vector3D &point=Vector3D()) const =0
Access to the normal direction at the given point.
v
View * v
Definition: MultiView.cpp:28
dd4hep::rec::Vector3D
Definition: Vector3D.h:32
Detector.h
dd4hep::rec::ISurface
Definition: ISurface.h:39
MultiView.h
DECLARE_APPLY
#define DECLARE_APPLY(name, func)
Definition: Factories.h:281
SurfaceHelper.h
dd4hep::rec::ISurface::origin
virtual const Vector3D & origin() const =0
dd4hep::rec::ISurface::v
virtual Vector3D v(const Vector3D &point=Vector3D()) const =0
dd4hep::rec::Vector3D::x
double x() const
Definition: Vector3D.h:103
Factories.h
help
void help(std::string argv0)
Definition: listcomponents.cpp:29
make_gui
void make_gui()
Definition: teve_display.cpp:284
getSurfaces
TEveStraightLineSet * getSurfaces(int col=kRed, const SurfaceType &type=SurfaceType(), TString name="Surfaces")
Definition: teve_display.cpp:230
dd4hep::Detector::getInstance
static Detector & getInstance(const std::string &name="default")
—Factory method----—
Definition: DetectorImp.cpp:150
dd4hep::DetElement
Handle class describing a detector element.
Definition: DetElement.h:188
dd4hep::rec::SurfaceList
Definition: Surface.h:681
run_plugin.h
dd4hep::rec::SurfaceType::Sensitive
@ Sensitive
Definition: ISurface.h:147
dd4hep::rec::SurfaceHelper::surfaceList
const SurfaceList & surfaceList()
Definition: SurfaceHelper.h:40
dd4hep::rec::Surface::type
virtual const SurfaceType & type() const
Definition: Surface.cpp:613
EvNavHandler.h
dd4hep::rec::SurfaceType::Helper
@ Helper
Definition: ISurface.h:148
dd4hep::rec::Surface::getLines
virtual std::vector< std::pair< Vector3D, Vector3D > > getLines(unsigned nMax=100)
Definition: Surface.cpp:833
dd4hep::rec::SurfaceType
Definition: ISurface.h:140
MultiView::instance
static MultiView * instance()
Definition: MultiView.h:57
dd4hep::rec::SurfaceType::isVisible
bool isVisible() const
true if surface is not invisble - for drawing only
Definition: ISurface.h:220
dd4hep::rec::Vector3D::y
double y() const
Definition: Vector3D.h:106
next_event
void next_event()
Definition: next_event_dummy.cpp:19
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::Detector
The main interface to the dd4hep detector description package.
Definition: Detector.h:90
dd4hep::execute::main_plugins
int main_plugins(const char *name, int argc, char **argv)
Main entry point as a program.
Definition: run_plugin.h:418
getSurfaceVectors
TEveStraightLineSet * getSurfaceVectors(bool addO=true, bool addU=true, bool addV=true, bool addN=true, TString name="SurfaceVectors", int color=kGreen)
Definition: teve_display.cpp:193
dd4hep::rec::Surface
Definition: Surface.h:498
dd4hep::rec::SurfaceHelper
Definition: SurfaceHelper.h:29
dd4hep::rec::ISurface::u
virtual Vector3D u(const Vector3D &point=Vector3D()) const =0