Make new tree with branch error

Seemingly basic question; I am following an example and want to add a new branch to a new TTree I created the line above, however it fails to recognise the TTree variable!

I’m sure I’m missing something obvious, but I can’t seem to see what it is.

#include "TROOT.h"
#include <iostream>
#include <fstream>
#include <string>
#include <numeric>
#include <iterator>
#include <vector>
#include <TH1F.h>
#include <TH2F.h>
#include "TCanvas.h"
#include "TFile.h"
#include "TTree.h"
#include "TBranch.h"
#include "TSystem.h"
#include "TGraph.h"
#include "ROOT/TDataFrame.hxx"

using namespace std;
using namespace ROOT::Experimental;

class file {

public:
 Double_t t;
 Double_t v;
};

class Event: public TObject {

public:
  file f;
  ClassDef(Event,1)
};

ClassImp(Event)


TFile *output = new TFile("process.root","RECREATE");
Event *e1 = new Event;
TTree *t1 = new TTree("t1","Test");
t1->Branch("bname",&e1);
First error: In file included from input_line_9:1:
/[filepath]/errtest.C:41:1: error: unknown type name 't1'
t1->Branch("bname",&e1);
^

Thanks in advance,
Alistair

what is your file errtest.C exactly ?

Everything I posted in the code captions is the contents of the document. It’s from a larger program (hence the long #include list), but I’ve singled it out to just the lines that make the TTree and branch.

in that case it should be:

#include "TROOT.h"
#include <iostream>
#include <fstream>
#include <string>
#include <numeric>
#include <iterator>
#include <vector>
#include <TH1F.h>
#include <TH2F.h>
#include "TCanvas.h"
#include "TFile.h"
#include "TTree.h"
#include "TBranch.h"
#include "TSystem.h"
#include "TGraph.h"
#include "ROOT/TDataFrame.hxx"

using namespace std;
using namespace ROOT::Experimental;

class file {

public:
 Double_t t;
 Double_t v;
};

class Event: public TObject {

public:
  file f;
  ClassDef(Event,1)
};

ClassImp(Event)


void errtest() {
   TFile *output = new TFile("process.root","RECREATE");
   Event *e1 = new Event;
   TTree *t1 = new TTree("t1","Test");
   t1->Branch("bname",&e1);
}

My apologies, I am extremely new to Root; I thought that by putting a variable outside of a function it was a global variable that could be accessed by every function. The larger macro that this extract is from contains a lot of functions that need access to the TTree, so is it the case if these lines are enclosed in a function?

Yes, in C++ (that’s not ROOT per se) executable code should be in a function/method/routine (whatever you call it), it cannot “fly alone” in the middle of a file.

Ah okay, thanks for clarifying!

In addition, when you are executing a ROOT macro, ROOT is looking for a function named the same as the file (unless you use unnamed macro). So in your example if your file in called errtest.C then when you do from the shell prompt: root errtest.C the function errtest in that file will be executed. Same if you enter ROOT and then type .x errtest.C

1 Like

Okay, so in the larger macro where this problem originally arose, I now have a function with the same name as the file, but it does not recognise any of the other functions when either executed with .x or loaded with .L, and my tree is classified as an “undeclared identifier” in the second function?

The other functions should be declared be known from the main function. An other (simpler way) is to put the main function at the end of the file, after the function it uses.

Sorry, typo on my part; I had forgotten to forward declare the first function as I call it again later in the function. The tree is still not being recognised in the new function though?

In the “errtest” function, you define the “e1” variable which you then pass to the “Branch” call. Note that this variable disappears when the “errtest” function finishes so, you need to make sure in advance that it is no longer used by your tree.

1 Like

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