Single call of procedure works while recurring call in loop returns error.

From: Stephan Zimmer <zimmerst_at_googlemail.com>
Date: Sat, 27 Feb 2010 03:21:19 +0100


Hi ROOTers,
I wonder if someone of you might understand why the following code crashes...

double CotTheta(const double x_i, const double alpha[2]){   // returns the estimate y(x_i) based on the theoretical distribution   return alpha[0]*x_i - alpha[1]*pow(x_i,-1); }

double CalculateChi2(const vector<double> x, const vector<double> y, const vect\
or<double> yError, const double alpha[2]){   // returns the chi2 value for a given set of parameters   double sumchi2=0;
  for (unsigned int i = 0; i<x.size(); i++){     sumchi2=sumchi2+(pow(yError[i],-2)*pow(y[i]-CotTheta(x[i],alpha),2));   }
  return sumchi2;
}

void littleHenry(){

  TVirtualFitter::SetDefaultFitter("Minuit");

  // readout henry.dat

  vector<double> xVec;
  vector<double> xEVec;
  vector<double> yVec;
  vector<double> yEVec;

  double x(0),y(0),xE(0), yE(0);
  double output[3];
  FILE *data;
  char datafname[]="henry.dat";

  data = fopen(datafname,"r");
  while (fscanf(data, "%lf %lf %lf %lf",&x,&xE,&y,&yE) != EOF) {     xVec.push_back(x);
    yVec.push_back(y);
    xEVec.push_back(xE);
    yEVec.push_back(yE);
  }
  double alpha[2];
  double chi2;
  alpha[0]=1e-4;
  alpha[1]=1e2;
  CalculateChi2(xVec,yVec,yEVec,alpha);
  --> that works...

However, once I want to execute the routine CalculateChi2 more than twice, by putting it inside a loop I get a weird error message:   for (int i = 0; i<5; i++){
    chi2=CalculateChi2(xVec,yVec,yEVec,alpha);     cout << "Chi2/Ndf " << chi2/xVec.size() << endl;   }
----> OUTPUT <-------
Processing littleHenry.C...
Chi2/Ndf 528.202
Chi2/Ndf 528.202
Error: Can't call vector<double,allocator<double> >::vector<double,allocator<double> >((class vector<double>***&)148530456) in current scope littleHenry.C:34: Possible candidates are...
(in vector<double,allocator<double> >)

vector.dll       -1:-1   0 public: vector<double,allocator<double> >
vector<double,allocator<double> >::vector<double,allocator<double> >(void);
vector.dll       -1:-1   0 public: vector<double,allocator<double> >
vector<double,allocator<double> >::vector<double,allocator<double>

>(vector<double,allocator<double> >::size_type n,const double&
value=double());
vector.dll -1:-1 0 public: vector<double,allocator<double> > vector<double,allocator<double> >::vector<double,allocator<double>
>(const vector<double,allocator<double> >& x);
vector.dll -1:-1 0 public: vector<double,allocator<double> > vector<double,allocator<double> >::vector<double,allocator<double>
>(vector<double,allocator<double> >::const_iterator
first,vector<double,allocator<double> >::const_iterator last); *** Interpreter error recovered ***

I wonder if I somehow run out of the boundaries of the vector but that should not happen as they are not touched and treated as constants... Any ideas?

Cheers,
Stephan Received on Sat Feb 27 2010 - 03:21:35 CET

This archive was generated by hypermail 2.2.0 : Sat Feb 27 2010 - 05:50:03 CET