pxar
 All Classes Namespaces Functions Variables Typedefs Friends
PixTestTrim.cc
1 #include <stdlib.h> /* atof, atoi */
2 #include <algorithm> // std::find
3 #include <iostream>
4 #include <fstream>
5 
6 #include <TH1.h>
7 #include <TRandom.h>
8 #include <TStopwatch.h>
9 
10 #include "PixTestTrim.hh"
11 #include "PixUtil.hh"
12 #include "log.h"
13 #include "helper.h"
14 
15 
16 using namespace std;
17 using namespace pxar;
18 
19 ClassImp(PixTestTrim)
20 
21 // ----------------------------------------------------------------------
22 PixTestTrim::PixTestTrim(PixSetup *a, std::string name) : PixTest(a, name), fParVcal(-1), fParNtrig(-1) {
23  PixTest::init();
24  init();
25  // LOG(logINFO) << "PixTestTrim ctor(PixSetup &a, string, TGTab *)";
26 }
27 
28 
29 //----------------------------------------------------------
30 PixTestTrim::PixTestTrim() : PixTest() {
31  // LOG(logINFO) << "PixTestTrim ctor()";
32 }
33 
34 // ----------------------------------------------------------------------
35 bool PixTestTrim::setParameter(string parName, string sval) {
36  bool found(false);
37  std::transform(parName.begin(), parName.end(), parName.begin(), ::tolower);
38  for (unsigned int i = 0; i < fParameters.size(); ++i) {
39  if (fParameters[i].first == parName) {
40  found = true;
41  sval.erase(remove(sval.begin(), sval.end(), ' '), sval.end());
42  if (!parName.compare("ntrig")) {
43  fParNtrig = atoi(sval.c_str());
44  }
45  if (!parName.compare("vcal")) {
46  fParVcal = atoi(sval.c_str());
47  }
48  break;
49  }
50  }
51 
52  return found;
53 }
54 
55 
56 // ----------------------------------------------------------------------
57 void PixTestTrim::init() {
58  setToolTips();
59  fDirectory = gFile->GetDirectory(fName.c_str());
60  if (!fDirectory) {
61  fDirectory = gFile->mkdir(fName.c_str());
62  }
63  fDirectory->cd();
64 
65 }
66 
67 
68 // ----------------------------------------------------------------------
69 void PixTestTrim::runCommand(std::string command) {
70  std::transform(command.begin(), command.end(), command.begin(), ::tolower);
71  LOG(logDEBUG) << "running command: " << command;
72  if (!command.compare("trimbits")) {
73  trimBitTest();
74  return;
75  }
76  if (!command.compare("trim")) {
77  trimTest();
78  return;
79  }
80  LOG(logDEBUG) << "did not find command ->" << command << "<-";
81 }
82 
83 
84 // ----------------------------------------------------------------------
86  fTestTip = string(Form("trimming results in a uniform in-time threshold\n")
87  + string("TO BE FINISHED!!"))
88  ;
89  fSummaryTip = string("summary plot to be implemented")
90  ;
91 }
92 
93 // ----------------------------------------------------------------------
94 void PixTestTrim::bookHist(string name) {
95  fDirectory->cd();
96 
97  LOG(logDEBUG) << "nothing done with " << name;
98 }
99 
100 
101 //----------------------------------------------------------
102 PixTestTrim::~PixTestTrim() {
103  LOG(logDEBUG) << "PixTestTrim dtor";
104 }
105 
106 
107 // ----------------------------------------------------------------------
109 
110  TStopwatch t;
111 
112  fDirectory->cd();
113  PixTest::update();
114  bigBanner(Form("PixTestTrim::doTest()"));
115 
116  fProblem = false;
117  trimTest();
118  if (fProblem) {
119  LOG(logINFO) << "PixTestTrim::doTest() aborted because of problem ";
120  return;
121  }
122  TH1 *h1 = (*fDisplayedHist);
123  h1->Draw(getHistOption(h1).c_str());
124  PixTest::update();
125 
126  fProblem = false;
127  trimBitTest();
128  if (fProblem) {
129  LOG(logINFO) << "PixTestTrim::doTest() aborted because of problem ";
130  return;
131  }
132  h1 = (*fDisplayedHist);
133  h1->Draw(getHistOption(h1).c_str());
134  PixTest::update();
135 
136  int seconds = t.RealTime();
137  LOG(logINFO) << "PixTestTrim::doTest() done, duration: " << seconds << " seconds";
138 }
139 
140 // ----------------------------------------------------------------------
141 void PixTestTrim::trimTest() {
142 
143  bool verbose(false);
144  cacheDacs(verbose);
145  fDirectory->cd();
146  PixTest::update();
147  banner(Form("PixTestTrim::trimTest() ntrig = %d, vcal = %d", fParNtrig, fParVcal));
148 
149  fApi->_dut->testAllPixels(true);
150  fApi->_dut->maskAllPixels(false);
151 
152  vector<uint8_t> rocIds = fApi->_dut->getEnabledRocIDs();
153 
154  fApi->setDAC("Vtrim", 0);
155  fApi->setDAC("ctrlreg", 0);
156  fApi->setDAC("Vcal", fParVcal);
157 
158  setTrimBits(15);
159 
160  // -- determine minimal VthrComp
161  int NTRIG(5);
162  map<int, int> rocVthrComp;
163  print("VthrComp thr map (minimal VthrComp)");
164  vector<TH1*> thr0 = scurveMaps("vthrcomp", "TrimThr0", NTRIG, 0, 159, 40, 7);
165  PixTest::update();
166  if (thr0.size()/3 != rocIds.size()) {
167  LOG(logERROR) << "scurve map size " << thr0.size() << " does not agree with number of enabled ROCs " << rocIds.size();
168  fProblem = true;
169  return;
170  }
171  vector<int> minVthrComp = getMinimumVthrComp(thr0, 10, 2.);
172 
173  TH2D* h2(0);
174  for (unsigned int iroc = 0; iroc < rocIds.size(); ++iroc) {
175  LOG(logINFO) << "ROC " << static_cast<int>(rocIds[iroc]) << " VthrComp = " << minVthrComp[iroc];
176  fApi->setDAC("VthrComp", static_cast<uint8_t>(minVthrComp[iroc]), rocIds[iroc]);
177  rocVthrComp.insert(make_pair(rocIds[iroc], minVthrComp[iroc]));
178  }
179 
180  // -- determine pixel with largest VCAL threshold
181  print("Vcal thr map (pixel with maximum Vcal thr)");
182  vector<TH1*> thr1 = scurveMaps("vcal", "TrimThr1", NTRIG, 0, 159, -1, 1);
183  PixTest::update();
184  if (thr1.size() != rocIds.size()) {
185  LOG(logERROR) << "scurve map size " << thr1.size() << " does not agree with number of enabled ROCs " << rocIds.size() << endl;
186  fProblem = true;
187  return;
188  }
189 
190  vector<int> Vcal;
191  fApi->_dut->testAllPixels(false);
192  fApi->_dut->maskAllPixels(true);
193  double vcalMean(-1), vcalMin(999.), vcalMax(-1.);
194  string hname("");
195  string::size_type s1, s2;
196  int rocid(-1);
197  double NSIGMA(3);
198  map<string, TH2D*> maps;
199  for (unsigned int i = 0; i < thr1.size(); ++i) {
200  h2 = (TH2D*)thr1[i];
201  hname = h2->GetName();
202 
203  TH1* d1 = distribution(h2, 256, 0., 256.);
204  vcalMean = d1->GetMean();
205  vcalMin = d1->GetMean() - NSIGMA*d1->GetRMS();
206  if (vcalMin < 0) vcalMin = 0;
207  vcalMax = d1->GetMean() + NSIGMA*d1->GetRMS();
208  if (vcalMax > 255) vcalMin = 255;
209  delete d1;
210 
211  s1 = hname.rfind("_C");
212  s2 = hname.rfind("_V");
213  rocid = atoi(hname.substr(s1+2, s2-s1-2).c_str());
214  double maxVcal(-1.), vcal(0.);
215  int ix(-1), iy(-1);
216  for (int ic = 0; ic < h2->GetNbinsX(); ++ic) {
217  for (int ir = 0; ir < h2->GetNbinsY(); ++ir) {
218  vcal = h2->GetBinContent(ic+1, ir+1);
219  if (vcal > vcalMin && vcal > maxVcal && vcal < vcalMax) {
220  maxVcal = vcal;
221  ix = ic;
222  iy = ir;
223  }
224  }
225  }
226 
227  LOG(logINFO) << " roc " << getIdxFromId(rocid) << " with ID = " << rocid
228  << " has maximal Vcal " << maxVcal << " for pixel " << ix << "/" << iy
229  << " mean/min/max = " << vcalMean << "/" << vcalMin << "/" <<vcalMax;
230  Vcal.push_back(static_cast<int>(maxVcal));
231 
232  fTrimBits[getIdxFromId(rocid)][ix][iy] = 0;
233  //?? fTrimBits[getIdxFromId(rocid)][ix][iy] = 1;
234 
235  fApi->_dut->testPixel(ix, iy, true, rocid);
236  fApi->_dut->maskPixel(ix, iy, false, rocid);
237 
238  h2 = bookTH2D(Form("trim_VCAL_VTRIM_C%d", rocid),
239  Form("trim_VCAL_VTRIM_c%d_r%d_C%d", ix, iy, rocid),
240  256, 0., 256., 256, 0., 256.);
241  h2->SetMinimum(0.);
242  setTitles(h2, "vcal", "vtrim");
243  fHistList.push_back(h2);
244  fHistOptions.insert(make_pair(h2, "colz"));
245  maps.insert(make_pair(Form("trim_VCAL_VTRIM_C%d", rocid), h2));
246  }
247  setTrimBits();
248 
249  // -- determine VTRIM with this pixel
250  int cnt(0);
251  bool done(false);
252  vector<pair<uint8_t, pair<uint8_t, vector<pixel> > > > results;
253  while (!done) {
254  try {
255  results = fApi->getEfficiencyVsDACDAC("vcal", 0, 200, "vtrim", 0, 255, FLAG_FORCE_MASKED, NTRIG);
256  done = true;
257  } catch(pxarException &e) {
258  LOG(logCRITICAL) << "pXar execption: "<< e.what();
259  fNDaqErrors = 666667;
260  ++cnt;
261  }
262  done = (cnt>2) || done;
263  }
264  PixTest::update();
265 
266  for (unsigned int i = 0; i < results.size(); ++i) {
267  pair<uint8_t, pair<uint8_t, vector<pixel> > > v = results[i];
268  int idac1 = v.first;
269  pair<uint8_t, vector<pixel> > w = v.second;
270  int idac2 = w.first;
271  vector<pixel> wpix = w.second;
272 
273  for (unsigned ipix = 0; ipix < wpix.size(); ++ipix) {
274  h2 = maps[Form("trim_VCAL_VTRIM_C%d", wpix[ipix].roc())];
275  if (h2) {
276  h2->Fill(idac1, idac2, wpix[ipix].value());
277  } else {
278  LOG(logDEBUG) << "wrong pixel decoded";
279  }
280  }
281  }
282 
283  map<int, int> rocTrim;
284  int vtrim(0);
285  for (unsigned int iroc = 0; iroc < rocIds.size(); ++iroc){
286  h2 = maps[Form("trim_VCAL_VTRIM_C%d", rocIds[iroc])];
287  TH1D *hy = h2->ProjectionY("_py", 150, 150);
288  double thresh = hy->FindLastBinAbove(0.5*NTRIG);
289  delete hy;
290  vtrim = static_cast<int>(thresh);
291  double oldThr(99.);
292  for (int itrim = vtrim-20; itrim > 0; --itrim) {
293  TH1D *hx = h2->ProjectionX("_px", itrim, itrim);
294  threshold(hx);
295  delete hx;
296  if (fThreshold > fParVcal) {
297  if (TMath::Abs(fParVcal - fThreshold) < TMath::Abs(fParVcal - oldThr)) {
298  rocTrim.insert(make_pair(rocIds[iroc], itrim));
299  fApi->setDAC("vtrim", itrim, rocIds[iroc]);
300  LOG(logDEBUG) << " vtrim: vcal = " << fThreshold << " < " << fParVcal << " for itrim = " << itrim << "; old thr = " << oldThr
301  << " ... break";
302  } else {
303  rocTrim.insert(make_pair(rocIds[iroc], itrim+1));
304  fApi->setDAC("vtrim", itrim+1, rocIds[iroc]);
305  LOG(logDEBUG) << "vtrim: vcal = " << fThreshold << " < " << fParVcal << " for itrim+1 = " << itrim+1 << "; old thr = " << oldThr
306  << " ... break";
307  }
308  break;
309  } else {
310  oldThr = fThreshold;
311  }
312  }
313  }
314 
315  fApi->_dut->testAllPixels(true);
316  fApi->_dut->maskAllPixels(false);
317 
318  for (unsigned int iroc = 0; iroc < rocIds.size(); ++iroc) {
319  for (int ix = 0; ix < 52; ++ix) {
320  for (int iy = 0; iy < 80; ++iy) {
321  fTrimBits[iroc][ix][iy] = 7;
322  }
323  }
324  }
325  setTrimBits();
326  PixTest::update();
327 
328  // -- set trim bits
329  int correction = 4;
330  vector<TH1*> thr2 = scurveMaps("vcal", "TrimThr2", fParNtrig, 0, 199, -1, 1);
331  if (thr2.size() != rocIds.size()) {
332  LOG(logERROR) << "scurve map thr2 size " << thr2.size() << " does not agree with number of enabled ROCs " << rocIds.size();
333  fProblem = true;
334  return;
335  }
336  double maxthr = getMaximumThreshold(thr2);
337  double minthr = getMinimumThreshold(thr2);
338  print(Form("TrimStepCorr4 extremal thresholds: %f .. %f", minthr, maxthr));
339  if (maxthr < 245) maxthr += 10;
340  if (minthr > 10) minthr -= 10;
341  vector<TH1*> thr2a = trimStep("trimStepCorr4", correction, thr2, static_cast<int>(minthr), static_cast<int>(maxthr));
342  PixTest::update();
343  if (thr2a.size() != rocIds.size()) {
344  LOG(logERROR) << "scurve map thr2a size " << thr2a.size() << " does not agree with number of enabled ROCs " << rocIds.size();
345  fProblem = true;
346  return;
347  }
348 
349 
350  correction = 2;
351  maxthr = getMaximumThreshold(thr2a);
352  minthr = getMinimumThreshold(thr2a);
353  print(Form("TrimStepCorr2 extremal thresholds: %f .. %f", minthr, maxthr));
354  if (maxthr < 245) maxthr += 10;
355  if (minthr > 10) minthr -= 10;
356  vector<TH1*> thr3a = trimStep("trimStepCorr2", correction, thr2a, static_cast<int>(minthr), static_cast<int>(maxthr));
357  PixTest::update();
358  if (thr3a.size() != rocIds.size()) {
359  LOG(logERROR) << "scurve map thr3a size " << thr3a.size() << " does not agree with number of enabled ROCs " << rocIds.size();
360  fProblem = true;
361  return;
362  }
363 
364  correction = 1;
365  maxthr = getMaximumThreshold(thr3a);
366  minthr = getMinimumThreshold(thr3a);
367  print(Form("TrimStepCorr1a extremal thresholds: %f .. %f", minthr, maxthr));
368  if (maxthr < 245) maxthr += 10;
369  if (minthr > 10) minthr -= 10;
370  vector<TH1*> thr4a = trimStep("trimStepCorr1a", correction, thr3a, static_cast<int>(minthr), static_cast<int>(maxthr));
371  PixTest::update();
372  if (thr4a.size() != rocIds.size()) {
373  LOG(logERROR) << "scurve map thr4a size " << thr4a.size() << " does not agree with number of enabled ROCs " << rocIds.size();
374  fProblem = true;
375  return;
376  }
377 
378  correction = 1;
379  maxthr = getMaximumThreshold(thr4a);
380  minthr = getMinimumThreshold(thr4a);
381  print(Form("TrimStepCorr1b extremal thresholds: %f .. %f", minthr, maxthr));
382  if (maxthr < 245) maxthr += 10;
383  if (minthr > 10) minthr -= 10;
384  vector<TH1*> thr5a = trimStep("trimStepCorr1b", correction, thr4a, static_cast<int>(minthr), static_cast<int>(maxthr));
385  PixTest::update();
386  if (thr5a.size() != rocIds.size()) {
387  LOG(logERROR) << "scurve map thr5a size " << thr5a.size() << " does not agree with number of enabled ROCs " << rocIds.size();
388  fProblem = true;
389  return;
390  }
391 
392  // -- create trimMap
393  string trimbitsMeanString(""), trimbitsRmsString("");
394  for (unsigned int i = 0; i < thr5a.size(); ++i) {
395  h2 = bookTH2D(Form("TrimMap_C%d", i),
396  Form("TrimMap_C%d", i),
397  52, 0., 52., 80, 0., 80.);
398  for (int ix = 0; ix < 52; ++ix) {
399  for (int iy = 0; iy < 80; ++iy) {
400  h2->SetBinContent(ix+1, iy+1, fTrimBits[i][ix][iy]);
401  }
402  }
403 
404  fHistList.push_back(h2);
405  fHistOptions.insert(make_pair(h2, "colz"));
406 
407  TH1* d1 = distribution(h2, 16, 0., 16.);
408 
409  trimbitsMeanString += Form("%6.2f ", d1->GetMean());
410  trimbitsRmsString += Form("%6.2f ", d1->GetRMS());
411 
412  fHistList.push_back(d1);
413  }
414 
415  print(Form("TrimThrFinal extremal thresholds: %d .. %d", fParVcal-20, fParVcal+20));
416  vector<TH1*> thrF = scurveMaps("vcal", "TrimThrFinal", fParNtrig, fParVcal-20, fParVcal+20, -1, 9);
417  PixTest::update();
418  string trimMeanString, trimRmsString;
419  for (unsigned int i = 0; i < thrF.size(); ++i) {
420  hname = thrF[i]->GetName();
421  // -- skip sig_ and thn_ histograms
422  if (string::npos == hname.find("dist_thr_")) continue;
423  trimMeanString += Form("%6.2f ", thrF[i]->GetMean());
424  trimRmsString += Form("%6.2f ", thrF[i]->GetRMS());
425  }
426 
427 
428  TH1 *h1 = (*fDisplayedHist);
429  h1->Draw(getHistOption(h1).c_str());
430  PixTest::update();
431  restoreDacs();
432  string vtrimString, vthrcompString;
433  for (unsigned int iroc = 0; iroc < rocIds.size(); ++iroc) {
434  fApi->setDAC("vtrim", rocTrim[rocIds[iroc]], rocIds[iroc]);
435  vtrimString += Form("%3d ", rocTrim[rocIds[iroc]]);
436  fApi->setDAC("vthrcomp", rocVthrComp[rocIds[iroc]], rocIds[iroc]);
437  vthrcompString += Form("%3d ", rocVthrComp[rocIds[iroc]]);
438  }
439 
440  // -- save into files
441  fPixSetup->getConfigParameters()->setTrimVcalSuffix(Form("%d", fParVcal), true);
442  saveDacs();
443  saveTrimBits();
444 
445  // -- summary printout
446  LOG(logINFO) << "PixTestTrim::trimTest() done";
447  LOG(logINFO) << "vtrim: " << vtrimString;
448  LOG(logINFO) << "vthrcomp: " << vthrcompString;
449  LOG(logINFO) << "vcal mean: " << trimMeanString;
450  LOG(logINFO) << "vcal RMS: " << trimRmsString;
451  LOG(logINFO) << "bits mean: " << trimbitsMeanString;
452  LOG(logINFO) << "bits RMS: " << trimbitsRmsString;
453 }
454 
455 
456 // ----------------------------------------------------------------------
457 void PixTestTrim::trimBitTest() {
458 
459  cacheDacs();
460 
461  vector<uint8_t> rocIds = fApi->_dut->getEnabledRocIDs();
462  unsigned int nrocs = rocIds.size();
463 
464  // -- cache trim bits
465  for (unsigned int i = 0; i < nrocs; ++i) {
466  vector<pixelConfig> pix = fApi->_dut->getEnabledPixels(rocIds[i]);
467  int ix(-1), iy(-1);
468  for (unsigned int ipix = 0; ipix < pix.size(); ++ipix) {
469  ix = pix[ipix].column();
470  iy = pix[ipix].row();
471  fTrimBits[i][ix][iy] = pix[ipix].trim();
472  }
473  }
474 
475  vector<int>vtrim;
476  vtrim.push_back(255);
477  vtrim.push_back(240);
478  vtrim.push_back(150);
479  vtrim.push_back(100);
480 
481  vector<int>btrim;
482  btrim.push_back(14);
483  btrim.push_back(13);
484  btrim.push_back(11);
485  btrim.push_back(7);
486 
487  fDirectory->cd();
488  PixTest::update();
489  banner(Form("PixTestTrim::trimBitTest() ntrig = %d, vtrims = %d %d %d %d",
490  fParNtrig, vtrim[0], vtrim[1], vtrim[2], vtrim[3]));
491 
492  fApi->_dut->testAllPixels(true);
493  fApi->_dut->maskAllPixels(false);
494 
495  vector<vector<TH1*> > steps;
496 
497  ConfigParameters *cp = fPixSetup->getConfigParameters();
498  // -- start untrimmed
499  bool ok = cp->setTrimBits(15);
500  if (!ok) {
501  LOG(logWARNING) << "could not set trim bits to " << 15;
502  fProblem = true;
503  return;
504  }
505  fApi->setDAC("CtrlReg", 0);
506  fApi->setDAC("Vtrim", 0);
507  LOG(logDEBUG) << "trimBitTest determine threshold map without trims ";
508  vector<TH1*> thr0 = mapsWithString(scurveMaps("Vcal", "TrimBitsThr0", fParNtrig, 0, 199, -1, 7), "thr");
509 
510  // -- now loop over all trim bits
511  vector<TH1*> thr;
512  double maxThr = getMaximumThreshold(thr0);
513  if (maxThr > 245.) maxThr = 245.;
514  for (unsigned int iv = 0; iv < vtrim.size(); ++iv) {
515  thr.clear();
516  ok = cp->setTrimBits(btrim[iv]);
517  if (!ok) {
518  LOG(logWARNING) << "could not set trim bits to " << btrim[iv];
519  continue;
520  }
521 
522  LOG(logDEBUG) << "trimBitTest initDUT with trim bits = " << btrim[iv];
523  for (vector<uint8_t>::size_type iroc = 0; iroc < rocIds.size(); ++iroc) {
524  fApi->_dut->updateTrimBits(cp->getRocPixelConfig(rocIds[iroc]), rocIds[iroc]);
525  }
526 
527  fApi->setDAC("Vtrim", vtrim[iv]);
528  LOG(logDEBUG) << "trimBitTest threshold map with trim = " << btrim[iv];
529  thr = mapsWithString(scurveMaps("Vcal", Form("TrimThr_trim%d", btrim[iv]), fParNtrig, 0, static_cast<int>(maxThr)+10, -1, 7), "thr");
530  maxThr = getMaximumThreshold(thr);
531  if (maxThr > 245.) maxThr = 245.;
532  steps.push_back(thr);
533  }
534 
535  // -- and now determine threshild difference
536  TH1 *h1(0);
537  double dthr(0.);
538  for (unsigned int i = 0; i < steps.size(); ++i) {
539  thr = steps[i];
540  for (unsigned int iroc = 0; iroc < thr.size(); ++iroc) {
541  h1 = bookTH1D(Form("TrimBit%d_C%d", btrim[i], rocIds[iroc]), Form("TrimBit%d_C%d", btrim[i], rocIds[iroc]), 256, 0., 256);
542  for (int ix = 0; ix < 52; ++ix) {
543  for (int iy = 0; iy < 80; ++iy) {
544  dthr = thr0[iroc]->GetBinContent(ix+1, iy+1) - thr[iroc]->GetBinContent(ix+1, iy+1);
545  h1->Fill(dthr);
546  }
547  }
548  fHistList.push_back(h1);
549  }
550  }
551 
552  fDisplayedHist = find(fHistList.begin(), fHistList.end(), h1);
553 
554  if (h1) h1->Draw();
555  PixTest::update();
556  restoreDacs();
557  setTrimBits();
558  LOG(logINFO) << "PixTestTrim::trimBitTest() done ";
559 
560 }
561 
562 
563 
564 // ----------------------------------------------------------------------
565 int PixTestTrim::adjustVtrim() {
566  int vtrim = -1;
567  int thr(255), thrOld(255);
568  int ntrig(10);
569  do {
570  vtrim++;
571  fApi->setDAC("Vtrim", vtrim);
572  thrOld = thr;
573  thr = pixelThreshold("Vcal", ntrig, 0, 100);
574  LOG(logDEBUG) << vtrim << " thr " << thr;
575  }
576  while (((thr > fParVcal) || (thrOld > fParVcal) || (thr < 10)) && (vtrim < 200));
577  vtrim += 5;
578  fApi->setDAC("Vtrim", vtrim);
579  LOG(logINFO) << "Vtrim set to " << vtrim;
580  return vtrim;
581 }
582 
583 
584 // ----------------------------------------------------------------------
585 vector<TH1*> PixTestTrim::trimStep(string name, int correction, vector<TH1*> calOld, int vcalMin, int vcalMax) {
586 
587  int NTRIG(4);
588 
589  if (vcalMin < 0) vcalMin = 0;
590  if (vcalMin > 255) vcalMax = 255;
591 
592  int trim(1);
593  int trimBitsOld[16][52][80];
594  for (unsigned int i = 0; i < calOld.size(); ++i) {
595  for (int ix = 0; ix < 52; ++ix) {
596  for (int iy = 0; iy < 80; ++iy) {
597  trimBitsOld[i][ix][iy] = fTrimBits[i][ix][iy];
598  if (calOld[i]->GetBinContent(ix+1, iy+1) > fParVcal) {
599  trim = fTrimBits[i][ix][iy] - correction;
600  } else {
601  trim = fTrimBits[i][ix][iy] + correction;
602  }
603  if (trim < 1) trim = 1;
604  if (trim > 15) trim = 15;
605  fTrimBits[i][ix][iy] = trim;
606  }
607  }
608  }
609 
610  setTrimBits();
611  vector<TH1*> calNew = scurveMaps("vcal", name, NTRIG, vcalMin, vcalMax, -1, 1);
612  if (calNew.size() != calOld.size()) {
613  LOG(logERROR) << "scurve map size " << calNew.size() << " does not agree with previous number " << calOld.size();
614  fProblem = true;
615  return calNew;
616  }
617 
618  // -- check that things got better, else revert and leave up to next correction round
619  for (unsigned int i = 0; i < calOld.size(); ++i) {
620  for (int ix = 0; ix < 52; ++ix) {
621  for (int iy = 0; iy < 80; ++iy) {
622  if (TMath::Abs(calOld[i]->GetBinContent(ix+1, iy+1) - fParVcal) < TMath::Abs(calNew[i]->GetBinContent(ix+1, iy+1) - fParVcal)) {
623  trim = trimBitsOld[i][ix][iy];
624  calNew[i]->SetBinContent(ix+1, iy+1, calOld[i]->GetBinContent(ix+1, iy+1));
625  } else {
626  trim = fTrimBits[i][ix][iy];
627  }
628  fTrimBits[i][ix][iy] = trim;
629  }
630  }
631  }
632  setTrimBits();
633 
634  return calNew;
635 }
636 
637 
638 // ----------------------------------------------------------------------
639 void PixTestTrim::setTrimBits(int itrim) {
640  vector<uint8_t> rocIds = fApi->_dut->getEnabledRocIDs();
641  for (unsigned int ir = 0; ir < rocIds.size(); ++ir){
642  vector<pixelConfig> pix = fApi->_dut->getEnabledPixels(rocIds[ir]);
643  for (unsigned int ipix = 0; ipix < pix.size(); ++ipix) {
644  if (itrim > -1) {
645  fTrimBits[ir][pix[ipix].column()][pix[ipix].row()] = itrim;
646  }
647  fApi->_dut->updateTrimBits(pix[ipix].column(), pix[ipix].row(), fTrimBits[ir][pix[ipix].column()][pix[ipix].row()], rocIds[ir]);
648  }
649  }
650 }
651 
std::map< TH1 *, std::string > fHistOptions
options can be stored with each histogram
Definition: PixTest.hh:308
bool setDAC(std::string dacName, uint8_t dacValue, uint8_t rocI2C)
Definition: api.cc:546
PixSetup * fPixSetup
all necessary stuff in one place
Definition: PixTest.hh:290
void setToolTips()
implement this to provide updated tool tips if the user changes test parameters
Definition: PixTestTrim.cc:85
void doTest()
function connected to "DoTest" button of PixTab
Definition: PixTestTrim.cc:108
std::vector< TH1 * > mapsWithString(std::vector< TH1 * >, std::string name)
return a list of TH* that have 'name' as part to their histogram name
void testPixel(uint8_t column, uint8_t row, bool enable)
Definition: dut.cc:432
std::vector< std::pair< std::string, std::string > > fParameters
the parameters of this test
Definition: PixTest.hh:302
int getIdxFromId(int id)
provide the mapping between ROC index and ID
Definition: PixTest.cc:1124
void maskAllPixels(bool mask, uint8_t rocid)
Definition: dut.cc:481
std::vector< int > getMinimumVthrComp(std::vector< TH1 * >, int reserve=10, double nsigma=3.)
minimum allowable VthrComp; reserve indicate the separation from the minimum VthrComp where noise set...
Definition: PixTest.cc:1033
bool updateTrimBits(std::vector< pixelConfig > trimming, uint8_t rocid)
Definition: dut.cc:551
TDirectory * fDirectory
where the root histograms will end up
Definition: PixTest.hh:306
void restoreDacs(bool verbose=false)
restore all DACs
Definition: PixTest.cc:771
dut * _dut
Definition: api.h:728
TH1D * distribution(TH2D *, int nbins, double xmin, double xmax)
creates a 1D distribution of a map
Definition: PixTest.cc:748
std::vector< uint8_t > getEnabledRocIDs()
Definition: dut.cc:214
void testAllPixels(bool enable)
Definition: dut.cc:503
std::list< TH1 * >::iterator fDisplayedHist
pointer to the histogram currently displayed
Definition: PixTest.hh:309
virtual bool setParameter(std::string parName, std::string sval)
set the string value of a parameter
Definition: PixTestTrim.cc:35
TH1D * bookTH1D(std::string sname, std::string title, int nbins, double xmin, double xmax)
book a TH1D, adding version information to the name and title
Definition: PixTest.cc:895
virtual std::string getHistOption(TH1 *)
get the hist display options (if stored in fHistOptions)
Definition: PixTest.cc:941
void cacheDacs(bool verbose=false)
cache all DACs
Definition: PixTest.cc:760
std::list< TH1 * > fHistList
list of histograms available in PixTab::next and PixTab::previous
Definition: PixTest.hh:307
void setTitles(TH1 *h, const char *sx, const char *sy, float size=0.05, float xoff=1.1, float yoff=1.1, float lsize=0.05, int font=42)
utility to set histogram titles
Definition: PixTest.cc:649
std::vector< std::pair< uint8_t, std::pair< uint8_t, std::vector< pixel > > > > getEfficiencyVsDACDAC(std::string dac1name, uint8_t dac1min, uint8_t dac1max, std::string dac2name, uint8_t dac2min, uint8_t dac2max, uint16_t flags, uint16_t nTriggers)
Definition: api.cc:951
void print(std::string, pxar::TLogLevel log=pxar::logINFO)
produce eye-catching printouts
Definition: PixTest.cc:1191
std::vector< pixelConfig > getEnabledPixels(size_t rocid)
Definition: dut.cc:124
double getMinimumThreshold(std::vector< TH1 * >)
return minimum threshold in a set of maps
Definition: PixTest.cc:1084
void saveDacs()
save DACs to file
Definition: PixTest.cc:1511
void saveTrimBits()
save trim bits to file
Definition: PixTest.cc:1516
TH2D * bookTH2D(std::string sname, std::string title, int nbinsx, double xmin, double xmax, int nbinsy, double ymin, double max)
book a TH2D, adding version information to the name and title
Definition: PixTest.cc:903
void maskPixel(uint8_t column, uint8_t row, bool mask)
Definition: dut.cc:397
void update()
signal to PixTab to update the canvas
Definition: PixTest.cc:569
int pixelThreshold(std::string dac, int ntrig, int dacmin, int dacmax)
work-around to cope with suboptimal pxar/core
Definition: PixTest.cc:142
pxar::pxarCore * fApi
pointer to the API
Definition: PixTest.hh:289
bool threshold(TH1 *)
fit an s-curve to a distribution. Fills fThreshold, fThresholdE, fSigma, fSigmaE
Definition: PixTest.cc:699
std::vector< TH1 * > scurveMaps(std::string dac, std::string name, int ntrig=10, int daclo=0, int dachi=255, int dacsperstep=-1, int result=15, int ihit=1, int flag=FLAG_FORCE_MASKED)
Definition: PixTest.cc:177
void init()
sets all test parameters
Definition: PixTest.cc:62
double getMaximumThreshold(std::vector< TH1 * >)
return maximum threshold in a set of maps
Definition: PixTest.cc:1101
void runCommand(std::string)
allow execution of any button in the test
Definition: PixTestTrim.cc:69