# Most efficient way to graph many ASCII files

**URL:** <https://root-forum.cern.ch/t/most-efficient-way-to-graph-many-ascii-files/45983>\
**Category:** Newbie\
**Created:** [July 22, 2021, 9:12pm UTC](https://root-forum.cern.ch/t/most-efficient-way-to-graph-many-ascii-files/45983 "2021-07-22T21:12:02Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Solar](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/77aa72/32.png) [@Solar](https://root-forum.cern.ch/u/Solar)\
**Post date:** [July 22, 2021, 9:12pm UTC](https://root-forum.cern.ch/t/most-efficient-way-to-graph-many-ascii-files/45983/1 "2021-07-22T21:12:02Z")

</div>

Hi there I am relatively new to ROOT, and I primarily do my coding in python, but my current job is all root and c++, so I would greatly appreciate some help.  
I have a folder with about 123 .txt files that have many different columns of data. I am trying to graph these files, but I have heard that just using TGraph every time I want to parse the columns of data is not a very good way to do this. Does anybody have any good suggestions? I can provide the code I currently have, just know that it is currently a work in progress and is unfinished

_ROOT Version:_ 5.34/38  
_Platform:_ Linux (Debian 64-bit  
_Compiler:_ Not Provided

* * *

---

<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:** [July 22, 2021, 10:53pm UTC](https://root-forum.cern.ch/t/most-efficient-way-to-graph-many-ascii-files/45983/2 "2021-07-22T22:53:35Z")

</div>

Hi and welcome!

You might try instead with TTree::ReadFile  
[https://root.cern.ch/doc/master/classTTree.html#a9c8da1fbc68221b31c21e55bddf72ce7](https://root.cern.ch/doc/master/classTTree.html#a9c8da1fbc68221b31c21e55bddf72ce7)

or just do the parsing yourself with C++:

```auto
ifstream f("myfile.txt");
if(!f.good()) return;
double x,y,z;
vector<double> xs, ys, zs;
while(f >> x >> y >> z)//assuming ascii file columns separated by spaces
{
    xs.push_back(x);
    ys.push_back(y);
    zs.push_back(z);
}

```

---

<div class="post-metadata">

**Author:** ![Solar](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/77aa72/32.png) [@Solar](https://root-forum.cern.ch/u/Solar)\
**Post date:** [July 22, 2021, 11:46pm UTC](https://root-forum.cern.ch/t/most-efficient-way-to-graph-many-ascii-files/45983/3 "2021-07-22T23:46:36Z")

</div>

This looks like it could work, but is there a way to iterate over the files I have in the directory without needing to hardcode each file name I am reading? I had seen that trees could fill plots with all of certain variables, but trees just looked more difficult to work with.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [July 23, 2021, 6:01am UTC](https://root-forum.cern.ch/t/most-efficient-way-to-graph-many-ascii-files/45983/4 "2021-07-23T06:01:39Z")

</div>

If your 123 .txt files have a kind of naming convention let’s say `foo1.txt`, `foo2.txt` … `foo123.txt` you can iterate over them using a loop, read them and fill a TTree. In the loop you will need to encode the file names using `Form`:

```auto
for (int i=1; i<=123; i++ {
   ifstream f(Form("foo$d.txt",i);
   ...
}

```

---

<div class="post-metadata">

**Author:** ![Solar](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/77aa72/32.png) [@Solar](https://root-forum.cern.ch/u/Solar)\
**Post date:** [July 23, 2021, 6:30am UTC](https://root-forum.cern.ch/t/most-efficient-way-to-graph-many-ascii-files/45983/5 "2021-07-23T06:30:14Z")

</div>

Yeah that would have been my first choice as well, but as you can see in the image, the only naming convention is that they all end in .txt. Any other thoughts?

 ![Screenshot (139)](https://root-forum.cern.ch/uploads/default/original/3X/3/4/345cb9a802e3c2fc77592ede97dacb33b9d8c171.png)

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [July 23, 2021, 6:37am UTC](https://root-forum.cern.ch/t/most-efficient-way-to-graph-many-ascii-files/45983/6 "2021-07-23T06:37:15Z")

</div>

They seems encoded somehow like: `ped.22xxxxxx.txt` . Do you know the logic behind the six digits `xxxxxx` ? a run number ? a time ? … something you can get from elsewhere… ?

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [July 23, 2021, 6:51am UTC](https://root-forum.cern.ch/t/most-efficient-way-to-graph-many-ascii-files/45983/7 "2021-07-23T06:51:47Z")

</div>

Maybe something like this:

```auto
   TSystemDirectory dir("~/fcs/qafiles", "~/fcs/qafiles");
   // get the list of files in the given directory
   TList *files = dir.GetListOfFiles();
   if (files) {
      TSystemFile *file;
      TString fname;
      TIter next(files);
      // now loop over the files
      while ((file=(TSystemFile*)next())) {
         fname = file->GetName();
         if (!file->IsDirectory() && fname.EndsWith(".txt") && 
              fname.BeginsWith("ped.")) {
            // this is a .txt file and its name begins with "ped."
            fname.Prepend(dirname);
            cout << "Processing: " << fname.Data() << endl;
            // so lets import its data
            ifstream f(fname.Data());
            ...
         }
      }
   }

```

This allows to add/remove files in `~/fcs/qafiles` without having to change the code.

---

<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:** [July 23, 2021, 7:54am UTC](https://root-forum.cern.ch/t/most-efficient-way-to-graph-many-ascii-files/45983/8 "2021-07-23T07:54:03Z")

</div>

If you can use C++17, you can also try:  
[https://en.cppreference.com/w/cpp/filesystem/directory\_iterator](https://en.cppreference.com/w/cpp/filesystem/directory_iterator)

> <https://stackoverflow.com/questions/40633529/how-to-list-all-the-files-with-given-extension-from-directory-using-filesystem>

and combine the ‘if-check’ it with std::starts\_with()

---

<div class="post-metadata">

**Author:** ![Solar](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/77aa72/32.png) [@Solar](https://root-forum.cern.ch/u/Solar)\
**Post date:** [July 26, 2021, 3:06pm UTC](https://root-forum.cern.ch/t/most-efficient-way-to-graph-many-ascii-files/45983/9 "2021-07-26T15:06:31Z")

</div>

Sorry for the late reply but thanks! This in conjunction with adding the files to an array solved my problem!

---

<div class="post-metadata">

**Author:** ![Solar](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/77aa72/32.png) [@Solar](https://root-forum.cern.ch/u/Solar)\
**Post date:** [July 26, 2021, 3:07pm UTC](https://root-forum.cern.ch/t/most-efficient-way-to-graph-many-ascii-files/45983/10 "2021-07-26T15:07:05Z")

</div>

This was very helpful for parsing the files, thank you.

---

<div class="post-metadata">

**Author:** ![Solar](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/77aa72/32.png) [@Solar](https://root-forum.cern.ch/u/Solar)\
**Post date:** [July 28, 2021, 5:23am UTC](https://root-forum.cern.ch/t/most-efficient-way-to-graph-many-ascii-files/45983/11 "2021-07-28T05:23:14Z")

</div>

Actually, I have one quick follow up. I just assumed honestly that this code work, but I have been having some issues with the fname.Prepend(dirname) line. I do not see where dirname is being defined, am I missing something? Thanks

---

<div class="post-metadata">

**Author:** ![Solar](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/77aa72/32.png) [@Solar](https://root-forum.cern.ch/u/Solar)\
**Post date:** [July 28, 2021, 5:33am UTC](https://root-forum.cern.ch/t/most-efficient-way-to-graph-many-ascii-files/45983/12 "2021-07-28T05:33:38Z")

</div>

Also after doing some bug hunting, it seems that I am not entering the first if statement, if (files). If sending my current code would help I would be more than happy to

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [July 28, 2021, 6:41am UTC](https://root-forum.cern.ch/t/most-efficient-way-to-graph-many-ascii-files/45983/13 "2021-07-28T06:41:04Z")

</div>

Right, replace `dirname` by the name of the directory containing the files (e.g. `~/fcs/qafiles`). and note that you might replace the `~` with the full path (your ` $(HOME}`) everywhere (also in `TSystemDirectory dir("~/fcs/qafiles", "~/fcs/qafiles");`)
