Hello!
I want to save TGraphErrors in the tree, but my method don’t work.
First of all, I explain you what I want.
I want to make fit of TGraphErrors object and save TGraphErrors (object) and fit parameters (double) in the tree.
- I know how to save TGraphErrors objects only:
TMultiGraph *mg = new TMultiGraph();
TGraphErrors *gr = new TGraphErrors(xv.size(), &xv[0], &yv[0], &xverr[0], &yverr[0]);
mg->Add(gr);
...
TObjArray Hlist(0);
Hlist.Add(mg);
- I know how to save double values only:
TNtuple ntuple("ntuple", "fit results", "x0:y0:chi2");
...
ntuple.Fill(x0, y0, chi2);
TFile f_ntuple(string_ntuple.c_str(), "RECREATE");
ntuple.Write();
f_ntuple.Close();
I have tried the next code in order to save both: graph and doubles:
void CreateTree()
{
TFile f("D:\\Data_work\\tree.root","recreate");
TTree t1("t1","a simple Tree with simple variables");
Double_t x0, y0, amp;
//create vectors
vector<double> xv;
vector<double> yv;
vector<double> xverr;
vector<double> yverr;
//loop to create several graphs
for(int j = 0; j < 10; j++)
{
//fill vectors
for (Int_t i= -10; i<= 10; i++)
{
xv.push_back(i);
yv.push_back(0.2 * i*i + gRandom->Gaus(0, 1));
xverr.push_back(0);
yverr.push_back(1);
}
TGraphErrors *gr = new TGraphErrors(xv.size(), &xv[0], &yv[0], &xverr[0], &yverr[0]);
gr->SetMarkerColor(4);
gr->SetMarkerStyle(kFullCircle);
TF1 *fitFcn = new TF1("fitFcn", fitFunction, -10, 10, 3);
gr->Fit("fitFcn", "R");
x0 = fitFcn->GetParameter(2);
y0 = fitFcn->GetParameter(1);
amp = fitFcn->GetParameter(0);
//cout << "x0 = " << x0 << " y0 = " << y0 << " amp = " << amp << endl;
//gr->Draw("AP");
t1.Branch("x0",&x0,"x0/D");
t1.Branch("y0",&y0,"y0/D");
t1.Branch("amp",&,"amp/D");
t1.Branch("gr","TGraphErrors",&gr, 128000, 0);
t1.Fill();
xv.clear();
yv.clear();
xverr.clear();
yverr.clear();
}
t1.Write();
}
But the result is differs from my expectation: I can’t open graph object like 1) method.
- prntscr.com/8vgg9f (via TObjArray); picture just for example
- prntscr.com/8vggw1 (via TNtuple )
- my attempt (via TTree) prntscr.com/8vghs2
I expected to find in the tree the array of these graphs prntscr.com/8vgisx
Thanks for advance.