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
37
38////////////////////////////////////////////////////////////////////////////////
39/// Constructor. Allocates the array with a size given by level.
40
41TGeoBranchArray::TGeoBranchArray(Int_t maxlevel) : fLevel(-1), fMaxLevel(maxlevel), fMatrix(), fArray(&fRealArray[0])
42{
43 memset(fRealArray, 0, fMaxLevel * sizeof(TGeoNode *));
44}
45
46////////////////////////////////////////////////////////////////////////////////
47/// Make an instance of the class which allocates the node array. To be
48/// released using ReleaseInstance. If addr is non-zero, the user promised that
49/// addr contains at least that many bytes: size_t needed = SizeOf(maxlevel);
50
52{
53 TGeoBranchArray *ba = 0;
54 size_t needed = SizeOf(maxlevel);
55 char *ptr = new char[needed];
56 if (!ptr)
57 return 0;
58 new (ptr) TGeoBranchArray(maxlevel);
59 ba = reinterpret_cast<TGeoBranchArray *>(ptr);
61 return ba;
62}
63
64////////////////////////////////////////////////////////////////////////////////
65/// Make an instance of the class which allocates the node array. To be
66/// released using ReleaseInstance. If addr is non-zero, the user promised that
67/// addr contains at least that many bytes: size_t needed = SizeOf(maxlevel);
68
70{
71 TGeoBranchArray *ba = 0;
72 new (addr) TGeoBranchArray(maxlevel);
73 ba = reinterpret_cast<TGeoBranchArray *>(addr);
75 return ba;
76}
77
78////////////////////////////////////////////////////////////////////////////////
79/// Make a copy of a branch array at the location (if indicated)
80
82{
83 TGeoBranchArray *copy = 0;
84 size_t needed = SizeOf(other.fMaxLevel);
85 char *ptr = new char[needed];
86 if (!ptr)
87 return 0;
88 new (ptr) TGeoBranchArray(other.fMaxLevel);
89 copy = reinterpret_cast<TGeoBranchArray *>(ptr);
91 copy->fLevel = other.fLevel;
92 copy->fMatrix = other.fMatrix;
93 if (other.fLevel + 1)
94 memcpy(copy->fArray, other.fArray, (other.fLevel + 1) * sizeof(TGeoNode *));
95 return copy;
96}
97
98////////////////////////////////////////////////////////////////////////////////
99/// Make a copy of a branch array at the location (if indicated)
100
102{
103 TGeoBranchArray *copy = 0;
104 new (addr) TGeoBranchArray(other.fMaxLevel);
105 copy = reinterpret_cast<TGeoBranchArray *>(addr);
106 copy->SetBit(kBASelfAlloc, kFALSE);
107 copy->fLevel = other.fLevel;
108 copy->fMatrix = other.fMatrix;
109 if (other.fLevel + 1)
110 memcpy(copy->fArray, other.fArray, (other.fLevel + 1) * sizeof(TGeoNode *));
111 return copy;
112}
113
114////////////////////////////////////////////////////////////////////////////////
115/// Raw memcpy of the branch array content to an existing destination.
116
118{
119 memcpy(dest->DataStart(), DataStart(), DataSize());
120 dest->fArray = &(dest->fRealArray[0]);
121}
122
123////////////////////////////////////////////////////////////////////////////////
124/// Releases the space allocated for the object
125
127{
128 obj->~TGeoBranchArray();
129 if (obj->TestBit(kBASelfAlloc))
130 delete[](char *) obj;
131}
132
133////////////////////////////////////////////////////////////////////////////////
134/// Updates the internal addresses for n contiguous objects which have the same
135/// fMaxLevel
136/// Updates the internal addresses for n contiguous objects which have the same fMaxLevel
137
139{
140 size_t needed = SizeOf();
141 // char *where = &fArray;
142 // for (size_t i=0; i<nobj; ++i, where += needed) {
143 // TGeoNode ***array = reinterpret_cast<TGeoNode***>(where);
144 // *array = ((void**)where)+1;
145 // }
146 char *where = reinterpret_cast<char *>(this);
147 for (size_t i = 0; i < nobj; ++i, where += needed) {
148 TGeoBranchArray *obj = reinterpret_cast<TGeoBranchArray *>(where);
149 obj->fArray = &(obj->fRealArray[0]);
150 }
151}
152
153////////////////////////////////////////////////////////////////////////////////
154/// Copy constructor. Not callable anymore. Use TGeoBranchArray::MakeCopy instead
155
157 : TObject(other), fLevel(other.fLevel), fMaxLevel(other.fMaxLevel), fMatrix(other.fMatrix), fArray(NULL)
158{
159 if (fMaxLevel) {
160 fArray = new TGeoNode *[fMaxLevel];
161 if (fLevel + 1)
162 memcpy(fArray, other.fArray, (fLevel + 1) * sizeof(TGeoNode *));
163 }
164}
165
166////////////////////////////////////////////////////////////////////////////////
167/// Assignment. Not valid anymore. Use TGeoBranchArray::MakeCopy instead
168
170{
171 if (&other == this)
172 return *this;
173 fLevel = other.fLevel;
174 fMatrix.CopyFrom(&other.fMatrix);
175 if (fLevel + 1)
176 memcpy(fArray, other.fArray, (fLevel + 1) * sizeof(TGeoNode *));
177 return *this;
178}
179
180////////////////////////////////////////////////////////////////////////////////
181/// Add and extra daughter to the current path array. No validity check performed !
182
184{
185 if (fLevel < 0) {
186 Error("AddLevel", "You must initialize from navigator or copy from another branch array first.");
187 return;
188 }
189 if (fLevel > fMaxLevel) {
190 Fatal("AddLevel", "Max level = %d reached\n", fMaxLevel);
191 return;
192 }
193 fLevel++;
194 /*
195 if (fLevel+1>fMaxLevel) {
196 TGeoNode **array = new TGeoNode*[fLevel+1];
197 memcpy(array, fArray, fLevel*sizeof(TGeoNode*));
198 delete [] fArray;
199 fArray = array;
200 }
201 */
202 fArray[fLevel] = fArray[fLevel - 1]->GetVolume()->GetNode(dindex);
203}
204
205////////////////////////////////////////////////////////////////////////////////
206/// Is equal operator.
207
209{
210 Int_t value = Compare(&other);
211 if (value == 0)
212 return kTRUE;
213 return kFALSE;
214}
215
216////////////////////////////////////////////////////////////////////////////////
217/// Not equal operator.
218
220{
221 Int_t value = Compare(&other);
222 if (value != 0)
223 return kTRUE;
224 return kFALSE;
225}
226
227////////////////////////////////////////////////////////////////////////////////
228/// Is equal operator.
229
231{
232 Int_t value = Compare(&other);
233 if (value > 0)
234 return kTRUE;
235 return kFALSE;
236}
237
238////////////////////////////////////////////////////////////////////////////////
239/// Is equal operator.
240
242{
243 Int_t value = Compare(&other);
244 if (value < 0)
245 return kTRUE;
246 return kFALSE;
247}
248
249////////////////////////////////////////////////////////////////////////////////
250/// Is equal operator.
251
253{
254 Int_t value = Compare(&other);
255 if (value >= 0)
256 return kTRUE;
257 return kFALSE;
258}
259
260////////////////////////////////////////////////////////////////////////////////
261/// Is equal operator.
262
264{
265 Int_t value = Compare(&other);
266 if (value <= 0)
267 return kTRUE;
268 return kFALSE;
269}
270
271////////////////////////////////////////////////////////////////////////////////
272/// Binary search in an array of n pointers to branch arrays, to locate value.
273/// Returns element index or index of nearest element smaller than value
274
276{
277 Long64_t nabove, nbelow, middle;
278 const TGeoBranchArray *pind;
279 nabove = n + 1;
280 nbelow = 0;
281 while (nabove - nbelow > 1) {
282 middle = (nabove + nbelow) / 2;
283 pind = array[middle - 1];
284 if (*value == *pind)
285 return middle - 1;
286 if (*value < *pind)
287 nabove = middle;
288 else
289 nbelow = middle;
290 }
291 return nbelow - 1;
292}
293
294////////////////////////////////////////////////////////////////////////////////
295/// Compare with other object of same type. Returns -1 if this is smaller (first
296/// smaller array value prevails), 0 if equal (size and values) and 1 if this is
297/// larger.
298
300{
301 Int_t i;
302 TGeoBranchArray *other = (TGeoBranchArray *)obj;
303 Int_t otherLevel = other->GetLevel();
304 Int_t maxLevel = TMath::Min(fLevel, otherLevel);
305 TGeoNode **otherArray = other->GetArray();
306 for (i = 0; i < maxLevel + 1; i++) {
307 if (fArray[i] == otherArray[i])
308 continue;
309 if ((Long64_t)fArray[i] < (Long64_t)otherArray[i])
310 return -1;
311 return 1;
312 }
313 if (fLevel == otherLevel)
314 return 0;
315 if (fLevel < otherLevel)
316 return -1;
317 return 1;
318}
319
320////////////////////////////////////////////////////////////////////////////////
321/// Garbage collect the stored matrix.
322
324
325////////////////////////////////////////////////////////////////////////////////
326/// Init the branch array from an array of nodes, the global matrix for the path and
327/// the level.
328
329void TGeoBranchArray::Init(TGeoNode **branch, TGeoMatrix *global, Int_t level)
330{
331 fMatrix.CopyFrom(global);
332 if (level > fMaxLevel) {
333 Fatal("Init", "Requested level %d exceeds maximum level %d", level + 1, fMaxLevel);
334 return;
335 }
336 fLevel = level;
337 memcpy(fArray, branch, (fLevel + 1) * sizeof(TGeoNode *));
338}
339
340////////////////////////////////////////////////////////////////////////////////
341/// Init the branch array from current navigator state.
342
344{
345 TGeoNodeCache *cache = nav->GetCache();
346 const TGeoNode **branch = (const TGeoNode **)cache->GetBranch();
347 Int_t level = cache->GetLevel();
349 if (level > fMaxLevel) {
350 Fatal("InitFromNavigator", "Requested level %d exceeds maximum level %d", level + 1, fMaxLevel);
351 return;
352 }
353 fLevel = level;
354 memcpy(fArray, branch, (fLevel + 1) * sizeof(TGeoNode *));
355 if (nav->IsOutside())
356 fLevel = -1;
357}
358
359////////////////////////////////////////////////////////////////////////////////
360/// Fill path pointed by the array.
361
363{
364 path = "";
365 if (!fArray || !fArray[0])
366 return;
367 for (Int_t i = 0; i < fLevel + 1; i++) {
368 path += "/";
369 path += fArray[i]->GetName();
370 }
371}
372
373////////////////////////////////////////////////////////////////////////////////
374/// Print branch information
375
377{
378 TString path;
379 GetPath(path);
380 printf("branch: %s\n", path.Data());
381}
382
383////////////////////////////////////////////////////////////////////////////////
384/// Sorting of an array of branch array pointers.
385
387{
388 for (Int_t i = 0; i < n; i++)
389 index[i] = i;
390 if (down)
391 std::sort(index, index + n, compareBAdesc(array));
392 else
393 std::sort(index, index + n, compareBAasc(array));
394}
395
396////////////////////////////////////////////////////////////////////////////////
397/// Update the navigator to reflect the branch.
398/// nav->CdTop();
399
401{
402 if (fLevel < 0) {
403 nav->SetOutside(kTRUE);
404 return;
405 }
406 Int_t matchlev = 0;
407 Int_t navlev = nav->GetLevel();
408 Int_t i;
409 Int_t maxlev = TMath::Min(fLevel, navlev);
410 for (i = 1; i < maxlev + 1; ++i) {
411 if (fArray[i] != nav->GetMother(navlev - i))
412 break;
413 matchlev++;
414 }
415 // Go to matching level
416 for (i = 0; i < navlev - matchlev; i++)
417 nav->CdUp();
418 for (i = matchlev + 1; i < fLevel + 1; i++)
419 nav->CdDown(fArray[i]);
420}
constexpr Bool_t kFALSE
Definition RtypesCore.h:101
long long Long64_t
Definition RtypesCore.h:80
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:377
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
TGeoNode ** GetArray() 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.
size_t GetLevel() const
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.
void CdUp()
Go one level up in geometry.
TGeoNode * GetMother(Int_t up=1) const
void SetOutside(Bool_t flag=kTRUE)
Bool_t IsOutside() const
TGeoNodeCache * GetCache() const
Int_t GetLevel() const
void CdDown(Int_t index)
Make a daughter of current node current.
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:47
Mother of all ROOT objects.
Definition TObject.h:41
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition TObject.h:199
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:780
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:987
virtual void Fatal(const char *method, const char *msgfmt,...) const
Issue fatal error message.
Definition TObject.cxx:1015
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:380
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:198
#define dest(otri, vertexptr)
Definition triangle.c:1041