Draw histos from root file

Hello to everyone!

I am trying to plot histos that already exist in a root file.
I can do that from terminal using

[code]thanos@thanos-laptop:~/recomm$ root run94_processed.root
root [0]
Attaching file run94_processed.root as _file0…
root [1] clu_mm2X->Scan()


  • Row * Instance * cl_event * cl_chambe * cl_readou * cl_chambe * cl_plane_ * cl_readou * cl_charge * cl_time *

  •    0 *        0 *         1 *       mm2 *         X *         0 *         0 *         0 *       681 *         2 *
    
  •    1 *        0 *         2 *       mm2 *         X *         0 *         0 *         0 *      5006 *         3 *
    
  •    2 *        0 *         3 *       mm2 *         X *         0 *         0 *         0 *       566 *         4 *
    
  •    3 *        0 *         4 *       mm2 *         X *         0 *         0 *         0 *      2729 *         2 *
    
  •    4 *        0 *         5 *       mm2 *         X *         0 *         0 *         0 *      1780 *         2 *
    
  •    5 *        0 *         6 *       mm2 *         X *         0 *         0 *         0 *       542 *         0 *
    
  •    6 *        0 *         7 *       mm2 *         X *         0 *         0 *         0 *       396 *         1 *
    
  •    7 *        0 *         8 *       mm2 *         X *         0 *         0 *         0 *      1648 *         3 *
    
  •    8 *        0 *         9 *       mm2 *         X *         0 *         0 *         0 *       322 *         3 *
    
  •    9 *        0 *        10 *       mm2 *         X *         0 *         0 *         0 *      1346 *         3 *
    
  •   10 *        0 *        11 *       mm2 *         X *         0 *         0 *         0 *       870 *         0 *
    
  •   11 *        0 *        12 *       mm2 *         X *         0 *         0 *         0 *      1572 *         0 *
    
  •   12 *        0 *        13 *       mm2 *         X *         0 *         0 *         0 *       896 *         2 *
    
  •   13 *        0 *           *           *           *           *           *           *           *           *
    
  •   14 *        0 *        15 *       mm2 *         X *         0 *         0 *         0 *      1450 *         2 *
    
  •   15 *        0 *           *           *           *           *           *           *           *           *
    
  •   16 *        0 *        17 *       mm2 *         X *         0 *         0 *         0 *       514 *         0 *
    
  •   17 *        0 *        18 *       mm2 *         X *         0 *         0 *         0 *       730 *         1 *
    
  •   18 *        0 *        19 *       mm2 *         X *         0 *         0 *         0 *      3306 *         4 *
    
  •   19 *        0 *        20 *       mm2 *         X *         0 *         0 *         0 *       470 *         1 *
    
  •   20 *        0 *           *           *           *           *           *           *           *           *
    
  •   21 *        0 *        22 *       mm2 *         X *         0 *         0 *         0 *      2411 *         2 *
    
  •   22 *        0 *        23 *       mm2 *         X *         0 *         0 *         0 *      1666 *         2 *
    
  •   23 *        0 *        24 *       mm2 *         X *         0 *         0 *         0 *       485 *         0 *
    
  •   24 *        0 *        25 *       mm2 *         X *         0 *         0 *         0 *      1057 *         0 *
    

Type to continue or q to quit ==> q


(Long64_t)25
root [2] clu_mm2X->Draw(“cl_charge”)
Info in TCanvas::MakeDefCanvas: created default TCanvas with name c1
root [3] .q[/code]

All I want to do is make a macro which will do it automatically.

I tried the same commands in a .c macro but it didn’t work

[code]# include “TCanvas.h”

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”

void test_recomm(){

// open file
TFile *fin = TFile::Open(“run94.root”);
fin->cd();

clu_mm2X->Draw("cl_charge");

}[/code]

Is it possible to be done?

Thank you in advance! :slight_smile:

http://root.cern.ch/drupal/content/reading-and-writing-objects
http://root.cern.ch/drupal/content/retrieving-objects-disk
http://root.cern.ch/root/html/TDirectoryFile.html#TDirectoryFile:Get
http://root.cern.ch/download/doc/ROOTUsersGuideHTML/ch12s20.html

I’ve tried the following but it’s not working…

[code]#include
#include
#include

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 test_recomm(){

// open file

//TFile * f1 = new TFile ( "run94_processed.root" ) ;
//TH1F *k1 = ( TH1F * ) f1-> Get ("clu_mm4X")->Get("cl_charge");
TFile f ("run94_processed.root");
TTree *MyTree = clu_mm4X;
//TCanvas *myCanvas = new TCanvas();
//MyTree->Draw("cl_charge");

gROOT->SetStyle(“Plain”);
gStyle->SetOptStat(1111);
gStyle->SetOptFit(1111);

TCanvas *c = new TCanvas(“c”, “cluster charge”);
c->SetFillColor(kRed);
c->SetFrameFillColor(10);

//MyTree->SetTitle(“Cluster Charge”);
gStyle->SetHistFillColor(kBlue);
MyTree->UseCurrentStyle();

//TAxis* xax = MyTree->GetXaxis();
//MyTree->SetTitle("cluster charge");
//xax->SetTitleColor(kBlue);
//----MyTree->Draw("cl_charge");---This command is really working
//MyTree->Draw("cl_charge>>h1(100,150,250)")
   
//TH1F *h1 = new TH1F;
MyTree->Draw("cl_charge>>h1(93,0,140000)");
//h1->Draw();

}
[/code]

I only get a red canvas but no histogram at all…

If I type during a root session

is working fine.

Why can’t I run through a macro?

Hi,

Try: TFile *f = TFile::Open("run94_processed.root"); TTree *MyTree = (TTree *)f->Get("clu_mm4X");
Cheers, Bertrand.

TFile *f = TFile::Open("run94_processed.root"); TTree *MyTree; f->GetObject("clu_mm4X", MyTree); if (!MyTree) { std::cout << "Warning: clu_mm4X TTree NOT found!" << std::endl; return; }

It is working!!! =D>
As you see, I declare the histo like that

MyTree->Draw("cl_charge>>h1(93,0,140000)");

Is there a way to declare it, without having to specify bin size, xmin and xmax?
I want these variables to be automatically created.

See “Saving the result of Draw to an histogram” in http://root.cern.ch/root/html/TTree.html#TTree:Draw@2 and “Filling a Histogram” in http://root.cern.ch/download/doc/ROOTUsersGuideHTML/ch12s20.html

Thank’s for your answer!

I replaced

with

but the bin size is different from the one of the root file…

I have tried to find a solution to that, but I couldn’t.
However, I did some improvements.
I changed the axis range, I divided the canvas, I added a pad as a title and I changed pad’s color.
My code is now like that:

[code]#include
#include
#include

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 test_recomm(){

// open file

//TFile * f1 = new TFile ( "run94_processed.root" ) ;
//TH1F *k1 = ( TH1F * ) f1-> Get ("clu_mm4X")->Get("cl_charge");

TFile *f = TFile::Open("run94_processed.root");
TTree *MyTree = (TTree *)f->Get("clu_mm4X");

//TFile f ("run94_processed.root");
//TTree *MyTree = clu_mm4X;
//TCanvas *myCanvas = new TCanvas();
//MyTree->Draw("cl_charge");

gROOT->SetStyle(“Plain”);
gStyle->SetOptStat(1111);
gStyle->SetOptFit(1111);

TCanvas *c = new TCanvas(“c”, “cluster charge”);
c->SetFillColor(5 );
//c->SetFrameFillColor(5);
c->Divide(2,3);
c->cd(1);
TPaveText *pt = new TPaveText(0.2101,0.2910,0.7944,0.8278,“br”);
pt->SetFillColor(19);
TText *text=pt->AddText(“Cluster charge”);
text=pt->AddText(“charge Cluster”);
pt->Draw();
c->Modified();
c->cd();
c_4->cd();
c_4->SetFillColor(kRed);
//gStyle->SetPadColor(10);
c_4->SetFrameFillColor(10);
c_4->SetBorderMode(1);
c_4->SetBorderSize(2);

//MyTree->SetTitle(“Cluster Charge”);
gStyle->SetHistFillColor(kBlue);
MyTree->UseCurrentStyle();

//TAxis* xax = MyTree->GetXaxis();
//MyTree->SetTitle("cluster charge");
//xax->SetTitleColor(kBlue);
//----MyTree->Draw("cl_charge");---This command is really working
//MyTree->Draw("cl_charge>>h1(100,150,250)")
//MyTree->Fit("landau","W",NULL,1,20);

//TH1F *h1 = new TH1F;
MyTree->Draw("cl_charge>>h1","cl_charge>0");

//MyTree->Draw("cl_cluster>>h1(100,150,250)")

h1->SetTitle("cluster charge");
h1->Draw();
h1->Fit("landau","W",NULL,1,20000);

c->Modified(); c->Update();

c_3->cd();
c_3->SetFillColor(kRed);
//gStyle->SetPadColor(10);
c_3->SetFrameFillColor(10);
c_3->SetBorderMode(1);
c_3->SetBorderSize(2);

//MyTree->SetTitle(“Cluster Charge”);
gStyle->SetHistFillColor(kBlue);
MyTree->UseCurrentStyle();

//TAxis* xax = MyTree->GetXaxis();
//MyTree->SetTitle("cluster charge");
//xax->SetTitleColor(kBlue);
//----MyTree->Draw("cl_charge");---This command is really working
//MyTree->Draw("cl_charge>>h1(100,150,250)")
//MyTree->Fit("landau","W",NULL,1,20);

//TH1F *h1 = new TH1F;
MyTree->Draw("cl_charge>>h1","cl_charge>0");

//MyTree->Draw("cl_cluster>>h1(100,150,250)")

h1->SetTitle("cluster charge");
h1->SetAxisRange(0,20000,"X");
h1->Draw();
h1->Fit("landau","W",NULL,1,20000);

c->Modified(); c->Update();

}
[/code]

But I still cannot figure out how to have the root file’s bin size…
Any ideas?

Please find attached the two histos that are created with both ways.
You can see that counts for first bin for “with_option_x>0” are greater than the ones for “straight from root file”.
So I assume that the bin size is different in two cases…
with_option_x>0.pdf (13.9 KB)
straight from root file.pdf (14.1 KB)

A “root file” has NO embedded “bin size”.
The difference in histogram’s “entries number” can simply be the number of entries in your tree with “cl_charge<=0 || cl_charge>140000”.

I know that a root file has no bin size.
I just meant, the histo that comes directly from the root file, if, while in root session, I give

[code]thanos@thanos-laptop:~/recomm$ root run94_processed.root
root [0]
Attaching file run94_processed.root as _file0…
root [1] .ls
TFile** run94_processed.root
TFile* run94_processed.root
KEY: TTree raw;1 rawapvdata
KEY: TTree pedestals;2 apvpedestals
KEY: TEnv DetConfig;1 Handle ROOT configuration resources
KEY: TDirectoryFile mm2;1 mm2
KEY: TDirectoryFile mm3;1 mm3
KEY: TDirectoryFile mm4;1 mm4
KEY: TGeoManager world;1 the simplest geometry
KEY: TVector3 posmm2;1 A 3D physics vector
KEY: TTree hits_mm2;1 hits_this_plane
KEY: TTree drift_mm2;1 hits_this_plane
KEY: TTree clu_mm2X;1 clusters_this_readout
KEY: TTree clu_mm2Y;1 clusters_this_readout
KEY: TVector3 posmm3;1 A 3D physics vector
KEY: TTree hits_mm3;1 hits_this_plane
KEY: TTree drift_mm3;1 hits_this_plane
KEY: TTree clu_mm3X;1 clusters_this_readout
KEY: TTree clu_mm3Y;1 clusters_this_readout
KEY: TVector3 posmm4;1 A 3D physics vector
KEY: TTree hits_mm4;1 hits_this_plane
KEY: TTree drift_mm4;1 hits_this_plane
KEY: TTree clu_mm4X;1 clusters_this_readout
KEY: TTree clu_mm4Y;1 clusters_this_readout
KEY: TPaveText ;1 PaveText. A Pave with several lines of text.
root [2] clu_mm2X->Scan()


  • Row * Instance * cl_event * cl_chambe * cl_readou * cl_chambe * cl_plane_ * cl_readou * cl_charge * cl_time *

  •    0 *        0 *         1 *       mm2 *         X *         0 *         0 *         0 *       681 *         2 *
    
  •    1 *        0 *         2 *       mm2 *         X *         0 *         0 *         0 *      5006 *         3 *
    
  •    2 *        0 *         3 *       mm2 *         X *         0 *         0 *         0 *       566 *         4 *
    
  •    3 *        0 *         4 *       mm2 *         X *         0 *         0 *         0 *      2729 *         2 *
    
  •    4 *        0 *         5 *       mm2 *         X *         0 *         0 *         0 *      1780 *         2 *
    
  •    5 *        0 *         6 *       mm2 *         X *         0 *         0 *         0 *       542 *         0 *
    
  •    6 *        0 *         7 *       mm2 *         X *         0 *         0 *         0 *       396 *         1 *
    
  •    7 *        0 *         8 *       mm2 *         X *         0 *         0 *         0 *      1648 *         3 *
    
  •    8 *        0 *         9 *       mm2 *         X *         0 *         0 *         0 *       322 *         3 *
    
  •    9 *        0 *        10 *       mm2 *         X *         0 *         0 *         0 *      1346 *         3 *
    
  •   10 *        0 *        11 *       mm2 *         X *         0 *         0 *         0 *       870 *         0 *
    
  •   11 *        0 *        12 *       mm2 *         X *         0 *         0 *         0 *      1572 *         0 *
    
  •   12 *        0 *        13 *       mm2 *         X *         0 *         0 *         0 *       896 *         2 *
    
  •   13 *        0 *           *           *           *           *           *           *           *           *
    
  •   14 *        0 *        15 *       mm2 *         X *         0 *         0 *         0 *      1450 *         2 *
    
  •   15 *        0 *           *           *           *           *           *           *           *           *
    
  •   16 *        0 *        17 *       mm2 *         X *         0 *         0 *         0 *       514 *         0 *
    
  •   17 *        0 *        18 *       mm2 *         X *         0 *         0 *         0 *       730 *         1 *
    
  •   18 *        0 *        19 *       mm2 *         X *         0 *         0 *         0 *      3306 *         4 *
    
  •   19 *        0 *        20 *       mm2 *         X *         0 *         0 *         0 *       470 *         1 *
    
  •   20 *        0 *           *           *           *           *           *           *           *           *
    
  •   21 *        0 *        22 *       mm2 *         X *         0 *         0 *         0 *      2411 *         2 *
    
  •   22 *        0 *        23 *       mm2 *         X *         0 *         0 *         0 *      1666 *         2 *
    
  •   23 *        0 *        24 *       mm2 *         X *         0 *         0 *         0 *       485 *         0 *
    
  •   24 *        0 *        25 *       mm2 *         X *         0 *         0 *         0 *      1057 *         0 *
    

Type to continue or q to quit ==> q


(Long64_t)25
root [3] clu_mm2X->Draw(“cl_carge”)
Error in TTreeFormula::Compile: Bad numerical expression : "cl_carge"
root [4] clu_mm2X->Draw(“cl_charge”)
Info in TCanvas::MakeDefCanvas: created default TCanvas with name c1
[/code]

I also tried

but I get the same as

clu_mm2X->Draw("cl_charge").

Please find attached the two plots, zoomed at the first bin.
You can clearly see that the bin size is different.
Could that be a bug?
with_option_x>0.pdf (13.3 KB)
straight from root file.pdf (13.5 KB)

Again, what concerns the histogram binning, i.e. in case you use Draw(“someVar”) or Draw(“someVar>>someHisto”), see “Saving the result of Draw to an histogram” in http://root.cern.ch/root/html/TTree.html#TTree:Draw@2 and “Filling a Histogram” in http://root.cern.ch/download/doc/ROOTUsersGuideHTML/ch12s20.html
You might also be interested in “Retrieving the result of Draw” in http://root.cern.ch/root/html/TTree.html#TTree:Draw@2 and “Accessing the Histogram in Batch Mode” in http://root.cern.ch/download/doc/ROOTUsersGuideHTML/ch12s20.html

Thank you very much for your time and support!!! :smiley:
What I was doing was absolutely right.
The only thing that was wrong was the fact that I was using a different tree in my macro-I was using mm4X instead of mm2X. :blush: Silly me!
But now I am having another issue…
I want to load multiple root files, that all contain same trees.
I want to print in a divided canvas histograms of the same variable for different root files.
I tried

[code]#include
#include
#include

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 test_recomm(){

// open file

//TFile * f1 = new TFile ( "run94_processed.root" ) ;
//TH1F *k1 = ( TH1F * ) f1-> Get ("clu_mm4X")->Get("cl_charge");

TFile *f = TFile::Open("run94_processed.root");
TTree *MyTree = (TTree *)f->Get("clu_mm2X");


TFile *f1 = TFile::Open("run92_processed.root");
TTree *MyTree1 = (TTree *)f->Get("clu_mm2X");
//TFile f ("run94_processed.root");
//TTree *MyTree = clu_mm4X;
//TCanvas *myCanvas = new TCanvas();
//MyTree->Draw("cl_charge");

gROOT->SetStyle(“Plain”);
gStyle->SetOptStat(1111);
gStyle->SetOptFit(1111);

TCanvas *c = new TCanvas(“c”, “cluster charge”);
c->SetFillColor(5 );
//c->SetFrameFillColor(5);
c->Divide(2,3);
c->cd(1);
TPaveText *pt = new TPaveText(0.2101,0.2910,0.7944,0.8278,“br”);
pt->SetFillColor(19);
TText *text=pt->AddText(“Cluster charge”);
text=pt->AddText(“charge Cluster”);
pt->Draw();
c->Modified();
c->cd();
c_4->cd();
c_4->SetFillColor(kRed);
//gStyle->SetPadColor(10);
c_4->SetFrameFillColor(10);
c_4->SetBorderMode(1);
c_4->SetBorderSize(2);

//MyTree->SetTitle(“Cluster Charge”);
gStyle->SetHistFillColor(kBlue);
MyTree->UseCurrentStyle();

//TAxis* xax = MyTree->GetXaxis();
//MyTree->SetTitle("cluster charge");
//xax->SetTitleColor(kBlue);
//----MyTree->Draw("cl_charge");---This command is really working
//MyTree->Draw("cl_charge>>h1(100,150,250)")
//MyTree->Fit("landau","W",NULL,1,20);

//TH1F *h1 = new TH1F;
MyTree->Draw("cl_charge>>+h1","cl_charge>0");

//MyTree->Draw("cl_cluster>>h1(100,150,250)")

h1->SetTitle("cluster charge");
h1->Draw();
h1->Fit("landau","W",NULL,1,20000);

c->Modified(); c->Update();

c_3->cd();
c_3->SetFillColor(kRed);
//gStyle->SetPadColor(10);
c_3->SetFrameFillColor(10);
c_3->SetBorderMode(1);
c_3->SetBorderSize(2);

//MyTree->SetTitle(“Cluster Charge”);
gStyle->SetHistFillColor(kBlue);
MyTree1->UseCurrentStyle();

//TAxis* xax = MyTree->GetXaxis();
//MyTree->SetTitle("cluster charge");
//xax->SetTitleColor(kBlue);
//----MyTree->Draw("cl_charge");---This command is really working
//MyTree->Draw("cl_charge>>h1(100,150,250)")
//MyTree->Fit("landau","W",NULL,1,20);

//TH1F *h1 = new TH1F;
MyTree1->Draw("cl_charge>>h2");

//MyTree->Draw("cl_cluster>>h1(100,150,250)")

h2->SetTitle("apv channel");
//h2->SetAxisRange(0,7000,"X");
h2->Draw();
//h2->Fit("landau","W",NULL,1,20000);

c->Modified(); c->Update();

}
[/code]

but it’s not working.

Is it possible to be done?

Thank you in advance!

P.S.: If I have to add a new topic for that, please let me know :wink:

TTree *MyTree1 = (TTree *)f1->Get(“clu_mm2X”);
BTW. You’d better learn to use “GetObject” instead of “Get”.

I hate when this is happening!!!
Thank very much my forever root-problem-solver!!! =D>

GetObject?
Why is that better?
Apart from my question, I trust you so I’m going to read sth about GetObject!

See the “NOTE:” in http://root.cern.ch/root/html/TDirectoryFile.html#TDirectoryFile:Get