Hello. I have a root file within which there is a folder which contains different histogram. I want to loop those and plot different histogram for all of them. The folder name is Energy spectra and within that there are histograms named Energy ch 1;1 , Energy ch 2;1, Energy ch 3;1, and so on.
I wrote the following code and it did not work
#include
#include <TFile.h>
#include <TH1D.h>
#include <TCanvas.h>
#include
using namespace std;
void tut17() {
//Open an existing ROOT File using the READ command. READ is used so as to not modify the data saved in the ROOT file.
TFile* fileOut = TFile::Open(“C:/root_v5.34.36/macros/run00037_000.root”, “READ”);
//TFile *fileOut = TFile::Open(“ROOT DIRECTORY + /DAQRoot.root”,“READ”);
if (!fileOut) {
cout << "File not found." << endl;
return 0;
}
TDirectory* folder = (TDirectory*)fileOut->Get("Energy spectra"); // get the folder from the file
for (int i = 1; i < 33; i++)
{
//std::cout << file_name << std::endl;
TH1F* histo[i] = (TH1F*)folder->Get(Form("Energy ch %d",i)); // get the histogram from the folder
//double counts = histo->Integral(200, 1000);
TCanvas* canvas = new TCanvas("canvas", "Histogram", 800, 600); // create a canvas for the plot
histo[i]->Draw("same");// draw the histogram
//cout << counts << endl;
//cout << histo[i]->Integral(200, 1000) << endl;
//canvas->SaveAs("histo.pdf"); // save the plot as a pdf
}
}