Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-16 09:22:22

0001 //==============================================================================
0002 // TGeoColorScheme.h
0003 //==============================================================================
0004 
0005 #ifndef ROOT_TGeoColorScheme
0006 #define ROOT_TGeoColorScheme
0007 
0008 #include "Rtypes.h"
0009 #include <functional>
0010 
0011 class TGeoVolume;
0012 class TGeoMaterial;
0013 
0014 /**
0015  * @enum EGeoColorSet
0016  * @brief Enumeration of predefined geometry color schemes.
0017  *
0018  * These values select the built-in default coloring policy used by
0019  * TGeoColorScheme when assigning colors to geometry volumes.
0020  *
0021  * @see TGeoColorScheme
0022  */
0023 enum class EGeoColorSet {
0024    kNatural = 0, ///< Natural, material-inspired colors (default)
0025    kFlashy,      ///< Bright, high-contrast colors for presentations
0026    kHighContrast ///< Dark, saturated colors for light backgrounds
0027 };
0028 
0029 /**
0030  * @class TGeoColorScheme
0031  * @brief Strategy object for assigning colors and transparency to geometry volumes.
0032  *
0033  * This class is used by TGeoManager::DefaultColors(const TGeoColorScheme*)
0034  * to assign visualization colors and transparency to geometry volumes,
0035  * typically after GDML import where no color information is stored.
0036  *
0037  * The default implementation combines:
0038  *  - name-based material classification (e.g. metals, polymers, gases),
0039  *  - a Z-binned fallback lookup when no name-based rule applies.
0040  *
0041  * This class is intended for runtime use only (not persistified).
0042  * Users can extend or override the default behavior by:
0043  *  - installing hooks via std::function, or
0044  *  - subclassing and overriding virtual methods.
0045  *
0046  * @see TGeoManager::DefaultColors
0047  */
0048 class TGeoColorScheme {
0049 public:
0050    /// Type of user hook for overriding color assignment.
0051    using ColorHook_t = std::function<Int_t(const TGeoVolume *)>;
0052 
0053    /// Type of user hook for overriding transparency assignment.
0054    using TranspHook_t = std::function<Int_t(const TGeoVolume *)>;
0055 
0056    /// Type of user hook for overriding the Z-based fallback coloring.
0057    using ZFallbackHook_t = std::function<Int_t(Int_t /*Z*/, EGeoColorSet /*set*/)>;
0058 
0059    /**
0060     * @brief Constructor.
0061     *
0062     * \param set  Initial color set selection (natural, flashy, high-contrast).
0063     */
0064    explicit TGeoColorScheme(EGeoColorSet set = EGeoColorSet::kNatural);
0065 
0066    /// @brief Virtual destructor.
0067    virtual ~TGeoColorScheme();
0068 
0069    /**
0070     * @brief Compute the color for a given volume.
0071     *
0072     * This method is called by TGeoManager::DefaultColors() for each volume.
0073     * The default implementation:
0074     *  - calls the user color hook if installed,
0075     *  - applies name-based material overrides,
0076     *  - falls back to ColorForZ() if no rule matches.
0077     *
0078     * @param vol  Geometry volume to be colored.
0079     * @return     ROOT color index (>=0) to apply, or <0 for "no decision".
0080     *
0081     * @see Transparency
0082     * @see ColorForZ
0083     * @see TGeoManager::DefaultColors
0084     */
0085    virtual Int_t Color(const TGeoVolume *vol) const;
0086 
0087    /**
0088     * @brief Compute the transparency for a given volume.
0089     *
0090     * The default implementation:
0091     *  - calls the user transparency hook if installed,
0092     *  - makes very low-density materials (e.g. gases) semi-transparent.
0093     *
0094     * @param vol  Geometry volume.
0095     * @return     Transparency value in [0..100] to apply, or <0 to leave unchanged.
0096     *
0097     * @see Color
0098     * @see TGeoManager::DefaultColors
0099     */
0100    virtual Int_t Transparency(const TGeoVolume *vol) const;
0101 
0102    /**
0103     * @brief Compute fallback color based on material effective Z.
0104     *
0105     * This method is used when no name-based material override applies.
0106     * Users may override this behavior via SetZFallbackHook() or by
0107     * subclassing and overriding this method.
0108     *
0109     * @param Z    Effective atomic number of the material.
0110     * @param set  Active color set selection.
0111     * @return     ROOT color index (>=0) to apply.
0112     *
0113     * @see SetZFallbackHook
0114     */
0115    virtual Int_t ColorForZ(Int_t Z, EGeoColorSet set) const;
0116 
0117    /**
0118     * @brief Set a user hook for color assignment.
0119     *
0120     * The hook is called before any built-in logic.
0121     * Returning a value <0 delegates the decision to the default implementation.
0122     *
0123     * @param h  Color hook (set to nullptr to disable).
0124     *
0125     * @see Color
0126     */
0127    void SetColorHook(ColorHook_t h) { fColorHook = std::move(h); }
0128 
0129    /**
0130     * @brief Set a user hook for transparency assignment.
0131     *
0132     * The hook is called before the default transparency logic.
0133     * Returning a value <0 delegates the decision to the default implementation.
0134     *
0135     * @param h  Transparency hook (set to nullptr to disable).
0136     *
0137     * @see Transparency
0138     */
0139    void SetTransparencyHook(TranspHook_t h) { fTranspHook = std::move(h); }
0140 
0141    /**
0142     * @brief Set a user hook for Z-based fallback coloring.
0143     *
0144     * The hook is called before the built-in Z-binned lookup.
0145     * Returning a value <0 delegates the decision to the default implementation.
0146     *
0147     * @param h  Z-fallback hook (set to nullptr to disable).
0148     *
0149     * @see ColorForZ
0150     */
0151    void SetZFallbackHook(ZFallbackHook_t h) { fZFallbackHook = std::move(h); }
0152 
0153    /// @brief Get the active color set.
0154    EGeoColorSet GetSet() const { return fSet; }
0155 
0156    /// @brief Set the active color set.
0157    void SetSet(EGeoColorSet s) { fSet = s; }
0158 
0159    /**
0160     * @brief Retrieve the material associated with a geometry volume.
0161     *
0162     * This helper performs all necessary pointer checks and may be safely
0163     * used inside user hooks.
0164     *
0165     * @param vol  Geometry volume.
0166     * @return     Pointer to the associated material, or nullptr if unavailable.
0167     */
0168    static const TGeoMaterial *GetMaterial(const TGeoVolume *vol);
0169 
0170 private:
0171    EGeoColorSet fSet; ///< Active color set selection
0172 
0173    ColorHook_t fColorHook = nullptr;         ///< Optional user hook for color assignment
0174    TranspHook_t fTranspHook = nullptr;       ///< Optional user hook for transparency
0175    ZFallbackHook_t fZFallbackHook = nullptr; ///< Optional user hook for Z fallback
0176 };
0177 
0178 #endif // ROOT_TGeoColorScheme