How to extract histograms from a root file and print them but writing them in a macro to run

So when I extract a histogram from my root file I do it the following way

root -l output_idAntiId_Mc16a.root
root [0]
Attaching file output_idAntiId_Mc16a.root as _file0…
(TFile ) 0x7f8b9cba9470
root [1] .ls
* output_idAntiId_Mc16a.root
TFile* output_idAntiId_Mc16a.root
KEY: TDirectoryFile plotEvent;1 plotEvent
KEY: TDirectoryFile pass_wgantiidcr_all_e_Nominal;1 pass_wgantiidcr_all_e_Nominal
KEY: TDirectoryFile pass_wgantiidcr_all_u_Nominal;1 pass_wgantiidcr_all_u_Nominal
root [2] pass_wgantiidcr_all_e_Nominal->cd()
(bool) true
root [3] .ls
TDirectoryFile* pass_wgantiidcr_all_e_Nominal pass_wgantiidcr_all_e_Nominal
KEY: TDirectoryFile pass_wgantiidcr_all_e_Nominal;1 pass_wgantiidcr_all_e_Nominal
KEY: TDirectoryFile plotEvent_Higgs;1 plotEvent_Higgs
KEY: TDirectoryFile plotEvent_Hyyd1;1 plotEvent_Hyyd1
KEY: TDirectoryFile plotEvent_Hyyd30;1 plotEvent_Hyyd30
KEY: TDirectoryFile plotEvent_Hyyd20;1 plotEvent_Hyyd20
KEY: TDirectoryFile plotEvent_VV;1 plotEvent_VV
KEY: TDirectoryFile plotEvent_Hyyd10;1 plotEvent_Hyyd10
KEY: TDirectoryFile plotEvent_data;1 plotEvent_data
KEY: TDirectoryFile plotEvent_wgamewk;1 plotEvent_wgamewk
KEY: TDirectoryFile plotEvent_EltoPhFakes;1 plotEvent_EltoPhFakes
KEY: TDirectoryFile plotEvent_higgs;1 plotEvent_higgs
KEY: TDirectoryFile plotEvent_Hyyd40;1 plotEvent_Hyyd40
KEY: TDirectoryFile plotEvent_VVV;1 plotEvent_VVV
KEY: TDirectoryFile plotEvent_vgg;1 plotEvent_vgg
KEY: TDirectoryFile plotEvent_Hyyd;1 plotEvent_Hyyd
KEY: TDirectoryFile plotEvent_wqcd;1 plotEvent_wqcd
KEY: TDirectoryFile plotEvent_Zy;1 plotEvent_Zy
KEY: TDirectoryFile plotEvent_bkgs;1 plotEvent_bkgs
KEY: TDirectoryFile plotEvent_Top;1 plotEvent_Top
KEY: TDirectoryFile plotEvent_Zjets;1 plotEvent_Zjets
KEY: TDirectoryFile plotEvent_ZHyy;1 plotEvent_ZHyy
KEY: TDirectoryFile plotEvent_wewk;1 plotEvent_wewk
KEY: TDirectoryFile plotEvent_Wy;1 plotEvent_Wy
KEY: TDirectoryFile plotEvent_zgamewk;1 plotEvent_zgamewk
root [4] plotEvent_Wy->cd()
(bool) true
root [5] .ls
TDirectoryFile* plotEvent_Wy plotEvent_Wy
KEY: TH1D FilterMet;1 FilterMet
KEY: TH1D JetEtaPt25;1 JetEtaPt25
KEY: TH1D JetEtaPt35;1 JetEtaPt35
KEY: TH1D JetEtaPt55;1 JetEtaPt55
KEY: TH1D Mtt;1 Mtt
KEY: TH1D alljet_metsig;1 alljet_metsig
KEY: TH1D averageIntPerXing;1 averageIntPerXing
KEY: TH1D avgCentrality;1 avgCentrality
KEY: TH1D avgmj3_over_mjj;1 avgmj3_over_mjj
KEY: TH1D baseElEta;1 baseElEta
KEY: TH1D baseElPt;1 baseElPt
KEY: TH1D baseMuEta;1 baseMuEta
KEY: TH1D baseMuPt;1 baseMuPt
KEY: TH1D centrality;1 centrality
KEY: TH1D chanFlavor;1 chanFlavor
KEY: TH1D dRj1;1 dRj1
KEY: TH1D dRj2;1 dRj2
KEY: TH1D dphi_mety_ll;1 dphi_mety_ll
KEY: TH1D el_id;1 el_id
KEY: TH1D elid0;1 elid0
KEY: TH1D elid1;1 elid1
KEY: TH1D etaj0TimesEtaj1;1 etaj0TimesEtaj1
KEY: TH1D j0fjvt;1 j0fjvt
KEY: TH1D j0jvt;1 j0jvt
KEY: TH1D j0timing;1 j0timing
KEY: TH1D j1fjvt;1 j1fjvt
KEY: TH1D j1jvt;1 j1jvt
KEY: TH1D j1timing;1 j1timing
KEY: TH1D j3Eta;1 j3Eta
KEY: TH1D j3FJvt;1 j3FJvt
KEY: TH1D j3Jvt;1 j3Jvt
KEY: TH1D j3Pt;1 j3Pt
KEY: TH1D jetEta0;1 jetEta0
KEY: TH1D jetEta1;1 jetEta1
KEY: TH1D jetHT;1 jetHT
KEY: TH1D jetNTracks0;1 jetNTracks0
KEY: TH1D jetNTracks1;1 jetNTracks1
KEY: TH1D jetPartonTruthLabelID0;1 jetPartonTruthLabelID0
KEY: TH1D jetPartonTruthLabelID1;1 jetPartonTruthLabelID1
KEY: TH1D jetPt0;1 jetPt0
KEY: TH1D jetPt1;1 jetPt1
KEY: TH1D jetPt3;1 jetPt3
KEY: TH1D jetTrackWidth0;1 jetTrackWidth0
KEY: TH1D jetTrackWidth1;1 jetTrackWidth1
KEY: TH1D jj_deta_abs;1 jj_deta_abs
KEY: TH1D jj_deta_diff;1 jj_deta_diff
KEY: TH1D jj_deta_signed;1 jj_deta_signed
KEY: TH1D jj_mass_variableBin;1 jj_mass_variableBin
KEY: TH1D jj_mass_variableBinGam;1 jj_mass_variableBinGam
KEY: TH1D lepCh0;1 lepCh0
KEY: TH1D lepCh1;1 lepCh1
KEY: TH1D lepPt0;1 lepPt0
KEY: TH1D lepPt1;1 lepPt1
KEY: TH1D lep_trig_match;1 lep_trig_match
KEY: TH1D maxCentrality;1 maxCentrality
KEY: TH1D max_j3_dr;1 max_j3_dr
KEY: TH1D max_j_eta;1 max_j_eta
KEY: TH1D maxmj3_over_mjj;1 maxmj3_over_mjj
KEY: TH1D mcEventWeight;1 mcEventWeight
KEY: TH1D met_cst_tst_ratio;1 met_cst_tst_ratio
KEY: TH1D met_cst_tst_sub;1 met_cst_tst_sub
KEY: TH1D met_ptllg;1 met_ptllg
KEY: TH1D met_soft_tst_et;1 met_soft_tst_et
KEY: TH1D met_tight_tst_et;1 met_tight_tst_et
KEY: TH1D met_tight_tst_phi;1 met_tight_tst_phi
KEY: TH1D met_truth_phi;1 met_truth_phi
KEY: TH1D met_tst_et;1 met_tst_et
KEY: TH1D met_tst_j1_dphi;1 met_tst_j1_dphi
KEY: TH1D met_tst_j2_dphi;1 met_tst_j2_dphi
KEY: TH1D met_tst_j3_dphi;1 met_tst_j3_dphi
KEY: TH1D met_tst_nolep_et;1 met_tst_nolep_et
KEY: TH1D met_tst_nolep_j1_dphi;1 met_tst_nolep_j1_dphi
KEY: TH1D met_tst_nolep_j2_dphi;1 met_tst_nolep_j2_dphi
KEY: TH1D met_tst_nolep_phi;1 met_tst_nolep_phi
KEY: TH1D met_tst_ph_dphi;1 met_tst_ph_dphi
KEY: TH1D met_tst_phi;1 met_tst_phi
KEY: TH1D metsig_tst;1 metsig_tst
KEY: TH1D metsig_variableBin;1 metsig_variableBin
KEY: TH1D minDR;1 minDR
KEY: TH1D minDRLep;1 minDRLep
KEY: TH1D minDRmj2;1 minDRmj2
KEY: TH1D min_mj3;1 min_mj3
KEY: TH1D min_mj3_over_mjj;1 min_mj3_over_mjj
KEY: TH1D mj1;1 mj1
KEY: TH1D mj2;1 mj2
KEY: TH1D mj34;1 mj34
KEY: TH1D mll;1 mll
KEY: TH1D mllg;1 mllg
KEY: TH1D mt;1 mt
KEY: TH1D mt_mety;1 mt_mety
KEY: TH1D muDR;1 muDR
KEY: TH1D muEta;1 muEta
KEY: TH1D mu_quality;1 mu_quality
KEY: TH1D muquality0;1 muquality0
KEY: TH1D muquality1;1 muquality1
KEY: TH1D nTruthJetMatch;1 nTruthJetMatch
KEY: TH1D n_baseel;1 n_baseel
KEY: TH1D n_basemu;1 n_basemu
KEY: TH1D n_baseph;1 n_baseph
KEY: TH1D n_bjet;1 n_bjet
KEY: TH1D n_el;1 n_el
KEY: TH1D n_el_baseline_crackVetoCleaning;1 n_el_baseline_crackVetoCleaning
KEY: TH1D n_el_baseline_iso;1 n_el_baseline_iso
KEY: TH1D n_el_w;1 n_el_w
KEY: TH1D n_jet;1 n_jet
KEY: TH1D n_jet_cen;1 n_jet_cen
KEY: TH1D n_jet_cenj;1 n_jet_cenj
KEY: TH1D n_jet_cenj30;1 n_jet_cenj30
KEY: TH1D n_jet_cenj40;1 n_jet_cenj40
KEY: TH1D n_jet_cenj50;1 n_jet_cenj50
KEY: TH1D n_jet_fwd;1 n_jet_fwd
KEY: TH1D n_jet_fwdj;1 n_jet_fwdj
KEY: TH1D n_jet_fwdj30;1 n_jet_fwdj30
KEY: TH1D n_jet_fwdj40;1 n_jet_fwdj40
KEY: TH1D n_jet_fwdj50;1 n_jet_fwdj50
KEY: TH1D n_mu;1 n_mu
KEY: TH1D n_mu_baseline_iso;1 n_mu_baseline_iso
KEY: TH1D n_mu_baseline_noOR;1 n_mu_baseline_noOR
KEY: TH1D n_mu_w;1 n_mu_w
KEY: TH1D n_ph;1 n_ph
KEY: TH1D n_ph_baseline_crackVetoCleaning;1 n_ph_baseline_crackVetoCleaning
KEY: TH1D n_tau;1 n_tau
KEY: TH1D n_truth_tau;1 n_truth_tau
KEY: TH1D n_vx;1 n_vx
KEY: TH1D pTt;1 pTt
KEY: TH1D phEta;1 phEta
KEY: TH1D phPt;1 phPt
KEY: TH1D phcentrality;1 phcentrality
KEY: TH1D ptll;1 ptll
KEY: TH1D ptllg;1 ptllg
KEY: TH1D ptvarcone20;1 ptvarcone20
KEY: TH1D ptvarcone30;1 ptvarcone30
KEY: TH1D tmva_variableBin;1 tmva_variableBin
KEY: TH1D topoetcone20;1 topoetcone20
KEY: TH1D trigger_lep;1 trigger_lep
KEY: TH1D truthElEta;1 truthElEta
KEY: TH1D truthElPt;1 truthElPt
KEY: TH1D truthJet1Pt;1 truthJet1Pt
KEY: TH1D truthMuEta;1 truthMuEta
KEY: TH1D truthMuPt;1 truthMuPt
KEY: TH1D truthTauDR;1 truthTauDR
KEY: TH1D truthTauEta;1 truthTauEta
KEY: TH1D truthTauPt;1 truthTauPt
KEY: TH1D truth_j1_pt;1 truth_j1_pt
KEY: TH1D truth_jj_deta;1 truth_jj_deta
KEY: TH1D truth_jj_mass_variableBin;1 truth_jj_mass_variableBin
KEY: TH1D truth_max_jj_mass;1 truth_max_jj_mass
KEY: TH1D w;1 w
KEY: TH1D wElEta;1 wElEta
KEY: TH1D wElPhi;1 wElPhi
KEY: TH1D wElPt;1 wElPt
KEY: TH1D wMuEta;1 wMuEta
KEY: TH1D wMuPhi;1 wMuPhi
KEY: TH1D wMuPt;1 wMuPt
KEY: TH2D DphimetPhDphimetjet1;1 DphimetPhDphimetjet1
KEY: TH2D DphimetPhDphimetjet2;1 DphimetPhDphimetjet2
KEY: TH2D DphimetyllDphimetPh;1 DphimetyllDphimetPh
KEY: TH2D DphimetyllDphimetjet1;1 DphimetyllDphimetjet1
KEY: TH2D DphimetyllDphimetjet2;1 DphimetyllDphimetjet2
KEY: TH2D DphimetyllMetptllg;1 DphimetyllMetptllg
KEY: TH2D DphimetyllMetsig;1 DphimetyllMetsig
KEY: TH2D MetptllgDphimetPh;1 MetptllgDphimetPh
KEY: TH2D MetptllgDphimetjet1;1 MetptllgDphimetjet1
KEY: TH2D MetptllgDphimetjet2;1 MetptllgDphimetjet2
KEY: TH2D MetsigDphimetPh;1 MetsigDphimetPh
KEY: TH2D MetsigDphimetjet1;1 MetsigDphimetjet1
KEY: TH2D MetsigDphimetjet2;1 MetsigDphimetjet2
KEY: TH2D MetsigMetptllg;1 MetsigMetptllg
KEY: TH2D MetvsMu;1 MetvsMu
KEY: TH2D jj_mass_dphi_variableBin;1 jj_mass_dphi_variableBin

root [7] wElEta->Draw()
Info in TCanvas::MakeDefCanvas: created default TCanvas with name c1

this gives me the desired plot but I am having trouble writing a macro that does all this and I do not have to keep typing this up over and over again I Tried
TFile *f= new TFile(“output_IdId_Mc16a.root”);
TH1F h1= (TH1F)f->Get("/pass_wgantiidcr_all_e_Nominal/plotEvent_Wy/wElEta");

but it doesnt work any ideas ?

TFile *f = TFile::Open("output_idAntiId_Mc16a.root");
if ((!f) || f->IsZombie()) { delete f; return; } // just a precaution
TH1D *h1;
f->GetObject("pass_wgantiidcr_all_e_Nominal/plotEvent_Wy/wElEta", h1);
if (h1) h1->Draw();

Processing practice.cpp…
In file included from input_line_11:1:
/Users/andrewbarajas/practice.cpp:14:1: error: expected unqualified-id
if ((!f) || f->IsZombie()) { delete f; return; } // just a precaution
/Users/andrewbarajas/practice.cpp:16:1: error: unknown type name ‘f’
f->GetObject(“pass_wgantiidcr_all_e_Nominal/plotEvent_Wy/wElEta”, h1);
/Users/andrewbarajas/practice.cpp:16:2: error: cannot use arrow operator on a type
f->GetObject(“pass_wgantiidcr_all_e_Nominal/plotEvent_Wy/wElEta”, h1);
/Users/andrewbarajas/practice.cpp:17:1: error: expected unqualified-id
if (h1) h1->Draw();

It is what I get when I try the macro you showed me I am not sure why its giving me problems ?

An “unnamed ROOT macro” MUST look like this:

  // ... here comes the source code ...

Right forgot the whole void function part my bad thank you !
So I understand the first line we create a pointer to the root file we are opening
what is the precaution part doing ?
After that I know we create 1D histogram pointer and then we use the f-> to access member of the root file which is basically the plot i want and the , h1 is so we like fill our empty histogram with the plot we want
and the last line I get we use to draw but why the if statement ? Let me know if any of my interpretations are wrong → precaution :wink:

would the get object differ if I use a TH1F ?? instead of a TH1D

It did tell you what it is:

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