Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:15:01

0001 //==========================================================================
0002 //  AIDA Detector description implementation 
0003 //--------------------------------------------------------------------------
0004 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
0005 // All rights reserved.
0006 //
0007 // For the licensing terms see $DD4hepINSTALL/LICENSE.
0008 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
0009 //
0010 // Author     : M.Frank
0011 //
0012 //==========================================================================
0013 
0014 // Framework include files
0015 #include <DD4hep/Factories.h>
0016 #include <DD4hep/Detector.h>
0017 #include <DDRec/SurfaceHelper.h>
0018 
0019 #include "EvNavHandler.h"
0020 #include "MultiView.h"
0021 
0022 #include "run_plugin.h"
0023 #include <TRint.h>
0024 
0025 #include <TROOT.h>
0026 #include <TEveGeoNode.h>
0027 #include <TEveBrowser.h>
0028 #include <TGNumberEntry.h>
0029 #include <TGButton.h>
0030 #include <TGLabel.h>
0031 #include <TStyle.h>
0032 #include <TGComboBox.h>
0033 #include <TEveManager.h>
0034 #include <TSystem.h>
0035 #include <TGLViewer.h>
0036 #include <TEveViewer.h>
0037 #include <TGLPerspectiveCamera.h>
0038 #include <TGLCamera.h>
0039 #include <TEveStraightLineSet.h>
0040 #include <TSysEvtHandler.h>
0041 #include <TEveScene.h>
0042 #include <TEveProjectionManager.h>
0043 #include <TEveProjectionAxes.h>
0044 #include <TEveWindow.h>
0045 
0046 #include <TGeoManager.h>
0047 #include <TGLClip.h>
0048 #include <TMap.h>
0049 #include <TObjString.h>
0050 #include <TGeoShape.h>
0051 #include <TGLScenePad.h>
0052 
0053 
0054 using namespace dd4hep ;
0055 using namespace rec ;
0056 using namespace detail ;
0057 
0058 //=====================================================================================
0059 // function declarations: 
0060 void next_event();
0061 void make_gui();
0062 
0063 TEveStraightLineSet* getSurfaces(int col=kRed, const SurfaceType& type=SurfaceType(), TString name="Surfaces" ) ;
0064 TEveStraightLineSet* getSurfaceVectors(bool addO=true, bool addU= true, bool addV=true, bool addN=true,TString name="SurfaceVectors",int color=kGreen) ;
0065 
0066 //=====================================================================================
0067 static void print_teve_help()   {
0068     std::cout <<
0069       "Usage: teveDisplay -arg [-arg]                                                    \n\n"
0070       "     Invoke TEve display using the factory mechanism.                             \n\n"
0071       "        -input  <file>  [OPTIONAL]  Specify geometry input file.                  \n";
0072     print_default_args() << std::endl <<
0073       "     -level    <number> Visualization level  [TGeoManager::SetVisLevel]  Default: 4 \n"
0074       "     -visopt   <number> Visualization option [TGeoManager::SetVisOption] Default: 0 \n"
0075       "     -help              Print this help output" << std::endl << std::flush;
0076     ::exit(EINVAL);
0077 }
0078 
0079 //=====================================================================================
0080 
0081 static long teve_display(Detector& description, int argc, char** argv)  {
0082   int level = 4, visopt = 0, help = 0;
0083   for( int i=0; i<argc; ++i )  {
0084     if ( strncmp(argv[i],"-visopt",4)    == 0 ) visopt = ::atol(argv[++i]);
0085     else if ( strncmp(argv[i],"-level", 4)    == 0 ) level  = ::atol(argv[++i]);
0086     else help = 1;
0087   }
0088   if ( help )   {
0089     print_teve_help();
0090   }
0091   
0092   TGeoManager* mgr = &description.manager();
0093   mgr->SetNsegments(100); // Increase the visualization resolution.
0094   TEveManager::Create();
0095 
0096   TEveGeoTopNode* tn = new TEveGeoTopNode(mgr, mgr->GetTopNode());
0097   // option 0 in TEve seems to correspond to option 1 in TGeo ( used in geoDisplay ...)
0098   tn->SetVisOption(visopt) ;
0099   tn->SetVisLevel(level);
0100 
0101   // // ---- try to set transparency - does not seem to work ...
0102   // TGeoNode* node1 = gGeoManager->GetTopNode();
0103   // int k_nodes = node1->GetNdaughters();
0104   // for(int i=0; i<k_nodes; i++) {
0105   //   TGeoNode * CurrentNode = node1->GetDaughter(i);
0106   //   CurrentNode->GetMedium()->GetMaterial()->SetTransparency(80);
0107   //   // TEveGeoNode CurrentNode( node1->GetDaughter(i) ) ;
0108   //   // CurrentNode.SetMainTransparency( 80 ) ;
0109   // }
0110   // this code seems to have no effect either ...
0111   // tn->CSCApplyMainTransparencyToAllChildren() ;
0112   // tn->SetMainTransparency( 80 ) ;
0113 
0114 
0115   gEve->AddGlobalElement( tn );
0116 
0117   TEveElement* surfaces = getSurfaces(  kRed, SurfaceType( SurfaceType::Sensitive ), "SensitiveSurfaces" ) ;
0118   TEveElement* helperSurfaces = getSurfaces(  kGray, SurfaceType( SurfaceType::Helper ),"HelperSurfaces" ) ;
0119   TEveElement* surfaceVectors = getSurfaceVectors(1,0,0,1,"SurfaceVectorsN",kGreen) ;
0120 
0121   gEve->AddGlobalElement( surfaces ) ;
0122   gEve->AddGlobalElement( helperSurfaces ) ;
0123   gEve->AddGlobalElement( surfaceVectors ) ;
0124   
0125   
0126   TEveElement* surfaceVectors_u = getSurfaceVectors(0,1,0,0,"SurfaceVectorsU",kMagenta) ;
0127   
0128   gEve->AddGlobalElement( surfaceVectors_u ) ;
0129 
0130   TEveElement* surfaceVectors_v = getSurfaceVectors(0,0,1,0,"SurfaceVectorsV",kBlack) ;
0131   
0132   gEve->AddGlobalElement( surfaceVectors_v ) ;
0133 
0134   TGLViewer *v = gEve->GetDefaultGLViewer();
0135   // v->GetClipSet()->SetClipType(TGLClip::kClipPlane);
0136   //  v->ColorSet().Background().SetColor(kMagenta+4);
0137   v->ColorSet().Background().SetColor(kWhite);
0138   v->SetGuideState(TGLUtil::kAxesEdge, kTRUE, kFALSE, 0);
0139   v->RefreshPadEditor(v);
0140   //  v->CurrentCamera().RotateRad(-1.2, 0.5);
0141 
0142   gEve->GetGlobalScene()->GetGLScene()->SetSelectable(kFALSE) ; //change for debugging (kTRUE);
0143 
0144 
0145   MultiView::instance()->ImportGeomRPhi( surfaces );
0146   MultiView::instance()->ImportGeomRhoZ( surfaces ) ;
0147   MultiView::instance()->ImportGeomRPhi( helperSurfaces );
0148   MultiView::instance()->ImportGeomRhoZ( helperSurfaces ) ;
0149 
0150   make_gui();
0151   next_event();
0152   gEve->FullRedraw3D(kTRUE);
0153   return 1;
0154 }
0155 DECLARE_APPLY(DD4hepTEveDisplay,teve_display)
0156 
0157 //=====================================================================================================================
0158 int main(int argc,char** argv)  {
0159   std::vector<const char*> av;
0160   std::string level, visopt, opt;
0161   bool help = (argc == 1);
0162   
0163   for( int i=0; i < argc; ++i )   {
0164     if ( i == 1 && argv[i][0] != '-' ) av.emplace_back("-input");
0165     else if ( strncmp(argv[i],"-help",   4) == 0 ) help   = true;
0166     else if ( strncmp(argv[i],"--help",  5) == 0 ) help   = true;
0167     else if ( strncmp(argv[i],"-visopt", 4) == 0 ) visopt = argv[++i];
0168     else if ( strncmp(argv[i],"--visopt",5) == 0 ) visopt = argv[++i];
0169     else if ( strncmp(argv[i],"-level",  4) == 0 ) level  = argv[++i];
0170     else if ( strncmp(argv[i],"--level", 5) == 0 ) level  = argv[++i];
0171     else if ( strncmp(argv[i],"-option", 4) == 0 ) opt    = argv[++i];
0172     else if ( strncmp(argv[i],"--option",5) == 0 ) opt    = argv[++i];
0173     else if ( argc > 2 ) av.emplace_back(argv[i]);
0174   }
0175   if ( !help && argc == 2 )   { // Single argument given --> geometry file
0176     av.emplace_back("-input");
0177     av.emplace_back(argv[1]);
0178   }
0179   av.emplace_back("-interactive");
0180   av.emplace_back("-plugin");
0181   av.emplace_back("DD4hepTEveDisplay");
0182   if ( help              ) av.emplace_back("-help");
0183   if ( !opt.empty()      ) av.emplace_back("-opt"),      av.emplace_back(opt.c_str());
0184   if ( !level.empty()    ) av.emplace_back("-level"),    av.emplace_back(level.c_str());
0185   if ( !visopt.empty()   ) av.emplace_back("-visopt"),   av.emplace_back(visopt.c_str());
0186   if ( help )   {
0187     print_teve_help();
0188   }
0189   return dd4hep::execute::main_plugins("DD4hepTEveDisplay", av.size(), (char**)&av[0]);
0190 }
0191 
0192 //=====================================================================================================================
0193 TEveStraightLineSet* getSurfaceVectors(bool addO, bool addU, bool addV, bool addN, TString name,int color)  {
0194   TEveStraightLineSet* ls = new TEveStraightLineSet(name);
0195   Detector& description = Detector::getInstance();
0196   DetElement world = description.world();
0197 
0198   // create a list of all surfaces in the detector:
0199   SurfaceHelper surfMan( world );
0200   const SurfaceList& sL = surfMan.surfaceList() ;
0201   for( SurfaceList::const_iterator it = sL.begin() ; it != sL.end() ; ++it ){
0202     ISurface* surf = *it ;
0203     const Vector3D& u = surf->u() ;
0204     const Vector3D& v = surf->v() ;
0205     const Vector3D& n = surf->normal() ;
0206     const Vector3D& o = surf->origin() ;
0207 
0208     Vector3D ou = o + u ;
0209     Vector3D ov = o + v ;
0210     Vector3D on = o + n ;
0211  
0212     if (addU) ls->AddLine( o.x(), o.y(), o.z(), ou.x() , ou.y() , ou.z()  );
0213     
0214 //     TEveStraightLineSet::Marker_t *m = ls->AddMarker(id,1.);
0215     
0216     if (addV) ls->AddLine( o.x(), o.y(), o.z(), ov.x() , ov.y() , ov.z()  );
0217     if (addN) ls->AddLine( o.x(), o.y(), o.z(), on.x() , on.y() , on.z()  );
0218     if (addO) ls->AddMarker(  o.x(), o.y(), o.z() );
0219   }
0220   ls->SetLineColor( color ) ;
0221   ls->SetMarkerColor( kBlue ) ;
0222   ls->SetMarkerSize(1);
0223   ls->SetMarkerStyle(4);
0224   
0225   //  gEve->AddElement(ls);
0226 
0227   return ls;
0228 }
0229 //=====================================================================================
0230 TEveStraightLineSet* getSurfaces(int col, const SurfaceType& type, TString name) {
0231 
0232   TEveStraightLineSet* ls = new TEveStraightLineSet(name);
0233 
0234   Detector& description = Detector::getInstance();
0235 
0236   DetElement world = description.world() ;
0237 
0238   // create a list of all surfaces in the detector:
0239   SurfaceHelper surfMan(  world ) ;
0240 
0241   const SurfaceList& sL = surfMan.surfaceList() ;
0242 
0243 //    std::cout << " getSurfaces() for "<<name<<" - #surfaces : " << sL.size() << std::endl ;
0244 
0245   for( SurfaceList::const_iterator it = sL.begin() ; it != sL.end() ; ++it ){
0246 
0247     Surface* surf = dynamic_cast< Surface*> ( *it ) ;
0248 
0249     if( ! surf ) 
0250       continue ;
0251     
0252     if( ! ( surf->type().isVisible() && ( surf->type().isSimilar( type ) ) ) ) 
0253       continue ;
0254 
0255 //     std::cout << " **** drawSurfaces() : empty lines vector for surface " << *surf << std::endl ;
0256 
0257     const std::vector< std::pair<Vector3D,Vector3D> > lines = surf->getLines() ;
0258 
0259     if( lines.empty() )
0260       std::cout << " **** drawSurfaces() : empty lines vector for surface " << *surf << std::endl ;
0261 
0262     unsigned nL = lines.size() ;
0263 
0264     for( unsigned i=0 ; i<nL ; ++i){
0265 
0266       //      std::cout << " **** drawSurfaces() : draw line for surface " <<   lines[i].first << " - " <<  lines[i].second  << std::endl ;
0267 
0268       ls->AddLine( lines[i].first.x(),  lines[i].first.y(),  lines[i].first.z(), 
0269                    lines[i].second.x(), lines[i].second.y(), lines[i].second.z() ) ;
0270     }
0271     
0272     ls->SetLineColor( col ) ;
0273     ls->SetMarkerColor( col ) ;
0274     ls->SetMarkerSize(.1);
0275     ls->SetMarkerStyle(4);
0276 
0277   }
0278 
0279   return ls;
0280 }
0281 
0282 //=====================================================================================
0283 
0284 void make_gui() {
0285 
0286   // Create minimal GUI for event navigation.
0287 
0288   TEveBrowser* browser = gEve->GetBrowser();
0289   browser->StartEmbedding(TRootBrowser::kLeft);
0290 
0291   TGMainFrame* frmMain = new TGMainFrame(gClient->GetRoot(), 1000, 600);
0292   frmMain->SetWindowName("dd4hep GUI");
0293   frmMain->SetCleanup(kDeepCleanup);
0294 
0295   TGHorizontalFrame* hf = new TGHorizontalFrame(frmMain);
0296   {
0297     TString icondir( Form("%s/", TROOT::GetIconPath().Data()) );
0298     TGPictureButton* b = 0;
0299     EvNavHandler    *fh = new EvNavHandler;
0300 
0301     b = new TGPictureButton(hf, gClient->GetPicture(icondir+"GoBack.gif"));
0302     b->SetEnabled(kFALSE);
0303     b->SetToolTipText("Go to previous event - not supported.");
0304     hf->AddFrame(b);
0305     b->Connect("Clicked()", "EvNavHandler", fh, "Bck()");
0306 
0307     b = new TGPictureButton(hf, gClient->GetPicture(icondir+"GoForward.gif"));
0308     b->SetToolTipText("Generate new event.");
0309     hf->AddFrame(b);
0310     b->Connect("Clicked()", "EvNavHandler", fh, "Fwd()");
0311   }
0312   frmMain->AddFrame(hf);
0313 
0314   frmMain->MapSubwindows();
0315   frmMain->Resize();
0316   frmMain->MapWindow();
0317 
0318   browser->StopEmbedding();
0319   browser->SetTabTitle("Event Control", 0);
0320 }
0321 //=====================================================================================
0322