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