Projection on axis in TH2

Best regards,

I am sorry for silly question but I am stuck :confused:
I have scattering plot TH2F, and I am trying to get projection of data on axis.
I open .root file in ROOT Object Browser (v 5.24.03), open my canvas with plot.
Right click on it, choose ProjectionX, set inputs as(first 3 as default):
name _px
firstybin 0
lastybin -1
option d

But it always returns empty canvas? I tried to input various numbers for bin, tried also ProfileX and so on, but it always returns empty, Entries 0( the original plot of course has data points).

What am I doing wrong?
Thanx to anyone reading this :slight_smile:

Edit: I added the file.
example.root (557 KB)

Hello,

the problem is that what you have in the root file is not a TH2F, but a canvas.

Just try this in your root prompt line to check:

//Create a TH2F
TH2F hh("hh","hh",10,0,10,10,0,10);

//Fill four point to test
hh.Fill(1,3);
hh.Fill(1,4);
hh.Fill(3,3);
hh.Fill(3,4);

//Draw
hh.SetMarkerStyle(2);
hh.Draw();

Then it should work on the projection you mentioned(work for me!).

And when you try to save something in a root file, you should save the object, not the canvas.
Because you cannot do much things on a canvas.

What I mean is like:(following the commands above, type below things)

//Write hh into a root file
TFile f("myroot.root","recreate");
hh.Write();

Then try to open this root file in TBrowser, look at the icon shown, you should see that the object saved in the root file is not a canvas, but a histogram. Then you can also do what you said on the saved histogram.

Regards,

YH

Thank you a lot :slight_smile:

I am doing this for a seminar, and my mentor gave me data file like this. Did I understand well that by using this file I can’t make projections?

Hello,

it turns out to be still possible.

I found this post being quite related, you may want to check this out:

root.cern.ch/root/roottalk/roottalk04/2633.html

However, if not knowing the name of the original histogram, this won’t work.

I am trying to find how to get the name of the histogram.

Regards,

YH

Hello,

although there are many documents on the web showing this to be possible, the attempts I made are in vain.

However, it turns out that the following piece of code can extract the points in the canvas.
As a work-around, you can try use these output to fill a 2D histogram.

If you do not know how to do, I already made one in the attachment.


{
   TFile ff("example.root");

   TCanvas *c2 = (TCanvas*)ff.Get("c1");
   TList *ll = c2->GetListOfPrimitives();
   ll->Print();
}

Maybe you can find a better solution!

Also, the link below will be very helpful in learning TPad(TCanvas): (I learn a lot from this, too!)

root.cern.ch/download/doc/ROOTUs … 09s03.html
out.root (323 KB)

Hello,

I just found a much easier way to solve the case:

1.Open the canvas and right click on the points (you will see the object is a TGraph named “Graph”)
2.Press ‘saveas’ in the menu
3.In the filename part, write “mygraph.root” (or whatever name you like.)
4.Then you get a root file storing a TGraph instead of a TCanvas.

When it is being plotted, it’s originally a TGraph named “Graph”, not a TH2F. So previous attempt were in vain. If modify the previous code to the following, it will work:


{
   TFile ff("example.root");

   TCanvas *c2 = (TCanvas*)ff.Get("c1");
   TGraph *gg = c2->GetPrimitive("Graph");

}

Only now I saw the previous post :slight_smile: Thank you, I will try that now.

Hello,

I am lost a bit with the instructions now, from where on do I get file where I can ProjectX?
I get .root file with “Graph”, what then?

Or if you could briefly explain/copy code on how you got file.out root attached above?

{ TFile *f = TFile::Open("example.root"); TCanvas *c1 = 0; f->GetObject("c1", c1); c1->Draw(); TGraph *g = ((TGraph *)(c1->GetPrimitive("Graph"))); TH2F *h = ((TH2F *)(c1->GetPrimitive("htemp"))); TPaveText *p = ((TPaveText *)(c1->GetPrimitive("TPave"))); TString h_x_title, h_y_title; h_x_title = h_x_title + p->GetLine(0)->GetTitle() + ";" + h->GetXaxis()->GetTitle() + ";" + "number of events"; h_y_title = h_y_title + p->GetLine(0)->GetTitle() + ";" + h->GetYaxis()->GetTitle() + ";" + "number of events"; Double_t xmin = h->GetXaxis()->GetXmin(); Double_t xmax = h->GetXaxis()->GetXmax(); Double_t ymin = h->GetYaxis()->GetXmin(); Double_t ymax = h->GetYaxis()->GetXmax(); Int_t n_x = 1000; Int_t n_y = 1000; TH1F *h_x = new TH1F("h_x", h_x_title, n_x, xmin, xmax); TH1F *h_y = new TH1F("h_y", h_y_title, n_y, ymin, ymax); for (Int_t i = 0; i < (g->GetN()); i++) { h_x->Fill( (g->GetX())[i] ); h_y->Fill( (g->GetY())[i] ); } TCanvas *c2 = new TCanvas("c2", "c2"); c2->Divide(1,2); c2->cd(1); gPad->SetLogy(); h_x->Draw(); c2->cd(2); gPad->SetLogy(); h_y->Draw(); c2->cd(0); }

Thank you a lot :slight_smile: