Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RFlat2DMatrixOperators.hxx
Go to the documentation of this file.
1// Author: Martin Føll, University of Oslo (UiO) & CERN 1/2026
2
3/*************************************************************************
4 * Copyright (C) 1995-2026, Rene Brun and Fons Rademakers. *
5 * All rights reserved. *
6 * *
7 * For the licensing terms see $ROOTSYS/LICENSE. *
8 * For the list of contributors see $ROOTSYS/README/CREDITS. *
9 *************************************************************************/
10
11#ifndef TMVA_RFLAT2DMATRIXOPERATORS
12#define TMVA_RFLAT2DMATRIXOPERATORS
13
14#include <random>
15#include <algorithm>
16
18
20// clang-format off
21/**
22\class ROOT::TMVA::Experimental::Internal::RFlat2DMatrixOperators
23\ingroup tmva
24\brief Collection of operations applied to one or multiple flat 2D matrices.
25*/
26
28private:
29 // clang-format on
31 std::size_t fSetSeed;
32public:
33 RFlat2DMatrixOperators(bool shuffle = true, const std::size_t setSeed = 0)
36 {
37
38 }
39
41 {
42 if (fShuffle) {
43 std::random_device rd;
44 std::mt19937 g;
45
46 if (fSetSeed == 0) {
47 g.seed(rd());
48 } else {
49 g.seed(fSetSeed);
50 }
51
52 std::size_t rows = Tensor.GetRows();
53 std::size_t cols = Tensor.GetCols();
54 ShuffledTensor.Resize(rows, cols);
55
56 // make an identity permutation map
57 std::vector<Long_t> indices(rows);
58 std::iota(indices.begin(), indices.end(), 0);
59
60 // shuffle the identity permutation to create a new permutation
61 std::shuffle(indices.begin(), indices.end(), g);
62
63 // shuffle data in the tensor with the permutation map defined above
64 for (std::size_t i = 0; i < rows; i++) {
65 std::copy(Tensor.GetData() + indices[i] * cols,
66 Tensor.GetData() + (indices[i] + 1) * cols,
67 ShuffledTensor.GetData() + i * cols);
68 }
69 }
70 else {
72 }
73 }
74
76 const std::vector<std::vector<std::size_t>>& slice)
77 {
78 const auto& rowSlice = slice[0];
79 const auto& colSlice = slice[1];
80
81 std::size_t rowStart = rowSlice[0];
82 std::size_t rowEnd = rowSlice[1];
83 std::size_t colStart = colSlice[0];
84 std::size_t colEnd = colSlice[1];
85
86 std::size_t rows = rowEnd - rowStart;
87 std::size_t cols = colEnd - colStart;
88
89 SlicedTensor.Resize(rows, cols);
90 std::copy(Tensor.GetData() + rowStart * cols,
91 Tensor.GetData() + rowStart * cols + rows * cols,
92 SlicedTensor.GetData());
93 }
94
95 void ConcatenateTensors(RFlat2DMatrix &ConcatTensor, const std::vector<RFlat2DMatrix> &Tensors)
96 {
97 std::size_t cols = Tensors[0].GetCols();
98 std::size_t rows = 0;
99
100 for (const auto& t : Tensors) {
101 rows += t.GetRows();
102 }
103
104 ConcatTensor.Resize(rows, cols);
105
106 std::size_t index = 0;
107 for (std::size_t i = 0; i < Tensors.size(); i++) {
108 std::size_t tensorRows = Tensors[i].GetRows();
109 std::copy(Tensors[i].GetData(),
110 Tensors[i].GetData() + tensorRows * cols,
111 ConcatTensor.GetData() + index * cols);
112 index += tensorRows;
113 }
114 }
115
116
117};
118
119} // namespace TMVA::Experimental::Internal
120#endif // ROOT_TMVA_RFLAT2DMATRIXOPERATORS
#define g(i)
Definition RSha256.hxx:105
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
void ShuffleTensor(RFlat2DMatrix &ShuffledTensor, RFlat2DMatrix &Tensor)
void ConcatenateTensors(RFlat2DMatrix &ConcatTensor, const std::vector< RFlat2DMatrix > &Tensors)
void SliceTensor(RFlat2DMatrix &SlicedTensor, RFlat2DMatrix &Tensor, const std::vector< std::vector< std::size_t > > &slice)
RFlat2DMatrixOperators(bool shuffle=true, const std::size_t setSeed=0)
Wrapper around ROOT::RVec<float> representing a 2D matrix.