pxar
 All Classes Namespaces Functions Variables Typedefs Friends
PixTestBBMap.cc
1 // -- author: Wolfram Erdmann
2 // Bump Bonding tests, really just a threshold map using cals
3 
4 #include <sstream> // parsing
5 #include <algorithm> // std::find
6 
7 #include <TArrow.h>
8 #include <TSpectrum.h>
9 #include "TStopwatch.h"
10 
11 #include "PixTestBBMap.hh"
12 #include "PixUtil.hh"
13 #include "log.h"
14 #include "constants.h" // roctypes
15 
16 using namespace std;
17 using namespace pxar;
18 
19 ClassImp(PixTestBBMap)
20 
21 //------------------------------------------------------------------------------
22 PixTestBBMap::PixTestBBMap(PixSetup *a, std::string name): PixTest(a, name),
23  fParNtrig(-1), fParVcalS(200), fDumpAll(-1), fDumpProblematic(-1) {
24  PixTest::init();
25  init();
26  LOG(logDEBUG) << "PixTestBBMap ctor(PixSetup &a, string, TGTab *)";
27 }
28 
29 
30 //------------------------------------------------------------------------------
31 PixTestBBMap::PixTestBBMap(): PixTest() {
32  LOG(logDEBUG) << "PixTestBBMap ctor()";
33 }
34 
35 
36 
37 //------------------------------------------------------------------------------
38 bool PixTestBBMap::setParameter(string parName, string sval) {
39 
40  std::transform(parName.begin(), parName.end(), parName.begin(), ::tolower);
41  for (uint32_t i = 0; i < fParameters.size(); ++i) {
42  if (fParameters[i].first == parName) {
43  sval.erase(remove(sval.begin(), sval.end(), ' '), sval.end());
44 
45  stringstream s(sval);
46  if (!parName.compare( "ntrig")) {
47  s >> fParNtrig;
48  setToolTips();
49  return true;
50  }
51  if (!parName.compare( "vcals")) {
52  s >> fParVcalS;
53  setToolTips();
54  return true;
55  }
56 
57  if (!parName.compare("dumpall")) {
58  PixUtil::replaceAll(sval, "checkbox(", "");
59  PixUtil::replaceAll(sval, ")", "");
60  fDumpAll = atoi(sval.c_str());
61  setToolTips();
62  }
63 
64  if (!parName.compare("dumpallproblematic")) {
65  PixUtil::replaceAll(sval, "checkbox(", "");
66  PixUtil::replaceAll(sval, ")", "");
67  fDumpProblematic = atoi(sval.c_str());
68  setToolTips();
69  }
70 
71  }
72  }
73  return false;
74 }
75 
76 //------------------------------------------------------------------------------
77 void PixTestBBMap::init() {
78  LOG(logDEBUG) << "PixTestBBMap::init()";
79 
80  fDirectory = gFile->GetDirectory("BumpBonding");
81  if (!fDirectory) {
82  fDirectory = gFile->mkdir("BumpBonding");
83  }
84  fDirectory->cd();
85 }
86 
87 // ----------------------------------------------------------------------
89  fTestTip = string( "Bump Bonding Test = threshold map for CalS");
90  fSummaryTip = string("module summary");
91 }
92 
93 
94 //------------------------------------------------------------------------------
95 PixTestBBMap::~PixTestBBMap() {
96  LOG(logDEBUG) << "PixTestBBMap dtor";
97 }
98 
99 //------------------------------------------------------------------------------
101 
102  TStopwatch t;
103 
104  cacheDacs();
105  PixTest::update();
106  bigBanner(Form("PixTestBBMap::doTest() Ntrig = %d, VcalS = %d (high range)", fParNtrig, fParVcalS));
107 
108  fDirectory->cd();
109 
110  fApi->_dut->testAllPixels(true);
111  fApi->_dut->maskAllPixels(false);
112 
113  int flag(FLAG_CALS);
114  fApi->setDAC("ctrlreg", 4); // high range
115  fApi->setDAC("vcal", fParVcalS);
116 
117  int result(1);
118  if (fDumpAll) result |= 0x20;
119  if (fDumpProblematic) result |= 0x10;
120 
121  fNDaqErrors = 0;
122  vector<TH1*> thrmapsCals = scurveMaps("VthrComp", "calSMap", fParNtrig, 0, 149, 50, result, 1, flag);
123 
124  // -- relabel negative thresholds as 255 and create distribution list
125  vector<TH1D*> dlist;
126  TH1D *h(0);
127  for (unsigned int i = 0; i < thrmapsCals.size(); ++i) {
128  for (int ix = 0; ix < thrmapsCals[i]->GetNbinsX(); ++ix) {
129  for (int iy = 0; iy < thrmapsCals[i]->GetNbinsY(); ++iy) {
130  if (thrmapsCals[i]->GetBinContent(ix+1, iy+1) < 0) thrmapsCals[i]->SetBinContent(ix+1, iy+1, 255.);
131  }
132  }
133  h = distribution((TH2D*)thrmapsCals[i], 256, 0., 256.);
134 
135  dlist.push_back(h);
136  fHistList.push_back(h);
137  }
138 
139  restoreDacs();
140 
141  // -- summary printout
142  string bbString(""), bbCuts("");
143  int bbprob(0);
144  int nPeaks(0), cutDead(0);
145  TSpectrum s;
146  for (unsigned int i = 0; i < dlist.size(); ++i) {
147  h = (TH1D*)dlist[i];
148  nPeaks = s.Search(h, 5, "", 0.01);
149  LOG(logDEBUG) << "found " << nPeaks << " peaks in " << h->GetName();
150  cutDead = fitPeaks(h, s, nPeaks);
151 
152  bbprob = static_cast<int>(h->Integral(cutDead, h->FindBin(255)));
153  bbString += Form(" %4d", bbprob);
154  bbCuts += Form(" %4d", cutDead);
155 
156  TArrow *pa = new TArrow(cutDead, 0.5*h->GetMaximum(), cutDead, 0., 0.06, "|>");
157  pa->SetArrowSize(0.1);
158  pa->SetAngle(40);
159  pa->SetLineWidth(2);
160  h->GetListOfFunctions()->Add(pa);
161 
162  }
163 
164  if (h) {
165  h->Draw();
166  fDisplayedHist = find(fHistList.begin(), fHistList.end(), h);
167  }
168  PixTest::update();
169 
170  int seconds = t.RealTime();
171  LOG(logINFO) << "PixTestBBMap::doTest() done"
172  << (fNDaqErrors>0? Form(" with %d decoding errors: ", static_cast<int>(fNDaqErrors)):"")
173  << ", duration: " << seconds << " seconds";
174  LOG(logINFO) << "number of dead bumps (per ROC): " << bbString;
175  LOG(logINFO) << "separation cut (per ROC): " << bbCuts;
176 
177 }
178 
179 
180 
181 // ----------------------------------------------------------------------
182 int PixTestBBMap::fitPeaks(TH1D *h, TSpectrum &s, int npeaks) {
183 
184  Float_t *xpeaks = s.GetPositionX();
185  string name;
186  double lcuts[3]; lcuts[0] = lcuts[1] = lcuts[2] = 255.;
187  TF1 *f(0);
188  double peak, sigma;
189  int fittedPeaks(0);
190  for (int p = 0; p < npeaks; ++p) {
191  double xp = xpeaks[p];
192  if (p > 1) continue;
193  if (xp > 200) {
194  continue;
195  }
196  if (xp < 15) {
197  continue;
198  }
199  name = Form("gauss_%d", p);
200  f = new TF1(name.c_str(), "gaus(0)", 0., 256.);
201  int bin = h->GetXaxis()->FindBin(xp);
202  double yp = h->GetBinContent(bin);
203  f->SetParameters(yp, xp, 2.);
204  h->Fit(f, "Q+");
205  ++fittedPeaks;
206  peak = h->GetFunction(name.c_str())->GetParameter(1);
207  sigma = h->GetFunction(name.c_str())->GetParameter(2);
208  if (0 == p) {
209  lcuts[0] = peak + 3*sigma;
210  if (h->Integral(h->FindBin(peak + 10.*sigma), 250) > 10.) {
211  lcuts[1] = peak + 5*sigma;
212  } else {
213  lcuts[1] = peak + 10*sigma;
214  }
215  } else {
216  lcuts[1] = peak - 3*sigma;
217  lcuts[2] = peak - sigma;
218  }
219  delete f;
220  }
221 
222  int startbin = (int)(0.5*(lcuts[0] + lcuts[1]));
223  int endbin = (int)(lcuts[1]);
224  if (endbin <= startbin) {
225  endbin = (int)(lcuts[2]);
226  if (endbin < startbin) {
227  endbin = 255.;
228  }
229  }
230 
231  int minbin(0);
232  double minval(999.);
233 
234  for (int i = startbin; i <= endbin; ++i) {
235  if (h->GetBinContent(i) < minval) {
236  if (1 == fittedPeaks) {
237  if (0 == h->Integral(i, i+4)) {
238  minval = h->GetBinContent(i);
239  minbin = i;
240  } else {
241  minbin = endbin;
242  }
243  } else {
244  minval = h->GetBinContent(i);
245  minbin = i;
246  }
247  }
248  }
249 
250  LOG(logDEBUG) << "cut for dead bump bonds: " << minbin << " (obtained for minval = " << minval << ")"
251  << " start: " << startbin << " .. " << endbin
252  << " last peak: " << peak << " last sigma: " << sigma
253  << " lcuts[0] = " << lcuts[0] << " lcuts[1] = " << lcuts[1];
254  return minbin+1;
255 }
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
bool setDAC(std::string dacName, uint8_t dacValue, uint8_t rocI2C)
Definition: api.cc:546
std::vector< std::pair< std::string, std::string > > fParameters
the parameters of this test
Definition: PixTest.hh:302
void maskAllPixels(bool mask, uint8_t rocid)
Definition: dut.cc:481
TDirectory * fDirectory
where the root histograms will end up
Definition: PixTest.hh:306
void setToolTips()
implement this to provide updated tool tips if the user changes test parameters
Definition: PixTestBBMap.cc:88
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
void testAllPixels(bool enable)
Definition: dut.cc:503
std::list< TH1 * >::iterator fDisplayedHist
pointer to the histogram currently displayed
Definition: PixTest.hh:309
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 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< 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
virtual bool setParameter(std::string parName, std::string sval)
set the string value of a parameter
Definition: PixTestBBMap.cc:38
void doTest()
function connected to "DoTest" button of PixTab