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