pxar
 All Classes Namespaces Functions Variables Typedefs Friends
PixTestXray.cc
1 #include <stdlib.h> /* atof, atoi,itoa */
2 #include <algorithm> // std::find
3 #include <iostream>
4 #include <fstream>
5 
6 #include "PixTestXray.hh"
7 #include "log.h"
8 #include "TStopwatch.h"
9 
10 #include "PixUtil.hh"
11 
12 #include <TH2.h>
13 #include <TMath.h>
14 
15 using namespace std;
16 using namespace pxar;
17 
18 ClassImp(PixTestXray)
19 
20 // ----------------------------------------------------------------------
21 PixTestXray::PixTestXray(PixSetup *a, std::string name) : PixTest(a, name),
22  fParSource("nada"), fParMaskFileName("default"), fParTriggerFrequency(0), fParRunSeconds(0), fParStepSeconds(0),
23  fParVthrCompMin(0), fParVthrCompMax(0), fParFillTree(false), fParDelayTBM(false), fParSaveMaskedPixels(0), fSourceChanged(false) {
24  PixTest::init();
25  init();
26  LOG(logDEBUG) << "PixTestXray ctor(PixSetup &a, string, TGTab *)";
27 
28  fTree = 0;
29  fPhCal.setPHParameters(fPixSetup->getConfigParameters()->getGainPedestalParameters());
30  fPhCalOK = fPhCal.initialized();
31 
32 }
33 
34 
35 //----------------------------------------------------------
36 PixTestXray::PixTestXray() : PixTest() {
37  LOG(logDEBUG) << "PixTestXray ctor()";
38  fTree = 0;
39 }
40 
41 
42 // ----------------------------------------------------------------------
43 bool PixTestXray::setParameter(string parName, string sval) {
44  bool found(false);
45  std::transform(parName.begin(), parName.end(), parName.begin(), ::tolower);
46  for (unsigned int i = 0; i < fParameters.size(); ++i) {
47  if (fParameters[i].first == parName) {
48  found = true;
49  if (!parName.compare("savemaskfile")) {
50  PixUtil::replaceAll(sval, "checkbox(", "");
51  PixUtil::replaceAll(sval, ")", "");
52  fParSaveMaskedPixels = !(atoi(sval.c_str())==0);
53  setToolTips();
54  }
55  if (!parName.compare("maskfilename")) {
56  fParMaskFileName = sval;
57  setToolTips();
58  }
59  if (!parName.compare("source")) {
60  fParSource = sval;
61  fSourceChanged = true;
62  setToolTips();
63  }
64  if (!parName.compare("vthrcompmin")) {
65  fParVthrCompMin = atoi(sval.c_str());
66  setToolTips();
67  }
68  if (!parName.compare("vthrcompmax")) {
69  fParVthrCompMax = atoi(sval.c_str());
70  setToolTips();
71  }
72  if (!parName.compare("trgfrequency(khz)")) {
73  fParTriggerFrequency = atoi(sval.c_str());
74  setToolTips();
75  }
76  if (!parName.compare("runseconds")) {
77  fParRunSeconds = atoi(sval.c_str());
78  setToolTips();
79  }
80  if (!parName.compare("stepseconds")) {
81  fParStepSeconds = atoi(sval.c_str());
82  setToolTips();
83  }
84  if (!parName.compare("delaytbm")) {
85  PixUtil::replaceAll(sval, "checkbox(", "");
86  PixUtil::replaceAll(sval, ")", "");
87  fParDelayTBM = !(atoi(sval.c_str())==0);
88  setToolTips();
89  }
90  if (!parName.compare("filltree")) {
91  PixUtil::replaceAll(sval, "checkbox(", "");
92  PixUtil::replaceAll(sval, ")", "");
93  fParFillTree = !(atoi(sval.c_str())==0);
94  setToolTips();
95  }
96  break;
97  }
98  }
99  return found;
100 }
101 
102 
103 // ----------------------------------------------------------------------
104 void PixTestXray::runCommand(std::string command) {
105  std::transform(command.begin(), command.end(), command.begin(), ::tolower);
106  LOG(logDEBUG) << "running command: " << command;
107 
108  if (!command.compare("stop")){
109  doStop();
110  }
111 
112  if (!command.compare("maskhotpixels")) {
113  doRunMaskHotPixels();
114  return;
115  }
116 
117  if (!command.compare("ratescan")) {
118  doRateScan();
119  return;
120  }
121 
122  if (!command.compare("phrun")) {
123  doPhRun();
124  return;
125  }
126 
127  LOG(logDEBUG) << "did not find command ->" << command << "<-";
128 }
129 
130 // ----------------------------------------------------------------------
131 void PixTestXray::init() {
132  LOG(logDEBUG) << "PixTestXray::init()";
133  setToolTips();
134  fDirectory = gFile->GetDirectory(fName.c_str());
135  if (!fDirectory) {
136  fDirectory = gFile->mkdir(fName.c_str());
137  }
138  fDirectory->cd();
139  fSourceChanged = false;
140 
141 }
142 
143 // ----------------------------------------------------------------------
145  fTestTip = string("Xray vcal calibration test")
146  ;
147  fSummaryTip = string("to be implemented")
148  ;
149  fStopTip = string("Stop 'phrun' and save data.")
150  ;
151 }
152 
153 
154 // ----------------------------------------------------------------------
155 vector<TH2D*> PixTestXray::bookHotPixelMap() {
156  fDirectory->cd();
157  vector<uint8_t> rocIds = fApi->_dut->getEnabledRocIDs();
158  unsigned nrocs = rocIds.size();
159  vector<TH2D*> v;
160  for (unsigned int iroc = 0; iroc < nrocs; ++iroc){
161  TH2D *h2 = bookTH2D(Form("hotPixelMap_C%d", rocIds[iroc]), Form("hotPixelMap_C%d", rocIds[iroc]),
162  52, 0., 52., 80, 0., 80.);
163  h2->SetMinimum(0.);
164  h2->SetDirectory(fDirectory);
165  fHotPixelMap.push_back(h2);
166  fHistOptions.insert(make_pair(h2,"colz"));
167  v.push_back(h2);
168  }
169  copy(fHotPixelMap.begin(), fHotPixelMap.end(), back_inserter(fHistList));
170  return v;
171 }
172 
173 
174 // ----------------------------------------------------------------------
175 void PixTestXray::bookHist(string name) {
176  fDirectory->cd();
177  if (fParFillTree) bookTree();
178 
179  vector<uint8_t> rocIds = fApi->_dut->getEnabledRocIDs();
180  unsigned nrocs = rocIds.size();
181 
182  //-- Sets up the histogram
183  TH1D *h1(0);
184  TH2D *h2(0);
185  for (unsigned int iroc = 0; iroc < nrocs; ++iroc){
186  h1 = bookTH1D(Form("hits_%s_C%d", name.c_str(), rocIds[iroc]), Form("hits_%s_C%d", name.c_str(), rocIds[iroc]),
187  256, 0., 256.);
188  h1->SetMinimum(0.);
189  h1->SetDirectory(fDirectory);
190  setTitles(h1, "VthrComp", "Hits");
191  fHits.push_back(h1);
192 
193  h1 = bookTH1D(Form("mpix_%s_C%d", name.c_str(), rocIds[iroc]), Form("mpix_%s_C%d", name.c_str(), rocIds[iroc]),
194  256, 0., 256.);
195  h1->SetMinimum(0.);
196  h1->SetDirectory(fDirectory);
197  setTitles(h1, "VthrComp", "maskedpixels");
198  fMpix.push_back(h1);
199 
200  h2 = bookTH2D(Form("hitMap_%s_C%d", name.c_str(), rocIds[iroc]), Form("hitMap_%s_C%d", name.c_str(), rocIds[iroc]),
201  52, 0., 52., 80, 0., 80.);
202  h2->SetMinimum(0.);
203  h2->SetDirectory(fDirectory);
204  fHitMap.push_back(h2);
205  }
206 
207  copy(fHits.begin(), fHits.end(), back_inserter(fHistList));
208  copy(fMpix.begin(), fMpix.end(), back_inserter(fHistList));
209  copy(fHitMap.begin(), fHitMap.end(), back_inserter(fHistList));
210 }
211 
212 
213 //----------------------------------------------------------
214 PixTestXray::~PixTestXray() {
215  LOG(logDEBUG) << "PixTestXray dtor";
216  fDirectory->cd();
217  if (fTree && fParFillTree) fTree->Write();
218 }
219 
220 
221 // ----------------------------------------------------------------------
223 
224  bigBanner(Form("PixTestXray::doTest()"));
225  doPhRun();
226  // doRateScan();
227 
228  LOG(logINFO) << "PixTestXray::doTest() done ";
229 
230 }
231 
232 
233 // ----------------------------------------------------------------------
234 void PixTestXray::doPhRun() {
235 
236  banner(Form("PixTestXray::doPhRun() fParRunSeconds = %d", fParRunSeconds));
237 
238  PixTest::update();
239  fDirectory->cd();
240 
241  // -- unmask entire chip and then mask hot pixels
242  fApi->_dut->testAllPixels(false);
243  fApi->_dut->maskAllPixels(false);
244  for (unsigned int i = 0; i < fHotPixels.size(); ++i) {
245  vector<pair<int, int> > hot = fHotPixels[i];
246  for (unsigned int ipix = 0; ipix < hot.size(); ++ipix) {
247  LOG(logINFO) << "ROC " << getIdFromIdx(i) << " masking hot pixel " << hot[ipix].first << "/" << hot[ipix].second;
248  fApi->_dut->maskPixel(hot[ipix].first, hot[ipix].second, true, getIdFromIdx(i));
249  }
250  }
251  maskPixels();
252 
253 
254  vector<uint8_t> rocIds = fApi->_dut->getEnabledRocIDs();
255 
256  int totalPeriod = prepareDaq(fParTriggerFrequency, 50);
257 
258  if (fParDelayTBM) {
259  LOG(logINFO) << "set TBM register delays = 0x40";
260  fApi->setTbmReg("delays", 0x40);
261  }
262 
263  fEventsMax = 1000 * fParTriggerFrequency * fParRunSeconds;
264 
265  if (fQ.size() > 0 && fSourceChanged) {
266  LOG(logDEBUG) << "booking new histograms as source name has changed";
267  fQ.clear();
268  fQmap.clear();
269  fHmap.clear();
270  fPHmap.clear();
271  fPH.clear();
272  fHitsVsEvents.clear();
273  fHitsVsColumn.clear();
274  fHitsVsEvtCol.clear();
275  }
276 
277  if (0 == fQ.size()) {
278  fSourceChanged = false;
279  if (fParFillTree) bookTree();
280  TH1D *h1(0);
281  TH2D *h2(0);
282  TH1D *h3(0);
283  TProfile2D *p2(0);
284  for (unsigned int iroc = 0; iroc < rocIds.size(); ++iroc){
285  h2 = bookTH2D(Form("hMap_%s_C%d", fParSource.c_str(), rocIds[iroc]),
286  Form("hMap_%s_C%d", fParSource.c_str(), rocIds[iroc]),
287  52, 0., 52., 80, 0., 80.);
288  h2->SetMinimum(0.);
289  h2->SetDirectory(fDirectory);
290  setTitles(h2, "col", "row");
291  fHistOptions.insert(make_pair(h2,"colz"));
292  fHmap.push_back(h2);
293 
294 
295  p2 = bookTProfile2D(Form("qMap_%s_C%d", fParSource.c_str(), rocIds[iroc]),
296  Form("qMap_%s_C%d", fParSource.c_str(), rocIds[iroc]),
297  52, 0., 52., 80, 0., 80.);
298  p2->SetMinimum(0.);
299  p2->SetDirectory(fDirectory);
300  setTitles(p2, "col", "row");
301  fHistOptions.insert(make_pair(p2,"colz"));
302  fQmap.push_back(p2);
303 
304  p2 = bookTProfile2D(Form("phMap_%s_C%d", fParSource.c_str(), rocIds[iroc]),
305  Form("phMap_%s_C%d", fParSource.c_str(), rocIds[iroc]),
306  52, 0., 52., 80, 0., 80.);
307  p2->SetMinimum(0.);
308  p2->SetDirectory(fDirectory);
309  setTitles(p2, "col", "row");
310  fHistOptions.insert(make_pair(p2,"colz"));
311  fPHmap.push_back(p2);
312 
313  h1 = bookTH1D(Form("q_%s_C%d", fParSource.c_str(), rocIds[iroc]),
314  Form("q_%s_C%d", fParSource.c_str(), rocIds[iroc]),
315  2000, 0., 2000.);
316  h1->SetMinimum(0.);
317  h1->SetDirectory(fDirectory);
318  setTitles(h1, "Q [Vcal]", "Entries/bin");
319  fQ.push_back(h1);
320 
321  h1 = bookTH1D(Form("ph_%s_C%d", fParSource.c_str(), rocIds[iroc]),
322  Form("ph_%s_C%d", fParSource.c_str(), rocIds[iroc]),
323  256, 0., 256.);
324  h1->SetMinimum(0.);
325  h1->SetDirectory(fDirectory);
326  setTitles(h1, "PH [ADC]", "Entries/bin");
327  fPH.push_back(h1);
328 
329  h1 = bookTH1D(Form("hitsVsEvents_%s_C%d", fParSource.c_str(), rocIds[iroc]),
330  Form("hitsVsEvents_%s_C%d", fParSource.c_str(), rocIds[iroc]), 1000, 0., fEventsMax);
331  h1->SetMinimum(0.);
332  h1->SetDirectory(fDirectory);
333  setTitles(h1, "events", "hits/bin");
334  fHitsVsEvents.push_back(h1);
335 
336  h1 = bookTH1D(Form("hitsVsColumn_%s_C%d", fParSource.c_str(), rocIds[iroc]),
337  Form("hitsVsColumn_%s_C%d", fParSource.c_str(), rocIds[iroc]),
338  52, 0., 52.);
339  h1->SetMinimum(0.);
340  h1->SetDirectory(fDirectory);
341  setTitles(h1, "col", "hits/bin");
342  fHitsVsColumn.push_back(h1);
343 
344  h2 = bookTH2D(Form("hitsVsEvtCol_%s_C%d", fParSource.c_str(), rocIds[iroc]),
345  Form("hitsVsEvtCol_%s_C%d", fParSource.c_str(), rocIds[iroc]),
346  1000, 0., fEventsMax, 52, 0., 52.);
347  h2->SetMinimum(0.);
348  h2->SetDirectory(fDirectory);
349  setTitles(h2, "events", "col");
350  fHistOptions.insert(make_pair(h2,"colz"));
351  fHitsVsEvtCol.push_back(h2);
352 
353  }
354  h3 = bookTH1D(Form("ntrig_%s", fParSource.c_str()),
355  Form("ntrig_%s", fParSource.c_str()), 1, 0., 1.);
356  h3->SetDirectory(fDirectory);
357  fTriggers.push_back(h3);
358 
359 
360  copy(fHmap.begin(), fHmap.end(), back_inserter(fHistList));
361  copy(fQmap.begin(), fQmap.end(), back_inserter(fHistList));
362  copy(fQ.begin(), fQ.end(), back_inserter(fHistList));
363  copy(fPHmap.begin(), fPHmap.end(), back_inserter(fHistList));
364  copy(fPH.begin(), fPH.end(), back_inserter(fHistList));
365  copy(fTriggers.begin(), fTriggers.end(), back_inserter(fHistList));
366  copy(fHitsVsEvents.begin(), fHitsVsEvents.end(), back_inserter(fHistList));
367  copy(fHitsVsColumn.begin(), fHitsVsColumn.end(), back_inserter(fHistList));
368  copy(fHitsVsEvtCol.begin(), fHitsVsEvtCol.end(), back_inserter(fHistList));
369  }
370 
371  uint8_t perFull;
372  TStopwatch t;
373  fApi->daqStart();
374  int finalPeriod = fApi->daqTriggerLoop(totalPeriod);
375  LOG(logINFO) << "PixTestXray::doPhRun start TriggerLoop with trigger frequency " << fParTriggerFrequency
376  << " kHz, period " << finalPeriod
377  << " and duration " << fParRunSeconds << " seconds, "
378  << " fEventsMax = " << fEventsMax ;
379 
380  t.Start(kTRUE);
381  fDaq_loop = true;
382  int seconds(0);
383 
384  while (fApi->daqStatus(perFull) && fDaq_loop) {
385  gSystem->ProcessEvents();
386  if (perFull > 80) {
387  seconds = t.RealTime();
388  LOG(logINFO) << "run duration " << seconds << " seconds, buffer almost full ("
389  << (int)perFull << "%), pausing triggers.";
391  processData(0);
392  LOG(logINFO) << "Resuming triggers.";
393  t.Start(kFALSE);
394  fApi->daqTriggerLoop(finalPeriod);
395  }
396 
397 
398  seconds = t.RealTime();
399  t.Start(kFALSE);
400  if (static_cast<int>(seconds >= fParRunSeconds)) {
401  LOG(logINFO) << "data taking finished, elapsed time: " << seconds << " seconds.";
402  fDaq_loop = false;
403  break;
404  }
405  }
406 
408  fApi->daqStop();
409 
410  processData(0);
411 
412  finalCleanup();
413  fQ[0]->Draw();
414  fDisplayedHist = find(fHistList.begin(), fHistList.end(), fQ[0]);
415  PixTest::update();
416 
417  LOG(logINFO) << "PixTestXray::doPhRun() done";
418 
419 }
420 
421 
422 // ----------------------------------------------------------------------
423 void PixTestXray::doRateScan() {
424 
425  banner(Form("PixTestXray::doRateScan() fParStepSeconds = %d, vthrcom = %d .. %d", fParStepSeconds, fParVthrCompMin, fParVthrCompMax));
426  cacheDacs();
427 
428  if (1) {
429  fPg_setup.clear();
430  fPg_setup = fPixSetup->getConfigParameters()->getTbPgSettings();
431  fApi->setPatternGenerator(fPg_setup);
432 
433  PixTest::update();
434  fDirectory->cd();
435 
436  if (0 == fHits.size()) bookHist("xrayVthrCompScan");
437 
438 
439  fApi->_dut->testAllPixels(false);
440  fApi->_dut->maskAllPixels(false);
441 
442 
443  int totalPeriod = prepareDaq(fParTriggerFrequency, 50);
444 
445  // -- scan VthrComp
446  for (fVthrComp = fParVthrCompMin; fVthrComp <= fParVthrCompMax; ++fVthrComp) {
447  for (unsigned i = 0; i < fHitMap.size(); ++i) {
448  fHitMap[i]->Reset();
449  }
450  TStopwatch t;
451  uint8_t perFull;
452  fApi->setDAC("vthrcomp", fVthrComp);
453  fDaq_loop = true;
454 
455  LOG(logINFO)<< "Starting Loop with VthrComp = " << fVthrComp;
456  t.Start(kTRUE);
457  fApi->daqStart();
458 
459  int finalPeriod = fApi->daqTriggerLoop(totalPeriod);
460  LOG(logINFO) << "PixTestXray::doRateScan start TriggerLoop with period " << finalPeriod << " and duration " << fParStepSeconds << " seconds";
461 
462  while (fApi->daqStatus(perFull) && fDaq_loop) {
463  gSystem->ProcessEvents();
464  if (perFull > 80) {
465  LOG(logINFO) << "Buffer almost full, pausing triggers.";
467  readData();
468  LOG(logINFO) << "Resuming triggers.";
469  fApi->daqTriggerLoop(finalPeriod);
470  }
471 
472  if (static_cast<int>(t.RealTime()) >= fParStepSeconds) {
473  LOG(logINFO) << "Elapsed time: " << t.RealTime() << " seconds.";
474  fDaq_loop = false;
475  break;
476  }
477  }
478 
480 
481  fApi->daqStop();
482  readData();
483 
484 
485 
486  analyzeData();
487 
488  fHits[0]->Draw();
489  fDisplayedHist = find(fHistList.begin(), fHistList.end(), fHits[0]);
490  PixTest::update();
491 
492  }
493  }
494 
495  if (0) {
496  // TFile *f = TFile::Open("testROC/pxar_firstXray30_90.root");
497  // TFile *f = TFile::Open("testROC/pxar_Ag_Vcal_10_70_10s.root");
498  TFile *f = TFile::Open("testROC/pxar_Mo_Vcal_30_80_10s.root");
499 
500  TH1D *h1 = ((TH1D*)f->Get("Xray/hits_xrayVthrCompScan_C0_V0"));
501  TH1D *h2 = (TH1D*)h1->Clone("local");
502  h2->SetDirectory(0);
503  f->Close();
504  fHits.push_back(h2);
505  copy(fHits.begin(), fHits.end(), back_inserter(fHistList));
506 
507  fHits[0]->Draw();
508  fDisplayedHist = find(fHistList.begin(), fHistList.end(), fHits[0]);
509  PixTest::update();
510  return;
511  }
512 
513 
514  vector<uint8_t> rocIds = fApi->_dut->getEnabledRocIDs();
515  unsigned nrocs = rocIds.size();
516  double lo(-1.), hi(-1.);
517  for (unsigned int i = 0; i < nrocs; ++i) {
518  TF1 *f1 = fPIF->xrayScan(fHits[i]);
519  f1->GetRange(lo, hi);
520  fHits[i]->Fit(f1, "lr", "", lo, hi);
521  double thr = f1->GetParameter(0);
522  if (thr < 0 || thr > 255.) thr = 0.;
523  uint8_t ithr = static_cast<uint8_t>(thr);
524  LOG(logINFO) << "ROC " << static_cast<int>(rocIds[i]) << " with VthrComp threshold = " << thr << " -> " << static_cast<int>(ithr);
525  fApi->setDAC("vthrcomp", ithr, rocIds[i]);
526  PixTest::update();
527  }
528 
529  finalCleanup();
530 
531  fApi->_dut->testAllPixels(true);
532  fApi->_dut->maskAllPixels(false);
533 
534  vector<TH1*> thr0 = scurveMaps("vcal", "xrayScan", 5, 0, 255, -1, 9);
535 
536  fHits[0]->Draw();
537  fDisplayedHist = find(fHistList.begin(), fHistList.end(), fHits[0]);
538  PixTest::update();
539 
540  string hname(""), scurvesMeanString(""), scurvesRmsString("");
541  for (unsigned int i = 0; i < thr0.size(); ++i) {
542  hname = thr0[i]->GetName();
543  // -- skip sig_ and thn_ histograms
544  if (string::npos == hname.find("dist_thr_")) continue;
545  scurvesMeanString += Form("%6.2f ", thr0[i]->GetMean());
546  scurvesRmsString += Form("%6.2f ", thr0[i]->GetRMS());
547  }
548 
549  LOG(logINFO) << "PixTestXray::doTest() done";
550  LOG(logINFO) << "vcal mean: " << scurvesMeanString;
551  LOG(logINFO) << "vcal RMS: " << scurvesRmsString;
552 
553  LOG(logINFO) << "PixTestXray::doRateScan() done";
554 
555 }
556 
557 // ----------------------------------------------------------------------
558 void PixTestXray::readDataOld() {
559 
560  int pixCnt(0);
561  vector<pxar::Event> daqdat;
562 
563  daqdat = fApi->daqGetEventBuffer();
564 
565  for(std::vector<pxar::Event>::iterator it = daqdat.begin(); it != daqdat.end(); ++it) {
566  pixCnt += it->pixels.size();
567 
568  for (unsigned int ipix = 0; ipix < it->pixels.size(); ++ipix) {
569  fHitMap[getIdxFromId(it->pixels[ipix].roc())]->Fill(it->pixels[ipix].column(), it->pixels[ipix].row());
570  }
571  }
572  LOG(logDEBUG) << "Processing Data: " << daqdat.size() << " events with " << pixCnt << " pixels";
573 }
574 
575 // ----------------------------------------------------------------------
576 void PixTestXray::analyzeData() {
577 
578  vector<uint8_t> rocIds = fApi->_dut->getEnabledRocIDs();
579  unsigned nrocs = rocIds.size();
580 
581  int cnt(0);
582  for (unsigned int i = 0; i < nrocs; ++i) {
583  double cut = meanHit(fHitMap[i]);
584  cut = noiseLevel(fHitMap[i]);
585  cnt = countHitsAndMaskPixels(fHitMap[i], cut, rocIds[i]);
586  fHits[i]->SetBinContent(fVthrComp+1, cnt);
587  int mpix = fApi->_dut->getNMaskedPixels(rocIds[i]);
588  fMpix[i]->SetBinContent(fVthrComp+1, mpix);
589  }
590 
591 
592 }
593 
594 
595 // ----------------------------------------------------------------------
596 double PixTestXray::meanHit(TH2D *h2) {
597 
598  TH1D *h1 = new TH1D("h1", "h1", 1000, 0., 1000.);
599 
600  for (int ix = 0; ix < h2->GetNbinsX(); ++ix) {
601  for (int iy = 0; iy < h2->GetNbinsY(); ++iy) {
602  h1->Fill(h2->GetBinContent(ix+1, iy+1));
603  }
604  }
605 
606  double mean = h1->GetMean();
607  delete h1;
608  LOG(logDEBUG) << "hist " << h2->GetName() << " mean hits = " << mean;
609  return mean;
610 }
611 
612 // ----------------------------------------------------------------------
613 double PixTestXray::noiseLevel(TH2D *h2) {
614 
615  TH1D *h1 = new TH1D("h1", "h1", 1000, 0., 1000.);
616 
617  for (int ix = 0; ix < h2->GetNbinsX(); ++ix) {
618  for (int iy = 0; iy < h2->GetNbinsY(); ++iy) {
619  h1->Fill(h2->GetBinContent(ix+1, iy+1));
620  }
621  }
622 
623  // -- skip bin for zero hits!
624  int noise(-1);
625  for (int ix = 1; ix < h1->GetNbinsX(); ++ix) {
626  if (h1->GetBinContent(ix+1) < 1) {
627  noise = ix;
628  break;
629  }
630  }
631 
632  int lastbin(1);
633  for (int ix = 1; ix < h1->GetNbinsX(); ++ix) {
634  if (h1->GetBinContent(ix+1) > 1) {
635  lastbin = ix;
636  }
637  }
638 
639 
640  delete h1;
641  LOG(logINFO) << "hist " << h2->GetName()
642  << " (maximum: " << h2->GetMaximum() << ") "
643  << " noise level = " << noise << " last bin above 1: " << lastbin;
644  return lastbin;
645 }
646 
647 
648 // ----------------------------------------------------------------------
649 int PixTestXray::countHitsAndMaskPixels(TH2D *h2, double noiseLevel, int iroc) {
650 
651  int cnt(0);
652  double entries(0.);
653  for (int ix = 0; ix < h2->GetNbinsX(); ++ix) {
654  for (int iy = 0; iy < h2->GetNbinsY(); ++iy) {
655  entries = h2->GetBinContent(ix+1, iy+1);
656  if (entries > noiseLevel) {
657  fApi->_dut->maskPixel(ix, iy, true, iroc);
658  LOG(logINFO) << "ROC " << iroc << " masking pixel " << ix << "/" << iy
659  << " with #hits = " << entries << " (cut: " << noiseLevel << ")";
660  } else {
661  cnt += static_cast<int>(entries);
662  }
663  }
664  }
665  return cnt;
666 }
667 
668 
669 // ----------------------------------------------------------------------
670 void PixTestXray::readData() {
671 
672  int pixCnt(0);
673  vector<pxar::Event> daqdat = fApi->daqGetEventBuffer();
674 
675  for (std::vector<pxar::Event>::iterator it = daqdat.begin(); it != daqdat.end(); ++it) {
676  pixCnt += it->pixels.size();
677 
678  if (fParFillTree) {
679  bookTree();
680  fTreeEvent.header = it->header;
681  fTreeEvent.dac = 0;
682  fTreeEvent.trailer = it->trailer;
683  }
684 
685  int idx(0);
686  double q(0.);
687  for (unsigned int ipix = 0; ipix < it->pixels.size(); ++ipix) {
688  idx = getIdxFromId(it->pixels[ipix].roc());
689  if (fPhCalOK) {
690  q = fPhCal.vcal(it->pixels[ipix].roc(),
691  it->pixels[ipix].column(),
692  it->pixels[ipix].row(),
693  it->pixels[ipix].value());
694  } else {
695  q = 0;
696  }
697  fHitMap[idx]->Fill(it->pixels[ipix].column(), it->pixels[ipix].row());
698  fQ[idx]->Fill(q);
699  fQmap[idx]->Fill(it->pixels[ipix].column(), it->pixels[ipix].row(), q);
700 
701  fPHmap[idx]->Fill(it->pixels[ipix].column(), it->pixels[ipix].row(), it->pixels[ipix].value());
702  fPH[idx]->Fill(it->pixels[ipix].value());
703 
704  if (fParFillTree && ipix < 20000) {
705  ++fTreeEvent.npix;
706  fTreeEvent.proc[ipix] = it->pixels[ipix].roc();
707  fTreeEvent.pcol[ipix] = it->pixels[ipix].column();
708  fTreeEvent.prow[ipix] = it->pixels[ipix].row();
709  fTreeEvent.pval[ipix] = it->pixels[ipix].value();
710  fTreeEvent.pq[ipix] = q;
711  }
712  }
713 
714  if (fParFillTree) fTree->Fill();
715 
716  }
717  LOG(logDEBUG) << "Processing Data: " << daqdat.size() << " events with " << pixCnt << " pixels";
718 }
719 
720 
721 // ----------------------------------------------------------------------
722 void PixTestXray::processData(uint16_t numevents) {
723  fDirectory->cd();
724  PixTest::update();
725 
726  static long int evtCnt(-1);
727  int pixCnt(0);
728  LOG(logDEBUG) << "Getting Event Buffer";
729  vector<pxar::Event> daqdat;
730 
731  if (numevents > 0) {
732  for (unsigned int i = 0; i < numevents ; i++) {
733  pxar::Event evt = fApi->daqGetEvent();
734  //Check if event is empty?
735  if(evt.pixels.size() > 0)
736  daqdat.push_back(evt);
737  }
738  }
739  else {
740  daqdat = fApi->daqGetEventBuffer();
741  }
742 
743  LOG(logDEBUG) << "Processing Data: " << daqdat.size() << " events.";
744 
745  int idx(-1);
746  uint16_t q;
747  for (std::vector<pxar::Event>::iterator it = daqdat.begin(); it != daqdat.end(); ++it) {
748  ++evtCnt;
749  pixCnt += it->pixels.size();
750 
751 
752  if (fParFillTree) {
753  bookTree();
754  fTreeEvent.header = it->header;
755  fTreeEvent.dac = 0;
756  fTreeEvent.trailer = it->trailer;
757  }
758 
759  for (unsigned int ipix = 0; ipix < it->pixels.size(); ++ipix) {
760  idx = getIdxFromId(it->pixels[ipix].roc());
761 
762  fHitsVsEvents[idx]->Fill(evtCnt);
763  fHitsVsColumn[idx]->Fill(it->pixels[ipix].column());
764  fHitsVsEvtCol[idx]->Fill(evtCnt, it->pixels[ipix].column());
765 
766  if (fPhCalOK) {
767  q = fPhCal.vcal(it->pixels[ipix].roc(),
768  it->pixels[ipix].column(),
769  it->pixels[ipix].row(),
770  it->pixels[ipix].value());
771  } else {
772  q = 0;
773  }
774  fHmap[idx]->Fill(it->pixels[ipix].column(), it->pixels[ipix].row());
775  fQ[idx]->Fill(q);
776  fQmap[idx]->Fill(it->pixels[ipix].column(), it->pixels[ipix].row(), q);
777 
778  fPHmap[idx]->Fill(it->pixels[ipix].column(), it->pixels[ipix].row(), it->pixels[ipix].value());
779  fPH[idx]->Fill(it->pixels[ipix].value());
780 
781  if (fParFillTree && ipix < 20000) {
782  ++fTreeEvent.npix;
783  fTreeEvent.proc[ipix] = it->pixels[ipix].roc();
784  fTreeEvent.pcol[ipix] = it->pixels[ipix].column();
785  fTreeEvent.prow[ipix] = it->pixels[ipix].row();
786  fTreeEvent.pval[ipix] = it->pixels[ipix].value();
787  fTreeEvent.pq[ipix] = q;
788  }
789  }
790 
791  if (fParFillTree) fTree->Fill();
792  }
793 
794  LOG(logDEBUG) << Form(" # events read: %6ld, pixels seen in all events: %3d", daqdat.size(), pixCnt);
795 
796  fHmap[0]->Draw("colz");
797  fTriggers[0]->SetBinContent(1, fTriggers[0]->GetBinContent(1) + daqdat.size());
798  PixTest::update();
799 }
800 
801 
802 // ----------------------------------------------------------------------
803 void PixTestXray::doRunMaskHotPixels() {
804  PixTest::update();
805  vector<TH2D*> v = mapsWithString(fHitMap, "hotpixels");
806  if (0 == v.size()) {
807  bookHist("hotpixels");
808  v = mapsWithString(fHitMap, "hotpixels");
809  }
810  for (unsigned int i = 0; i < v.size(); ++i) v[i]->Reset();
811  maskHotPixels(v);
812 
813  if (fParSaveMaskedPixels) {
814  if (fParMaskFileName == "default") {
815  fPixSetup->getConfigParameters()->writeMaskFile(fHotPixels);
816  } else {
817  fPixSetup->getConfigParameters()->writeMaskFile(fHotPixels, fParMaskFileName);
818  }
819  }
820 
821 
822  // -- display
823  fDisplayedHist = find(fHistList.begin(), fHistList.end(), v[0]);
824  v[0]->Draw("colz");
825  PixTest::update();
826  return;
827 }
828 
829 
830 // ----------------------------------------------------------------------
831 void PixTestXray::doStop(){
832  // Interrupt the test
833  fDaq_loop = false;
834  LOG(logINFO) << "Stop pressed. Ending test.";
835 }
836 
uint16_t prepareDaq(int triggerFreq, uint8_t trgTkDel)
set up DAQ (including call to setTriggerFrequency)
Definition: PixTest.cc:1799
static void replaceAll(std::string &str, const std::string &from, const std::string &to)
in str, replace all occurences of from to to
Definition: PixUtil.cc:24
size_t getNMaskedPixels(uint8_t rocid)
Definition: dut.cc:59
void setPatternGenerator(std::vector< std::pair< std::string, uint8_t > > pg_setup)
Definition: api.cc:78
virtual bool setParameter(std::string parName, std::string sval)
set the string value of a parameter
Definition: PixTestXray.cc:43
bool daqStart()
Definition: api.cc:1139
std::map< TH1 *, std::string > fHistOptions
options can be stored with each histogram
Definition: PixTest.hh:308
void daqTriggerLoopHalt()
Definition: api.cc:1257
bool setDAC(std::string dacName, uint8_t dacValue, uint8_t rocI2C)
Definition: api.cc:546
std::string fStopTip
information for this test
Definition: PixTest.hh:300
PixSetup * fPixSetup
all necessary stuff in one place
Definition: PixTest.hh:290
std::vector< TH1 * > mapsWithString(std::vector< TH1 * >, std::string name)
return a list of TH* that have 'name' as part to their histogram name
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
void maskHotPixels(std::vector< TH2D * >)
determine hot pixels with high occupancy
Definition: PixTest.cc:1837
TDirectory * fDirectory
where the root histograms will end up
Definition: PixTest.hh:306
uint16_t daqTriggerLoop(uint16_t period=1000)
Definition: api.cc:1239
void bookTree()
book a minimal tree with pixel events
Definition: PixTest.cc:99
dut * _dut
Definition: api.h:728
void doTest()
function connected to "DoTest" button of PixTab
Definition: PixTestXray.cc:222
Event daqGetEvent()
Definition: api.cc:1300
std::vector< uint8_t > getEnabledRocIDs()
Definition: dut.cc:214
void finalCleanup()
functions for DAQ
Definition: PixTest.cc:1765
void testAllPixels(bool enable)
Definition: dut.cc:503
bool daqStop()
Definition: api.cc:1324
std::list< TH1 * >::iterator fDisplayedHist
pointer to the histogram currently displayed
Definition: PixTest.hh:309
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
void cacheDacs(bool verbose=false)
cache all DACs
Definition: PixTest.cc:760
bool setTbmReg(std::string regName, uint8_t regValue, uint8_t tbmid)
Definition: api.cc:633
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
PixInitFunc * fPIF
function instantiation and automatic initialization
Definition: PixTest.hh:292
void runCommand(std::string command)
allow execution of any button in the test
Definition: PixTestXray.cc:104
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
pxar::pxarCore * fApi
pointer to the API
Definition: PixTest.hh:289
std::vector< Event > daqGetEventBuffer()
Definition: api.cc:1285
TProfile2D * bookTProfile2D(std::string sname, std::string title, int nbinsx, double xmin, double xmax, int nbinsy, double ymin, double max, std::string option="")
book a TProfile2D, adding version information to the name and title
Definition: PixTest.cc:911
void setToolTips()
implement this to provide updated tool tips if the user changes test parameters
Definition: PixTestXray.cc:144
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
void maskPixels()
mask all pixels mentioned in the mask file
Definition: PixTest.cc:1741
bool daqStatus()
Definition: api.cc:1191
int getIdFromIdx(int idx)
provide the mapping between ROC ID and index
Definition: PixTest.cc:1114