Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
geomColors.C
Go to the documentation of this file.
1/**
2 * \file geomColors.C
3 * \ingroup tutorial_geom
4 * Script demonstrating geometry color schemes.
5 *
6 *
7 * \macro_code
8 *
9 * \author andrei.gheata@cern.ch
10 */
11
12#include <string>
13#include <algorithm>
14
15#include "TROOT.h"
16#include "TColor.h"
17#include "TControlBar.h"
18#include "TGeoManager.h"
19#include "TGeoColorScheme.h"
20
23
24void geomAlice_itsv()
25{
26 TGeoManager::Import("http://root.cern/files/alice2.root");
28 gGeoManager->GetVolume("ITSV")->Draw("ogl");
29}
30
31void help()
32{
33 printf("In the viewer window:\n"
34 " - de-select \"Reset on update\"\n"
35 " - in the \"Clipping\" tab, select \"Plane\"\n"
36 " - rotate the image as uou wish and zoom using the mouse wheel\n"
37 " - click on the different default color schemes in control bar menu\n"
38 " - the \"gray\" function demonstrates overriding the Z-based fallback colors\n"
39 " - the \"override\" function demonstrates color override from a scheme by using a hook\n"
40 " - the \"transparency\" function demonstrates transparency override\n");
41}
42
43//______________________________________________________________________________
44void natural()
45{
46 // Predefined "natural" color scheme
47 if (!gGeoManager)
48 return;
52}
53
54//______________________________________________________________________________
55void flashy()
56{
57 // Predefined "flashy" color scheme
58 if (!gGeoManager)
59 return;
63}
64
65//______________________________________________________________________________
66void high_contrast()
67{
68 // Predefined "high-contrast" color scheme
69 if (!gGeoManager)
70 return;
74}
75
76//______________________________________________________________________________
77void gray()
78{
79 // Gray palette override of the Z-binned fallback
80 if (!gGeoManager)
81 return;
83 cs.SetZFallbackHook([](Int_t Z, EGeoColorSet) -> Int_t {
84 float g = std::min(1.f, Z / 100.f);
85 return TColor::GetColor(g, g, g);
86 });
88}
89
90//______________________________________________________________________________
91void transparent()
92{
93 // Transparency override for a color scheme
94 if (!gGeoManager)
95 return;
98 if (gTransparent) {
99 cs.SetTransparencyHook([](const TGeoVolume *v) -> Int_t {
101 if (!m)
102 return -1;
103
104 // Base glass-like transparency for "everything"
105 Int_t tr = 85; // 0=opaque, 100=fully transparent
106
107 // Make typical gases/fluids even more "invisible"
108 if (m->GetDensity() < 0.1)
109 tr = 95;
110
111 // Optional: slightly reduce transparency for cables/services so they remain visible
112 if (m->GetName()) {
113 std::string n = m->GetName();
114 std::transform(n.begin(), n.end(), n.begin(), [](unsigned char c) { return (char)std::tolower(c); });
115
116 if (n.find("cable") != std::string::npos || n.find("cables") != std::string::npos)
117 tr = 70;
118
119 // Optional: keep heavy metals a bit less transparent to preserve structure
120 if (n.find("tungsten") != std::string::npos || n.find("_w") != std::string::npos)
121 tr = 75;
122 }
123
124 return tr;
125 });
126 } else {
127 cs.SetTransparencyHook([](const TGeoVolume *v) -> Int_t { return 0; });
128 }
129
131}
132
133//______________________________________________________________________________
134void color_override()
135{
136 // Color override based on volume-related properties
137 if (!gGeoManager)
138 return;
140 cs.SetColorHook([](const TGeoVolume *v) -> Int_t {
142 if (!m || !m->GetName())
143 return -1;
144 if (std::string(m->GetName()).find("ITS_GEN") != std::string::npos)
145 return TColor::GetColor(0.9f, 0.55f, 0.3f);
146 return -1; // fallback to defaults
147 });
149}
150
151//______________________________________________________________________________
152void geomColors()
153{
154 // root[0] .x geomColors.C
155 //
156 // This opens a GL viewer showing the detector with the user-defined colors.
157 // In the viewer window:
158 // - de-select "Reset on update"
159 // - in the "Clipping" tab, select "Plane"
160 // - rotate the image as uou wish and zoom using the mouse wheel
161 // - click on the different default color schemes (natural, flashy, high-contrast) in control bar menu
162 // - the "Gray fallback" function demonstrates overriding the Z-based fallback colors
163 // - the "Color override" function demonstrates color override from a scheme by using a hook
164 // - the "Transparency" function demonstrates transparency override from a scheme using a hook.
165 // Transparency is attached to TGeoMaterial, so it affects all volumes sharing the same material
166
167 TControlBar *bar = new TControlBar("vertical", "Geometry color schemes", 10, 10);
168 bar->AddButton("How to run ", "help()", "Instructions for running this macro");
169 bar->AddButton("Natural ", "natural()",
170 "A natural color scheme with name-based/Z-binned lookup for material classification");
171 bar->AddButton("Flashy ", "flashy()", "Flashy, high-contrast, presentation-friendly colors");
172 bar->AddButton("High contrast ", "high_contrast()", "Darker, saturated colors suited for light backgrounds");
173 bar->AddButton("Gray fallback ", "gray()", "Demonstrates overriding the Z-based fallback colors");
174 bar->AddButton("Color override ", "color_override()", "Demonstrates color override from a scheme by using a hook");
175 bar->AddButton("Transparency ON/OFF ", "transparent()",
176 "Demonstrates transparency override from a scheme by using a hook");
177 bar->Show();
178 gROOT->SaveContext();
180}
#define c(i)
Definition RSha256.hxx:101
#define g(i)
Definition RSha256.hxx:105
bool Bool_t
Boolean (0=false, 1=true) (bool)
Definition RtypesCore.h:77
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:59
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
EGeoColorSet
Enumeration of predefined geometry color schemes.
@ kNatural
Natural, material-inspired colors (default)
@ kFlashy
Bright, high-contrast colors for presentations.
@ kHighContrast
Dark, saturated colors for light backgrounds.
R__EXTERN TGeoManager * gGeoManager
#define gROOT
Definition TROOT.h:414
const_iterator begin() const
const_iterator end() const
static Int_t GetColor(const char *hexcolor)
Static method returning color number for color specified by hex color string of form: "#rrggbb",...
Definition TColor.cxx:1926
A Control Bar is a fully user configurable tool which provides fast access to frequently used operati...
Definition TControlBar.h:26
void Show()
Show control bar.
void AddButton(TControlBarButton *button)
Add button.
Strategy object for assigning colors and transparency to geometry volumes.
static const TGeoMaterial * GetMaterial(const TGeoVolume *vol)
Retrieve the material associated with a geometry volume.
void SetVisLevel(Int_t level=3)
set default level down to which visualization is performed
static TGeoManager * Import(const char *filename, const char *name="", Option_t *option="")
static function Import a geometry from a gdml or ROOT file
TGeoVolume * GetVolume(const char *name) const
Search for a named volume. All trailing blanks stripped.
void DefaultColors(const TGeoColorScheme *cs=nullptr)
Set default volume colors according to A of material.
Base class describing materials.
TGeoVolume, TGeoVolumeMulti, TGeoVolumeAssembly are the volume classes.
Definition TGeoVolume.h:43
void Draw(Option_t *option="") override
draw top volume according to option
virtual const char * GetName() const
Returns name of object.
Definition TObject.cxx:457
const Int_t n
Definition legend1.C:16
TMarker m
Definition textangle.C:8