Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
zdc_calib.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file zdc_calib.C
1 //get zdc weigthts per side for 3 modules
2 //updated for sPHENIX ZDC
3 //Ejiro Umaka, Peter Steinberg
4 
5 
6 #define Nmodules 3
7 float E_n;
8 
9 float no_booster_weight[3] = {1,1,1};
10 std::vector<float> skip_zdc_mod = {1,1,1};
11 
12 //change per distribution
13 float uncorr_lo = 133.138;
14 float uncorr_hi = 224.234;
15 
16 
18  std::vector< std::vector<float> >& in_data,
19  TVectorD& out_weights,
20  TMatrixD& out_matrix
21  )
22 {
23 
24  int n_zdc = in_data.size();
25  int dim = n_zdc+1;
26 
27  std::cout << "Calculate weights for " << n_zdc << " modules!" << std::endl;
28 
29  TVectorD mom1(dim);
30 
31  size_t nevt = in_data.at(0).size();
32  for (size_t i = 0;i<nevt;i++)
33  {
34  for (size_t ir=0;ir<n_zdc;ir++)
35  {
36  mom1(ir) += in_data.at(ir).at(i)/double(nevt);
37  for (size_t ic=0;ic<n_zdc;ic++)
38  {
39  out_matrix(ir,ic) += 2*in_data.at(ir).at(i)*in_data.at(ic).at(i)/double(nevt);
40  }
41  }
42  }
43 
44 
45  for (size_t ir=0;ir<n_zdc;ir++)
46  {
47  out_matrix(ir,n_zdc)=mom1(ir);
48  out_matrix(n_zdc,ir)=mom1(ir);
49  }
50 
51  TVectorD solution(dim);
52  solution(n_zdc) = energy;
53 
54  TDecompLU lu(out_matrix);
55  lu.Decompose();
56  bool status = lu.Solve(solution);
57 
58  if (!status)
59  {
60  std::cout << "Fail!" << endl;
61  }
62  else
63  {
64  std::cout << "Success!" << endl;
65  }
66 
67  solution.Print();
68 
69  out_weights = solution;
70 
71  return status;
72 }
73 
74 
75 //put input file and set constraint energy to 100 GeV
76 void zdc_calib(TString filename = "", float e = 100)
77 
78 {
79 
80  TString s_zdc = "";
81  for (int iz=0;iz<Nmodules;iz++)
82  {
83  s_zdc += TString::Itoa(skip_zdc_mod[iz],10);
84  }
85 
86  TFile fout("output"+filename+"_"+s_zdc+"_fout.root","RECREATE");
87  TH1F h_e_uncorr("h_e_uncorr","Uncorr. energy;E_{uncorr}",3000,0,6000);
88  TH2F h_e_uncorr2D("h_e_uncorr2D","Uncorr. energy;E_{ZDC} [GeV]};E_{EM} [GeV];",100,0,300,100,0,300);
89  TH1F h_e_corr("h_e_corr","corr. energy;E_{corr}",3000,0,6000);
90  TH2F h_e_corr2D("h_e_corr2D","corr. energy;E_{ZDC} [GeV];E_{EM} [GeV]",100,0,600,100,0,600);
91 
92  int n_zdc = std::accumulate(skip_zdc_mod.begin(),skip_zdc_mod.end(),0);
93  std::cout << "Working with " << n_zdc << " modules!" << std::endl;
94 
95  TMatrixD* zdcMatrix = new TMatrixD(n_zdc+1,n_zdc+1);
96  TVectorD* zdcWeights = new TVectorD(n_zdc+1);
97 
98 
99  std::vector< std::vector<float> > data;
100  data.resize(n_zdc);
101  std::vector< std::vector<float> > all_data;
102  all_data.resize(n_zdc);
103 
104  ifstream ifs(filename);
105 
106  int event;
107  float x,y;
108  std::vector<float> z(Nmodules);
109  int Nevents;
110 
111 
112 
113 float thissum = 0.;
114  while (!ifs.eof())
115  {
116 
117  ifs >> z[0] >> z[1] >> z[2];
118 
119  float sum = 0;
120  float sum_had = 0;
121  bool fail_event = false;
122 
123  for (int iz=0;iz<Nmodules;iz++)
124  {
125  z[iz] = (z[iz]<4000?z[iz]:0)/no_booster_weight[iz];
126 
127  if (!skip_zdc_mod[iz] && z[iz]>10) fail_event = true;
128 
129  sum += z[iz]*skip_zdc_mod[iz];
130  if (iz>0) sum_had += z[iz]*skip_zdc_mod[iz];
131  }
132  if (fail_event) continue;
133 
134  int ind = 0;
135  if (sum>uncorr_lo&&sum<uncorr_hi)
136  {
137  for (int iz=0;iz<Nmodules;iz++)
138  {
139  if (skip_zdc_mod[iz])
140  {
141  data.at(ind).push_back(z[iz]);
142  ind++;
143  }
144  }
145  }
146 
147  ind = 0;
148  for (int iz=0;iz<Nmodules;iz++)
149  {
150  if (skip_zdc_mod[iz])
151  {
152  all_data.at(ind).push_back(z[iz]);
153  ind++;
154  }
155  }
156 
157  h_e_uncorr.Fill(sum);
158  h_e_uncorr2D.Fill(sum_had,sum-sum_had);
159  }
160 
161  calculateWeights(e, data, *zdcWeights, *zdcMatrix);
162 
163  int Nevt = all_data.at(0).size();
164  std::cout << Nevt << " events used!" << std::endl;
165  for (int i = 0;i<Nevt;i++)
166  {
167 
168  float e_corr = 0;
169  float e_corr_had = 0;
170  int ind = 0;
171  for (int iz=0;iz<Nmodules;iz++)
172  {
173  if (skip_zdc_mod[iz])
174  {
175  e_corr += all_data.at(ind).at(i) * (*zdcWeights)(ind);
176  if (iz>0) e_corr_had += all_data.at(ind).at(i) * (*zdcWeights)(ind);
177  ind++;
178  }
179  }
180 
181  h_e_corr.Fill(e_corr);
182  h_e_corr2D.Fill(e_corr_had,e_corr-e_corr_had);
183 
184  }
185 
186  fout.cd();
187  zdcWeights->Write("zdcTBWeights");
188  fout.Write();
189  fout.Close();
190 
191 }
192