GGS(GenericGEANT4Simulation)Software  2.6.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 
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) {
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  iArray->array->Clear("C");
44  COUT(DEEPDEB) << "Assigned array at " << iArray->array << " for class " << _className << " with size " << sizeHint
45  << ENDL;
46  return iArray->array;
47  }
48  }
49  // Step 2: return the greatest available array if step 1 failed.
50  if (_availableArrays.size() > 0) {
51  ArrayList::iterator lastAvailable = _availableArrays.end();
52  lastAvailable--;
53  _assignedArrays.splice(_assignedArrays.end(), _availableArrays, lastAvailable);
54  _assignedArrays.back().array->Clear("C");
55  COUT(DEEPDEB) << "Assigned array at " << _assignedArrays.back().array << " for class " << _className
56  << " with size " << sizeHint << ENDL;
57  return _assignedArrays.back().array;
58  }
59  // Step 3: return a new array if step 2 failed.
60  _assignedArrays.push_back(ArrayDescriptor());
61  _assignedArrays.back().array = new TClonesArray(_className, sizeHint);
62  _assignedArrays.back().allocSize = sizeHint;
63  COUT(DEEPDEB) << "Created array at " << _assignedArrays.back().array << " for class " << _className << " with size "
64  << sizeHint << ENDL;
65  return _assignedArrays.back().array;
66 
67 }
68 
69 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
70 
71 void GGSTClonesArrayService::TakeBackArray(TClonesArray* &array) {
72  static const std::string routineName("GGSTClonesArrayService::TakeBackArray");
73 
74  int arraySize = array->GetEntries();
75 
76  // Step 1: find insertion point in available arrays list
77  ArrayList::iterator iAvArray;
78  for (iAvArray = _availableArrays.begin(); iAvArray != _availableArrays.end(); iAvArray++) {
79  if (iAvArray->allocSize > arraySize) {
80  break;
81  }
82  }
83 
84  // Step 2: search the array between the currently assigned ones
85  for (ArrayList::iterator iArray = _assignedArrays.begin(); iArray != _assignedArrays.end(); iArray++) {
86  if (array == iArray->array) {
87  _availableArrays.splice(iAvArray, _assignedArrays, iArray);
88  array = NULL;
89  COUT(DEEPDEB) << "Took back array at " << iArray->array << " for class " << _className << " with size "
90  << arraySize << ENDL;
91  return;
92  }
93  }
94  // Step 2: new array, allocate a new element for it
95  ArrayList::iterator newArray = _availableArrays.insert(iAvArray, ArrayDescriptor());
96  newArray->array = array;
97  newArray->allocSize = arraySize;
98  array = NULL;
99  COUT(DEEPDEB) << "Adopted array at " << newArray->array << " for class " << _className << " with size " << arraySize
100  << ENDL;
101 
102 }
103 
104 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
105 
107  _availableArrays.resize(0); // The destructor of ArrayDescriptor will delete the array
108 }
109 
110 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
#define ENDL
Definition: GGSSmartLog.h:93
#define COUT(level)
Smart log macro. It writes on stdout only if the specified verbosity level is lesser than the maximum...
Definition: GGSSmartLog.h:66
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:88
TClonesArray * ProvideArray(int sizeHint=0)
Method to obtain an array from the service.
void TakeBackArray(TClonesArray *&array)
Gives an array back to the service.
GGSTClonesArrayService(const char *className)
Constructor.