pxar
 All Classes Namespaces Functions Variables Typedefs Friends
PixTestDacDacScan.cc
1 #include <stdlib.h> /* atof, atoi */
2 #include <algorithm> // std::find
3 #include <iostream>
4 #include "PixTestDacDacScan.hh"
5 #include "PixUtil.hh"
6 #include "log.h"
7 
8 #include <TH2.h>
9 
10 using namespace std;
11 using namespace pxar;
12 
13 ClassImp(PixTestDacDacScan)
14 
15 // ----------------------------------------------------------------------
16 PixTestDacDacScan::PixTestDacDacScan(PixSetup *a, std::string name) :
17 PixTest(a, name), fParNtrig(-1), fParPHmap(0), fParDAC1("nada"), fParDAC2("nada"),
18  fParLoDAC1(-1), fParHiDAC1(-1), fParLoDAC2(-1), fParHiDAC2(-1) {
19  PixTest::init();
20  init();
21  LOG(logDEBUG) << "PixTestDacDacScan ctor(PixSetup &a, string, TGTab *)";
22 }
23 
24 
25 //----------------------------------------------------------
26 PixTestDacDacScan::PixTestDacDacScan() : PixTest() {
27  LOG(logDEBUG) << "PixTestDacDacScan ctor()";
28 }
29 
30 // ----------------------------------------------------------------------
31 bool PixTestDacDacScan::setParameter(string parName, string sval) {
32  bool found(false);
33  string str1, str2;
34  string::size_type s1;
35  int pixc, pixr;
36  std::transform(parName.begin(), parName.end(), parName.begin(), ::tolower);
37  for (unsigned int i = 0; i < fParameters.size(); ++i) {
38  if (fParameters[i].first == parName) {
39  found = true;
40  sval.erase(remove(sval.begin(), sval.end(), ' '), sval.end());
41  if (!parName.compare("ntrig")) {
42  fParNtrig = atoi(sval.c_str());
43  setToolTips();
44  }
45  if (!parName.compare("phmap")) {
46  PixUtil::replaceAll(sval, "checkbox(", "");
47  PixUtil::replaceAll(sval, ")", "");
48  fParPHmap = atoi(sval.c_str());
49  setToolTips();
50  }
51  if (!parName.compare("dac1")) {
52  fParDAC1 = sval;
53  setToolTips();
54  }
55  if (!parName.compare("dac2")) {
56  fParDAC2 = sval;
57  setToolTips();
58  }
59  if (!parName.compare("dac1lo")) {
60  fParLoDAC1 = atoi(sval.c_str());
61  setToolTips();
62  }
63  if (!parName.compare("dac1hi")) {
64  fParHiDAC1 = atoi(sval.c_str());
65  setToolTips();
66  }
67  if (!parName.compare("dac2lo")) {
68  fParLoDAC2 = atoi(sval.c_str());
69  setToolTips();
70  }
71  if (!parName.compare("dac2hi")) {
72  fParHiDAC2 = atoi(sval.c_str());
73  setToolTips();
74  }
75  if (!parName.compare("pix")) {
76  s1 = sval.find(",");
77  if (string::npos != s1) {
78  str1 = sval.substr(0, s1);
79  pixc = atoi(str1.c_str());
80  str2 = sval.substr(s1+1);
81  pixr = atoi(str2.c_str());
83  fPIX.push_back(make_pair(pixc, pixr));
84  addSelectedPixels(sval);
85  } else {
87  fPIX.push_back(make_pair(-1, -1));
88  addSelectedPixels("-1,-1");
89  }
90  }
91 
92  break;
93  }
94  }
95 
96  return found;
97 }
98 
99 
100 // ----------------------------------------------------------------------
101 void PixTestDacDacScan::init() {
102  LOG(logDEBUG) << "PixTestDacDacScan::init()";
103 
104  setToolTips();
105  fDirectory = gFile->GetDirectory(fName.c_str());
106  if (!fDirectory) {
107  fDirectory = gFile->mkdir(fName.c_str());
108  }
109  fDirectory->cd();
110 
111 }
112 
113 
114 // ----------------------------------------------------------------------
116  fTestTip = string(Form("scan the two DACs %s vs %s and ", fParDAC1.c_str(), fParDAC2.c_str()))
117  + string("determine the number of hits for each setting")
118  + string("\nNOTE: There is currently a limitation that the total number of scanned points is less than 2^14!")
119  ;
120  fSummaryTip = string("summary plot to be implemented")
121  ;
122 
123 }
124 
125 // ----------------------------------------------------------------------
126 void PixTestDacDacScan::bookHist(string /*name*/) {
127  fDirectory->cd();
128  // LOG(logDEBUG) << "nothing done with " << name;
129 
130 }
131 
132 
133 //----------------------------------------------------------
134 PixTestDacDacScan::~PixTestDacDacScan() {
135  LOG(logDEBUG) << "PixTestDacDacScan dtor";
136 }
137 
138 
139 // ----------------------------------------------------------------------
141  // uint16_t FLAGS = FLAG_FORCE_SERIAL | FLAG_FORCE_MASKED; // required for manual loop over ROCs
142  uint16_t FLAGS = FLAG_FORCE_MASKED; // required for manual loop over ROCs
143  fDirectory->cd();
144  PixTest::update();
145 
146  string zname = fParDAC1 + string(":") + fParDAC2;
147  bookHist(zname);
148  string::size_type s1 = zname.find(":");
149  string dac1 = zname.substr(0, s1);
150  string dac2 = zname.substr(s1+1);
151  LOG(logINFO) << "PixTestDacDacScan: " << dac1 << "[" << fParLoDAC1 << ", " << fParHiDAC1 << "]"
152  << " vs. " << dac2 << "[" << fParLoDAC2 << ", " << fParHiDAC2 << "]"
153  << (fParPHmap?" average PH":" readouts")
154  << ", ntrig = " << fParNtrig
155  << ", npixels = " << fPIX.size();
156 
157  TH2D *h2(0);
158  map<string, TH2D*> maps;
159  vector<uint8_t> rocIds = fApi->_dut->getEnabledRocIDs();
160  string name(fParPHmap?"ph":"nhits");
161  int nx = fApi->getDACRange(dac1)+1;
162  int ny = fApi->getDACRange(dac2)+1;
163  for (unsigned int iroc = 0; iroc < rocIds.size(); ++iroc){
164  for (unsigned int ip = 0; ip < fPIX.size(); ++ip) {
165  h2 = bookTH2D(Form("%s_%s_%s_c%d_r%d_C%d", name.c_str(), dac1.c_str(), dac2.c_str(), fPIX[ip].first, fPIX[ip].second, rocIds[iroc]),
166  Form("%s_%s_%s_c%d_r%d_C%d", name.c_str(), dac1.c_str(), dac2.c_str(), fPIX[ip].first, fPIX[ip].second, rocIds[iroc]),
167  nx, 0., static_cast<double>(nx), ny, 0., static_cast<double>(ny));
168  if (0 == fParPHmap) h2->SetMinimum(0.);
169  setTitles(h2, dac1.c_str(), dac2.c_str());
170  fHistList.push_back(h2);
171  fHistOptions.insert(make_pair(h2, "colz"));
172  maps.insert(make_pair(Form("%s_%s_%s_c%d_r%d_C%d",
173  name.c_str(), dac1.c_str(), dac2.c_str(), fPIX[ip].first, fPIX[ip].second, rocIds[iroc]), h2));
174  }
175 
176  }
177 
178  fApi->_dut->testAllPixels(false);
179  fApi->_dut->maskAllPixels(true);
180  vector<pair<uint8_t, pair<uint8_t, vector<pixel> > > > rresults, results;
181  int problems(0);
182  fNDaqErrors = 0;
183  for (unsigned int i = 0; i < fPIX.size(); ++i) {
184  if (fPIX[i].first > -1) {
185  fApi->_dut->testPixel(fPIX[i].first, fPIX[i].second, true);
186  fApi->_dut->maskPixel(fPIX[i].first, fPIX[i].second, false);
187  bool done = false;
188  int cnt(0);
189  while (!done) {
190  try{
191  if (0 == fParPHmap) {
192  rresults = fApi->getEfficiencyVsDACDAC(fParDAC1, fParLoDAC1, fParHiDAC1,
193  fParDAC2, fParLoDAC2, fParHiDAC2, FLAGS, fParNtrig);
194  fNDaqErrors = fApi->getStatistics().errors_pixel();
195  done = true;
196  } else {
197  rresults = fApi->getPulseheightVsDACDAC(fParDAC1, fParLoDAC1, fParHiDAC1,
198  fParDAC2, fParLoDAC2, fParHiDAC2, FLAGS, fParNtrig);
199  fNDaqErrors = fApi->getStatistics().errors_pixel();
200  done = true;
201  }
202  } catch(DataMissingEvent &e){
203  LOG(logCRITICAL) << "problem with readout: "<< e.what() << " missing " << e.numberMissing << " events";
204  fNDaqErrors = 666666;
205  ++cnt;
206  if (e.numberMissing > 10) done = true;
207  } catch(pxarException &e) {
208  LOG(logCRITICAL) << "pXar execption: "<< e.what();
209  fNDaqErrors = 666667;
210  ++cnt;
211  }
212  done = (cnt>5) || done;
213  }
214 
215  if (fNDaqErrors > 0) problems = fNDaqErrors;
216  copy(rresults.begin(), rresults.end(), back_inserter(results));
217 
218  fApi->_dut->testPixel(fPIX[i].first, fPIX[i].second, false);
219  fApi->_dut->maskPixel(fPIX[i].first, fPIX[i].second, true);
220  }
221  }
222 
223  TH2D *h(0);
224  for (unsigned int iroc = 0; iroc < rocIds.size(); ++iroc){
225  for (unsigned int i = 0; i < results.size(); ++i) {
226  pair<uint8_t, pair<uint8_t, vector<pixel> > > v = results[i];
227  int idac1 = v.first;
228  pair<uint8_t, vector<pixel> > w = v.second;
229  int idac2 = w.first;
230  vector<pixel> wpix = w.second;
231 
232  for (unsigned ipix = 0; ipix < wpix.size(); ++ipix) {
233  if (wpix[ipix].roc() == rocIds[iroc]) {
234  h = maps[Form("%s_%s_%s_c%d_r%d_C%d",
235  name.c_str(), fParDAC1.c_str(), fParDAC2.c_str(), wpix[ipix].column(), wpix[ipix].row(), rocIds[iroc])];
236  if (h) {
237  h->Fill(idac1, idac2, wpix[ipix].value());
238  } else {
239  LOG(logDEBUG) << "XX did not find "
240  << Form("%s_%s_%s_c%d_r%d_C%d",
241  name.c_str(), fParDAC1.c_str(), fParDAC2.c_str(), wpix[ipix].column(), wpix[ipix].row(), rocIds[iroc]);
242  }
243 
244  }
245  }
246 
247  }
248 
249  }
250 
251  LOG(logINFO) << "dac dac scan done" << (problems > 0? Form(" problems observed: %d", problems): " no problems seen");
252  fDisplayedHist = find(fHistList.begin(), fHistList.end(), h);
253  if (h) h->Draw(getHistOption(h).c_str());
254  PixTest::update();
255 
256 }
257 
258 
259 // ----------------------------------------------------------------------
260 TH1* PixTestDacDacScan::moduleMap(string histname) {
261  LOG(logDEBUG) << "PixTestDacScan::moduleMap histname: " << histname;
262  TH1* h0 = (*fDisplayedHist);
263  if (!h0->InheritsFrom(TH2::Class())) {
264  return 0;
265  }
266 
267  TH2D *h1 = (TH2D*)h0;
268  string h1name = h1->GetName();
269  string::size_type s1 = h1name.rfind("_c");
270  string barename = h1name.substr(0, s1);
271  LOG(logDEBUG) << "h1->GetName() = " << h1name << " -> " << barename;
272 
273  TH2D *h(0);
274  string hname;
275  int cycle(-1);
276  vector<uint8_t> rocIds = fApi->_dut->getEnabledRocIDs();
277  for (unsigned int iroc = 0; iroc < rocIds.size(); ++iroc){
278 
279  h1 = bookTH2D(Form("%s_C%d", barename.c_str(), rocIds[iroc]), Form("%s_C%d", barename.c_str(), rocIds[iroc]),
280  h0->GetNbinsX(), h0->GetXaxis()->GetBinLowEdge(1), h0->GetXaxis()->GetBinLowEdge(h0->GetNbinsX()+1),
281  h0->GetNbinsY(), h0->GetYaxis()->GetBinLowEdge(1), h0->GetYaxis()->GetBinLowEdge(h0->GetNbinsY()+1));
282  fHistOptions.insert(make_pair(h1, "colz"));
283  if (0 == iroc) cycle = -1 + histCycle(Form("%s_C%d", barename.c_str(), rocIds[iroc]));
284 
285  list<TH1*>::iterator hbeg = fHistList.begin();
286  list<TH1*>::iterator hend = fHistList.end();
287  cout << "Search for: " << Form("%s", barename.c_str()) << endl;
288  for (list<TH1*>::iterator il = hbeg; il != hend; ++il) {
289  h = dynamic_cast<TH2D*>(*il);
290  hname = h->GetName();
291  cout << " looking at " << hname << endl;
292  if (string::npos != hname.find(Form("%s", barename.c_str()))) {
293  if (string::npos != hname.rfind(Form("_C%d", rocIds[iroc]))) {
294  if (string::npos != hname.rfind(Form("_V%d", cycle))) {
295  for (int ix = 1; ix < h->GetNbinsX(); ++ix) {
296  for (int iy = 1; iy < h->GetNbinsX(); ++iy) {
297  h1->Fill(h->GetBinCenter(ix), h->GetBinCenter(iy), h->GetBinContent(ix, iy));
298  }
299  }
300  }
301  }
302  }
303  }
304  fHistList.push_back(h1);
305  }
306 
307  fDisplayedHist = find(fHistList.begin(), fHistList.end(), h1);
308 
309  if (h1) h1->Draw(getHistOption(h1).c_str());
310  update();
311  return h1;
312 }
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::map< TH1 *, std::string > fHistOptions
options can be stored with each histogram
Definition: PixTest.hh:308
int histCycle(std::string hname)
determine histogram cycle
Definition: PixTest.cc:921
statistics getStatistics()
Definition: api.cc:538
uint8_t getDACRange(std::string dacName)
Definition: api.cc:619
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
dut * _dut
Definition: api.h:728
void addSelectedPixels(std::string sval)
add a selected pixel to the internal parameter list
Definition: PixTest.cc:480
TH1 * moduleMap(std::string histname)
combine all available ROC maps into a module map
std::vector< std::pair< uint8_t, std::pair< uint8_t, std::vector< pixel > > > > getPulseheightVsDACDAC(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:878
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 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
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
virtual bool setParameter(std::string parName, std::string sval)
set the string value of a parameter
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
void doTest()
function connected to "DoTest" button of PixTab
pxar::pxarCore * fApi
pointer to the API
Definition: PixTest.hh:289
void init()
sets all test parameters
Definition: PixTest.cc:62
void setToolTips()
implement this to provide updated tool tips if the user changes test parameters