Home | News | Documentation | Download

Macro crashing without any error

___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.

#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


Can you provide the input data file(s) ?

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

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

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 (39.5 KB)

So your code is:

{
        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.

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.

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).

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.

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

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

1 Like