Dear All,

I, want to convert the TGraph into 1D histogram. Please guide me how to do.

Regards

Mohammad Asif Bhat

Dear All,

I, want to convert the TGraph into 1D histogram. Please guide me how to do.

Regards

Mohammad Asif Bhat

Hi Mohammad,

```
TGraph* gr = new TGraph();
TH1F* hist = gr->GetHistogram();
```

Thanks yus for your reply I applied the same but it is not working I am attaching here the full program Please have a look.

Float_t x[8]={2.4,2.6,2.8,3.0,3.2,3.4,3.6,3.8};

Float_t y[8]={27.35,27.2,27.1,26.9,26.6,26.25,25.75,25.05};

Float_t ex[8] = {0.1, 0.1, 0.1, 0.1, 0.1,0.1,0.1,0.1};

//Float_t y[8]= {5.47668/0.2,5.44697/0.2,5.42052/0.2,5.38596/0.2,5.32839/0.2,5.25601/0.2,5.1532/0.2,5.01971/0.2}; //original values

int n=8;

TCanvas *c= new TCanvas();

TMultiGraph *mg = new TMultiGraph();

//gr->Scale(1./0.2);

TGraph *gr = new TGraphErrors(n,x,y,ex);

gr->SetMarkerStyle(22);

gr->SetMarkerSize(2);

gr->SetMarkerColor(kBlue);

gr->GetXaxis()->SetTitle("#scale[1.5]{#eta}");

gr->GetXaxis()->CenterTitle();

gr->GetYaxis()->SetTitle("#scale[1.5]{#frac{dN_{#gamma}}{d#eta}}");

gr->GetYaxis()->CenterTitle();

gr->GetYaxis()->SetRangeUser(0,30);

gr->GetXaxis()->SetRangeUser(2.3,3.9);

gr->SetTitle("unfolded pseudorapidity distribution of photons for cut2 ");

gr->Draw(“ap”);

TH1F *hist = gr->GetHistogram();

TFile *f1 = new TFile(“unfolded_cut2_new.root”,“Recreate”);

hist->Write();

f1->Close();

Ah yes, the `TGraph::GetHistogram()`

returns the empty histogram as explained here. To get a real histogram with values off a `TGraph`

, replace

```
TH1F *hist = gr->GetHistogram();
```

with this:

```
TH1F* hist = new TH1F("hist", Form("%s;%s;%s", gr->GetTitle(), gr->GetXaxis()->GetTitle(), gr->GetYaxis()->GetTitle()), 1000, 0.9*TMath::MinElement(sizeof(x)/sizeof(*x), x), 1.1*TMath::MaxElement(sizeof(x)/sizeof(*x), x));
for (unsigned short i=0; i < gr->GetN(); ++i) // setting bin contents to the TGraph values
{
double x,y;
gr->GetPoint(i, x, y);
hist->Fill(x, y); // uncertainties are of course screwed up
}
for (unsigned short i=1; i < hist->GetNbinsX(); ++i) // let's nullify the y-axis uncertainties as it was in the TGraph
hist->SetBinError(i, 0);
```

(via this post)

Thanks yus lot for helping me.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.