A very simple 1d histogram....clueless why it doesn't work

#include “TTree.h”

void onedhistogram()
{

// const char *data_file = “1dhistogram.txt”;
// const char *root_file = “1dhistogram.root”;

TTree *MyTree = new TTree(“MyTree”,“MyTree”);
MyTree->Readfile(“onedhistogram.txt”,“x”);
MyTree->SetEstimate(-1);
MyTree->Draw(“x”);

}

Can someone please help?
Thanks

You’ll have to show us your 1dhistogram.txt file for us to figure out what’s going on (and for us to test your simple macro). You should also explain what exactly “doesn’t work” means: what did you expect to happen, or what unexpected thing happened?

You should also tell us how you are running the macro. Are you doing: .L foo.C+, .x foo.C, compiling outside of ROOT with g++, etc.

My guess is that it has to do with the type descriptors that are needed for TTree::ReadFile to properly set up the branches, but without 1dhistogram.txt I can’t tell for sure.

I probably don’t need to tell you, but make sure you read this: http://root.cern.ch/root/html/TTree.html#TTree:ReadFile

Jean-François

Hey thanks for your input. Yeah I should have been more specific.
it is a macro that is run by the “.x 1d histogram.c” command in CINT.

I have looked through a couple of different things online including:


and

and I have also checked out the Readfile link you point me towards. I tried adding a TBranch.h header file to the macro but that apparently wasn’t the problem.

I wonder where I messed up.

The data file are floats and there is only one column:

Like:
29.10
33.1
55.2
52.10
33.2
42.1


The error message I get is:
Error: Can’t call TTree::Readfile('onedhistogram.txt,“x”) in current scope [directory of my macrofile].c(13)

Let me know if i can provide more info.

#include “TTree.h”
#include “TBranch.h”
#include “TCanvas.h”
#include “TFile.h”
#include “TGraph.h”

void onedhistogram()
{
gROOT->Reset();

TFile *f =new TFile(“onedhistogram.root”,“RECREATE”);
TH1F *h1 =new TH1F (“h1”, “MPV Distribution”, 100, -4,4);
TTree *MyTree = new TTree(“MyTree”,“MyTree”);
Long64_t nlines = MyTree->Readfile(“onedhistogram.txt”,“x”);
MyTree->SetEstimate(-1);
MyTree->Draw(“x”);
MyTree->Write();

}

an updated file…it still doesn’t work. same error.

You’re going to be kicking yourself…

You made a typo in “ReadFile”, you left the F as lowercase. Probably in CINT you were using tab-completion or were more careful with typing.

Fixing the “F” and trying your original macro in the first post with a dummy data file works for me.

Jean-François

yeah dude. it works now… :open_mouth: Thanks a bunch