Looping through root files, plot histograms and record values to an output file

Hello all,

I am trying to loop through a folder containing about 150 ntuple root files with a TTree histogram called EdepNTuple;1 stored in each of them. My goal is to plot the histogram with log(y) axis, save the canvas of each plot and perform some analysis detailed in my code below. The output csv files should contain values of x, y, z for each of those files.

I’ve spent quite some time on my own trying to solve this issue and will really appreciate some help. The files are too many to perform this manually

void efficiencyandplots(const char *dirname="$HOME/B1-b/", const char *ext=".root"){
 
 char* dir = gSystem->ExpandPathName(dirname);
 void* dirp = gSystem->OpenDirectory(dir);
  
 const char* entry;
 Int_t n = 0;
 TString str;

 while((entry = (char*)gSystem->GetDirEntry(dirp))) {
  str = entry;
  if(str.EndsWith(ext))
  	TFile *f = new TFile(str,"READ");
 	TTree *E = (TTree*)f->Get("EdepNtuple;1");
 	E->Draw("Edep>>hist");
 	double a = hist->Integral(1,1)/hist->Integral()
 	double b = hist->Integral(1,20)/hist->Integral()
 	double c = hist->Integral(1,30)/hist->Integral()
        
        double x = 1 - a
        double y = 1 - b
        double z = 1 - c
    
  }


}

@Wile_E_Coyote and @couet I will be really grateful if you can please take a look at this. Any guide you give will be very helpful.

What does not work ?

Hello
the loop does not work and I haven’t found a way to store neither the canvas from the plot for each root file or the outputs. I have tried doing it manually for each file and it works but the files are too many for this.

I have attached samples of the root files if it is of any help.

Thank you so much!

EdepNTuples.zip (303.5 KB)

You should create the canvas before the loop and then print it in the loop using TCnavas::Print()

auto c = new TCanvas();
....

loop {
 .....
 c->Print("image.png");
}