ndmspc  v1.1.1-1
NDimensionalExecutor.cxx
1 #include <string>
2 #include <THnSparse.h>
3 #include <TAxis.h>
4 #include "NDimensionalExecutor.h"
5 
6 namespace Ndmspc {
7 
8 // --- Private Increment Logic ---
10 {
11  for (int i = fNumDimensions - 1; i >= 0; --i) {
12  fCurrentCoords[i]++;
13  if (fCurrentCoords[i] <= fMaxBounds[i]) {
14  return true;
15  }
16  fCurrentCoords[i] = fMinBounds[i];
17  }
18  return false;
19 }
20 
21 NDimensionalExecutor::NDimensionalExecutor(const std::vector<int> & minBounds, const std::vector<int> & maxBounds)
22  : fMinBounds(minBounds), fMaxBounds(maxBounds)
23 {
27 
28  if (fMinBounds.size() != fMaxBounds.size()) {
29  throw std::invalid_argument("Min and max bounds vectors must have the same size.");
30  }
31  if (fMinBounds.empty()) {
32  throw std::invalid_argument("Bounds vectors cannot be empty.");
33  }
34 
35  fNumDimensions = fMinBounds.size();
36 
37  for (size_t i = 0; i < fNumDimensions; ++i) {
38  if (fMinBounds[i] > fMaxBounds[i]) {
39  throw std::invalid_argument("Min bound (" + std::to_string(fMinBounds[i]) +
40  ") cannot be greater than max bound (" + std::to_string(fMaxBounds[i]) +
41  ") for dimension " + std::to_string(i));
42  }
43  }
44 
47 }
48 
49 NDimensionalExecutor::NDimensionalExecutor(THnSparse * hist, bool onlyfilled)
50 {
54  if (hist == nullptr) {
55  throw std::invalid_argument("THnSparse pointer cannot be null.");
56  }
57 
58  if (onlyfilled) {
59  // Check if the histogram is filled
60  if (hist->GetNbins() <= 0) {
61  throw std::invalid_argument("THnSparse histogram is empty.");
62  }
63 
64  fMinBounds.push_back(0);
65  fMaxBounds.push_back(hist->GetNbins());
66  }
67  else {
68  // loop over all dimensions
69  for (int i = 0; i < hist->GetNdimensions(); ++i) {
70  fMinBounds.push_back(0);
71  fMaxBounds.push_back(hist->GetAxis(i)->GetNbins());
72  }
73  }
74  fNumDimensions = fMinBounds.size();
77 }
78 
79 void NDimensionalExecutor::Execute(const std::function<void(const std::vector<int> & coords)> & func)
80 {
84 
85  if (fNumDimensions == 0) {
86  return;
87  }
88  fCurrentCoords = fMinBounds; // Reset state
89  do {
90  func(fCurrentCoords);
91  } while (Increment());
92 }
93 
94 } // namespace Ndmspc
std::vector< int > fMinBounds
Minimum bounds for each dimension.
std::vector< int > fMaxBounds
Maximum bounds for each dimension.
std::vector< int > fCurrentCoords
Current coordinates during iteration.
bool Increment()
Increment the current coordinates to the next point in the N-dimensional space.
NDimensionalExecutor(const std::vector< int > &minBounds, const std::vector< int > &maxBounds)
Constructor from min/max bounds for each dimension.
size_t fNumDimensions
Number of dimensions.
void Execute(const std::function< void(const std::vector< int > &coords)> &func)
Execute a function over all coordinates in the N-dimensional space.