5 #include <TStopwatch.h>
9 #include "PixTestBarePretest.hh"
25 PixTestBarePretest::PixTestBarePretest() :
PixTest() {
36 std::transform(parName.begin(), parName.end(), parName.begin(), ::tolower);
37 for (
unsigned int i = 0; i <
fParameters.size(); ++i) {
40 sval.erase(
remove(sval.begin(), sval.end(),
' '), sval.end());
42 if (!parName.compare(
"targetia")) {
43 fTargetIa = atoi(sval.c_str());
44 LOG(logDEBUG) <<
"setting fTargetIa = " << fTargetIa <<
" mA/ROC";
47 if (!parName.compare(
"noisewidth")) {
48 fNoiseWidth = atoi(sval.c_str());
49 LOG(logDEBUG) <<
"setting fNoiseWidth = " << fNoiseWidth <<
" DAC units";
52 if (!parName.compare(
"noisemargin")) {
53 fNoiseMargin = atoi(sval.c_str());
54 LOG(logDEBUG) <<
"setting fNoiseMargin = " << fNoiseMargin <<
" DAC units";
57 if (!parName.compare(
"ntrig") ) {
58 fParNtrig = atoi(sval.c_str() );
59 LOG(logDEBUG) <<
"setting fParNtrig = " << fParNtrig;
62 if (!parName.compare(
"vcal") ) {
63 fParVcal = atoi(sval.c_str() );
64 LOG(logDEBUG) <<
"setting fParVcal = " << fParVcal;
67 if (!parName.compare(
"deltavthrcomp") ) {
68 fParDeltaVthrComp = atoi(sval.c_str() );
69 LOG(logDEBUG) <<
"setting fParDeltaVthrComp = " << fParDeltaVthrComp;
72 if (!parName.compare(
"pix") || !parName.compare(
"pix1") ) {
74 if (string::npos != s1) {
75 str1 = sval.substr(0, s1);
76 pixc = atoi(str1.c_str());
77 str2 = sval.substr(s1+1);
78 pixr = atoi(str2.c_str());
79 fPIX.push_back( make_pair(pixc, pixr) );
90 void PixTestBarePretest::init() {
92 fDirectory = gFile->GetDirectory(fName.c_str());
102 fTestTip = string(
"Pretest: set Vana, VthrComp, and CalDel")
103 + string(
"\ntune PH into ADC range using VOffsetRO and VIref_ADC")
105 fSummaryTip = string(
"summary plot to be implemented");
109 void PixTestBarePretest::bookHist(
string name) {
112 LOG(logDEBUG) <<
"nothing done with " << name;
116 PixTestBarePretest::~PixTestBarePretest() {
117 LOG(logDEBUG) <<
"PixTestBarePretest dtor";
125 bigBanner(Form(
"PixTestBarePretest::doTest()"));
128 TH1 *h1 = (*fDisplayedHist);
133 bigBanner(
"ERROR: some ROCs are not programmable; stop");
138 h1 = (*fDisplayedHist);
143 h1 = (*fDisplayedHist);
149 LOG(logINFO) <<
"PixTestBarePretest::doTest() done";
154 std::transform(command.begin(), command.end(), command.begin(), ::tolower);
155 LOG(logDEBUG) <<
"running command: " << command;
156 if (!command.compare(
"programroc")) {
160 if (!command.compare(
"setcaldel")) {
164 if (!command.compare(
"savedacs")) {
168 if (!command.compare(
"setvana")) {
172 if (!command.compare(
"setvthrcompcaldel")) {
176 if (!command.compare(
"setvthrcompid")) {
181 LOG(logDEBUG) <<
"did not find command ->" << command <<
"<-";
186 void PixTestBarePretest::setVana() {
190 banner(Form(
"PixTestBarePretest::setVana() target Ia = %d mA/ROC", fTargetIa));
194 vector<uint8_t> vanaStart;
195 vector<double> rocIana;
199 for (
int iroc = 0; iroc < nRocs; ++iroc) {
201 rocIana.push_back(0.);
213 }
while (sw.RealTime() < 0.1);
216 double i015 = (nRocs-1) * i016 / nRocs;
217 LOG(logDEBUG) <<
"offset current from other " << nRocs-1 <<
" ROCs is " << i015 <<
" mA";
221 const double extra = 0.1;
222 const double eps = 0.25;
223 const double slope = 6;
225 for (
int roc = 0; roc < nRocs; ++roc) {
227 LOG(logDEBUG) <<
"skipping ROC idx = " << roc <<
" (not selected) for Vana tuning";
230 int vana = vanaStart[roc];
238 }
while (sw.RealTime() < 0.1);
240 double diff = fTargetIa + extra - (ia - i015);
243 LOG(logDEBUG) <<
"ROC " << roc <<
" iter " << iter
245 <<
" Ia " << ia-i015 <<
" mA";
247 while (TMath::Abs(diff) > eps && iter < 11 && vana > 0 && vana < 255) {
249 int stp =
static_cast<int>(TMath::Abs(slope*diff));
250 if (stp == 0) stp = 1;
251 if (diff < 0) stp = -stp;
271 while( sw.RealTime() < 0.1 );
273 diff = fTargetIa + extra - (ia - i015);
275 LOG(logDEBUG) <<
"ROC " << setw(2) << roc
276 <<
" iter " << setw(2) << iter
277 <<
" Vana " << setw(3) << vana
278 <<
" Ia " << ia-i015 <<
" mA";
281 rocIana[roc] = ia-i015;
282 vanaStart[roc] = vana;
287 TH1D *hsum =
bookTH1D(
"VanaSettings",
"Vana per ROC", nRocs, 0., nRocs);
291 hsum->SetMaximum(256);
294 TH1D *hcurr =
bookTH1D(
"Iana",
"Iana per ROC", nRocs, 0., nRocs);
297 hcurr->SetMinimum(0);
298 hcurr->SetMaximum(30.0);
303 for (
int roc = 0; roc < nRocs; ++roc) {
306 LOG(logDEBUG) <<
"ROC " << setw(2) << roc <<
" Vana " << setw(3) << int(vanaStart[roc]);
309 hsum->Fill(roc, vanaStart[roc] );
310 hcurr->Fill(roc, rocIana[roc]);
320 while( sw.RealTime() < 0.1 );
327 LOG(logINFO) <<
"PixTestBarePretest::setVana() done, Module Ia " << ia16 <<
" mA = " << ia16/nRocs <<
" mA/ROC";
333 void PixTestBarePretest::setVthrCompCalDel() {
334 uint16_t FLAGS = FLAG_FORCE_SERIAL | FLAG_FORCE_MASKED | FLAG_CALS;
339 banner(Form(
"PixTestBarePretest::setVthrCompCalDel()"));
341 string name(
"pretestVthrCompCalDel");
351 std::cout <<
"at the beginning the trigger: " << fParNtrig << std::endl;
354 vector<pair<uint8_t, pair<uint8_t, vector<pixel> > > > rresults;
357 h1 =
bookTH1D(Form(
"pretestCalDel"), Form(
"pretestCalDel"), rocIds.size(), 0., rocIds.size());
366 vector<int> calDel(rocIds.size(), -1);
367 vector<int> vthrComp(rocIds.size(), -1);
368 vector<int> calDelE(rocIds.size(), -1);
370 for (
unsigned int iroc = 0; iroc < rocIds.size(); ++iroc){
377 h2 =
bookTH2D(Form(
"%s_c%d_r%d_C%d", name.c_str(),
fPIX[ip].first,
fPIX[ip].second, rocIds[iroc]),
378 Form(
"%s_c%d_r%d_C%d", name.c_str(),
fPIX[ip].first,
fPIX[ip].second, rocIds[iroc]),
379 255, 0., 255., 255, 0., 255.);
384 h_optimize =
bookTH1D(Form(
"VthrComp-Optimized"), Form(
"VthrComp-Optimized"),255, 0., 255);
392 LOG(logDEBUG) <<
" looking at roc = " <<
static_cast<unsigned int>(rocIds[iroc])
393 <<
" pixel col = " <<
fPIX[ip].first <<
", row = " <<
fPIX[ip].second
402 LOG(logCRITICAL) <<
"problem with readout: "<< e.what() <<
" missing " << e.numberMissing <<
" events";
404 if (e.numberMissing > 10) done =
true;
406 LOG(logCRITICAL) <<
"pXar execption: "<< e.what();
409 done = (cnt>5) || done;
415 for (
unsigned i = 0; i < rresults.size(); ++i) {
416 pair<uint8_t, pair<uint8_t, vector<pixel> > > v = rresults[i];
418 pair<uint8_t, vector<pixel> > w = v.second;
420 vector<pixel> wpix = w.second;
421 for (
unsigned ipix = 0; ipix < wpix.size(); ++ipix) {
422 if (wpix[ipix].roc() == rocIds[iroc]) {
423 if (wpix[ipix].value() >= fParNtrig/2){
424 h2->Fill(idac1, idac2, wpix[ipix].value());
427 LOG(logDEBUG) <<
"ghost ROC " <<
static_cast<unsigned int>(wpix[ipix].roc()) <<
" seen with pixel "
428 << static_cast<unsigned int>(wpix[ipix].column()) <<
" " << static_cast<unsigned int>(wpix[ipix].row());
433 TH1D *h0 = h2->ProjectionX(
"_px", fParDeltaVthrComp+1, fParDeltaVthrComp+2);
444 TH1D *hy = h2->ProjectionY(
"_py", 5, h2->GetNbinsX());
445 int top = hy->FindLastBinAbove(1.);
451 for (itop = top; itop > 0; --itop) {
452 h0 = h2->ProjectionX(
"_px", itop, itop);
454 for (
int i = 0; i < h0->GetNbinsX(); ++i) {
455 if (fParNtrig == h0->GetBinContent(i)) cnt++;
469 int dac1Mean = int ( h2->GetMean( 1 ) );
470 int i1 = h2->GetXaxis()->FindBin( dac1Mean);
472 cout <<
"Mean ist at Bin: " << i1 << endl;
476 for(
int j = 0; j < 255; ++j ) {
478 int cnt = h2->GetBinContent( i1, j+1 );
481 h_optimize->Fill(j, cnt);
494 std::cout <<
"Number of bins x- "
495 << h2->GetXaxis()->GetNbins()
498 <<
" at bin: " << i1 << endl;
499 std::cout <<
"Number of bins y- "
500 << h2->GetYaxis()->GetNbins()
502 << h2->GetMean(2) << endl;
504 cout <<
" plateau from " << i0 <<
" to " << i9 << endl;
506 int dac2Best = i0 + ( i9 - i0 ) / 4;
510 calDelE[iroc] =
static_cast<int>(0.);
511 calDel[iroc] =
static_cast<int>(h2->GetMean( 1 ));
513 vthrComp[iroc] = dac2Best;
516 h1->SetBinContent(rocIds[iroc]+1, calDel[iroc]);
517 h1->SetBinError(rocIds[iroc]+1, calDelE[iroc]);
518 LOG(logDEBUG) <<
"CalDel: " << calDel[iroc] <<
" +/- " << calDelE[iroc];
521 TMarker *tm =
new TMarker();
522 tm->SetMarkerSize(1);
523 tm->SetMarkerStyle(21);
524 tm->DrawMarker(calDel[iroc], vthrComp[iroc]);
537 string caldelString(
""), vthrcompString(
"");
546 for (
unsigned int iroc = 0; iroc < rocIds.size(); ++iroc){
547 fApi->
setDAC(
"CalDel", calDel[iroc], rocIds[iroc]);
548 caldelString += Form(
" %4d", calDel[iroc]);
549 fApi->
setDAC(
"VthrComp", vthrComp[iroc], rocIds[iroc]);
550 vthrcompString += Form(
" %4d", vthrComp[iroc]);
555 LOG(logINFO) <<
"PixTestBarePretest::setVthrCompCalDel() done";
556 LOG(logINFO) <<
"CalDel: " << caldelString;
557 LOG(logINFO) <<
"VthrComp: " << vthrcompString;
561 FLAGS = FLAG_FORCE_SERIAL | FLAG_FORCE_MASKED;
568 void PixTestBarePretest::setVthrCompId() {
573 banner(Form(
"PixTestBarePretest::setVthrCompId()"));
580 for (
int roc = 0; roc < nRocs; ++roc) {
582 h1 =
bookTH1D(Form(
"Id_vs_VthrComp_C%d",
int(roc)),
583 Form(
"Id vs VthrComp C%d",
int(roc)),
587 setTitles( h1,
"VthrComp [DAC]",
"ROC digital current [mA]" );
594 for (
int roc = 0; roc < nRocs; ++roc) {
606 while( sw.RealTime() < 0.1 );
608 double i015 = (nRocs-1) * i016 / nRocs;
610 LOG(logINFO) <<
"offset current from other " << nRocs-1 <<
" ROCs is "
617 vector<int> rocVthrComp(nRocs, -1);
618 for (
int roc = 0; roc < nRocs; ++roc) {
621 LOG(logDEBUG) <<
"ROC " << setw(2) << roc;
625 for (
size_t idac = 0; idac < 256; ++idac) {
633 if (hid) hid->Draw();
638 LOG(logDEBUG) <<
"current peak " << hid->GetMaximum()
639 <<
" mA at DAC " << hid->GetMaximumBin();
643 for (
int i = 1; i <= 256-fNoiseWidth; ++i) {
644 double ni = hid->GetBinContent(i);
645 double d = hid->GetBinContent(i+fNoiseWidth) - ni;
651 LOG(logDEBUG) <<
"[SetComp] max d" << fNoiseWidth
652 << maxd <<
" at " << maxi;
654 int32_t val = maxi - fNoiseMargin;
655 if (val < 0) val = 0;
656 rocVthrComp[roc] = val;
657 LOG(logDEBUG) <<
"set VthrComp to " << val;
661 TH1D *hsum =
bookTH1D(
"VthrCompSettings",
"VthrComp per ROC", nRocs, 0., nRocs);
662 setTitles(hsum,
"ROC",
"VthrComp [DAC]");
665 hsum->SetMaximum(256);
670 for (
int roc = 0; roc < nRocs; ++roc) {
672 fApi->
setDAC(
"VthrComp", rocVthrComp[roc], roc);
674 LOG(logINFO) <<
"ROC " << setw(2) << roc
675 <<
" VthrComp " << setw(3) << rocVthrComp[roc];
676 hsum->Fill(roc, rocVthrComp[roc]);
683 LOG(logINFO) <<
"PixTestBarePretest::setVthrCompId() done";
689 void PixTestBarePretest::setCalDel() {
690 uint16_t FLAGS = FLAG_FORCE_SERIAL | FLAG_FORCE_MASKED;
695 banner(Form(
"PixTestBarePretest::setCalDel()"));
700 if (
fPIX[0].first > -1) {
704 LOG(logWARNING) <<
"PreTest: no pixel defined, return";
713 string DacName =
"caldel";
717 vector<pair<uint8_t, vector<pixel> > > results;
724 LOG(logCRITICAL) <<
"problem with readout: "<< e.what() <<
" missing " << e.numberMissing <<
" events";
726 if (e.numberMissing > 10) done =
true;
728 LOG(logCRITICAL) <<
"pXar execption: "<< e.what();
731 done = (cnt>5) || done;
738 for (
int iroc = 0; iroc < nRocs; ++iroc) {
741 Form(
"NhitsVs%s_c%d_r%d_C%d", DacName.c_str(),
fPIX[0].first,
fPIX[0].second,
fId2Idx[iroc]),
744 setTitles(h1, Form(
"%s [DAC]", DacName.c_str() ),
"readouts");
749 TH1D *hsum =
bookTH1D(
"CalDelSettings",
"CalDel per ROC;ROC;CalDel [DAC]", 16, 0., 16.);
752 hsum->SetMaximum(256);
760 for (
size_t i = 0; i < results.size(); ++i) {
761 int caldel = results[i].first;
762 vector<pixel> vpix = results[i].second;
764 for (
size_t ipx = 0; ipx < vpix.size(); ++ipx) {
765 uint32_t roc = vpix.at(ipx).roc();
768 && vpix[ipx].column() ==
fPIX[0].first
769 && vpix[ipx].row() ==
fPIX[0].second
772 int nn = vpix.at(ipx).value();
776 i0[fId2Idx[roc]] = caldel;
781 h1 = hsts.at(fId2Idx[roc]);
782 h1->Fill(caldel, nn);
790 for (
int roc = 0; roc < nRocs; ++roc) {
797 for (
int roc = 0; roc < nRocs; ++roc) {
804 <<
": eff plateau from " << setw(3) << i0[
fId2Idx[roc]]
805 <<
" to " << setw(3) << i9[
fId2Idx[roc]]
806 <<
": set CalDel to " << i2;
818 void PixTestBarePretest::programROC() {
822 banner(Form(
"PixTestBarePretest::programROC() "));
825 unsigned int nRocs = rocIds.size();
826 TH1D *h1 =
bookTH1D(
"programROC",
"#Delta(Iana) vs ROC", nRocs, 0., nRocs);
829 vector<int> vanaStart;
830 for (
unsigned int iroc = 0; iroc < nRocs; ++iroc) {
840 string result(
"ROCs");
842 for (
unsigned int iroc = 0; iroc < rocIds.size(); ++iroc){
843 fApi->
setDAC(
"vana", vanaStart[iroc], rocIds[iroc]);
848 result += Form(
" %d", rocIds[iroc]);
851 h1->SetBinContent(iroc+1, dA);
856 result +=
" cannot be programmed! Error!";
859 result +=
" are all programmable";
863 string dIaString(
"");
864 for (
unsigned int i = 0; i < nRocs; ++i) {
865 dIaString += Form(
" %3.1f", h1->GetBinContent(i+1));
868 LOG(logINFO) <<
"PixTestBarePretest::programROC() done: " << result;
869 LOG(logINFO) <<
"IA differences per ROC: " << dIaString;
size_t getNEnabledPixels(uint8_t rocid)
void setToolTips()
implement this to provide updated tool tips if the user changes test parameters
std::map< TH1 *, std::string > fHistOptions
options can be stored with each histogram
bool setDAC(std::string dacName, uint8_t dacValue, uint8_t rocI2C)
std::vector< std::pair< int, int > > fPIX
range of enabled pixels for time-consuming tests
void testPixel(uint8_t column, uint8_t row, bool enable)
std::vector< std::pair< std::string, std::string > > fParameters
the parameters of this test
void doTest()
function connected to "DoTest" button of PixTab
void maskAllPixels(bool mask, uint8_t rocid)
void runCommand(std::string)
allow execution of any button in the test
TDirectory * fDirectory
where the root histograms will end up
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)
void restoreDacs(bool verbose=false)
restore all DACs
std::vector< uint8_t > getEnabledRocIDs()
void testAllPixels(bool enable)
std::list< TH1 * >::iterator fDisplayedHist
pointer to the histogram currently displayed
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
virtual std::string getHistOption(TH1 *)
get the hist display options (if stored in fHistOptions)
void cacheDacs(bool verbose=false)
cache all DACs
std::list< TH1 * > fHistList
list of histograms available in PixTab::next and PixTab::previous
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
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)
bool selectedRoc(int id)
is ROC ID selected?
void saveDacs()
save DACs to file
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
void maskPixel(uint8_t column, uint8_t row, bool mask)
void update()
signal to PixTab to update the canvas
uint8_t getDAC(size_t rocId, std::string dacName)
pxar::pxarCore * fApi
pointer to the API
std::map< int, int > fId2Idx
map the ROC ID onto the (results vector) index of the ROC
void init()
sets all test parameters
int getIdFromIdx(int idx)
provide the mapping between ROC ID and index