*** Break *** segmentation violation in adding new branch to the tree

Hi.
I want to add new branch[12] to the tree which is already exist.
I succeeded running only TS_Calibration(0), but I couldn’t All_TS_Calibration because of segmentation violation. What is the problem in my macros??
*a[N] and b[N] are vector and calculated in TS_Fit.

void TS_Calibration(int IP=0){
if (fChain == 0) return;
  Long64_t nentries = fChain->GetEntriesFast();
  Long64_t nbytes = 0, nb = 0;

  TTree *tree = fChain->GetTree();
  if (!tree) cout << "TTree not found." << endl;

  TFile *newFile = TFile::Open("../ROOT/test_0808/run_94/MSE000094.root","UPDATE");
  TTree *newTree = new TTree("newTree","newTree");
  newTree->Branch("TS_Kal_calib",TS_Kal_calib,"TS_Kal_calib[12]/D");
  newTree->Branch("TS_calib_diff",TS_calib_diff,"TS_calib_diff[12]/D");

  for (Long64_t jentry=0; jentry<nentries;jentry++) {
    Long64_t ientry = LoadTree(jentry);
    if (ientry < 0) break;
    nb = fChain->GetEntry(jentry);   nbytes += nb;
    int N = jentry/100;
    TS_Kal_calib[IP] = TS_Kal[IP] - (a[N] * TS_Kal[IP] + b[N]);
    TS_calib_diff[IP] = TS_Kal_calib[IP] - TS_NIM;
    newTree->Fill();
 }
  newFile->cd();
  newTree->Write();
  newFile->Close();
}



void All_TS_Calibration(){
for(int IP=0;IP<IP_max;IP++){
    TS_Fit(IP);
    TS_Calibration(IP);
  }
  TFile *f1 = TFile::Open("../ROOT/test_0808/run_94/MSE000094.root");
}
}

On your original question, the problem may or may not comes from the declaration of TS_Kal_calib and TS_calib_diff. How are they declared?

On a different note I do not understand the logic …
It looks like the execution would include the following sequence of operations:

TS_Calibration(0);
TS_Calibration(1);
etc..

which if unfolded would execute:

TFile *newFile = TFile::Open("../ROOT/test_0808/run_94/MSE000094.root","UPDATE");
TTree *newTree = new TTree("newTree","newTree");
...
TS_Kal_calib[0] = ...
...
newTree->Fill()
...
newTree->Write();

So newTree is created anew IP_max times, where in the first IP_max-1 times the data content is only partially files (since at each call to TS_Calibration(IP); only one element of the array TS_calib_diff is set …

Worse in the 1 through IP_max the array contains for the element 0 through current iteration minus 1, not the intended value but the value corresponding to the last entry only.

So it looks like the loop from IP to IP_max might need to actually be inside the TS_Calibration function (and more precisely inside the loop over the entries). It is also very possible that TS_Calibration and TS_Fit needs to be merged into a single function to obtain the intended result.

Am I missing something?

Thank you for a reply.

On your original question, the problem may or may not comes from the declaration of TS_Kal_calib and TS_calib_diff . How are they declared?

they are decleared in source file.

I also forgot that, so I modified like below and tried to run, but segmentaion violation occurred the same as before.

void EventMatch::TS_Calibration(Int_t IP_max=12)
{
  if (fChain == 0) return;
  Long64_t nentries = fChain->GetEntriesFast();
  Long64_t nbytes = 0, nb = 0;

  double TS_Kal_calib[12]={}, TS_calib_diff[12]={};;
  TTree *tree = fChain->GetTree();
  if (!tree) cout << "TTree not found." << endl;

  TFile *newFile = TFile::Open("../ROOT/test_0808/run_94/MSE000094.root","UPDATE");
  //TTree *newTree = tree->CloneTree(0);
  TTree *newTree = new TTree("newtree","newtree");
  //newTree->Delete("TS_Kal_calib");
  //newTree->DropBranchFromCache("TS_Kal_calib");
  newTree->Branch("TS_Kal_calib",TS_Kal_calib,"TS_Kal_calib[12]/D");
  newTree->Branch("TS_calib_diff",TS_calib_diff,"TS_calib_diff[12]/D");

  for (Long64_t jentry=0; jentry<nentries;jentry++) {
    Long64_t ientry = LoadTree(jentry);
    if (ientry < 0) break;
    nb = fChain->GetEntry(jentry);   nbytes += nb;
    int N = jentry/100;
    for(int IP=0;IP<IP_max;IP++){
      TS_Kal_calib[IP] = TS_Kal[IP] - (a[IP][N] * TS_Kal[IP] + b[IP][N]);
      TS_calib_diff[IP] = TS_Kal_calib[IP] - TS_NIM;
    }
    newTree->Fill();
  }
 TFile *f1 = TFile::Open("../ROOT/test_0808/run_94/MSE000094.root");
  TH2F *hTS_calib_diff[12];
  TCanvas *c1 = new TCanvas("c1","c1",1200,900);
  c1->Divide(4,3);
  for(int IP=0;IP<IP_max;IP++){
    hTS_calib_diff[IP] = new TH2F(Form("hTS_calib_diff_%d",IP),Form("TS_calib_%d",IP),1000,0,60,1000,\
-1e-9,1e-9);
    hTS_calib_diff[IP]->Fill(TS_Kal_calib[IP],TS_calib_diff[IP]);
    c1->cd(IP+1);
    hTS_calib_diff[IP]->Draw();
  }
  newFile->cd();
  newTree->Write();
  newFile->Close();
}

What is the actual declaration for TS_Kal_calib and TS_calib_diff ?

In source file, they were declaered like below.
Double_t TS_Kal_calib[12] = {};
Double_t TS_calib_diff[12] = {};

I modified in void EventMatch::TS_Calibration(Int_t IP_max=12)
//double TS_Kal_calib[12]={}, TS_calib_diff[12]={};

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