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