Hi there,
I am going crazy with THStack and need some help to find the mistake I’m apparently making. I loop over a couple of files, each filling multiple histograms and adding them to several THStacks hst[i]
. However, after looping over all files, drawing each of the THStacks to a separate canvas and saving this canvas, only the last histogram is plotted in my canvas.
I simply cannot find any reason why the THStack doesn’t seem to save the individual histograms although they have different names and different indices. A portion of my code is below, hopefully enough for somebody to find my mistake.
For better understanding: fin
and fout
are pointers to in- and output files and nchan*j
is some offset (with j
being the counter of files i have looped over so far) to avoid filling the same histogram again and to add a different histogram to the histogram stack.
Thanks,
Martin
//open output file, remove file extension for name
foutName.Form("%.*s_henergy.root",len-5,filename[j]); //set name of output file
fout = new TFile(foutName,"recreate");
for (int i = 0; i < nchan; i++) {
//write histograms to output file
hE[i+(nchan*j)]->Write();
//if bool "stack" is true, add current histogram to stack
if (stack) {
c[i]->cd(); //switch to canvas corresponding to current channel
hst[i]->Add(hE[i+(nchan*j)]); //add current histogram to stack
hst[i]->Draw("plc nostack"); //draw histogram stack to compare all histograms
//if current file is the last file to be analyzed, write histogram stack to file with legend
if (j == nFiles-1) {
gPad->SetLogy(1); //set logarithmic scale on y axis
TLegend *l1 = (TLegend*)gPad->BuildLegend(0.6,0.8,0.9,0.9,""); //automatically build legend
l1->SetTextFont(43);
l1->SetTextSize(16);
c[i]->Write();
c[i]->Close();
delete c[i]; //delete all instances of canvas' created with new
delete hst[i]; //delete all instances of stacks created with new
}
}
}
cout << "file created: " << foutName << endl << endl; //output for user to see that file was written
//close files and delete them from memory
fout->Close();
fin->Close();
delete fout;
delete fin;
ROOT Version: 6.22/02
Platform: CentOS 7