Dear root experts,
please help me with this silly question I am getting crazy and none of my colleagues found the error
I have a macro which opens a TFile, reads the Tree contained in the file (and in particular its branch “maximum” and then fills a Th1D with the data contained in the branch:
TFile file(filename_bkg, "READ");
TTree* tree;
file.GetObject("EventTree", tree);
EventInfo event;
tree->SetBranchAddress("maximum", &event.maximum);
int nEntries = tree->GetEntries();
TH1D* histogram = new TH1D("maximumPulses", "Pulses I Maximum Value", nBins, minRange, maxRange);
for (Long64_t i = 0; i < nEntries; ++i) {
tree->GetEntry(i);
histogram->Fill(event.maximum);
this works and I obtain a wonderful histogram. Now, I have to open many files and create a histogram for each file so I decided to make a method:
TH1D* CreateHisto(const char* filename){
TFile file(filename, "READ");
TTree* tree;
file.GetObject("EventTree", tree);
EventInfo event;
tree->SetBranchAddress("maximum", &event.maximum);
int nEntries = tree->GetEntries();
TH1D* h1 = new TH1D("maximumPulses1", "Pulses", 100, 0, 5);
for (Long64_t i = 0; i < nEntries; ++i) {
tree->GetEntry(i);
h1->Fill(event.maximum);
}
file.Close();
return h1;
}
and call it in the main:
TH1D* histogram = CreateHisto(filename_bkg);
if (histogram) {
cout<<"the histogram exists"<<endl;
histogram->Draw();
} else {
std::cerr << "Error: Unable to create histogram." << std::endl;
}
The problem is that, the program writes “the histogram exists” but when I uncomment “histogram->Draw” I got a segmentation violation why does it happen can you find the giant stupid mistake I made?
ps. I checked that the file exists, that the tree exists, and that the data in event.maximum are properly read, I just removed those lines from the code.
_ROOT Version: v6.22.08
_Platform: Mac OS Ventura 13.6.3 (22G436)
Compiler: Not Provided