*** Break *** segmentation violation while closing .root file

Hi,
I’m trying to read two .root files. I comment the part regarding one of them to make it easier. The next code works, but left the file openned:

void analysis(){
	
	TString filename1 = "run_01/run.root"; // CP-even
	TString hist1 = "h_Phi_q1_q2_t_tbarSM;1";
	
	//TString filename2 = "run_02/run.root"; // CP-odd
	//TString hist2 = "h_Phi_q1_q2_t_tbarSM;1";
	
	TCanvas* c1 = new TCanvas("Name", "Title", 1280, 1024);
	
	THStack *hstack = new THStack("Name", "");	
	
	TFile* file1 = new TFile(filename1, "READ");
	TH1F* th1f1 = (TH1F*)file1->Get(hist1);
	//file1->Close();
	
	//TFile* file2 = new TFile(filename2, "READ");
	//TH1F* th1f2 = (TH1F*)file2->Get(hist2);
	//file2->Close();

	th1f1->Sumw2();
	//th1f2->Sumw2();
	
	th1f1->SetLineColor(kBlack);
	//th1f2->SetLineColor(kBlue);
	
	//normalize
	th1f1->Scale(1./th1f1->Integral());
	//th1f2->Scale(1./th1f2->Integral());
	
	hstack-> Add(th1f1);
	//hstack-> Add(th1f2);
	
	hstack->Draw("b nostack e1");
	
	hstack->GetXaxis()->SetTitle("\\phi_C");
	hstack->GetYaxis()->SetTitle("Distribution");
	
	TLegend* legend = new TLegend(0.7, 0.75, 0.9, 0.9);
	legend->AddEntry(th1f1, "CP-even", "l");//"fas"
	//legend->AddEntry(th1f2, "CP-odd", "l");
	legend->Draw();
	
	
	gPad->Modified();
	gPad->Update();
	
	c1->Print("PhiC.pdf");
	c1->Print("PhiC.png");
	
	
}

I get the histogram:

However, if I uncomment file1->Close() I get:

root [0]
Processing analysis.c...

 *** Break *** segmentation violation
 Generating stack trace...
 0x00007f200ba0f2ad in <unknown> from /mnt/d/Debian/ROOT/ROOT6.24.06/lib/libCling.so
 0x00007f200b999a71 in <unknown> from /mnt/d/Debian/ROOT/ROOT6.24.06/lib/libCling.so
 0x00007f200b99a2cb in <unknown> from /mnt/d/Debian/ROOT/ROOT6.24.06/lib/libCling.so
 0x00007f200ba63f44 in <unknown> from /mnt/d/Debian/ROOT/ROOT6.24.06/lib/libCling.so
 0x00007f200ba72db9 in <unknown> from /mnt/d/Debian/ROOT/ROOT6.24.06/lib/libCling.so
 0x00007f200ba74692 in <unknown> from /mnt/d/Debian/ROOT/ROOT6.24.06/lib/libCling.so
 0x00007f200ba5cff9 in <unknown> from /mnt/d/Debian/ROOT/ROOT6.24.06/lib/libCling.so
 0x00007f200b8c7f5c in <unknown> from /mnt/d/Debian/ROOT/ROOT6.24.06/lib/libCling.so
 0x00007f200b8e03c6 in TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) + 0x1106 from /mnt/d/Debian/ROOT/ROOT6.24.06/lib/libCling.so
 0x00007f200b8e06e5 in TCling::ProcessLineSynch(char const*, TInterpreter::EErrorCode*) + 0xd5 from /mnt/d/Debian/ROOT/ROOT6.24.06/lib/libCling.so
 0x00007f200ffb771b in TApplication::ExecuteFile(char const*, int*, bool) at TApplication.cxx:? from /mnt/d/Debian/ROOT/ROOT6.24.06/lib/libCore.so
 0x00007f2010376fb2 in TRint::ProcessLineNr(char const*, char const*, int*) + 0x52 from /mnt/d/Debian/ROOT/ROOT6.24.06/lib/libRint.so
 0x00007f201037876a in TRint::Run(bool) + 0x2ea from /mnt/d/Debian/ROOT/ROOT6.24.06/lib/libRint.so
 0x000055a9b5a6e11c in main + 0x4c from /mnt/d/Debian/ROOT/ROOT6.24.06/bin/root.exe
 0x00007f200fa6cd0a in __libc_start_main + 0xea from /lib/x86_64-linux-gnu/libc.so.6
 0x000055a9b5a6e16a in _start + 0x2a from /mnt/d/Debian/ROOT/ROOT6.24.06/bin/root.exe

I upload the .root file, since it is small.
run.root (16.5 KB)


Please read tips for efficient and successful posting and posting code

ROOT Version: 6.24/06
Platform: linuxx8664gcc
Compiler: Not Provided


Hi,

you can uncomment the

//file1->Close();

, but first you have to add

th1f1->SetDirectory(nullptr);

right before

file1->Close();

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