pxar
 All Classes Namespaces Functions Variables Typedefs Friends
PixTestPh.cc
1 #include <stdlib.h> // atof, atoi
2 #include <algorithm> // std::find
3 #include <sstream> // parsing
4 
5 #include "PixTestPh.hh"
6 #include "log.h"
7 
8 using namespace std;
9 using namespace pxar;
10 
11 ClassImp(PixTestPh)
12 
13 //------------------------------------------------------------------------------
14 PixTestPh::PixTestPh( PixSetup *a, std::string name ) : PixTest(a, name),
15  fParNtrig(-1), fParDAC("nada"), fParDacVal(100) {
16  PixTest::init();
17  init();
18 }
19 
20 
21 //------------------------------------------------------------------------------
22 PixTestPh::PixTestPh() : PixTest() {
23  // LOG(logDEBUG) << "PixTestPh ctor()";
24 }
25 
26 //------------------------------------------------------------------------------
27 bool PixTestPh::setParameter(string parName, string sval) {
28  bool found(false);
29  string str1, str2;
30  string::size_type s1;
31  int pixc, pixr;
32  std::transform(parName.begin(), parName.end(), parName.begin(), ::tolower);
33  for (unsigned int i = 0; i < fParameters.size(); ++i) {
34  if (!fParameters[i].first.compare(parName)) {
35  found = true;
36  sval.erase(remove(sval.begin(), sval.end(), ' '), sval.end());
37  if (!parName.compare("ntrig")) {
38  setTestParameter("ntrig", sval);
39  fParNtrig = atoi( sval.c_str() );
40  LOG(logDEBUG) << " setting fParNtrig ->" << fParNtrig
41  << "<- from sval = " << sval;
42  }
43  if (!parName.compare("dac")) {
44  setTestParameter("dac", sval);
45  fParDAC = sval;
46  LOG(logDEBUG) << " setting fParDAC ->" << fParDAC
47  << "<- from sval = " << sval;
48  }
49 
50  if (!parName.compare("dacval")) {
51  setTestParameter("dacval", sval);
52  fParDacVal = atoi(sval.c_str());
53  LOG(logDEBUG) << " setting fParDacVal ->" << fParDacVal
54  << "<- from sval = " << sval;
55  }
56 
57  if (!parName.compare("pix")) {
58  s1 = sval.find(",");
59  if (string::npos != s1) {
60  str1 = sval.substr(0, s1);
61  pixc = atoi(str1.c_str());
62  str2 = sval.substr(s1+1);
63  pixr = atoi(str2.c_str());
65  fPIX.push_back(make_pair(pixc, pixr));
66  addSelectedPixels(sval);
67  LOG(logDEBUG) << " adding to FPIX ->" << pixc << "/" << pixr << " fPIX.size() = " << fPIX.size() ;
68  } else {
70  addSelectedPixels("-1,-1");
71  LOG(logDEBUG) << " clear fPIX: " << fPIX.size();
72  }
73  }
74  break;
75  }
76  }
77  return found;
78 }
79 
80 //------------------------------------------------------------------------------
81 void PixTestPh::init() {
82  fDirectory = gFile->GetDirectory(fName.c_str());
83  if( !fDirectory) {
84  fDirectory = gFile->mkdir(fName.c_str());
85  }
86  fDirectory->cd();
87 }
88 
89 //------------------------------------------------------------------------------
90 void PixTestPh::bookHist(string name) {
91  LOG(logDEBUG) << "nothing done with " << name;
92 }
93 
94 //------------------------------------------------------------------------------
95 PixTestPh::~PixTestPh() {
96 }
97 
98 //------------------------------------------------------------------------------
100 
101  cacheDacs();
102  fDirectory->cd();
103  PixTest::update();
104 
105  uint16_t FLAGS = FLAG_FORCE_MASKED | FLAG_FORCE_SERIAL;
106  LOG(logDEBUG) << " using FLAGS = " << (int)FLAGS;
107 
108  TH1D *h1(0);
109  vector<uint8_t> rocIds = fApi->_dut->getEnabledRocIDs();
110  map<string, TH1D*> hists;
111  string name;
112  for (unsigned int iroc = 0; iroc < rocIds.size(); ++iroc){
113  for (unsigned int i = 0; i < fPIX.size(); ++i) {
114  if (fPIX[i].first > -1) {
115  name = Form("PH_c%d_r%d_C%d", fPIX[i].first, fPIX[i].second, rocIds[iroc]);
116  h1 = bookTH1D(name, name, 256, 0., 256.);
117  h1->SetMinimum(0);
118  setTitles(h1, Form("PH [ADC] for %s = %d", fParDAC.c_str(), fParDacVal), "Entries/bin");
119  hists.insert(make_pair(name, h1));
120  fHistList.push_back(h1);
121  }
122  }
123  }
124 
125  fApi->setDAC(fParDAC, fParDacVal);
126 
127  fApi->_dut->testAllPixels(false);
128  fApi->_dut->maskAllPixels(true);
129  vector<pair<uint8_t, vector<pixel> > > rresult, result;
130  for (int ievt = 0; ievt < fParNtrig; ++ievt) {
131  for (unsigned int i = 0; i < fPIX.size(); ++i) {
132  if (fPIX[i].first > -1) {
133  fApi->_dut->testPixel(fPIX[i].first, fPIX[i].second, true);
134  fApi->_dut->maskPixel(fPIX[i].first, fPIX[i].second, false);
135 
136  int cnt(0);
137  bool done(false);
138  while (!done) {
139  try {
140  rresult = fApi->getPulseheightVsDAC(fParDAC, fParDacVal, fParDacVal, FLAGS, 1);
141  done = true;
142  } catch(pxarException &e) {
143  LOG(logCRITICAL) << "pXar execption: "<< e.what();
144  ++cnt;
145  }
146  done = (cnt>5) || done;
147  }
148 
149  copy(rresult.begin(), rresult.end(), back_inserter(result));
150  fApi->_dut->testPixel(fPIX[i].first, fPIX[i].second, false);
151  fApi->_dut->maskPixel(fPIX[i].first, fPIX[i].second, true);
152  }
153  }
154  }
155 
156  for (unsigned int i = 0; i < result.size(); ++i) {
157  vector<pixel> vpix = result[i].second;
158  for (unsigned int ipx = 0; ipx < vpix.size(); ++ipx) {
159  int roc = vpix[ipx].roc();
160  int ic = vpix[ipx].column();
161  int ir = vpix[ipx].row();
162  name = Form("PH_c%d_r%d_C%d", ic, ir, roc);
163  h1 = hists[name];
164  if (h1) {
165  h1->Fill(vpix[ipx].value());
166  } else {
167  LOG(logDEBUG) << " histogram " << Form("PH_c%d_r%d_C%d", ic, ir, roc) << " not found";
168  }
169  } // ipx
170  } // dac values
171 
172  for (list<TH1*>::iterator il = fHistList.begin(); il != fHistList.end(); ++il) {
173  (*il)->Draw();
174  PixTest::update();
175  }
176  fDisplayedHist = find(fHistList.begin(), fHistList.end(), h1);
177 
178  restoreDacs();
179 }
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
void doTest()
function connected to "DoTest" button of PixTab
Definition: PixTestPh.cc:99
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
void restoreDacs(bool verbose=false)
restore all DACs
Definition: PixTest.cc:771
dut * _dut
Definition: api.h:728
void addSelectedPixels(std::string sval)
add a selected pixel to the internal parameter list
Definition: PixTest.cc:480
bool setTestParameter(std::string parname, std::string value)
change the local parameter
Definition: PixTest.cc:514
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
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
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
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: PixTestPh.cc:27