Search code examples
root-framework

Histogram with ntuple using CERN ROOT


Can't obtain/fill histogram starting from ntuple using ROOT.

I have been looking for previous answers and they all use ttree, including root documentation. I dont have one. Not new to root, but i struggle A LOT using it. None of the methods found work with my code (or i dont know how to implement them).

This is more or less what i have found everywhere:

https://root.cern.ch/root/roottalk/roottalk03/2620.html

but no, i am doing TH1F and i have no ttree (see code below)

{    
   gROOT->Reset();

#include "Riostream.h"
#include <iostream>
#include <fstream>

  in.open( "somefile.dat");

TNtuple *ntuple = new TNtuple("ntuple","some data from ascii file","index1:index2:index3");

//declare variables, create histograms   
   Double_t x,y,Price[215000],Diff[215000],Ret[215000],trend, Data[215000];
   TFile *f = new TFile("TrendsCountBove.root","RECREATE");
   TH1F *h1 = new TH1F("h1","Retornos",100,-0.3,0.3);
   TH1F *histo = new TH1F ("hist_from_ntuple", "some title", nbins, min,max);

//do some stuff (didn't paste all calculations i do, but that works fine)

for (Int_t i = 0; i+1 < nlines-1; i++) {
     Diff[i] = Price[i+1]-Price[i];
     Ret[i] = TMath::Log(Price[i+1])-TMath::Log(Price[i]);
     h1->Fill(Ret[i]);
     ntuple->Fill(i*1.0,Ret[i],Price[i+1]);
  }

So, it all works fine, no problem at all. But then again if please somebody could explain me as detailed as possible how can i fill histo with, say, index1, or 2 or 3. I suck at c++ and i dont really like it nor understand it.

I expect a beautiful histogram where i can set all things like title, max and minimum, stat box, etc. By default root does it via ntuple but it's not what i need.

Thank so much in advance.


Solution

  • Thank you so much guys, first answer worked just right. Now, i'd love to use python instead of ROOT. Problem is, my thesis directors (both physicists, both from particles) never wanted to do so. As a result, suffering for about six months with c++ and ROOT. Such a drag.

    Thanks again!