Reading text files with more than 100,000 lines


ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


Hi,
I have written a code to read a text file (line by line) and create histograms. The code works pretty well for files with less then 100,000 lines. But it doesn’t work for more than 100,000 lines. each line contains integer numbers including event number, row, column, trigger,etc. The code reads each line and stores the numbers (information for each event) in structures (struct hits {…} ). I am new to root and root forum and Your help is greatly appreciated.

You need to attach your source code for inspection.

Hi Wile_E_Coyote,
Thanks for the response. Here is my code. I tried to read the input file in two different ways (Commented section) but non worked. I only upload part of the code since it’s very long. Please let me know if I need to add the rest. The rest of the code is just to do some calculations and create histograms.
Thanks!

//*****************************************************************
#include <stdio.h>
#include <string.h>
#include "TFile.h"
#include "TTree.h"
#include "TF1.h"
#include "TH2.h"
#include "TH1D.h"
#include "TCut.h"
#include "TMath.h"
#include "TPad.h"
#include "TAxis.h"
#include "TGaxis.h"
#include "TCanvas.h"
#include "TGraph.h"
#include <fstream>


struct HitInfo {
  unsigned long int Event_Number;
  unsigned long int Ext_Trigger_Number;
  unsigned long int Trigger_ID;
  unsigned long int BCID;
  unsigned long int Rel_BCID;
  unsigned int Column;
  unsigned int Row;
  unsigned int TOT;
  unsigned int Scan_Param_ID;
  unsigned int Trigger_Tag;
  unsigned int Event_Status;
};

void HitAnalysis(const char* inputfile)
{
  
  unsigned long int event_number;
  unsigned long int ext_trigger_number;
  unsigned long int trigger_id;
  unsigned long int bcid;
  unsigned long int rel_bcid;
  unsigned int col;
  unsigned int row;
  unsigned int tot;
  unsigned int scan_param_id;
  unsigned int trigger_tag;
  unsigned int event_status;

  struct HitInfo Hits[100000];
  int Occp[192][400] = {0};
  int Time_O_Threshold[15] = {0};
  int CD[400] = {0};
  int RD[192] = {0};
  int ReBCID[32] = {0};

//******************************************

  FILE *inputf = fopen(inputfile,"r");
  if (inputf == NULL) {
    std::cout << "Could NOT open the Input File!" << "\n";
    return;
  }

  printf("Reading the input file!\n");

  char fileline[256];
  int i = 0;
  while (fgets(fileline,256,inputf) && i<100000) {
    sscanf(fileline, "%lu %lu %lu %lu %lu %u %u %u %u %u %u", &event_number, &ext_trigger_number, &trigger_id, &bcid, &rel_bcid, &col, &row, &tot, &scan_param_id, &trigger_tag, &event_status);
    Hits[i].Event_Number = event_number;
    Hits[i].Ext_Trigger_Number = ext_trigger_number;
    Hits[i].Trigger_ID = trigger_id;
    Hits[i].BCID = bcid;
    Hits[i].Rel_BCID = rel_bcid;
    Hits[i].Column = col;
    Hits[i].Row = row;
    Hits[i].TOT = tot;
    Hits[i].Scan_Param_ID = scan_param_id;
    Hits[i].Trigger_Tag = trigger_tag;
    Hits[i].Event_Status = event_status;
    i++;
  }

//******************************************
/*                                                                                           Commented Section
  ifstream inputf;
  inputf.open(inputfile);
  if (!inputf.is_open()) {
      cerr << "Could NOT open the Input File!" << "\n";
      return;
  }

  printf("Reading the input file!\n");

  char fileline[256];  
  int i=0;
  while(inputf.getline(fileline,256))
    {
      sscanf(fileline, "%lu %lu %lu %lu %lu %u %u %u %u %u %u", &event_number, &ext_trigger_number, &trigger_id, &bcid, &rel_bcid, &col, &row, &tot, &scan_param_id, &trigger_tag, &event_status);
      Hits[i].Event_Number = event_number;
      Hits[i].Ext_Trigger_Number = ext_trigger_number;
      Hits[i].Trigger_ID = trigger_id;
      Hits[i].BCID = bcid;
      Hits[i].Rel_BCID = rel_bcid;
      Hits[i].Column = col;
      Hits[i].Row = row;
      Hits[i].TOT = tot;
      Hits[i].Scan_Param_ID = scan_param_id;
      Hits[i].Trigger_Tag = trigger_tag;
      Hits[i].Event_Status = event_status;
      i++;
    }
*/
//*********************************************************
  printf("Finished reading the input file!\n");

Try with:

  // ...
  struct HitInfo *Hits = new HitInfo[100000];
  // ...
  fclose(inputf); // right after the "while" loop
  // ...
  delete [] Hits; // when no longer needed
  // ...

BTW. When you post “source code” or “output” here, do remember to enclose them into two lines which contain just three characters ``` (see how your post has been edited above).

1 Like

Thank you very much. It’s working perfectly!
and sorry about that, this was my first time using Root Forum.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.