I have a simple program, which works fine. It reads one-by-one a sort of files (with their respective name stored in a file “data.list”), then sums the corresponding bins up.
I changed the c++ to root-based script, so as I can fit the summed histogram and show the result.
Although it runs and give me the same result as c++ code, ROOT always crash and shows errors like:
*** glibc detected *** /usr/local/bin/root.exe: double free or corruption (!prev): 0x09969b80 ***
======= Backtrace: =========
/lib/libc.so.6(+0x6c501)[0xe0a501]
/lib/libc.so.6(+0x6dd70)[0xe0bd70]
/lib/libc.so.6(cfree+0x6d)[0xe0ee5d]
…
Can any one help to locate where my mistake is?
I post the relevant c++ here. Since the original C++ works fine, I guess that it might relate to CINT.
Thanks in advance!
// read "data.list", which contains the name of files to be read in
// read the corresponding file, and then sum the corresponding channel up
// root > .L peaks.C (generate 10 peaks by default)
// root > peaks("frag004.root") // to run
//#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <cmath>
#include <stdlib.h>
#include <fstream>
#include "TROOT.h"
#include "TCanvas.h"
#include "TMath.h"
#include "TH1.h"
#include "TF1.h"
#include "TRandom.h"
#include "TSpectrum.h"
#include "TVirtualFitter.h"
#include "TStyle.h"
using namespace std;
#define Draw
//#undef Draw
void sum(string filename = "data.list1") {
//Int_t main() {
// void sum(){
gStyle->SetPalette(1);
gStyle->SetOptStat(0);
Int_t nbins = 375;
Int_t ystart = 40;
Int_t yend = 220;
Int_t naverage = 20;
Double_t dtime = 0.032; //s
Int_t i,ifile=0, ri;
Double_t rvalue, rerr;
string lname,ldata;
//char filename[] = "data.list";
struct hist{
Double_t value;
Double_t err;
}histogram[nbins];
for(i=0;i<nbins;i++)
{
histogram[i].value=0.0;
histogram[i].err=0.0;
}
// read the filelist
ifstream fin(filename.c_str(),ios::in);
if( ! fin)
{
cerr << "ERROR: Cannot open list file: " << filename << endl;
return ;
}
while(! std::getline(fin,lname).eof())
{
if((lname.find_first_of('#',0) == string::npos)||(lname.length()>3))
{
cout << "Now analyzing file " << lname.c_str() << endl;
ifile++;
// open the hist file
ifstream fhist(lname.c_str(),ios::in);
if(! fhist)
{
cerr << "ERROR: cannot open hist file : " << lname.c_str() << endl;
return;
}
// open the file
i=1;
while(! std::getline(fhist,ldata).eof())
{
if(( ldata.find_first_of('#',0) == string::npos)&&(ldata.length()>3) )
{
istringstream hist_line; //creat a string stream for reading
hist_line.clear();
hist_line.str(ldata);
hist_line >> ri >> rvalue >> rerr;
//if(ri==1) cout << ri << " " << rvalue << " " << rerr << endl;
histogram[i].value += rvalue;
histogram[i].err += rerr*rerr;
i++;
}
}
fhist.close();
cout << " file: " << lname.c_str() << " closed " << endl;
} // if((lname.find_first_of
} // while(! std::getline(fin,lname).eof()
fin.close();
}