Ofstream in for-loop

Hey there,

I’m trying to save data from histograms created in a for-loop in a dat-file in each iteration. However if I type “return;” after closing the output, ROOT stops after running through the loop once, if I don’t, there’s a error. What’s the problem?

My code:

ofstream output ("out.dat"); output << fit->GetParameter(1) << endl; output << fit->GetChisquare() << endl; output.close(); return 0;

Thanks for your help!

Kati

Can you, please, give more complete code?

The complete code is:

[code]{

gROOT->Reset();
#include “TSystem.h”
#include
#include
using namespace std;

const char* inDir = “/data/PhD/Data\ Analysis/w01s04-hist/”;
const char* ext = “.csv”;

char* dir = gSystem->ExpandPathName(inDir);
void* dirp = gSystem->OpenDirectory(dir);
//gSystem->FreeDirectory(dirp);

if(!dirp) {
printf(“Failed to open directory: %d\n”,inDir);
return;
}

const char* entry;
const char* filename[100];
FILE *inp;
TString str;
Int_t j;
ofstream output;

while(entry = gSystem->GetDirEntry(dirp)) {
str = entry;

if( str.EndsWith(ext) ) {
  
  filename[j++] = gSystem->ConcatFileName(dir,entry);
}

for(Int_t i=0;i<j;i++){
        
  FILE *inp = fopen(filename[i],"r");
  float x,y1,y2,y3,y4,y5,y6,y7,y8;
  
  Double_t nbins = 4100;
  Double_t xmin = 0;
  Double_t xmax = (Double_t)nbins;
  
  TCanvas *c1 = new TCanvas();
  TH1F *adc = new TH1F("adc",entry,nbins,xmin,xmax);
  
  /* Histogramm wird mit Daten gefüllt */
  for(int k=1;k<nbins;k++){

fscanf(inp,"%f; %f; %f; %f; %f; %f; %f; %f; %f;",&x,&y1,&y2,&y3,&y4,&y5,&y6,&y7,&y8);
adc->Fill(x,y5);
  }
  
  fclose(inp);
        
  adc->SetMaximum(800);
  //c1->SetLogy(1);
  
  Double_t par[12];
  TF1 *g1 = new TF1("g1","gaus",1450,1650);
  TF1 *g2 = new TF1("g2","gaus",1660,1800);
  TF1 *g3 = new TF1("g3","gaus",2000,2180);
  TF1 *g4 = new TF1("g4","gaus",2200,2450);
  TF1 *total = new TF1("total","gaus(0)+gaus(3)+gaus(6)+gaus(9)",1450,2450);
  adc->Fit("g1","R");
  adc->Fit("g2","R+");
  adc->Fit("g3","R+");
  adc->Fit("g4","R+");
  g1->GetParameters(&par[0]);
  g2->GetParameters(&par[3]);
  g3->GetParameters(&par[6]);
  g4->GetParameters(&par[9]);
  total->SetParameters(par);
  adc->Fit("total","R+");
             
  /* /\* Zeichne Histogramm *\/ */
  c1->Update();
  adc->Draw("L");
  total->SetLineColor(kRed);
  total->Draw("SAME");
  
  /* Schreibe Plot in png-File */
  gSystem->ProcessEvents();
  char imgname[150];
  sprintf(imgname,"/data/PhD/Data\ Analysis/w01s04-hist/spec_plots/gauss_%s.png",entry);
  c1->SaveAs(imgname);
       
  Float_t chpos1 = total->GetParameter(1);
  Float_t sig1 = total->GetParameter(2);
  Float_t chpos2 = total->GetParameter(4);
  Float_t sig2 = total->GetParameter(5);
  Float_t chpos3 = total->GetParameter(7);
  Float_t sig3 = total->GetParameter(8);
  Float_t chpos4 = total->GetParameter(10);
  Float_t sig4 = total->GetParameter(11);

        
  /* Schreibe Daten-Output-File für Gauss-Fit-Peaks */
  char outname[150];

  sprintf(outname,"/data/PhD/Data\ Analysis/w01s04-hist/dat/gauss_peaks_%s_1.dat",entry);

  output.open(outname);
  output << "Peak " << "Position[keV] " << "Sigma " << endl;
  output << "Ti-KAlpha " << chpos1 << "  " << sig1 << endl;
  output << "Ti-KBeta " << chpos2 << "  " << sig2 << endl;
  output << "Mn-KAlpha " << chpos3 << "  " << sig3 << endl;
  output << "Mn-KBeta " << chpos4 << "  " << sig4 << endl;
  output.close();
  return 0;
  
  delete total, g1, g2, g3, g4;

  delete adc;
  c1->Close();
}

}

return;
}
[/code]

Hi,

remove the return statement, it doesn’t make sense as it kicks you out of the loop (or I just don’t understand why you have it). But then again I don’t understand your looping - you iterate over all entries in a dir (j - which you forgot to initialize to 0), and then for the say 5th file you iterate again over all files you’ve found up to there (i=0…4)? I don’t think that this is what you want… To understand what I’m talking about add the line
printf(“I’m at loop j=%d, i=%d, and reading the file %s\n”, j, i, filename[i])
right before your fopen() call.

What is the error message you saw without the return statement in the middle of the loop?
Axel.

Hi Axel,

You were right, the for-loop was not necessary… I just kind of overlooked its lack of meaning while developping the code. Now it works, thanks a lot for your help!

Cheers,
Kati