Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:22:31

0001 //
0002 // A collection of Eve macros for geometry visualization.
0003 // Load with .L EveMacros.C+
0004 // For usage, cf:
0005 // https://twiki.cern.ch/twiki/bin/view/CMSPublic/SWGuideMagneticField#Visualization_of_the_field_map_i
0006 //
0007 
0008 #include "TEveManager.h"
0009 #include "TEveViewer.h"
0010 #include "TEveScene.h"
0011 #include "TEveGeoNode.h"
0012 #include "TEveTrans.h"
0013 
0014 #include "TGLUtil.h"
0015 #include "TGLViewer.h"
0016 #include "TGLCamera.h"
0017 #include "TGLClip.h"
0018 
0019 #include "TGeoManager.h"
0020 #include "TGeoNode.h"
0021 #include "TGeoVolume.h"
0022 #include "TGeoShape.h"
0023 #include "TGeoMaterial.h"
0024 #include "TGeoMedium.h"
0025 #include "TGeoMatrix.h"
0026 #include "TGLSAViewer.h"
0027 
0028 #include "TGListTree.h"
0029 
0030 #include "TPRegexp.h"
0031 
0032 #include <iostream>
0033 
0034 //==============================================================================
0035 // Creation, initialization
0036 //==============================================================================
0037 
0038 //initialization
0039 void macros(){
0040   ((TGLSAViewer*)gEve->GetDefaultGLViewer())->DisableMenuBarHiding();
0041 }
0042 
0043 // void std_init()
0044 // {
0045 //    TEveManager::Create();
0046 //    gGeoManager = gEve->GetGeometry("cmsSimGeom-14.root");
0047 //    gGeoManager->DefaultColors();
0048 // }
0049 
0050 // TEveGeoTopNode* make_node(const TString& path, Int_t vis_level, Bool_t global_cs)
0051 // {
0052 //   if (! gGeoManager->cd(path))
0053 //   {
0054 //     Warning("make_node", "Path '%s' not found.", path.Data());
0055 //     return 0;
0056 //   }
0057 
0058 //   TEveGeoTopNode* tn = new TEveGeoTopNode(gGeoManager, gGeoManager->GetCurrentNode());
0059 //   tn->SetVisLevel(vis_level);
0060 //   if (global_cs)
0061 //   {
0062 //     tn->RefMainTrans().SetFrom(*gGeoManager->GetCurrentMatrix());
0063 //   }
0064 //   gEve->AddGlobalElement(tn);
0065 
0066 //   return tn;
0067 // }
0068 
0069 
0070 // void std_camera_clip()
0071 // {
0072 //    // EClipType not exported to CINT (see TGLUtil.h):
0073 //    // 0 - no clip, 1 - clip plane, 2 - clip box
0074 
0075 //    TGLViewer *v = gEve->GetDefaultGLViewer();
0076 //    v->GetClipSet()->SetClipType((EClipType) 1);
0077 //    v->SetGuideState(TGLUtil::kAxesEdge, kTRUE, kFALSE, 0);
0078 //    v->RefreshPadEditor(v);
0079 
0080 //    v->CurrentCamera().RotateRad(-1.2, 0.5);
0081 //    v->DoDraw();
0082 // }
0083 
0084 
0085 //==============================================================================
0086 // EVE helpers
0087 //==============================================================================
0088 
0089 void update_evegeonodes(TEveElement* el, Bool_t top)
0090 {
0091    TEveGeoNode *en = dynamic_cast<TEveGeoNode*>(el);
0092 
0093    if (en && !top)
0094    {
0095       TGeoNode *n = en->GetNode();
0096       en->SetRnrSelfChildren(n->IsVisible(), n->IsVisDaughters());
0097       en->SetMainColor(n->GetVolume()->GetLineColor());
0098       en->SetMainTransparency(n->GetVolume()->GetTransparency());
0099    }
0100 
0101    for (TEveElement::List_i i = el->BeginChildren(); i != el->EndChildren(); ++i)
0102    {
0103       update_evegeonodes(*i, kFALSE);
0104    }
0105 }
0106 
0107 void full_update()
0108 {
0109    TEveScene *gs = gEve->GetGlobalScene();
0110    for (TEveElement::List_i i = gs->BeginChildren(); i != gs->EndChildren(); ++i)
0111    {
0112       update_evegeonodes(*i, kTRUE);
0113    }
0114    gEve->FullRedraw3D();
0115    gEve->GetListTree()->ClearViewPort();
0116 }
0117 
0118 
0119 //==============================================================================
0120 // Global node / volume toggles
0121 //==============================================================================
0122 
0123 // Toggle everything on/off
0124 void visibility_all_volumes(Bool_t vis_state)
0125 {
0126    TGeoVolume *v;
0127    TIter it(gGeoManager->GetListOfVolumes());
0128    while ((v = (TGeoVolume*) it()) != 0)
0129    {
0130       v->SetVisibility(vis_state);
0131    }
0132    full_update();
0133 }
0134 
0135 // Toggle a specific volume on or off. Volume names are typically:
0136 // MagneticFieldVolumes_1103l:V_1001_1
0137 // FIXME: after running this command, you must click on the toggle box of
0138 // cms::World_1 in the Eve panel. There is no known workaround for this in the macro.
0139 void visibility_all_nodes(Bool_t vis_state, const char* nameFilter=".*")
0140 {
0141 
0142   TPMERegexp re(nameFilter, "o");
0143 
0144    TGeoVolume *v;
0145    TIter it(gGeoManager->GetListOfVolumes());
0146    while ((v = (TGeoVolume*) it()) != 0)
0147    {
0148       TGeoNode *n;
0149       TIter it2(v->GetNodes());
0150       while ((n = (TGeoNode*) it2()) != 0)
0151       {
0152     TString name(n->GetName());
0153     if (name!="cms:World_1"&&name!="cmsMagneticField:MAGF_1" && re.Match(name)) {
0154       std::cout << name << std::endl;
0155       n->SetVisibility(vis_state);
0156      //  v->SetVisLeaves(vis_state);
0157      //v->SetVisContainers(vis_state);
0158     } else {
0159       //      std::cout << name << std::endl;
0160     }
0161       }
0162    }
0163    full_update();
0164 }
0165 
0166 
0167 //==============================================================================
0168 // Utilities by material type
0169 //==============================================================================
0170 
0171 // List materials:
0172 //   gGeoManager->GetListOfMaterials()->ls()
0173 //   gGeoManager->GetListOfMaterials()->ls("*Si*")
0174 //
0175 // Print materials:
0176 //   gGeoManager->GetListOfMaterials()->Print()
0177 //   gGeoManager->GetListOfMaterials()->Print("", "*Si*")
0178 
0179 void set_volume_color_by_material(const char* material_re, Color_t color, Char_t transparency=-1)
0180 {
0181    // Note: material_re is a perl regexp!
0182    // If you want exact match, enclose in begin / end meta characters (^ / $):
0183    //   set_volume_color_by_material("^materials:Silicon$", kRed);
0184 
0185    TPMERegexp re(material_re, "o");
0186    TGeoMaterial *m;
0187    TIter it(gGeoManager->GetListOfMaterials());
0188    while ((m = (TGeoMaterial*) it()) != 0)
0189    {
0190       if (re.Match(m->GetName()))
0191       {
0192          if (transparency != -1)
0193          {
0194             m->SetTransparency(transparency);
0195          }
0196          TGeoVolume *v;
0197          TIter it2(gGeoManager->GetListOfVolumes());
0198          while ((v = (TGeoVolume*) it2()) != 0)
0199          {
0200             if (v->GetMaterial() == m)
0201             {
0202                v->SetLineColor(color);
0203             }
0204          }
0205       }
0206    }
0207    full_update();
0208 }
0209 
0210 void visibility_volume_by_material(const char* material_re, Bool_t vis_state)
0211 {
0212    TPMERegexp re(material_re, "o");
0213    TGeoMaterial *m;
0214    TIter it(gGeoManager->GetListOfMaterials());
0215    while ((m = (TGeoMaterial*) it()) != 0)
0216    {
0217       if (re.Match(m->GetName()))
0218       {
0219          TGeoVolume *v;
0220          TIter it2(gGeoManager->GetListOfVolumes());
0221          while ((v = (TGeoVolume*) it2()) != 0)
0222          {
0223             if (v->GetMaterial() == m)
0224             {
0225                v->SetVisibility(vis_state);
0226             }
0227          }
0228       }
0229    }
0230    full_update();
0231 }
0232 
0233 
0234 //==============================================================================
0235 // A collection of predefined views.
0236 // clip=true clips the geometry in a thin section order to draw boundaries on the specified plane.
0237 //==============================================================================
0238 
0239 float GetZoom() //FIXME: works only in ORTOGRAPHIC view
0240 {
0241   TGLViewer* v = gEve->GetDefaultGLViewer();
0242   TGLOrthoCamera& c = (TGLOrthoCamera& )v->CurrentCamera(); //FIXME should dynamic_cast
0243   printf("zoom %f \n", c.GetZoom());
0244   return c.GetZoom();
0245 }
0246 
0247 void SetZoom(float zoom) //FIXME: works only in ORTOGRAPHIC view
0248 {
0249   TGLViewer* v = gEve->GetDefaultGLViewer();
0250   TGLOrthoCamera& c = (TGLOrthoCamera& )v->CurrentCamera(); //FIXME should dynamic_cast
0251   c.SetZoom(zoom);
0252   c.IncTimeStamp();
0253   v->RequestDraw();
0254 }
0255 
0256 
0257 
0258 void goTo(TString view, bool clip) {
0259 
0260   TGLViewer* v   = gEve->GetDefaultGLViewer();
0261   TGLClipSet* cs = v->GetClipSet();
0262   
0263 
0264   if (view=="ZY"){
0265     v->SetCurrentCamera(TGLViewer::kCameraOrthoZOY);
0266 //     gEve->FullRedraw3D(kTRUE);
0267 //     v->CurrentCamera().RotateRad(0,TMath::Pi()/2.);
0268 //     v->DoDraw();
0269 
0270     if (clip) {
0271       Double_t clipBox[6] = {0,0,0, 3,1800,4000};
0272       cs->SetClipType(TGLClip::kClipBox);
0273       v->RefreshPadEditor(v);
0274       cs->GetCurrentClip()->SetMode(TGLClip::kOutside);
0275       cs->SetClipState(TGLClip::kClipBox, clipBox);
0276       v->RefreshPadEditor(v);
0277       v->SetStyle(TGLRnrCtx::kOutline);
0278     }
0279   }
0280 
0281   else if (view=="XY"){
0282     v->SetCurrentCamera(TGLViewer::kCameraOrthoXOY);
0283 
0284     if (clip) {
0285       Double_t clipBox[6] = {0,0,0, 1800,1800,3};
0286       cs->SetClipType(TGLClip::kClipBox);
0287       v->RefreshPadEditor(v);
0288       cs->GetCurrentClip()->SetMode(TGLClip::kOutside);
0289       cs->SetClipState(TGLClip::kClipBox, clipBox);
0290       v->RefreshPadEditor(v);
0291       v->SetStyle(TGLRnrCtx::kOutline);
0292     }
0293     
0294   }
0295 
0296   gEve->FullRedraw3D(kTRUE);
0297   //  v->DoDraw();
0298 }
0299 
0300 
0301 
0302 
0303 void ZYView(float zoom=-999, bool clip=true){
0304 
0305   TGLViewer* v   = gEve->GetDefaultGLViewer();
0306   v->SetCurrentCamera(TGLViewer::kCameraOrthoZOY);
0307 
0308   if (clip) {
0309     TGLClipSet* cs = v->GetClipSet();
0310     Double_t clipBox[6] = {0,0,0, 3,1800,4000};
0311     cs->SetClipType(TGLClip::kClipBox);
0312     v->RefreshPadEditor(v);
0313     cs->GetCurrentClip()->SetMode(TGLClip::kOutside);
0314     cs->SetClipState(TGLClip::kClipBox, clipBox);
0315     v->RefreshPadEditor(v);
0316     v->SetStyle(TGLRnrCtx::kOutline);
0317   }
0318   gEve->FullRedraw3D(kTRUE);
0319 
0320   if (zoom>0) SetZoom(zoom);
0321 
0322 }
0323 
0324 
0325 void XYView(float Z=0, bool clip=true){
0326   TGLViewer* v   = gEve->GetDefaultGLViewer();
0327   v->SetCurrentCamera(TGLViewer::kCameraOrthoXOY);
0328 
0329   if (clip) {
0330     TGLClipSet* cs = v->GetClipSet();
0331     Double_t clipBox[6] = {0,0,Z, 1800,1800,3};
0332     cs->SetClipType(TGLClip::kClipBox);
0333     v->RefreshPadEditor(v);
0334     cs->GetCurrentClip()->SetMode(TGLClip::kOutside);
0335     cs->SetClipState(TGLClip::kClipBox, clipBox);
0336     v->RefreshPadEditor(v);
0337     v->SetStyle(TGLRnrCtx::kOutline);
0338   }
0339   gEve->FullRedraw3D(kTRUE);    
0340 }
0341 
0342   
0343 void print(TString filename) {
0344   gEve->GetDefaultGLViewer()->SavePicture(filename);
0345 }
0346 
0347 
0348 
0349 //  Double_t center[3] = {0,0,0};
0350 //    v->SetOrthoCamera(TGLViewer::kCameraOrthoXOY, 0.5, 1500, center, 0,TMath::Pi()/2.);
0351 //    v->ResetCurrentCamera();
0352 //   v->SetGuideState(TGLUtil::kAxesEdge, kTRUE, kFALSE, 0)
0353 
0354