pxar
 All Classes Namespaces Functions Variables Typedefs Friends
PixTestDacScan.cc
1 #include <stdlib.h> /* atof, atoi */
2 #include <algorithm> // std::find
3 #include <iostream>
4 
5 #include <TH1.h>
6 
7 #include "PixTestDacScan.hh"
8 #include "PixUtil.hh"
9 #include "log.h"
10 
11 
12 using namespace std;
13 using namespace pxar;
14 
15 ClassImp(PixTestDacScan)
16 
17 // ----------------------------------------------------------------------
18 PixTestDacScan::PixTestDacScan(PixSetup *a, std::string name) : PixTest(a, name), fParNtrig(-1), fParDAC("nada"), fParLoDAC(-1), fParHiDAC(-1) {
19  PixTest::init();
20  init();
21 }
22 
23 
24 //----------------------------------------------------------
25 PixTestDacScan::PixTestDacScan() : PixTest() {
26  // LOG(logINFO) << "PixTestDacScan ctor()";
27 }
28 
29 // ----------------------------------------------------------------------
30 bool PixTestDacScan::setParameter(string parName, string sval) {
31  bool found(false);
32  string str1, str2;
33  string::size_type s1;
34  int pixc, pixr;
35  std::transform(parName.begin(), parName.end(), parName.begin(), ::tolower);
36  for (unsigned int i = 0; i < fParameters.size(); ++i) {
37  if (fParameters[i].first == parName) {
38  found = true;
39  sval.erase(remove(sval.begin(), sval.end(), ' '), sval.end());
40  if (!parName.compare("phmap")) {
41  PixUtil::replaceAll(sval, "checkbox(", "");
42  PixUtil::replaceAll(sval, ")", "");
43  fParPHmap = atoi(sval.c_str());
44  setToolTips();
45  }
46  if (!parName.compare("ntrig")) {
47  fParNtrig = atoi(sval.c_str());
48  setToolTips();
49  }
50  if (!parName.compare("dac")) {
51  fParDAC = sval;
52  setToolTips();
53  }
54  if (!parName.compare("daclo")) {
55  fParLoDAC = atoi(sval.c_str());
56  setToolTips();
57  }
58  if (!parName.compare("dachi")) {
59  fParHiDAC = atoi(sval.c_str());
60  setToolTips();
61  }
62  if (!parName.compare("pix")) {
63  s1 = sval.find(",");
64  if (string::npos != s1) {
65  str1 = sval.substr(0, s1);
66  pixc = atoi(str1.c_str());
67  str2 = sval.substr(s1+1);
68  pixr = atoi(str2.c_str());
70  fPIX.push_back(make_pair(pixc, pixr));
71  addSelectedPixels(sval);
72  } else {
74  fPIX.push_back(make_pair(-1, -1));
75  addSelectedPixels("-1,-1");
76  }
77  }
78  break;
79  }
80  }
81 
82  return found;
83 }
84 
85 
86 // ----------------------------------------------------------------------
87 void PixTestDacScan::init() {
88  setToolTips();
89  fDirectory = gFile->GetDirectory(fName.c_str());
90  if (!fDirectory) {
91  fDirectory = gFile->mkdir(fName.c_str());
92  }
93  fDirectory->cd();
94 
95 }
96 
97 
98 // ----------------------------------------------------------------------
100  fTestTip = string(Form("scan the DAC %s and determine the number of hits vs DAC value\n", fParDAC.c_str()))
101  ;
102  fSummaryTip = string("summary plot to be implemented")
103  ;
104 
105 }
106 
107 // ----------------------------------------------------------------------
108 void PixTestDacScan::bookHist(string /*name*/) {
109  fDirectory->cd();
110 }
111 
112 
113 //----------------------------------------------------------
114 PixTestDacScan::~PixTestDacScan() {
115  LOG(logDEBUG) << "PixTestDacScan dtor";
116 }
117 
118 
119 // ----------------------------------------------------------------------
121  uint16_t FLAGS = FLAG_FORCE_MASKED; // required for manual loop over ROCs
122  fDirectory->cd();
123  TH1D *h1(0);
124  vector<TH1D*> vhist;
125  vector<uint8_t> rocIds = fApi->_dut->getEnabledRocIDs();
126  map<string, TH1D*> hmap;
127  string name(fParPHmap?"ph":"nhits");
128  string hname;
129  for (unsigned int iroc = 0; iroc < rocIds.size(); ++iroc){
130  for (unsigned int ip = 0; ip < fPIX.size(); ++ip) {
131  hname = Form("%s_%s_c%d_r%d_C%d", name.c_str(), fParDAC.c_str(), fPIX[ip].first, fPIX[ip].second, rocIds[iroc]);
132  h1 = bookTH1D(hname.c_str(), hname.c_str(), 256, 0., 256.);
133  h1->SetMinimum(0.);
134  setTitles(h1, Form("%s [DAC]", fParDAC.c_str()), (fParPHmap?"average PH":"readouts"));
135  hmap[hname] = h1;
136  fHistList.push_back(h1);
137  }
138 
139  }
140 
141 
142  PixTest::update();
143  LOG(logINFO) << "PixTestDacScan: " << fParDAC << "[" << fParLoDAC << ", " << fParHiDAC << "]"
144  << (fParPHmap?" average PH":" readouts")
145  << ", ntrig = " << fParNtrig
146  << ", npixels = " << fPIX.size();
147 
148  fApi->_dut->testAllPixels(false);
149  fApi->_dut->maskAllPixels(true);
150  vector<pair<uint8_t, vector<pixel> > > rresults, results;
151  int problems(0);
152  fNDaqErrors = 0;
153  for (unsigned int i = 0; i < fPIX.size(); ++i) {
154  if (fPIX[i].first > -1) {
155  fApi->_dut->testPixel(fPIX[i].first, fPIX[i].second, true);
156  fApi->_dut->maskPixel(fPIX[i].first, fPIX[i].second, false);
157 
158  bool done = false;
159  int cnt(0);
160  while (!done) {
161  try{
162  if (0 == fParPHmap) {
163  rresults = fApi->getEfficiencyVsDAC(fParDAC, fParLoDAC, fParHiDAC, FLAGS, fParNtrig);
164  fNDaqErrors = fApi->getStatistics().errors_pixel();
165  done = true;
166  } else {
167  rresults = fApi->getPulseheightVsDAC(fParDAC, fParLoDAC, fParHiDAC, FLAGS, fParNtrig);
168  fNDaqErrors = fApi->getStatistics().errors_pixel();
169  done = true;
170  }
171  } catch(DataMissingEvent &e){
172  LOG(logCRITICAL) << "problem with readout: "<< e.what() << " missing " << e.numberMissing << " events";
173  fNDaqErrors = 666666;
174  ++cnt;
175  if (e.numberMissing > 10) done = true;
176  } catch(pxarException &e) {
177  fNDaqErrors = 666667;
178  LOG(logCRITICAL) << "pXar execption: "<< e.what();
179  ++cnt;
180  }
181  done = (cnt>5) || done;
182  }
183 
184  if (fNDaqErrors > 0) problems = fNDaqErrors;
185  copy(rresults.begin(), rresults.end(), back_inserter(results));
186  fApi->_dut->testPixel(fPIX[i].first, fPIX[i].second, false);
187  fApi->_dut->maskPixel(fPIX[i].first, fPIX[i].second, true);
188  }
189  }
190 
191  LOG(logDEBUG) << " dacscandata.size(): " << results.size();
192  TH1D *h(0);
193  for (unsigned int iroc = 0; iroc < rocIds.size(); ++iroc){
194  for (unsigned int i = 0; i < results.size(); ++i) {
195  pair<uint8_t, vector<pixel> > v = results[i];
196  int idac = v.first;
197 
198  vector<pixel> vpix = v.second;
199  for (unsigned int ipix = 0; ipix < vpix.size(); ++ipix) {
200  if (vpix[ipix].roc() == rocIds[iroc]) {
201  hname = Form("%s_%s_c%d_r%d_C%d", name.c_str(), fParDAC.c_str(), vpix[ipix].column(), vpix[ipix].row(), rocIds[iroc]);
202  h = hmap[hname];
203  if (h) {
204  h->Fill(idac, vpix[ipix].value());
205  } else {
206  LOG(logDEBUG) << "XX did not find " << hname;
207  }
208  }
209 
210  }
211  }
212 
213  }
214 
215  LOG(logINFO) << "dac scan done" << (problems > 0? Form(" problems observed: %d", problems): " no problems seen");
216 
217  fDisplayedHist = fHistList.begin();
218  for (list<TH1*>::iterator il = fHistList.begin(); il != fHistList.end(); ++il) {
219  (*il)->Draw((getHistOption(*il)).c_str());
220  }
221  PixTest::update();
222 
223 }
224 
225 // ----------------------------------------------------------------------
226 TH1* PixTestDacScan::moduleMap(string histname) {
227  LOG(logDEBUG) << "PixTestDacScan::moduleMap histname: " << histname;
228  TH1* h0 = (*fDisplayedHist);
229  if (!h0->InheritsFrom(TH1::Class())) {
230  return 0;
231  }
232 
233  TH1D *h1 = (TH1D*)h0;
234  string h1name = h1->GetName();
235  string::size_type s1 = h1name.rfind("_c");
236  string barename = h1name.substr(0, s1);
237  LOG(logDEBUG) << "h1->GetName() = " << h1name << " -> " << barename;
238 
239  TH1 *h(0);
240  string hname;
241  int cycle(-1);
242  vector<uint8_t> rocIds = fApi->_dut->getEnabledRocIDs();
243  for (unsigned int iroc = 0; iroc < rocIds.size(); ++iroc){
244 
245  h1 = bookTH1D(Form("%s_C%d", barename.c_str(), rocIds[iroc]), Form("%s_C%d", barename.c_str(), rocIds[iroc]),
246  h0->GetNbinsX(), h0->GetBinLowEdge(1), h0->GetBinLowEdge(h0->GetNbinsX()+1));
247  if (0 == iroc) cycle = -1 + histCycle(Form("%s_C%d", barename.c_str(), rocIds[iroc]));
248 
249  list<TH1*>::iterator hbeg = fHistList.begin();
250  list<TH1*>::iterator hend = fHistList.end();
251  for (list<TH1*>::iterator il = hbeg; il != hend; ++il) {
252  h = (*il);
253  hname = h->GetName();
254  if (string::npos != hname.find(Form("%s", barename.c_str()))) {
255  if (string::npos != hname.find(Form("_C%d", rocIds[iroc]))) {
256  if (string::npos != hname.find(Form("_V%d", cycle))) {
257  for (int i = 1; i < h->GetNbinsX(); ++i) {
258  h1->Fill(h->GetBinCenter(i), h->GetBinContent(i));
259  }
260  }
261  }
262  }
263  }
264  fHistList.push_back(h1);
265  }
266 
267  fDisplayedHist = find(fHistList.begin(), fHistList.end(), h1);
268 
269  if (h1) h1->Draw();
270  update();
271  return h1;
272 }
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
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
int histCycle(std::string hname)
determine histogram cycle
Definition: PixTest.cc:921
statistics getStatistics()
Definition: api.cc:538
std::vector< std::pair< int, int > > fPIX
range of enabled pixels for time-consuming tests
Definition: PixTest.hh:311
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
void maskAllPixels(bool mask, uint8_t rocid)
Definition: dut.cc:481
void clearSelectedPixels()
clear selected pixel list
Definition: PixTest.cc:498
TDirectory * fDirectory
where the root histograms will end up
Definition: PixTest.hh:306
std::vector< std::pair< uint8_t, std::vector< pixel > > > getEfficiencyVsDAC(std::string dacName, uint8_t dacMin, uint8_t dacMax, uint16_t flags, uint16_t nTriggers)
Definition: api.cc:733
dut * _dut
Definition: api.h:728
void addSelectedPixels(std::string sval)
add a selected pixel to the internal parameter list
Definition: PixTest.cc:480
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
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
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
TH1 * moduleMap(std::string histname)
combine all available ROC maps into a module map
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
void doTest()
function connected to "DoTest" button of PixTab
pxar::pxarCore * fApi
pointer to the API
Definition: PixTest.hh:289
void setToolTips()
implement this to provide updated tool tips if the user changes test parameters
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