Dear all,
I am having the same problem mentioned above (I would like to create a TF1 from a TGraph or a TSpline3 if it is possible).
However, the solution proposed in this topic is not working for me.
[code]
/* c++ includes */
#include
#include <stdio.h>
#include
/* root includes */
#include “TROOT.h”
#include “TRandom3.h”
#include “TTree.h”
#include “TProfile.h”
#include <TMath.h>
#include <TString.h>
#include <TFile.h>
#include <TCanvas.h>
#include <TGraph.h>
#include <TAxis.h>
#include <TF1.h>
#include
#include “TVectorD.h”
#include “TSystem.h”
#include “TLatex.h”
#include <TSpline.h>
#include “TMultiGraph.h”
#include “TRint.h”
using namespace std;
// TGraph to TF1
TGraph *g1;
double myfunc(double *xx, double *)
{
return g1->Eval(xx[0]);
}
// Main function
int main(int argc, char *argv[])
{
int fargc = 1;
TRint *rint = new TRint(“Test”, &fargc, argv);
// Opening data. Here energy and PAI cross section relative to Rutherford cross section is provided
TTree *MyTree = new TTree("MyTree","MyTree");
Double_t energy, crossSection;
MyTree->ReadFile("data-Bichsel.txt","energy/D:crossSection/D");
MyTree->SetBranchAddress("energy",&energy);
MyTree->SetBranchAddress("crossSection",&crossSection);
// Probability of energy deposition
Int_t nn = MyTree->GetEntries(); // number of entries on .txt file
const Int_t n = nn;
TVectorD ene(n), Pn1(n), sigma(n), Sigma(n), SSigma(n);
TVectorD dSigma_dE(n), dSigmaRuth_dE(n), dSigmaRuth(n);
const Double_t beta_lorentz = 0.97;
const Double_t betagamma = beta_lorentz/sqrt(1 - pow(beta_lorentz,2));
const Double_t mc2 = 0.510998928*pow(10,6);
const Double_t Emax = 2*mc2*pow(betagamma,2);
const Double_t kr = 2.54955*pow(10,-19); // constant used in Rutherford cross-section (eV.cm2)
for (int i=0; i<n; i++) // loop on all values of energy
{
MyTree->GetEntry(i);
ene(i) = energy;
sigma(i) = crossSection;
// Rutherford cross-section
dSigmaRuth_dE(i) = kr/pow(beta_lorentz*ene(i),2)*(1 - pow(beta_lorentz,2)*ene(i)/Emax);
// Converting inital cross section
Sigma(i) = sigma(i)*dSigmaRuth_dE(i);
}
TCanvas *c1 = new TCanvas("c1","c1",800,600);
c1->cd();
TGraph *g1 = new TGraph(ene,Sigma);
TSpline3 *s1 = new TSpline3("interpolation1",g1);
s1->Draw();
Double_t emin = 9.203623;
Double_t emax = 94867.336;
gPad->SetLogx();gPad->SetLogy();
TF1 *f1 = new TF1("f1",myfunc,emin,emax);
Double_t area = f1->Integral(emin,emax);
cout << " Area: " << area << endl;
rint->Run(kTRUE);
}
[/code]
And I am having the following message when the line “double newsum = f1->Integral(Emin,Emax);” is not commented:
*** Break *** segmentation violation
Generating stack trace…
0x00000001111b0c73 in TF1_EvalWrapper::DoEval(double) const (in libHist.so) + 35
0x0000000112142ff2 in ROOT::Math::GaussIntegrator::DoIntegral(double, double, ROOT::Math::IBaseFunctionOneDim const*) (in libMathCore.so) + 482
0x00000001111abcf8 in TF1::Integral(double, double, double const*, double) (in libHist.so) + 200
0x000000010f39a060 in main (in paiBichselv2) + 3520
0x00007fffeb7f8255 in start (in libdyld.dylib) + 1
Thanks for any help!
Best,
Diego
data-Bichsel.txt (2.91 KB)