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");
}
}
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.