ATLAS Offline Software
CalibrateIBL.cxx
Go to the documentation of this file.
1 //======================================================================
2 // PixelCalibIBL : Read calibration file, redo the fit.
3 //
4 // Note : this program only runs on IBL.
5 //
6 //
7 // Mapping convention for FE in the calibration.
8 //
9 // ^
10 // phi|
11 // |
12 // 336| sFE0 sFE1
13 // |
14 // 0+------------------------------>
15 // 0 80 160
16 // eta
17 //======================================================================
18 
21 
22 
23 // ignore the "demonstrate Xcheck" lines unless necessary
24 // #define DEMOXCHECK true
25 #include <fstream>
26 #include <iostream>
27 #include <iterator>
28 #include <string>
29 #include <sstream>
30 #include <utility>
31 #include <map>
32 #include <array>
33 #include "TFile.h"
34 #include "TDirectory.h"
35 #include "TDirectoryFile.h"
36 #include "TKey.h"
37 #include "TH2D.h"
38 #include "TH2F.h"
39 #include "TProfile.h"
40 #include "TGraph.h"
41 #include "TGraphErrors.h"
42 #include "TMultiGraph.h"
43 #include "TMath.h"
44 #include "TF1.h"
45 #include "TStyle.h"
46 #include "TVectorT.h"
47 #include "THStack.h"
48 #include "TRandom3.h"
51 
52 using namespace std;
53 using pix::PixelMapping;
54 
55 // Lianyou: simplely capacitor charging curve
56 double funcDisp(double *x, double *par)
57 {
58  double ret = 9.9e10;
59  ret = par[0] * 0.1 * (1. - exp(-(x[0] - 1400.) * (x[0] - 1400.) / (1000. * par[1])) + par[2] * 0.0001 * sqrt(x[0]));
60  return ret;
61 }
62 
63 double ChrgfromToT(float tot, double par[10])
64 {
65  int lr = (tot <= 6. ? 0 : 5);
66  double chrg = 1400.;
67 
68  double num = par[0 + lr] + tot * (par[1 + lr] + tot * (par[2 + lr] + tot * par[3 + lr]));
69  double denom = 1. + tot * par[4 + lr];
70  if (denom != 0.)
71  chrg = num / denom;
72 
73  return chrg;
74 }
75 
76 double ToTfromChrg(float chrg, double par[5])
77 {
78  double tot = 0.;
79  double num = par[0] + chrg * (par[1] + chrg * (par[2] + chrg * par[3]));
80  double denom = 1. + chrg * par[4];
81  if (denom != 0.0)
82  tot = num / denom;
83  return tot;
84 }
85 
86 double funcRation5(double *x, double *par)
87 {
88  double ret = 9.9e10;
89  double num = par[0] + x[0] * (par[1] + x[0] * (par[2] + x[0] * par[3]));
90  double denom = 1. + x[0] * par[4];
91  if (denom != 0.0)
92  ret = num / denom;
93  return ret;
94 }
95 
96 double funcDispConvoluted(double *x, double *par)
97 {
98  double ret = 9.9e09;
99  ret = -par[0] * (x[0] - par[1]) + par[2];
100  return ret;
101 }
102 
103 int iblCalib(const std::string& InDir, const std::string& THRscan, const std::string& scan, const std::string& scanLowCharge)
104 {
105  // creating the object for the pixel mapping
106  PixelMapping pixmap(PathResolver::find_file("PixelCalibAlgs/mapping.csv", "DATAPATH"));
107 
108  const bool run3 = true; // new series of injection charges (22) and new format
109  const float badRDfracCut = 0.2; // Cut on the fraction of bad read out per frontend
110  float HDCshift = 2;
111 
112  // Shall we try this later ?
113  const int finerToT = 1; // smaller bin size for ToT if doing fit
114  constexpr bool moreFE = false;
115  constexpr int npsFEs = moreFE ? 8 : 2;
116 
117  gStyle->SetOptFit(1100);
118 
119 #if defined(DEMOXCHECK)
120 
121  std::cout << " DEMOXCHECK will run ... " << std::endl;
122 
123  // For debugging a small area of pixels will print their readout and injection
124  int XcheckCharge[2] = {13, 14}, XcheckToT = 10;
125  int XcheckPhi[2] = {57, 63}, XcheckEta[2] = {23, 29};
126  TString XcheckModule = "LI_S11_A_M3_A6";
127 
128  // fill some histograms for monitoring
129  int ToTfill = 3 * finerToT, CHRGfill = 3;
130  const int HDC = 2; // specifying output
131 
132 #endif
133 
134  Double_t tmpRCf_H[4] = {0.868295, 0.67523, 0.732279, 0.678458}; // initial values from history calibration
135  Double_t tmpRCf_T[4] = {1.02844, 1.10788, 1.07204, 1.11015}; // initial values from history calibration
136  Double_t reverseCF_H[4], reverseCF_T[4];
137  for (int t = 0; t < 4; t++)
138  {
139  reverseCF_H[t] = tmpRCf_H[t];
140  reverseCF_T[t] = tmpRCf_T[t];
141  }
142 
143  Double_t RDentries[33] = {0, 10494.8, 14605.6, 62952.6, 8663.57, 17320.2, 20417, 49508.9,
144  21646.8, 31503.9, 14955.6, 16290.7, 11617.5, 16461.2, 11980.5, 17210.5,
145  12773.7, 18574, 13930.9, 19777, 14629.4, 20619.5, 15191.6, 20883,
146  15411.1, 19198, 12097.4, 12109.7, 8881.96, 17235.1, 20802.3, 11265.8,
147  0.};
148 
149  std::vector<int> reversedModules = {182, 211, 213, 229, 223, 231, 246, 280, 295, 350, 357, 360, 371, 403, 416, 423};
150 
151  // outputs : something@somewhere as one like ...
152  TString Outdir = "./";
153  string spec = "ToTbin" + to_string(finerToT) + "_FrtEnd" + to_string(npsFEs) + "_";
154 
155 #if defined(DEMOXCHECK)
156  spec += "DEMOXCHECK_";
157 #endif
158 
159  TString StrFileName = spec.c_str() + scan;
160  TString rootFileName = Outdir + "/TotChargeCalib_" + StrFileName + ".root";
161  TString logFileName = Outdir + "/ChargeCalib_" + StrFileName + ".log";
162  TString dbFileName = Outdir + "/ChargeCalib_" + StrFileName + ".TXT";
163  if (!run3)
164  {
165  rootFileName = Outdir + "/TotChargeCalib_run2_" + StrFileName + ".root";
166  logFileName = Outdir + "/ChargeCalib_run2_" + StrFileName + ".log";
167  dbFileName = Outdir + "/ChargeCalib_run2_" + StrFileName + ".TXT";
168  }
169 
170  // fetch pictures from directory : ToT/ROD_L1_Sxx/LI_Sy_AC_Mz_ACk
171  ofstream logout(logFileName);
172  ofstream txtDB(dbFileName);
173 
174  TString inThrFile = "";
175  TString inTotFile = "";
176  TString rodPath = "";
177  TString inTotFileAux = "";
178  TString rodPathAux = "";
179 
180  std::cout << " Running IBL calibration analysis ... " << endl;
181 
182  inThrFile = InDir + THRscan + ".root";
183  inTotFile = InDir + scan + ".root";
184  rodPath = inTotFile + ":/" + scan.substr(scan.find("_")+1);
185  inTotFileAux = InDir + scanLowCharge + ".root";
186  rodPathAux = inTotFileAux + ":/" + scanLowCharge.substr(scanLowCharge.find("_")+1);
187 
188  // historical record for run2 fomat
189  if (!run3)
190  {
191  inThrFile = "calib2018/IBL/THR_SCAN_S000083686.root";
192  inTotFile = "calib2018/IBL/TOT_SCAN_S000083690.root";
193  rodPath = inTotFile + ":/S000083690";
194  }
195 
196  // selecting Q threshold
197  int nrow = 336, ncol = 160; // y-axis, x-axis respectively
198 
199  const Int_t numChrgs = 22;
200  Double_t IBLchrgs[numChrgs] = {1400., 1500., 1750., 2000., 2500., 3000., 3500., 4000., 5000., 6000., 8000., 10000.,
201  12000., 14000., 16000., 18000., 20000., 22000., 24000., 26000., 28000., 30000.};
202 
203  const Int_t nchargeIBL = (run3 ? 22 : 19);
204 
205  int skip = numChrgs - nchargeIBL;
206  Double_t chrgAbaciIBL[nchargeIBL], chargeErrArrIBL[nchargeIBL];
207  for (int c = 0; c < nchargeIBL; c++)
208  {
209  chrgAbaciIBL[c] = IBLchrgs[c + skip];
210  chargeErrArrIBL[c] = 0.;
211  }
212 
213  Double_t chrgsbins[nchargeIBL + 1];
214  for (int c = 1; c < nchargeIBL; c++)
215  chrgsbins[c] = 0.5 * (chrgAbaciIBL[c - 1] + chrgAbaciIBL[c]);
216 
217  chrgsbins[0] = chrgAbaciIBL[0] - 0.5 * (chrgAbaciIBL[1] - chrgAbaciIBL[0]);
218  chrgsbins[nchargeIBL] = chrgAbaciIBL[nchargeIBL - 1] + 0.5 * (chrgAbaciIBL[nchargeIBL - 1] - chrgAbaciIBL[nchargeIBL - 2]);
219 
220  // please note extra +1 for ending bins.
221  const Int_t nToTibl = 16 * finerToT + 1;
222 
223  Double_t totAbaci[nToTibl], totbins[nToTibl + 1];
224 
225  totbins[0] = -0.25;
226  totbins[1] = 0.5;
227  totAbaci[0] = 0.0;
228  totAbaci[1] = 1.;
229  for (int t = 2; t < nToTibl; t++)
230  {
231  totAbaci[t] = totAbaci[1] + (t - 1) / (1. * finerToT);
232  totbins[t] = 0.5 * (totAbaci[t] + totAbaci[t - 1]);
233  }
234  totbins[nToTibl] = totAbaci[nToTibl - 1] + 0.5 * (totbins[nToTibl - 1] - totbins[nToTibl - 2]);
235 
236  Double_t totErrArr[nToTibl];
237  for (int t = 0; t < nToTibl; t++)
238  totErrArr[t] = 0.;
239 
240 #if defined(DEMOXCHECK)
241  for (int t = 0; t < nToTibl; t++)
242  logout << " totAbaci : " << t << " " << totbins[t] << " < " << totAbaci[t] << " > " << totbins[t + 1] << endl;
243 
244  if (finerToT == 1)
245  {
246  for (int t = 1; t < nToTibl; t++)
247  {
248  RDentries[t] = RDentries[2 * t - 1] + RDentries[2 * t];
249  logout << " finer RDentries : " << t << " " << 2 * t - 1 << " " << 2 * t << " " << RDentries[t] << endl;
250  }
251  }
252 #endif
253 
254  map<string, map<string, map<string, float>>> pcdMap;
255 
256  TFile roFile(rootFileName, "RECREATE");
257  TDirectory *roThrDir = roFile.mkdir("Threshold");
258  TDirectory *roTotDir = roFile.mkdir("ToT");
259 
260  // ****************************** Threshold IBL ******************************
261 
262  Double_t THR_avg[2][npsFEs], ThrSig_avg[2][npsFEs];
263  array<std::unique_ptr<TH2D> , npsFEs> h2_Thr{};
264  array<std::unique_ptr<TH2D> , npsFEs> h2_ThrSig{};
265 
266  std::multimap<float, TString, std::greater<float>> badThr_Order;
267 
268  for (int sfe = 0; sfe < npsFEs; sfe++)
269  {
270  stringstream ss;
271  ss << sfe;
272 
273  string knowModule = "LI_S06_C_M1_C1";
274  string idx = "I" + ss.str();
275 
276  pcdMap[knowModule][idx]["ThrNorm"] = -42.;
277  pcdMap[knowModule][idx]["ThrRmsNorm"] = -42.;
278  pcdMap[knowModule][idx]["ThrSigNorm"] = -42.;
279  pcdMap[knowModule][idx]["ThrLong"] = -42.;
280  pcdMap[knowModule][idx]["ThrRmsLong"] = -42.;
281  pcdMap[knowModule][idx]["ThrSigLong"] = -42.;
282 
283  for (int nl = 0; nl < 2; nl++)
284  THR_avg[nl][sfe] = ThrSig_avg[nl][sfe] = -99.;
285 
286  idx = "Threshold" + ss.str();
287  h2_Thr[sfe] = std::make_unique<TH2D>(idx.c_str(), " ", 2, 0, 2, 200, 0, 5000);
288  idx = "ThresholdSig" + ss.str();
289  h2_ThrSig[sfe] = std::make_unique<TH2D>(idx.c_str(), " ", 2, 0, 2, 200, 0, 500);
290  }
291 
292  if (inThrFile.Length() > 0)
293  {
294  std::cout << endl
295  << "INFO =>> [IBL] threshold scan analysis..." << endl;
296 
297  TFile riThrFile(inThrFile, "READ");
298  TString chi2HistName = "SCURVE_CHI2";
299  TString thrHistName = "SCURVE_MEAN";
300  TString sigHistName = "SCURVE_SIGMA";
301 
302  std::unique_ptr<TH1F> h1dChi2 = std::make_unique<TH1F>("h1dChi2", "", 200, 0, 1);
303  std::unique_ptr<TH1F> h1dThr = std::make_unique<TH1F>("h1dThr" , "", 200, 0, 5000);
304  std::unique_ptr<TH1F> h1dSig = std::make_unique<TH1F>("h1dSig" , "", 200, 0, 500);
305 
306  TDirectoryFile *scanDir = (TDirectoryFile *)((TKey *)riThrFile.GetListOfKeys()->First())->ReadObj();
307  TList *rodKeyList = (TList *)scanDir->GetListOfKeys();
308  TIter rodItr(rodKeyList);
309  TKey *rodKey;
310 
311  while ((rodKey = (TKey *)rodItr()))
312  {
313  TString rodName(rodKey->GetName());
314  TDirectoryFile *rodDir = (TDirectoryFile *)rodKey->ReadObj();
315  TList *modKeyList = (TList *)rodDir->GetListOfKeys();
316  TIter modItr(modKeyList);
317  TKey *modKey;
318 
319  while ((modKey = (TKey *)modItr()))
320  {
321  TString modName(modKey->GetName());
322  string modStr(modKey->GetName());
323 
324  TString chi2HistDirPath = modName + "/" + chi2HistName + "/A0/B0";
325 
326  TDirectory *chi2HistDir = (TDirectory *)rodDir->Get(chi2HistDirPath);
327  if (chi2HistDir == NULL)
328  {
329  cout << " Warning : NULL dir " << endl;
330  continue;
331  }
332 
333  std::unique_ptr<TH2D> h2dChi2 (static_cast<TH2D*>(static_cast<TKey*>(chi2HistDir->GetListOfKeys()->First())->ReadObj()));
334  if (h2dChi2 == NULL)
335  {
336  cout << " Warning : NULL dir " << endl;
337  continue;
338  }
339  TString thrHistDirPath = modName + "/" + thrHistName + "/A0/B0";
340  TDirectoryFile *thrHistDir = (TDirectoryFile *)rodDir->Get(thrHistDirPath);
341  std::unique_ptr<TH2D> h2dThr(static_cast<TH2D*>(static_cast<TKey*>(thrHistDir->GetListOfKeys()->First())->ReadObj()));
342  TString sigHistDirPath = modName + "/" + sigHistName + "/A0/B0";
343 
344  TDirectoryFile *sigHistDir = (TDirectoryFile *)rodDir->Get(sigHistDirPath);
345  std::unique_ptr<TH2D> h2dSig (static_cast<TH2D*>(static_cast<TKey*>(sigHistDir->GetListOfKeys()->First())->ReadObj()));
346 
347  array<std::unique_ptr<TH1F>, npsFEs> h1_ThrNorm{};
348  array<std::unique_ptr<TH1F>, npsFEs> h1_ThrSigNorm{};
349  array<std::unique_ptr<TH1F>, npsFEs> h1_ThrLong{};
350  array<std::unique_ptr<TH1F>, npsFEs> h1_ThrSigLong{};
351  array<float, npsFEs> IlledThr{};
352 
353  for (int sfe = 0; sfe < npsFEs; sfe++)
354  {
355  stringstream ss;
356  ss << sfe;
357 
358  string hname = modStr + "ThrNorm" + ss.str();
359  h1_ThrNorm[sfe] = std::make_unique<TH1F>(hname.c_str(), "", 200, 0, 5000);
360  hname = modStr + "ThrSigNorm" + ss.str();
361  h1_ThrSigNorm[sfe] = std::make_unique<TH1F>(hname.c_str(), "", 200, 0, 500);
362  hname = modStr + "ThrLong" + ss.str();
363  h1_ThrLong[sfe] = std::make_unique<TH1F>(hname.c_str(), "", 200, 0, 5000);
364  hname = modStr + "ThrSigLong" + ss.str();
365  h1_ThrSigLong[sfe] = std::make_unique<TH1F>(hname.c_str(), "", 200, 0, 500);
366  }
367 
368  for (int col = 1; col <= ncol; col++)
369  {
370  for (int row = 1; row <= nrow; row++)
371  {
372  float chi2 = h2dChi2->GetBinContent(col, row);
373  float thr = h2dThr->GetBinContent(col, row);
374  float sig = h2dSig->GetBinContent(col, row);
375 
376  h1dChi2->Fill(chi2);
377  h1dThr->Fill(thr);
378  h1dSig->Fill(sig);
379  // Shall we measure/count the goodness of THR scan here ?
380  bool filled = true;
381  if (thr == 0 || thr > 10000 || sig == 0 || sig > 1000 || chi2 > 0.5 || chi2 <= 0)
382  {
383  filled = false;
384  }
385  int circ = -1;
386  if (filled)
387  {
388  if (col == 1 || col == ncol / 2 || col == ncol / 2 + 1 || col == ncol)
389  {
390  if (moreFE)
391  {
392  circ = (int)(row / 84.);
393  if (col > 80)
394  {
395  circ = 7 - circ;
396  }
397  }
398  else
399  {
400  if (col <= ncol / 2)
401  circ = 0;
402  else
403  circ = 1;
404  }
405 
406  h1_ThrLong[circ]->Fill(thr);
407  h1_ThrSigLong[circ]->Fill(sig);
408  }
409  else
410  {
411  if (moreFE)
412  {
413  circ = (int)(row / 84.);
414  if (col > 80)
415  {
416  circ = 7 - circ;
417  }
418  }
419  else
420  {
421  if (col <= ncol / 2)
422  circ = 0;
423  else
424  circ = 1;
425  }
426 
427  h1_ThrNorm[circ]->Fill(thr);
428  h1_ThrSigNorm[circ]->Fill(sig);
429  }
430  }
431  else
432  {
433  // Note: what is the intention? circ is -1 here
434  continue;
435  }
436  }
437  }
438  h2dSig.reset();
439  h2dThr.reset();
440  h2dChi2.reset();
441 
442  for (int sfe = 0; sfe < npsFEs; sfe++)
443  {
444  stringstream ss;
445  ss << sfe;
446  string feName = "I" + ss.str();
447 
448  float THRnorm = h1_ThrNorm[sfe]->GetMean();
449  bool valid = THRnorm > 100.;
450  pcdMap[modStr][feName]["ThrNorm"] = THRnorm;
451  pcdMap[modStr][feName]["ThrRmsNorm"] = h1_ThrNorm[sfe]->GetRMS();
452  float THRnormSig = h1_ThrSigNorm[sfe]->GetMean();
453  pcdMap[modStr][feName]["ThrSigNorm"] = THRnormSig;
454  float THRlong = h1_ThrLong[sfe]->GetMean();
455  pcdMap[modStr][feName]["ThrLong"] = THRlong;
456  pcdMap[modStr][feName]["ThrRmsLong"] = h1_ThrLong[sfe]->GetRMS();
457  float THRlongSig = h1_ThrSigLong[sfe]->GetMean();
458  pcdMap[modStr][feName]["ThrSigLong"] = THRlongSig;
459 
460  float blank = 100. * IlledThr[sfe] * npsFEs / (1. * ncol * nrow);
461 
462  if (blank > 0.01)
463  badThr_Order.insert(std::pair<float, TString>(blank, (TString)(modName + " : " + blank)));
464 
465  if (valid)
466  {
467  h2_Thr[sfe]->Fill(0.5, THRnorm, 1);
468  h2_Thr[sfe]->Fill(1.5, THRlong, 1);
469  h2_ThrSig[sfe]->Fill(0.5, THRnormSig, 1);
470  h2_ThrSig[sfe]->Fill(1.5, THRlongSig, 1);
471  }
472  h1_ThrNorm[sfe].reset();
473  h1_ThrSigNorm[sfe].reset();
474  h1_ThrLong[sfe].reset();
475  h1_ThrSigLong[sfe].reset();
476  }
477  }
478  }
479  roThrDir->WriteTObject(h1dChi2.get());
480  h1dChi2.reset();
481  roThrDir->WriteTObject(h1dThr.get());
482  h1dThr.reset();
483  roThrDir->WriteTObject(h1dSig.get());
484  h1dSig.reset();
485  }
486 
487  for (int sfe = 0; sfe < npsFEs; sfe++)
488  {
489  stringstream ss;
490  ss << sfe;
491  string hName = "thrNorm" + ss.str();
492  THR_avg[0][sfe] = h2_Thr[sfe]->ProjectionY(hName.c_str(), 1, 1)->GetMean();
493  hName = "thrLong" + ss.str();
494  THR_avg[1][sfe] = h2_Thr[sfe]->ProjectionY(hName.c_str(), 2, 2)->GetMean();
495  hName = "thrSigNorm" + ss.str();
496  ThrSig_avg[0][sfe] = h2_ThrSig[sfe]->ProjectionY(hName.c_str(), 1, 1)->GetMean();
497  hName = "thrSigLong" + ss.str();
498  ThrSig_avg[1][sfe] = h2_ThrSig[sfe]->ProjectionY(hName.c_str(), 2, 2)->GetMean();
499  h2_Thr[sfe].reset();
500  h2_ThrSig[sfe].reset();
501  }
502  // ****************************** End - Threshold IBL ******************************
503 
505  /************************* IBL ToT ***************************/
507 
508  if (inTotFile.Length() == 0 || inTotFileAux.Length() == 0)
509  {
510  logout << " Missing ToT file from calib scan as input " << endl;
511  return 0;
512  }
513 
514  std::cout << endl
515  << "INFO =>> [IBL] tot calib analysis..." << endl;
516 
517  TFile riTotFile(inTotFile, "READ");
518  TFile riTotFileAux(inTotFileAux, "READ");
519 
520  TString totHistName = "TOT_MEAN";
521  TString totSigHistName = "TOT_SIGMA";
522 
523  TDirectoryFile *scanDir = (TDirectoryFile *)((TKey *)riTotFile.GetListOfKeys()->First())->ReadObj();
524  TList *rodKeyList = (TList *)scanDir->GetListOfKeys();
525  TIter rodItr(rodKeyList);
526  TKey *rodKey;
527 
528  std::map<float, std::pair<vector<TString>, vector<Double_t>>> ModuDataToPrint;
529 
530 #if defined(DEMOXCHECK)
531  vector<TH1F *> h1_ChrgEntry;
532  h1_ChrgEntry.reserve(nToTibl);
533 
534  vector<TH1F *> h1d_totSprdAll;
535  h1d_totSprdAll.reserve(nToTibl - 1);
536 
537  for (int t = 0; t < nToTibl; t++)
538  {
539  stringstream tt;
540  tt << t;
541 
542  string hname = "ChrgEntries_ToT_" + tt.str();
543  h1_ChrgEntry[t] = new TH1F(hname.c_str(), "RD entries ", 100, 0., 100000);
544 
545  hname = "totSprdsFll_ToT_" + tt.str();
546  if (t < nToTibl - 1)
547  h1d_totSprdAll[t] = new TH1F(hname.c_str(), "ToT spread ", 36, 0.2, 1.1);
548  }
549 #endif
550 
551  const Int_t totFE = 14 * 16 * npsFEs; // 16 modules on each of 14 ReadOutDisk
552  Double_t TotArray[totFE][nchargeIBL], TotErrArray[totFE][nchargeIBL];
553  Double_t TotSigArray[totFE][nchargeIBL], TotSigErrArray[totFE][nchargeIBL];
554  Double_t ChrgArray[totFE][nToTibl], ChrgErrArray[totFE][nToTibl];
555 
556  gRandom = new TRandom3(2203);
557 
558  float occuPhiEta[nchargeIBL][totFE];
559 
560  Int_t cntRod = 0;
561  std::map<float, TString> devChrg_Order;
562  std::map<float, TString> devToT_Order;
563 
564  std::multimap<float, TString, std::greater<float>> badModules_Order;
565  std::multimap<float, TString, std::greater<float>> badModules_Order_detailed;
566 
567  while ((rodKey = (TKey *)rodItr()))
568  {
569  TString rodName(rodKey->GetName());
570  string rodStr(rodKey->GetName());
571  TDirectoryFile *rodDir = (TDirectoryFile *)rodKey->ReadObj();
572 
573  TString path = rodDir->GetPath();
574  TString pathAux = path.ReplaceAll(rodPath, rodPathAux);
575 
576  TDirectoryFile *rodDirAux = (TDirectoryFile *)riTotFileAux.Get(pathAux);
577  if (rodDirAux == NULL)
578  {
579  std::cout<< " Fail to get the rodPath in Aux: "<< pathAux << endl;
580  logout << " Fail to get the rodPath in Aux " << endl;
581  continue;
582  }
583 
584  TDirectory *dirRod = roTotDir->mkdir(rodName);
585 
586  TList *modKeyList = (TList *)rodDir->GetListOfKeys();
587  TIter modItr(modKeyList);
588 
589  TKey *modKey;
590 
591  int cntMod = 0;
592  string modNames[16 * npsFEs];
593 
594  float occuChrgs[nToTibl][16 * npsFEs];
595 
596 #if defined(DEMOXCHECK)
597  TString feName_maxDevChrg = "", feName_maxDevToT = "";
598  float maxDevChrg = -9., maxDevToT = -9., avgDevChrg = 0., avgDevToT = 0.;
599 
600  vector<TH1F *> h1d_totSprd;
601  h1d_totSprd.reserve(nToTibl - 1);
602 
603  for (int t = 0; t < nToTibl - 1; t++)
604  {
605  stringstream tt;
606  tt << t + 1;
607 
608  string prfmodname = "ToT_Sprd_" + tt.str();
609  h1d_totSprd[t] = new TH1F(prfmodname.c_str(), "ToT spread ", 28, 0.3, 1.0);
610  prfmodname = rodStr + "ToT spread @ ToT_" + tt.str();
611  h1d_totSprd[t]->SetTitle(prfmodname.c_str());
612  }
613 #endif
614 
615  while ((modKey = (TKey *)modItr()))
616  {
617  TString modName(modKey->GetName());
618  string modStr(modKey->GetName());
619 
620  TDirectory *dirMod = dirRod->mkdir(modName);
621  bool ibl3Dfe0 = false, ibl3Dfe1 = false;
622 
623  int hashID = -1, hashIDL = -1, hashIDR = -1;
624  int bec = -1;
625  int layer = -1;
626  int phi_module = -1;
627  int eta_module = -1;
628  int eta_moduleL = -1, eta_moduleR = -1;
629  pixmap.mapping(modStr, &hashID, &bec, &layer, &phi_module, &eta_module);
630  if (hashID == -1)
631  {
632  pixmap.mapping(modStr + "_1", &hashID, &bec, &layer, &phi_module, &eta_module);
633  if (hashID != -1)
634  {
635  ibl3Dfe0 = true;
636  hashIDL = hashID;
637  eta_moduleL = eta_module;
638  }
639 
640  pixmap.mapping(modStr + "_2", &hashID, &bec, &layer, &phi_module, &eta_module);
641  if (hashID != -1)
642  {
643  ibl3Dfe1 = true;
644  hashIDR = hashID;
645  eta_moduleR = eta_module;
646  }
647  }
648  if (hashID == -1){
649  std::cout<< "negative hash ID\n";
650  continue;
651  }
652 
653 
654  if (pcdMap.find(modStr) == pcdMap.end()){
655  std::cout<< "MOD not found "<< modStr << "\n";
656  continue;
657  }
658 
659 
660  // a stack of Charge-ToT data for ToT-charge converting
661  array<std::unique_ptr<TH2D>, npsFEs> h2d_XchrgYtot{};
662  array<std::unique_ptr<TH2D>, npsFEs> h2d_XchrgYToTSig{};
663 
664  for (int sfe = 0; sfe < npsFEs; sfe++)
665  {
666  stringstream ss;
667  ss << sfe;
668 
669  modNames[cntMod * npsFEs + sfe] = modStr + "-" + ss.str();
670 
671  string prfmodname = modStr + "ToTvsChrg_FE" + ss.str();
672  h2d_XchrgYtot[sfe] = std::make_unique<TH2D>(prfmodname.c_str(), "ToT vs Charge", nchargeIBL, chrgsbins, nToTibl, totbins);
673 
674  prfmodname = modStr + "ToT_Sig_vsChrg_FE" + ss.str();
675  h2d_XchrgYToTSig[sfe] = std::make_unique<TH2D>(prfmodname.c_str(), "ToT Sig vs Charge", nchargeIBL, chrgsbins, 100, 0., 1.);
676  }
677 
678  // Fill ToT-Chrg for pixels per module
679  for (int c = 0; c < nchargeIBL; c++)
680  {
681  TString totHistDirPath = modName + "/" + totHistName + "/A0/B0/C";
682  totHistDirPath += std::to_string(c);
683 
684  TDirectoryFile* totHistDir(static_cast<TDirectoryFile*> (rodDir->Get(totHistDirPath)));
685  if(!totHistDir){
686  std::cout<<" Missing totHistDir in : " << totHistDirPath << endl;
687  }
688 
689  std::unique_ptr<TH2F> h2dTot(static_cast<TH2F*> ((static_cast<TKey*>(totHistDir->GetListOfKeys()->First()))->ReadObj()));
690  h2dTot->SetDirectory(0);
691 
692  TDirectoryFile* totHistDirAux(static_cast<TDirectoryFile*>(rodDirAux->Get(totHistDirPath)));
693  if (!totHistDirAux)
694  {
695  std::cout<<" Missing totHistDir in : " << totHistDirPath << endl;
696  logout << " Missing totHistDir in : " << totHistDirPath << endl;
697  }
698 
699  unique_ptr<TH2F> h2dTotAux (static_cast<TH2F*> ((static_cast<TKey*>(totHistDirAux->GetListOfKeys()->First())->ReadObj())));
700  h2dTotAux->SetDirectory(0);
701 
702 
703  TString totSigHistDirPath = modName + "/" + totSigHistName + "/A0/B0/C";
704  totSigHistDirPath += std::to_string(c);
705  TDirectoryFile* totSigHistDir (static_cast<TDirectoryFile*>(rodDir->Get(totSigHistDirPath)));
706  unique_ptr<TH2F> h2dTotSig (static_cast<TH2F*> ((static_cast<TKey*>(totSigHistDir->GetListOfKeys()->First()))->ReadObj()));
707  h2dTotSig->SetDirectory(0);
708 
709  // Now one fill the calib-data from all pixles as basics.
710  for (int ieta = 0; ieta < ncol; ieta++)
711  {
712  for (int iphi = 0; iphi < nrow; iphi++)
713  {
714  int circ = -1;
715  if (moreFE)
716  {
717  // virtual FE with 80*84 pixels, so that sFE0, sFE1, sFE7, sFE6 will make up the physical 1'st FE
718  // while sFE2, sFE3, sFE5, sFE4 make up the physical 2'nd.
719  circ = (int)(iphi / 84.); // sFE0, sFE1, sFE2, sFE3
720  if (ieta > 80)
721  {
722  circ = 7 - circ;
723  } // sFE7, sFE6, sFE5, sFE4
724  }
725  else
726  {
727  if (ieta < ncol / 2)
728  circ = 0;
729  else
730  circ = 1;
731  }
732 
733  float tot = h2dTot->GetBinContent(ieta + 1, iphi + 1) + HDCshift;
734  float totAux = h2dTotAux->GetBinContent(ieta + 1, iphi + 1);
735 
736  // use ToT from higher HDC as default
737  float filltot = tot;
738 
739  // use ToT from lower HDC for SMALL charges, dirty in studying
740  // May 4, 2022, please keep below strategy, it give reasonable merging till now
741 
742  if (tot <= 5 || c <= 5)
743  {
744  if ((c == 0 && tot - totAux > 0.8) || (c >= 1 && c <= 3 && tot - totAux > 0.5) || (c > 4 && tot - totAux > 0.3))
745  filltot = totAux;
746  else
747  filltot = 0.5 * (totAux + tot);
748  if (c == 0 && totAux > 2.)
749  filltot = 1.;
750  }
751 
752  if (c > 19)
753  filltot = 0.5 * (totAux + tot + 2.);
754 
755  if (filltot > 0.){
756  h2d_XchrgYtot[circ]->Fill(chrgAbaciIBL[c], filltot, 1.);
757  }
758 
759 
760  float err = h2dTotSig->GetBinContent(ieta + 1, iphi + 1);
761  h2d_XchrgYToTSig[circ]->Fill(chrgAbaciIBL[c], err, 1.);
762  if (err == 0.)
763  err = 0.05;
764 
765 #if defined(DEMOXCHECK)
766  if (modName == XcheckModule && (iphi >= XcheckPhi[0] && iphi <= XcheckPhi[1]) && (ieta >= XcheckEta[0] && ieta <= XcheckEta[1]) && (c == XcheckCharge[0] || c == XcheckCharge[1] || (int)(tot + 0.5) == XcheckToT || (int)(totAux + 0.5) == XcheckToT))
767  logout << " tot = " << tot << ", totAux = " << totAux << " @injection " << chrgAbaciIBL[c] << " " << err << endl;
768 #endif
769  }
770  } // all pixels done over a module
771 
772  h2dTotSig.reset();
773  h2dTotAux.reset();
774  h2dTot.reset();
775 
776  } // end looping over charges
777  float modHash = -1;
778  if (!(ibl3Dfe0 || ibl3Dfe1))
779  {
780  modHash = hashID;
781  }
782 
783  array<TDirectory *, npsFEs> dirFE;
784 
785  Int_t idxMod = cntRod * 16 * npsFEs + cntMod * npsFEs;
786 
787  for (int sfe = 0; sfe < npsFEs; sfe++)
788  {
789  stringstream ss;
790  ss << sfe;
791 
792  if (ibl3Dfe0 && sfe < npsFEs / 2)
793  {
794  modHash = hashIDL;
795  }
796  else if (ibl3Dfe1 && sfe >= npsFEs / 2)
797  {
798  modHash = hashIDR;
799  }
800  else
801  modHash += (sfe < npsFEs / 2 ? 0 : 1) * 0.8;
802 
803  string prfmodname = "DirFE_" + ss.str();
804  dirFE[sfe] = dirMod->mkdir(prfmodname.c_str());
805 
806  // standard deviation will be adopted for ERROR
807  prfmodname = modStr + "_profile_Tot_FE" + ss.str();
808  std::unique_ptr<TProfile> prfl_TotsFE(h2d_XchrgYtot[sfe]->ProfileX(prfmodname.c_str(), 1, -1, "s"));
809  prfl_TotsFE->SetTitle(prfmodname.c_str());
810  prfmodname = modStr + "_profile_Chrg_FE" + ss.str();
811  // shanly : seeming free from ROOT-7770 issue. 2021 Sep 17
812  std::unique_ptr<TProfile> prfl_ChrgsFE(h2d_XchrgYtot[sfe]->ProfileY(prfmodname.c_str(), 1, -1, "s"));
813  prfl_ChrgsFE->SetTitle(prfmodname.c_str());
814 
815  Double_t TotArr[nchargeIBL], TotErrArr[nchargeIBL];
816  Double_t ChrgArr[nToTibl], ChrgErrArr[nToTibl];
817 
818  Int_t idxFE = idxMod + sfe;
819  // ToT distribution along nchargeIBL bins charges
820  int dumbFE = 0;
821  for (int c = 0; c < nchargeIBL; c++)
822  {
823  TotArr[c] = TotArray[idxFE][c] = prfl_TotsFE->GetBinContent(c + 1);
824  TotErrArr[c] = TotErrArray[idxFE][c] = prfl_TotsFE->GetBinError(c + 1);
825 
826  occuPhiEta[c][idxFE] = prfl_TotsFE->GetBinEntries(c + 1) * npsFEs / (1. * ncol * nrow);
827 
828  if ((1. - occuPhiEta[c][idxFE]) > badRDfracCut)
829  dumbFE++;
830 
831  stringstream sc;
832  sc << c;
833  prfmodname = modStr + "_ToTsig_FE" + ss.str() + "Chrg" + sc.str();
834 
835  std::unique_ptr<TH1D> h_ToTsig(h2d_XchrgYToTSig[sfe]->ProjectionY(prfmodname.c_str(), c + 1, c + 1));
836  TotSigArray[idxFE][c] = TMath::Sqrt(h_ToTsig->GetMean() * h_ToTsig->GetMean() + h_ToTsig->GetRMS() * h_ToTsig->GetRMS());
837  TotSigErrArray[idxFE][c] = TMath::Sqrt(h_ToTsig->GetMeanError() * h_ToTsig->GetMeanError() + h_ToTsig->GetRMSError() * h_ToTsig->GetRMSError());
838 
839  h_ToTsig.reset();
840  }
841  prfl_TotsFE.reset();
842 
843  if (dumbFE > 5)
844  {
845  for (int c = 0; c < nchargeIBL; c++)
846  {
847  TotArr[c] = TotArray[idxFE][c] = 0.5 * (TotArray[idxFE - 1][c] + TotArray[idxFE - 2][c]);
848  TotErrArr[c] = TotErrArray[idxFE][c] = 0.5 * (TotErrArray[idxFE - 1][c] + TotErrArray[idxFE - 2][c]);
849 
850  TotSigArray[idxFE][c] = 0.5 * (TotSigArray[idxFE - 1][c] + TotSigArray[idxFE - 2][c]);
851  TotSigErrArray[idxFE][c] = 0.5 * (TotSigErrArray[idxFE - 1][c] + TotSigErrArray[idxFE - 2][c]);
852 
853  }
854  }
855  // Charges distribution along 16 bins ToT
856  Double_t errToT_overChrg[nToTibl];
857  for (int t = 0; t < nToTibl; t++)
858  {
859  ChrgArr[t] = ChrgArray[idxFE][t] = prfl_ChrgsFE->GetBinContent(t + 1);
860  ChrgErrArr[t] = ChrgErrArray[idxFE][t] = prfl_ChrgsFE->GetBinError(t + 1);
861 
862  occuChrgs[t][cntMod * npsFEs + sfe] = prfl_ChrgsFE->GetBinEntries(t + 1) / RDentries[t];
863 
864  stringstream st;
865  st << t;
866  prfmodname = modStr + "_Chrg_FE" + ss.str() + "ToT" + st.str();
867  std::unique_ptr<TH1D> h_chrg(h2d_XchrgYtot[sfe]->ProjectionX(prfmodname.c_str(), t + 1, t + 1));
868  st.str("");
869  st << totAbaci[t];
870  prfmodname = modStr + " Charge_FE" + ss.str() + " @ ToT : " + st.str();
871  h_chrg->SetTitle(prfmodname.c_str());
872 
873  Double_t scl = h_chrg->Integral();
874  errToT_overChrg[t] = 0.;
875  if (scl > 0.)
876  for (int b = 0; b < nchargeIBL; b++)
877  {
878  errToT_overChrg[t] += TotErrArr[b] * (h_chrg->GetBinContent(b + 1)) / scl;
879  }
880  if (errToT_overChrg[t] == 0.)
881  errToT_overChrg[t] = 1.1;
882 
883  h_chrg.reset();
884  }
885  prfl_ChrgsFE.reset();
886 #if defined(DEMOXCHECK)
887  for (int b = 1; b < nToTibl; b++)
888  {
889  double err = errToT_overChrg[b];
890  h1d_totSprd[b - 1]->Fill(err);
891  h1d_totSprdAll[b - 1]->Fill(err);
892  }
893 #endif
894 
895  // try a correction upon dumb FE
896  if (dumbFE > 5)
897  {
898  for (int t = 0; t < nToTibl; t++)
899  {
900  ChrgArr[t] = ChrgArray[idxFE][t] = 0.5 * (ChrgArray[idxFE - 1][t] + ChrgArray[idxFE - 2][t]);
901  ChrgErrArr[t] = ChrgErrArray[idxFE][t] = 0.5 * (ChrgErrArray[idxFE - 1][t] + ChrgErrArray[idxFE - 2][t]);
902  }
903  }
904  bool xoticMod = std::find(reversedModules.begin(), reversedModules.end(), int(modHash)) != reversedModules.end();
905 
906  // when charges' order are distorted along ToTs
907  bool reverseH = false, reverseT = false;
908  for (int t = 4; t >= 1; t--)
909  if (ChrgArr[t] > ChrgArr[t + 1])
910  {
911  reverseH = true;
912  break;
913  }
914  for (int t = 4; t >= 1; t--)
915  {
916  int tl = nToTibl - t;
917  if (ChrgArr[tl] < ChrgArr[tl - 1])
918  {
919  reverseT = true;
920  break;
921  }
922  }
923 
924  if (reverseH)
925  {
926 #if defined(DEMOXCHECK)
927  logout << " Head reversed : " << int(modHash) << " " << modName << " " << sfe << " " << ChrgArr[1] << " "
928  << ChrgArr[2] << " " << ChrgArr[3] << " " << ChrgArr[4] << " " << ChrgArr[5] << std::endl;
929 #endif
930  // try a correction with some averaged factors
931  for (int t = 4; t >= 1; t--)
932  if (ChrgArr[t] > ChrgArr[t + 1] && (occuChrgs[t][cntMod * npsFEs + sfe] < 0.5 || xoticMod))
933  ChrgArr[t] = ChrgArray[idxFE][t] = reverseCF_H[t - 1] * ChrgArr[t + 1];
934  }
935  else // prepare the average factors along ToTs
936  {
937  for (int t = 4; t >= 1; t--)
938  if (ChrgArr[t + 1] != 0.)
939  reverseCF_H[t - 1] = (reverseCF_H[t - 1] == tmpRCf_H[t - 1] ? ChrgArr[t] / ChrgArr[t + 1] : 0.5 * (reverseCF_H[t - 1] + ChrgArr[t] / ChrgArr[t + 1]));
940  }
941 
942  if (reverseT)
943  {
944 #if defined(DEMOXCHECK)
945  logout << " Tail reversed : " << int(modHash) << " " << modName << " " << sfe << " " << ChrgArr[nToTibl - 5] << " "
946  << ChrgArr[nToTibl - 4] << " " << ChrgArr[nToTibl - 3] << " " << ChrgArr[nToTibl - 2] << " " << ChrgArr[nToTibl - 1] << std::endl;
947 #endif
948  // try a correction with some averaged factors
949  for (int t = 4; t >= 1; t--)
950  {
951  int tl = nToTibl - t;
952  if (ChrgArr[tl] < ChrgArr[tl - 1] && (occuChrgs[tl][cntMod * npsFEs + sfe] < 0.5 || xoticMod))
953  ChrgArr[tl] = ChrgArray[idxFE][tl] = reverseCF_T[t - 1] * ChrgArr[tl - 1];
954  }
955  }
956  else // prepare the average factors along ToTs
957  {
958  for (int t = 4; t >= 1; t--)
959  {
960  int tl = nToTibl - t;
961  if (ChrgArr[tl - 1] != 0.)
962  reverseCF_T[t - 1] = (reverseCF_T[t - 1] == tmpRCf_T[t - 1] ? ChrgArr[tl] / ChrgArr[tl - 1] : 0.5 * (reverseCF_T[t - 1] + ChrgArr[tl] / ChrgArr[tl - 1]));
963  }
964  }
965 
966  string gername = modStr + "_grToTsig_FE_" + ss.str();
967  std::unique_ptr<TGraphErrors> grTotSig = std::make_unique<TGraphErrors>(nchargeIBL, chrgAbaciIBL, TotSigArray[sfe], chargeErrArrIBL, TotSigErrArray[sfe]);
968  grTotSig->SetTitle(gername.c_str());
969  grTotSig->SetName(gername.c_str());
970 
971  std::unique_ptr<TGraph> grTotSprd = std::make_unique<TGraph>(nToTibl, totAbaci, errToT_overChrg);
972  gername = "ToTsprd";
973  grTotSprd->SetName(gername.c_str());
974  gername = modStr + " ToT spread over FrontEnd @ FE " + ss.str();
975  grTotSprd->SetTitle(gername.c_str());
976  grTotSprd->SetLineColor(4);
977  grTotSprd->SetMarkerStyle(4);
978  grTotSprd->SetMarkerSize(1.);
979  grTotSprd->SetMaximum(0.9);
980 
981  TF1 ToTres("ToTres", funcDispConvoluted, 0.8, 16.5, 3);
982  ToTres.SetParameter(1,13.);
983  ToTres.SetParameter(2,0.65);
984  ToTres.SetParLimits(1, 10., 16.);
985  ToTres.SetParLimits(2, 0.3, 1.0);
986 
987  grTotSprd->Fit(&ToTres, "MRQ");
988  Double_t parP0 = ToTres.GetParameter(0);
989  Double_t parP1 = ToTres.GetParameter(1);
990 
991  dirFE[sfe]->WriteTObject(grTotSprd.get());
992  grTotSprd.reset();
993  // Lets' keep the bad 2-para fit untill the dataBase will be updated someday
1016  gername = modStr + "_grToT_FE_" + ss.str();
1017  std::unique_ptr<TGraphErrors> grTot = std::make_unique<TGraphErrors>(nchargeIBL, chrgAbaciIBL, TotArr, chargeErrArrIBL, TotErrArr);
1018  grTot->SetTitle(gername.c_str());
1019  grTot->SetName(gername.c_str());
1020  TF1 *f1ToTfromCharge = new TF1("ToTfromCharge", funcRation5, chrgAbaciIBL[0] - 100., chrgAbaciIBL[nchargeIBL - 1] + 300., 5);
1021 
1022  grTot->Fit(f1ToTfromCharge, "MRQ");
1023 
1024  std::unique_ptr<TGraphErrors> grChrg = std::make_unique<TGraphErrors>(nToTibl, totAbaci, ChrgArr, totErrArr, ChrgErrArr);
1025 
1026  gername = modStr + "_grChrg_FE_" + ss.str();
1027  grChrg->SetName(gername.c_str());
1028  grChrg->SetTitle(gername.c_str());
1029  grChrg->SetLineColor(3);
1030  grChrg->SetLineWidth(3);
1031 
1032  TF1 *f1ChargefromToTLeft = new TF1("ChargefromToTL", funcRation5, 1., 7.5, 5);
1033  f1ChargefromToTLeft->SetLineColor(2);
1034  grChrg->Fit(f1ChargefromToTLeft, "MRQ");
1035  if (f1ChargefromToTLeft->GetChisquare() / 4. > 2.){
1036  logout << "bad fit Left... " << std::endl;
1037  }
1038 
1039  TF1 *f1ChargefromToTRight = new TF1("ChargefromToTR", funcRation5, 5.5, 16., 5);
1040  f1ChargefromToTRight->SetLineColor(4);
1041  grChrg->Fit(f1ChargefromToTRight, "MRQ+");
1042 
1043  dirFE[sfe]->WriteTObject(grTot.get());
1044  dirFE[sfe]->WriteTObject(grTotSig.get());
1045  dirFE[sfe]->WriteTObject(h2d_XchrgYtot[sfe].get());
1046 
1047  h2d_XchrgYtot[sfe].reset();
1048  h2d_XchrgYToTSig[sfe].reset();
1049  grTot.reset();
1050  grTotSig.reset();
1051  grChrg.reset();
1052 
1053  // prepare a map sor sorting
1054  string Idx = "I" + ss.str();
1055  vector<TString> modName2prt;
1056  modName2prt.push_back(modName);
1057  modName2prt.push_back((TString)(Idx));
1058  // unfortunately some tuning is necessary to satisfy dataBase reauirements
1059  // 5 for module ID, 4 for threshold, nToTibl - 1 for charges at ToT, 3 for ResToT
1060  std::vector<Double_t> prtAux;
1061  prtAux.reserve(5 + 4 + nToTibl - 1 + 3); // skip ToT = 0
1062  // @ 0 ... 4
1063  prtAux.push_back(modHash);
1064  prtAux.push_back(bec);
1065  prtAux.push_back(layer);
1066  prtAux.push_back(phi_module);
1067  prtAux.push_back(eta_module);
1068 
1069  modName2prt[0] = (modStr + "_" + ss.str()).c_str();
1070  if (ibl3Dfe0 && sfe < npsFEs / 2)
1071  {
1072  if (!moreFE)
1073  modName2prt[0] = modName + "_0";
1074  prtAux[0] = hashIDL;
1075  prtAux[4] = eta_moduleL;
1076  }
1077  if (ibl3Dfe1 && sfe >= npsFEs / 2)
1078  {
1079  if (!moreFE)
1080  modName2prt[0] = modName + "_1";
1081  prtAux[0] = hashIDR;
1082  prtAux[4] = eta_moduleR;
1083  }
1084 
1085  // @ 5 ... 8
1086  float ThrNorm = pcdMap[modStr][Idx]["ThrNorm"];
1087  if (ThrNorm == 0. || ThrNorm == -42.)
1088  {
1089  // retrieve something from average when it's missing in calibration
1090  prtAux.push_back(THR_avg[0][sfe]);
1091  prtAux.push_back(ThrSig_avg[0][sfe]);
1092  prtAux.push_back(THR_avg[1][sfe]);
1093  prtAux.push_back(ThrSig_avg[1][sfe]);
1094  }
1095  else
1096  {
1097  prtAux.push_back(pcdMap[modStr][Idx]["ThrNorm"]);
1098  prtAux.push_back(pcdMap[modStr][Idx]["ThrSigNorm"]);
1099  prtAux.push_back(pcdMap[modStr][Idx]["ThrLong"]);
1100  prtAux.push_back(pcdMap[modStr][Idx]["ThrSigLong"]);
1101  }
1102  // since this version, the goodness will be skipped
1103  // @ 10 ... nToTibl + 8, with ToT = 0 skipped
1104  for (int t = 1; t < nToTibl; t++)
1105  prtAux.push_back(ChrgArr[t]);
1106 
1107  // @ nToTibl + 9, nToTibl + 10
1108  prtAux.push_back(parP0);
1109  prtAux.push_back(parP1);
1110 
1111  std::pair<vector<TString>, vector<Double_t>> payloadDB = std::pair<vector<TString>, vector<Double_t>>(modName2prt, prtAux);
1112  ModuDataToPrint.insert(std::pair<float, std::pair<vector<TString>, vector<Double_t>>>(modHash, payloadDB));
1113  } // end 1'st loop over FrontEnds
1114 
1115  cntMod++;
1116  } // end of loop over Mods
1117 
1118 #if defined(DEMOXCHECK)
1119 
1120  TDirectory *dirToTSprd = dirRod->mkdir("ToT_Spreads");
1121  for (int b = 0; b < nToTibl - 1; b++)
1122  {
1123  Int_t upper = h1d_totSprd[b]->FindLastBinAbove(1, 1);
1124  h1d_totSprd[b]->SetAxisRange(h1d_totSprd[b]->GetBinLowEdge(
1125  h1d_totSprd[b]->FindFirstBinAbove(1, 1)),
1126  h1d_totSprd[b]->GetBinLowEdge(upper) + h1d_totSprd[b]->GetBinWidth(upper), "X");
1127 
1128  dirToTSprd->WriteTObject(h1d_totSprd[b]);
1129  delete h1d_totSprd[b];
1130  }
1131  h1d_totSprd.clear();
1132 
1133  TString hn = "";
1134 
1135  TH2F *h2_badChrgs = new TH2F(rodName + "ChargeOccupancy", "Occupancy ", nchargeIBL, 0, nchargeIBL, 16 * npsFEs, 0, 16 * npsFEs);
1136  hn = "Occupancy along charges @ " + rodName;
1137  h2_badChrgs->SetTitle(hn);
1138 
1139  TH2F *h2_badToT = new TH2F(rodName + "ToTOccupancy", "Occupancy ", nToTibl - 1, 0.5, nToTibl - 0.5, 16 * npsFEs, 0, 16 * npsFEs);
1140  hn = "Occupancy along ToT @ " + rodName;
1141  h2_badToT->SetTitle(hn);
1142 
1143  bool fillChrg = false, fillToT = false;
1144 
1145  for (int sfe = 0; sfe < 16 * npsFEs; sfe++)
1146  {
1147  float PhiEta = 0.;
1148  for (int c = 0; c < nchargeIBL; c++)
1149  {
1150  float occu = occuPhiEta[c][cntRod * 16 * npsFEs + sfe];
1151  float nt = 10. - floor(10. * occu + 0.5);
1152  PhiEta += 0.1 * nt;
1153  TString modchrg = (TString)(modNames[sfe] + "_Chrg_" + c);
1154  if (nt > 0.01)
1155  badModules_Order_detailed.insert(std::pair<float, TString>(1. - occu, modchrg));
1156  if (nt > 5.)
1157  continue;
1158  if (nt < 0.1)
1159  nt = 0.01;
1160 
1161  h2_badChrgs->SetBinContent(c + 1, sfe + 1, nt);
1162  fillChrg = true;
1163  }
1164  PhiEta /= nchargeIBL;
1165  if (PhiEta > 0.01)
1166  badModules_Order.insert(std::pair<float, TString>(PhiEta,
1167  (TString)(modNames[sfe])));
1168 
1169  for (int t = 1; t < nToTibl; t++)
1170  {
1171  float nt = floor(10. * occuChrgs[t][sfe] + 0.5);
1172  if (nt > 5.)
1173  continue;
1174  h2_badToT->SetBinContent(t + 1, sfe + 1, nt);
1175  fillToT = true;
1176  }
1177 
1178  TString shortName = modNames[sfe].substr(9, 13).c_str();
1179  h2_badChrgs->GetYaxis()->SetBinLabel(sfe + 1, shortName);
1180  h2_badToT->GetYaxis()->SetBinLabel(sfe + 1, shortName);
1181  h2_badChrgs->SetStats(kFALSE);
1182  h2_badToT->SetStats(kFALSE);
1183  h2_badChrgs->SetTickLength(0.01, "Y");
1184  h2_badToT->SetTickLength(0.01, "Y");
1185  }
1186 
1187  h2_badChrgs->SetOption("TEXT");
1188  h2_badToT->SetOption("TEXT");
1189 
1190  if (fillChrg)
1191  {
1192  dirRod->WriteTObject(h2_badChrgs, "", "Overwrite");
1193  }
1194 
1195  if (fillToT)
1196  {
1197  dirRod->WriteTObject(h2_badToT, "", "WriteDelete");
1198  }
1199 
1200  delete h2_badChrgs;
1201  delete h2_badToT;
1202 
1203 #endif
1204 
1205  cntRod++;
1206  } // end of loop over Rods
1207 
1208 #if defined(DEMOXCHECK)
1209 
1210  for (int t = 0; t < 4; t++)
1211  {
1212  if (t == 0)
1213  logout << " correction factors for reversed charges : ";
1214  logout << reverseCF_H[t] << ", ";
1215  if (t == 3)
1216  logout << endl;
1217  }
1218  for (int t = 0; t < 4; t++)
1219  {
1220  if (t == 0)
1221  logout << " correction factors for reversed charges : ";
1222  logout << reverseCF_T[t] << ", ";
1223  if (t == 3)
1224  logout << endl;
1225  }
1226 
1227  for (int t = 0; t < nToTibl; t++)
1228  {
1229  if (t == 0)
1230  logout << "RD Entries per ToT bin ";
1231  logout << h1_ChrgEntry[t]->GetMean() << ", ";
1232  if (t == nToTibl - 1)
1233  logout << std::endl;
1234  roTotDir->WriteTObject(h1_ChrgEntry[t]);
1235  delete h1_ChrgEntry[t];
1236  }
1237  h1_ChrgEntry.clear();
1238 
1239  for (std::map<float, TString>::const_iterator itr = devChrg_Order.begin();
1240  itr != devChrg_Order.end(); ++itr)
1241  logout << " Charge dev order : " << itr->second << " : " << itr->first << endl;
1242 
1243  for (std::map<float, TString>::const_iterator itr = devToT_Order.begin();
1244  itr != devToT_Order.end(); ++itr)
1245  logout << " ToT dev order : " << itr->second << " : " << itr->first << endl;
1246 
1247  logout << "ToTRes (spread) & its RMS @ ToT from 1 to 16 : " << endl;
1248  for (int t = 0; t < nToTibl - 1; t++)
1249  {
1250  if (t == 0)
1251  logout << "[ ";
1252  logout << "[ " << h1d_totSprdAll[t]->GetMean() << " , " << h1d_totSprdAll[t]->GetStdDev() << " ] ";
1253  if (t < nToTibl - 2)
1254  logout << " , " << endl;
1255  if (t == nToTibl - 2)
1256  logout << " ] " << endl;
1257  delete h1d_totSprdAll[t];
1258  }
1259  h1d_totSprdAll.clear();
1260 #endif
1261 
1262  logout << " goto print " << std::endl;
1263 
1264  // Comment out for information only
1265  // txtDB << "####################################################################################################" << endl;
1266  // txtDB << "=========== physics meaning of each column ====================" << endl;
1267  // txtDB << "hashId " << "Threshold " << "ThresholdRMS " << "ThresholdLong " << "ThresholdRMSLong " << "Chrg_ToT1 "
1268  // << "Chrg_ToT2 " << "Chrg_ToT3 " << "Chrg_ToT4 " << "Chrg_ToT5 " << "Chrg_ToT6 " << "Chrg_ToT7 " << "Chrg_ToT8 " << "Chrg_ToT9 "
1269  // << "Chrg_ToT10 " << "Chrg_ToT11 " << "Chrg_ToT12 " << "Chrg_ToT13 " << "Chrg_ToT14 " << "Chrg_ToT15 " << "Chrg_ToT16 "
1270  // << "ToT_sprd " << " ToT_sprd_var" << endl;
1271 
1272  // txtDB << "######################## TXT for IBL calibration ____Start : _> ###########################" << endl;
1273 
1274  // now print the dataBase payloads in a TXT format
1275  for (std::map<float, std::pair<vector<TString>, vector<Double_t>>>::const_iterator itr = ModuDataToPrint.begin();
1276  itr != ModuDataToPrint.end(); ++itr)
1277  {
1278  int hash = floor(itr->first);
1279  std::pair<vector<TString>, vector<Double_t>> payload = itr->second;
1280  vector<Double_t> fe = payload.second;
1281 
1282  txtDB << hash << " ";
1283 
1284  for (unsigned int t = 5; t < fe.size(); t++)
1285  {
1286  if (t < fe.size() - 2)
1287  {
1288  txtDB << (int)(fe[t]) << " ";
1289 #if defined(DEMOXCHECK)
1290  if (fe[t] > fe[t + 1] && t >= 9 && t <= 23)
1291  logout << " Reversed charge ! " << t - 5 << " " << fe[t] << " " << fe[t + 1] << std::endl;
1292 #endif
1293  }
1294  else
1295  {
1296  float p01 = abs(fe[t]);
1297  if (t < fe.size() - 1)
1298  txtDB << 0.001 * (int)(p01 * 1000.) << " ";
1299  else
1300  txtDB << 0.000000001 * (int)(p01 * 1000000000.) << " ";
1301  }
1302  }
1303  txtDB << endl;
1304  }
1305  // txtDB << "####################################################################################################" << endl;
1306  // txtDB << "######################## TXT for IBL calibration <___ : End _____ ###########################" << endl;
1307  // txtDB << "####################################################################################################" << endl;
1308 
1309  std::cout << " Please find the file : " << dbFileName << " for dataBase payload " << endl;
1310 
1311  // statistics for BAD frontends, only for ELOG reports
1312  logout << " modules lacking in RD during Threshold scan : " << std::endl;
1313  for (std::multimap<float, TString, std::greater<float>>::const_iterator itr = badThr_Order.begin(); itr != badThr_Order.end(); ++itr)
1314  logout << " " << itr->second << " " << (itr->first) * 100. << "%" << endl;
1315 
1316  logout << " modules lacking in RD during ToT scan : " << std::endl;
1317  for (std::multimap<float, TString, std::greater<float>>::const_iterator itr = badModules_Order.begin(); itr != badModules_Order.end(); ++itr)
1318  logout << " " << itr->second << " " << (itr->first) * 100. << "%" << endl;
1319 
1320  logout << " modules lacking in RD at certain charges during ToT scan : " << std::endl;
1321  for (std::multimap<float, TString, std::greater<float>>::const_iterator itr = badModules_Order_detailed.begin();
1322  itr != badModules_Order_detailed.end(); ++itr)
1323  logout << " " << itr->second << " " << (itr->first) * 100. << "%" << endl;
1324 
1325  roFile.Close();
1326  txtDB.close();
1327  logout.close();
1328 
1329  return 0;
1330 }
1331 
1332 void printError(){
1333  printf("ERROR - Argument not expected or wrongly set:\n\n");
1334  printf("Valid format is: ./IBLCalibration THR=SCAN_Sxxxxxxxxx TOT_HISDIS=SCAN_Sxxxxxxxxx TOT_LOWQ=SCAN_Sxxxxxxxxx directory_path=path/to/file/\n");
1335  printf("\n\t i.e: ./IBLCalibration THR=SCAN_S000087719 TOT_HISDIS=SCAN_S000087717 TOT_LOWQ=SCAN_S000087710 directory_path=/eos/user/x/xxxx/\n");
1336 }
1337 
1338 int main(int argc, char *argv[]) {
1339 
1340  bool saveInfo = false;
1341  std::string THR = "THR";
1342  std::string TOT_HISDIS = "TOT_HISDIS";
1343  std::string TOT_LOWQ = "TOT_LOWQ";
1344  std::string dpath = "directory_path";
1345 
1346  for(int i=1; i<argc; i++){
1347  std::string aux(argv[i]);
1348  if (THR.compare(aux.substr(0,aux.find("="))) == 0) THR = aux.substr(aux.find("=")+1);
1349  else if(TOT_HISDIS.compare(aux.substr(0,aux.find("="))) == 0) TOT_HISDIS = aux.substr(aux.find("=")+1);
1350  else if(TOT_LOWQ.compare(aux.substr(0,aux.find("="))) == 0) TOT_LOWQ = aux.substr(aux.find("=")+1);
1351  else if(dpath.compare(aux.substr(0,aux.find("="))) == 0) dpath = aux.substr(aux.find("=")+1);
1352  else if(aux.compare("saveInfo") == 0) saveInfo = true;
1353  else{
1354  printError();
1355  return 1;
1356  }
1357  }
1358 
1359  printf("%-14s = %s\n","Directory path",dpath.c_str());
1360  printf("%-14s = %s.root\n","THR",THR.c_str());
1361  printf("%-14s = %s.root\n","TOT_HISDIS",TOT_HISDIS.c_str());
1362  printf("%-14s = %s.root\n","TOT_LOWQ",TOT_LOWQ.c_str());
1363  printf("%-14s = %s\n\n\n" ,"Save root file",saveInfo ? "True" : "False" );
1364 
1365  bool correctArgc = (THR.compare("THR") == 0) or (TOT_HISDIS.compare("TOT_HISDIS") == 0) or (TOT_LOWQ.compare("TOT_LOWQ") == 0) or (dpath.compare("directory_path") == 0);
1366 
1367  if(correctArgc){
1368  printf("Cannot continue, one arguments is incorrect or not filled correctly...\n");
1369  printf("Helper below:\n**********************\n\n");
1370  printError();
1371  return 1;
1372  }
1373 
1374  iblCalib(dpath, THR, TOT_HISDIS, TOT_LOWQ);
1375  return 0;
1376 
1377 }
1378 
query_example.row
row
Definition: query_example.py:24
printError
void printError()
Definition: CalibrateIBL.cxx:1332
athena.path
path
python interpreter configuration --------------------------------------—
Definition: athena.py:128
Merge_LAr_blobs.filled
list filled
Definition: Merge_LAr_blobs.py:43
PowhegControl_ttHplus_NLO.ss
ss
Definition: PowhegControl_ttHplus_NLO.py:83
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
PathResolver::find_file
static std::string find_file(const std::string &logical_file_name, const std::string &search_path, SearchType search_type=LocalSearch)
Definition: PathResolver.cxx:251
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
ParticleGun_SamplingFraction.bec
int bec
Definition: ParticleGun_SamplingFraction.py:89
dqt_zlumi_pandas.hname
string hname
Definition: dqt_zlumi_pandas.py:279
CaloCondBlobAlgs_fillNoiseFromASCII.spec
spec
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:47
PixelMapping.h
SH::scanDir
void scanDir(SampleHandler &sh, DiskList &list, const std::string &pattern, const std::string &samplePattern, const std::string &samplePostfix)
effects: scan the given directory and add all subdirectories as samples that contain root files.
Definition: ToolsDiscovery.cxx:45
ToTfromChrg
double ToTfromChrg(float chrg, double par[5])
Definition: CalibrateIBL.cxx:76
main
int main(int argc, char *argv[])
Definition: CalibrateIBL.cxx:1338
GetMean
TGraphErrors * GetMean(TH2F *histo)
Definition: TRTCalib_makeplots.cxx:3907
upper
int upper(int c)
Definition: LArBadChannelParser.cxx:49
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
drawFromPickle.exp
exp
Definition: drawFromPickle.py:36
python.TrigEgammaMonitorHelper.TH2F
def TH2F(name, title, nxbins, bins_par2, bins_par3, bins_par4, bins_par5=None, bins_par6=None, path='', **kwargs)
Definition: TrigEgammaMonitorHelper.py:45
x
#define x
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
RunTileCalibRec.run3
run3
Definition: RunTileCalibRec.py:208
calibdata.valid
list valid
Definition: calibdata.py:45
dqt_zlumi_pandas.err
err
Definition: dqt_zlumi_pandas.py:182
lumiFormat.i
int i
Definition: lumiFormat.py:85
LArCellNtuple.argv
argv
Definition: LArCellNtuple.py:152
TRT::Hit::layer
@ layer
Definition: HitInfo.h:79
chi2
double chi2(TH1 *h0, TH1 *h1)
Definition: comparitor.cxx:522
python.BuildSignatureFlags.sig
sig
Definition: BuildSignatureFlags.py:218
DQHistogramMergeRegExp.argc
argc
Definition: DQHistogramMergeRegExp.py:20
LB_AnalMapSplitter.tot
tot
Definition: LB_AnalMapSplitter.py:46
compute_lumi.denom
denom
Definition: compute_lumi.py:76
pix::PixelMapping
Definition: PixelMapping.h:18
trigbs_pickEvents.num
num
Definition: trigbs_pickEvents.py:76
PathResolver.h
createCoolChannelIdFile.par
par
Definition: createCoolChannelIdFile.py:29
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
funcDisp
double funcDisp(double *x, double *par)
Definition: CalibrateIBL.cxx:56
funcRation5
double funcRation5(double *x, double *par)
Definition: CalibrateIBL.cxx:86
ChrgfromToT
double ChrgfromToT(float tot, double par[10])
Definition: CalibrateIBL.cxx:63
query_example.col
col
Definition: query_example.py:7
PixelModuleFeMask_create_db.payload
string payload
Definition: PixelModuleFeMask_create_db.py:69
scan
void scan(TDirectory *td=0, int depth=0)
Definition: listroot.cxx:440
CaloCondBlobAlgs_fillNoiseFromASCII.hash
dictionary hash
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:109
ATLAS_NO_CHECK_FILE_THREAD_SAFETY
ATLAS_NO_CHECK_FILE_THREAD_SAFETY
Definition: CalibrateIBL.cxx:20
BchCleanup.modName
modName
Definition: BchCleanup.py:201
get
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition: hcg.cxx:127
LArNewCalib_DelayDump_OFC_Cali.idx
idx
Definition: LArNewCalib_DelayDump_OFC_Cali.py:69
pix::PixelMapping::mapping
void mapping(const std::string &geographicalID, int *hashID, int *bec, int *layer, int *phimod, int *etamod) const
Definition: PixelMapping.cxx:70
DiTauMassTools::MMCFitMethod::shortName
const std::string shortName[MAX]
Definition: PhysicsAnalysis/TauID/DiTauMassTools/DiTauMassTools/HelperFunctions.h:48
dqt_zlumi_alleff_HIST.tl
tl
Definition: dqt_zlumi_alleff_HIST.py:73
python.TrigEgammaMonitorHelper.TH1F
def TH1F(name, title, nxbins, bins_par2, bins_par3=None, path='', **kwargs)
Definition: TrigEgammaMonitorHelper.py:24
beamspotnt.nt
def nt
Definition: bin/beamspotnt.py:1063
skip
bool skip
Definition: TrigGlobEffCorrValidation.cxx:190
checker_macros.h
Define macros for attributes used to control the static checker.
TileDCSDataPlotter.tt
tt
Definition: TileDCSDataPlotter.py:874
python.compressB64.c
def c
Definition: compressB64.py:93
funcDispConvoluted
double funcDispConvoluted(double *x, double *par)
Definition: CalibrateIBL.cxx:96
iblCalib
int iblCalib(const std::string &InDir, const std::string &THRscan, const std::string &scan, const std::string &scanLowCharge)
Definition: CalibrateIBL.cxx:103