Problem drawing histograms in a loop

Hi everyone,

I am not very experienced with root and I want to apologize if this is a dumb question, but I am experiencing some problem when trying to draw histograms from a file and using a loop. I have six different files which hold the same histograms, but for different runs and I would like to draw these on two canvases and stack some of them. My macro will go through a for loop, load the files, extract the histograms, stack them and then draw them for each run (it loops six times). When loading and running the macro, all canvases are empty. The histograms are not drawn. When cutting and pasting the code directly into the CINT interpreter, the histograms are drawn. Could you please let me know what I am missing here and why all canvases are empty when executing the macro? It compiles fine.

Also I clone the histograms, but then I close the file, the histograms disappear from the canvas. How is it possible to open a file, get the histogram and retain it so that the file can be closed again. I am sorry for all these trivial questions, but they have me quite puzzled. Thank you for your time and help. Below is my code.

Thank you

Peter

#include <TString.h>
#include <TH1F.h>
#include <TFile.h>
#include <THStack.h>
#include <TLegend.h>
#include <TCanvas.h>

TString pathData(“data directory”);
TString pathMc(“Monte Carlo directory”);

void drawRawMcToDataComarison() {

//gROOT->SetStyle(“Plain”);
//gStyle->SetOptStat(0);

TH1F* data[7];
TH1F* contBg[7];
TH1F* combBg[7];
TH1F* peakBg[7];
TH1F* mcData[7];
THStack* hStack[7];
TLegend* theLegend;

TCanvas* c[2];

// loop over run numbers
for( int i=1; i<=6; ++i) {

TString filename_f1(pathData + "on");
filename_f1 = filename_f1 + Form("_run%d.root", i);
TString filename_f2(pathData + "off");
filename_f2 = filename_f2 + Form("_run%d.root", i);
TString filename_f3(pathMc + "b0b0");
filename_f3 = filename_f3 + Form("_run%d.root", i);
TString filename_f4(pathMc + "bpbm");
filename_f4 = filename_f4 + Form("_run%d.root", i);

TFile f1(filename_f1);
TFile f2(filename_f2);
TFile f3(filename_f3);
TFile f4(filename_f4);

TH1F* h1001 = (TH1F*)f1.Get("h1001");
TH1F* h2001 = (TH1F*)f2.Get("h2001");
TH1F* h3002 = (TH1F*)f3.Get("h3002");
TH1F* h3004 = (TH1F*)f3.Get("h3004");
TH1F* h4004 = (TH1F*)f4.Get("h4004");
TH1F* h3025 = (TH1F*)f3.Get("h3025");
TH1F* h3026 = (TH1F*)f3.Get("h3026");
TH1F* h3027 = (TH1F*)f3.Get("h3027");
TH1F* h3063 = (TH1F*)f3.Get("h3063");
TH1F* h4025 = (TH1F*)f4.Get("h4025");
TH1F* h4026 = (TH1F*)f4.Get("h4026");
TH1F* h4027 = (TH1F*)f4.Get("h4027");
TH1F* h4063 = (TH1F*)f4.Get("h4063");

data[i] = (TH1F*)h1001->Clone();
data[i]->SetName(Form("data_run%d", i));
contBg[i] = (TH1F*)h2001->Clone();
contBg[i]->SetName(Form("contBg_run%d", i));
combBg[i] = (TH1F*)h3004->Clone();
combBg[i]->SetName(Form("combBg_run%d", i));
combBg[i]->Add(h4004);
peakBg[i] = (TH1F*)h3025->Clone();
peakBg[i]->SetName(Form("peakBg_run%d", i));
peakBg[i]->Add(h3026);
peakBg[i]->Add(h3027);
peakBg[i]->Add(h3063);
peakBg[i]->Add(h4025);
peakBg[i]->Add(h4026);
peakBg[i]->Add(h4027);
peakBg[i]->Add(h4063);
mcData[i] = (TH1F*)h3002->Clone();
mcData[i]->SetName(Form("mcData_run%d", i));    

if( i == 1 || i == 4) {
  c[i/4] = new TCanvas(Form("c%d",i/4), Form("c%d", i/4));
  c[i/4]->Divide(2,3);
}

c[i/4]->cd((i-1)%3 * 2 + 1);
hStack[i] = new THStack(Form("hs_run%d", i), Form("Results Run %d", i));

contBg[i]->SetFillColor(kMagenta);
contBg[i]->SetFillStyle(3777);
hStack[i]->Add(contBg[i]);
combBg[i]->SetFillColor(kRed);
combBg[i]->SetFillStyle(3444);
hStack[i]->Add(combBg[i]);
peakBg[i]->SetFillColor(kBlue);
peakBg[i]->SetFillStyle(3222);
hStack[i]->Add(peakBg[i]);

mcData[i]->Add(contBg[i]);
mcData[i]->Add(combBg[i]);
mcData[i]->Add(peakBg[i]);
mcData[i]->SetLineColor(kGreen);

data[i]->SetTitle(Form("Raw Monte Carlo to data comparison for Run %d", i));
data[i]->Draw("e");
mcData[i]->Draw("same");
hStack[i]->Draw("same");

//f1[i]->Close();
//f2[i]->Close();
//f3[i]->Close();
//f4[i]->Close();

}

}

Hi just a quick update,

I think the problem is that I am defining the files as objects in the for loop and these objects get deleted once the scope ends. I have since moved them out of the for loop as an array of file pointers and it now works as long as I don’t close any of the files. The question I still have is how can I retain the histograms after the files are closed. Apparently cloning them is not enough. Thanks again for the help.

Peter

If you want to manage the histograms yourself and dissociate them from the file, I suggest to add

TH1::AddDirectory(kFALSE): at the beginning of your program. See chapter about object ownership in the users guide.

Rene

[quote=“brun”]If you want to manage the histograms yourself and dissociate them from the file, I suggest to add

TH1::AddDirectory(kFALSE): at the beginning of your program. See chapter about object ownership in the users guide.

Rene[/quote]

Thank you for the quick response. It works nicely now with this addition. I somehow missed the chapter on ownership in the user guide. I will be sure to read more carefully next time before posting. Thank you again for your help.

Peter