GGS(GenericGEANT4Simulation)Software  2.7.0
 All Data Structures Namespaces Files Functions Variables Typedefs Macros
GGSTClonesArrayService.cpp
Go to the documentation of this file.
1 /*
2  * GGSTClonesArrayService.cpp
3  *
4  * Created on: 27 Jul 2013
5  * Author: Nicola Mori
6  */
7 
11 #include "utils/GGSSmartLog.h"
12 
13 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
14 
15 GGSTClonesArrayService::GGSTClonesArrayService(const char *className) : _className(className) {}
16 
17 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
18 
20  static const std::string routineName("GGSTClonesArrayService::~GGSTClonesArrayService");
21  if (_assignedArrays.size() != 0) {
22  COUT(WARNING) << "There are still " << _assignedArrays.size() << " currently assigned arrays for class "
23  << _className << "." << ENDL;
24  CCOUT(WARNING) << "These will not be deleted. The user routine must delete them." << ENDL;
25  for (ArrayList::iterator iArray = _assignedArrays.begin(); iArray != _assignedArrays.end(); iArray++) {
26  iArray->array = NULL; // Set the array pointer to NULL to avoid array deletion in ~ArrayDescriptor
27  }
28  }
29 }
30 
31 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
32 
33 TClonesArray *GGSTClonesArrayService::ProvideArray(int sizeHint, bool clearElements) {
34  static const std::string routineName("GGSTClonesArrayService::ProvideArray");
35 
36  // Step 1: search for available array whose size is fitting the request.
37  // Current "size fit" implementation: return the first array which has same or more elements than sizeHint
38  // (_availableArrays is ordered).
39  for (ArrayList::iterator iArray = _availableArrays.begin(); iArray != _availableArrays.end(); iArray++) {
40  if (iArray->allocSize >= sizeHint) {
41  _assignedArrays.splice(_assignedArrays.end(), _availableArrays, iArray);
42  if (clearElements) {
43  iArray->array->Clear("C");
44  } else {
45  iArray->array->Clear();
46  }
47  COUT(DEEPDEB) << "Assigned array at " << iArray->array << " for class " << _className << " with size " << sizeHint
48  << ENDL;
49  return iArray->array;
50  }
51  }
52  // Step 2: return the greatest available array if step 1 failed.
53  if (_availableArrays.size() > 0) {
54  ArrayList::iterator lastAvailable = _availableArrays.end();
55  lastAvailable--;
56  _assignedArrays.splice(_assignedArrays.end(), _availableArrays, lastAvailable);
57  if (clearElements) {
58  _assignedArrays.back().array->Clear("C");
59  } else {
60  _assignedArrays.back().array->Clear();
61  }
62  COUT(DEEPDEB) << "Assigned array at " << _assignedArrays.back().array << " for class " << _className
63  << " with size " << sizeHint << ENDL;
64  return _assignedArrays.back().array;
65  }
66  // Step 3: return a new array if step 2 failed.
67  _assignedArrays.push_back(ArrayDescriptor());
68  _assignedArrays.back().array = new TClonesArray(_className.c_str(), sizeHint);
69  _assignedArrays.back().allocSize = sizeHint;
70  COUT(DEEPDEB) << "Created array at " << _assignedArrays.back().array << " for class " << _className << " with size "
71  << sizeHint << ENDL;
72  return _assignedArrays.back().array;
73 }
74 
75 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
76 
77 void GGSTClonesArrayService::TakeBackArray(TClonesArray *&array) {
78  static const std::string routineName("GGSTClonesArrayService::TakeBackArray");
79 
80  int arraySize = array->GetEntries();
81 
82  // Step 1: find insertion point in available arrays list
83  ArrayList::iterator iAvArray;
84  for (iAvArray = _availableArrays.begin(); iAvArray != _availableArrays.end(); iAvArray++) {
85  if (iAvArray->allocSize > arraySize) {
86  break;
87  }
88  }
89 
90  // Step 2: search the array between the currently assigned ones
91  for (ArrayList::iterator iArray = _assignedArrays.begin(); iArray != _assignedArrays.end(); iArray++) {
92  if (array == iArray->array) {
93  _availableArrays.splice(iAvArray, _assignedArrays, iArray);
94  array = NULL;
95  COUT(DEEPDEB) << "Took back array at " << iArray->array << " for class " << _className << " with size "
96  << arraySize << ENDL;
97  return;
98  }
99  }
100  // Step 2: new array, allocate a new element for it
101  ArrayList::iterator newArray = _availableArrays.insert(iAvArray, ArrayDescriptor());
102  newArray->array = array;
103  newArray->allocSize = arraySize;
104  array = NULL;
105  COUT(DEEPDEB) << "Adopted array at " << newArray->array << " for class " << _className << " with size " << arraySize
106  << ENDL;
107 }
108 
109 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
110 
112  _availableArrays.resize(0); // The destructor of ArrayDescriptor will delete the array
113 }
114 
115 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
116 
117 void GGSTClonesArrayService::DumpMemInfo() {
118  const std::string routineName("GGSTClonesArrayService::DumpMemInfo");
119  COUT(DEBUG) << "Class: " << _className << ", available: " << _availableArrays.size()
120  << ", assigned: " << _assignedArrays.size() << ENDL;
121  int nAvailable = 0;
122  size_t allocForAvaialble = 0;
123  for (auto &arr : _availableArrays) {
124  nAvailable += arr.array->GetEntries();
125  allocForAvaialble += arr.array->GetSize();
126  }
127  int nAssigned = 0;
128  size_t allocForAssigned = 0;
129  for (auto &arr : _assignedArrays) {
130  nAssigned += arr.array->GetEntries();
131  allocForAssigned += arr.array->GetSize();
132  }
133  CCOUT(DEBUG) << "Available: nElemTot=" << nAvailable
134  << ", <nElem>=" << (float)nAvailable / (float)_availableArrays.size()
135  << ", allocated: " << allocForAvaialble << ENDL;
136  CCOUT(DEBUG) << "Assigned: nElemTot=" << nAssigned
137  << ", <nElem>=" << (float)nAssigned / (float)_assignedArrays.size()
138  << ", allocated: " << allocForAssigned << ENDL;
139 }
#define ENDL
Definition: GGSSmartLog.h:105
#define COUT(level)
Smart log macro. It writes on stdout only if the specified verbosity level is lesser than the maximum...
Definition: GGSSmartLog.h:76
void DeleteAll()
Delete all unassigned arrays in storage.
#define CCOUT(level)
Smart log utility which prints no header at the beginning of the line.
Definition: GGSSmartLog.h:100
TClonesArray * ProvideArray(int sizeHint=0, bool clearElements=false)
Method to obtain an array from the service.
void TakeBackArray(TClonesArray *&array)
Gives an array back to the service.
GGSTClonesArrayService(const char *className)
Constructor.