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 std::random_device rd;
42 std::mt19937 g;
43
44 if (fSetSeed == 0) {
45 g.seed(rd());
46 } else {
47 g.seed(fSetSeed);
48 }
49
50 std::size_t rows = Tensor.GetRows();
51 std::size_t cols = Tensor.GetCols();
52 ShuffledTensor.Resize(rows, cols);
53
54 // make an identity permutation map
55 std::vector<Long_t> indices(rows);
56 std::iota(indices.begin(), indices.end(), 0);
57
58 // shuffle the identity permutation to create a new permutation
59 if (fShuffle) {
60 std::shuffle(indices.begin(), indices.end(), g);
61 }
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
72 const std::vector<std::vector<std::size_t>>& slice)
73 {
74 const auto& rowSlice = slice[0];
75 const auto& colSlice = slice[1];
76
77 std::size_t rowStart = rowSlice[0];
78 std::size_t rowEnd = rowSlice[1];
79 std::size_t colStart = colSlice[0];
80 std::size_t colEnd = colSlice[1];
81
82 std::size_t rows = rowEnd - rowStart;
83 std::size_t cols = colEnd - colStart;
84
85 SlicedTensor.Resize(rows, cols);
86 std::copy(Tensor.GetData() + rowStart * cols,
87 Tensor.GetData() + rowStart * cols + rows * cols,
88 SlicedTensor.GetData());
89 }
90
91 void ConcatenateTensors(RFlat2DMatrix &ConcatTensor, std::vector<RFlat2DMatrix> &Tensors)
92 {
93 std::size_t cols = Tensors[0].GetCols();
94 std::size_t rows = 0;
95
96 for (const auto& t : Tensors) {
97 rows += t.GetRows();
98 }
99
100 ConcatTensor.Resize(rows, cols);
101
102 std::size_t index = 0;
103 for (std::size_t i = 0; i < Tensors.size(); i++) {
104 std::size_t tensorRows = Tensors[i].GetRows();
105 std::copy(Tensors[i].GetData(),
106 Tensors[i].GetData() + tensorRows * cols,
107 ConcatTensor.GetData() + index * cols);
108 index += tensorRows;
109 }
110 }
111
112
113};
114
115} // namespace TMVA::Experimental::Internal
116#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, 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.