Opening a TFile and drawing a histogram

Hi everyones, I’ve some root files, I must open them and to make histograms.
I use this code

TFile *f = new TFile(“myfile.root”); ();

the command gives me

KEY: TTree lemma;1 silicon ntuple

then I write

TTree *MyTree=lemma
TTree *MyTree;f.GetObject("lemma",MyTree);
TCanvas *myCanvas=new TCanvas()

but I don’t know how ti continue to draw histograms…

si-500330.root (1.5 MB)


so, I inspected the content of the file you shared (thanks!) with the rootls command:

> rootls -lt si-500330.root:lemma
TTree  Sep 25 21:57 2018 lemma  "silicon ntuple"
  iev         "iev/I"             24547
  nhits       "nhits/I"           24549
  subdet      "subdet[500]/I"     12268192
  xh          "xh[500]/F"         12266560
  yh          "yh[500]/F"         12266560
  zh          "zh[500]/F"         12266560
  itrack      "itrack[500]/I"     12268192
  Calo_EnDep  "Calo_EnDep[25]/D"  1226842
  Calo_Time   "Calo_Time[25]/I"   613440

Now I make up a dummy study of your ntuple: I hope you can start copy and pasting from it :slight_smile: I do that with RDataFrame: you can visit the link to go through the doc of the utility.

// Create a data frame to access the dataset
root [1] ROOT::RDataFrame rdf("lemma", "si-500330.root");

// Now, a couple of histograms and simple data manipulation

// Simple histo of the number of hits
auto hNhits = rdf.Histo1D("nhits");

// Simple histo of the energy deposition for number of hits greater than 12
auto hCaloEnDep = rdf.Filter("nhits > 12").Histo1D("Calo_EnDep");

TCanvas c;

I hope this helps.


Hi, first of all thanks for your reply. I had root 5.3436, I used your code but it didn’t work…I searched on internet then I read that it works just on root 6, then I downloaded it …but I get the error in the print screen…



I don’t know how to help here… I am confused by the error message which does not tell me much :frowning:
Did you try reducing code reproducing the issue to the minimum?


Hi…the error in the previous screen is not due to your code…I get it just opening Root 6! (I used Root 5, but given that works in your code need Root 6 I installed it).

Maybe I must open a New topic asking help to install Root 6, then I Will try tour code!

Hi, those error messages are harmless and should not prevent you to use ROOT 6 on Windows. They will disappear in the version 6.16.00

Hi bellenot, thank you for your reply…then is there a code to plot those histograms using root 5?

Best regards

Here is an example:

root [0] TFile *f = TFile::Open("si-500330.root");
root [1] TTree *t = 0;
root [2] f->GetObject("lemma", t);
root [3] t->Draw("Calo_EnDep", "nhits > 12");
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1
root [4]

Cheers, Bertrand.

Hi bellenot, thank for your help, it worked! just last question, please…what can I use in Root 5 to read the tree as Dpapir did in Root 6 using the command

 rootls -lt si-500330.root:lemma

I tried

TTree *h1=(TTree*)f.Get("lemma");

but I don’t have an output…

this line alone is not suppose to print or draw anything. What do you do to (try to) get output?

Hi pcanl, sorry for late reply. I’d like to do what dpapiro did using this code (read the tree content)

> rootls -lt si-500330.root:lemma

by using a Root 5 code (the code used by dpapiro just work using root 6, but I want to read the “lemma” content using Root 5 (Root 6 doesn’t workon my computer).

TFile *f = TFile::Open("si-500330.root");
TKey *key = (TKey*)f->GetListOfKeys()->FindObject("lemma");
if (key) key->ls();

Hi pcanal, first thanks for your help, but maybe I didnt’ explain well what I mean. By using your code I just get

KEY: TTree      lemma;1 silicon ntuple

but I was able to to gett it also by using the code that I wrote in my first message

TFile *f = new TFile(“si-500330.root”); ();

What I need now is to inspect the “lemma” content ``
TTree Sep 25 21:57 2018 lemma “silicon ntuple”
iev “iev/I” 24547
nhits “nhits/I” 24549
subdet “subdet[500]/I” 12268192
xh “xh[500]/F” 12266560
yh “yh[500]/F” 12266560
zh “zh[500]/F” 12266560
itrack “itrack[500]/I” 12268192
Calo_EnDep “Calo_EnDep[25]/D” 1226842
Calo_Time “Calo_Time[25]/I” 613440

as dpapiro did by using the Root 6 code

rootls -lt si-500330.root:lemma

Thank you


Thank you Wile! Is it possible also to write the lemma content on a .txt file?

In an interactive ROOT 5 session, try:

lemma->Print(); > lemma.Print.txt

Hi wile, thanks again for your help.
I can’t use an interactive ROOT 5 session, because, I’m writing the macro (I’ve more files). Anyway, it isnt’ important so important to write the tree content.
I wroote the macro (see attachment), it works, but I get errors trying to modify the labels (title, fonts, size, offset etc.). I commented the code that gives the errors.
Can you (or someone else) look it?
calor.cpp (2.6 KB)

In both pads, use:

(TH1F*)gPad->GetPrimitive("htemp"); // the default temporary histogram

Thank you Wile, it worked! But I’ve a (I hope) last problem. Calo_EnDep is an array, as you can see by dpaparo’s message:

Calo_EnDep  "Calo_EnDep[25]/D"  1226842

By plotting “Calo_EnDep”, I get the plot of all 25 culoms of the array. I modified the macro by replacing

char caloname[200]= "Calo_EnDep";


char caloname[200]= “Calo_EnDep[10]”;

so that for example I should get only the plot of the culomn 10 (I think to get it, can you confirm that it is the right way?)

Is it possible to get more culoms (for example 1,5, 10, 20)?

I tried

char caloname[200]= "Calo_EnDep[1,5,10,20,]"; 

but I don’t think it’s the right way.

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