I’m attempting to write a code to be implemented in the following manner:
Convert a .csv data file to a .root file and rewrite it.
Create a histogram of the data and save it as a .png image.
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.
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.