Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
loc_lib.h
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file loc_lib.h
1 #ifndef LOC_LIB_H
2 #define LOC_LIB_H
3 
4 // local library for jet unfolding
5 
6 #include "noiBinVec.h"
7 #include "pAu_bins.h"
8 #include "noi_fnc.h"
9 
10 
11 TH1D* norm_binwidths(TH1D* hg) {
12  auto ax = hg->GetXaxis();
13  for (int i=1;i<=ax->GetNbins();++i){
14  double _val = hg->GetBinContent(i);
15  double _err = hg->GetBinError(i);
16  double weight = 1./ax->GetBinWidth(i);
17  hg->SetBinContent(i,_val*weight);
18  hg->SetBinError (i,_err*weight);
19  }
20  return hg;
21 }
22 
23  float calc_dphi(float a, float b) {
24  float _ = a-b;
25  while (_ <-M_PI) _ += 2*M_PI;
26  while (_ > M_PI) _ -= 2*M_PI;
27  return _;
28  }
29 
30 TH1D* rebin_TH1D(TH1D* hg_in, vector<int> i_bins, string _name="", bool delete_hg_in=true, bool norm_by_cnt=true, bool norm_by_width = true) {
31  // input: bins numbers for left-hand most bin in each new bin. Example: 1, 2, 3, 4, 20, would have 5 bins, with bin 1-1, 2-2, 3-3, 4-20 in each of the new bins
32  double cnt = hg_in->Integral();
33  string name = (_name=="") ? noiUniqueName() : _name;
34  auto ax = hg_in->GetXaxis();
35  vector<double> edges;
36  int nbins = i_bins.size()-1;
37 
38  if (nbins<1) return hg_in;
39  for (int i=0; i<=nbins; ++i) {
40  edges.push_back(ax->GetBinLowEdge(i_bins[i]));
41  }
42  tuBinVec new_bins { edges };
43  auto hg_out = new TH1D(name.c_str(), Form("%s;%s;%s",hg_in->GetTitle(),
44  hg_in->GetXaxis()->GetTitle(), hg_in->GetYaxis()->GetTitle()),
45  new_bins, new_bins);
46  for (int i=0; i<nbins; ++i) {
47  int i0 = i_bins[i];
48  int i1 = i_bins[i+1];
49  double sum=0;
50  for (int k=i0;k<i1;++k) {
51  sum += hg_in->GetBinContent(k);
52  }
53  hg_out->SetBinContent(i+1, sum);
54  hg_out->SetBinError(i+1, pow(sum,0.5));
55  }
56  if (norm_by_width) {
57  for (int i=1; i<=nbins; ++i) {
58  auto c = hg_out->GetBinContent(i);
59  auto e = hg_out->GetBinError (i);
60  auto w = hg_out->GetXaxis()->GetBinWidth(i);
61  hg_out->SetBinContent(i, c/w);
62  hg_out->SetBinError (i, e/w);
63  }
64  }
65  if (norm_by_cnt) {
66  hg_out->Scale(1./cnt);
67  }
68  if (delete_hg_in) delete hg_in;
69  return hg_out;
70 }
71 
72 void div_by_W (TH1D* hg, TH1D* w) {
73  for (int i=1; i<=hg->GetNbinsX(); ++i) {
74  double W = w->GetBinContent(i);
75  if (W==0) {
76  hg->SetBinContent(i, 0.);
77  hg->SetBinError (i, 0.);
78  } else {
79  hg->SetBinContent(i, hg->GetBinContent(i)/W);
80  hg->SetBinError (i, hg->GetBinError (i)/W);
81  }
82  }
83 }
84 
85 void set_range(int axis, int bin0, int bin1, vector<THnSparseD*> vec_sparse, bool print=false) {
86  bool first = true;
87  for (auto& sparse : vec_sparse) {
88  TAxis* ax = sparse->GetAxis(axis);
89  sparse->GetAxis(axis)->SetRange(bin0, bin1);
90  if (first && print) {
91  first = false;
92  cout << Form("Set range(%2i) to %7.2f-%7.2f",axis,ax->GetBinLowEdge(bin0),ax->GetBinUpEdge(bin1)) << endl;
93  }
94  }
95 }
96 
97 pair<double,double> sparse_integral(THnSparseD* sp) {
98  auto hg = sp->Projection(0);
99  double _val, _err;
100  _val = hg->IntegralAndError(1, hg->GetNbinsX(), _err);
101  delete hg;
102  return {_val,_err};
103 }
104 
105 TH1D* sparse_proj(THnSparseD* sp, int i_ax, string name="", bool norm_bin_width=false) {
106  auto hg = (TH1D*) sp->Projection(i_ax);
107  if (name != "") hg->SetName(name.c_str());
108  else hg->SetName(noiUniqueName());
109  if (norm_bin_width) norm_binwidths(hg);
110  return hg;
111 }
112 
113 void set_track_cuts(vector<THnSparseD*> data) {
114  set_range(_dca, 1, 5, data);
115  set_range(_nhitfit, 20, 48, data);
116  set_range(_nhitrat, 6, 10, data);
117 }
118 
119 TH1D* emb_eff_per_zdcx_binnedby3(THnSparseD* reco, THnSparseD* truth, string name) {
120  array<int,5> i0_zdcx { 5, 8, 11, 14, 17 }; // binning by 3 for zdcx
121  array<int,5> i1_zdcx { 7, 10, 13, 16, 19 };
122 
123  tuBinVec bins_5by3to20 {{ 5000., 5000., 5, 20000 }};
124  //
125  /* array<int,7> i0_zdcx { 4, 7, 10, 13, 16, 19, 22 }; */
126  /* array<int,7> i1_zdcx { 4, 7, 10, 13, 16, 19, 22 }; */
127 
128  /* tuBinVec bins_5by3to20 {{ 5000., 5000., 5, 20000 }}; */
129  TH1D* hg_out = new TH1D(name.c_str(), ";ZDCx;Tracking Efficiency", bins_5by3to20, bins_5by3to20 );
130 
131  for (int i=0; i<5; ++i) {
132  set_range(_zdcx, i0_zdcx[i], i1_zdcx[i], {reco, truth});
133  auto h_reco = (TH1D*) reco->Projection(_pt); h_reco ->SetName(noiUniqueName());
134  auto h_truth = (TH1D*) truth->Projection(_pt); h_truth->SetName(noiUniqueName());
135  double _val, _err;
136  _val = h_reco->IntegralAndError(3,h_reco->GetNbinsX(),_err);
137  cout << " i("<<i<<") val("<<_val<<" +/- "<<_err<<") ----> " << _err/_val << endl;
138  double norm = h_truth->Integral(3,h_truth->GetNbinsX());
139  delete h_reco;
140  delete h_truth;
141  if (norm == 0) continue;
142  _val /= norm;
143  _err /= norm;
144  hg_out->SetBinContent(i+1,_val);
145  hg_out->SetBinError (i+1,_err);
146  }
147  return hg_out;
148 }
149 
150 TH1D* hg_abs_max(vector<TH1D*> vdat) {
151  if (vdat.size()==0) return nullptr;
152  auto hg_new = (TH1D*) vdat[0]->Clone(noiUniqueName());
153  for (auto bin=1; bin<=hg_new->GetNbinsX(); ++bin) {
154  double val0 = abs(hg_new->GetBinContent(bin));
155  for (auto ihg=0; ihg<vdat.size(); ++ihg) {
156  double val1 = abs(vdat[ihg]->GetBinContent(bin));
157  if (val1>val0) val0 = val1;
158  }
159  hg_new->SetBinContent(bin,val0);
160  hg_new->SetBinError(bin,0);
161  }
162  return hg_new;
163 }
164 
165 TH1D* hg_abs_deltarat(TH1D* nom, TH1D* comp) {
166  auto hg_new = (TH1D*) nom->Clone(noiUniqueName());
167  hg_new->Reset();
168  for (int i=1; i<=nom->GetNbinsX(); ++i) {
169  if (nom->GetBinContent(i) != 0) {
170  auto v_nom = nom->GetBinContent(i);
171  auto v_comp = comp->GetBinContent(i);
172  hg_new->SetBinContent(i, TMath::Abs(v_comp-v_nom)/v_nom);
173  }
174  }
175  return hg_new;
176 }
177 
178 TH1D* hg_abs_delta(TH1D* nom, TH1D* comp) {
179  auto hg_new = (TH1D*) nom->Clone(noiUniqueName());
180  hg_new->Reset();
181  for (int i=1; i<=nom->GetNbinsX(); ++i) {
182  /* if (nom->GetBinContent(i) != 0) { */
183  auto v_nom = nom->GetBinContent(i);
184  auto v_comp = comp->GetBinContent(i);
185  hg_new->SetBinContent(i, TMath::Abs(v_comp-v_nom));
186  /* } */
187  }
188  return hg_new;
189 }
190 
191 TH1D* hg_const(TH1D* nom, double val, string name) {
192  auto hg_new = (TH1D*) nom->Clone(name.c_str());
193  hg_new->Reset();
194  for (int i=1; i<=hg_new->GetNbinsX(); ++i) {
195  hg_new->SetBinContent(i, val);
196  }
197  return hg_new;
198 }
199 
200 TH1D* hg_RSS(vector<TH1D*> vec, string name) {
201  auto hg_new = (TH1D*) vec[0]->Clone(name.c_str());
202  hg_new->Reset();
203  for (auto& hg : vec) {
204  for (int i=1; i<=hg_new->GetNbinsX(); ++i) {
205  hg_new->SetBinContent(i, hg_new->GetBinContent(i) + TMath::Sq(hg->GetBinContent(i)));
206  }
207  }
208  for (int i=1; i<=hg_new->GetNbinsX(); ++i) {
209  hg_new->SetBinContent(i, TMath::Sqrt(hg_new->GetBinContent(i)));
210  }
211  return hg_new;
212 }
213 
214 TH1D* hg_syserr(TH1D* hg_nom, TH1D* hg_rss, string name) {
215  auto hg_new = (TH1D*) hg_nom->Clone(name.c_str());
216  for (int i=1; i<=hg_new->GetNbinsX(); ++i) {
217  hg_new->SetBinError(i, hg_nom->GetBinContent(i) * hg_rss->GetBinContent(i));
218  /* hg_new->SetBinError(i, .4); */
219  /* cout << "i " << i << " -> " << hg_new->GetBinContent(i) << " " << hg_new->GetBinError(i) << endl; */
220  }
221  return hg_new;
222 }
223 
224 /* int noi_geant05(int geantid) { */
225 /* switch (geantid) { */
226 /* case 8: return 0; */
227 /* case 9: return 1; */
228 /* case 11: return 2; */
229 /* case 12: return 3; */
230 /* case 14: return 4; */
231 /* case 15: return 5; */
232 /* } */
233 /* return -1; */
234 /* }; */
235 
236 
237 
238 // make tuBinVec avialable locally
239 
240 #endif