Question About File Reading to TGraphError

Hi,

I have a directory full of “.dat” files with three columns with spaces between each that are of the form ( x | y | yerror) in a child directory “dataFiles” and need to write a script that takes in all the files of the directory, and saves them accordingly in another child directory “Plots.” I am currently using TList which makes a list of the file names and iterates over them. But I am getting a bunch of errors and am not completely sure how to implement the file name constraint so that it is saved accordingly to the proper directory. I am getting a lot of errors especially in reading in the proper data files. The file names are essentially the same, but it is not identifying the actual files and reading them into TGraphError.

Note: I am using ROOT version 5.22

Here is the code I have so far (I used parts of an another users’s implementation). Please let me know if you have any comments or corrections.


void ROOTplot(const char *dirname = "/cluster/tufts/ATLASNet/ksoni02/FEWZ/FEWZ_3.1.b2/bin/histDrawTests/NLO_Z/dataFiles",
                const char *suffix = ".dat",
                const char *prefix = "")
{
  if (!dirname || !(*dirname)) 
         return;
  
  
  TString pwd(gSystem->pwd());
  TSystemDirectory dir(dirname, dirname);
  TList *files = dir.GetListOfFiles();
  
  gSystem->cd(dir()); 
 
  if (files) 
  {
    TSystemFile *file;
    TString fname;
    TIter next(files);
    
    while ( (file = (TSystemFile*)next()) ) 
    {
      fname = file->GetName();
      
     
      if ( !(file->IsDirectory()) &&
           (!prefix || !(*prefix) || fname.BeginsWith(prefix)) &&
           (!suffix || !(*suffix) || fname.EndsWith(suffix)) ) 
      {
          gROOT->Reset();
          gROOT->SetStyle("Plain");
          TCanvas *nCanvas = new TCanvas();
          nCanvas->SetGrid();
          nCanvas->SetFillColor(0);


         
          TGraphErrors *graph = new TGraphErrors(fname ,"%lg %lg %lg");
          graph->SetTitle(fname);
          
          graph->SetMarkerStyle(kOpenCircle);
          graph->SetMarkerColor(kBlue);
          graph->SetMarkerSize(1);
          graph->SetFillColor(4);

         
          graph->Draw("ALP");
          graph->Print();
          
          nCanvas->Update();
          nCanvas->SaveAs("../Plots/"+fname+".root");

          
      }
    }
  }
  delete files;
}

Thanks in advance!

What kind of errors ?

[code]#include “TROOT.h”
#include “TSystem.h”
#include “TSystemFile.h”
#include “TSystemDirectory.h”
#include “TList.h”
#include “TString.h”
#include “TGraphErrors.h”
#include “TCanvas.h”

void ROOTplot(const char *dirname = “/cluster/tufts/ATLASNet/ksoni02/FEWZ/FEWZ_3.1.b2/bin/histDrawTests/NLO_Z/dataFiles”,
const char *suffix = “.dat”,
const char *prefix = “”)
{
if (!dirname || !(*dirname)) return; // just a precaution

TString pwd(gSystem->pwd());
TSystemDirectory dir(dirname, dirname);
TList *files = dir.GetListOfFiles();
gSystem->cd(pwd.Data()); // bug fix for ROOT prior to 5.34

if (files) {
TSystemFile *file;
TString fname;
TIter next(files);

gROOT->SetStyle("Plain");

gSystem->cd(dirname);

while ( (file = (TSystemFile*)next()) ) {
  fname = file->GetName();
  
  if ( !(file->IsDirectory()) &&
       (!prefix || !(*prefix) || fname.BeginsWith(prefix)) &&
       (!suffix || !(*suffix) || fname.EndsWith(suffix)) ) {
    TCanvas *nCanvas = new TCanvas();
    nCanvas->SetGrid();
    nCanvas->SetFillColor(0);
    
    TGraphErrors *graph = new TGraphErrors(fname, "%lg %lg %lg");
    graph->SetTitle(fname);
    
    graph->SetMarkerStyle(kOpenCircle);
    graph->SetMarkerColor(kBlue);
    graph->SetMarkerSize(1);
    graph->SetFillColor(4);
    
    graph->Draw("ALP");
    
    nCanvas->Modified(); nCanvas->Update();
    nCanvas->SaveAs("../Plots/" + fname + ".root");

#if 0 /* 0 or 1 /
delete nCanvas;
delete graph;
#endif /
0 or 1 */
}
}

gSystem->cd(pwd.Data());

}

delete files;
}[/code]