I have already made six histograms and they are stored in six different .root files, located not in root directory. I wanted to know how can I superimpose all of them and add a legend to each of them (by writing a macro.C file or just using root terminal).
Load each of them into memory (by opening the different TFiles and Get each one). Create a THStack to put them in, then draw the THStack with the “nostack” option. The gPad should have a useful BuildLegend method, or make a TLegend object and add each of the histograms to it manually. Then draw the TLegend.
Error: Symbol f is not defined in current scope (tmpfile):1:
Can anyone help?
Also, I am using this THStack (within canvas) code:
[code]TCanvas *hstack() {
THStack *hs = new THStack(“hs”,“Stacked 1D histograms”);
h1>SetFillColor(kRed);
h1->SetMarkerColor(kRed);
hs->Add(h1st);
TCanvas *cst = new TCanvas(“cst”,“stacked hists”,0, 0, 1000, 1000);
//the parameters below are optional and I will edit them when I can run the THStack this code.
cst->SetFillColor(41);
cst->Divide(2,2);
cst->cd(1);
hs->Draw();
cst->cd(2);
gPad->SetGrid();
hs->Draw(“nostack,e1p”);
cst->cd(3);
gPad->SetFrameFillColor(17);
gPad->SetTheta(3.77);
gPad->SetPhi(2.9);
hs->Draw(“lego1”);
{[/code]
If anything seems suspicious on the code I wrote let me know.
Thank you.
root [4] .x stackhist.cxx
Error: Symbol f is not defined in current scope stackhist.cxx:15:
Error: Failed to evaluate f.Get("h1")
*** Interpreter error recovered ***
(…)[/code]
And then by accessing TBrowser, I get all 6 root files each one with different histograms in ROOT Files (temporarily because if I close root they disappear). But the histograms inside each .root file are all named pt;1 (I’m not sure if is that you are talking about).
How do I copy them permanently to the main directory (that I suppose is ROOT Files…)?
Indeed. Use eitherTFile *f4 = new TFile("/home/jorge/madanalysis5/pp_TT_Zt/Build/pt_8tev_mtp1_2e3.root");
TH1F *h4 = (TH1F*)f4.Get("h4"); orTFile f4("/home/jorge/madanalysis5/pp_TT_Zt/Build/pt_8tev_mtp1_2e3.root");
TH1F *h4 = (TH1F*)f4.Get("h4");
if (h4) h4->SetDirectory(0);The histogram are attached by default to their TFile and are deleted/close when the TFile object disappear. When I histogram is deleted, its representation in the canvas is also deleted.