Belle II Software  release-05-01-25
eclcovmat_calc.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2011 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Program for calculation and inversion cavariance matrices *
7  * *
8  * Contributors: Alexander Bobrov (a.v.bobrov@inp.nsk.su) , *
9  * Guglielmo De Nardo *
10  * *
11  * This software is provided "as is" without any warranty. *
12  **************************************************************************/
13 
14 #include <TChain.h>
15 #include <TF1.h>
16 #include <TMath.h>
17 #include <vector>
18 #include <iostream>
19 #include <iomanip>
20 #include <cassert>
21 #include <framework/utilities/FileSystem.h>
22 #include <ecl/digitization/WrapArray2D.h>
23 #include <Eigen/Dense>
24 
25 using namespace std;
26 using namespace Belle2;
27 using namespace ECL;
28 using namespace Eigen;
29 
30 void matrix_cal(int cortyp, const char* inputRootFilename,
31  const char* corrDirSuffix, const char* cutFilename)
32 {
33 
34  TChain fChain("m_tree");
35  Int_t poq;
36 
37  cout << "file for calibration:" << inputRootFilename << endl;
38  fChain.Add(inputRootFilename);
39 
40  Int_t nhits;
41  WrapArray2D<Int_t> hitA(8736, 31);
42  TBranch* b_nhits;
43  TBranch* b_hitA;
44 
45  fChain.SetBranchAddress("nhits", &nhits, &b_nhits);
46  fChain.SetBranchAddress("hitA", hitA, &b_hitA);
47 
48 
49  // not used commented to silence warning
50  // double SI[16][16];
51  // double IS[16][16];
52 
53  double IM[16][16];
54  double UU[16][16];
55  double FF[16][16];
56 
57 
58 
59  constexpr int mapmax = 217;
60 
61  //Can't be std::array because those are constructed on the stack and then there's a compiler warning about a stack overflow
62  std::vector <
63  std::vector <
64  std::vector<double >>> W(mapmax, std::vector<std::vector<double>>(16, std::vector<double>(1, 0.0)));
65 
66  std::vector <
67  std::vector <
68  std::vector<double >>> WW(mapmax, std::vector<std::vector<double>>(16, std::vector<double>(16, 0.0)));
69 
70  WrapArray2D<double> Q(mapmax, 16);
71  vector<double> Mean(8736, 0);
72 
73 
74  Eigen::MatrixXd m(16, 16);
75  Eigen::MatrixXd u(16, 16);
76  Eigen::MatrixXd y(16, 16);
77 
78  // not used commented to silence warning
79  // double rms(8736,0);
80 
81  vector<double> sr(8736, 0);
82  vector<double> sl(8736, 0);
83 
84  vector<double> dt(mapmax, 0);
85  vector<double> dtn(mapmax, 0);
86 
87 
88  vector<double> inmt(mapmax, 0);
89  int it;
90  double A0;
91  // cortyp = 43;
92  double Nsigcut;
93  Nsigcut = 3.5;
94  double delta = 0.;
95  int index;
96  index = 0;
97  int icn, id;
98 
99 
100  vector<int> DS(8736, 0);
101 
102  string crystalGroupsFilename = FileSystem::findFile("/data/ecl/CIdToEclData.txt");
103  assert(! crystalGroupsFilename.empty());
104  ifstream ctoecldatafile(crystalGroupsFilename);
105 
106  int cid, group;
107  bool readerr = false;
108  //vector<int> grmap[217];
109  while (! ctoecldatafile.eof()) {
110  ctoecldatafile >> cid >> group;
111 
112  if (ctoecldatafile.eof()) break;
113  if (group <= -1 || group >= 217 || cid <= -1 || cid >= 8736) {
114  cout << "Error cid=" << cid << " group=" << group << endl;
115  readerr = true;
116  break;
117  }
118  DS[cid] = group;
119  }
120  assert(!readerr);
121  int ia, ib;
122 
123  int max = 8736;
124  int j;
125  int trun;
126 
127  double Y[16];
128 
129  // red mean and rms for channels
130 // sprintf(cutin,"/home/belle/avbobrov/ecl/covmat/chdatu005.txt");
131  ifstream cutFile(cutFilename);
132 
133  int jk;
134  double mn;
135  double rs;
136  double el;
137  double er;
138  double ch2;
139 
140  while (true) {
141  cutFile >> jk >> mn >> rs >> el >> er >> ch2;
142  if (cutFile.eof()) break;
143  Mean[jk] = mn;
144  // not used: commented to silence warnings
145  // rms[jk] = rs;
146  sr[jk] = er;
147  sl[jk] = el;
148  }
149  cutFile.close();
150 
151  for (icn = 0; icn < mapmax; icn++) {
152  inmt[icn] = 0.;
153  dt[icn] = 0.;
154  dtn[icn] = 0.;
155  for (it = 0; it < 16; it++) {
156  W[icn][it][0] = 0.;
157  Q[icn][it] = 0.;
158 
159  for (id = 0; id < 16; id++) {
160  WW[icn][it][id] = 0.;
161 
162  }
163  }
164  }
165 
166 
167  Int_t nevent = fChain.GetEntries();
168  std::cout << "! nevent=" << nevent << std::endl;
169 
170  for (Int_t i = 0; i < nevent; i++) {
171  fChain.GetEntry(i);
172  if (i % 100 == 0) { cout << " nevent=" << i << endl; }
173  for (icn = 0; icn < max; icn++) { //%%%%%%%%%%%%%%%%%%%%%%%%55555555555
174  index = 1;
175  for (id = 0; id < 16; id++) {
176  Y[id] = 0.; // was it, fixed 20151126.
177  }
178 
179  A0 = 0;
180  for (j = 0; j < 31; j++) {
181  if (j < 16) {
182  A0 = A0 + (double)hitA[icn][j] / 16.;
183  if ((double)hitA[icn][j] - Mean[icn] < -Nsigcut * sl[icn]) {index = 0;}
184  if ((double)hitA[icn][j] - Mean[icn] > Nsigcut * sr[icn]) {index = 0;}
185  } else {
186  if ((double)hitA[icn][j] - Mean[icn] > Nsigcut * sr[icn] || (double)hitA[icn][j] - Mean[icn] < -Nsigcut * sl[icn]) {index = 0;}
187  }
188  } // points cicle for selection events
189 
190  // selection not applied because seems to cut all events
191  // to be checked
192 
193  if (index == 1) {
194  inmt[DS[icn]] = inmt[DS[icn]] + 1.;
195  A0 = 0.;
196 
197  for (j = 0; j < 31; j++) {
198 
199  if (j < 16) {A0 = A0 + (double)hitA[icn][j] / 16.;}
200 
201  if (j == 15) {
202  W[DS[icn]][0][0] = W[DS[icn]][0][0] + A0;
203  Q[DS[icn]][0] = Q[DS[icn]][0] + A0;
204  Y[0] = A0;
205 
206  }
207 
208  if (j > 15) {
209  trun = j - 15;
210  Y[trun] = (double)hitA[icn][j];
211  W[DS[icn]][trun][0] = W[DS[icn]][trun][0] + (double)hitA[icn][j];
212  Q[DS[icn]][trun] = Q[DS[icn]][trun] + (double)hitA[icn][j];
213 
214  }
215 
216  } // points cicle for writing
217 
218 
219 
220  for (it = 0; it < 16; it++) {
221  for (id = 0; id < 16; id++) {
222  WW[DS[icn]][it][id] = WW[DS[icn]][it][id] + Y[it] * Y[id];
223  }
224  }
225 
226  } // index
227 
228  } //channels cilce
229 
230  } // events cicle
231 
232 
233 
234 
235 
236  // MATRICES INVERTION
237 
238 
239  for (icn = 0; icn < mapmax; icn++) { //%%%%%%%%%%%%%%%%%%%%%%%%55555555555
240 
241 
242  {
243  // conventional comment
244  if (icn % 10 == 0) {
245  cout << "icn= " << icn << " imnt= " << inmt[icn] << endl;
246  // printf("icn=%d inmt=%lf \n ", icn, inmt[icn]);
247  }
248 
249  if (inmt[icn] == 0) {
250  cout << "bad icn= " << icn << endl;
251  // printf("bad icn=%d\n", icn);
252  }
253 
254  // WRITE DATA INTO ARRAY
255  for (ia = 0; ia < 16; ia++) {
256  for (ib = 0; ib < 16; ib++) {
257  if (ib < 16 && ia < 16) {
258  m(ia, ib) = (WW[icn][ia][ib] - W[icn][ia][0] * W[icn][ib][0] / inmt[icn]) / inmt[icn];
259  UU[ia][ib] = (WW[icn][ia][ib] - W[icn][ia][0] * W[icn][ib][0] / inmt[icn]) / inmt[icn];
260  // not used commented to silence warning
261  // SI[ia][ib] = (WW[icn][ia][ib] - W[icn][ia][0] * W[icn][ib][0] / inmt[icn]) / inmt[icn];
262 
263 
264 
265  }
266 
267  }
268  // not used commented to silence warning
269  // IS[ia][ib] = 0;
270 
271  }
272 
273 
274  // INVERSION
275 
276 
277  u = m.inverse();
278 
279 
280 // u = y*y;
281  int ic;
282 
283  // CALCULATION RESIDUAL
284  dt[icn] = 0.;
285  for (ia = 0; ia < 16; ia++) {
286  for (ib = 0; ib < 16; ib++) {
287  IM[ia][ib] = u(ia, ib);
288  FF[ia][ib] = 0.;
289  for (ic = 0; ic < 16; ic++) {
290  FF[ia][ib] = FF[ia][ib] + UU[ia][ic] * u(ic, ib);
291  }
292  if (ia == ib && fabs(FF[ia][ib] - 1.) > delta) {delta = fabs(FF[ia][ib] - 1.);}
293  if (ia != ib && fabs(FF[ia][ib]) > delta) {delta = fabs(FF[ia][ib]);}
294 
295  if (ia == ib && fabs(FF[ia][ib] - 1.) > dt[icn]) {dt[icn] = fabs(FF[ia][ib] - 1.);}
296  if (ia != ib && fabs(FF[ia][ib]) > dt[icn]) {dt[icn] = fabs(FF[ia][ib]);}
297  }
298  }
299  if (dt[icn] < 1.e-33 || dt[icn] > 1.e-9) {
300  cout << "icn=" << icn << " inmt=" << inmt[icn] << " cut=??" << "delta=" << dt[icn] << endl;
301  // printf("icn=%d inmt=%lf cut=%lf delta= %e \n ", icn, inmt[icn], dt[icn]);
302  }
303 
304 
305  } // largest conventional comment, but what is this for???
306 
307 
308 
309  // WRITE INVERS MATRICES
310 
311 
312  {
313  // conventional comment
314 
315  string mcorFilename(corrDirSuffix);
316  mcorFilename += to_string(cortyp) + "/mcor" + to_string(icn) + "_L.dat";
317  // sprintf(Min, "/hsm/belle/bdata2/users/avbobrov/belle2/corr%d/mcor%d_L.dat", cortyp, icn);
318  ofstream mcorFile(mcorFilename);
319 
320  for (poq = 0; poq < 16; poq++) {
321  mcorFile << setprecision(5)
322  << m(0, poq) << "\t" << m(1, poq) << "\t" << m(2, poq) << "\t"
323  << m(3, poq) << "\t" << m(4, poq) << "\t" << m(5, poq) << "\t"
324  << m(6, poq) << "\t" << m(7, poq) << "\t" << m(8, poq) << "\t"
325  << m(9, poq) << "\t" << m(10, poq)
326  << m(11, poq) << "\t" << m(12, poq) << "\t"
327  << m(13, poq) << "\t" << m(14, poq) << "\t" << m(15, poq) << endl;
328  // fprintf(McoIN,
329  // "%.5e \t %.5e \t %.5e \t %.5e \t %.5e \t %.5e \t %.5e \t %.5e \t %.5e \t %.5e \t %.5e \t %.5e \t %.5e \t %.5e \t %.5e \t %.5e \n ",
330  // UU[0][poq], UU[1][poq], UU[2][poq], UU[3][poq], UU[4][poq], UU[5][poq], UU[6][poq], UU[7][poq], UU[8][poq], UU[9][poq], UU[10][poq],
331  // UU[11][poq], UU[12][poq], UU[13][poq], UU[14][poq], UU[15][poq]);
332 
333 
334 
335  }
336  mcorFile.close();
337 
338  string inmcorFilename(corrDirSuffix);
339  inmcorFilename += to_string(cortyp) + "/inmcor" + to_string(icn) + "_L.dat";
340  // sprintf(Min, "/hsm/belle/bdata2/users/avbobrov/belle2/corr%d/inmcor%d_L.dat", cortyp, icn);
341 
342  ofstream inmcorFile(inmcorFilename);
343  for (poq = 0; poq < 16; poq++) {
344  inmcorFile << setprecision(3)
345  << u(0, poq) << "\t" << u(1, poq) << "\t" << u(2, poq) << "\t"
346  << u(3, poq) << "\t" << u(4, poq) << "\t" << u(5, poq) << "\t"
347  << u(6, poq) << "\t" << u(7, poq) << "\t" << u(8, poq) << "\t"
348  << u(9, poq) << "\t" << u(10, poq)
349  << u(11, poq) << "\t" << u(12, poq) << "\t"
350  << u(13, poq) << "\t" << u(14, poq) << "\t" << u(15, poq) << endl;
351 
352  // fprintf(McoIN,
353  // "%.3e \t %.3e \t %.3e \t %.3e \t %.3e \t %.3e \t %.3e \t %.3e \t %.3e \t %.3e \t %.3e \t %.3e \t %.3e \t %.3e \t %.3e \t %.3e \n ",
354  // IM[0][poq], IM[1][poq], IM[2][poq], IM[3][poq], IM[4][poq], IM[5][poq], IM[6][poq], IM[7][poq], IM[8][poq], IM[9][poq], IM[10][poq],
355  //IM[11][poq], IM[12][poq], IM[13][poq], IM[14][poq], IM[15][poq]);
356  }
357  inmcorFile.close();
358 
359  string binmcorFilename(corrDirSuffix);
360  binmcorFilename += to_string(cortyp) + "/Binmcor" + to_string(icn) + "_L.dat";
361  // sprintf(BMin, "/hsm/belle/bdata2/users/avbobrov/belle2/corr%d/Binmcor%d_L.dat", cortyp, icn);
362  ofstream binmcorFile(binmcorFilename, ofstream::binary);
363  binmcorFile.write(reinterpret_cast<char*>(IM), sizeof(double) * 16 * 16);
364  binmcorFile.close();
365  } // converional cooment
366  //%%%%%%%%%%%%%%%%%%%%%%%%55555555555
367 
368  } // icn
369 
370  cout << endl;
371 // cout << " delta= " << delta << endl;
372  cout << " delta= " << delta << endl;
373 
374 
375 
376 
377 }
378 
379 int main(int argc, char** argv)
380 {
381  // first argument is cortype : what does it mean?
382  // second argument is input root file name
383  // third argument is (existing) directory to put the output files (cor matrices)
384  // fourth argument is the cut file name
385  assert(argc == 5);
386  matrix_cal(atoi(argv[1]), argv[2], argv[3], argv[4]);
387 }
388 
389 
390 
391 
392 
393 
394 
395 
396 
397 
398 
399 
400 
401 
402 
403 
Belle2::ECL::WrapArray2D
class to replace POD 2D array to save stack usage since it just allocates memory dynamically.
Definition: WrapArray2D.h:33
main
int main(int argc, char **argv)
Run all tests.
Definition: test_main.cc:77
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
prepareAsicCrosstalkSimDB.u
u
merged u1 and u2
Definition: prepareAsicCrosstalkSimDB.py:46