Drawing histogram via compilled macros

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:

#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

.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

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


Can you try:

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;
}
1 Like