#include "TF1.h" #include "TCanvas.h" #include "TGraph.h" #include "TFile.h" #include "TTree.h" #include "TLeaf.h" #include "TH1F.h" #include "TGraphErrors.h" // This first bit is my attempt to make various structures available globally TF1 *FTheta90; // TGraph *Gr_fTheta90; TGraph *x0Theta90; TGraph *x2Theta90; TGraph *x4Theta90; // Double_t h1X0[5]; Double_t h1X2[5]; Double_t h1X4[5]; Double_t h2X0[5]; Double_t h2X2[5]; Double_t h2X4[5]; Double_t h3X0[5]; Double_t h3X2[5]; Double_t h3X4[5]; Double_t h4X0[5]; Double_t h4X2[5]; Double_t h4X4[5]; // Int_t nlines; // Int_t n = 5; Double_t phi[] = {0,45,90,135,180}; // Double_t G2; Double_t G4; // Double_t fTheta90(Double_t *x, Double_t *par) { G2=par[0]; G4=par[1]; return x0Theta90->Eval(x[0])+G2*x2Theta90->Eval(x[0])+G4*x4Theta90->Eval(x[0]); //I have tried passing values both this way - via TGraph //Int_t i = x[0]/45; //return h1X0[i]+h1X2[i]+h1X4[i]; //and this way by reference to arrays } void Example_script(){ TFile* file = TFile::Open( "PhiCanvas.root", "READ" ); TCanvas * PhiCanvas = (TCanvas*)file->Get("PhiCanvas"); PhiCanvas->Draw(); // TTree *TREE = new TTree("TREE","Xk values from XkDat file"); nlines = TREE->ReadFile("Xk.txt","G:P:X0:X2:X4"); printf(" found %d points\n",nlines); // for (int i=0;iGetEntry(i); if (i<5){ h1X0[i] = TREE->GetLeaf("X0")->GetValue(0); h1X2[i] = TREE->GetLeaf("X2")->GetValue(0); h1X4[i] = TREE->GetLeaf("X4")->GetValue(0); } else if (i<10){ h2X0[i-5] = TREE->GetLeaf("X0")->GetValue(0); h2X2[i-5] = TREE->GetLeaf("X2")->GetValue(0); h2X4[i-5] = TREE->GetLeaf("X4")->GetValue(0); } else if (i<15) { h3X0[i-10] = TREE->GetLeaf("X0")->GetValue(0); h3X2[i-10] = TREE->GetLeaf("X2")->GetValue(0); h3X4[i-10] = TREE->GetLeaf("X4")->GetValue(0); } else { h4X0[i-15] = TREE->GetLeaf("X0")->GetValue(0); h4X2[i-15] = TREE->GetLeaf("X2")->GetValue(0); h4X4[i-15] = TREE->GetLeaf("X4")->GetValue(0); } } // x0Theta90 = new TGraph(n,phi,h1X0); // TGraph * x0Theta135 = new TGraph(n,phi,h2X0); // TGraph * x0ThetaNeg115 = new TGraph(n,phi,h3X0); // TGraph * x0ThetaZero = new TGraph(n,phi,h4X0); // x2Theta90 = new TGraph(n,phi,h1X2); // TGraph * x2Theta135 = new TGraph(n,phi,h2X2); // TGraph * x2ThetaNeg115 = new TGraph(n,phi,h3X2); // TGraph * x2ThetaZero = new TGraph(n,phi,h4X2); // x4Theta90 = new TGraph(n,phi,h1X4); // TGraph * x4Theta135 = new TGraph(n,phi,h2X4); // TGraph * x4ThetaNeg115 = new TGraph(n,phi,h3X4); // TGraph * x4ThetaZero = new TGraph(n,phi,h4X4); // TVirtualPad * pad1 = PhiCanvas->cd(1); //also possible //TPad * pad1 = ((TPad*)PhiCanvas->GetListOfPrimitives()->At(0)); if (pad1) { FTheta90 = new TF1("FTheta90",fTheta90,0,180,2); // This is the bit I can't get!!!!!!!! FTheta90->SetParameters(1,1); //TGraph of the Pad is called "Graph" not very descriptive :) TGraphErrors * gr = (TGraphErrors*)pad1->FindObject("Graph"); //another possible way //TGraphErrors * gr = (TGraphErrors*)pad1->GetListOfPrimitives()->At(2); if (gr) { gr->Fit(FTheta90); } else { cout << "no graph found" << endl; } } // }