# Segmentation violation when creating a Th1D :(

**URL:** <https://root-forum.cern.ch/t/segmentation-violation-when-creating-a-th1d/57951>\
**Category:** Newbie\
**Tags:** root\
**Created:** [January 24, 2024, 1:26pm UTC](https://root-forum.cern.ch/t/segmentation-violation-when-creating-a-th1d/57951 "2024-01-24T13:26:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Laly86](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/e47c2d/32.png) [@Laly86](https://root-forum.cern.ch/u/Laly86)\
**Post date:** [January 24, 2024, 1:26pm UTC](https://root-forum.cern.ch/t/segmentation-violation-when-creating-a-th1d/57951/1 "2024-01-24T13:26:13Z")

</div>

Dear root experts,  
please help me with this silly question I am getting crazy and none of my colleagues found the error ☹  
I have a macro which opens a TFile, reads the Tree contained in the file (and in particular its branch “maximum” and then fills a Th1D with the data contained in the branch:

```auto
TFile file(filename_bkg, "READ");                                                                              
  TTree* tree;                                                                                                       
  file.GetObject("EventTree", tree);                                                                                 
  EventInfo event;                                                                                                   
  tree->SetBranchAddress("maximum", &event.maximum);                                                                 
  int nEntries = tree->GetEntries();                                                                                 
                                                                                                                     
  TH1D* histogram = new TH1D("maximumPulses", "Pulses I Maximum Value", nBins, minRange, maxRange);                  
  for (Long64_t i = 0; i < nEntries; ++i) {                                                                          
    tree->GetEntry(i);                                                                                               
    histogram->Fill(event.maximum);         

```

this works and I obtain a wonderful histogram. Now, I have to open many files and create a histogram for each file so I decided to make a method:

```auto
TH1D* CreateHisto(const char* filename){

TFile file(filename, "READ");

TTree* tree;

file.GetObject("EventTree", tree);

EventInfo event;

tree->SetBranchAddress("maximum", &event.maximum);

int nEntries = tree->GetEntries();

TH1D* h1 = new TH1D("maximumPulses1", "Pulses", 100, 0, 5);

for (Long64_t i = 0; i < nEntries; ++i) {

tree->GetEntry(i);

h1->Fill(event.maximum);

}

file.Close();

return h1;

}

```

and call it in the main:

```auto
 TH1D* histogram = CreateHisto(filename_bkg);
  if (histogram) {
    cout<<"the histogram exists"<<endl;
    histogram->Draw();
  } else {
    std::cerr << "Error: Unable to create histogram." << std::endl;
  }

```

The problem is that, the program writes “the histogram exists” but when I uncomment “histogram-\>Draw” I got a segmentation violation ☹ why does it happen can you find the giant stupid mistake I made?  
ps. I checked that the file exists, that the tree exists, and that the data in event.maximum are properly read, I just removed those lines from the code.

\_ROOT Version: v6.22.08  
\_Platform: Mac OS Ventura 13.6.3 (22G436)  
_Compiler:_ Not Provided

* * *

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [January 24, 2024, 1:31pm UTC](https://root-forum.cern.ch/t/segmentation-violation-when-creating-a-th1d/57951/2 "2024-01-24T13:31:39Z")

</div>

Try with:

```auto
TH1D* h1 = new TH1D("maximumPulses1", "Pulses", 100, 0, 5);
h1->SetDirectory(nullptr);

```

I.e. Add `h1->SetDirectory(nullptr);` after the creation of the histogram. See:

> **[Object ownership](https://root.cern/manual/object_ownership/)**
>
> Data Analysis Framework

---

<div class="post-metadata">

**Author:** ![Laly86](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/e47c2d/32.png) [@Laly86](https://root-forum.cern.ch/u/Laly86)\
**Post date:** [January 24, 2024, 1:37pm UTC](https://root-forum.cern.ch/t/segmentation-violation-when-creating-a-th1d/57951/3 "2024-01-24T13:37:03Z")

</div>

> [@Laly86](#):
>
> ```auto
> TH1D* histogram = CreateHisto(filename_bkg);
> if (histogram) {
> cout<<"the histogram exists"<<endl;
> histogram->Draw();
> } else {
> std::cerr << "Error: Unable to create histogram." << std::endl;
> }
> 
> ```

Thank you so much \<3
