Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
REveUtil.cxx
Go to the documentation of this file.
1// @(#)root/eve7:$Id$
2// Authors: Matevz Tadel & Alja Mrak-Tadel: 2006, 2007
3
4/*************************************************************************
5 * Copyright (C) 1995-2019, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#include <ROOT/REveUtil.hxx>
13#include <ROOT/REveElement.hxx>
14#include <ROOT/REveManager.hxx>
15
16#include <ROOT/RLogger.hxx>
17
18#include "TError.h"
19#include "TGeoManager.h"
20#include "TGeoMatrix.h"
21#include "TClass.h"
22#include "TMath.h"
23
24#include "TColor.h"
25
26#include "TROOT.h"
27#include "TInterpreter.h"
28
29#include <list>
30#include <algorithm>
31#include <string>
32
33using namespace ROOT::Experimental;
34namespace REX = ROOT::Experimental;
35
36
37/** \class REveUtil
38\ingroup REve
39Standard utility functions for Eve.
40*/
41
42TObjArray *REveUtil::fgDefaultColors = nullptr;
43
44namespace
45{
46////////////////////////////////////////////////////////////////////////////////
47/// Remove last part of string 's', starting from the last
48/// occurrence of character 'c'.
49/// Remove directory part -- everything until the last '/'.
50
51void ChompTailAndDir(TString& s, char c='.')
52{
53 Ssiz_t p = s.Last(c);
54 if (p != kNPOS)
55 s.Remove(p);
56
57 Ssiz_t ls = s.Last('/');
58 if (ls != kNPOS)
59 s.Remove(0, ls + 1);
60}
61}
62
63////////////////////////////////////////////////////////////////////////////////
64/// Checks if macro 'mac' is loaded.
65
66Bool_t REveUtil::CheckMacro(const char* mac)
67{
68 // Axel's advice; now sth seems slow, using old method below for test.
69 // return gROOT->GetInterpreter()->IsLoaded(mac);
70
71 // Previous version expected function with same name and used ROOT's
72 // list of global functions.
73
74 TString foo(mac); ChompTailAndDir(foo);
75 if (gROOT->GetGlobalFunction(foo.Data(), 0, kFALSE) != 0)
76 return kTRUE;
77 else
78 return (gROOT->GetGlobalFunction(foo.Data(), 0, kTRUE) != 0);
79}
80
81////////////////////////////////////////////////////////////////////////////////
82/// Load and execute macro 'mac' if it has not been loaded yet.
83
84void REveUtil::AssertMacro(const char* mac)
85{
86 if( CheckMacro(mac) == kFALSE) {
87 gROOT->Macro(mac);
88 }
89}
90
91////////////////////////////////////////////////////////////////////////////////
92/// Execute macro 'mac'. Do not reload the macro.
93
94void REveUtil::Macro(const char* mac)
95{
96 if (CheckMacro(mac) == kFALSE) {
97 gROOT->LoadMacro(mac);
98 }
99 TString foo(mac); ChompTailAndDir(foo); foo += "()";
100 gROOT->ProcessLine(foo.Data());
101}
102
103////////////////////////////////////////////////////////////////////////////////
104/// Makes sure that macro 'mac' is loaded, but do not reload it.
105
106void REveUtil::LoadMacro(const char* mac)
107{
108 if (CheckMacro(mac) == kFALSE) {
109 gROOT->LoadMacro(mac);
110 }
111}
112
113////////////////////////////////////////////////////////////////////////////////
114/// Fill col with RGBA values corresponding to index ci. If alpha
115/// is true, set alpha component of col to 255.
116/// ROOT's indexed color palette does not support transparency.
117
119{
120 TColor* c = gROOT->GetColor(ci);
121 if (c)
122 {
123 col[0] = (UChar_t)(255*c->GetRed());
124 col[1] = (UChar_t)(255*c->GetGreen());
125 col[2] = (UChar_t)(255*c->GetBlue());
126 if (alpha) col[3] = 255;
127 }
128 else
129 {
130 // Set to magenta.
131 col[0] = 255; col[1] = 0; col[2] = 255;
132 if (alpha) col[3] = 255;
133 return;
134 }
135}
136
137////////////////////////////////////////////////////////////////////////////////
138/// Fill col with RGBA values corresponding to index ci and transparency.
139/// ROOT's indexed color palette does not support transparency.
140
141void REveUtil::ColorFromIdx(Color_t ci, UChar_t col[4], Char_t transparency)
142{
143 UChar_t alpha = (255*(100 - transparency))/100;
144
145 TColor* c = gROOT->GetColor(ci);
146 if (c)
147 {
148 col[0] = (UChar_t)(255*c->GetRed());
149 col[1] = (UChar_t)(255*c->GetGreen());
150 col[2] = (UChar_t)(255*c->GetBlue());
151 col[3] = alpha;
152 }
153 else
154 {
155 // Set to magenta.
156 col[0] = 255; col[1] = 0; col[2] = 255; col[3] = alpha;
157 return;
158 }
159}
160
161////////////////////////////////////////////////////////////////////////////////
162/// Fill col with weighted RGBA values corresponding to
163/// color-indices c1 and c2. If alpha is true, set alpha component
164/// of col to 255.
165
167 UChar_t col[4], Bool_t alpha)
168{
170 TColor* t2 = gROOT->GetColor(c2);
171 if(t1 && t2) {
172 col[0] = (UChar_t)(255*(f1*t1->GetRed() + f2*t2->GetRed()));
173 col[1] = (UChar_t)(255*(f1*t1->GetGreen() + f2*t2->GetGreen()));
174 col[2] = (UChar_t)(255*(f1*t1->GetBlue() + f2*t2->GetBlue()));
175 if (alpha) col[3] = 255;
176 }
177}
178
179////////////////////////////////////////////////////////////////////////////////
180/// Find address of Color_t data-member with name varname in object
181/// obj.
182///
183/// This is used to access color information for TGListTreeItem
184/// coloration from visualization macros that wrap TObjects into
185/// REveElementObjectPtr instances.
186
187Color_t* REveUtil::FindColorVar(TObject* obj, const char* varname)
188{
189 static const REveException eh("REveUtil::FindColorVar");
190
191 Int_t off = obj->IsA()->GetDataMemberOffset(varname);
192 if(off == 0)
193 throw(eh + "could not find member '" + varname + "' in class " + obj->IsA()->GetName() + ".");
194 return (Color_t*) (((char*)obj) + off);
195}
196
197////////////////////////////////////////////////////////////////////////////////
198/// Tweak all ROOT colors to become brighter (if value > 0) or
199/// darker (value < 0). Reasonable values for the value argument are
200/// from -2.5 to 2.5 (error will be printed otherwise).
201/// If value is zero, the original colors are restored.
202///
203/// You should call REveManager::FullRedraw3D() afterwards or set
204/// the argument full_redraw to true (default is false).
205
207{
208 if (value < -2.5 || value > 2.5)
209 {
210 Error("REveUtil::SetColorBrightness", "value '%f' out of range [-0.5, 0.5].", value);
211 return;
212 }
213
214 TObjArray *colors = (TObjArray*) gROOT->GetListOfColors();
215
216 if (fgDefaultColors == 0)
217 {
218 const Int_t n_col = colors->GetEntriesFast();
219 fgDefaultColors = new TObjArray(n_col);
220 for (Int_t i = 0; i < n_col; ++i)
221 {
222 TColor* c = (TColor*) colors->At(i);
223 if (c)
224 fgDefaultColors->AddAt(new TColor(*c), i);
225 }
226 }
227
228 const Int_t n_col = fgDefaultColors->GetEntriesFast();
229 for (Int_t i = 0; i < n_col; ++i)
230 {
231 TColor* cdef = (TColor*) fgDefaultColors->At(i);
232 if (cdef)
233 {
234 TColor* croot = (TColor*) colors->At(i);
235 if (!croot)
236 {
237 croot = new TColor(*cdef);
238 colors->AddAt(croot, i);
239 }
240 else
241 {
242 cdef->Copy(*croot);
243 }
244
245 Float_t r, g, b;
246 croot->GetRGB(r, g, b);
247 r = TMath::Power( r, (2.5 - value)/2.5);
248 g = TMath::Power(g, (2.5 - value)/2.5);
249 b = TMath::Power(b, (2.5 - value)/2.5);
250
251 r = TMath::Min(r, 1.0f);
252 g = TMath::Min(g, 1.0f);
253 b = TMath::Min(b, 1.0f);
254
255 croot->SetRGB(r, g, b);
256 }
257 else
258 {
259 delete colors->RemoveAt(i);
260 }
261 }
262
263 if (full_redraw && REX::gEve)
265}
266
267////////////////////////////////////////////////////////////////////////////////
268/// Return true if interval Q is contained within interval M for U1 variables.
269/// It is assumed that all values are within the [-2pi, 2pi] interval and
270/// minM <= maxM & minQ <= maxQ.
271
273 Float_t minQ, Float_t maxQ)
274{
275 using namespace TMath;
276
277 if (maxQ < minM)
278 {
279 minQ += TwoPi(); maxQ += TwoPi();
280 }
281 else if (minQ > maxM)
282 {
283 minQ -= TwoPi(); maxQ -= TwoPi();
284 }
285 return minQ >= minM && maxQ <= maxM;
286}
287
288////////////////////////////////////////////////////////////////////////////////
289/// Return true if interval Q is overlapping within interval M for U1 variables.
290/// It is assumed that all values are within the [-2pi, 2pi] interval and
291/// minM <= maxM & minQ <= maxQ.
292
294 Float_t minQ, Float_t maxQ)
295{
296 using namespace TMath;
297
298 if (maxQ < minM)
299 {
300 minQ += TwoPi(); maxQ += TwoPi();
301 }
302 else if (minQ > maxM)
303 {
304 minQ -= TwoPi(); maxQ -= TwoPi();
305 }
306 return maxQ >= minM && minQ <= maxM;
307}
308
309////////////////////////////////////////////////////////////////////////////////
310/// Get fraction of interval [minQ, maxQ] in [minM, maxM]
311
313{
314 if (minQ>=minM && maxQ<=maxM)
315 return 1;
316
317 else if (minQ<minM && maxQ>maxM)
318 return (maxM-minM)/(maxQ-minQ);
319
320 else if (minQ>=minM && maxQ>maxM)
321 return (maxM-minQ)/(maxQ-minQ);
322
323 else if (minQ<minM && maxQ<=maxM)
324 return (maxQ-minM)/(maxQ-minQ);
325
326 return 0;
327}
328
329
330/** \class REveGeoManagerHolder
331\ingroup REve
332Exception safe wrapper for setting gGeoManager.
333Functionality to lock-unlock via setting of a static lock in
334TGeoManager should be added (new feature of TGeoManager).
335*/
336
337////////////////////////////////////////////////////////////////////////////////
338/// Constructor.
339/// If n_seg is specified and larger than 2, the new geo-manager's
340/// NSegments is set to this value.
341
343 fManager (gGeoManager),
344 fNSegments (0)
345{
346 gGeoManager = new_gmgr;
347 if (gGeoManager) {
349 if (n_seg > 2) {
352 }
353 } else {
354 gGeoIdentity = nullptr;
355 }
356}
357
358////////////////////////////////////////////////////////////////////////////////
359/// Destructor.
360
362{
363 if (gGeoManager && fNSegments > 2) {
365 }
367 if (gGeoManager) {
369 } else {
370 gGeoIdentity = nullptr;
371 }
372}
373
374/** \class REveRefCnt
375\ingroup REve
376Base-class for reference-counted objects.
377By default the object is destroyed when zero reference-count is reached.
378*/
379
380/** \class REveRefBackPtr
381\ingroup REve
382Base-class for reference-counted objects with reverse references to
383REveElement objects.
384*/
385
386////////////////////////////////////////////////////////////////////////////////
387/// Destructor. Noop, should complain if back-ref list is not empty.
388
390{
391 // !!! Complain if list not empty.
392}
393
394////////////////////////////////////////////////////////////////////////////////
395/// Increase reference count and add re to the list of back-references.
396
398{
400 ++fBackRefs[re];
401}
402
403////////////////////////////////////////////////////////////////////////////////
404/// Decrease reference count and remove re from the list of back-references.
405
407{
408 auto i = fBackRefs.find(re);
409 if (i != fBackRefs.end()) {
410 if (--(i->second) <= 0)
411 fBackRefs.erase(i);
413 } else {
414 Warning("REveRefBackPtr::DecRefCount", "element '%s' not found in back-refs.", re->GetCName());
415 }
416}
417
418////////////////////////////////////////////////////////////////////////////////
419/// Add given stamps to elements in the list of reverse references.
420
422{
423 for (auto &i: fBackRefs)
424 i.first->AddStamp(stamps);
425}
#define b(i)
Definition RSha256.hxx:100
#define c(i)
Definition RSha256.hxx:101
#define g(i)
Definition RSha256.hxx:105
short Color_t
Definition RtypesCore.h:92
unsigned char UChar_t
Definition RtypesCore.h:38
char Char_t
Definition RtypesCore.h:37
constexpr Bool_t kFALSE
Definition RtypesCore.h:101
constexpr Ssiz_t kNPOS
Definition RtypesCore.h:124
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:197
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Definition TError.cxx:241
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
R__EXTERN TGeoManager * gGeoManager
R__EXTERN TGeoIdentity * gGeoIdentity
Definition TGeoMatrix.h:478
#define gROOT
Definition TROOT.h:405
Color * colors
Definition X3DBuffer.c:21
const char * GetCName() const
REveException Exception-type thrown by Eve classes.
Definition REveTypes.hxx:41
Int_t fNSegments
! previous settings for num segments
Definition REveUtil.hxx:90
TGeoManager * fManager
! hold manager
Definition REveUtil.hxx:89
REveGeoManagerHolder(TGeoManager *new_gmgr=nullptr, Int_t n_seg=0)
Constructor.
Definition REveUtil.cxx:342
void FullRedraw3D(Bool_t resetCameras=kFALSE, Bool_t dropLogicals=kFALSE)
Perform 3D redraw of all scenes and viewers.
virtual ~REveRefBackPtr()
Destructor. Noop, should complain if back-ref list is not empty.
Definition REveUtil.cxx:389
virtual void StampBackPtrElements(UChar_t stamps)
Add given stamps to elements in the list of reverse references.
Definition REveUtil.cxx:421
static Bool_t IsU1IntervalOverlappingByMinMax(Float_t minM, Float_t maxM, Float_t minQ, Float_t maxQ)
Return true if interval Q is overlapping within interval M for U1 variables.
Definition REveUtil.cxx:293
static void LoadMacro(const char *mac)
Makes sure that macro 'mac' is loaded, but do not reload it.
Definition REveUtil.cxx:106
static void Macro(const char *mac)
Execute macro 'mac'. Do not reload the macro.
Definition REveUtil.cxx:94
static Bool_t CheckMacro(const char *mac)
Checks if macro 'mac' is loaded.
Definition REveUtil.cxx:66
static Float_t GetFraction(Float_t minM, Float_t maxM, Float_t minQ, Float_t maxQ)
Get fraction of interval [minQ, maxQ] in [minM, maxM].
Definition REveUtil.cxx:312
static void SetColorBrightness(Float_t value, Bool_t full_redraw=kFALSE)
Tweak all ROOT colors to become brighter (if value > 0) or darker (value < 0).
Definition REveUtil.cxx:206
static Bool_t IsU1IntervalContainedByMinMax(Float_t minM, Float_t maxM, Float_t minQ, Float_t maxQ)
Return true if interval Q is contained within interval M for U1 variables.
Definition REveUtil.cxx:272
static void ColorFromIdx(Color_t ci, UChar_t col[4], Bool_t alpha=kTRUE)
Fill col with RGBA values corresponding to index ci.
Definition REveUtil.cxx:118
static Color_t * FindColorVar(TObject *obj, const char *varname)
Find address of Color_t data-member with name varname in object obj.
Definition REveUtil.cxx:187
static void AssertMacro(const char *mac)
Load and execute macro 'mac' if it has not been loaded yet.
Definition REveUtil.cxx:84
static TObjArray * fgDefaultColors
Definition REveUtil.hxx:38
Longptr_t GetDataMemberOffset(const char *membername) const
return offset for member name.
Definition TClass.cxx:3477
The color creation and management class.
Definition TColor.h:19
virtual void SetRGB(Float_t r, Float_t g, Float_t b)
Initialize this color and its associated colors.
Definition TColor.cxx:1759
virtual void GetRGB(Float_t &r, Float_t &g, Float_t &b) const
Definition TColor.h:52
Float_t GetRed() const
Definition TColor.h:58
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:1823
void Copy(TObject &color) const override
Copy this color to obj.
Definition TColor.cxx:1290
Float_t GetBlue() const
Definition TColor.h:60
Float_t GetGreen() const
Definition TColor.h:59
An identity transformation.
Definition TGeoMatrix.h:384
The manager class for any TGeo geometry.
Definition TGeoManager.h:45
TObjArray * GetListOfMatrices() const
void SetNsegments(Int_t nseg)
Set number of segments for approximating circles in drawing.
Int_t GetNsegments() const
Get number of segments approximating circles.
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
An array of TObjects.
Definition TObjArray.h:31
Int_t GetEntriesFast() const
Definition TObjArray.h:58
void AddAt(TObject *obj, Int_t idx) override
Add object at position ids.
TObject * At(Int_t idx) const override
Definition TObjArray.h:164
Mother of all ROOT objects.
Definition TObject.h:41
virtual TClass * IsA() const
Definition TObject.h:245
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:380
Ssiz_t Last(char c) const
Find last occurrence of a character c.
Definition TString.cxx:924
TString & Remove(Ssiz_t pos)
Definition TString.h:685
return c1
Definition legend1.C:41
TF1 * f1
Definition legend1.C:11
return c2
Definition legend2.C:14
R__EXTERN REveManager * gEve
TMath.
Definition TMathBase.h:35
LongDouble_t Power(LongDouble_t x, LongDouble_t y)
Returns x raised to the power y.
Definition TMath.h:719
Short_t Min(Short_t a, Short_t b)
Returns the smallest of a and b.
Definition TMathBase.h:198
auto * t1
Definition textangle.C:20