10 #include "PixTestPattern.hh"
13 #include "constants.h"
23 PixTestPattern::
PixTestPattern(
PixSetup *a, std::
string name) :
PixTest(a, name), fParPgCycles(0), fParTrigLoop(0), fParPeriod(0), fParSeconds(0), fPatternFromFile(0), fResultsOnFile(1), fBinOut(0), fInputFile("null"), fOutputFile("null"), fUnMaskAll(0), fExtClk(0), fParFillTree(0){
26 LOG(logDEBUG) <<
"PixTestPattern ctor(PixSetup &a, string, TGTab *)";
30 PixTestPattern::PixTestPattern() :
PixTest(){
37 fParOutOfRange =
false;
38 std::transform(parName.begin(), parName.end(), parName.begin(), ::tolower);
39 for (
unsigned int i = 0; i <
fParameters.size(); ++i)
44 sval.erase(
remove(sval.begin(), sval.end(),
' '), sval.end());
46 if (!parName.compare(
"pgcycles")){
47 fParPgCycles = atoi(sval.c_str());
49 if (fParPgCycles < 0) {
50 LOG(logWARNING) <<
"PixTestPattern::setParameter() pg_cycles must be positive";
51 found =
false; fParOutOfRange =
true;
55 if (!parName.compare(
"triggerloop")) {
58 fParTrigLoop = atoi(sval.c_str());
62 if (!parName.compare(
"period")){
63 fParPeriod = atoi(sval.c_str());
66 LOG(logWARNING) <<
"PixTestPattern::setParameter() period must be positive";
67 found =
false; fParOutOfRange =
true;
71 if (!parName.compare(
"seconds")){
72 fParSeconds = atoi(sval.c_str());
74 if (fParSeconds < 0) {
75 LOG(logWARNING) <<
"PixTestPattern::setParameter() seconds must be positive";
76 found =
false; fParOutOfRange =
true;
80 if (!parName.compare(
"patternfromfile")) {
83 fPatternFromFile = atoi(sval.c_str());
87 if (!parName.compare(
"resultsonfile")){
90 fResultsOnFile = atoi(sval.c_str());
94 if (!parName.compare(
"binaryoutput")){
97 fBinOut = atoi(sval.c_str());
101 if (!parName.compare(
"inputfile")){
102 fInputFile = sval.c_str();
106 if (!parName.compare(
"outputfile")){
107 fOutputFile = sval.c_str();
111 if (!parName.compare(
"unmaskall")){
114 fUnMaskAll = atoi(sval.c_str());
118 if (!parName.compare(
"externalclk")){
121 fExtClk = atoi(sval.c_str());
125 if (!parName.compare(
"filltree")) {
128 fParFillTree = !(atoi(sval.c_str()) == 0);
139 void PixTestPattern::init()
141 LOG(logDEBUG) <<
"PixTestPattern::init()";
143 fDirectory = gFile->GetDirectory(fName.c_str());
153 fTestTip = string(
"run DAQ");
154 fSummaryTip = string(
"Show summary plot");
159 PixTestPattern::~PixTestPattern(){
161 if (fTree && fParFillTree) fTree->Write();
165 void PixTestPattern::stop()
169 LOG(logINFO) <<
"PixTestPattern:: STOP PRESSED. Ending test.";
174 std::transform(command.begin(), command.end(), command.begin(), ::tolower);
175 LOG(logDEBUG) <<
"running command: " << command;
177 if (!command.compare(
"resettodefault")) {
178 LOG(logINFO) <<
"PixTestPattern:: reset parameters from testParameters.dat";
179 for (
unsigned int i = 0; i <
fParameters.size(); ++i)
184 else if (!command.compare(
"stop")){
188 else LOG(logDEBUG) <<
"did not find command ->" << command <<
"<-";
192 bool PixTestPattern::setPattern(
string fname) {
194 ifstream is(fname.c_str());
196 LOG(logWARNING) <<
"PixTestPattern::setPattern() cannot read " << fname;
200 bool patternFound(
false);
208 if (string::npos != line.find(
"-- Pattern")){
213 if (string::npos != line.find(
"-- Test Pixels"))
break;
217 std::string::size_type sep;
218 std::string sig, str;
224 line.erase(new_end, line.end());
225 if (line.length() < 2)
continue;
227 sep = line.find(
",");
229 if (string::npos != sep)
231 sig = line.substr(0, sep - 1);
232 str = line.substr(sep + 1);
233 val = atoi(str.c_str());
236 if (val < 0 || val > 255) {
237 LOG(logWARNING) <<
"PixTestPattern::setPattern() delay out of range [0,255]";
241 fPg_setup.push_back(make_pair(sig, del));
242 LOG(logDEBUG) <<
" pg set to -> \"" << sig <<
"\" " << del;
247 fPg_setup.push_back(make_pair(
"", 0));
248 LOG(logWARNING) <<
"PixTestPattern::setPattern() wrong ... ";
255 LOG(logWARNING) <<
"PixTestPattern::setPattern() '-- Pattern' not found in testPattern.dat";
256 fPg_setup.push_back(make_pair(
"", 0));
264 bool PixTestPattern::setPixels(
string fname,
string flag) {
267 std::stringstream sstr;
269 ifstream is(fname.c_str());
271 LOG(logWARNING) <<
"PixTestPattern::setPixels() cannot read " << fname;
275 bool pixelFound(
false);
285 if (string::npos != line.find(
"-- Test Pixels")) {
290 if (string::npos != line.find(
"-- Unmask Pixels"))
break;
295 if (string::npos != line.find(
"-- Unmask Pixels")) {
304 std::string::size_type s0, s1;
305 std::string str1, str2;
310 line.erase(new_end, line.end());
311 if (line.length() < 2)
continue;
315 if (string::npos != s1)
317 str1 = line.substr(s0, s1);
318 pixc = atoi(str1.c_str());
319 str2 = line.substr(s1 + 1);
320 pixr = atoi(str2.c_str());
321 if (flag ==
"Test")
fPIX.push_back(make_pair(pixc, pixr));
322 else fPIXm.push_back(make_pair(pixc, pixr));
323 sstr <<
" (" << pixc <<
"," << pixr <<
")";
328 fPIX.push_back(make_pair(-1, -1));
329 fPIXm.push_back(make_pair(-1, -1));
337 if (flag ==
"Test") {
338 LOG(logWARNING) <<
"PixTestPattern::setPixels() '-- Test Pixels' not found in testPattern.dat";
339 fPIX.push_back(make_pair(-1, -1));
342 LOG(logWARNING) <<
"PixTestPattern::setPixels() '-- Unmask Pixels' not found in testPattern.dat";
343 fPIXm.push_back(make_pair(-1, -1));
348 if (flag ==
"Test") {
349 LOG(logINFO) <<
"PixTestPattern:: " << npix <<
" 'armed' pixels:" << sstr.str();
352 else LOG(logINFO) <<
"PixTestPattern:: " << npix <<
" unmasked pixels:" << sstr.str();
359 void PixTestPattern::FillHistos(std::vector<pxar::Event> data, std::vector<TH2D*> hits, std::vector<TProfile2D*> phmap, std::vector<TH1D*> ph) {
363 for (std::vector<pxar::Event>::iterator it = data.begin(); it != data.end(); ++it) {
366 fTreeEvent.header = it->header;
368 fTreeEvent.trailer = it->trailer;
369 fTreeEvent.npix = it->pixels.size();
371 for (
unsigned int ipix = 0; ipix < it->pixels.size(); ++ipix) {
374 LOG(logWARNING) <<
"PixTestPattern::FillHistos() wrong 'idx' value --> return";
377 hits[idx]->Fill(it->pixels[ipix].column(), it->pixels[ipix].row());
378 phmap[idx]->Fill(it->pixels[ipix].column(), it->pixels[ipix].row(), it->pixels[ipix].value());
379 ph[idx]->Fill(it->pixels[ipix].value());
381 fTreeEvent.proc[ipix] = it->pixels[ipix].roc();
382 fTreeEvent.pcol[ipix] = it->pixels[ipix].column();
383 fTreeEvent.prow[ipix] = it->pixels[ipix].row();
384 fTreeEvent.pval[ipix] = it->pixels[ipix].value();
385 fTreeEvent.pq[ipix] = 0;
388 if (fParFillTree) fTree->Fill();
391 TH2D* h2 = (TH2D*)(hits.back());
397 void PixTestPattern::PrintEvents(
int par1,
int par2,
string flag, std::vector<TH2D*> hits, std::vector<TProfile2D*> phmap, std::vector<TH1D*> ph) {
399 std::vector<pxar::Event> daqEvBuffer;
405 daqEvBuffsiz = daqEvBuffer.size();
407 FillHistos(daqEvBuffer, hits, phmap, ph);
409 if (daqEvBuffsiz <= 201) {
410 LOG(logINFO) <<
"PixTestPattern:: data from buffer:";
412 for (
unsigned int i = 0; i < daqEvBuffsiz; i++) {
413 cout << i <<
" : " << daqEvBuffer[i] << endl;
419 LOG(logINFO) <<
"PixTestPattern:: data from buffer:";
420 for (
unsigned int i = 0; i <= 100; i++) {
421 cout << i <<
" : " << daqEvBuffer[i] << endl;
424 cout << endl <<
"................... SKIP EVENTS TO NOT SATURATE THE SHELL ...................." << endl << endl;
425 for (
unsigned int i = (daqEvBuffsiz - 100); i < daqEvBuffsiz; i++) {
426 cout << i <<
" : " << daqEvBuffer[i] << endl;
430 LOG(logINFO) <<
"PixTestPattern:: " << daqEvBuffsiz <<
" events read from buffer";
436 LOG(logINFO) <<
"PixTestPattern:: Start reading data from DTB RAM";
437 std::stringstream sstr, sdata;
439 if (flag ==
"trg") sstr <<
"_" << par1 <<
"pgCycles";
440 else sstr <<
"_" << par1 <<
"sec" <<
"_" << par2;
441 if (fBinOut) FileName = f_Directory +
"/" + fOutputFile.c_str() + sstr.str() +
".bin";
442 else FileName = f_Directory +
"/" + fOutputFile.c_str() + sstr.str() +
".dat";
448 if (daqdat.size() > 550000) sdata << (daqdat.size() / 524288) <<
"MB";
449 else sdata << (daqdat.size() / 512) <<
"kB";
450 LOG(logINFO) <<
"PixTestPattern:: " << daqdat.size() <<
" words of data read : " << sdata.str();
451 std::ofstream fout(FileName.c_str(), std::ios::out | std::ios::binary);
452 LOG(logINFO) <<
"PixTestPattern:: Writing binary";
453 fout.write(reinterpret_cast<const char*>(&daqdat[0]),
sizeof(daqdat[0])*daqdat.size());
460 daqEvBuffsiz = daqEvBuffer.size();
461 LOG(logINFO) <<
"PixTestPattern:: " << daqEvBuffsiz <<
" events read";
463 FillHistos(daqEvBuffer, hits, phmap, ph);
465 std::ofstream fout(FileName.c_str(), std::ofstream::out);
466 if (daqEvBuffsiz <= 201) {
467 LOG(logINFO) <<
"PixTestPattern:: Writing decoded events";
468 for (
unsigned int i = 0; i < daqEvBuffsiz; ++i) {
469 fout << i <<
" : " << daqEvBuffer[i] << endl;
473 LOG(logINFO) <<
"PixTestPattern:: Writing decoded events (a fraction of)";
474 for (
unsigned int i = 0; i <= 100; i++) {
475 fout << i <<
" : " << daqEvBuffer[i] << endl;
478 fout << endl <<
"................... SKIP EVENTS TO NOT TAKE TOO LONG ...................." << endl << endl;
479 for (
unsigned int i = (daqEvBuffsiz - 100); i < daqEvBuffsiz; i++) {
480 fout << i <<
" : " << daqEvBuffer[i] << endl;
486 LOG(logINFO) <<
"PixTestPattern:: Wrote data to " << FileName.c_str();
494 void PixTestPattern::TriggerLoop(
int checkfreq, std::vector<TH2D*> hits, std::vector<TProfile2D*> phmap, std::vector<TH1D*> ph) {
498 uint64_t diff = 0, timepaused = 0, timeff = 0;
499 bool TotalTime =
false;
501 LOG(logINFO) <<
"PixTestPattern:: starting TriggerLoop for " << fParSeconds <<
" seconds";
506 diff = t.get() - diff;
508 LOG(logDEBUG) <<
"PixTestPattern:: readout time " << timepaused / 1000 <<
" seconds";
509 LOG(logINFO) <<
"PixTestPattern:: restarting TriggerLoop for " << fParSeconds - (timeff / 1000) <<
" s";
513 if (nloop == 1) { LOG(logINFO) <<
"PixTestPattern:: TriggerLoop period = " << fPeriod <<
" clks"; }
516 while (
fApi->
daqStatus(perFull) && perFull < 80 && fDaq_loop) {
518 timeff = t.get() - timepaused;
519 LOG(logINFO) <<
"PixTestPattern:: elapsed time " << timeff / 1000 <<
" seconds";
520 if (timeff / 1000 >= (uint64_t)fParSeconds) {
525 LOG(logINFO) <<
"PixTestPattern:: buffer not full, at " << (int)perFull <<
"%";
526 gSystem->ProcessEvents();
530 LOG(logINFO) <<
"PixTestPattern:: buffer almost full, pausing triggers";
535 if (TotalTime) { LOG(logINFO) <<
"PixTestPattern:: total time reached - DAQ stopped."; }
539 PrintEvents(fParSeconds, nloop,
"loop", hits, phmap, ph);
545 void PixTestPattern::pgToDefault() {
547 fPg_setup =
fPixSetup->getConfigParameters()->getTbPgSettings();
549 LOG(logINFO) <<
"PixTestPattern:: pg_setup set to default";
553 void PixTestPattern::FinalCleaning() {
572 if (fParOutOfRange)
return;
574 LOG(logINFO) <<
"PixTestPattern::doTest() start";
579 f_Directory = config->getDirectory();
580 fname = f_Directory +
"/" + fInputFile +
".dat";
585 LOG(logINFO) <<
"PixTestPattern:: all pixels unmasked";
590 if (!setPixels(fname,
"Unmask")){
594 for (
unsigned int i = 0; i < fPIXm.size(); ++i) {
595 if (fPIXm[i].first > -1)
fApi->
_dut->
maskPixel(fPIXm[i].first, fPIXm[i].second,
false);
601 if (!setPixels(fname,
"Test")){
606 for (
unsigned int i = 0; i <
fPIX.size(); ++i) {
607 if (
fPIX[i].first > -1) {
617 std::vector<TH2D*> Hits;
618 std::vector<TProfile2D*> Phmap;
619 std::vector<TH1D*> Ph;
626 std::stringstream strs;
627 strs <<
"_" << fNpix <<
"pix";
628 if (!fParTrigLoop) strs <<
"_" << fParPgCycles <<
"cyc";
629 else strs <<
"_" << fParSeconds <<
"sec";
630 if (!fPatternFromFile) strs <<
"_stdPG";
631 string histname = strs.str();
633 for (
unsigned int iroc = 0; iroc < rocIds.size(); ++iroc){
634 h2 =
bookTH2D(Form(
"hits_C%d%s", rocIds[iroc], histname.c_str()), Form(
"hits_C%d%s", rocIds[iroc], histname.c_str()), 52, 0., 52., 80, 0., 80.);
641 p2 =
bookTProfile2D(Form(
"phMap_C%d%s", rocIds[iroc], histname.c_str()), Form(
"phMap_C%d%s", rocIds[iroc], histname.c_str()), 52, 0., 52., 80, 0., 80.);
648 h1 =
bookTH1D(Form(
"ph_C%d%s", rocIds[iroc], histname.c_str()), Form(
"ph_C%d%s", rocIds[iroc], histname.c_str()), 256, 0., 256.);
657 LOG(logINFO) <<
"PixTestPattern:: Setting CLK to external";
670 fPg_setup.push_back(make_pair(
"resetroc", 0));
680 LOG(logINFO) <<
"PixTestPattern:: RES sent once ";
683 LOG(logINFO) <<
"PixTestPattern:: pg_setup clean";
686 if (fPatternFromFile)
688 LOG(logINFO) <<
"PixTestPattern:: Set pattern from file: " << fname;
689 if (!setPattern(fname)){
696 fPg_setup =
fPixSetup->getConfigParameters()->getTbPgSettings();
707 LOG(logINFO) <<
"PixTestPattern:: " << fParPgCycles <<
" pg_Single() sent with period " << fPeriod;
712 PrintEvents(fParPgCycles, 0,
"trg", Hits, Phmap, Ph);
716 TriggerLoop(2, Hits, Phmap, Ph);
723 copy(Ph.begin(), Ph.end(), back_inserter(
fHistList));
724 copy(Phmap.begin(), Phmap.end(), back_inserter(
fHistList));
725 copy(Hits.begin(), Hits.end(), back_inserter(
fHistList));
736 LOG(logINFO) <<
"PixTestPattern::doTest() done for.";
static void replaceAll(std::string &str, const std::string &from, const std::string &to)
in str, replace all occurences of from to to
bool setExternalClock(bool enable)
void setPatternGenerator(std::vector< std::pair< std::string, uint8_t > > pg_setup)
std::map< TH1 *, std::string > fHistOptions
options can be stored with each histogram
void daqTriggerLoopHalt()
std::string fStopTip
information for this test
PixSetup * fPixSetup
all necessary stuff in one place
std::vector< std::pair< int, int > > fPIX
range of enabled pixels for time-consuming tests
static bool bothAreSpaces(char lhs, char rhs)
what would you expect?
void testPixel(uint8_t column, uint8_t row, bool enable)
std::vector< std::pair< std::string, std::string > > fParameters
the parameters of this test
int getIdxFromId(int id)
provide the mapping between ROC index and ID
void maskAllPixels(bool mask, uint8_t rocid)
TDirectory * fDirectory
where the root histograms will end up
uint16_t daqTriggerLoop(uint16_t period=1000)
void bookTree()
book a minimal tree with pixel events
std::vector< uint8_t > getEnabledRocIDs()
void testAllPixels(bool enable)
std::list< TH1 * >::iterator fDisplayedHist
pointer to the histogram currently displayed
void setToolTips()
implement this to provide updated tool tips if the user changes test parameters
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 runCommand(std::string)
allow execution of any button in the test
std::vector< uint16_t > daqGetBuffer()
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
void doTest()
function connected to "DoTest" button of PixTab
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)
virtual bool setParameter(std::string parName, std::string sval)
set the string value of a parameter
void update()
signal to PixTab to update the canvas
pxar::pxarCore * fApi
pointer to the API
std::vector< Event > daqGetEventBuffer()
TProfile2D * bookTProfile2D(std::string sname, std::string title, int nbinsx, double xmin, double xmax, int nbinsy, double ymin, double max, std::string option="")
book a TProfile2D, adding version information to the name and title
uint16_t daqTrigger(uint32_t nTrig=1, uint16_t period=0)
void init()
sets all test parameters