pxar
 All Classes Namespaces Functions Variables Typedefs Friends
PixTestSetup.cc
1 // -- author: Martino Dall'Osso
2 // to set clk and deser160phase for single roc test
3 
4 #include <stdlib.h> // atof, atoi
5 #include <iostream>
6 #include <algorithm> // std::find
7 #include <TBox.h>
8 
9 #include "PixTestSetup.hh"
10 #include "log.h"
11 #include "constants.h"
12 
13 using namespace std;
14 using namespace pxar;
15 
16 ClassImp(PixTestSetup)
17 
18 //------------------------------------------------------------------------------
19 PixTestSetup::PixTestSetup(PixSetup *a, std::string name) : PixTest(a, name)
20 {
21  PixTest::init();
22  init();
23 }
24 
25 //------------------------------------------------------------------------------
26 PixTestSetup::PixTestSetup() : PixTest() {}
27 
28 bool PixTestSetup::setParameter(string parName, string sval) {
29  bool found(false);
30  ParOutOfRange = false;
31  std::transform(parName.begin(), parName.end(), parName.begin(), ::tolower);
32  for (unsigned int i = 0; i < fParameters.size(); ++i) {
33  if (fParameters[i].first == parName) {
34  found = true;
35 
36  if (!parName.compare("clkmax")) {
37  fClkMax = atoi(sval.c_str());
38  if(fClkMax < 0 || fClkMax > 25) {
39  LOG(logWARNING) << "PixTestSetup::setParameter() ClkMax out of range (0-25)";
40  found=false; ParOutOfRange = true;
41  }
42  }
43  if (!parName.compare("desermax")) {
44  fDeserMax = atoi(sval.c_str());
45  if(fDeserMax < 0 || fDeserMax > 7) {
46  LOG(logWARNING) << "PixTestSetup::setParameter() DeserMax out of range (0-7)";
47  found=false; ParOutOfRange = true;
48  }
49  }
50  break;
51  }
52  }
53  return found;
54 }
55 
56 void PixTestSetup::init()
57 {
58  LOG(logDEBUG) << "PixTestSetup::init()";
59 
60  fDirectory = gFile->GetDirectory(fName.c_str());
61  if (!fDirectory)
62  fDirectory = gFile->mkdir(fName.c_str());
63  fDirectory->cd();
64 }
65 
67 {
68  fTestTip = string(Form("scan testboard parameter settings and check for valid readout\n")
69  + string("TO BE IMPLEMENTED!!")); //FIXME
70  fSummaryTip = string("summary plot to be implemented"); //FIXME
71 }
72 
73 void PixTestSetup::bookHist(string name)
74 {
75  fDirectory->cd();
76  LOG(logDEBUG) << "nothing done with " << name;
77 }
78 
79 PixTestSetup::~PixTestSetup()
80 {
81  LOG(logDEBUG) << "PixTestSetup dtor";
82  std::list<TH1*>::iterator il;
83  fDirectory->cd();
84  for (il = fHistList.begin(); il != fHistList.end(); ++il)
85  {
86  LOG(logINFO) << "Write out " << (*il)->GetName();
87  (*il)->SetDirectory(fDirectory);
88  (*il)->Write();
89  }
90 }
91 
92 //------------------------------------------------------------------------------
94 {
95  cacheDacs();
96  if (ParOutOfRange) return;
97  fDirectory->cd();
98  fHistList.clear();
100 
101  //fixed number of trigger (unstable with only 1 trigger)
102  int Ntrig = 10;
103 
104  LOG(logINFO) << "PixTestSetup::doTest() ntrig = " << Ntrig;
105 
106  bookHist("bla"); //FIXME
107 
108  // Setup a new pattern with only res and token:
109  std::vector<std::pair<std::string, uint8_t> > pg_setup;
110  pg_setup.push_back(make_pair("resetroc", 25)); // PG_RESR b001000
111  pg_setup.push_back(make_pair("token", 0)); // PG_TOK b000001
112  uint16_t period = 28;
113  fApi->setPatternGenerator(pg_setup);
114  LOG(logINFO) << "PixTestSetup:: pg set to RES|TOK";
115 
116 
117  TH2D *histo = new TH2D(Form("DeserphaseClkScan"), Form("DeserphaseClkScan"), fDeserMax+1, 0., fDeserMax+1, fClkMax+1, 0., fClkMax+1);
118  histo->GetXaxis()->SetTitle("deser160phase");
119  histo->GetYaxis()->SetTitle("clk");
120  fHistList.push_back(histo);
121 
122  std::vector<rawEvent> daqRawEv;
123  int ideser, iclk;
124  int good_clk = -1, good_deser = -1;
125 
126  std::stringstream tablehead;
127  for (ideser = 0; ideser <= fDeserMax; ideser++) tablehead << std::setw(8) << ideser;
128  LOG(logINFO) << tablehead.str();
129 
130  // Start the DAQ:
131  fApi->daqStart();
132 
133  // Loop over the Signal Delay range we want to scan:
134  for (iclk = 0; iclk <= fClkMax; iclk++) {
135  std::stringstream oneline;
136  oneline << std::setw(2) << iclk << ": ";
137 
138  for (ideser = 0; ideser <= fDeserMax; ideser++) {
139 
140  // Set up the delays in the DTB:
141  fApi->setTestboardDelays(getMagicDelays(iclk,ideser));
142 
143  // Send the triggers and read out the events:
144  fApi->daqTrigger(Ntrig,period);
145  daqRawEv = fApi->daqGetRawEventBuffer();
146 
147  unsigned int head_good = 0;
148  unsigned int head_bad = 0;
149 
150  // Trying to find the ROC header 0x7f8 in the raw DESER160 data:
151  for (std::vector<rawEvent>::iterator evt = daqRawEv.begin(); evt != daqRawEv.end(); ++evt) {
152  // Get the first word from ROC header:
153  int head = static_cast<int>(evt->data.at(0) & 0xffc);
154  if (head == 0x7f8) { head_good++; }
155  else head_bad++;
156  }
157 
158  // Print the stuff:
159  if(head_good > 0) {
160  oneline << std::hex << "<7f8>" << std::dec;
161  histo->Fill(ideser, iclk, head_good);
162  good_clk = iclk; good_deser = ideser;
163 
164  // Additionally print number of good headers:
165  if (head_good < 10) oneline << "[" << head_good << "]";
166  else oneline << "[*]";
167  }
168  else if(head_bad > 0) {
169  oneline << std::hex << " " << std::setw(3) << std::setfill('0') << (daqRawEv.at(0).data.at(0) & 0xffc) << std::setfill(' ') << " " << std::dec;
170  }
171  else oneline << " [.] ";
172  }
173  LOG(logINFO) << oneline.str();
174  }
175 
176  // Stop the DAQ:
177  fApi->daqStop();
178 
179 
180  int finalclk, finaldeser;
181  histo->Draw("colz");
182  Int_t bin = histo->GetMaximumBin(); //set always to minimum binx/biny
183  Int_t binx, biny, binz;
184  histo->GetBinXYZ(bin, binx, biny, binz);
185 
186  //to choose a middle value between various 'good clk':
187  LOG(logDEBUG) << "PixTestSetup:: choosing the good clk phase.";
188  Double_t val1 = histo->GetBinContent(bin);
189  Double_t val2 = 0;
190  int cnt = 0;
191  for (int ibiny = (biny+1); ibiny <= fClkMax; ibiny ++) {
192  val2 = histo->GetBinContent(binx, ibiny);
193  if (val2 < val1) break;
194  else cnt++;
195  }
196  biny = biny + ((int)(cnt / 2));
197 
198  Double_t x1 = histo->GetXaxis()->GetBinLowEdge(binx);
199  Double_t x2 = histo->GetXaxis()->GetBinUpEdge(binx);
200  Double_t y1 = histo->GetYaxis()->GetBinLowEdge(biny);
201  Double_t y2 = histo->GetYaxis()->GetBinUpEdge(biny);
202  TBox b(x1, y1, x2, y2);
203  b.SetFillStyle(0);
204  b.SetLineWidth(4);
205  b.SetLineColor(kBlack);
206 
207  // If we found some good settings, find the best:
208  if (good_clk != -1 && good_deser != -1) {
209 
210  // Highlight the maximum
211  b.Draw();
212 
213  finalclk = biny - 1;
214  finaldeser = binx - 1;
215  LOG(logINFO) << "Found good delays at " << "CLK = "<< finalclk << ", DESER160 = " << finaldeser;
216  }
217  else {
218  //back to default values
219  finalclk = finaldeser = 4;
220  LOG(logINFO) << "DTB Delay Setup could not find any good delays.";
221  LOG(logINFO) << "Falling back to default values.";
222  }
223 
224  // Update the histogram, also print the added box around selected settings:
225  fDisplayedHist = find(fHistList.begin(), fHistList.end(), histo);
226  PixTest::update();
227 
228  // Set final clk and deser160:
229  sig_delays = getMagicDelays(finalclk,finaldeser);
230  for(std::vector<std::pair<std::string,uint8_t> >::iterator sig = sig_delays.begin(); sig != sig_delays.end(); ++sig) {
231  fPixSetup->getConfigParameters()->setTbParameter(sig->first, sig->second);
232  }
233 
234  // Store them in the file.
235  saveTbParameters();
236  fApi->setTestboardDelays(sig_delays);
237 
238  // Reset the pattern generator to the configured default:
239  fApi->setPatternGenerator(fPixSetup->getConfigParameters()->getTbPgSettings());
240 
241  fHistList.clear();
242 
243  LOG(logINFO) << "PixTestSetup::doTest() done for.";
244 }
245 
246 std::vector<std::pair<std::string,uint8_t> > PixTestSetup::getMagicDelays(uint8_t clk, uint8_t deser160) {
247  std::vector<std::pair<std::string,uint8_t> > sigdelays;
248  sigdelays.push_back(std::make_pair("clk", clk));
249  sigdelays.push_back(std::make_pair("ctr", clk));
250  sigdelays.push_back(std::make_pair("sda", clk + 15));
251  sigdelays.push_back(std::make_pair("tin", clk + 5));
252  sigdelays.push_back(std::make_pair("deser160phase", deser160));
253  return sigdelays;
254 }
255 
256 // ----------------------------------------------------------------------
257 void PixTestSetup::saveTbParameters() {
258  LOG(logINFO) << "PixTestSetup:: Write Tb parameters to file.";
259  fPixSetup->getConfigParameters()->writeTbParameterFile();
260 }
261 
262 // ----------------------------------------------------------------------
263 void PixTestSetup::runCommand(std::string command) {
264  std::transform(command.begin(), command.end(), command.begin(), ::tolower);
265  LOG(logDEBUG) << "running command: " << command;
266  if (!command.compare("savetbparameters")) {
267  saveTbParameters();
268  return;
269  }
270  LOG(logDEBUG) << "did not find command ->" << command << "<-";
271 }
void setPatternGenerator(std::vector< std::pair< std::string, uint8_t > > pg_setup)
Definition: api.cc:78
bool daqStart()
Definition: api.cc:1139
virtual bool setParameter(std::string parName, std::string sval)
set the string value of a parameter
Definition: PixTestSetup.cc:28
PixSetup * fPixSetup
all necessary stuff in one place
Definition: PixTest.hh:290
std::vector< std::pair< std::string, std::string > > fParameters
the parameters of this test
Definition: PixTest.hh:302
void setTestboardDelays(std::vector< std::pair< std::string, uint8_t > > sig_delays)
Definition: api.cc:68
TDirectory * fDirectory
where the root histograms will end up
Definition: PixTest.hh:306
bool daqStop()
Definition: api.cc:1324
std::list< TH1 * >::iterator fDisplayedHist
pointer to the histogram currently displayed
Definition: PixTest.hh:309
void setToolTips()
implement this to provide updated tool tips if the user changes test parameters
Definition: PixTestSetup.cc:66
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 doTest()
function connected to "DoTest" button of PixTab
Definition: PixTestSetup.cc:93
void runCommand(std::string)
allow execution of any button in the test
void update()
signal to PixTab to update the canvas
Definition: PixTest.cc:569
pxar::pxarCore * fApi
pointer to the API
Definition: PixTest.hh:289
uint16_t daqTrigger(uint32_t nTrig=1, uint16_t period=0)
Definition: api.cc:1222
void init()
sets all test parameters
Definition: PixTest.cc:62
std::vector< rawEvent > daqGetRawEventBuffer()
Definition: api.cc:1270