How to add error bars to efficiency graphs

Hi Root Experts,

I am plotting an efficiency graph and would like to add error bars on the efficiency. The htemp1->Sumw2(); function doesn’t seem to do this correctly. I have added two plots where I add plotting the efficiency and adding Sumw2().

Any help would be greatly appreciated,
Brandon




Hi,

How are you doing your efficiency plot ?
You should use the TEfficiency class, see

root.cern.ch/doc/master/classTEfficiency.html

Lorenzo

Here is an example on how I am calculating the efficiency:

TTree *MyTree = MyTree;
TCanvas *myCanvas = new TCanvas();

MyTree->Draw("Variable1>>htemp1","");
MyTree->Draw("Variable1>>htemp2","Variable1 with cut","same");

htemp2->Divide(htemp1);
htemp2->Draw("");

htemp1->Sumw2(); Seems to not give me the right error.

Hi,

TH1::Divide will not give you the right error. As mentioned earlier use the TEfficiency class.
Just do for example, after having filled your histogram htemp1 and htemp2 from your TTree

TEfficiency * off = new TEfficiency(htemp2,htemp1); 
eff->SetStatisticOption(TEfficiency::kFCP);  // to set option for errors (see ref doc)
eff->SetConfidenceLevel(0.68);
eff->Draw("AP");

Lorenzo

Following the link here : https://root.cern.ch/doc/master/classTEfficiency.html#aa0e99b4161745fd3bee0ae5c0f58880e

In a root interactive sesson, I do:

-bash-4.1$ root -l Gallery_output.root 
root [0] 
Attaching file Gallery_output.root as _file0...
(TFile *) 0x3343fb0
root [1] MCTracktree->Draw("muontracklength_det >> h0(30,0,900)")
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1
root [2] MCTracktree->Draw("muontracklength_det >> h1(30,0,900)","angle_smallest<0.1")
(Long64_t) 36684
root [3] TEfficiency* eff= new TEfficiency(h1,h0);

I get the following error:

ROOT_prompt_3:1:23: error: no matching constructor for initialization of 'TEfficiency'
TEfficiency* eff= new TEfficiency(h1,h0);
                      ^           ~~~~~
/cvmfs/fermilab.opensciencegrid.org/products/larsoft/root/v6_08_06g/Linux64bit+2.6-2.12-e14-nu-prof/etc/../include/TEfficiency.h:80:7: note: candidate constructor not viable: no known conversion from
      'TH1F *' to 'const TH1' for 1st argument; dereference the argument with *
      TEfficiency(const TH1& passed,const TH1& total);
      ^
/cvmfs/fermilab.opensciencegrid.org/products/larsoft/root/v6_08_06g/Linux64bit+2.6-2.12-e14-nu-prof/etc/../include/TEfficiency.h:96:7: note: candidate constructor not viable: requires single argument
      'heff', but 2 arguments were provided
      TEfficiency(const TEfficiency& heff);
      ^
/cvmfs/fermilab.opensciencegrid.org/products/larsoft/root/v6_08_06g/Linux64bit+2.6-2.12-e14-nu-prof/etc/../include/TEfficiency.h:79:7: note: candidate constructor not viable: requires 0 arguments, but 2
      were provided
      TEfficiency();
      ^
/cvmfs/fermilab.opensciencegrid.org/products/larsoft/root/v6_08_06g/Linux64bit+2.6-2.12-e14-nu-prof/etc/../include/TEfficiency.h:81:7: note: candidate constructor not viable: requires 4 arguments, but 2
      were provided
      TEfficiency(const char* name,const char* title,Int_t nbins,
      ^
/cvmfs/fermilab.opensciencegrid.org/products/larsoft/root/v6_08_06g/Linux64bit+2.6-2.12-e14-nu-prof/etc/../include/TEfficiency.h:83:7: note: candidate constructor not viable: requires 5 arguments, but 2
      were provided
      TEfficiency(const char* name,const char* title,Int_t nbins,Double_t xlow,
      ^
/cvmfs/fermilab.opensciencegrid.org/products/larsoft/root/v6_08_06g/Linux64bit+2.6-2.12-e14-nu-prof/etc/../include/TEfficiency.h:88:7: note: candidate constructor not viable: requires 6 arguments, but 2
      were provided
      TEfficiency(const char* name,const char* title,Int_t nbinsx,
      ^
/cvmfs/fermilab.opensciencegrid.org/products/larsoft/root/v6_08_06g/Linux64bit+2.6-2.12-e14-nu-prof/etc/../include/TEfficiency.h:85:7: note: candidate constructor not viable: requires 8 arguments, but 2
      were provided
      TEfficiency(const char* name,const char* title,Int_t nbinsx,
      ^
/cvmfs/fermilab.opensciencegrid.org/products/larsoft/root/v6_08_06g/Linux64bit+2.6-2.12-e14-nu-prof/etc/../include/TEfficiency.h:93:7: note: candidate constructor not viable: requires 8 arguments, but 2
      were provided
      TEfficiency(const char* name,const char* title,Int_t nbinsx,
      ^
/cvmfs/fermilab.opensciencegrid.org/products/larsoft/root/v6_08_06g/Linux64bit+2.6-2.12-e14-nu-prof/etc/../include/TEfficiency.h:90:7: note: candidate constructor not viable: requires 11 arguments, but
      2 were provided
      TEfficiency(const char* name,const char* title,Int_t nbinsx,

Anybody have a clue as to what it means by “no matching constructor for initialization of TEfficiency” ?

Thanks !

Try:

TH1D *h0 = new TH1D("h0", "h0", 30, 0., 900.);
TH1D *h1 = new TH1D("h1", "h1", 30, 0., 900.);
MCTracktree->Draw("muontracklength_det >> h0");
MCTracktree->Draw("muontracklength_det >> h1", "angle_smallest < 0.1");
TEfficiency *eff= new TEfficiency(*h1, *h0);
1 Like

Same error as before:

-bash-4.1$ root -l Gallery_output.root 
root [0] 
Attaching file Gallery_output.root as _file0...
(TFile *) 0x3f6f1e0
root [1] TH1D *h0 = new TH1D("h0", "h0", 30, 0., 900.);
root [2] TH1D *h1 = new TH1D("h1", "h1", 30, 0., 900.);
root [3] MCTracktree->Draw("muontracklength_det >> h0");
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1
root [4] MCTracktree->Draw("muontracklength_det >> h1", "angle_smallest < 0.1");
root [5] TEfficiency *eff= new TEfficiency(h1,h0);
ROOT_prompt_5:1:23: error: no matching constructor for initialization of 'TEfficiency'
TEfficiency *eff= new TEfficiency(h1,h0);
                      ^           ~~~~~
/cvmfs/fermilab.opensciencegrid.org/products/larsoft/root/v6_08_06g/Linux64bit+2.6-2.12-e14-nu-prof/etc/../include/TEfficiency.h:80:7: note: candidate constructor not viable: no known conversion from
      'TH1D *' to 'const TH1' for 1st argument; dereference the argument with *
      TEfficiency(const TH1& passed,const TH1& total);
      ^
/cvmfs/fermilab.opensciencegrid.org/products/larsoft/root/v6_08_06g/Linux64bit+2.6-2.12-e14-nu-prof/etc/../include/TEfficiency.h:96:7: note: candidate constructor not viable: requires single argument
      'heff', but 2 arguments were provided
      TEfficiency(const TEfficiency& heff);
      ^
/cvmfs/fermilab.opensciencegrid.org/products/larsoft/root/v6_08_06g/Linux64bit+2.6-2.12-e14-nu-prof/etc/../include/TEfficiency.h:79:7: note: candidate constructor not viable: requires 0 arguments, but 2
      were provided
      TEfficiency();
      ^
/cvmfs/fermilab.opensciencegrid.org/products/larsoft/root/v6_08_06g/Linux64bit+2.6-2.12-e14-nu-prof/etc/../include/TEfficiency.h:81:7: note: candidate constructor not viable: requires 4 arguments, but 2
      were provided
      TEfficiency(const char* name,const char* title,Int_t nbins,
      ^
/cvmfs/fermilab.opensciencegrid.org/products/larsoft/root/v6_08_06g/Linux64bit+2.6-2.12-e14-nu-prof/etc/../include/TEfficiency.h:83:7: note: candidate constructor not viable: requires 5 arguments, but 2
      were provided
      TEfficiency(const char* name,const char* title,Int_t nbins,Double_t xlow,
      ^
/cvmfs/fermilab.opensciencegrid.org/products/larsoft/root/v6_08_06g/Linux64bit+2.6-2.12-e14-nu-prof/etc/../include/TEfficiency.h:88:7: note: candidate constructor not viable: requires 6 arguments, but 2
      were provided
      TEfficiency(const char* name,const char* title,Int_t nbinsx,
      ^
/cvmfs/fermilab.opensciencegrid.org/products/larsoft/root/v6_08_06g/Linux64bit+2.6-2.12-e14-nu-prof/etc/../include/TEfficiency.h:85:7: note: candidate constructor not viable: requires 8 arguments, but 2
      were provided
      TEfficiency(const char* name,const char* title,Int_t nbinsx,
      ^
/cvmfs/fermilab.opensciencegrid.org/products/larsoft/root/v6_08_06g/Linux64bit+2.6-2.12-e14-nu-prof/etc/../include/TEfficiency.h:93:7: note: candidate constructor not viable: requires 8 arguments, but 2
      were provided
      TEfficiency(const char* name,const char* title,Int_t nbinsx,
      ^
/cvmfs/fermilab.opensciencegrid.org/products/larsoft/root/v6_08_06g/Linux64bit+2.6-2.12-e14-nu-prof/etc/../include/TEfficiency.h:90:7: note: candidate constructor not viable: requires 11 arguments, but
      2 were provided
      TEfficiency(const char* name,const char* title,Int_t nbinsx,
TEfficiency *eff= new TEfficiency(*h1, *h0);
1 Like

My apologies. It works now. Thanks !