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

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:

#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.

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:

#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? :slight_smile: )

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

Can you provide a small reproducer ?

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

A piece of code we can run reproducing your problem.

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

reproducer.cpp (1.1 KB)

functions.h (2.1 KB)

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

data.txt (206.1 KB)

print.cpp is missing

print.cpp (1.0 KB)

Here it is, sorry.

there is some undeclared variables in your program:

/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;

reproducer.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.

I would say you need a Application as in the modified files:
functions.h (2.1 KB) reproducer.cpp (1.1 KB)

That works for me

Note also your program produces warnings:

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.

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

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

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

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.

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