Drawing a Step Function

Hi I tried drawing a step Function but ran into a segmentation error. I tried reducing the array number to 7( the minimum) but I can’t seem to fix it. I wonder if I have missed something or misunderstood some root convention.
Thanks in advance.

[code]#include <stdio.h>
#include “TFile.h”
#include “TF1.h”
#include
#include
#include
#include “TGraph.h”
#include “TGraphErrors.h”
#include “TCanvas.h”
#include “TMultigraph.h”

using namespace std;

void FiducialLimit(void){
TF1 *f[7];
TGraph *gr[7];

//F is the different y values of the step-wise function for this particular graph
//100 is the number of steps possible in this function
double m[7], c[7];

int i, j=0, k=1;
int n;
ifstream infile;
string fileName;


TMultiGraph *mg = new TMultiGraph;

// mg->GetXaxis()->SetLimit(0, 600);
mg->SetMaximum(10^3);
mg->SetMinimum(0.001);

std::cout<<"What is the file name(including the .txt ext)?"<<endl;
cin>>fileName;

//Declaring the size dependent/name independent variables
TFile *CrossSection = new TFile(Form("/Users/Desktop/Working_Programs/Temp_Root_Files/%s.root",fileName.c_str()), "RECREATE");


//reading in the points in the txt file.

infile.open(Form("/Users/Desktop/Working_Programs/Input_txt/Fiducial_CrossSection/%s",fileName.c_str()));

while (!infile.eof()) {
    infile >>m[k]>>c[k];
    cout<<m[k]<<"   "<<c[k]<<endl;
    k++;
    
}
k--;

m[0]=0;//The X-axis starts at 0


for (i=1; i<k; i++){
    f[i]->SetParameter(0,c[k]);

    f[i]= new TF1(Form("f%f",m[k]),"[0]",m[k-1],m[k]);
    gr[i]->Fit(Form("f[%d]",i),"R");

    mg->Add(gr[i]);
    
}
mg->SetTitle("Cross Section(pB) vs Mass(GeV),Mass (GeV),Cross Section(pB)");
//mg->GetYaxis()->SetTitle("Cross Section(pB)");
TCanvas *c1= new TCanvas();
c1->SetLogy();
mg->Draw("APL");

}[/code]

Error:

root [0] .x FiducialLimit.C+
Info in TMacOSXSystem::ACLiC: creating shared library /Users/Desktop/Working_Programs/./FiducialLimit_C.so
What is the file name(including the .txt ext)?
ePlusEPlus.txt
15 0.032
100 0.014
200 0.0053
300 0.0033
400 0.00203
500 0.0011
600 0.00102

*** Break *** segmentation violation
Generating stack trace…
0x000000010a4d58c8 in Cint::G__ExceptionWrapper(int ()(G__value, char const*, G__param*, int), G__value*, char*, G__param*, int) (in libCint.so) + 49
0x000000010a5631d8 in G__execute_call (in libCint.so) + 75
0x000000010a56363a in G__call_cppfunc (in libCint.so) + 866
0x000000010a542a0b in G__interpret_func (in libCint.so) + 3624
0x000000010a5322f1 in G__getfunction (in libCint.so) + 5802
0x000000010a527beb in G__getitem (in libCint.so) + 511
0x000000010a52360c in G__getexpr (in libCint.so) + 29988
0x000000010a51c02c in G__calc_internal (in libCint.so) + 783
0x000000010a596fdb in G__process_cmd (in libCint.so) + 15800
0x0000000109e853db in TCint::ProcessLine(char const*, TInterpreter::EErrorCode*) (in libCore.so) + 773
0x0000000109e8566c in TCint::ProcessLineSynch(char const*, TInterpreter::EErrorCode*) (in libCore.so) + 116
0x0000000109df8f7a in TApplication::ExecuteFile(char const*, int*, bool) (in libCore.so) + 2380
0x0000000109df7e45 in TApplication::ProcessLine(char const*, bool, int*) (in libCore.so) + 1509
0x000000010abbaeea in TRint::HandleTermInput() (in libRint.so) + 678
0x0000000109eb72af in TUnixSystem::CheckDescriptors() (in libCore.so) + 171
0x0000000109ec032d in TMacOSXSystem::DispatchOneEvent(bool) (in libCore.so) + 261
0x0000000109e4c894 in TSystem::InnerLoop() (in libCore.so) + 26
0x0000000109e4c7a9 in TSystem::Run() (in libCore.so) + 353
0x0000000109df91da in TApplication::Run(bool) (in libCore.so) + 36
0x000000010abba85d in TRint::Run(bool) (in libRint.so) + 1409
0x0000000109de8e1f in main (in root.exe) + 79
0x00007fff84f285fd in start (in libdyld.dylib) + 1
Root >
ePlusEPlus.txt (75 Bytes)

You have 7 lines in “ePlusEPlus.txt” and you read them as “infile >>m[k]>>c[k];” starting with “k = 1;”, so you should have “double m[(7+1)], c[(7+1)];”.
You use “gr[i]” but you nowhere create any of these graphs.
BTW. You should have: gr[i]->Fit(f[i], “R”);

hi So these are the corrected code.
Thanks for your help! But it is still giving me the same error.

[code]
#include <stdio.h>
#include “TFile.h”
#include “TF1.h”
#include
#include
#include
#include “TGraph.h”
#include “TGraphErrors.h”
#include “TCanvas.h”
#include “TMultigraph.h”

using namespace std;

void FiducialLimit(void){
TF1 *f[8];
TGraph *gr[8];

//F is the different y values of the step-wise function for this particular graph
//100 is the number of steps possible in this function
double m[7], c[7];

int i, j=0, k=1;
int n;
ifstream infile;
string fileName;


TMultiGraph *mg = new TMultiGraph;

// mg->GetXaxis()->SetLimit(0, 600);
mg->SetMaximum(10^3);
mg->SetMinimum(0.001);

for (i=0; i<k; i++)
{
    gr[i]= new TGraph;

}



std::cout<<"What is the file name(including the .txt ext)?"<<endl;
cin>>fileName;

//Declaring the size dependent/name independent variables
TFile *CrossSection = new TFile(Form("/Users/yvonne/Desktop/Whiteson_Lab/Working_Programs/Temp_Root_Files/%s.root",fileName.c_str()), "RECREATE");


//reading in the points in the txt file.

infile.open(Form("/Users/yvonne/Desktop/Whiteson_Lab/Working_Programs/Input_txt/Fiducial_CrossSection/%s",fileName.c_str()));

while (!infile.eof()) {
    infile >>m[k]>>c[k];
    cout<<m[k]<<"   "<<c[k]<<endl;
    k++;
    
}
k--;

m[0]=0;//The X-axis starts at 0


for (i=1; i<k; i++){
    f[i]->SetParameter(0,c[k]);
    f[i]= new TF1(Form("f%f",m[k]),"[0]",m[k-1],m[k]);
    gr[i]->Fit(f[i],"R");

    mg->Add(gr[i]);
    
}
mg->SetTitle("Cross Section(pB) vs Mass(GeV),Mass (GeV),Cross Section(pB)");
//mg->GetYaxis()->SetTitle("Cross Section(pB)");
TCanvas *c1= new TCanvas();
c1->SetLogy();
mg->Draw("APL");

}[/code]

Error: root [0] .x FiducialLimit.C+
What is the file name(including the .txt ext)?
ePlusEPlus.txt
15 0.032
100 0.014
200 0.0053
300 0.0033
400 0.00203
500 0.0011
600 0.00102

*** Break *** segmentation violation
Generating stack trace…
0x000000010a7498c8 in Cint::G__ExceptionWrapper(int ()(G__value, char const*, G__param*, int), G__value*, char*, G__param*, int) (in libCint.so) + 49
0x000000010a7d71d8 in G__execute_call (in libCint.so) + 75
0x000000010a7d763a in G__call_cppfunc (in libCint.so) + 866
0x000000010a7b6a0b in G__interpret_func (in libCint.so) + 3624
0x000000010a7a62f1 in G__getfunction (in libCint.so) + 5802
0x000000010a79bbeb in G__getitem (in libCint.so) + 511
0x000000010a79760c in G__getexpr (in libCint.so) + 29988
0x000000010a79002c in G__calc_internal (in libCint.so) + 783
0x000000010a80afdb in G__process_cmd (in libCint.so) + 15800
0x000000010a0fe3db in TCint::ProcessLine(char const*, TInterpreter::EErrorCode*) (in libCore.so) + 773
0x000000010a0fe66c in TCint::ProcessLineSynch(char const*, TInterpreter::EErrorCode*) (in libCore.so) + 116
0x000000010a071f7a in TApplication::ExecuteFile(char const*, int*, bool) (in libCore.so) + 2380
0x000000010a070e45 in TApplication::ProcessLine(char const*, bool, int*) (in libCore.so) + 1509
0x000000010ae2feea in TRint::HandleTermInput() (in libRint.so) + 678
0x000000010a1302af in TUnixSystem::CheckDescriptors() (in libCore.so) + 171
0x000000010a13932d in TMacOSXSystem::DispatchOneEvent(bool) (in libCore.so) + 261
0x000000010a0c5894 in TSystem::InnerLoop() (in libCore.so) + 26
0x000000010a0c57a9 in TSystem::Run() (in libCore.so) + 353
0x000000010a0721da in TApplication::Run(bool) (in libCore.so) + 36
0x000000010ae2f85d in TRint::Run(bool) (in libRint.so) + 1409
0x000000010a061e1f in main (in root.exe) + 79
0x00007fff84f285fd in start (in libdyld.dylib) + 1
Root >

You still have the “k=1;” problem that I described in the previous post.
You create “empty” graphs now -> I don’t think you can “Fit” them or “Add” them to a multigraph.
I also noticed that you “f[i]->SetParameter(0,c[k]);” before you actually create the “f[i]” function.
BTW. it should be: #include “TMultiGraph.h”

Hi Thanks for you input. How should I define the graphs then, if I can’t make them empty?
Thanks

I would say that each of graphs should have at least 2 points (otherwise different methods may break).

Hi. How exactly would I do that?(declaring a graph with at least 2 points only)
I am only familiar with the empty declaration. and TGraph(n, x,y)
Thanks

It’s up to you to provide “data points” for all your graphs.
BTW. I noticed that you actually create “gr[0]” only (as “k=1;” when you try to create “gr[i]”).

but what I was trying to do is actually to use the TF1 instead of the points(I wanna make a step function)
Is this not the right way to do this? how else would I go about making a step function?
Thanks