# Creating a TH1 type branch error

**URL:** <https://root-forum.cern.ch/t/creating-a-th1-type-branch-error/42239>\
**Category:** Newbie\
**Tags:** tree, root\
**Created:** [November 11, 2020, 4:06pm UTC](https://root-forum.cern.ch/t/creating-a-th1-type-branch-error/42239 "2020-11-11T16:06:17Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![jparlone](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/3e96dc/32.png) [@jparlone](https://root-forum.cern.ch/u/jparlone)\
**Post date:** [November 11, 2020, 4:06pm UTC](https://root-forum.cern.ch/t/creating-a-th1-type-branch-error/42239/1 "2020-11-11T16:06:17Z")

</div>

Hi all, my program is throwing the error: `Error in <TClass::New>: cannot create object of class TH1` but seems to be producing the expected output, which also behaves correctly. However, I believe this is causing some problems when I try to manipulate the output with a DataFrame (see my previous question here: [Attaching vector as a branch to an existing TTree](https://root-forum.cern.ch/t/attaching-vector-as-a-branch-to-an-existing-ttree/42193)). Below are the lines I believe might have something to do with it, along with the line throwing the error commented.

```auto
TH1* pdfs[N];
    for(int ih = 0; ih < N; ih++){
        pdfs[ih] = nullptr;
    }
	for (unsigned int imodel = 0; imodel < N; imodel++) {
    _tree_pars->Branch(model_name.c_str(), &(pdfs[imodel]), 32000, 0); //This is the line creating the error. 
	}

```

Then an example where `pdfs[imodel]` is used later:

```auto
TH1D *curr_pdf = (TH1D *) array->FindObject(name.Data());
pdfs[imodel] = (TH1*) curr_pdf;

```

Thank you!  
Best wishes,  
Jaiden

---

<div class="post-metadata">

**Author:** ![jblomer](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jblomer](https://root-forum.cern.ch/u/jblomer)\
**Post date:** [November 11, 2020, 8:13pm UTC](https://root-forum.cern.ch/t/creating-a-th1-type-branch-error/42239/2 "2020-11-11T20:13:54Z")

</div>

In

```auto
_tree_pars->Branch(model_name.c_str(), &(pdfs[imodel]), 32000, 0);

```

the branch name does not depend on `imodel`. It looks like several branches are created with the same name, which might be the cause of the error.

---

<div class="post-metadata">

**Author:** ![jparlone](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/3e96dc/32.png) [@jparlone](https://root-forum.cern.ch/u/jparlone)\
**Post date:** [November 11, 2020, 8:35pm UTC](https://root-forum.cern.ch/t/creating-a-th1-type-branch-error/42239/3 "2020-11-11T20:35:07Z")

</div>

I apologise, the full bit of that code is:

```auto
 for (unsigned int imodel = 0; imodel < N; imodel++) {
                   GenericSingleSampleFitModel *this_model = (GenericSingleSampleFitModel *)(cm->GetSubModel(imodel));
                   conv::Detector_t d = this_model->GetDetector();
                   conv::BeamConf_t b = this_model->GetBeam();
                   conv::Sample_t s = this_model->GetSample();
                   conv::KineSpace_t ks = this_model->GetKineSpace();
                   std::string model_name = util::nm::MdlName(d, b, s, ks);
                   _tree_pars->Branch(model_name.c_str(), &(pdfs[imodel]), 32000, 0); //This is the line creating the error.
               }

```

`model_name` does change with `imodel` and I’ve checked that the branches do indeed have different names.

---

<div class="post-metadata">

**Author:** ![jblomer](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jblomer](https://root-forum.cern.ch/u/jblomer)\
**Post date:** [November 12, 2020, 10:34am UTC](https://root-forum.cern.ch/t/creating-a-th1-type-branch-error/42239/4 "2020-11-12T10:34:22Z")

</div>

Could it be at when you call `_tree_pars->Branch`, `pdfs[imodel]` still points to `nullptr`? In this case, `TTree` only sees the abstract base class `TH1` which, unlike the derived classes `TH1D`, `TH1F`, cannot be created in memory.

---

<div class="post-metadata">

**Author:** ![jparlone](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/3e96dc/32.png) [@jparlone](https://root-forum.cern.ch/u/jparlone)\
**Post date:** [November 12, 2020, 1:27pm UTC](https://root-forum.cern.ch/t/creating-a-th1-type-branch-error/42239/5 "2020-11-12T13:27:13Z")

</div>

That definitely seems sensible, do you have a suggested work around for this?

---

<div class="post-metadata">

**Author:** ![jblomer](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jblomer](https://root-forum.cern.ch/u/jblomer)\
**Post date:** [November 12, 2020, 2:50pm UTC](https://root-forum.cern.ch/t/creating-a-th1-type-branch-error/42239/6 "2020-11-12T14:50:16Z")

</div>

You could create a (possibly empty) histogram in `pdfs[imodel]` before calling `TTree::Branch()`. Or perhaps you can make `pdfs[imodel]` a vector of the concrete histogram classes, e.g. `std::vector<TH1D *>` instead of `std::vector<TH1 *>`.

---

<div class="post-metadata">

**Author:** ![jparlone](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/3e96dc/32.png) [@jparlone](https://root-forum.cern.ch/u/jparlone)\
**Post date:** [November 12, 2020, 2:57pm UTC](https://root-forum.cern.ch/t/creating-a-th1-type-branch-error/42239/7 "2020-11-12T14:57:27Z")

</div>

Thanks! I’ll try the first suggestion first. I think the second suggestion would be stopped by the fact I later fill it with both TH1D and TH2D objects.

---

<div class="post-metadata">

**Author:** ![jparlone](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/3e96dc/32.png) [@jparlone](https://root-forum.cern.ch/u/jparlone)\
**Post date:** [November 12, 2020, 3:24pm UTC](https://root-forum.cern.ch/t/creating-a-th1-type-branch-error/42239/8 "2020-11-12T15:24:47Z")

</div>

Yep the first one worked! This was the exact solution:  
EDIT: This caused some problems with later plotting of TH2D hists, experimenting around with a fix.

```auto
TH1* pdfs[N];
   TH1D* blankhist = new TH1D;
   for(int ih = 0; ih < N; ih++){
       pdfs[ih] = blankhist;
     // pdfs[ih] = nullptr;
   }

```

---

<div class="post-metadata">

**Author:** ![jparlone](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/3e96dc/32.png) [@jparlone](https://root-forum.cern.ch/u/jparlone)\
**Post date:** [November 12, 2020, 4:58pm UTC](https://root-forum.cern.ch/t/creating-a-th1-type-branch-error/42239/9 "2020-11-12T16:58:54Z")

</div>

So the problem was indeed it having a `nullptr` to the abstract base class `TH1`. Unfortunately as I needed the branches to interchange between `TH1D` and `TH2D` later on, just setting the array to be filled with an empty hist of either type caused problems. In the end I solved it by placing the construction of the branch after each entry was sorted into the object types, and created a bool to make sure that the loop did not repeatedly create the branches. Thank you for all your help!

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [November 26, 2020, 4:58pm UTC](https://root-forum.cern.ch/t/creating-a-th1-type-branch-error/42239/10 "2020-11-26T16:58:56Z")

</div>

Closed on November 26, 2020, 4:58pm UTC

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