# Can't Fill TH2F after I split my code in multiple files

**URL:** <https://root-forum.cern.ch/t/cant-fill-th2f-after-i-split-my-code-in-multiple-files/38275>\
**Category:** ROOT\
**Tags:** graphics, bug, hist, root\
**Created:** [March 5, 2020, 12:12pm UTC](https://root-forum.cern.ch/t/cant-fill-th2f-after-i-split-my-code-in-multiple-files/38275 "2020-03-05T12:12:26Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![FreddyCCH](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/freddycch/32/19612_2.png) [@FreddyCCH](https://root-forum.cern.ch/u/FreddyCCH)\
**Post date:** [March 5, 2020, 12:12pm UTC](https://root-forum.cern.ch/t/cant-fill-th2f-after-i-split-my-code-in-multiple-files/38275/1 "2020-03-05T12:12:26Z")

</div>

Hello everyone,

I’m an italian student and I’ve been working on a lot of data for my thesis in physics, using ROOT to plot histograms and graphs: I’ll get to the point straight ahead.

My source code, a single .cpp file, looked kind of like this:

```auto
#include < all the libraries i need >
< declaration of the various functions, with all the instructions > 
int main ()
{
     TH1F h1 = ( ... );
     TH2F h2 = ( "", "", nBins, -range1, range1, nBins, -range2, range2 );
     // fill histograms here
     while ( things happen )
     {
          h1.Fill( v[i] );
          h2.Fill( v[i], w[i] );
          i++;
     }
     < print histograms here >;
     return 0;
}

```

Everything was fine, and I could correctly plot the scatterplot I needed to show a correlation between two variables. Here’s a draft.

 ![q1e-1_scat](https://root-forum.cern.ch/uploads/default/original/3X/7/e/7e0cfca72b0cb17c85f98da64f6e28c5a16d6afd.png)

Having made lots of custom functions for data analysis, I have decided to split my code into three different parts:  
1- header file: functions.h -\> names of the functions and all c++ and ROOT libraries are declared here.  
2- functions.cpp: what the various functions do appears here + #include “functions.h” on top  
3- main.cpp: no need to explain this, nothing changed here + #include “functions.h” on top  
The code is compiled correctly.

Now, my source code looks quite like this:

```auto
#include "functions.h"

int main () // nothing changed here
{
      TH1F h1 = ( ... );
      TH2F h2 = ( "", "", nBins, -range1, range1, nBins, -range2, range2 );
     // fill histograms here
     while ( things happen )
     {
          h1.Fill( v[i] );
          h2.Fill( v[i], w[i] );
          i++;
     }
     < print histograms here >;
     return 0;
}

```

And _now_, apparently for no reason at all, h2 is printed empty. The axis range is plotted correctly, I just don’t understand _why on earth_ h2 pleases me with all this emptiness. h1 is always printed correctly, by the way. Both libraries ( “TH1F.h” and “TH2F.h” ) are included in the same file ( functions.h ), and no errors appear when I compile my code.

Fun part in this absurdity: if, for example, I “tell” h2 to fill the bin corresponding to the coordinates (0., 0.) ten thousand times, the histogram is printed correctly, with te thousand entries in ( 0., 0. ) - If I manually edit range1 and range2 ( see picture ) and fill the histogram with my actual data, h2 is either printed empty or in a completely wrong way. ( New users can only post one image in a post, sorry \>\_\_\> )

Does anyone know why this is happening? I really don’t know what to do to fix this issue and I could use some help \>\_\_\>

( Of course I can just merge my files and ignore the issue, but my code was just too messy to deal with and I wouldn’t want to do that. Free bug report, I guess? 🙂 )

Thank you for your help,  
Freddy

_ROOT Version:_ 6.18.04  
_Platform:_ Ubuntu 18.04 on Windows for Developers ( + Xming )  
_Compiler:_ No idea, I have been taught to type " c++ -o program main.cpp ‘root-config --cflags --glibs’ " in my terminal and it works

---

<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:** [March 5, 2020, 12:31pm UTC](https://root-forum.cern.ch/t/cant-fill-th2f-after-i-split-my-code-in-multiple-files/38275/2 "2020-03-05T12:31:21Z")

</div>

Can you provide a small reproducer ?

---

<div class="post-metadata">

**Author:** ![FreddyCCH](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/freddycch/32/19612_2.png) [@FreddyCCH](https://root-forum.cern.ch/u/FreddyCCH)\
**Post date:** [March 5, 2020, 12:43pm UTC](https://root-forum.cern.ch/t/cant-fill-th2f-after-i-split-my-code-in-multiple-files/38275/3 "2020-03-05T12:43:47Z")

</div>

I’m sorry, what do you mean with reproducer?

---

<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:** [March 5, 2020, 12:50pm UTC](https://root-forum.cern.ch/t/cant-fill-th2f-after-i-split-my-code-in-multiple-files/38275/4 "2020-03-05T12:50:50Z")

</div>

A piece of code we can run reproducing your problem.

---

<div class="post-metadata">

**Author:** ![FreddyCCH](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/freddycch/32/19612_2.png) [@FreddyCCH](https://root-forum.cern.ch/u/FreddyCCH)\
**Post date:** [March 5, 2020, 1:39pm UTC](https://root-forum.cern.ch/t/cant-fill-th2f-after-i-split-my-code-in-multiple-files/38275/5 "2020-03-05T13:39:33Z")

</div>

Yes! Uploading now. This should work. I got rid of all the unnecessary parts to make it simple.

[reproducer.cpp](https://root-forum.cern.ch/uploads/short-url/2VViZhxXIk2ItuvqD7LsLn4kwXa.cpp) (1.1 KB)

[functions.h](https://root-forum.cern.ch/uploads/short-url/ajWWBcEwXovN6fnI6cr6dF1WGpu.h) (2.1 KB)

---

<div class="post-metadata">

**Author:** ![FreddyCCH](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/freddycch/32/19612_2.png) [@FreddyCCH](https://root-forum.cern.ch/u/FreddyCCH)\
**Post date:** [March 5, 2020, 1:40pm UTC](https://root-forum.cern.ch/t/cant-fill-th2f-after-i-split-my-code-in-multiple-files/38275/6 "2020-03-05T13:40:15Z")

</div>

You need to read this file. I can’t upload more than two files per post ☹

[data.txt](https://root-forum.cern.ch/uploads/short-url/z0Ttdq1vRrsYEVfEnsRqQeHXejU.txt) (206.1 KB)

---

<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:** [March 5, 2020, 1:45pm UTC](https://root-forum.cern.ch/t/cant-fill-th2f-after-i-split-my-code-in-multiple-files/38275/7 "2020-03-05T13:45:39Z")

</div>

print.cpp is missing

---

<div class="post-metadata">

**Author:** ![FreddyCCH](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/freddycch/32/19612_2.png) [@FreddyCCH](https://root-forum.cern.ch/u/FreddyCCH)\
**Post date:** [March 5, 2020, 1:48pm UTC](https://root-forum.cern.ch/t/cant-fill-th2f-after-i-split-my-code-in-multiple-files/38275/8 "2020-03-05T13:48:44Z")

</div>

[print.cpp](https://root-forum.cern.ch/uploads/short-url/A5GnsNfsxb27W5BfHlEPlFYwJq.cpp) (1.0 KB)

Here it is, sorry.

---

<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:** [March 5, 2020, 1:52pm UTC](https://root-forum.cern.ch/t/cant-fill-th2f-after-i-split-my-code-in-multiple-files/38275/9 "2020-03-05T13:52:13Z")

</div>

there is some undeclared variables in your program:

```auto
/Users/couet/Downloads/reproducer.cpp:36:14: error: use of undeclared identifier 'angvar'
                scat.Fill( angvar[n] , eccvar[n] ); // this does not work: either blank or messed up (line 28 )
                           ^
/Users/couet/Downloads/reproducer.cpp:36:26: error: use of undeclared identifier 'eccvar'
                scat.Fill( angvar[n] , eccvar[n] ); // this does not work: either blank or messed up (line 28 )
                                       ^
/Users/couet/Downloads/reproducer.cpp:38:13: error: use of undeclared identifier 'angvar'
                if ( n == angvar.size() || n == eccvar.size() ) break;
                          ^
/Users/couet/Downloads/reproducer.cpp:38:35: error: use of undeclared identifier 'eccvar'
                if ( n == angvar.size() || n == eccvar.size() ) break;

```

---

<div class="post-metadata">

**Author:** ![FreddyCCH](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/freddycch/32/19612_2.png) [@FreddyCCH](https://root-forum.cern.ch/u/FreddyCCH)\
**Post date:** [March 5, 2020, 1:56pm UTC](https://root-forum.cern.ch/t/cant-fill-th2f-after-i-split-my-code-in-multiple-files/38275/10 "2020-03-05T13:56:44Z")

</div>

[reproducer.cpp](https://root-forum.cern.ch/uploads/short-url/aDyUWrH7E7cMZY6qclihtYO8w7X.cpp) (1.0 KB)

Corrections have been made. angvar is the X vector, eccvar is the Y vector. I forgot to rename and edit that part, I’m sorry. Grab the edited file down here, there was an extra dot causing problems.

---

<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:** [March 5, 2020, 2:30pm UTC](https://root-forum.cern.ch/t/cant-fill-th2f-after-i-split-my-code-in-multiple-files/38275/11 "2020-03-05T14:30:33Z")

</div>

I would say you need a Application as in the modified files:  
[functions.h](https://root-forum.cern.ch/uploads/short-url/g62iEJLHDe0UbAPNA8FfhmJBOxk.h) (2.1 KB) [reproducer.cpp](https://root-forum.cern.ch/uploads/short-url/uXqZEgMh1NeTHi39N8EPdoNTohK.cpp) (1.1 KB)

That works for me

Note also your program produces warnings:

```auto
reproducer.cpp:41:10: warning: comparison of integers of different signs: 'int' and 'std:: __1::vector<double, std::__ 1::allocator<double> >::size_type' (aka 'unsigned long') [-Wsign-compare]
                if ( n == X.size() || n == Y.size() ) break;
                     ~ ^ ~~~~~~~~
reproducer.cpp:41:27: warning: comparison of integers of different signs: 'int' and 'std:: __1::vector<double, std::__ 1::allocator<double> >::size_type' (aka 'unsigned long') [-Wsign-compare]
                if ( n == X.size() || n == Y.size() ) break;
                                      ~ ^ ~~~~~~~~
2 warnings generated.

```

---

<div class="post-metadata">

**Author:** ![FreddyCCH](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/freddycch/32/19612_2.png) [@FreddyCCH](https://root-forum.cern.ch/u/FreddyCCH)\
**Post date:** [March 5, 2020, 3:55pm UTC](https://root-forum.cern.ch/t/cant-fill-th2f-after-i-split-my-code-in-multiple-files/38275/12 "2020-03-05T15:55:27Z")

</div>

I receive this error message now, once the program gets to the printing part ( my folder is empty now, no images are present ):

```auto
Error in <TASImage::WriteImage>: no image loaded

```

---

<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:** [March 5, 2020, 4:16pm UTC](https://root-forum.cern.ch/t/cant-fill-th2f-after-i-split-my-code-in-multiple-files/38275/13 "2020-03-05T16:16:37Z")

</div>

The files I sent you are working for me. How do you compile/run the program ?

---

<div class="post-metadata">

**Author:** ![FreddyCCH](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/freddycch/32/19612_2.png) [@FreddyCCH](https://root-forum.cern.ch/u/FreddyCCH)\
**Post date:** [March 5, 2020, 4:48pm UTC](https://root-forum.cern.ch/t/cant-fill-th2f-after-i-split-my-code-in-multiple-files/38275/14 "2020-03-05T16:48:33Z")

</div>

I write this: c++ -o myExe myExe.cpp ’ root-config --cflags --glibs ’  
Then i just type ./myExe  
( The apostrophe is actually a ` )

Edit, late at night: I have finally found the mistake I made while splitting the code into different files.

Basically, it was not a ROOT problem with the class but a problem of variables in the group of files. I will provide further explanation if needed, but I finally understood why it was compiled correctly but gave the wrong result. Still, thank you very much for 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:** [March 19, 2020, 4:48pm UTC](https://root-forum.cern.ch/t/cant-fill-th2f-after-i-split-my-code-in-multiple-files/38275/15 "2020-03-19T16:48:35Z")

</div>

Closed on March 19, 2020, 4:48pm UTC

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