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();
}
}