Question on how to analyze more than one data file

Hi,

I’m attempting to write a code to be implemented in the following manner:

  1. Convert a .csv data file to a .root file and rewrite it.
  2. Create a histogram of the data and save it as a .png image.
  3. Go to the next .csv data file and repeat.

I was able to do step 1 and 2, and I’ve even managed to get the the code the looping to the next file correctly, but the way I’ve written the code to create a histogram results in only one histogram being made.

Here is the section of my code that is giving me trouble:


 void plot_histos2(const char* ext = ".csv"){

const char* dir = "<pathname>";
void* dirp = gSystem->OpenDirectory(dir);
const char* entry;
const char* filename[100];
Int_t n = 0;
TString str;

while((entry = (char*)gSystem->GetDirEntry(dirp)))
{
  str = entry;
  if(str.EndsWith(ext))
    filename[n++] = gSystem->ConcatFileName(dir, entry);
}

for(Int_t i = 0; i < n; i++)
{
  TString dir = "<pathname>";
  
  dir.ReplaceAll("/./","/");

  
  TFile *f = new TFile("data1.root","RECREATE");
  


  TH1F *h1 = new TH1F("h1","signal distribution",1000,-100,100);
  TTree *T = new TTree("ntuple","csv file data");
  Long64_t nlines = T->ReadFile(Form("%sdata1.csv",dir.Data()),"a:b:c:d:e");
  printf(" found %lld points\n",nlines);
  T->Draw("a","b");
  T->Write();
}

}

What is inside the for loop successfully creates a new .root file with the data from the .csv file and creates a histogram. I used this method with inspiration from a couple older question asked to the forum. But I was wondering about how I could change the syntax to make it so that, given for example the data files data1.csv, data2.csv, and data3.csv, I could make it so that this code creates a corresponding .root file and histogram for each .csv file.

In other words, how could I change the code so that it would loop and work for each file individually file? It might be an easy fix, but it’s been tripping me up.

Thank you very much

Try:

for(Int_t i = 1; i <= n; i++) {
  TString dir = "<pathname>";
  dir.ReplaceAll("/./","/");
  
  TFile *f = new TFile(TString::Format("data%d.root", i), "RECREATE");
  
  TTree *T = new TTree("ntuple", "csv file data");
  Long64_t nlines =
    T->ReadFile(TString::Format("%sdata%d.csv", dir.Data(), i), "a:b:c:d:e");
  printf(" found %lld points\n",nlines);
  
  TH1F *H = new TH1F("h_a", "signal distribution", 1000, -100, 100);
  T->Project("h_a", "a", "b"); // ... or ... T->Draw("a >> h_a", "b", "goff");
  
  T->Write();
  H->Write();
  delete f; // automatically deletes "T" and "H"
}

This worked quite well for converting the csv files to root files correspondingly, thanks for the help!

It did not build any histograms though. So I changed the following:

 T->Project("h_a", "a", "b"); // ... or ... T->Draw("a >> h_a", "b", "goff");

to the original

 T->Draw("a","b");

and it built only one histogram, but I’d like to build one for each data file.

Also, I would like to save the histograms to the directory, but I wasn’t able to. How could I do this?

Again, I very much appreciate the help.

Are you sure the histograms were not built? Did you inspect the output root file? @Wile_E_Coyote suggestions does not make any plotting visible to the screen when producing the output, but will write the histograms into the file. Your suggestion that only one plot is created leads me to believe that you are only see the last plot that populated the canvas as you have called draw repeatedly as you have removed the graphics off (goff) option.

I see. That makes perfect sense, thank you. I am admittedly quite new to ROOT, these little things trip me up.

Is there a way that I could save the histograms also as separate .png images in the directory?

As far as I know you need to draw it on a canvas and then use the TPad::Print method.

...

TH1D* hist = T->Project("h_a", "a", "b"); // ... or ... T->Draw("a >> h_a", "b", "goff");
TCanvas *c = new TCanvas("c", "");
hist->Draw();
c->SaveAs(TString::Format("data%d.png", i);
delete c;

...

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