How to simply make a histogram from .dat file

Hi,

what were your expectations?

Best,
D

I expected something that would look more like a histogram, this is just a lot of dots. But it’s okay, I was probably just imagining it wrong

h->DrawCopy("COLZ"); // "COLZ" or "LEGO2Z" or ...

Hi,

here you can find all the options you can use to draw your histograms: https://root.cern.ch/doc/master/classTHistPainter.html

Best,
D

This is great, thank you

I was also wondering if it is possible to convert this into a TH1D type, or TH1F type?
If I am using the TTree method proposed on this thread, or your method can I convert them to TH1D? My boss was suggesting I work with TH1D or TH1F histograms for something, so I wanted to know if it can be easily converted ? I tired simply

TTree *t1 = new TTree(“t1”, “my events”);
t1->ReadFile(“events.dat”, “first:second”);
//t1->Draw(“second”, “first==1”);
TH1F *hdet1 = new TH1F(“hdet1”, “Histogram”, 1000,-1500,1500);
hdet1->Fill(t1);
TCanvas *c1 = new TCanvas(“c1”,“c1”,900,700);
c1->Draw();
c1->cd();
hdet1->Draw();
But it tells me error: no matching member function for call to ‘Fill’

And when I use your suggested code, it gives me
terminate called after throwing an instance of 'std::runtime_error

TH1D *hdet1 = new TH1D("hdet1", "Histogram", 1000, -1500., 1500.);
t1->Project("hdet1", "second", "first == 1");
hdet1->Draw();

If I wanted to plot multiple histograms at the same time using this method, how would I do so? I know how when I create a new canvas, I can simply divide the canvas or create separate ones for each plot.
I tried

TTree *t1 = new TTree("t1", "my events");
    t1->ReadFile("events.dat", "first:second");
    TH1D *hdet1 = new TH1D("hdet1", "Histogram", 1000, -1500., 1500.);
	TCanvas *c1 = new TCanvas("c1","c1",900,700);
	t1->Project("hdet1", "second", "first == 1");
	hdet1->Draw();
    hdet1->Fit("gaus");
  
  TTree *t2 = new TTree("t2", "my events");
    t2->ReadFile("events.dat", "first:second");
    t2->Draw("second", "first==2");
    TH1D *hdet2 = new TH1D("hdet2", "Histogram", 1000, -1500., 1500.);
	TCanvas *c2 = new TCanvas("c2","c2",900,700);
	t2->Project("hdet2", "second", "first == 2");
	hdet2->Draw(c2);
    hdet2->Fit("gaus");

Try:

gStyle->SetOptFit(111);
TTree *t = new TTree("t", "my events");
t->ReadFile("events.dat", "first/I:second/D");
TH1D *hdet1 = new TH1D("hdet1", "Histogram 1", 1000, -1500., 1500.);
t->Project("hdet1", "second", "first == 1");
TH1D *hdet2 = new TH1D("hdet2", "Histogram 2", 1000, -1500., 1500.);
t->Project("hdet2", "second", "first == 2");
TCanvas *c = new TCanvas("c", "c", 900, 700);
c->Divide(1, 2);
c->cd(1);
hdet1->Fit("gaus");
c->cd(2);
hdet2->Fit("gaus");
c->cd(0);

BTW. When you post “source code” or “output” here, do remember to enclose them into two lines which contain just three characters ``` (see how your post has been edited above).

Is it possible to divide into 5 ? This is what I tried;

	TTree *t = new TTree("t", "my events");
	t->ReadFile("events.dat", "first/I:second/D");
	
	TH1D *hdet1 = new TH1D("hdet1", "Histogram 1", 1000, -1500., 1500.);
	t->Project("hdet1", "second", "first == 1");
	
	
	TH1D *hdet2 = new TH1D("hdet2", "Histogram 2", 1000, -1500., 1500.);
	t->Project("hdet2", "second", "first == 2");
	
	TH1D *hdet3 = new TH1D("hdet3", "Histogram 3", 1000, -1500., 1500.);
	t->Project("hdet3", "third", "first == 3");
	
	TH1D *hdet4 = new TH1D("hdet4", "Histogram 4", 1000, -1500., 1500.);
	t->Project("hdet4", "fourth", "first == 4");
	
	TH1D *hdet5 = new TH1D("hdet5", "Histogram 5", 1000, -1500., 1500.);
	t->Project("hdet5", "third", "first == 5");
	
	
	TCanvas *c = new TCanvas("c", "c", 900, 700);
	c->Divide(1, 2, 3, 4, 5.q
	);
	
	c->cd(1);
	hdet1->Fit("gaus");
	
	c->cd(2);
	hdet2->Fit("gaus");
	
	c->cd(3);
	hdet3->Fit("gaus");
	
	c->cd(4);
	hdet4->Fit("gaus");
	
	c->cd(5);
	hdet5->Fit("gaus");
	
	c->cd(0);```

Try: c->Divide(1, 5);
or: c->DivideSquare(5);

It still only plots 2 though, just makes them smaller (to leave space for 5)

Your tree has “first/I:second/D”.
What is “third” and “fourth” then?

Wow, I was totally confused. I fixed it now. Thanks

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