Ntuple->Draw() can't get correct TH2F histogrsm

Hi, Rooters,

I have a Geant4 produced ntuple file. This file has 200+ branches.

When I make a 2D plot in Treeviewer, this plot has a correct results, as the first attachment.
However, when I convert this kind of plot into a TH2F histogram with the attached script[1], I got a weird plot : (1) there exists box like distributed points, (2) some of the points “projected” to the Y axis. As another attached plot show.

As mentioned earlier, there exists many branches in my ntuple file, which means I do need a script to get 2D histograms instead of openning and saving them in Treeviewer manually.
Could you please give me a hint on how to solve this problem?

Thanks !

Best,
Junhui

[1] Script
void htr1() {
TFile *f = new TFile("/home/junhui/Geantfour/producedRootFiles/SDDArrayRectangleBeamShape15MEvents.root “);
TTree T = (TTree)f->Get(“ScintillatorRun”);
T->Draw(“Sdd_Energy:Bar2_Time_1stHit >> h_SDDE_vs_Bar2Time”,”(Sdd_N_of_Hits > 0) && (Bar2_N_of_Hits > 0)");
TH2F h_SDDE_vs_Bar2Time = (TH2F)gDirectory->Get(“h_SDDE_vs_Bar2Time”);
h_SDDE_vs_Bar2Time->SetLineColor(2);
TCanvas *c1 = new TCanvas(“c1”,“test”,10,10,1200,1500);
c1->Divide(1,1);
c1->cd(1);
h_SDDE_vs_Bar2Time->Draw();

TPaveText *pt = new TPaveText(10.57013,30608.47,14.91404,36353.8,“br”);
pt = new TPaveText(0.4,0.65,0.9,0.75,“brNDC”);
pt->SetFillColor(0);
text = pt->AddText("# of hits in Bar1");
pt->Draw();
c1_1->Modified();
c1->cd();
}

void ntupleToHistogramSDDArray() {
htr1();
}

TGrpahInTreeviewer.pdf (71.5 KB)
ntupleConvertToHist.pdf (72 KB)

See the TTree::Draw method description for explanations (note: “e1:e2” produces an unbinned 2-d scatter-plot, i.e. a TGraph, while “e1:e2>>hnew” produces a histogram with something like 40x40 default binning which you then see as “boxes”).

Hi, Wile,

Thanks for reply !

As you mentioned, I do observe the lines.
The thing is : is there any way to produce binned 2D histograms by using tuple->Draw() with a cut, like a TGraph plot ?

Thanks !

Best,
Junhui

Search for “binning” in the TTree::Draw method description.
Note: a TGraph is always UN-binned. If you like it, search for “Retrieving the result of Draw” in the same place.

Hi, Wile,

Currently, I need TGraph-like scattering plots, not binned ones.
While under the command of “Draw()”, it tells us how to bin data which doesn’t fit my case.

Any comments ?

Best,
Junhui

So, why don’t you forget “>>hnew” and simply keep clones of the drawn graphs?

Hi, Wile,

I confused myself, #-o .
I actually came out already even without your latest post, still, thanks for instant response, appreciate !

Best,
Junhui

I think it probably makes sense to post my passed script here.

void htrZ() {
   TFile *f = new TFile("fileName.root ");
   TTree *T = (TTree*)f->Get("TreeName");
   TCanvas *cZ = new TCanvas("cZ","temporary",10,10,500,400);

   T->Draw("Sdd_Energy:BarZ_Time_1stHit ","(Sdd_N_of_Hits > 0) && (BarZ_N_of_Hits > 0)");

   TGraph *g_SDDE_vs_BarZTime    = (TGraph*)gPad->GetPrimitive("Graph");
   htemp->GetXaxis()->SetTitle("BarZ time(ns)");  /// "htemp" confused me a lot ...
   htemp->GetXaxis()->CenterTitle(true);
   htemp->GetXaxis()->SetTitleOffset(1.2);
   htemp->GetYaxis()->SetTitle("Sdd energy(keV)");  /// "htemp" confused me a lot ...
   htemp->GetYaxis()->CenterTitle(true);
   htemp->GetYaxis()->SetTitleOffset(1.2);

   cZ->SaveAs("BarZTime_vs_SddE.pdf");
   cZ->SaveAs("BarZTime_vs_SddE.root");

   f->Close();
}

PS : in my script, all of the “Z” has been replaced to number in real case.