Running a C++ Script in ROOT


Please read tips for efficient and successful posting and posting code

ROOT Version: root_v6.19.02.macosx64-10.15-clang110.tar.gz
Platform: Mac OS
Compiler: XCode


Hi I am running through a root tutorial for my lab. I tried making a C++ script to open a file and make histograms as well as a .root file.

Can anyone let me know what I did wrong in my code.

First here is my script.

oot_v6.19.02.macosx64-10.15-clang110.tar.gz

#include "/Users/conor/root/macros/exo_j2.C"
#include "Riostream.h"
#include "TFile.h"
#include "TH1.h"
#include "TNtuple.h"

void exo_j2(const Char_t* fdata="exo_j2.data", const Char_t* froot="exo_j2.root")
{
    
   ifstream in;

   open(fdata, ios::in);

   Float_t x,y,z,e;
   Int_t nlines = 0;

   TFile *f = new TFile(froot,"exo_j2.root");

    TH1F *hz = new TH1F("hz","z distribution",100,-10,10);
    TH2F *hy_hx = new TH2F("hy_hx","Y vs X",100,-25,25,100,-25,25);
    TH2F *hz_hx = new TH2F("hz_hx","Z vs X)",100,-10,10,100,-25,25);
    TH2F *hz_hy = new TH2F("hz_hy","Z vs X)",100,-10,10,100,-25,25);
    //TProfile *hxprof = new TProfile("profx","<e> vs X",100,-25,25);
    //TProfile *hyprof = new TProfile("profx","<e> vs X",100,-25,25);
    TH3F *hx_hy_hz = new TH3F("hx_hy_hz","Z vs Y vs X",100,-25,25,100,-25,25,100,-10,10);
    TNtuple *ntuple = new TNtuple("ntuple","data from ascii file","x:y:z:e");


   while (in.good())
    {
      in >> x >> y >> z >> e;
      if (in.good())
       {
       if (nlines < 5)
        {
        cout << "X =  " << x << ", Y =  " << y ;
        cout << ",Z =   " << z << ",E = " << e << endl;
        }
       hz->Fill(z);
           hy_hx->Fill(x,y);
           hz_hx->Fill(x,z);
           hz_hy->Fill(y,z);
           //hxprof->Fill(x,e,1);
           //hyprof->Fill(y,e,1);
       ntuple->Fill(x,y,z,e);
       nlines++;
       }
    };
   in.close();

   f->Write();
   delete f;
}

Here are the errors I received after running the script.

**/Users/conor/root/macros/exo_j2.C:23:4:** **error:** **unknown type name 'TFile'**

TFile *f = new TFile(froot,"exo_j2.root");

**^**

**/Users/conor/root/macros/exo_j2.C:23:19:** **error:** **unknown type name 'TFile'**

TFile *f = new TFile(froot,"exo_j2.root");

**^**

**/Users/conor/root/macros/exo_j2.C:25:5:** **error:** **unknown type name 'TH1F'**

TH1F *hz = new TH1F("hz","z distribution",100,-10,10);

**^**

**/Users/conor/root/macros/exo_j2.C:25:20:** **error:** **unknown type name 'TH1F'**

TH1F *hz = new TH1F("hz","z distribution",100,-10,10);

**^**

**/Users/conor/root/macros/exo_j2.C:26:5:** **error:** **unknown type name 'TH2F'**

TH2F *hy_hx = new TH2F("hy_hx","Y vs X",100,-25,25,100,-25,25);

**^**

**/Users/conor/root/macros/exo_j2.C:26:23:** **error:** **unknown type name 'TH2F'**

TH2F *hy_hx = new TH2F("hy_hx","Y vs X",100,-25,25,100,-25,25);

**^**

**/Users/conor/root/macros/exo_j2.C:27:5:** **error:** **unknown type name 'TH2F'**

TH2F *hz_hx = new TH2F("hz_hx","Z vs X)",100,-10,10,100,-25,25);

**^**

**/Users/conor/root/macros/exo_j2.C:27:23:** **error:** **unknown type name 'TH2F'**

TH2F *hz_hx = new TH2F("hz_hx","Z vs X)",100,-10,10,100,-25,25);

**^**

**/Users/conor/root/macros/exo_j2.C:28:5:** **error:** **unknown type name 'TH2F'**

TH2F *hz_hy = new TH2F("hz_hy","Z vs X)",100,-10,10,100,-25,25);

**^**

**/Users/conor/root/macros/exo_j2.C:28:23:** **error:** **unknown type name 'TH2F'**

TH2F *hz_hy = new TH2F("hz_hy","Z vs X)",100,-10,10,100,-25,25);

**^**

**/Users/conor/root/macros/exo_j2.C:31:5:** **error:** **unknown type name 'TH3F'**

TH3F *hx_hy_hz = new TH3F("hx_hy_hz","Z vs Y vs X",100,-25,25,100,-25,2...

**^**

**/Users/conor/root/macros/exo_j2.C:31:26:** **error:** **unknown type name 'TH3F'**

TH3F *hx_hy_hz = new TH3F("hx_hy_hz","Z vs Y vs X",100,-25,25,100,-25,2...

It seems to be not recognizing the TH3F types. Unsure how to fix this issue.

Hi,
does that first #include statement include the script itself?

Cheers,
Enrico

For including the script do I need to specify its file path or just the name of the script as well as the data file?

Hi Conor,
including it where exactly? The script definitely does not need to #include itself. To run the script from ROOT, assuming a working ROOT installation you can just run root /Users/conor/root/macros/exo_j2.C from your command line.

With more information (what you tried, what you expected to happen, what actually happened, and steps to reproduce the situation myself) I might be of more help.

Cheers,
Enrico

So I am expecting to create a new root file exo_j2.root with the following histograms hz, hy_hz, hz_hx, hz_hy, and hx_hy_hz. I am using the command line to open root and then from in root I give the command .L exo_j2.c to load the script and then execute it. I have removed the include from the start of the script. I then try to open open the new file in the TBrowser but I do not see it existing. Attached are the script file and the data file in a txt format. exo_j2.c (1.3 KB) exo_j2.txt (658.4 KB)

It’s .x exo_j2.c to execute the function in the file with the same name of the file. Otherwise .L exo_j2.c followed by, e.g. exo_j2() to actually execute the function. Are you sure the function is executed (you can e.g. add a std::cout << "i'm executing!" << std::endl; inside)?

Also looking at your script you need to open the file in “recreate” mode, new TFile(froot, "recreate")

TFile *f = TFile::Open(froot, "RECREATE");

1 Like

So it now makes the histograms but for some reason they are empty. Thank you for your help.

Yeah I can see that it is executing but its not filling from the file.

in.open(fdata, ios::in);

Where did I go wrong with that line?