Hello, I should fill an histogram from data file.
The file has some text files before the data, then I get an empty plot.
Is there a way to automatically ignore the first lines without manually deleting them (i.e.
void depenetxt()
{
double DepEne; //Released energy
int entry=1;
gSystem->mkdir(TString::Format("simulazione" ), kTRUE);
TGaxis::SetMaxDigits(3);
FILE * infile=fopen("data.txt", "r");
if(infile==NULL)
{
printf("Error opening file");
exit(1);
}
TCanvas *c0 = new TCanvas("c0","c0",1280,1024);
int num=57;
int bin;
c0->SetLogy();
TH1F *histo = new TH1F("histo","DepositedEnergy",2500,0.,2500); //
char line[40];
Int_t i = -4; // skip 4 lines
while (fgets(line,40,infile)) {
if (i>=0) {
sscanf(&line[0],"%d %lf",&bin,&DepEne);
histo->Fill(DepEne);
}
i++;
}
fclose(infile);
histo->SetTitle("");
gStyle->SetTitleFontSize(0.08);
histo->GetXaxis()->SetTitle("Energy (keV)");
histo->GetYaxis()->SetTitle("Counts");
histo->GetYaxis()->SetTitleSize(40);
histo->GetYaxis()->SetTitleFont(43);
histo->GetYaxis()->SetTitleOffset(1);
histo->GetYaxis()->SetLabelFont(43);
histo->GetYaxis()->SetLabelSize(40);
histo->GetXaxis()->SetTitleSize(40);
histo->GetXaxis()->SetTitleFont(43);
histo->GetXaxis()->SetTitleOffset(1.1);
histo->GetXaxis()->SetLabelFont(43);
histo->GetXaxis()->SetLabelSize(40);
histo->SetStats(0);
histo->Draw();
}
Note that your file is not very informative, the first column is an index n growing by step of 1 from 0 to 2500 and the 2nd one (the one you put in the histogram) is n+0.5.
The first culomn is the bin number of histogram, the second one the mean value of the beam got by GEANT.
Unfortunately, my boss can’t use ROOT, then I’m going crazy using strange text files that he needs to use them with some scripts that it has (I don’t know for wich toolkit)
Yes that’s what I mean, the first column is an index and the 2nd just the first column +0.5 (yes middle of the bin) . If you do an histogram with that yo will get a flat line … That’s why I said it is not very interesting.