# Macro crashing without any error

**URL:** <https://root-forum.cern.ch/t/macro-crashing-without-any-error/47767>\
**Category:** Newbie\
**Created:** [November 22, 2021, 6:40pm UTC](https://root-forum.cern.ch/t/macro-crashing-without-any-error/47767 "2021-11-22T18:40:48Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![damandeep\_kaur](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/damandeep_kaur/32/27949_2.png) [@damandeep\_kaur](https://root-forum.cern.ch/u/damandeep_kaur)\
**Post date:** [November 22, 2021, 6:40pm UTC](https://root-forum.cern.ch/t/macro-crashing-without-any-error/47767/1 "2021-11-22T18:40:48Z")

</div>

\_\_\_I am trying to get average of number of events between particular range saved in a text file. Every event has 1024 values. My macro is crashing after giving some output averages without error. After giving some output values, all values are coming zero. Here is my code.

```auto
#include <stdio.h>

#include <stdlib.h>

#include <iostream>

#include <fstream>

#include <sstream>

#include <string.h>

#include <string>

#include <vector>

#include <dirent.h>

#include <sys/types.h>

using namespace std;

void avg(){

   

        string line;

        float time;

       

        ifstream file("wave_7.txt");

        ofstream MyFile("filename.txt");

    for(int e=0;e<=1831;e++)

    {

        int l = 1032*e + 9;

        for(int i=1;i<l;i++){

            getline(file,line);

        }

       

        time = 0;

        double sum=0.0;

        int num=0;

        double avg=0.0;

        for(int j=1;j<=1024;j++){

            getline(file,line);

            if(line != "")

            {

                double dataPoint = std::stod(line);

                if(time>=10 && time<=50)

                {

                    sum=sum+dataPoint;

                    num=num+1;

                }

                

                time += 0.2;

                if(time>50)

                {

                    avg=sum/num;

                    break;

                }

            }

           

        }

        

        //MyFile << s;

        cout<<avg<<"\n";

        }

          // Close the file

        MyFile.close();

   

}

```

Do i need to add any debug statement? if yes, how and which statement should i add to run it? Please solve this issue.

_ROOT Version:_ 6.24/02  
_Platform:_ WSL  
_Compiler:_ Not Provided

* * *

---

<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:** [November 23, 2021, 1:39pm UTC](https://root-forum.cern.ch/t/macro-crashing-without-any-error/47767/2 "2021-11-23T13:39:46Z")

</div>

Can you provide the input data file(s) ?

---

<div class="post-metadata">

**Author:** ![damandeep\_kaur](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/damandeep_kaur/32/27949_2.png) [@damandeep\_kaur](https://root-forum.cern.ch/u/damandeep_kaur)\
**Post date:** [November 23, 2021, 9:47pm UTC](https://root-forum.cern.ch/t/macro-crashing-without-any-error/47767/3 "2021-11-23T21:47:57Z")

</div>

My input file is too big to upload here. is there any other option to share with you?

---

<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:** [November 24, 2021, 7:47am UTC](https://root-forum.cern.ch/t/macro-crashing-without-any-error/47767/4 "2021-11-24T07:47:03Z")

</div>

May be you can reduce it ? do you get the crash with smaller version of it ?

---

<div class="post-metadata">

**Author:** ![damandeep\_kaur](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/damandeep_kaur/32/27949_2.png) [@damandeep\_kaur](https://root-forum.cern.ch/u/damandeep_kaur)\
**Post date:** [November 24, 2021, 4:31pm UTC](https://root-forum.cern.ch/t/macro-crashing-without-any-error/47767/5 "2021-11-24T16:31:08Z")

</div>

i am sending you a input file with only 3 events in it. But the actual file has 1831 events. I am not able to reduce the size of a .txt file.[wave\_7.txt](https://root-forum.cern.ch/uploads/short-url/fwU9fE0yGaR42NPy1r08QfrLfo5.txt) (39.5 KB)

---

<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:** [November 24, 2021, 5:00pm UTC](https://root-forum.cern.ch/t/macro-crashing-without-any-error/47767/6 "2021-11-24T17:00:50Z")

</div>

So your code is:

```auto
{
        string line;
        float time;

        ifstream file("wave_7.txt");
        ofstream MyFile("filename.txt");

    for(int e=0;e<=1831;e++) {
        int l = 1032*e + 9;
        for(int i=1;i<l;i++){
            getline(file,line);
        }

        time = 0;
        double sum=0.0;
        int num=0;
        double avg=0.0;
        for(int j=1;j<=1024;j++){
            getline(file,line);
            if(line != "")
            {
                double dataPoint = std::stod(line);
                if(time>=10 && time<=50)
                {
                    sum=sum+dataPoint;
                    num=num+1;
                }
                time += 0.2;
                if(time>50)
                {
                    avg=sum/num;
                    break;
                }
            }
        }
        //MyFile << s;
    /// cout<<avg<<"\n";
        }
        MyFile.close();
}

```

I do not understand your logic … it seems to loop forever.

---

<div class="post-metadata">

**Author:** ![damandeep\_kaur](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/damandeep_kaur/32/27949_2.png) [@damandeep\_kaur](https://root-forum.cern.ch/u/damandeep_kaur)\
**Post date:** [November 24, 2021, 5:06pm UTC](https://root-forum.cern.ch/t/macro-crashing-without-any-error/47767/7 "2021-11-24T17:06:38Z")

</div>

first for loop from 0 to 1831 is for all the 1832 events and second for loop from 1 to 1024 is to read 1024 values for each event.

---

<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 24, 2021, 5:12pm UTC](https://root-forum.cern.ch/t/macro-crashing-without-any-error/47767/8 "2021-11-24T17:12:04Z")

</div>

> [@damandeep\_kaur](#):
>
> ```auto
> int l = 1032*e + 9;
> for(int i=1;i<l;i++){
> getline(file,line);
> }
> 
> ```

Looks like your problem is with C++, not ROOT. This loop should only go over 8 lines, according to the file you uploaded; then check the rest of the code (I didn’t check but there may be other issues).

---

<div class="post-metadata">

**Author:** ![damandeep\_kaur](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/damandeep_kaur/32/27949_2.png) [@damandeep\_kaur](https://root-forum.cern.ch/u/damandeep_kaur)\
**Post date:** [November 24, 2021, 5:14pm UTC](https://root-forum.cern.ch/t/macro-crashing-without-any-error/47767/9 "2021-11-24T17:14:28Z")

</div>

if you can see my input file, there are some lines which i need to omit for reading the values. This loop is for ignoring these lines.

---

<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 24, 2021, 5:17pm UTC](https://root-forum.cern.ch/t/macro-crashing-without-any-error/47767/10 "2021-11-24T17:17:21Z")

</div>

Exactly. Read again and check your logic. as couet suggested.

---

<div class="post-metadata">

**Author:** ![damandeep\_kaur](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/damandeep_kaur/32/27949_2.png) [@damandeep\_kaur](https://root-forum.cern.ch/u/damandeep_kaur)\
**Post date:** [November 24, 2021, 5:31pm UTC](https://root-forum.cern.ch/t/macro-crashing-without-any-error/47767/11 "2021-11-24T17:31:45Z")

</div>

yeah i found my mistake and corrected it. Thank you!
