Segmentation violation ROOT6 on Mac

Hello,

I’m using ROOT6 on Mac. There is some problem when I using the following code.

//*********************************************************
void test(){

   TCanvas* c1 = new TCanvas("c1","c1 test", 900, 300);
   c1->Divide(3,1);

   TFile* ofile = new TFile("test.root","RECREATE");
   TH1F* hpx = new TH1F("hpx", "hpx" , 400, -20, 20);
   TH1F* hpy = new TH1F("hpy", "hpy" , 400, -20, 20);
   TH1F* hpz = new TH1F("hpz", "hpz" , 400, -20, 20);

   for(int i=0; i<10000; i++)
   {   
      hpx->Fill(gRandom->Gaus(0,1)); hpy->Fill(gRandom->Gaus(0,2)); hpz->Fill(gRandom->Gaus(0,3));
   }   

   c1->cd(1); hpx->Draw(); hpx->Fit("gaus");
   c1->cd(2); hpy->Draw(); hpy->Fit("gaus");
   c1->cd(3); hpz->Draw(); hpz->Fit("gaus");
   c1->cd();

   ofile->cd();
   hpx->Write();hpy->Write();hpz->Write();
   c1->Write();
   ofile->Close();
}

//*********************************************************
root -l test.root
root [0] 
Attaching file test.root as _file0...
(TFile *) 0x7ffdfc289e30
root [1] .ls
TFile**		test.root	
 TFile*		test.root	
  KEY: TH1F	hpx;1	hpx
  KEY: TH1F	hpy;1	hpy
  KEY: TH1F	hpz;1	hpz
  KEY: TCanvas	c1;1	c1 test
root [2] c1->Draw()
Error in <TClingCallFunc::make_wrapper>: Failed to compile
  ==== SOURCE BEGIN ====
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wformat-security"
__attribute__((used)) extern "C" void __cf_0(void* obj, int nargs, void** args, void* ret)
{
   if (ret) {
      new (ret) (double) (TFormula____id18130473110346259110(*(double**)args[0], *(double**)args[1]));
      return;
   }
   else {
      TFormula____id18130473110346259110(*(double**)args[0], *(double**)args[1]);
      return;
   }
}
#pragma clang diagnostic pop
  ==== SOURCE END ====

 *** Break *** segmentation violation
 Generating stack trace...

An admin user name and password is required to enter Developer Mode.

This works for me:

void test() {

  TCanvas* c1 = new TCanvas("c1","c1 test", 900, 300);
  c1->Divide(3,1);

  TFile* ofile = new TFile("test.root","RECREATE");
  TH1F* hpx = new TH1F("hpx", "hpx" , 400, -20, 20);
  TH1F* hpy = new TH1F("hpy", "hpy" , 400, -20, 20);
  TH1F* hpz = new TH1F("hpz", "hpz" , 400, -20, 20);

  for(int i=0; i<10000; i++) {
    hpx->Fill(gRandom->Gaus(0,1)); hpy->Fill(gRandom->Gaus(0,2)); hpz->Fill(gRandom->Gaus(0,3));
  }
  hpx->SetDirectory(0);
  hpy->SetDirectory(0);
  hpz->SetDirectory(0);

  c1->cd(1); hpx->Draw(); hpx->Fit("gaus");
  c1->cd(2); hpy->Draw(); hpy->Fit("gaus");
  c1->cd(3); hpz->Draw(); hpz->Fit("gaus");

  c1->Update();
  c1->Modified();

  ofile->cd();
  c1->Write();
  hpx->Write(); hpy->Write(); hpz->Write();
  ofile->Close();
}

The main changes are the SetDirectory(0) for the histos, and updating the canvas; without these, things are not correctly written to the output file.
-Daniel

Thank you Daniel.

I use your code. However, the problem is still there:

root [0] 
Attaching file test.root as _file0...
(TFile *) 0x7fc27d9a10d0
root [1] .ls
TFile**		test.root	
 TFile*		test.root	
  KEY: TCanvas	c1;1	c1 test
  KEY: TH1F	hpx;1	hpx
  KEY: TH1F	hpy;1	hpy
  KEY: TH1F	hpz;1	hpz
root [2] c1->Draw()
Error in <TClingCallFunc::make_wrapper>: Failed to compile
  ==== SOURCE BEGIN ====
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wformat-security"
__attribute__((used)) extern "C" void __cf_0(void* obj, int nargs, void** args, void* ret)
{
   if (ret) {
      new (ret) (double) (TFormula____id18130473110346259110(*(double**)args[0], *(double**)args[1]));
      return;
   }
   else {
      TFormula____id18130473110346259110(*(double**)args[0], *(double**)args[1]);
      return;
   }
}
#pragma clang diagnostic pop
  ==== SOURCE END ====

 *** Break *** segmentation violation
 Generating stack trace...
An admin user name and password is required to enter Developer Mode.

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