Hello all,
I have a root file which has canvas saved inside it. I want to convert canvas into histograms. I have attached my root file with this.
My root code is:
{
TFile file(“jetht_mad.root”,“read”);
TH1F *h;
TCanvas c1 = (TCanvas )file.Get(“Canvas_1”);
TGraphAsymmErrors ae = (TGraphAsymmErrors )c1->GetPrimitive(“JetsHT_mu_inc1jet”);
TH1F h = (TH1F )ae->GetHistogram();
// h1 = (TGraphAsymmErrors*)c1->GetPrimitive(“JetsHT_mu_inc1jet”);
c1->Clear();
c1->Draw();
//TCanvas c2;
h->Draw();
}
jetht_mad.root (13.7 KB)
couet
July 3, 2013, 10:12am
2
when you do:
{
TFile file("jetht_mad.root","read");
TCanvas *c1 = (TCanvas*)file.Get("Canvas_1");
TGraphAsymmErrors *ae = (TGraphAsymmErrors*)c1->GetPrimitive("JetsHT_mu_inc1jet");
TH1F *h = (TH1F*)ae->GetHistogram();
TCanvas c2;
h->Draw();
}
you get a canvas with an empty plot. That’s normal because the histogram inside the Graph is just an empty frame used to draw the axis. The data are hold by the graph.
The TGraph::GetHistogram method returns a pointer to the histogram used to draw the axis. This histogram is “empty” (i.e. the contents of the graph is not used to fill the histogram). If this is what you want, try:
TH1F *h = (TH1F*)(ae->GetHistogram()->Clone("h")); h->SetDirectory(gROOT);
I have used a discussion to get the plot but again I am getting the Canvas. root.cern.ch/root/roottalk/roottalk04/2633.html
Please let me know if I am going wrong somewhere. I did now:
{
TFile file(“jetht_mad.root”,“read”);
TH1F h;
TCanvas c1 = (TCanvas )file.Get(“Canvas_1”);
h = (TH1F )c1->GetPrimitive(“JetsHT_mu_inc1jet”);
c1->Clear();
c1->Draw();
h->Draw();
}
Try:
TFile file("jetht_mad.root","read");
file.ls();
Canvas_1->ls();
couet
July 3, 2013, 1:50pm
6
Your canvas contains a TGraph not a TH1 I would suggest you use the TGraph as follow:
{
TFile file("jetht_mad.root","read");
TCanvas *c1 = (TCanvas*)file.Get("Canvas_1");
TGraphAsymmErrors *ae = (TGraphAsymmErrors*)c1->GetPrimitive("JetsHT_mu_inc1jet");
TCanvas c2;
ae->Draw();
}
Then you can eventually get the points from the graph and fill an histogram with them but I think you better use directly the graph …
This is the output after trying this:
TFile** jetht_mad.root
TFile* jetht_mad.root
KEY: TCanvas Canvas_1;1 Canvas_1
Canvas Name=Canvas_1 Title=Canvas_1 Option=
TCanvas fXlowNDC=0 fYlowNDC=0 fWNDC=1 fHNDC=1 Name= Canvas_1 Title= Canvas_1 Option=
OBJ: TList TList Doubly linked list : 0
TFrame X1= 0.000000 Y1=-3.795196 X2=1022.200000 Y2=0.379502
OBJ: TGraphAsymmErrors JetsHT_mu_inc1jet : 1 at: 0x8c8cd98
I have tried this :
{
TFile file(“jetht_mad.root”,“read”);
TCanvas c1 = (TCanvas )file.Get(“Canvas_1”);
TGraphAsymmErrors ae = (TGraphAsymmErrors )c1->GetPrimitive(“JetsHT_mu_inc1jet”);
TCanvas c2;
ae->Draw();
}
But i m getting an empty canvas.
{
TFile file("jetht_mad.root","read");
TCanvas *c1 = (TCanvas*)file.Get("Canvas_1");
TGraphAsymmErrors *ae = (TGraphAsymmErrors*)(c1->GetPrimitive("JetsHT_mu_inc1jet")->Clone("ae"));
delete c1; // no longer needed
TCanvas *c2 = new TCanvas("c2", "c2");
ae->Draw("AL");
}
Thanks. Now I’m getting the plot but its again on new canvas. I want to get the plot in histogram form not in the canvas form. can you please help?
There is no method which transforms a “TGraphAsymmErrors” into a “TH1”.
couet
July 4, 2013, 7:45am
12
As I said earlier you can get the TGraphAsymmErrors content and fill a histogram with it but seems to me you better stay with the TGraphAsymmErrors. Your data set consist of discrete points with asymmetric errors bars. TGraphAsymmErrors is the best object to hold such data.
Actually I have two root files. One is attached above. Other one is attached with this message. I want to superimpose two histograms from these files. Both root files have canvas saved inside it. But I am n’t able to superimpose them. That’s why I want to convert them to histograms. I followed this:
{
TFile f1(“jetht_mad.root”);
h1->Draw();
h1->SetLineColor(kRed);
TFile f2(“jet_HT_data.root”);
h2->Draw(“same”);
h2->SetLineColor(kBlue);
c1->Update();
}
There are canvas inside these root files. So do nt know how to deal with them.
regards,
kanishka
jet_HT_data.root (14.8 KB)
couet
July 4, 2013, 9:12am
14
{
TFile f1("jetht_mad.root","read");
TCanvas *c1 = (TCanvas*)f1.Get("Canvas_1");
TGraphAsymmErrors *ae = (TGraphAsymmErrors*)c1->GetPrimitive("JetsHT_mu_inc1jet");
TFile f2("jet_HT_data.root","read");
TCanvas *c2 = (TCanvas*)f2.Get("c1");
TH1D *h1;
TList* l = c2-> GetListOfPrimitives();
TIter next(l);
TObject *found, *obj;
while ((obj=next())) {
if (obj->InheritsFrom(TH1D::Class())) {
h1 = (TH1D*)obj;
}
}
TCanvas c3;
h1->Draw();
ae->Draw("L");
}
#include "TFile.h"
#include "TCanvas.h"
#include "TPad.h"
#include "TGraphAsymmErrors.h"
#include "TH1.h"
#include "THStack.h"
#include "TROOT.h"
#include "TObject.h"
void trial(void)
{
TFile *f;
TCanvas *c;
TGraphAsymmErrors *g = 0;
TH1D *h = 0;
f = TFile::Open("jetht_mad.root");
f->GetObject("Canvas_1", c);
if (c) {
g = (TGraphAsymmErrors*)(c->GetPrimitive("JetsHT_mu_inc1jet")->Clone("g"));
if (g) {
g->SetMarkerColor(kRed);
g->SetLineColor(kRed);
}
}
delete c; // no longer needed
delete f; // no longer needed
f = TFile::Open("jet_HT_data.root");
f->GetObject("c1", c);
if (c) {
h = (TH1D*)(c->GetPrimitive("Central")->Clone("h"));
if (h) {
h->SetDirectory(gROOT);
h->SetMarkerColor(kBlue);
h->SetLineColor(kBlue);
h->SetStats(0); // delete the statistics box for this histogram
}
}
delete c; // no longer needed
delete f; // no longer needed
c = new TCanvas("c", "c");
gPad->SetLogy(kTRUE);
// create and draw a "common" axes frame
THStack *s = new THStack("s", "s");
if (g) s->Add(g->GetHistogram());
if (h) s->Add(h);
s->Draw("nostack axis");
if (g) {
g->Draw("same");
if (h) h->Draw("same");
} else if (h) h->Draw("same");
c->Modified(); c->Update();
}
couet
July 4, 2013, 10:11am
17
Note that in your second canvas the TH1D has a totally unusable name, that’s why I was obliged to use this ugly code to retrieve it. In the future try to have short names with no special characters. Such long string should be reserved for the title.
Fortunately, ROOT recognized that TH1D as “Central”. Let’s call it a “feature”, not a “bug”.
mendoza
November 25, 2020, 10:00am
19
Dear experts,
I am sorry to bother you. I am trying to implement your code in my program, but using pyroot. I can make it work and I don’t know what more to do.
Thanks for your time and I apologize again.
Diego
Dear Users,
I use PYROOT and I have a TFile with a canvas inside. I need to convert it into an histogram because I will work with it after. I have used some of the topics very related to that but it does not work, maybe because I am using PYROOT.