SegFault when Looping through histograms in different files


ROOT Version: 5.34/32
Platform: Ubuntu 16.04.3
Compiler: 5.18.00


Hi,

This is my first post on this forum, let me know if there’s any standard formatting I’m missing, information I should be providing, or anything like that that I’m missing and I’ll be happy to put it in. Thanks!

I have a directory full of 25 procedurally named and generated TTree files (all named run(NUMBER).root ) and with identical internal structure. I’m trying to write a macro to loop through them, build a histogram from data in each, extract a value from that histogram, and put it into a table. As far as I can tell, all of my commands work when ran in interactive mode and moving through the loop manually, but I get a segfault on the second iteration when I run it as an actual automated loop. I’m wondering if there is something obvious I’m missing here.

Here’s my macro:

int i=0;
double nmean[24];
TString runpart;
TString runfile;
for(i=0;i<24;++i) {
   runpart=Form("%d",i); 
   runfile="run"+runpart+".root";
   TFile current(runfile);
   str->Draw("nphotons","","",500,0);            #str and nphotons are common contents of all these files
   nmean[i]=htemp->GetMean();
   c1->Close();
};

Running this gives the output

Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1

 *** Break *** segmentation violation

and then dumps a stack trace, which I can provide if anyone thinks it would be helpful. By putting in some printf flags, it seems the crash is happening on the Draw command on the second iteration.

On the other hand, entering all code line by line and manually ++ing “i” after every run seems to work fine. Is there something obvious I’m missing here?

Thank you for your time.

Hi,

I would try first to retrieve the TTree object from the file using compliant C++ code and not shortcuts (e.g. TTree * str = (TTree*) current.Get(“str”)

and also retrieve the htemp histogram from the current directory.
I would then check if the returned pointers are not null

Lorenzo

1 Like

Thanks for your help! I’ve modified my code with the suggestions you gave, retrieving htemp and using more “proper” code, and that seems to be working! Here’s my full (working) code, in case people from the future have the same problem.

#include<iostream>


void histmakerbackup(){
int i=0;
double nmean[24];
cout <<"\n";
TString runpart;
TString runfile;
for (i=0;i<24;++i){
        gROOT->cd();
        cout << "Cleared directory, preparing for the " << i <<"th loop.\n";
        runpart=Form("%d",i);
        runfile="run"+runpart+".root";
        TFile current(runfile);
        cout << "Loaded the runfile " << runfile << "\n";
        TTree *str=(TTree*) current.Get("str");
        cout << "initialized the TTree current\n";
        str->Draw("nphotons","","",500,0); 
        cout << "Drew the nphotons histogram\n";
        TH1F *htemp = (TH1F*)gPad->GetPrimitive("htemp");
        nmean[i]=htemp->GetMean();
        cout << "Got the mean value\n";
        c1->Close();
        cout<< "closed the histogram, preparing for the next run.\n";
 
}
}

Now that it’s working and that problem is solved, any ideas as to why it was an issue in the first place, or why it would have worked in interactive mode? Not urgent anymore, just hoping to avoid similar problems in the future.

Thanks again!

Hi,

These shortcuts cannot work in a compiled program because it is not valid C++.
It should be used just at the prompt for convenience and avoid excessive typing but not in a macro that you execute either from the prompt or in batch

Lorenzo

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