StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
plCalcZeffic.C
1 
2 void plCalcZeffic(char* iPath="/star/u/stevens4/wAnalysis/efficXsec/outEmb/gainUp2/"){
3 
4  system("mkdir -p plots/");
5 
6  char* core0;
7  core0="Ze+e-Interf";
8 
9  //gStyle->SetOptFit(1);
10  //gStyle->SetOptStat(0);
11  gStyle->SetPalette(1);
12 
13  //load file
14  TString fullInpName=iPath; fullInpName+=core0;
15  fullInpName+=".wana.hist.root";
16  fd=new TFile(fullInpName);
17  if(!fd->IsOpen()) {
18  printf("ERROR: input histo file not found, quit\n",fullInpName.Data());
19  return;
20  } else {
21  printf("Opened: %s\n",fullInpName.Data());
22  }
23 
24  //use rebin 1 for effic # calc, but rebin N for plot
25  int massRebin=4;
26  int zdcRebin=5;
27 
28  //get total efficiency and error
29  float massPlot[5]={1.0,1.0,1.0,1.0,1.0};
30  float massPlotErr[5]={1.0,1.0,1.0,1.0,1.0};
31 
32  //trigger efficiency
33  TH1F *hTrigMass=doEfficiency(fd,"MCzMassAll","MCzMassTrig",Form("Z trigger efficiency"),massRebin,massPlot[0],massPlotErr[0]);
34  TH1F *hTrigZDC=doEfficiency(fd,"MCzZdcAll","MCzZdcTrig",Form("Z trigger efficiency"),zdcRebin,0);
35  //vertex efficiency
36  TH1F *hVertMass=doEfficiency(fd,"MCzMassTrig","MCzMassVert",Form("Z vertex efficiency"),massRebin,massPlot[1],massPlotErr[1]);
37  TH1F *hVertZDC=doEfficiency(fd,"MCzZdcTrig","MCzZdcVert",Form("Z vertex efficiency"),zdcRebin,0);
38  //tracking efficiency
39  TH1F *hTrackMass=doEfficiency(fd,"MCzMassVert","MCzMassTrack",Form("Z tracking efficiency"),massRebin,massPlot[2],massPlotErr[2]);
40  TH1F *hTrackZDC=doEfficiency(fd,"MCzZdcVert","MCzZdcTrack",Form("Z tracking efficiency"),zdcRebin,0);
41  //algo efficiency
42  TH1F *hRecoMass=doEfficiency(fd,"MCzMassTrack","MCzMassReco",Form("Z algo efficiency"),massRebin,massPlot[3],massPlotErr[3]);
43  TH1F *hRecoZDC=doEfficiency(fd,"MCzZdcTrack","MCzZdcReco",Form("Z algo efficiency"),zdcRebin,0);
44 
45  //total efficiency for overall stat uncertainty
46  TH1F *hTotMass=doEfficiency(fd,"MCzMassAll","MCzMassReco",Form("Z total efficiency"),massRebin,massPlot[4],massPlotErr[4]);
47  cout<<"Total Efficiency = "<<massPlot[4]<<" $\\pm$ "<<massPlotErr[4]<<endl;
48  TH1F *hTotZDC=doEfficiency(fd,"MCzZdcAll","MCzZdcReco",Form("Z total efficiency"),zdcRebin,0);
49  hTotZDC->Draw();
50 
51  //*************** Vertex+Trigger Efficiency plots ********//
52  cV=new TCanvas(Form("Z vertex and trig effic"),"vertex and effic",800,600);
53  cV->Divide(2,2);
54  cV->cd(3);
55  hVertMass->Draw();
56  cV->cd(4);
57  hVertZDC->Draw();
58  cV->cd(1);
59  hTrigMass->Draw();
60  cV->cd(2);
61  hTrigZDC->Draw();
62  cV->Print("plots/ZVert-TrigEffic.eps");
63  cV->Print("plots/ZVert-TrigEffic.png");
64 
65  //*************** Algo+Track Efficiency plots *************//
66  cA=new TCanvas(Form("Z algo and track effic"),"algo and track effic",800,600);
67  cA->Divide(2,2);
68  cA->cd(3);
69  hRecoMass->Draw();
70  cA->cd(4);
71  hRecoZDC->Draw();
72  cA->cd(1);
73  hTrackMass->Draw();
74  cA->cd(2);
75  hTrackZDC->Draw();
76  cA->Print("plots/ZAlgo-TrkEffic.eps");
77  cA->Print("plots/ZAlgo-TrkEffic.png");
78 
79  return;
80 }
81 
82 //calculate efficiencies
83 TH1F* doEfficiency(TFile *fd,char* name0, char* name1, char *tit, int reb=1, float &etplot, float &etplotErr=0){
84 
85  TH1F * h0=(TH1F * )fd->Get(name0); assert(h0);
86  TH1F * h1=(TH1F * )fd->Get(name1); assert(h1);
87 
88  ha=(TH1F*) h0->Clone(); assert(ha);
89  hb=(TH1F*) h1->Clone(); assert(hb);
90 
91  ha->Rebin(reb);
92  hb->Rebin(reb);
93 
94  hc=(TH1F*) hb->Clone(); assert(hc);
95  hc->SetTitle(tit);
96  hc->Reset();
97 
98  float num=0; float den=0;
99  float matchErr2=0; float thrownErr2=0;
100  int nb=hb->GetNbinsX();
101  int startBin=hb->GetXaxis()->FindBin(70.);
102  int endBin=hb->GetXaxis()->FindBin(109.9);
103  //cout<<startBin<<endl;
104  //cout<<hb->GetNbinsX()<<" "<<ha->GetNbinsX()<<endl;
105  assert(nb==ha->GetNbinsX());
106  for(int i=1; i<=nb; i++) {
107  float n0=ha->GetBinContent(i);//n0: # of thrown tracks
108  float n1=hb->GetBinContent(i);//n1: # of matched tracks
109  float e0=ha->GetBinError(i);//e0: error on # of thrown tracks
110  float e1=hb->GetBinError(i);//e1: error on # of matched tracks
111  if(n0==0) continue; // no thown tracks found
112  float eff=(float) n1/n0;
113 
114  //old method with un-weighted histos
115  //float x=n1*(n0-n1);
116  //if(n1==0 || n1==n0) x=n0;
117  //float errEff=sqrt(x/n0/n0/n0);
118 
119  float errEff=0;
120  //method for using weighted histos (must use Sumw2)
121  if(n1==n0) {//problem in effic=1 limit, statErr=0
122  //cout<<name1<<" bin="<<i<<" problem with error"<<endl;
123  errEff=1./n0; //just something big to see its low stats
124  }
125  else errEff=sqrt(e1*e1*(n0-n1)*(n0-n1)+(e0-e1)*(e0-e1)*n1*n1)/n0/n0;
126 
127  //printf("name=%s: bin#= %d, n0= %f, n1= %f, e0=%f, e1=%f, eff= %f, errEff=+/- %f\n",name0,i,n0,n1,e0,e1,eff,errEff);
128  hc->SetBinContent(i,eff);
129  hc->SetBinError(i,errEff);
130  if(i>=startBin && i<=endBin){//define mass range for efficTot
131  num+=n1;
132  den+=n0;
133  matchErr2+=e1*e1;
134  thrownErr2+=e0*e0;
135  }
136 
137  }
138 
139  if(etplot==1.0){
140  //old method with un-weighted histos
141  //cout<<name1<<"="<<(float) num/den<<"+/-"<<sqrt((float)num*(den-num)/den/den/den)<<endl;
142 
143  //method for using weighted histos (must use Sumw2)
144  cout<<name1<<"="<< num/den <<" $\\pm$ "<< sqrt(matchErr2*(den-num)*(den-num) + (thrownErr2-matchErr2)*num*num)/den/den <<endl;
145  etplot=num/den;
146  etplotErr=sqrt(matchErr2*(den-num)*(den-num) + (thrownErr2-matchErr2)*num*num)/den/den;
147  }
148  hc->SetMinimum(0.0);
149  hc->SetMaximum(1.2);
150 
151  return hc;
152 }