joa
January 18, 2022, 1:58pm
#1
Dear all,

some info…

ROOT Version: 6.23/01
Platform: ubuntu 20.04
Compiler: g++ (Ubuntu 9.4.0-1ubuntu1~20.04) 9.4.0

then the problem

I’m trying to understand how to write something like
(gammas is a vector stored in a tree)

```
for(auto &it : gammas){
for(auto &it2 : gammas){
if(it2!=it){
A2DMatrix.Fill(it,it2);
}
}
```

using a RDataFrame but can not quite figure out how to loop over interations in an event using RDataFrames.

best regards

Joa

couet
January 18, 2022, 2:16pm
#2
I think @eguiraud can help you.

Hi @joa ,
in first approximation you can write exactly that code:

```
TMatrix A2DMatrix;
auto fillA2DMatrix = [&] (const ROOT::RVec<float> &gammas) {
for(auto &it : gammas){
for(auto &it2 : gammas){
if(it2!=it){
A2DMatrix.Fill(it,it2);
}
}
}
};
df.Foreach(fillA2DMatrix, {"gammas"});
```

This only works on a single thread, of course.
Depending on your exact usecase we can probably also implement something that works in a multi-thread run, but I hope this can get you started.

Cheers,
Enrico

joa
January 18, 2022, 2:46pm
#4
Hi,

Ok. So for large data sets I’ll be better off with a TSelector and proof…?
This is not threadsafe because of the Fill in TMatrix, correct?

cheers

Joa

Correct.

There are some simple ways to make this thread-safe, for example (again, just to give you an idea, the best solution depends on your exact problem):

```
ROOT::EnableImplicitMT();
ROOT::RDataFrame df(...);
std::vector<Matrix2D> matrices(df.GetNSlots()); // a vector of nThreads matrices
df.ForeachSlot([&matrices](unsigned int slot, const ROOT::RVec<float> &gammas) {
for(auto &it : gammas){
for(auto &it2 : gammas){
if(it2!=it)
matrices[slot].Fill(it,it2);
}
}
}, {"gammas"});
// ...merge matrices...
```

See also the ForeachSlot docs .

If you can use 2D histograms as 2D matrices the `Histo2D`

method can be used and it takes care of merging the results of each thread under the hood.

Cheers,
Enrico

joa
January 18, 2022, 3:04pm
#6
Hi again,

Ok, I think I got it. Thanks

cheers

Joa

system
closed
February 1, 2022, 3:05pm
#7
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.