Logo ROOT  
Reference Guide
TGeoMCBranchArrayContainer.cxx
Go to the documentation of this file.
1// @(#)root/vmc:$Id$
2// Authors: Benedikt Volkel 07/03/2019
3
4/*************************************************************************
5 * Copyright (C) 2019, Rene Brun and Fons Rademakers. *
6 * Copyright (C) 2019, ALICE Experiment at CERN. *
7 * All rights reserved. *
8 * *
9 * For the licensing terms see $ROOTSYS/LICENSE. *
10 * For the list of contributors see $ROOTSYS/README/CREDITS. *
11 *************************************************************************/
12
13/** \class TGeoMCBranchArrayContainer
14 \ingroup vmc
15
16Storing and re-using geometry states of the TGeoManager in use by storing
17them as TGeoBranchArrays.
18
19After having initialized a navigator using a stored state, it can be freed
20to be used again for storing another geometry state. This makes it easy to
21handle many events with many stored geometry states and the memory used is
22kept as small as possible.
23*/
24
26#include "TGeoManager.h"
27#include "TError.h"
28
30{
31 fMaxLevels = maxLevels;
32 if (fIsInitialized) {
33 ResetCache();
34 }
35 ExtendCache(size);
37}
38
40{
41 Initialize(man->GetMaxLevels(), size);
42}
43
45{
46 fCache.clear();
47 fFreeIndices.clear();
49}
50
52{
53 if (fFreeIndices.empty()) {
54 ExtendCache(2 * fCache.size());
55 }
56 // Get index from the back
57 UInt_t internalIndex = fFreeIndices.back();
58 fFreeIndices.pop_back();
59 // indices seen by the user are +1
60 userIndex = internalIndex + 1;
61 fCache[internalIndex]->SetUniqueID(userIndex);
62 return fCache[internalIndex].get();
63}
64
66{
67 if (userIndex == 0) {
68 return nullptr;
69 }
70 if (userIndex > fCache.size()) {
71 ::Fatal("TGeoMCBranchArrayContainer::GetGeoState",
72 "ID %u is not an index referring to TGeoBranchArray "
73 "managed by this TGeoMCBranchArrayContainer",
74 userIndex);
75 }
76 if (fCache[userIndex - 1]->GetUniqueID() == 0) {
77 ::Fatal("TGeoMCBranchArrayContainer::GetGeoState", "Passed index %u refers to an empty/unused geo state",
78 userIndex);
79 }
80 return fCache[userIndex - 1].get();
81}
82
84{
85 if (userIndex > fCache.size() || userIndex == 0) {
86 return;
87 }
88 // Unlock this index so it is free for later use. No need to delete since TGeoBranchArray can be re-used
89 if (fCache[userIndex - 1]->GetUniqueID() > 0) {
90 fFreeIndices.push_back(userIndex - 1);
91 fCache[userIndex - 1]->SetUniqueID(0);
92 }
93}
94
96{
97 if (geoState) {
98 FreeGeoState(geoState->GetUniqueID());
99 }
100}
101
103{
104 // Start counting at 1 since that is the index seen by the user which is assumed by
105 // TGeoMCBranchArrayContainer::FreeGeoState(UInt_t userIndex)
106 for (UInt_t i = 0; i < fCache.size(); i++) {
107 FreeGeoState(i + 1);
108 }
109}
110
112{
113 if (targetSize <= fCache.size()) {
114 targetSize = 2 * fCache.size();
115 }
116 fFreeIndices.reserve(targetSize);
117 fCache.reserve(targetSize);
118 for (UInt_t i = fCache.size(); i < targetSize; i++) {
120 fCache.back()->SetUniqueID(0);
121 fFreeIndices.push_back(i);
122 }
123}
unsigned int UInt_t
Definition: RtypesCore.h:42
const Bool_t kFALSE
Definition: RtypesCore.h:88
const Bool_t kTRUE
Definition: RtypesCore.h:87
void Fatal(const char *location, const char *msgfmt,...)
An array of daughter indices making a geometry path.
static TGeoBranchArray * MakeInstance(size_t maxlevel)
Make an instance of the class which allocates the node array.
void ExtendCache(UInt_t targetSize=1)
Resize the cache.
UInt_t fMaxLevels
Maximum level of node array inside a chached state.
void Initialize(UInt_t maxlevels=100, UInt_t size=8)
Initialize manually specifying initial number of internal TGeoBranchArray objects.
TGeoBranchArray * GetNewGeoState(UInt_t &userIndex)
Get a TGeoBranchArray to set to current geo state.
const TGeoBranchArray * GetGeoState(UInt_t userIndex)
Get a TGeoBranchArray to read the current state from.
std::vector< UInt_t > fFreeIndices
Provide indices in fCachedStates which are already popped and can be re-populated again.
void InitializeFromGeoManager(TGeoManager *man, UInt_t size=8)
Initialize from TGeoManager to extract maxlevels.
Bool_t fIsInitialized
Flag if initialized.
void ResetCache()
Clear the internal cache.
void FreeGeoStates()
Free all geo states at once but keep the container size.
void FreeGeoState(UInt_t userIndex)
Free the index of this geo state such that it can be re-used.
std::vector< std::unique_ptr< TGeoBranchArray > > fCache
Cache states via TGeoBranchArray.
The manager class for any TGeo geometry.
Definition: TGeoManager.h:43
static Int_t GetMaxLevels()
Return maximum number of levels used in the geometry.
virtual UInt_t GetUniqueID() const
Return the unique object id.
Definition: TObject.cxx:375