Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TGeoColorScheme Class Reference

Strategy object for assigning colors and transparency to geometry volumes.

Strategy class for assigning colors/transparency to geometry volumes.

This class is used by TGeoManager::DefaultColors(const TGeoColorScheme*) to assign visualization colors and transparency to geometry volumes, typically after GDML import where no color information is stored.

The default implementation combines:

  • name-based material classification (e.g. metals, polymers, gases),
  • a Z-binned fallback lookup when no name-based rule applies.

This class is intended for runtime use only (not persistified). Users can extend or override the default behavior by:

  • installing hooks via std::function, or
  • subclassing and overriding virtual methods.
See also
TGeoManager::DefaultColors

Helper "strategy" class used by TGeoManager::DefaultColors() to assign visualization attributes (line color and optional transparency) to imported geometries (e.g. GDML), where the source format does not encode colors.

Design goals
  • Backward compatibility: calling TGeoManager::DefaultColors() with no arguments behaves like today (default "natural" scheme).
  • Reasonable defaults for detector geometries: first try name-based material classification (e.g. copper, aluminium, steel, FR4/G10, kapton/polyimide, gases), then fall back to a Z-binned lookup.
  • User extensibility without requiring subclassing: users can provide hooks (std::function) that override the default decisions partially or fully. Hooks are runtime-only (not persistified).
How TGeoManager::DefaultColors() uses a scheme
For each volume:
  • scheme->Color(vol) is queried:
  • return >= 0 : use returned ROOT color index (TColor index)
  • return < 0 : caller may ignore or use its own fallback
  • scheme->Transparency(vol) is queried:
  • return in [0..100] : set volume transparency
  • return < 0 : do not change transparency
Default behavior
  • Color(vol):
  • If a user color hook is installed, it is called first. If it returns >= 0, that color is used.
  • Otherwise, try name-based overrides (tokens like "copper", "steel", "fr4", "g10", "kapton", "argon", "c6f14", ...).
  • If still unresolved, fall back to ColorForZ(effectiveZ). Users can override the Z fallback via SetZFallbackHook() or by subclassing and overriding ColorForZ().
  • Transparency(vol):
  • If a user transparency hook is installed and returns >= 0, use it.
  • Otherwise, the default makes very low-density materials (gases) semi-transparent (60), preserving historical behavior.
User extensibility examples
Example A: Tweak just copper-like materials (leave everything else default)
cs.SetColorHook([](const TGeoVolume* v) -> Int_t {
if (!m || !m->GetName()) return -1;
std::string n = m->GetName();
std::transform(n.begin(), n.end(), n.begin(), ::tolower);
if (n.find("copper") != std::string::npos || n.find("_cu") != std::string::npos)
return TColor::GetColor(0.90f, 0.55f, 0.30f); // custom copper tint
return -1; // let defaults handle the rest
});
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
@ kNatural
Natural, material-inspired colors (default)
R__EXTERN TGeoManager * gGeoManager
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
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 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
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

Example B: Override Z-fallback mapping (keep name overrides)

cs.SetZFallbackHook([](Int_t Z, EGeoColorSet) -> Int_t {
float t = std::min(1.f, std::max(0.f, Z / 100.f));
return TColor::GetColor(t, t, t); // grayscale by Z
});
EGeoColorSet
Enumeration of predefined geometry color schemes.

Example C: Full custom policy via subclassing (optional)

class MyScheme : public TGeoColorScheme {
public:
Int_t ColorForZ(Int_t Z, EGeoColorSet set) const override { ... }
Int_t Color(const TGeoVolume* vol) const override { ... }
};
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:59
@ kFlashy
Bright, high-contrast colors for presentations.
TGeoColorScheme(EGeoColorSet set=EGeoColorSet::kNatural)
Constructor.
Notes
  • This class is intended for runtime use; it is not persistified because hooks are user-provided callables which are not I/O friendly.
  • The default name token rules are heuristic and aimed at typical HEP detector material naming conventions; users can refine them locally via hooks or subclassing.
See also
TGeoManager::DefaultColors

Definition at line 48 of file TGeoColorScheme.h.

Public Types

using ColorHook_t = std::function<Int_t(const TGeoVolume *)>
 Type of user hook for overriding color assignment.
 
using TranspHook_t = std::function<Int_t(const TGeoVolume *)>
 Type of user hook for overriding transparency assignment.
 
using ZFallbackHook_t = std::function<Int_t(Int_t , EGeoColorSet )>
 Type of user hook for overriding the Z-based fallback coloring.
 

Public Member Functions

 TGeoColorScheme (EGeoColorSet set=EGeoColorSet::kNatural)
 Constructor.
 
virtual ~TGeoColorScheme ()
 Virtual destructor.
 
virtual Int_t Color (const TGeoVolume *vol) const
 Compute the color for a given volume.
 
virtual Int_t ColorForZ (Int_t Z, EGeoColorSet set) const
 Compute fallback color based on material effective Z.
 
EGeoColorSet GetSet () const
 Get the active color set.
 
void SetColorHook (ColorHook_t h)
 Set a user hook for color assignment.
 
void SetSet (EGeoColorSet s)
 Set the active color set.
 
void SetTransparencyHook (TranspHook_t h)
 Set a user hook for transparency assignment.
 
void SetZFallbackHook (ZFallbackHook_t h)
 Set a user hook for Z-based fallback coloring.
 
virtual Int_t Transparency (const TGeoVolume *vol) const
 Compute the transparency for a given volume.
 

Static Public Member Functions

static const TGeoMaterialGetMaterial (const TGeoVolume *vol)
 Retrieve the material associated with a geometry volume.
 

Private Attributes

ColorHook_t fColorHook = nullptr
 Optional user hook for color assignment.
 
EGeoColorSet fSet
 Active color set selection.
 
TranspHook_t fTranspHook = nullptr
 Optional user hook for transparency.
 
ZFallbackHook_t fZFallbackHook = nullptr
 Optional user hook for Z fallback.
 

#include <TGeoColorScheme.h>

Member Typedef Documentation

◆ ColorHook_t

Type of user hook for overriding color assignment.

Definition at line 51 of file TGeoColorScheme.h.

◆ TranspHook_t

Type of user hook for overriding transparency assignment.

Definition at line 54 of file TGeoColorScheme.h.

◆ ZFallbackHook_t

Type of user hook for overriding the Z-based fallback coloring.

Definition at line 57 of file TGeoColorScheme.h.

Constructor & Destructor Documentation

◆ TGeoColorScheme()

TGeoColorScheme::TGeoColorScheme ( EGeoColorSet set = EGeoColorSet::kNatural)
explicit

Constructor.

Parameters
setInitial color set selection (natural, flashy, high-contrast).

Definition at line 358 of file TGeoColorScheme.cxx.

◆ ~TGeoColorScheme()

TGeoColorScheme::~TGeoColorScheme ( )
virtualdefault

Virtual destructor.

Member Function Documentation

◆ Color()

Int_t TGeoColorScheme::Color ( const TGeoVolume * vol) const
virtual

Compute the color for a given volume.

This method is called by TGeoManager::DefaultColors() for each volume. The default implementation:

  • calls the user color hook if installed,
  • applies name-based material overrides,
  • falls back to ColorForZ() if no rule matches.
Parameters
volGeometry volume to be colored.
Returns
ROOT color index (>=0) to apply, or <0 for "no decision".
See also
Transparency
ColorForZ
TGeoManager::DefaultColors

Definition at line 372 of file TGeoColorScheme.cxx.

◆ ColorForZ()

Int_t TGeoColorScheme::ColorForZ ( Int_t Z,
EGeoColorSet set ) const
virtual

Compute fallback color based on material effective Z.

This method is used when no name-based material override applies. Users may override this behavior via SetZFallbackHook() or by subclassing and overriding this method.

Parameters
ZEffective atomic number of the material.
setActive color set selection.
Returns
ROOT color index (>=0) to apply.
See also
SetZFallbackHook

Definition at line 420 of file TGeoColorScheme.cxx.

◆ GetMaterial()

const TGeoMaterial * TGeoColorScheme::GetMaterial ( const TGeoVolume * vol)
static

Retrieve the material associated with a geometry volume.

This helper performs all necessary pointer checks and may be safely used inside user hooks.

Parameters
volGeometry volume.
Returns
Pointer to the associated material, or nullptr if unavailable.

Definition at line 362 of file TGeoColorScheme.cxx.

◆ GetSet()

EGeoColorSet TGeoColorScheme::GetSet ( ) const
inline

Get the active color set.

Definition at line 154 of file TGeoColorScheme.h.

◆ SetColorHook()

void TGeoColorScheme::SetColorHook ( ColorHook_t h)
inline

Set a user hook for color assignment.

The hook is called before any built-in logic. Returning a value <0 delegates the decision to the default implementation.

Parameters
hColor hook (set to nullptr to disable).
See also
Color

Definition at line 127 of file TGeoColorScheme.h.

◆ SetSet()

void TGeoColorScheme::SetSet ( EGeoColorSet s)
inline

Set the active color set.

Definition at line 157 of file TGeoColorScheme.h.

◆ SetTransparencyHook()

void TGeoColorScheme::SetTransparencyHook ( TranspHook_t h)
inline

Set a user hook for transparency assignment.

The hook is called before the default transparency logic. Returning a value <0 delegates the decision to the default implementation.

Parameters
hTransparency hook (set to nullptr to disable).
See also
Transparency

Definition at line 139 of file TGeoColorScheme.h.

◆ SetZFallbackHook()

void TGeoColorScheme::SetZFallbackHook ( ZFallbackHook_t h)
inline

Set a user hook for Z-based fallback coloring.

The hook is called before the built-in Z-binned lookup. Returning a value <0 delegates the decision to the default implementation.

Parameters
hZ-fallback hook (set to nullptr to disable).
See also
ColorForZ

Definition at line 151 of file TGeoColorScheme.h.

◆ Transparency()

Int_t TGeoColorScheme::Transparency ( const TGeoVolume * vol) const
virtual

Compute the transparency for a given volume.

The default implementation:

  • calls the user transparency hook if installed,
  • makes very low-density materials (e.g. gases) semi-transparent.
Parameters
volGeometry volume.
Returns
Transparency value in [0..100] to apply, or <0 to leave unchanged.
See also
Color
TGeoManager::DefaultColors

Definition at line 398 of file TGeoColorScheme.cxx.

Member Data Documentation

◆ fColorHook

ColorHook_t TGeoColorScheme::fColorHook = nullptr
private

Optional user hook for color assignment.

Definition at line 173 of file TGeoColorScheme.h.

◆ fSet

EGeoColorSet TGeoColorScheme::fSet
private

Active color set selection.

Definition at line 171 of file TGeoColorScheme.h.

◆ fTranspHook

TranspHook_t TGeoColorScheme::fTranspHook = nullptr
private

Optional user hook for transparency.

Definition at line 174 of file TGeoColorScheme.h.

◆ fZFallbackHook

ZFallbackHook_t TGeoColorScheme::fZFallbackHook = nullptr
private

Optional user hook for Z fallback.

Definition at line 175 of file TGeoColorScheme.h.

Libraries for TGeoColorScheme:

The documentation for this class was generated from the following files: