Plot histogram from several ascii files(.txt)

Hello everyone!

I am trying to make a histogramm from several .txt files. My plan is to make a macro that will read all ascii files and plot them in a histogram. To start, I tried to plot data from one .txt file, but I don’t to can!

My ascii file is

91.05363788568253 4 92.18552111171896 e 53.79679631716357 m 90.8046694021715 e 86.99876449534648 e 89.82364101505519 m 95.19489972604939 g 89.65891167531177 e 86.75620482372211 e

To plot, I use

TNtuple t("t","test","x:y"); t.ReadFile("Invariant_MassesNew.txt"); t.Draw("x","y==\"e\"");

but nothing is plotted! I can only plot if I delete the second column, but I want my histo to have data only if the second column is e or m or 4 or g.
What am I doing wrong?
Thank you in advance!

{ TTree t("t", "test"); t.ReadFile("Invariant_MassesNew.txt", "x/F:y/C"); t.Draw("x","y==\"e\""); }

Thank you very much!!!
It is working!!!

The thing now is that I have a lot of ascii files.
How can I make a histo for each letter of the second column?

Assume that there are 10 .txt files each of them containing two column “data” as in my first post.
I want to plot in one histo the “e’s” and “m’s”(with different colors for each) and in another histo the “4’s” and “l’s”.
Is that possible?

Thank’s in advance! :smiley:

TTree t(“t”, “test”);
t.ReadFile(“Invariant_Masses_0.txt”, “x/F:y/C”);

TCanvas ch(“ch”, “ch”);
t.Draw(“x >> hh”, “”, “AXIS”);
t.Draw(“x >> h4”, "y==“4"”, “SAME”); h4->SetLineColor(1);
t.Draw(“x >> he”, “y==“e””, “SAME”); he->SetLineColor(2);
t.Draw(“x >> hg”, “y==“g””, “SAME”); hg->SetLineColor(3);
t.Draw(“x >> hm”, “y==“m””, “SAME”); hm->SetLineColor(4);

THStack s(“s”, “s”);

TCanvas cs(“cs”, “cs”);

I tried to put your code in a root macro named plot.c

#include <cstdio>
#include <iostream>
#include <fstream>

# include "TROOT.h"
# include "TGraphErrors.h"
# include "TStyle.h"
# include "TMultiGraph.h"
# include "TF1.h"
# include "TLegend.h"
# include "TPaveStats.h"
# include "TArrow.h"
# include "TLatex.h"
# include "TPaveText.h"
# include "TText.h"
# include "TPavesText.h"

#include "TString.h"
#include "TSystem.h"
#include "TInterpreter.h"
#include "TFile.h"
#include "TH1.h"
#include "TNtuple.h"
#include "TCanvas.h"

void plot(){

TTree t("t", "test");
  t.ReadFile("Invariant_Masses_0.txt", "x/F:y/C");

TCanvas ch("ch", "ch");
  t.Draw("x >> hh", "", "AXIS");
  t.Draw("x >> he", "y==\"e\"", "SAME"); he->SetLineColor(1);

  THStack s;

TCanvas cs("cs", "cs");


But there isn’t any plot coming out!
I use this macro writing

In a “named macro”, you must use “pointers to objects” (e.g. “TTree *t” instead of “TTree t”), otherwise everything will be automatically deleted as soon as your macro terminates.

I used “TTree *t(“t”,“test”);” and I get the error
Syntax error.

Unless you mean something else.

I tried to compile it and it seems that TStack header was missing. I included it but I still don’t get a plot.
It says that he isn’t defined!

How about TTree *t = new TTree(“t”, “test”);

It says “.” is wrong so I changed it to “->” and I get a plot.
However there is an error about TStack

Error: Symbol TStack is not defined in current scope plot.c:60:
Error: type TStack not defined FILE:/home/thanos/Desktop/MC2013/./plot.c LINE:60

How about THStack *s = new THStack(“s”, “s”);

This is done already, like I did with all the “new items”(Canvas, File, Stack).
This error is appearing with THStack *s = new THStack(“s”, “s”);

Never mind…
I misspeled THStack with TStack… :blush:

What is the difference between the plots in the canvas ch and cs?