# Large loop on txt files

**URL:** <https://root-forum.cern.ch/t/large-loop-on-txt-files/39684>\
**Category:** Newbie\
**Created:** [May 30, 2020, 8:35pm UTC](https://root-forum.cern.ch/t/large-loop-on-txt-files/39684 "2020-05-30T20:35:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![florian\_Gautier](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/florian_gautier/32/20809_2.png) [@florian\_Gautier](https://root-forum.cern.ch/u/florian_Gautier)\
**Post date:** [May 30, 2020, 8:35pm UTC](https://root-forum.cern.ch/t/large-loop-on-txt-files/39684/1 "2020-05-30T20:35:59Z")

</div>

Hi,  
I am trying to loop over txt files in directory and write them into root files (one root files can include several txt files when events of same specs).  
While my code is working it takes a huge time to compile when executed through “root -l txttoroot.C” (I get “Processing txttoroot.C…”)  
Here is my current code[txttoroot.C](https://root-forum.cern.ch/uploads/short-url/sVyTEQBlRm37CLeZvJ34i9GWuPt.C) (1.9 KB)  
I think it should come from the allocation when creating the histograms, I might not understand well if I am creating memory loss through wrong free. I think closing the files written was enough.

Could I get advice on this? Even a better way to do this .txt to .root transformation.

Thanks  
Florian

* * *

\_ROOT Version:6.20/04  
\_Platform: Centos  
\_Compiler: gcc version 4.8.5 20150623 (Red Hat 4.8.5-39)

* * *

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [May 31, 2020, 9:16am UTC](https://root-forum.cern.ch/t/large-loop-on-txt-files/39684/2 "2020-05-31T09:16:08Z")

</div>

Instead of `data->Close();` use `delete data; // automatically deletes hist_L, too`

---

<div class="post-metadata">

**Author:** ![florian\_Gautier](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/florian_gautier/32/20809_2.png) [@florian\_Gautier](https://root-forum.cern.ch/u/florian_Gautier)\
**Post date:** [June 1, 2020, 5:28pm UTC](https://root-forum.cern.ch/t/large-loop-on-txt-files/39684/3 "2020-06-01T17:28:23Z")

</div>

Thanks for this advice.  
It seems it still takes a huge time to start pass the “Processing” stage (Is this message marking the compilation?)

I am processing 10,000 files of 1.3MB. I tried to do it with less files and it seems to be faster to compile. I don’t understand why this compilation should be that long when it is just looping over files in a folder.

---

<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:** [June 1, 2020, 5:35pm UTC](https://root-forum.cern.ch/t/large-loop-on-txt-files/39684/4 "2020-06-01T17:35:07Z")

</div>

Hi @florian_Gautier,  
the time difference is definitely not due to a difference in compile/interpret times. It might have to do with [Slow down reading many TFiles](https://root-forum.cern.ch/t/slow-down-reading-many-tfiles/39512) (@pcanal might be able to comment).

You can check where the program spends time e.g. with [perf record and perf report](https://perf.wiki.kernel.org/index.php/Tutorial#Sampling_with_perf_record), or with the [poor man’s profiler](https://poormansprofiler.org/), which works very well when there is a single callstack that occupies most of the time, as it is probably the case here.

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![florian\_Gautier](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/florian_gautier/32/20809_2.png) [@florian\_Gautier](https://root-forum.cern.ch/u/florian_Gautier)\
**Post date:** [June 1, 2020, 6:11pm UTC](https://root-forum.cern.ch/t/large-loop-on-txt-files/39684/5 "2020-06-01T18:11:26Z")

</div>

Thanks, I will try to run this perf commands and provide the output.

A difference I noted compared to the other thread you mention is that I don’t see a slowdown of my process during the execution but immediately. As long as too many files are present, then the starting of process is really long (My code process should output the current file being read each loop)

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [June 1, 2020, 6:45pm UTC](https://root-forum.cern.ch/t/large-loop-on-txt-files/39684/6 "2020-06-01T18:45:03Z")

</div>

Can you try to modify the beginning of your macro and observe how it behaves (i.e. how fast it prints the “debug output”):

```cpp
void txttoroot()
{
  std::cout << "txttoroot ..." << std::endl << std::flush;
  const char *dirname="/media/sf_Partag_virtualbox/Ampli/Test";
  std::cout << "... pwd" << std::endl << std::flush;
  TString pwd(gSystem->pwd()); //Get a current directory
  std::cout << "... dir" << std::endl << std::flush;
  TSystemDirectory dir(dirname, dirname);
  std::cout << "... files" << std::endl << std::flush;
  TList *files = dir.GetListOfFiles();
  std::cout << "files ..." << std::endl << std::flush;
  // ...
    if (files)
    {
     std::cout << "... next" << std::endl << std::flush;
     TSystemFile *file; TString fname,fname2; TIter next(files);
     std::cout << "next ..." << std::endl << std::flush;
     // ...

```

---

<div class="post-metadata">

**Author:** ![florian\_Gautier](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/florian_gautier/32/20809_2.png) [@florian\_Gautier](https://root-forum.cern.ch/u/florian_Gautier)\
**Post date:** [June 4, 2020, 8:51pm UTC](https://root-forum.cern.ch/t/large-loop-on-txt-files/39684/7 "2020-06-04T20:51:08Z")

</div>

> [flo@Scientific AmplifiedL]$ root -l txttoroot.C  
> root [0]  
> Processing txttoroot.C…  
> txttoroot …  
> … pwd  
> … dir  
> … files  
> files …  
> … next  
> next …  
> out\_config300\_ion\_14\_28\_14\_5MeV\_nucl\_0\_deg\_10000\_8\_20200529\_210423.txt

@Wile_E_Coyote, I implemented your debug output and the huge delay happens after `Processing txttoroot.C...` and before `txttoroot ...`

Next, I will try to look at how perf report works and try to apply it.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [June 4, 2020, 9:04pm UTC](https://root-forum.cern.ch/t/large-loop-on-txt-files/39684/8 "2020-06-04T21:04:02Z")

</div>

So the huge delay seems to be unrelated to your macro (it happens before its first line is executed).  
Just to make sure … can you try two command lines:  
`root -b -n -l txttoroot.C`  
`root.exe -b -n -l txttoroot.C`

---

<div class="post-metadata">

**Author:** ![florian\_Gautier](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/florian_gautier/32/20809_2.png) [@florian\_Gautier](https://root-forum.cern.ch/u/florian_Gautier)\
**Post date:** [June 4, 2020, 9:56pm UTC](https://root-forum.cern.ch/t/large-loop-on-txt-files/39684/9 "2020-06-04T21:56:55Z")

</div>

It did not change time the delay.
