Array error

Hi,

So I have stared at this problem for quite a while now and I just can’t seem to fix it the error message I got is :
Error: Array index out of range m -> [1201] valid upto m[1199] GaussianResonance.C:43:
*** Interpreter error recovered ***

Attached is the txt file.

It seems like it is a silly syntax problem? I wonder if the — in some of the TXT file that messing it up. and if I just need to skip through a couple of these lines (----) are there ways for me to do it easily? Thanks.

//
// GaussianResonance.C

#include <stdio.h>
#include “TGraphErrors.h”
#include “TCanvas.h”
#include “TFile.h”
#include
#include “TFile.h”
#include “TGraph.h”
#include

using namespace std;
void GaussianResonance(void) {

TFile *CrossSection = new TFile("CrossSection2.root", "RECREATE");
TCanvas *c2 = new TCanvas();
c2->SetLogy();

int i, j=0, k=0;
ifstream infile;
double m[1200], pt07[1200],pt10[1200],pt15[1200],res[1200];

infile.open("GaussianResonance.txt");

for(i=0;i<1200;i++) { m[i]=0; res[i]=0, pt07[i]=0; pt10[i]=0; pt15[i]=0;}

while (!infile.eof()) {
    infile >>m[k]>>res[k]>>pt07[k]>>pt10[k]>>pt15[k];
    // cout<<"Mass: "<<m[k]<<"  "<<c[k]<<"  "<<br[k]<<"  "<<a[k]<<endl;
    k=k+1;
    //cout <<"k: "<<k;
}

//k--;
cout<<"Test";



TGraph *gr3= new TGraph(k-1,m, pt07);
gr3->SetNameTitle("gr3","Cross Section*Branching Ratio*Acceptance vs Mass jj; Mass jj (GeV);Cross Section*branching ratio*acceptance(pb)");
//  gr3->SetName("gr3");

gr3->SetMaximum(10e+5);
gr3->SetMinimum(10e-10);
gr3->SetLineColor(kGreen);
gr3->SetLineWidth(4);
gr3->SetMarkerColor(kBlack);
gr3->SetMarkerStyle(21);
// gr3->Write();

TGraph *gr4= new TGraph(k-1,m,pt10);
gr4->SetNameTitle("gr4","Cross Section*Branching Ratio*Acceptance vs Mass jj; Mass jj (GeV);Cross Section*branching ratio*acceptance(pb)");
gr4->SetLineColor(kViolet+6);
gr4->SetLineWidth(4);
gr4->SetMarkerColor(kBlack);


TGraph *gr5= new TGraph(k-1,m,pt15);
gr4->SetNameTitle("gr4","Cross Section*Branching Ratio*Acceptance vs Mass jj; Mass jj (GeV);Cross Section*branching ratio*acceptance(pb)");
gr4->SetLineColor(kBlue);
gr4->SetLineWidth(4);
gr4->SetMarkerColor(kBlack);

c2->cd();
gr3->Draw("ALP");
gr4->Draw("LP");
gr5->Draw("LP");

c2->Modified();
c2->Update();

gDirectory->Append(gr3);
CrossSection->Write();

}
Gaussian_Resonance.txt (1.4 KB)

Hi,

See attached, fixed version.

Cheers, Axel.
GaussianResonance.C (1.83 KB)

Hi Thanks for helping me fix the problem.
I wonder why I can’t do what I did though, why do I have to first read it as a string?
Thanks!

Hi,

See e.g. stackoverflow.com/questions/4774 … -next-line

Axel.

I think this is another extension to this problem…
So I change the way the infile is inputted and it gives me a different error.

The error message : Error: String literal syntax error ReadingCrossSectionMass2.C:76:
5 15(class G__CINT_ENDL)140556477109136
*** Interpreter error recovered ***

[code]//
// ReadingCrossectionMass.C

//
//

#include <stdio.h>
#include
#include “TFile.h”
#include “TGraph.h”
#include
#include
#include
#include <
//#include

using namespace std;

void ReadingCrossSectionMass2(void);
//Declaration
void ReadingCrossSectionMass2(void) {

TFile *CrossSection = new TFile("CrossSectionSameSignedLeptons.root", "RECREATE");

const int nF=5;
const int nP=7;
//Number of Files
int i, j=0, k=0;
double endtime;
ifstream infile;
double m[nP], c[nP];
vector Filename;
vector CompleteFileName;
string file =“abc”;

// Filename=(6,{“eMinusEMinus”,“eMinusMuMinus”,“ePlusEPlus”,“ePlusMuPlus”,“muMinusMuMinus”,“muPlusMuPlus”});
//Filename[6]={“eMinusEMinus”,“eMinusMuMinus”,“ePlusEPlus”,“ePlusMuPlus”,“muMinusMuMinus”,“muPlusMuPlus”};
Filename.push_back(string(“eMinusEMinus”));
Filename.push_back(string(“eMinusMuMinus”));
Filename.push_back(“ePlusEPlus”);
Filename.push_back(“ePlusMuPlus”);
Filename.push_back(“muMinusMuMinus”);
Filename.push_back(“muPlusMuPlus”);

string extention=".txt";

// string[6] CompleteFileName;

for (i=0; i<Filename.size(); i++)
{
    cout<<"file name:"<<Filename.at(i)<<endl;
    CompleteFileName.push_back(Filename.at(i)+extention);
    cout<<"Complete File Name: "<<CompleteFileName.at(i)<<endl;
}

TGraph *mvc[nF];

for (i=0; i<nF; i++)
{
infile.open(CompleteFileName.at(i).c_str());

for(j=0;j<nP;j++)
{m[j]=0; c[j]=0;}


if (!infile) {
    printf("ERROR: cannot open input file");
    return;
}

for (std::string line; std::getline(infile, line); ++k)
{
    std::istringstream strstrm(line);
    strstrm >>m[k]>>c[k];    				//this is where the error is at.
    cout<<nF<<"   "<<m[k]<<"   "c[k]<<endl;
}

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

//    for(i=0;i<k;i++) {p[i]=p[i]*3.386388; cout<<t[i]<<"  "<<p[i]<<endl;
//        endtime=t[i];
//        cout<<endtime;}

mvc[i] = new TGraph(k,m,c);
mvc[i]->SetName(Form("mvc %d",i));
mvc[i]->SetMarkerStyle(20);
mvc[i]->SetTitle(Form("Cross Section Resolution vs Mass %s",CompleteFileName.at(i)) );
mvc[i]->GetXaxis()->SetTitle("Mass (GeV)");
mvc[i]->GetYaxis()->SetTitle("Cross Section (pB)");
mvc[i]->Draw("ALP");

// gDirectory->Append(mvc);
mvc[i]->Write();

}
CrossSection->Write();

}[/code]

Hi,

ROOT 6 helps:

root [0] .x ~/Documents/ReadingCrossSectionMass2.C
In file included from input_line_9:1:
/Users/axel/Documents/ReadingCrossSectionMass2.C:14:10: fatal error: '<sstream' file not found
#include <<sstream>
         ^

:slight_smile:

Fixing that I get

Users/axel/Documents/ReadingCrossSectionMass2.C:77:37: error: invalid suffix on literal; C++11 requires a space between
      literal and identifier [-Wreserved-user-defined-literal]
        cout<<nF<<"   "<<m[k]<<"   "c[k]<<endl;
                                    ^

ROOT 6 can be really helpful :slight_smile: As are compilers: I’d recommend to run .x ReadingCrossSectionMass2.C++ to invoke your compiler on your source file if you don’t understand what CINT is trying to say.

Axel.