Histogram Stacks

Hello All,

I am trying something very simple: open multiple root files, plot a histogram and fit a function. I can do that for a single root file without error but am stumped about how to draw histograms from several root files.

My code (stripped down) is :

[code]{
gROOT->Reset();
gStyle->SetOptFit(1011);

THStack *hs;
hs = new THStack("hs","TOF Stack");

char CUTS[300];
sprintf(CUTS,"energy >= 100");
sprintf(CUTS,"%s && energy <= 1200",CUTS);


 TFile f("File1.root"); 
     EventTree->Draw("time1-time2>>TOFHIST1",CUTS,"N"); 
     TH1* TOF_Hist1 = gPad->GetListOfPrimitives()->FindObject("TOFHIST1");
     f.Close();

TFile f("File2.root"); 
    EventTree->Draw("time1-time2>>TOFHIST2",CUTS,"N"); 
    TH1* TOF_Hist2 = gPad->GetListOfPrimitives()->FindObject("TOFHIST2");
    f.Close();

TOF_Hist1->SetLineColor(2); TOF_Hist1->Fit("gaus","Q"); hs->Add(TOF_Hist1);
TOF_Hist2->SetLineColor(3); TOF_Hist1->Fit("gaus","Q"); hs->Add(TOF_Hist2);

TCanvas c1("c1","stacked hists",10,10,700,900);
hs->Draw();
c1->SaveAs("TimingDifference.png");

}[/code]

I want the fit parameters on the canvas that is saved.

When I run this (root -l -b -q FitFile.C), I get a segmentation fault.

What am I messing up? Thanks

Karthik.

Many problems in your script. I suggest to change it as follows:

Rene

[code]void kart() {
gStyle->SetOptFit(1011);
TCanvas *c1 = new TCanvas(“c1”,“stacked hists”,10,10,700,900);

THStack *hs;
hs = new THStack(“hs”,“TOF Stack”);

char CUTS[300];
sprintf(CUTS,“energy >= 100 && energy <= 1200”);

TFile f = TFile::Open(“File1.root”);
TTree EventReee = (TTree)f->Get(“EventTree”);
EventTree->Draw(“time1-time2>>TOFHIST1”,CUTS,“goff”);
TH1
TOF_Hist1 = f->FindObject(“TOFHIST1”);
TOF_Hist1->SetDirectory(0);
delete f;

f = TFile::Open(“File2.root”);
EventReee = (TTree*)f->Get(“EventTree”);
EventTree->Draw(“time1-time2>>TOFHIST2”,CUTS,“goff”);
TH1* TOF_Hist2 = f->FindObject(“TOFHIST2”);
TOF_Hist2->SetDirectory(0);
delete f;

TOF_Hist1->SetLineColor(2); TOF_Hist1->Fit(“gaus”,“Q”); hs->Add(TOF_Hist1);
TOF_Hist2->SetLineColor(3); TOF_Hist1->Fit(“gaus”,“Q”); hs->Add(TOF_Hist2);

hs->Draw();
c1->SaveAs(“TimingDifference.png”);
}
[/code]

Hi, did not want to create a separate topic for this, in my quite big macro, but also in the tutorial example[1], if I try to save the canvas as .C file, it is not working, since in the tutorial script it is:
TH1F *h1st = new TH1F(“h1st”,“test hstack”,100,-4,4);

hs->Add(h3st);

while the canvas saved macro contains:
TH1F *h1st__1 = new TH1F(“h1st__1”,“test hstack”,100,-4,4);

hs->Add(h1st,"");
so there is a mismatch between the original names and what is stored and called by stack.

Do you happen to know if there is a simple way to fix it?
Cheers.

[1]
https://root.cern.ch/root/html/tutorials/hist/hstack.C.html