Hallo,
I have a problem with reading ASCII files in loop and creating histograms from them. Here is the code:
using namespace TMath;
void energy2()
{
TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName());
dir.ReplaceAll("energy2.C","");
dir.ReplaceAll("/./","/");
dir += "Messung_03.08.16_50V";
cout<<dir<<endl<<endl<<endl;
TSystemDirectory dir_list(dir.Data(), dir.Data());
TList* files = dir_list.GetListOfFiles();
files->Sort(1);
files->Print(); //just to know, that list is not empty (and it's not)
TH1F *Rel_counts = new TH1F("Rel_counts", "Counts in time", 72, 0, 72);
Int_t counts = 0;
for(int i=2; i<74; ++i)
{
ifstream in;
TString fname = (files->At(i))->GetName();
in.open(Form(dir.Data(),fname.Data()));
if(in.is_open())
{
cout<<fname<<endl;
string line;
getline(in,line);
cout<<line<<endl;
}
TH1F *Am241 = new TH1F("Am241","Am241",1024,1,1024);
//string line;
Float_t dat;
Int_t chan = 0;
Int_t max = 0;
Int_t temp = counts;
counts = 0;
Int_t difference = 0;
while(getline(in, line))
{
if(line[0] == ' ')
{
chan++;
istringstream str(line);
str>>dat;
Am241->AddAt(dat, chan);
}
}
for(int k=0; k<=1024; k++)
{
counts += Am241->GetBinContent(k);
}
difference = counts - temp;
Rel_counts -> AddAt(difference, i-2);
in.close();
}
TCanvas *histo_Rc = new TCanvas("histo_Rc","Counts in time",1);
Rel_counts -> Draw();
delete files;
}
It looks as if files are opening, what I check in this test:
if(in.is_open())
{
cout<<fname<<endl;
string line;
getline(in,line);
cout<<line<<endl;
}
But they looks like empty files and I know that the first line of file, which I’m calling, is not empty.
Any idea what is happening here?
Thanks!
ps. Sorry for mess in code. It’s probably because of copying.