pxar
 All Classes Namespaces Functions Variables Typedefs Friends
PixTestGainPedestal.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 <TROOT.h>
9 #include <TStyle.h>
10 #include <TMath.h>
11 #include <TStopwatch.h>
12 
13 #include "PixTestGainPedestal.hh"
14 #include "PixUtil.hh"
15 #include "log.h"
16 
17 
18 using namespace std;
19 using namespace pxar;
20 
21 ClassImp(PixTestGainPedestal)
22 
23 // ----------------------------------------------------------------------
24 PixTestGainPedestal::PixTestGainPedestal(PixSetup *a, std::string name) : PixTest(a, name),
25  fParNtrig(-1), fParShowFits(0), fParExtended(0), fParDumpHists(0) {
26  PixTest::init();
27  init();
28 }
29 
30 
31 //----------------------------------------------------------
32 PixTestGainPedestal::PixTestGainPedestal() : PixTest() {
33  // LOG(logDEBUG) << "PixTestGainPedestal ctor()";
34 }
35 
36 // ----------------------------------------------------------------------
37 bool PixTestGainPedestal::setParameter(string parName, string sval) {
38  bool found(false);
39  std::transform(parName.begin(), parName.end(), parName.begin(), ::tolower);
40  for (unsigned int i = 0; i < fParameters.size(); ++i) {
41  if (fParameters[i].first == parName) {
42  found = true;
43  sval.erase(remove(sval.begin(), sval.end(), ' '), sval.end());
44  if (!parName.compare("ntrig")) {
45  fParNtrig = atoi(sval.c_str());
46  }
47  if (!parName.compare("showfits")) {
48  PixUtil::replaceAll(sval, "checkbox(", "");
49  PixUtil::replaceAll(sval, ")", "");
50  fParShowFits = atoi(sval.c_str());
51  }
52  if (!parName.compare("extended")) {
53  PixUtil::replaceAll(sval, "checkbox(", "");
54  PixUtil::replaceAll(sval, ")", "");
55  fParExtended = atoi(sval.c_str());
56  }
57  if (!parName.compare("dumphists")) {
58  PixUtil::replaceAll(sval, "checkbox(", "");
59  PixUtil::replaceAll(sval, ")", "");
60  fParDumpHists = atoi(sval.c_str());
61  }
62  setToolTips();
63  break;
64  }
65  }
66 
67  return found;
68 }
69 
70 
71 // ----------------------------------------------------------------------
73  fTestTip = string(Form("measure and fit pulseheight vs VCAL (combining low- and high-range)\n"));
74  fSummaryTip = string("all ROCs are displayed side-by-side. Note the orientation:")
75  + string("\nthe canvas bottom corresponds to the narrow module side with the cable")
76  ;
77 }
78 
79 // ----------------------------------------------------------------------
80 void PixTestGainPedestal::init() {
81 
82  setToolTips();
83 
84  fDirectory = gFile->GetDirectory(fName.c_str());
85  if (!fDirectory) {
86  fDirectory = gFile->mkdir(fName.c_str());
87  }
88  fDirectory->cd();
89 
90 }
91 
92 // ----------------------------------------------------------------------
93 void PixTestGainPedestal::bookHist(string /*name*/) {
94  fDirectory->cd();
95  // fHistList.clear();
96 
97 }
98 
99 
100 //----------------------------------------------------------
101 PixTestGainPedestal::~PixTestGainPedestal() {
102  LOG(logDEBUG) << "PixTestGainPedestal dtor";
103 }
104 
105 
106 // ----------------------------------------------------------------------
108 
109  TStopwatch t;
110 
111  fDirectory->cd();
112  PixTest::update();
113  bigBanner(Form("PixTestGainPedestal::doTest() ntrig = %d", fParNtrig));
114 
115  measure();
116  fit();
117  saveGainPedestalParameters();
118 
119  int seconds = t.RealTime();
120  LOG(logINFO) << "PixTestGainPedestal::doTest() done, duration: " << seconds << " seconds";
121 }
122 
123 
124 // ----------------------------------------------------------------------
126 
127  TStopwatch t;
128 
129  fDirectory->cd();
130  PixTest::update();
131  bigBanner(Form("PixTestGainPedestal::fullTest() ntrig = %d", fParNtrig));
132 
133  // fParDumpHists = 1;
134 
135  measure();
136  fit();
137  saveGainPedestalParameters();
138 
139  int seconds = t.RealTime();
140  LOG(logINFO) << "PixTestGainPedestal::doTest() done, duration: " << seconds << " seconds";
141 }
142 
143 
144 // ----------------------------------------------------------------------
145 void PixTestGainPedestal::runCommand(string command) {
146  std::transform(command.begin(), command.end(), command.begin(), ::tolower);
147  LOG(logDEBUG) << "running command: " << command;
148  if (!command.compare("measure")) {
149  measure();
150  return;
151  }
152  if (!command.compare("fit")) {
153  fit();
154  return;
155  }
156  if (!command.compare("save")) {
157  saveGainPedestalParameters();
158  return;
159  }
160  return;
161 }
162 
163 
164 
165 // ----------------------------------------------------------------------
166 void PixTestGainPedestal::measure() {
167  uint16_t FLAGS = FLAG_FORCE_MASKED;
168  LOG(logDEBUG) << " using FLAGS = " << (int)FLAGS;
169 
170 
171  fLpoints.clear();
172  fLpoints.push_back(50);
173  fLpoints.push_back(100);
174  fLpoints.push_back(150);
175  fLpoints.push_back(200);
176  fLpoints.push_back(250);
177 
178  fHpoints.clear();
179  if (1 == fParExtended) {
180  fHpoints.push_back(10); //new: 70
181  fHpoints.push_back(17); //new: 119
182  fHpoints.push_back(24); //new: 168
183  }
184  fHpoints.push_back(30);
185  fHpoints.push_back(50);
186  fHpoints.push_back(70);
187  fHpoints.push_back(90);
188  if (1 == fParExtended) {
189  fHpoints.push_back(120); //new
190  }
191  fHpoints.push_back(200);
192 
193 
194  cacheDacs();
195 
196  vector<uint8_t> rocIds = fApi->_dut->getEnabledRocIDs();
197 
198  shist256 *pshistBlock = new (fPixSetup->fPxarMemory) shist256[16*52*80];
199  shist256 *ph;
200 
201  int idx(0);
202  fHists.clear();
203  for (unsigned int iroc = 0; iroc < rocIds.size(); ++iroc) {
204  for (unsigned int ic = 0; ic < 52; ++ic) {
205  for (unsigned int ir = 0; ir < 80; ++ir) {
206  idx = PixUtil::rcr2idx(iroc, ic, ir);
207  ph = pshistBlock + idx;
208  fHists.push_back(ph);
209  }
210  }
211  }
212 
213  fApi->_dut->testAllPixels(true);
214  fApi->_dut->maskAllPixels(false);
215 
216  // -- first low range
217  fApi->setDAC("ctrlreg", 0);
218 
219  vector<pair<uint8_t, vector<pixel> > > rresult, lresult, hresult;
220  for (unsigned int i = 0; i < fLpoints.size(); ++i) {
221  LOG(logINFO) << "scanning low vcal = " << fLpoints[i];
222  int cnt(0);
223  bool done = false;
224  while (!done){
225  try {
226  rresult = fApi->getPulseheightVsDAC("vcal", fLpoints[i], fLpoints[i], FLAGS, fParNtrig);
227  copy(rresult.begin(), rresult.end(), back_inserter(lresult));
228  done = true; // got our data successfully
229  }
230  catch(pxar::DataMissingEvent &e){
231  LOG(logCRITICAL) << "problem with readout: "<< e.what() << " missing " << e.numberMissing << " events";
232  ++cnt;
233  if (e.numberMissing > 10) done = true;
234  } catch(pxarException &e) {
235  LOG(logCRITICAL) << "pXar execption: "<< e.what();
236  ++cnt;
237  }
238  done = (cnt>2) || done;
239  }
240  }
241 
242  // -- and high range
243  fApi->setDAC("ctrlreg", 4);
244  for (unsigned int i = 0; i < fHpoints.size(); ++i) {
245  LOG(logINFO) << "scanning high vcal = " << fHpoints[i] << " (= " << 7*fHpoints[i] << " in low range)";
246  int cnt(0);
247  bool done = false;
248  while (!done){
249  try {
250  rresult = fApi->getPulseheightVsDAC("vcal", fHpoints[i], fHpoints[i], FLAGS, fParNtrig);
251  copy(rresult.begin(), rresult.end(), back_inserter(hresult));
252  done = true; // got our data successfully
253  }
254  catch(pxar::DataMissingEvent &e){
255  LOG(logCRITICAL) << "problem with readout: "<< e.what() << " missing " << e.numberMissing << " events";
256  ++cnt;
257  if (e.numberMissing > 10) done = true;
258  } catch(pxarException &e) {
259  LOG(logCRITICAL) << "pXar execption: "<< e.what();
260  ++cnt;
261  }
262  done = (cnt>2) || done;
263  }
264  }
265 
266  for (unsigned int i = 0; i < lresult.size(); ++i) {
267  int dac = lresult[i].first;
268  int dacbin(-1);
269  for (unsigned int v = 0; v < fLpoints.size(); ++v) {
270  if (fLpoints[v] == dac) {
271  dacbin = v;
272  break;
273  }
274  }
275  vector<pixel> vpix = lresult[i].second;
276  for (unsigned int ipx = 0; ipx < vpix.size(); ++ipx) {
277  int roc = vpix[ipx].roc();
278  int ic = vpix[ipx].column();
279  int ir = vpix[ipx].row();
280 
281  double val = vpix[ipx].value();
282  int idx = PixUtil::rcr2idx(getIdxFromId(roc), ic, ir);
283  if (idx > -1) fHists[idx]->fill(dacbin+1, val);
284 
285  }
286  }
287 
288  for (unsigned int i = 0; i < hresult.size(); ++i) {
289  int dac = hresult[i].first;
290  int dacbin(-1);
291  for (unsigned int v = 0; v < fHpoints.size(); ++v) {
292  if (fHpoints[v] == dac) {
293  dacbin = 100 + v;
294  break;
295  }
296  }
297  vector<pixel> vpix = hresult[i].second;
298  for (unsigned int ipx = 0; ipx < vpix.size(); ++ipx) {
299  int roc = vpix[ipx].roc();
300  int ic = vpix[ipx].column();
301  int ir = vpix[ipx].row();
302  double val = vpix[ipx].value();
303  int idx = PixUtil::rcr2idx(getIdxFromId(roc), ic, ir);
304  if (idx > -1) fHists[idx]->fill(dacbin+1, val);
305 
306  }
307  }
308 
309  printHistograms();
310 
311  PixTest::update();
312  restoreDacs();
313  LOG(logINFO) << "PixTestGainPedestal::measure() done ";
314 }
315 
316 
317 
318 
319 // ----------------------------------------------------------------------
320 void PixTestGainPedestal::fit() {
321  PixTest::update();
322  fDirectory->cd();
323 
324  string name = Form("gainPedestal_c%d_r%d_C%d", 0, 0, 0);
325  TH1D *h1 = bookTH1D(name, name, 1800, 0., 1800.);
326  h1->SetMinimum(0);
327  h1->SetMaximum(260.);
328  h1->SetNdivisions(506);
329  h1->SetMarkerStyle(20);
330  h1->SetMarkerSize(1.);
331 
332  TF1 *f(0);
333 
334  vector<vector<gainPedestalParameters> > v;
335  vector<uint8_t> rocIds = fApi->_dut->getEnabledRocIDs();
337  a.p0 = a.p1 = a.p2 = a.p3 = 0.;
338  TH1D* h(0);
339  vector<TH1D*> p1list;
340  double fracErr(0.05);
341  for (unsigned int i = 0; i < rocIds.size(); ++i) {
342  LOG(logDEBUG) << "Create hist " << Form("gainPedestalP1_C%d", rocIds[i]);
343  h = bookTH1D(Form("gainPedestalP1_C%d", i), Form("gainPedestalP1_C%d", rocIds[i]), 100, 0., 2.);
344  setTitles(h, "p1", "Entries / Bin");
345  p1list.push_back(h);
346  vector<gainPedestalParameters> vroc;
347  for (unsigned j = 0; j < 4160; ++j) {
348  vroc.push_back(a);
349  }
350  v.push_back(vroc);
351  }
352 
353  int iroc(0), ic(0), ir(0);
354  for (unsigned int i = 0; i < fHists.size(); ++i) {
355 
356  h1->Reset();
357  for (int ib = 0; ib < static_cast<int>(fLpoints.size()); ++ib) {
358  h1->SetBinContent(fLpoints[ib]+1, fHists[i]->get(ib+1));
359  h1->SetBinError(fLpoints[ib]+1, fracErr*fHists[i]->get(ib+1));
360  }
361  for (int ib = 0; ib < static_cast<int>(fHpoints.size()); ++ib) {
362  h1->SetBinContent(7*fHpoints[ib]+1, fHists[i]->get(100+ib+1));
363  h1->SetBinError(7*fHpoints[ib]+1, fracErr*fHists[i]->get(100+ib+1));
364  }
365 
366  f = fPIF->gpTanH(h1);
367  if (h1->Integral() < 1) continue;
368  PixUtil::idx2rcr(i, iroc, ic, ir);
369  if (fParShowFits) {
370  TH1D *hc = (TH1D*)h1->Clone(Form("gainPedestal_c%d_r%d_C%d", ic, ir, iroc));
371  hc->SetTitle(Form("gainPedestal_c%d_r%d_C%d", ic, ir, iroc));
372  string hcname = hc->GetName();
373  LOG(logDEBUG) << hcname;
374  hc->Fit(f, "r");
375  fHistList.push_back(hc);
376  PixTest::update();
377  } else {
378  // cout << Form("gainPedestal_c%d_r%d_C%d", ic, ir, iroc) << endl;
379  if (fParDumpHists) {
380  h1->SetTitle(Form("gainPedestal_c%d_r%d_C%d", ic, ir, iroc));
381  h1->SetName(Form("gainPedestal_c%d_r%d_C%d", ic, ir, iroc));
382  }
383  h1->Fit(f, "rq");
384  if (fParDumpHists) {
385  h1->SetDirectory(fDirectory);
386  h1->Write();
387  }
388  }
389  int idx = ic*80 + ir;
390  v[iroc][idx].p0 = f->GetParameter(0);
391  v[iroc][idx].p1 = f->GetParameter(1);
392  v[iroc][idx].p2 = f->GetParameter(2);
393  v[iroc][idx].p3 = f->GetParameter(3);
394 
395  p1list[getIdxFromId(iroc)]->Fill(f->GetParameter(1));
396  }
397 
398  fPixSetup->getConfigParameters()->setGainPedestalParameters(v);
399 
400  copy(p1list.begin(), p1list.end(), back_inserter(fHistList));
401  h = (TH1D*)(fHistList.back());
402  h->Draw();
403 
404  string p1MeanString(""), p1RmsString("");
405  for (unsigned int i = 0; i < p1list.size(); ++i) {
406  p1MeanString += Form(" %5.3f", p1list[i]->GetMean());
407  p1RmsString += Form(" %5.3f", p1list[i]->GetRMS());
408  }
409 
410  fDisplayedHist = find(fHistList.begin(), fHistList.end(), h);
411  PixTest::update();
412 
413  LOG(logINFO) << "PixTestGainPedestal::fit() done";
414  LOG(logINFO) << "p1 mean: " << p1MeanString;
415  LOG(logINFO) << "p1 RMS: " << p1RmsString;
416 }
417 
418 
419 
420 // ----------------------------------------------------------------------
421 void PixTestGainPedestal::saveGainPedestalParameters() {
422  fPixSetup->getConfigParameters()->writeGainPedestalParameters();
423 }
424 
425 
426 // ----------------------------------------------------------------------
427 void PixTestGainPedestal::printHistograms() {
428 
429  ofstream OutputFile;
430  vector<uint8_t> rocIds = fApi->_dut->getEnabledRocIDs();
431  int nRocs = static_cast<int>(rocIds.size());
432 
433  for (int iroc = 0; iroc < nRocs; ++iroc) {
434 
435  OutputFile.open(Form("%s/%s_C%d.dat", fPixSetup->getConfigParameters()->getDirectory().c_str(),
436  fPixSetup->getConfigParameters()->getGainPedestalFileName().c_str(),
437  iroc));
438 
439  OutputFile << "Pulse heights for the following Vcal values:" << endl;
440  OutputFile << "Low range: ";
441  for (unsigned int i = 0; i < fLpoints.size(); ++i) OutputFile << fLpoints[i] << " ";
442  OutputFile << endl;
443  OutputFile << "High range: ";
444  for (unsigned int i = 0; i < fHpoints.size(); ++i) OutputFile << fHpoints[i] << " ";
445  OutputFile << endl;
446  OutputFile << endl;
447 
448  int roc(0), ic(0), ir(0);
449  string line("");
450  for (int i = iroc*4160; i < (iroc+1)*4160; ++i) {
451  PixUtil::idx2rcr(i, roc, ic, ir);
452  if (roc != iroc) {
453  LOG(logDEBUG) << "BIG CONFUSION?! iroc = " << iroc << " roc = " << roc;
454  }
455 
456  line.clear();
457  for (int ib = 0; ib < static_cast<int>(fLpoints.size()); ++ib) {
458  line += Form(" %3d", static_cast<int>(fHists[i]->get(ib+1)));
459  }
460 
461  for (int ib = 0; ib < static_cast<int>(fHpoints.size()); ++ib) {
462  line += Form(" %3d", static_cast<int>(fHists[i]->get(100+ib+1)));
463  }
464 
465  line += Form(" Pix %2d %2d", ic, ir);
466  OutputFile << line << endl;
467  }
468 
469  OutputFile.close();
470  }
471 }
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
static int rcr2idx(int iroc, int icol, int irow)
convert ROC/COL/ROW into idx
Definition: PixUtil.cc:51
std::vector< std::pair< uint8_t, std::vector< pixel > > > getPulseheightVsDAC(std::string dacName, uint8_t dacMin, uint8_t dacMax, uint16_t flags, uint16_t nTriggers)
Definition: api.cc:675
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 fullTest()
function called when FullTest is running; most often this is simply calling doTest() ...
virtual bool setParameter(std::string parName, std::string sval)
set the string value of a parameter
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
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
void setToolTips()
implement this to provide updated tool tips if the user changes test parameters
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
void runCommand(std::string command)
allow execution of any button in the test
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
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
static void idx2rcr(int idx, int &iroc, int &icol, int &irow)
and back again
Definition: PixUtil.cc:59
PixInitFunc * fPIF
function instantiation and automatic initialization
Definition: PixTest.hh:292
void update()
signal to PixTab to update the canvas
Definition: PixTest.cc:569
pxar::pxarCore * fApi
pointer to the API
Definition: PixTest.hh:289
void init()
sets all test parameters
Definition: PixTest.cc:62
void doTest()
function connected to "DoTest" button of PixTab