Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Plot-SVTX_Efficiency.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Plot-SVTX_Efficiency.cpp
1 #include <iostream>
2 #include <cstdlib>
3 #include <memory>
4 #include <sstream>
5 #include <string>
6 #include <cstring>
7 #include <vector>
8 #include <unordered_map>
9 #include <cmath>
10 #include <utility>
11 
12 #include "TFile.h"
13 #include "TTree.h"
14 #include "TCanvas.h"
15 #include "TLegend.h"
16 #include "TROOT.h"
17 #include "TH1F.h"
18 #include "TColor.h"
19 #include "TImage.h"
20 #include "TApplication.h"
21 #include "TGraphErrors.h"
22 #include "/sphenix/user/gregtom3/SBU/research/macros/macros/sPHENIXStyle/sPhenixStyle.C"
23 
24 TTree *load_tree(const char *const file_name, const char *const tree_name);
25 const char *const svtx_file_path {"/sphenix/user/giorgian/muons/svtx.root"};
26 
27 double MOMENTUM_MARGIN {0.01};
28 
30 {
32  gROOT->SetBatch(true);
33 
34  TTree *const ntp_gtrack {load_tree(svtx_file_path, "ntp_gtrack")};
35  TTree *const ntp_track {load_tree(svtx_file_path, "ntp_track")};
36 
37  Long64_t ngtrack {ntp_gtrack->GetEntries()};
38  Long64_t ntrack {ntp_track->GetEntries()};
39 
40  TH1F *h_true_count {new TH1F("h_true_count", "Psuedorapidity count", 100, -5, 5)};
41  h_true_count->SetXTitle("#eta");
42  h_true_count->SetYTitle("Count");
43  h_true_count->SetLineColor(kBlue);
44  Float_t geta;
45  ntp_gtrack->SetBranchAddress("geta", &geta);
46  for (Long64_t i {0}; i < ngtrack; ++i) {
47  if (ntp_gtrack->LoadTree(i) < 0)
48  break;
49  ntp_gtrack->GetEntry(i);
50  h_true_count->Fill(geta);
51  }
52  TH1F *h_reco_count {new TH1F("h_reco_count", "Psuedorapidity count", 100, -5, 5)};
53  h_reco_count->SetXTitle("#eta");
54  h_reco_count->SetYTitle("Count");
55  h_reco_count->SetLineColor(kRed);
56  Float_t gpx, gpy, gpz, px, py, pz;
57  ntp_track->SetBranchAddress("geta", &geta);
58  ntp_track->SetBranchAddress("gpx", &gpx);
59  ntp_track->SetBranchAddress("gpy", &gpy);
60  ntp_track->SetBranchAddress("gpz", &gpz);
61  ntp_track->SetBranchAddress("px", &px);
62  ntp_track->SetBranchAddress("py", &py);
63  ntp_track->SetBranchAddress("pz", &pz);
64  for (Long64_t i {0}; i < ntrack; ++i) {
65  if (ntp_track->LoadTree(i) < 0)
66  break;
67 
68  ntp_track->GetEntry(i);
69  if (fabs((px - gpx) / gpx) < MOMENTUM_MARGIN
70  && fabs((py - gpy) / gpy) < MOMENTUM_MARGIN
71  && fabs((pz - gpz) / gpz) < MOMENTUM_MARGIN)
72  h_reco_count->Fill(geta);
73  }
74 
75  const Long64_t nbins {h_true_count->GetSize() - 2}; /* -2 for underflow and overflow */
76  Double_t *x {new Double_t[nbins]};
77  Double_t *y {new Double_t[nbins]};
78  Double_t *ey {new Double_t[nbins]};
79  Long64_t top {0};
80  for (Long64_t i {0}; i < nbins; ++i) {
81  if (h_true_count->GetBinContent(i + 1) != 0) {
82  const Double_t n {h_reco_count->GetBinContent(i + 1)};
83  const Double_t N {h_true_count->GetBinContent(i + 1)};
84 
85  x[top] = {h_true_count->GetBinCenter(i + 1)};
86  y[top] = {n / N};
87  ey[top++] = {sqrt(n / (N * N) + (n * n) / (N * N * N))};
88  }
89  }
90  TGraphErrors *gr {new TGraphErrors(top, x, y, nullptr, ey)};
91  gr->SetMarkerColor(kBlue);
92  gr->SetMarkerStyle(21);
93  gr->SetMarkerSize(0.5);
94  gr->GetXaxis()->SetTitle("#eta");
95  gr->GetYaxis()->SetTitle("Efficiency");
96 
97  TCanvas *count {new TCanvas("count", "SVTX Event Count",
98  gStyle->GetCanvasDefW(), gStyle->GetCanvasDefH())};
99  h_true_count->GetYaxis()->SetRangeUser(0, 2000);
100  h_true_count->Draw();
101  h_reco_count->Draw("SAME");
102  TLegend *l1 {new TLegend(0.825, .90, .95, 0.80, "Track")};
103  l1->SetTextSize(0.03);
104  l1->AddEntry(h_true_count, "True", "l");
105  l1->AddEntry(h_reco_count, "Reco", "l");
106  l1->Draw();
107  gPad->RedrawAxis();
108 
109  TCanvas *efficiency {new TCanvas("efficiency", "SVTX Efficiency",
110  gStyle->GetCanvasDefW(), gStyle->GetCanvasDefH())};
111  gr->GetYaxis()->SetRangeUser(-0.05, 1);
112  gr->Draw("ALP");
113  TLegend *l2 {new TLegend(0.825, 0.90, 0.95, 0.8, "Track")};
114  l2->SetTextSize(0.03);
115  l2->AddEntry(gr, "Efficiency", "l");
116  l2->Draw();
117  gPad->RedrawAxis();
118 
119  TImage *const img {TImage::Create()};
120  img->FromPad(count);
121  std::stringstream name;
122  name << "SVTX_Event_Count-" << "margin=" << MOMENTUM_MARGIN << ".png";
123  img->WriteImage(strdup(name.str().c_str()));
124 
125  name.str("");
126  img->FromPad(efficiency);
127  name << "SVTX_Efficiency-" << "margin=" << MOMENTUM_MARGIN << ".png";
128  img->WriteImage(strdup(name.str().c_str()));
129  gApplication->Terminate(0);
130 }
131 
132 TTree *load_tree(const char *const file_name, const char *const tree_name)
133 {
134  return (TTree *) (new TFile(file_name, "READ"))->Get(tree_name);
135 }
136 
137 int main(int argc, char *argv[])
138 {
139  TApplication app {"SVTX Efficiency Plots", &argc, argv};
140  if (argc > 1) {
141  std::stringstream tmp {argv[1]};
142  tmp >> MOMENTUM_MARGIN;
143  }
144 
146  app.Run();
147  return 0;
148 }