# How can I save values to .txt file with RDataFrame?

**URL:** <https://root-forum.cern.ch/t/how-can-i-save-values-to-txt-file-with-rdataframe/51118>\
**Category:** Newbie\
**Created:** [August 9, 2022, 4:49pm UTC](https://root-forum.cern.ch/t/how-can-i-save-values-to-txt-file-with-rdataframe/51118 "2022-08-09T16:49:15Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![beg1nner](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/d26b3c/32.png) [@beg1nner](https://root-forum.cern.ch/u/beg1nner)\
**Post date:** [August 9, 2022, 4:49pm UTC](https://root-forum.cern.ch/t/how-can-i-save-values-to-txt-file-with-rdataframe/51118/1 "2022-08-09T16:49:16Z")

</div>

Dear experts,

I am trying to save to the .txt file values of basic variables, such as event and run number for cases, when the transverse mass is bigger than 90. How can one do that?  
I was thinking about a filter, but I do not know what do I do after. F.g.

```auto
auto interesting_evts = df.Filter( [] (const float& mt, const UInt_t& run, const ULong64_t& event){
    if ( mt > 90 ) { 
      DO SOMETHING TO SAVE EVENT AND RUN NUMBERS (but how?)
    }
}, {"mt", "run", "event"});

```

I am not sure if " auto " is a right approach here, because it has to be called somewhere so that it gets “activated” and I do not call it anywhere…

What I want to have as an outcome is a txt file like:

MT EVENT RUN  
95 2133 12948  
… … …

Thank you in advance.

Best,  
Newbie

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [August 10, 2022, 8:21am UTC](https://root-forum.cern.ch/t/how-can-i-save-values-to-txt-file-with-rdataframe/51118/2 "2022-08-10T08:21:43Z")

</div>

You can use an open `std::ofstream` to write to a txt file within the lambda.

Maybe also:  
[https://root.cern/doc/master/classROOT\_1\_1RDF\_1\_1RInterface.html#a7fb6ccf99d69bee7c519dcfe25bfa16b](https://root.cern/doc/master/classROOT_1_1RDF_1_1RInterface.html#a7fb6ccf99d69bee7c519dcfe25bfa16b)

---

<div class="post-metadata">

**Author:** ![beg1nner](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/d26b3c/32.png) [@beg1nner](https://root-forum.cern.ch/u/beg1nner)\
**Post date:** [August 10, 2022, 10:30am UTC](https://root-forum.cern.ch/t/how-can-i-save-values-to-txt-file-with-rdataframe/51118/3 "2022-08-10T10:30:30Z")

</div>

I can use an open std::ofstream, but since my “interesting\_evts” is not called anywhere, it does nothing. So, for example, if I do

```auto
auto interesting_evts = df.Filter( [] (const float& mt, const UInt_t& run, const ULong64_t& event){
    if ( mt > 90 ) { 
      std::cout << " run " << run << " event " << event << std::endl;
    }
}, {"mt", "run", "event"});

```

It does not print out anything, because the function is not called. So, I am curious how should one call it so that it prints out something.

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [August 10, 2022, 10:44am UTC](https://root-forum.cern.ch/t/how-can-i-save-values-to-txt-file-with-rdataframe/51118/4 "2022-08-10T10:44:22Z")

</div>

Can you post of the result of:

`std::cout << interesting_evts.Count() << " entries passed all filters" << std::endl;`

---

<div class="post-metadata">

**Author:** ![beg1nner](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/d26b3c/32.png) [@beg1nner](https://root-forum.cern.ch/u/beg1nner)\
**Post date:** [August 10, 2022, 11:02am UTC](https://root-forum.cern.ch/t/how-can-i-save-values-to-txt-file-with-rdataframe/51118/5 "2022-08-10T11:02:25Z")

</div>

So now I have

```auto
auto interesting_evts = df.Filter( [] (const float& mt, const UInt_t& run, const ULong64_t& event){
    if ( mt > 90 ) { 
      std::cout << " run " << run << " event " << event << std::endl;
    }
}, {"mt", "run", "event"});

std::cout << interesting_evts.Count() << " entries passed all filters" << std::endl; 

```

and I get a compilation error : " static assertion failed: filter expression returns a type that is not convertable to bool  
`static_assert(std::is_convertible<FilterRet_t, bool>::value,`

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [August 10, 2022, 11:16am UTC](https://root-forum.cern.ch/t/how-can-i-save-values-to-txt-file-with-rdataframe/51118/6 "2022-08-10T11:16:28Z")

</div>

The Lambda filter you defined should return a boolean.  
`if(mt>90) { .... return true;} else return false;`

See here how to properly define filters.  
[https://root.cern.ch/doc/master/df001\_\_introduction\_8C.html](https://root.cern.ch/doc/master/df001__introduction_8C.html)

---

<div class="post-metadata">

**Author:** ![beg1nner](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/d26b3c/32.png) [@beg1nner](https://root-forum.cern.ch/u/beg1nner)\
**Post date:** [August 10, 2022, 11:27am UTC](https://root-forum.cern.ch/t/how-can-i-save-values-to-txt-file-with-rdataframe/51118/7 "2022-08-10T11:27:49Z")

</div>

Well, when I define it as follows :

```auto
auto interesting_evts = df.Filter( [] (const float& mt, const UInt_t& run, const ULong64_t& event){
    if ( mt > 90 ) { 
      return true;
    }
 return false;
}, {"mt", "run", "event"});

std::cout << interesting_evts.Count() << " entries passed all filters" << std::endl; 

```

It still gives me a compilation error `no match for " operator<<" (operand types are "std::ostream" {aka "std::basic_ostream<char>"} and "ROOT::RDF::RResultPtr<long long unsigned int>")`  
and note `no known conversion for argument 2 from "ROOT::RDF::RResultPtr<long long unsigned int>" to "int"`

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [August 10, 2022, 11:40am UTC](https://root-forum.cern.ch/t/how-can-i-save-values-to-txt-file-with-rdataframe/51118/8 "2022-08-10T11:40:02Z")

</div>

Use instead:

```auto
(*(interesting_evts.Count()))

```

---

<div class="post-metadata">

**Author:** ![beg1nner](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/d26b3c/32.png) [@beg1nner](https://root-forum.cern.ch/u/beg1nner)\
**Post date:** [August 10, 2022, 12:00pm UTC](https://root-forum.cern.ch/t/how-can-i-save-values-to-txt-file-with-rdataframe/51118/9 "2022-08-10T12:00:37Z")

</div>

It worked and the output is that " 20 entries passed all filters ".  
It gave me some hope, how can I get the " event " and " run " information for those entries?

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [August 10, 2022, 12:33pm UTC](https://root-forum.cern.ch/t/how-can-i-save-values-to-txt-file-with-rdataframe/51118/10 "2022-08-10T12:33:40Z")

</div>

Reintroduce

```auto
std::cout << " run " << run << " event " << event << std::endl;

```

just before  
`return true;`

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [August 10, 2022, 12:41pm UTC](https://root-forum.cern.ch/t/how-can-i-save-values-to-txt-file-with-rdataframe/51118/11 "2022-08-10T12:41:11Z")

</div>

You can also follow the example of the link above.

namely using the function [Take()](https://root.cern/doc/master/classROOT_1_1RDF_1_1RInterface.html#aa571f08b8e4d9300cacdea039a379a22) or [Foreach()](https://root.cern/doc/master/classROOT_1_1RDF_1_1RInterface.html#ad2822a7ccb8a9afdf3e5b2ea321886ca).

Or you can use [Snapshot()](https://root.cern/doc/master/classROOT_1_1RDF_1_1RInterface.html#ac5903d3acec8c52f13cbd405371f7fb7) to save a TTree, and then [export it as CSV](https://root-forum.cern.ch/t/root-tree-to-csv-file-format/27633/4)

---

<div class="post-metadata">

**Author:** ![beg1nner](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/d26b3c/32.png) [@beg1nner](https://root-forum.cern.ch/u/beg1nner)\
**Post date:** [August 10, 2022, 1:57pm UTC](https://root-forum.cern.ch/t/how-can-i-save-values-to-txt-file-with-rdataframe/51118/12 "2022-08-10T13:57:30Z")

</div>

Thank you for your replies.

`Foreach` does exactly what I want:

```auto
df.Foreach([](const float& mt, const UInt_t& run, const ULong64_t& event){
 if ( mt > 90 ) { 
      std::cout << " run " << run << " event " << event << std::endl;
    }
}, {"mt", "run", "event"});

```

Then I do not need to have a bool, it’s easy and short 😄 Thanks again!

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [August 11, 2022, 5:49pm UTC](https://root-forum.cern.ch/t/how-can-i-save-values-to-txt-file-with-rdataframe/51118/13 "2022-08-11T17:49:16Z")

</div>

Hi,

I’m late to the party but I’d like to suggest a slight rewrite of the basic approach you converged to, which is completely right:

- it might be more readable to separate the logic into a `Filter` for selecting events, a `Foreach` for printing them
- variables of fundamental types are [usually passed by value](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rf-in) in C++ (it’s faster and more readable)
- `std::endl` is sometimes [slower than just a `\n'`](https://stackoverflow.com/questions/35580919/should-stdendl-always-be-used)

```cpp
auto printEvent = [](UInt_t r, ULong64_t e) {
  std::cout << r << ',' << e << '\n';
}; 

std::cout << "run,event\n"; // print CSV file header
df.Filter("mt > 90")
  .Foreach(printEvent, {"run", "event"});

```

Cheers,  
Enrico
