# Root macro to retrieve and copy in a csv file the number of photo-electrons from a root file

**URL:** <https://root-forum.cern.ch/t/root-macro-to-retrieve-and-copy-in-a-csv-file-the-number-of-photo-electrons-from-a-root-file/57189>\
**Category:** Newbie\
**Tags:** root\
**Created:** [November 21, 2023, 11:09am UTC](https://root-forum.cern.ch/t/root-macro-to-retrieve-and-copy-in-a-csv-file-the-number-of-photo-electrons-from-a-root-file/57189 "2023-11-21T11:09:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SarahBlind](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/6bbea6/32.png) [@SarahBlind](https://root-forum.cern.ch/u/SarahBlind)\
**Post date:** [November 21, 2023, 11:09am UTC](https://root-forum.cern.ch/t/root-macro-to-retrieve-and-copy-in-a-csv-file-the-number-of-photo-electrons-from-a-root-file/57189/1 "2023-11-21T11:09:49Z")

</div>

Dear Root users,

I am using GATE (an open source software dedicated to digital simulations in medical imaging and radiotherapy) to analyze some radio physical quantities (photo-electrons for example) after an X-rays irradiation simulation. I’m using root 6.28/08 on Ubuntu 20.04.6 LTS.

My GATE simulations give rise to .root files in which I can found the number of photo-electrons produced.

I know how to recover this number “by hand” in the T Browser :

 ![tbrowser](https://root-forum.cern.ch/uploads/default/original/3X/4/8/485414118eb8e0abb88d290f0be1635e6d141d89.png)

or through the root prompt :

 ![root_prompt](https://root-forum.cern.ch/uploads/default/original/3X/2/2/2252137ebca5963b541c396c14644d72f42f882e.png)

But I would like to make a root macro that goes through each root files, recover and copy the number of photo-electrons produced for each root file into an ods or csv file.

I know how to open my root file :

> std::unique\_ptr myFile( TFile::Open(“phaseSpaceCluster\_IN.root”) );

but I can’t manage to find how to retrieve the number of photo-electrons…  
In other words, how can I implement the root prompt command line

> PhaseSpace-\>Draw(“Ekine”,“CreatorProcess=="PhotoElectric"”)

into a root macro ? Does anyone have any tips to help me?

Many thanks for your help!

Sarah

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [November 21, 2023, 12:39pm UTC](https://root-forum.cern.ch/t/root-macro-to-retrieve-and-copy-in-a-csv-file-the-number-of-photo-electrons-from-a-root-file/57189/2 "2023-11-21T12:39:45Z")

</div>

You can read the tree with an RDataFrame and use Filter() and Count():

```auto
void test() {
  ROOT::RDataFrame d("PhaseSpace", "phaseSpaceCluster_IN.root");
  auto n = d.Filter("CreatorProcess==\"PhotoElectric\"").Count();
  cout << *n << endl;
}

```

(save as test.C)  
Note the `*n`, since [Count()](https://root.cern/doc/master/classROOT_1_1RDF_1_1RInterface.html#a876bfce418c82a93caf2b143c9c08704) returns a pointer.

More on dataframes:

> **[Dataframes](https://root.cern/manual/data_frame/)**
>
> Data Analysis Framework

[https://root.cern/doc/master/classROOT\_1\_1RDataFrame.html](https://root.cern/doc/master/classROOT_1_1RDataFrame.html)

---

<div class="post-metadata">

**Author:** ![SarahBlind](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/6bbea6/32.png) [@SarahBlind](https://root-forum.cern.ch/u/SarahBlind)\
**Post date:** [November 21, 2023, 2:26pm UTC](https://root-forum.cern.ch/t/root-macro-to-retrieve-and-copy-in-a-csv-file-the-number-of-photo-electrons-from-a-root-file/57189/3 "2023-11-21T14:26:47Z")

</div>

Thank you so much for your response dastudillo!

While I’m reading the dataframes info you suggested, here is the error I obtain when running your code:

 ![tbrowser2](https://root-forum.cern.ch/uploads/default/original/3X/4/5/455daf03e78241aaf1dd6a2142b4c5e279a17a9c.png)

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [November 21, 2023, 4:58pm UTC](https://root-forum.cern.ch/t/root-macro-to-retrieve-and-copy-in-a-csv-file-the-number-of-photo-electrons-from-a-root-file/57189/4 "2023-11-21T16:58:19Z")

</div>

Check out this thread:

> [@Add a String Filter in Datafram](https://root-forum.cern.ch/t/add-a-string-filter-in-datafram/46200):
>
> ROOT Version: 6.24/02 Platform: Scientific Linux 7.3 (Nitrogen) Compiler: gcc 4.8.5(Red Hat 4.8.5-28) I am using RDataFrame to analyze some data in a root file, which involves the filtering of string data. i.e. auto Si = d.Filter("ParticleName == \"proton\"").Histo1D({"Si","Si",4000,0,400},"particleEnergy"); That is to filter out the energy of protons. However, there is a problem with the above filter statement, and the error is as follows: /opt/root62402/include/ROOT/RVec.hxx:609:23: er…

---

<div class="post-metadata">

**Author:** ![SarahBlind](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/6bbea6/32.png) [@SarahBlind](https://root-forum.cern.ch/u/SarahBlind)\
**Post date:** [November 22, 2023, 6:57am UTC](https://root-forum.cern.ch/t/root-macro-to-retrieve-and-copy-in-a-csv-file-the-number-of-photo-electrons-from-a-root-file/57189/5 "2023-11-22T06:57:45Z")

</div>

Thank you so much dastudillo, it works !
