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