Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TGeoBranchArray.cxx
Go to the documentation of this file.
1// @(#):$Id$
2// Author: Andrei Gheata 01/03/11
3
4/*************************************************************************
5 * Copyright (C) 1995-2000, 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/** \class TGeoBranchArray
13\ingroup Geometry_classes
14
15An array of daughter indices making a geometry path. Can be used to
16backup/restore a state. Allocated contiguously in memory.
17
18To setup an object of this type, one should use:
19
20~~~ {.cpp}
21 TGeoBranchArray *array = new TGeoBranchArray(level);
22 array->InitFromNavigator(nav); // To initialize from current navigator state
23~~~
24
25The navigator can be updated to reflect this path array: `array->UpdateNavigator();`
26*/
27
28#include "TGeoBranchArray.h"
29
30#include "TMath.h"
31#include "TString.h"
32#include "TGeoNavigator.h"
33#include "TGeoCache.h"
34#include "TGeoManager.h"
35
36
37////////////////////////////////////////////////////////////////////////////////
38/// Constructor. Allocates the array with a size given by level.
39
40TGeoBranchArray::TGeoBranchArray(Int_t maxlevel) : fLevel(-1), fMaxLevel(maxlevel), fMatrix(), fArray(&fRealArray[0])
41{
42 memset(fRealArray, 0, fMaxLevel * sizeof(TGeoNode *));
43}
44
45////////////////////////////////////////////////////////////////////////////////
46/// Make an instance of the class which allocates the node array. To be
47/// released using ReleaseInstance. If addr is non-zero, the user promised that
48/// addr contains at least that many bytes: size_t needed = SizeOf(maxlevel);
49
51{
52 TGeoBranchArray *ba = nullptr;
53 size_t needed = SizeOf(maxlevel);
54 char *ptr = new char[needed];
55 if (!ptr)
56 return nullptr;
57 new (ptr) TGeoBranchArray(maxlevel);
58 ba = reinterpret_cast<TGeoBranchArray *>(ptr);
59 ba->SetBit(kBASelfAlloc, kTRUE);
60 return ba;
61}
62
63////////////////////////////////////////////////////////////////////////////////
64/// Make an instance of the class which allocates the node array. To be
65/// released using ReleaseInstance. If addr is non-zero, the user promised that
66/// addr contains at least that many bytes: size_t needed = SizeOf(maxlevel);
67
69{
70 TGeoBranchArray *ba = nullptr;
72 ba = reinterpret_cast<TGeoBranchArray *>(addr);
73 ba->SetBit(kBASelfAlloc, kFALSE);
74 return ba;
75}
76
77////////////////////////////////////////////////////////////////////////////////
78/// Make a copy of a branch array at the location (if indicated)
79
81{
82 TGeoBranchArray *copy = nullptr;
83 size_t needed = SizeOf(other.fMaxLevel);
84 char *ptr = new char[needed];
85 if (!ptr)
86 return nullptr;
87 new (ptr) TGeoBranchArray(other.fMaxLevel);
88 copy = reinterpret_cast<TGeoBranchArray *>(ptr);
90 copy->fLevel = other.fLevel;
91 copy->fMatrix = other.fMatrix;
92 if (other.fLevel + 1)
93 memcpy(copy->fArray, other.fArray, (other.fLevel + 1) * sizeof(TGeoNode *));
94 return copy;
95}
96
97////////////////////////////////////////////////////////////////////////////////
98/// Make a copy of a branch array at the location (if indicated)
99
101{
102 TGeoBranchArray *copy = nullptr;
103 new (addr) TGeoBranchArray(other.fMaxLevel);
104 copy = reinterpret_cast<TGeoBranchArray *>(addr);
105 copy->SetBit(kBASelfAlloc, kFALSE);
106 copy->fLevel = other.fLevel;
107 copy->fMatrix = other.fMatrix;
108 if (other.fLevel + 1)
109 memcpy(copy->fArray, other.fArray, (other.fLevel + 1) * sizeof(TGeoNode *));
110 return copy;
111}
112
113////////////////////////////////////////////////////////////////////////////////
114/// Raw memcpy of the branch array content to an existing destination.
115
117{
118 memcpy(dest->DataStart(), DataStart(), DataSize());
119 dest->fArray = &(dest->fRealArray[0]);
120}
121
122////////////////////////////////////////////////////////////////////////////////
123/// Releases the space allocated for the object
124
126{
127 obj->~TGeoBranchArray();
128 if (obj->TestBit(kBASelfAlloc))
129 delete[](char *) obj;
130}
131
132////////////////////////////////////////////////////////////////////////////////
133/// Updates the internal addresses for n contiguous objects which have the same
134/// fMaxLevel
135/// Updates the internal addresses for n contiguous objects which have the same fMaxLevel
136
138{
139 size_t needed = SizeOf();
140 // char *where = &fArray;
141 // for (size_t i=0; i<nobj; ++i, where += needed) {
142 // TGeoNode ***array = reinterpret_cast<TGeoNode***>(where);
143 // *array = ((void**)where)+1;
144 // }
145 char *where = reinterpret_cast<char *>(this);
146 for (size_t i = 0; i < nobj; ++i, where += needed) {
147 TGeoBranchArray *obj = reinterpret_cast<TGeoBranchArray *>(where);
148 obj->fArray = &(obj->fRealArray[0]);
149 }
150}
151
152////////////////////////////////////////////////////////////////////////////////
153/// Copy constructor. Not callable anymore. Use TGeoBranchArray::MakeCopy instead
154
156 : TObject(other), fLevel(other.fLevel), fMaxLevel(other.fMaxLevel), fMatrix(other.fMatrix), fArray(nullptr)
157{
158 if (fMaxLevel) {
159 fArray = new TGeoNode *[fMaxLevel];
160 if (fLevel + 1)
161 memcpy(fArray, other.fArray, (fLevel + 1) * sizeof(TGeoNode *));
162 }
163}
164
165////////////////////////////////////////////////////////////////////////////////
166/// Assignment. Not valid anymore. Use TGeoBranchArray::MakeCopy instead
167
169{
170 if (&other == this)
171 return *this;
172 fLevel = other.fLevel;
173 fMatrix.CopyFrom(&other.fMatrix);
174 if (fLevel + 1)
175 memcpy(fArray, other.fArray, (fLevel + 1) * sizeof(TGeoNode *));
176 return *this;
177}
178
179////////////////////////////////////////////////////////////////////////////////
180/// Add and extra daughter to the current path array. No validity check performed !
181
183{
184 if (fLevel < 0) {
185 Error("AddLevel", "You must initialize from navigator or copy from another branch array first.");
186 return;
187 }
188 if (fLevel > fMaxLevel) {
189 Fatal("AddLevel", "Max level = %d reached\n", fMaxLevel);
190 return;
191 }
192 fLevel++;
193 /*
194 if (fLevel+1>fMaxLevel) {
195 TGeoNode **array = new TGeoNode*[fLevel+1];
196 memcpy(array, fArray, fLevel*sizeof(TGeoNode*));
197 delete [] fArray;
198 fArray = array;
199 }
200 */
202}
203
204////////////////////////////////////////////////////////////////////////////////
205/// Is equal operator.
206
208{
210 if (value == 0)
211 return kTRUE;
212 return kFALSE;
213}
214
215////////////////////////////////////////////////////////////////////////////////
216/// Not equal operator.
217
219{
221 if (value != 0)
222 return kTRUE;
223 return kFALSE;
224}
225
226////////////////////////////////////////////////////////////////////////////////
227/// Is equal operator.
228
230{
232 if (value > 0)
233 return kTRUE;
234 return kFALSE;
235}
236
237////////////////////////////////////////////////////////////////////////////////
238/// Is equal operator.
239
241{
243 if (value < 0)
244 return kTRUE;
245 return kFALSE;
246}
247
248////////////////////////////////////////////////////////////////////////////////
249/// Is equal operator.
250
252{
254 if (value >= 0)
255 return kTRUE;
256 return kFALSE;
257}
258
259////////////////////////////////////////////////////////////////////////////////
260/// Is equal operator.
261
263{
265 if (value <= 0)
266 return kTRUE;
267 return kFALSE;
268}
269
270////////////////////////////////////////////////////////////////////////////////
271/// Binary search in an array of n pointers to branch arrays, to locate value.
272/// Returns element index or index of nearest element smaller than value
273
275{
277 const TGeoBranchArray *pind;
278 nabove = n + 1;
279 nbelow = 0;
280 while (nabove - nbelow > 1) {
281 middle = (nabove + nbelow) / 2;
282 pind = array[middle - 1];
283 if (*value == *pind)
284 return middle - 1;
285 if (*value < *pind)
286 nabove = middle;
287 else
288 nbelow = middle;
289 }
290 return nbelow - 1;
291}
292
293////////////////////////////////////////////////////////////////////////////////
294/// Compare with other object of same type. Returns -1 if this is smaller (first
295/// smaller array value prevails), 0 if equal (size and values) and 1 if this is
296/// larger.
297
299{
300 Int_t i;
302 Int_t otherLevel = other->GetLevel();
304 TGeoNode **otherArray = other->GetArray();
305 for (i = 0; i < maxLevel + 1; i++) {
306 if (fArray[i] == otherArray[i])
307 continue;
308 if ((Long64_t)fArray[i] < (Long64_t)otherArray[i])
309 return -1;
310 return 1;
311 }
312 if (fLevel == otherLevel)
313 return 0;
314 if (fLevel < otherLevel)
315 return -1;
316 return 1;
317}
318
319////////////////////////////////////////////////////////////////////////////////
320/// Garbage collect the stored matrix.
321
323
324////////////////////////////////////////////////////////////////////////////////
325/// Init the branch array from an array of nodes, the global matrix for the path and
326/// the level.
327
329{
331 if (level > fMaxLevel) {
332 Fatal("Init", "Requested level %d exceeds maximum level %d", level + 1, fMaxLevel);
333 return;
334 }
335 fLevel = level;
336 memcpy(fArray, branch, (fLevel + 1) * sizeof(TGeoNode *));
337}
338
339////////////////////////////////////////////////////////////////////////////////
340/// Init the branch array from current navigator state.
341
343{
344 TGeoNodeCache *cache = nav->GetCache();
345 const TGeoNode **branch = (const TGeoNode **)cache->GetBranch();
346 Int_t level = cache->GetLevel();
348 if (level > fMaxLevel) {
349 Fatal("InitFromNavigator", "Requested level %d exceeds maximum level %d", level + 1, fMaxLevel);
350 return;
351 }
352 fLevel = level;
353 memcpy(fArray, branch, (fLevel + 1) * sizeof(TGeoNode *));
354 if (nav->IsOutside())
355 fLevel = -1;
356}
357
358////////////////////////////////////////////////////////////////////////////////
359/// Fill path pointed by the array.
360
362{
363 path = "";
364 if (!fArray || !fArray[0])
365 return;
366 for (Int_t i = 0; i < fLevel + 1; i++) {
367 path += "/";
368 path += fArray[i]->GetName();
369 }
370}
371
372////////////////////////////////////////////////////////////////////////////////
373/// Print branch information
374
376{
377 TString path;
378 GetPath(path);
379 printf("branch: %s\n", path.Data());
380}
381
382////////////////////////////////////////////////////////////////////////////////
383/// Sorting of an array of branch array pointers.
384
386{
387 for (Int_t i = 0; i < n; i++)
388 index[i] = i;
389 if (down)
390 std::sort(index, index + n, compareBAdesc(array));
391 else
392 std::sort(index, index + n, compareBAasc(array));
393}
394
395////////////////////////////////////////////////////////////////////////////////
396/// Update the navigator to reflect the branch.
397/// nav->CdTop();
398
400{
401 if (fLevel < 0) {
402 nav->SetOutside(kTRUE);
403 return;
404 }
405 Int_t matchlev = 0;
406 Int_t navlev = nav->GetLevel();
407 Int_t i;
409 for (i = 1; i < maxlev + 1; ++i) {
410 if (fArray[i] != nav->GetMother(navlev - i))
411 break;
412 matchlev++;
413 }
414 // Go to matching level
415 for (i = 0; i < navlev - matchlev; i++)
416 nav->CdUp();
417 for (i = matchlev + 1; i < fLevel + 1; i++)
418 nav->CdDown(fArray[i]);
419}
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
long long Long64_t
Portable signed long integer 8 bytes.
Definition RtypesCore.h:83
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
const char Option_t
Option string (const char)
Definition RtypesCore.h:80
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t dest
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
An array of daughter indices making a geometry path.
static void ReleaseInstance(TGeoBranchArray *obj)
Releases the space allocated for the object.
void * DataStart() const
TGeoBranchArray & operator=(const TGeoBranchArray &)
Assignment. Not valid anymore. Use TGeoBranchArray::MakeCopy instead.
size_t SizeOf() const
static TGeoBranchArray * MakeCopyAt(const TGeoBranchArray &other, void *addr)
Make a copy of a branch array at the location (if indicated)
static TGeoBranchArray * MakeCopy(const TGeoBranchArray &other)
Make a copy of a branch array at the location (if indicated)
void Init(TGeoNode **branch, TGeoMatrix *global, Int_t level)
Init the branch array from an array of nodes, the global matrix for the path and the level.
void GetPath(TString &path) const
Fill path pointed by the array.
~TGeoBranchArray() override
void CleanMatrix()
Garbage collect the stored matrix.
Bool_t operator!=(const TGeoBranchArray &other) const
Not equal operator.
static TGeoBranchArray * MakeInstanceAt(size_t maxlevel, void *addr)
Make an instance of the class which allocates the node array.
Bool_t operator<(const TGeoBranchArray &other) const
Is equal operator.
size_t DataSize() const
TGeoBranchArray(Int_t level)
Constructor. Allocates the array with a size given by level.
void CopyTo(TGeoBranchArray *dest)
Raw memcpy of the branch array content to an existing destination.
TGeoNode * fRealArray[1]
[fMaxLevel+1] Array of nodes
Bool_t operator>(const TGeoBranchArray &other) const
Is equal operator.
void UpdateArray(size_t nobj)
Updates the internal addresses for n contiguous objects which have the same fMaxLevel Updates the int...
void UpdateNavigator(TGeoNavigator *nav) const
Update the navigator to reflect the branch.
Bool_t operator>=(const TGeoBranchArray &other) const
Is equal operator.
static TGeoBranchArray * MakeInstance(size_t maxlevel)
Make an instance of the class which allocates the node array.
Bool_t operator<=(const TGeoBranchArray &other) const
Is equal operator.
static void Sort(Int_t n, TGeoBranchArray **array, Int_t *index, Bool_t down=kTRUE)
Sorting of an array of branch array pointers.
TGeoNode ** fArray
void AddLevel(Int_t dindex)
Add and extra daughter to the current path array. No validity check performed !
void InitFromNavigator(TGeoNavigator *nav)
Init the branch array from current navigator state.
Bool_t operator==(const TGeoBranchArray &other) const
Is equal operator.
static Long64_t BinarySearch(Long64_t n, const TGeoBranchArray **array, TGeoBranchArray *value)
Binary search in an array of n pointers to branch arrays, to locate value.
TGeoHMatrix fMatrix
Int_t Compare(const TObject *obj) const override
Compare with other object of same type.
void Print(Option_t *option="") const override
Print branch information.
void CopyFrom(const TGeoMatrix *other)
Fast copy method.
Geometrical transformation package.
Definition TGeoMatrix.h:38
Class providing navigation API for TGeo geometries.
Special pool of reusable nodes.
Definition TGeoCache.h:56
void * GetBranch() const
Definition TGeoCache.h:104
TGeoHMatrix * GetCurrentMatrix() const
Definition TGeoCache.h:109
Int_t GetLevel() const
Definition TGeoCache.h:121
A node represent a volume positioned inside another.They store links to both volumes and to the TGeoM...
Definition TGeoNode.h:39
TGeoVolume * GetVolume() const
Definition TGeoNode.h:99
TGeoNode * GetNode(const char *name) const
get the pointer to a daughter node
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
Mother of all ROOT objects.
Definition TObject.h:41
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition TObject.h:202
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:864
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:1071
virtual void Fatal(const char *method, const char *msgfmt,...) const
Issue fatal error message.
Definition TObject.cxx:1099
Basic string class.
Definition TString.h:138
const char * Data() const
Definition TString.h:384
const Int_t n
Definition legend1.C:16
Short_t Min(Short_t a, Short_t b)
Returns the smallest of a and b.
Definition TMathBase.h:199