# Drawing histogram via compilled macros

**URL:** <https://root-forum.cern.ch/t/drawing-histogram-via-compilled-macros/32414>\
**Category:** Newbie\
**Tags:** hist\
**Created:** [January 23, 2019, 2:20pm UTC](https://root-forum.cern.ch/t/drawing-histogram-via-compilled-macros/32414 "2019-01-23T14:20:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bdrum](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bdrum/32/16729_2.png) [@bdrum](https://root-forum.cern.ch/u/bdrum)\
**Post date:** [January 23, 2019, 2:20pm UTC](https://root-forum.cern.ch/t/drawing-histogram-via-compilled-macros/32414/1 "2019-01-23T14:20:38Z")

</div>

Hi rooters.

Could somebody help me and explain what wrong with my macro, please?  
I have the macros file which called “Analysis.C” with such content:

```auto
#include "TFile.h"
#include "TTree.h"
#include "TMath.h"
#include "TH1D.h"

TH1D* DrawingHistogram() {
  TFile fin("result.root");
  TTree* tr = nullptr;
  fin.GetObject("events", tr);
  int n = 10;
  Float_t TrackPara[177][18];
  tr->SetBranchAddress("TrackPara", TrackPara);
  Double_t tempPt = 0;
  auto hpt = new TH1D("pt", "pt", 100, 0, 1);
  for (auto i = 0; i < n; ++i) {
    tr->GetEntry(i);
    for (auto j = 0; j < 177; ++j) {
      tempPt = TMath::Sqrt(TrackPara[j][11]*TrackPara[j][11]+TrackPara[j][12]*TrackPara[j][12]);
      hpt->Fill(tempPt);
    }
  }
  return hpt;
}

```

So the problem is  
If I just copy the body of function in root interpreter and call hpt-\>Draw() it works fine, but if I do

```auto
.L Analysis.C+
auto h = DrawingHistogram();
h->Draw()

```

It returns seg. violation.

Where is the error in my code?

One more remark:  
If I’ll change body on something simple, let say

```auto
TH1D* DrawHist() {
  auto hpt = new TH1D("pt", "pt", 100, 0, 1);
  hpt->FillRandom("gaus");
  return hpt; 
}

```

It will be work.

Thanks!

* * *

_ROOT Version:_ 6.15/01  
_Platform:_ ubuntu 18.04  
_Compiler:_ gcc 7.3.0

* * *

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [January 23, 2019, 2:34pm UTC](https://root-forum.cern.ch/t/drawing-histogram-via-compilled-macros/32414/2 "2019-01-23T14:34:22Z")

</div>

Can you try:

```auto
TH1D* DrawingHistogram() {
  auto fin = new TFile("result.root");
  TTree* tr = nullptr;
  fin->GetObject("events", tr);
  int n = 10;
  Float_t TrackPara[177][18];
  tr->SetBranchAddress("TrackPara", TrackPara);
  Double_t tempPt = 0;
  auto hpt = new TH1D("pt", "pt", 100, 0, 1);
  for (auto i = 0; i < n; ++i) {
    tr->GetEntry(i);
    for (auto j = 0; j < 177; ++j) {
      tempPt = TMath::Sqrt(TrackPara[j][11]*TrackPara[j][11]+TrackPara[j][12]*TrackPara[j][12]);
      hpt->Fill(tempPt);
    }
  }
  return hpt;
}

```
