# Reading text file and fit the plot

**URL:** <https://root-forum.cern.ch/t/reading-text-file-and-fit-the-plot/49776>\
**Category:** Newbie\
**Created:** [April 27, 2022, 10:08pm UTC](https://root-forum.cern.ch/t/reading-text-file-and-fit-the-plot/49776 "2022-04-27T22:08:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dil](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dil/32/26887_2.png) [@Dil](https://root-forum.cern.ch/u/Dil)\
**Post date:** [April 27, 2022, 10:08pm UTC](https://root-forum.cern.ch/t/reading-text-file-and-fit-the-plot/49776/1 "2022-04-27T22:08:21Z")

</div>

Hello experts,  
I am not sure if this error related to root or C program.  
Don’t understand my error.  
What I wanted to do is,I have two txt files and need to create the following function and fit the plot.  
rhoPiPi = |A \* zOmnes00 + B \* zOmnes11 |^2

```auto
//
//
//

#include "Omnes_fitting.hpp"
#include <TComplex.h>
#include <TRandom3.h>
#include <TH2D.h>
#include <TFile.h>
#include <TNtuple.h>
#include <TMath.h>
#include <TGraph.h>
#include <TCanvas.h>
#include <TH1D.h>

Double_t Omnes_fit(Double_t *par);
int main(){
   
    TLegend * legend=new TLegend();
    
    
    
  
    
// rhoPiPi = |A * zOmnes00 + B * zOmnes11 |^2
    
    TFile *hFile=TFile::Open("plots_1.root");
    hFile->ls();
    TH1D * h_pion_invariant_mass_weight;
    TH1D * h_pion_invariant_mass_Stat_Syst;
    hFile->GetObject("h_pion_invariant_mass_weight", h_pion_invariant_mass_weight);
    hFile->GetObject("h_pion_invariant_mass_Stat_Syst", h_pion_invariant_mass_Stat_Syst);
    h_pion_invariant_mass_weight->SetLineColor(2);
    h_pion_invariant_mass_weight->SetMarkerColor(2);
    h_pion_invariant_mass_weight->SetMarkerStyle(kFullSquare);
    h_pion_invariant_mass_weight->Draw("E1");
    h_pion_invariant_mass_Stat_Syst->Draw("same E1");
    legend->AddEntry(h_pion_invariant_mass_weight,"Stat","EP");
    legend->AddEntry(h_pion_invariant_mass_Stat_Syst,"Stat+Sys","EP");
    legend->Draw();
    
    TF1 *omnes= new TF1("omnes", Omnes_fit,0.0,1.0,2);
    omnes->SetParameter(0,0.707);
    omnes->SetParameter(1,0.707);
    
    omnes->SetLineColor(4);
    omnes->Draw("sames");
    
    
    
    
    
    
}

Double_t Omnes_fit(Double_t *par){
    
    
    Double_t result=0.0;
    int mPts;
    
    const Int_t nPts = mPts;
     mPts=0;
   
    
    float mass,re, im;
    
    
    TComplex zOmnes00[nPts],zOmnes11[nPts];
    Double_t zOmnes11M[nPts],zOmnes00M[nPts],xOmnes11[nPts],xOmnes00[nPts],yOmnes11[nPts],yOmnes00[nPts];
    Double_t MassPiPi[nPts];
    
    char rhomeson[32] = "Omnes11.txt";
    FILE * fOmnes;
    fOmnes=fopen(rhomeson,"r");
    char line[charLine],chars[80];
    // Read Header lines
    fgets(line,charLine,fOmnes);
    // How many data lines?
    fgets(line,charLine,fOmnes);
    sscanf(line,"%d", &mPts);
    // fgets(line,charLine,fOmnes);
    
    
    
    char Sigmameson[32] = "Omnes00KM.txt";
    FILE * fOmnes1;
    fOmnes1=fopen(Sigmameson,"r");
    
    // Read Header lines
    fgets(line,charLine,fOmnes1);
    // How many data lines?
    fgets(line,charLine,fOmnes1);
    sscanf(line,"%d", &mPts);
   
    
    for (Int_t iPt=0; iPt< nPts; iPt++) {
        if(fgets(line,charLine,fOmnes)==NULL){
            printf("Error reading File %s line %d\n",
                   rhomeson, iPt+iHeader);
            continue;
        } else {
            sscanf(line,"%f %f %f", &mass,&re, &im);
            MassPiPi[iPt] = mass;
            xOmnes11[iPt]=re;
            yOmnes11[iPt]=im;
            zOmnes11[iPt] = TComplex(re,im);
            zOmnes11M[iPt]=sqrt(re*re+im*im);
            
        }
        
        
         mPts=0;
        if(fgets(line,charLine,fOmnes1)==NULL){
            printf("Error reading File %s line %d\n",
                   Sigmameson, iPt+iHeader);
            continue;
        } else {
            sscanf(line,"%f %f %f", &mass,&re, &im);
            xOmnes00[iPt]=re;
            yOmnes00[iPt]=im;
            zOmnes00[iPt] = TComplex(re,im);
            zOmnes00M[iPt]=sqrt(re*re+im*im);
            printf("xOmenes= %f \n", xOmnes00[iPt]);
            
              mPts++;
            
        }
        
        result = par[0]*zOmnes00M[iPt];
        result += par[1]*zOmnes11M[iPt];
        result=result* result;
        
    }
    fclose(fOmnes);
    fclose(fOmnes1);
    return mPts;
   return result;
}

```

this my error  
root [0] .L Omnes\_fitting.cpp  
In file included from /Users/D/root-6-12-04-02/input\_line\_12:24:  
In file included from /Users/D/root-6-12-04-02/etc/dictpch/allHeaders.h:774:  
In file included from /Users/D/root-6-12-04-02/include/TMVA/CrossValidation.h:9:  
In file included from /Users/D/root-6-12-04-02/include/TMultiGraph.h:26:  
/Users/D/root-6-12-04-02/include/TF1.h:716:104: error: type ‘double (double _)’ cannot be used prior to ‘::’ because it has no members  
using Fnc\_t = typename ROOT::Internal::GetFunctorType\<decltype(ROOT::Internal::GetTheRightOp(&Func::operator()))\>::type;  
^  
/Users/D/root-6-12-04-02/include/TF1.h:361:41: note: in instantiation of member function 'ROOT::Internal::TF1Builder\<double (_)(double _)\>::Build’ requested here  
ROOT::Internal::TF1Builder::Build(this, f);  
^  
/Users/D/Documents/cross\_section/pipi\_fitting/Omnes\_fitting.cpp:43:21: note: in instantiation of function template specialization 'TF1::TF1\<double (_)(double _)\>’ requested here  
TF1 omnes= new TF1(“omnes”, Omnes\_fit,0.0,1.0,2);  
^  
In file included from /Users/D/root-6-12-04-02/input\_line\_12:24:  
In file included from /Users/D/root-6-12-04-02/etc/dictpch/allHeaders.h:774:  
In file included from /Users/D/root-6-12-04-02/include/TMVA/CrossValidation.h:9:  
In file included from /Users/D/root-6-12-04-02/include/TMultiGraph.h:26:  
In file included from /Users/D/root-6-12-04-02/include/TF1.h:35:  
/Users/D/root-6-12-04-02/include/Math/ParamFunctor.h:125:29: error: too many arguments to function call, expected 1, have 2  
return (f)((T)x, (double)p);  
~~~~^~~~~~~~~~  
/Users/D/root-6-12-04-02/include/Math/ParamFunctor.h:95:45: note: in instantiation of member function 'ROOT::Math::ParamFunctorHandler\<ROOT::Math::ParamFunctorTempl, double (_)(double _)\>::FuncEvaluator\<double  
(_)(double _), int\>::EvalConst’ requested here  
return FuncEvaluator\<Func, EvalType\>::EvalConst(fFunc,x,p);  
^  
/Users/D/root-6-12-04-02/include/Math/ParamFunctor.h:73:4: note: in instantiation of member function 'ROOT::Math::ParamFunctorHandler\<ROOT::Math::ParamFunctorTempl, double (_)(double _)\>::operator()’ requested here  
ParamFunctorHandler(const Func & fun) : fFunc(fun) {}  
^  
/Users/D/root-6-12-04-02/include/Math/ParamFunctor.h:304:17: note: in instantiation of member function 'ROOT::Math::ParamFunctorHandler\<ROOT::Math::ParamFunctorTempl, double (_)(double _)\>::ParamFunctorHandler’ requested  
here  
fImpl(new ParamFunctorHandler\<ParamFunctorTempl,Func\>(f) )  
^  
/Users/D/root-6-12-04-02/include/TF1.h:718:62: note: in instantiation of function template specialization 'ROOT::Math::ParamFunctorTempl::ParamFunctorTempl\<double (_)(double _)\>’ requested here  
f-\>fFunctor = new TF1::TF1FunctorPointerImpl\<Fnc\_t\>(ROOT::Math::ParamFunctorTempl\<Fnc\_t\>(func));  
^  
/Users/D/root-6-12-04-02/include/TF1.h:361:41: note: in instantiation of member function 'ROOT::Internal::TF1Builder\<double (_)(double _)\>::Build’ requested here  
ROOT::Internal::TF1Builder::Build(this, f);  
^  
/Users/D/Documents/cross\_section/pipi\_fitting/Omnes\_fitting.cpp:43:21: note: in instantiation of function template specialization 'TF1::TF1\<double (_)(double \*)\>’ requested here  
TF1 _omnes= new TF1(“omnes”, Omnes\_fit,0.0,1.0,2);  
^  
In file included from /Users/D/root-6-12-04-02/input\_line\_12:24:  
In file included from /Users/D/root-6-12-04-02/etc/dictpch/allHeaders.h:774:  
In file included from /Users/D/root-6-12-04-02/include/TMVA/CrossValidation.h:9:  
In file included from /Users/D/root-6-12-04-02/include/TMultiGraph.h:26:  
In file included from /Users/D/root-6-12-04-02/include/TF1.h:35:  
/Users/D/root-6-12-04-02/include/Math/ParamFunctor.h:121:25: error: too many arguments to function call, expected 1, have 2  
return (\*f)(x, p);  
~~~~ ^  
/Users/D/root-6-12-04-02/include/Math/ParamFunctor.h:91:45: note: in instantiation of member function 'ROOT::Math::ParamFunctorHandler\<ROOT::Math::ParamFunctorTempl, double (_)(double _)\>::FuncEvaluator\<double  
(_)(double _), int\>::Eval’ requested here  
return FuncEvaluator\<Func, EvalType\>::Eval(fFunc,x,p);  
^  
/Users/D/root-6-12-04-02/include/Math/ParamFunctor.h:73:4: note: in instantiation of member function 'ROOT::Math::ParamFunctorHandler\<ROOT::Math::ParamFunctorTempl, double (_)(double _)\>::operator()’ requested here  
ParamFunctorHandler(const Func & fun) : fFunc(fun) {}  
^  
/Users/D/root-6-12-04-02/include/Math/ParamFunctor.h:304:17: note: in instantiation of member function 'ROOT::Math::ParamFunctorHandler\<ROOT::Math::ParamFunctorTempl, double (_)(double _)\>::ParamFunctorHandler’ requested  
here  
fImpl(new ParamFunctorHandler\<ParamFunctorTempl,Func\>(f) )  
^  
/Users/D/root-6-12-04-02/include/TF1.h:718:62: note: in instantiation of function template specialization 'ROOT::Math::ParamFunctorTempl::ParamFunctorTempl\<double (_)(double _)\>’ requested here  
f-\>fFunctor = new TF1::TF1FunctorPointerImpl\<Fnc\_t\>(ROOT::Math::ParamFunctorTempl\<Fnc\_t\>(func));  
^  
/Users/D/root-6-12-04-02/include/TF1.h:361:41: note: in instantiation of member function 'ROOT::Internal::TF1Builder\<double (_)(double _)\>::Build’ requested here  
ROOT::Internal::TF1Builder::Build(this, f);  
^  
/Users/D/Documents/cross\_section/pipi\_fitting/Omnes\_fitting.cpp:43:21: note: in instantiation of function template specialization 'TF1::TF1\<double (_)(double \*)\>’ requested here  
TF1 \*omnes= new TF1(“omnes”, Omnes\_fit,0.0,1.0,2);  
^  
root [1]

Any help would be greatly appreciated.

Thanks  
Dil  
[Omnes\_fitting.cpp](https://root-forum.cern.ch/uploads/short-url/9Qs3dGT2EzVLYbpJaTfzJKUFf8o.cpp) (3.5 KB)  
[Omnes\_fitting.hpp](https://root-forum.cern.ch/uploads/short-url/zGYwn52DTofYk268svoFYHkewKs.hpp) (236 Bytes)  
[Omnes00KM.txt](https://root-forum.cern.ch/uploads/short-url/bxlyHtvF6EBNervbSBxKmIespew.txt) (9.4 KB)  
[plots\_1.root](https://root-forum.cern.ch/uploads/short-url/yLxVeHh5EwieiNWbEusAx2Je3YS.root) (695.2 KB)  
[Omnes11.txt](https://root-forum.cern.ch/uploads/short-url/byHwfwg7EOanuSmBdviPn6vYnY3.txt) (9.4 KB)

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [April 28, 2022, 4:46am UTC](https://root-forum.cern.ch/t/reading-text-file-and-fit-the-plot/49776/2 "2022-04-28T04:46:59Z")

</div>

> [@Dil](#):
>
> `Double_t Omnes_fit(Double_t *par){`

Should be

```auto
Double_t Omnes_fit(Double_t *x, Double_t *par){

```

See “Creating a TF1 with a User Function” on [Chapter: Fitting Histograms](https://root.cern/root/htmldoc/guides/users-guide/FittingHistograms.html)  
Also, these returns? this won’t do what I guess you think

```auto
    return mPts;
    return result;

```

---

<div class="post-metadata">

**Author:** ![Dil](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dil/32/26887_2.png) [@Dil](https://root-forum.cern.ch/u/Dil)\
**Post date:** [April 28, 2022, 1:07pm UTC](https://root-forum.cern.ch/t/reading-text-file-and-fit-the-plot/49776/3 "2022-04-28T13:07:57Z")

</div>

Thank you for the reply.That didn’t fix the issue.  
Any example that uses txt files to create a function would help me.

Thanks  
Dil
