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/REveManager.hxx>
13#include <ROOT/REveElement.hxx>
14#include <ROOT/REveUtil.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#include <regex>
33
34using namespace ROOT::Experimental;
35namespace REX = ROOT::Experimental;
36
37
38/** \class REveUtil
39\ingroup REve
40Standard utility functions for Eve.
41*/
42
43TObjArray *REveUtil::fgDefaultColors = nullptr;
44
45namespace
46{
47////////////////////////////////////////////////////////////////////////////////
48/// Remove last part of string 's', starting from the last
49/// occurrence of character 'c'.
50/// Remove directory part -- everything until the last '/'.
51
52void ChompTailAndDir(TString& s, char c='.')
53{
54 Ssiz_t p = s.Last(c);
55 if (p != kNPOS)
56 s.Remove(p);
57
58 Ssiz_t ls = s.Last('/');
59 if (ls != kNPOS)
60 s.Remove(0, ls + 1);
61}
62}
63
64////////////////////////////////////////////////////////////////////////////////
65/// Checks if macro 'mac' is loaded.
66
67Bool_t REveUtil::CheckMacro(const char* mac)
68{
69 // Axel's advice; now sth seems slow, using old method below for test.
70 // return gROOT->GetInterpreter()->IsLoaded(mac);
71
72 // Previous version expected function with same name and used ROOT's
73 // list of global functions.
74
75 TString foo(mac); ChompTailAndDir(foo);
76 if (gROOT->GetGlobalFunction(foo.Data(), nullptr, kFALSE) != nullptr)
77 return kTRUE;
78 else
79 return (gROOT->GetGlobalFunction(foo.Data(), nullptr, kTRUE) != nullptr);
80}
81
82////////////////////////////////////////////////////////////////////////////////
83/// Load and execute macro 'mac' if it has not been loaded yet.
84
85void REveUtil::AssertMacro(const char* mac)
86{
87 if( CheckMacro(mac) == kFALSE) {
88 gROOT->Macro(mac);
89 }
90}
91
92////////////////////////////////////////////////////////////////////////////////
93/// Execute macro 'mac'. Do not reload the macro.
94
95void REveUtil::Macro(const char* mac)
96{
97 if (CheckMacro(mac) == kFALSE) {
98 gROOT->LoadMacro(mac);
99 }
100 TString foo(mac); ChompTailAndDir(foo); foo += "()";
101 gROOT->ProcessLine(foo.Data());
102}
103
104////////////////////////////////////////////////////////////////////////////////
105/// Makes sure that macro 'mac' is loaded, but do not reload it.
106
107void REveUtil::LoadMacro(const char* mac)
108{
109 if (CheckMacro(mac) == kFALSE) {
110 gROOT->LoadMacro(mac);
111 }
112}
113
114////////////////////////////////////////////////////////////////////////////////
115/// Input string verification and sanitization
116
118{
119 static const std::regex bad_re("[^\\w](?:gSystem|gROOT)[^\\w]", std::regex::optimize);
120 static const std::regex public_extra_re("(?:\\|\")", std::regex::optimize);
121
122 auto beg = expr.cbegin(), end = expr.cend();
123 if (std::regex_search(beg, end, bad_re))
124 return false;
125
126 const bool is_public = true; // to come from gEve
127 if (is_public && std::regex_search(beg, end, public_extra_re))
128 return false;
129
130 return true;
131}
132
133////////////////////////////////////////////////////////////////////////////////
134/// Fill col with RGBA values corresponding to index ci. If alpha
135/// is true, set alpha component of col to 255.
136/// ROOT's indexed color palette does not support transparency.
137
139{
140 TColor* c = gROOT->GetColor(ci);
141 if (c)
142 {
143 col[0] = (UChar_t)(255*c->GetRed());
144 col[1] = (UChar_t)(255*c->GetGreen());
145 col[2] = (UChar_t)(255*c->GetBlue());
146 if (alpha) col[3] = 255;
147 }
148 else
149 {
150 // Set to magenta.
151 col[0] = 255; col[1] = 0; col[2] = 255;
152 if (alpha) col[3] = 255;
153 return;
154 }
155}
156
157////////////////////////////////////////////////////////////////////////////////
158/// Fill col with RGBA values corresponding to index ci and transparency.
159/// ROOT's indexed color palette does not support transparency.
160
161void REveUtil::ColorFromIdx(Color_t ci, UChar_t col[4], Char_t transparency)
162{
163 UChar_t alpha = (255*(100 - transparency))/100;
164
165 TColor* c = gROOT->GetColor(ci);
166 if (c)
167 {
168 col[0] = (UChar_t)(255*c->GetRed());
169 col[1] = (UChar_t)(255*c->GetGreen());
170 col[2] = (UChar_t)(255*c->GetBlue());
171 col[3] = alpha;
172 }
173 else
174 {
175 // Set to magenta.
176 col[0] = 255; col[1] = 0; col[2] = 255; col[3] = alpha;
177 return;
178 }
179}
180
181////////////////////////////////////////////////////////////////////////////////
182/// Fill col with weighted RGBA values corresponding to
183/// color-indices c1 and c2. If alpha is true, set alpha component
184/// of col to 255.
185
187 UChar_t col[4], Bool_t alpha)
188{
190 TColor* t2 = gROOT->GetColor(c2);
191 if(t1 && t2) {
192 col[0] = (UChar_t)(255*(f1*t1->GetRed() + f2*t2->GetRed()));
193 col[1] = (UChar_t)(255*(f1*t1->GetGreen() + f2*t2->GetGreen()));
194 col[2] = (UChar_t)(255*(f1*t1->GetBlue() + f2*t2->GetBlue()));
195 if (alpha) col[3] = 255;
196 }
197}
198
199////////////////////////////////////////////////////////////////////////////////
200/// Find address of Color_t data-member with name varname in object
201/// obj.
202///
203/// This is used to access color information for TGListTreeItem
204/// coloration from visualization macros that wrap TObjects into
205/// REveElementObjectPtr instances.
206
207Color_t* REveUtil::FindColorVar(TObject* obj, const char* varname)
208{
209 static const REveException eh("REveUtil::FindColorVar");
210
211 Int_t off = obj->IsA()->GetDataMemberOffset(varname);
212 if(off == 0)
213 throw(eh + "could not find member '" + varname + "' in class " + obj->IsA()->GetName() + ".");
214 return (Color_t*) (((char*)obj) + off);
215}
216
217////////////////////////////////////////////////////////////////////////////////
218/// Tweak all ROOT colors to become brighter (if value > 0) or
219/// darker (value < 0). Reasonable values for the value argument are
220/// from -2.5 to 2.5 (error will be printed otherwise).
221/// If value is zero, the original colors are restored.
222///
223/// You should call REveManager::FullRedraw3D() afterwards or set
224/// the argument full_redraw to true (default is false).
225
227{
228 if (value < -2.5 || value > 2.5)
229 {
230 Error("REveUtil::SetColorBrightness", "value '%f' out of range [-0.5, 0.5].", value);
231 return;
232 }
233
234 TObjArray *colors = (TObjArray*) gROOT->GetListOfColors();
235
236 if (fgDefaultColors == nullptr)
237 {
238 const Int_t n_col = colors->GetEntriesFast();
239 fgDefaultColors = new TObjArray(n_col);
240 for (Int_t i = 0; i < n_col; ++i)
241 {
242 TColor* c = (TColor*) colors->At(i);
243 if (c)
244 fgDefaultColors->AddAt(new TColor(*c), i);
245 }
246 }
247
248 const Int_t n_col = fgDefaultColors->GetEntriesFast();
249 for (Int_t i = 0; i < n_col; ++i)
250 {
251 TColor* cdef = (TColor*) fgDefaultColors->At(i);
252 if (cdef)
253 {
254 TColor* croot = (TColor*) colors->At(i);
255 if (!croot)
256 {
257 croot = new TColor(*cdef);
258 colors->AddAt(croot, i);
259 }
260 else
261 {
262 cdef->Copy(*croot);
263 }
264
265 Float_t r, g, b;
266 croot->GetRGB(r, g, b);
267 r = TMath::Power( r, (2.5 - value)/2.5);
268 g = TMath::Power(g, (2.5 - value)/2.5);
269 b = TMath::Power(b, (2.5 - value)/2.5);
270
271 r = TMath::Min(r, 1.0f);
272 g = TMath::Min(g, 1.0f);
273 b = TMath::Min(b, 1.0f);
274
275 croot->SetRGB(r, g, b);
276 }
277 else
278 {
279 delete colors->RemoveAt(i);
280 }
281 }
282
283 if (full_redraw && REX::gEve)
285}
286
287////////////////////////////////////////////////////////////////////////////////
288/// Return true if interval Q is contained within interval M for U1 variables.
289/// It is assumed that all values are within the [-2pi, 2pi] interval and
290/// minM <= maxM & minQ <= maxQ.
291
293 Float_t minQ, Float_t maxQ)
294{
295 using namespace TMath;
296
297 if (maxQ < minM)
298 {
299 minQ += TwoPi(); maxQ += TwoPi();
300 }
301 else if (minQ > maxM)
302 {
303 minQ -= TwoPi(); maxQ -= TwoPi();
304 }
305 return minQ >= minM && maxQ <= maxM;
306}
307
308////////////////////////////////////////////////////////////////////////////////
309/// Return true if interval Q is overlapping within interval M for U1 variables.
310/// It is assumed that all values are within the [-2pi, 2pi] interval and
311/// minM <= maxM & minQ <= maxQ.
312
314 Float_t minQ, Float_t maxQ)
315{
316 using namespace TMath;
317
318 if (maxQ < minM)
319 {
320 minQ += TwoPi(); maxQ += TwoPi();
321 }
322 else if (minQ > maxM)
323 {
324 minQ -= TwoPi(); maxQ -= TwoPi();
325 }
326 return maxQ >= minM && minQ <= maxM;
327}
328
329////////////////////////////////////////////////////////////////////////////////
330/// Get fraction of interval [minQ, maxQ] in [minM, maxM]
331
333{
334 if (minQ>=minM && maxQ<=maxM)
335 return 1;
336
337 else if (minQ<minM && maxQ>maxM)
338 return (maxM-minM)/(maxQ-minQ);
339
340 else if (minQ>=minM && maxQ>maxM)
341 return (maxM-minQ)/(maxQ-minQ);
342
343 else if (minQ<minM && maxQ<=maxM)
344 return (maxQ-minM)/(maxQ-minQ);
345
346 return 0;
347}
348
349
350/** \class REveGeoManagerHolder
351\ingroup REve
352Exception safe wrapper for setting gGeoManager.
353Functionality to lock-unlock via setting of a static lock in
354TGeoManager should be added (new feature of TGeoManager).
355*/
356
357////////////////////////////////////////////////////////////////////////////////
358/// Constructor.
359/// If n_seg is specified and larger than 2, the new geo-manager's
360/// NSegments is set to this value.
361
363 fManager (gGeoManager),
364 fNSegments (0)
365{
366 gGeoManager = new_gmgr;
367 if (gGeoManager) {
369 if (n_seg > 2) {
372 }
373 } else {
374 gGeoIdentity = nullptr;
375 }
376}
377
378////////////////////////////////////////////////////////////////////////////////
379/// Destructor.
380
382{
383 if (gGeoManager && fNSegments > 2) {
385 }
387 if (gGeoManager) {
389 } else {
390 gGeoIdentity = nullptr;
391 }
392}
393
394/** \class REveRefCnt
395\ingroup REve
396Base-class for reference-counted objects.
397By default the object is destroyed when zero reference-count is reached.
398*/
399
400/** \class REveRefBackPtr
401\ingroup REve
402Base-class for reference-counted objects with reverse references to
403REveElement objects.
404*/
405
406////////////////////////////////////////////////////////////////////////////////
407/// Destructor. Noop, should complain if back-ref list is not empty.
408
410{
411 // !!! Complain if list not empty.
412}
413
414////////////////////////////////////////////////////////////////////////////////
415/// Increase reference count and add re to the list of back-references.
416
418{
420 ++fBackRefs[re];
421}
422
423////////////////////////////////////////////////////////////////////////////////
424/// Decrease reference count and remove re from the list of back-references.
425
427{
428 auto i = fBackRefs.find(re);
429 if (i != fBackRefs.end()) {
430 if (--(i->second) <= 0)
431 fBackRefs.erase(i);
433 } else {
434 Warning("REveRefBackPtr::DecRefCount", "element '%s' not found in back-refs.", re->GetCName());
435 }
436}
437
438////////////////////////////////////////////////////////////////////////////////
439/// Add given stamps to elements in the list of reverse references.
440
442{
443 for (auto &i: fBackRefs)
444 i.first->AddStamp(stamps);
445}
#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:85
unsigned char UChar_t
Definition RtypesCore.h:38
char Char_t
Definition RtypesCore.h:37
constexpr Bool_t kFALSE
Definition RtypesCore.h:94
constexpr Ssiz_t kNPOS
Definition RtypesCore.h:117
constexpr Bool_t kTRUE
Definition RtypesCore.h:93
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:185
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Definition TError.cxx:229
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:537
#define gROOT
Definition TROOT.h:406
Color * colors
Definition X3DBuffer.c:21
const char * GetCName() const
REveException Exception-type thrown by Eve classes.
Definition REveTypes.hxx:43
Int_t fNSegments
! previous settings for num segments
Definition REveUtil.hxx:93
TGeoManager * fManager
! hold manager
Definition REveUtil.hxx:92
REveGeoManagerHolder(TGeoManager *new_gmgr=nullptr, Int_t n_seg=0)
Constructor.
Definition REveUtil.cxx:362
void FullRedraw3D(Bool_t resetCameras=kFALSE, Bool_t dropLogicals=kFALSE)
Perform 3D redraw of all scenes and viewers.
~REveRefBackPtr() override
Destructor. Noop, should complain if back-ref list is not empty.
Definition REveUtil.cxx:409
virtual void StampBackPtrElements(UChar_t stamps)
Add given stamps to elements in the list of reverse references.
Definition REveUtil.cxx:441
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:313
static void LoadMacro(const char *mac)
Makes sure that macro 'mac' is loaded, but do not reload it.
Definition REveUtil.cxx:107
static void Macro(const char *mac)
Execute macro 'mac'. Do not reload the macro.
Definition REveUtil.cxx:95
static Bool_t CheckMacro(const char *mac)
Checks if macro 'mac' is loaded.
Definition REveUtil.cxx:67
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:332
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:226
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:292
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:138
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:207
static void AssertMacro(const char *mac)
Load and execute macro 'mac' if it has not been loaded yet.
Definition REveUtil.cxx:85
static bool VerifyObjectFilterOrTableExpression(std::string_view expr)
Input string verification and sanitization.
Definition REveUtil.cxx:117
static TObjArray * fgDefaultColors
Definition REveUtil.hxx:37
Longptr_t GetDataMemberOffset(const char *membername) const
return offset for member name.
Definition TClass.cxx:3546
The color creation and management class.
Definition TColor.h:21
virtual void SetRGB(Float_t r, Float_t g, Float_t b)
Initialize this color and its "dark" and "bright" associated colors.
Definition TColor.cxx:1850
virtual void GetRGB(Float_t &r, Float_t &g, Float_t &b) const
Definition TColor.h:54
Float_t GetRed() const
Definition TColor.h:60
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:1920
void Copy(TObject &color) const override
Copy this color to obj.
Definition TColor.cxx:1346
Float_t GetBlue() const
Definition TColor.h:62
Float_t GetGreen() const
Definition TColor.h:61
An identity transformation.
Definition TGeoMatrix.h:406
The manager class for any TGeo geometry.
Definition TGeoManager.h:44
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:243
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:376
Ssiz_t Last(char c) const
Find last occurrence of a character c.
Definition TString.cxx:931
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:721
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