VMC Version 2.0
Loading...
Searching...
No Matches
TGeoMCBranchArrayContainer.cxx
Go to the documentation of this file.
1// -----------------------------------------------------------------------
2// Copyright (C) 2019 CERN and copyright holders of VMC Project.
3// This software is distributed under the terms of the GNU General Public
4// License v3 (GPL Version 3), copied verbatim in the file "LICENSE".
5//
6// See https://github.com/vmc-project/vmc for full licensing information.
7// -----------------------------------------------------------------------
8
9// Authors: Benedikt Volkel 07/03/2019
10
11/*************************************************************************
12 * Copyright (C) 2019, Rene Brun and Fons Rademakers. *
13 * Copyright (C) 2019, ALICE Experiment at CERN. *
14 * All rights reserved. *
15 * *
16 * For the licensing terms see $ROOTSYS/LICENSE. *
17 * For the list of contributors see $ROOTSYS/README/CREDITS. *
18 *************************************************************************/
19
33#include "TGeoManager.h"
34#include "TError.h"
35
36void TGeoMCBranchArrayContainer::Initialize(UInt_t maxLevels, UInt_t size)
37{
38 fMaxLevels = maxLevels;
39 if (fIsInitialized) {
40 ResetCache();
41 }
42 ExtendCache(size);
43 fIsInitialized = kTRUE;
44}
45
47{
48 Initialize(man->GetMaxLevels(), size);
49}
50
52{
53 fCache.clear();
54 fFreeIndices.clear();
55 fIsInitialized = kFALSE;
56}
57
58TGeoBranchArray *TGeoMCBranchArrayContainer::GetNewGeoState(UInt_t &userIndex)
59{
60 if (fFreeIndices.empty()) {
61 ExtendCache(2 * fCache.size());
62 }
63 // Get index from the back
64 UInt_t internalIndex = fFreeIndices.back();
65 fFreeIndices.pop_back();
66 // indices seen by the user are +1
67 userIndex = internalIndex + 1;
68 fCache[internalIndex]->SetUniqueID(userIndex);
69 return fCache[internalIndex].get();
70}
71
72const TGeoBranchArray *TGeoMCBranchArrayContainer::GetGeoState(UInt_t userIndex)
73{
74 if (userIndex == 0) {
75 return nullptr;
76 }
77 if (userIndex > fCache.size()) {
78 ::Fatal("TGeoMCBranchArrayContainer::GetGeoState",
79 "ID %u is not an index referring to TGeoBranchArray "
80 "managed by this TGeoMCBranchArrayContainer",
81 userIndex);
82 }
83 if (fCache[userIndex - 1]->GetUniqueID() == 0) {
84 ::Fatal("TGeoMCBranchArrayContainer::GetGeoState", "Passed index %u refers to an empty/unused geo state",
85 userIndex);
86 }
87 return fCache[userIndex - 1].get();
88}
89
91{
92 if (userIndex > fCache.size() || userIndex == 0) {
93 return;
94 }
95 // Unlock this index so it is free for later use. No need to delete since TGeoBranchArray can be re-used
96 if (fCache[userIndex - 1]->GetUniqueID() > 0) {
97 fFreeIndices.push_back(userIndex - 1);
98 fCache[userIndex - 1]->SetUniqueID(0);
99 }
100}
101
102void TGeoMCBranchArrayContainer::FreeGeoState(const TGeoBranchArray *geoState)
103{
104 if (geoState) {
105 FreeGeoState(geoState->GetUniqueID());
106 }
107}
108
110{
111 // Start counting at 1 since that is the index seen by the user which is assumed by
112 // TGeoMCBranchArrayContainer::FreeGeoState(UInt_t userIndex)
113 for (UInt_t i = 0; i < fCache.size(); i++) {
114 FreeGeoState(i + 1);
115 }
116}
117
119{
120 if (targetSize <= fCache.size()) {
121 targetSize = 2 * fCache.size();
122 }
123 fFreeIndices.reserve(targetSize);
124 fCache.reserve(targetSize);
125 for (UInt_t i = fCache.size(); i < targetSize; i++) {
126 fCache.emplace_back(TGeoBranchArray::MakeInstance(fMaxLevels));
127 fCache.back()->SetUniqueID(0);
128 fFreeIndices.push_back(i);
129 }
130}
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.