pxar
 All Classes Namespaces Functions Variables Typedefs Friends
PixTestSetVana.cc
1 // -- author: Daniel Pitzl
2 // set Vana to get desired target Ia per ROC
3 
4 #include <cmath> // fabs
5 #include <stdlib.h> // atof, atoi
6 #include <algorithm> // std::find
7 #include <iostream>
8 
9 #include <TH1.h>
10 #include <TStopwatch.h>
11 
12 #include "PixTestSetVana.hh"
13 #include "log.h"
14 
15 using namespace std;
16 using namespace pxar;
17 
18 ClassImp(PixTestSetVana)
19 
20 //------------------------------------------------------------------------------
21 PixTestSetVana::PixTestSetVana( PixSetup *a, std::string name )
22 : PixTest(a, name), fTargetIa(24)
23 {
24  PixTest::init();
25  init();
26 }
27 
28 //------------------------------------------------------------------------------
29 PixTestSetVana::PixTestSetVana() : PixTest()
30 {
31  //LOG(logDEBUG) << "PixTestSetVana ctor()";
32 }
33 
34 //------------------------------------------------------------------------------
35 bool PixTestSetVana::setParameter( string parName, string sval ) {
36  bool found(false);
37  string stripParName;
38  for (unsigned int i = 0; i < fParameters.size(); ++i) {
39  if (fParameters[i].first == parName) {
40  found = true;
41 
42  LOG(logDEBUG) << " ==> parName: " << parName;
43  LOG(logDEBUG) << " ==> sval: " << sval;
44  if (!parName.compare("targetia")) {
45  fTargetIa = atoi(sval.c_str());
46  setToolTips();
47  }
48  break;
49  }
50  }
51  return found;
52 }
53 
54 //------------------------------------------------------------------------------
55 void PixTestSetVana::init()
56 {
57  fDirectory = gFile->GetDirectory( fName.c_str() ); // fName set in PixTest
58  if( !fDirectory )
59  fDirectory = gFile->mkdir( fName.c_str() );
60  fDirectory->cd();
61 }
62 
63 //------------------------------------------------------------------------------
64 void PixTestSetVana::bookHist(string name) // no histos
65 {
66  LOG(logDEBUG) << "not using " << name;
67  fDirectory->cd();
68  fHistList.clear();
69 }
70 
71 //------------------------------------------------------------------------------
72 PixTestSetVana::~PixTestSetVana()
73 {
74  LOG(logDEBUG) << "PixTestSetVana dtor";
75  std::list<TH1*>::iterator il;
76  fDirectory->cd();
77  for( il = fHistList.begin(); il != fHistList.end(); ++il) {
78  LOG(logDEBUG) << "Write out " << (*il)->GetName();
79  (*il)->SetDirectory(fDirectory);
80  (*il)->Write();
81  }
82 }
83 
84 //------------------------------------------------------------------------------
86 {
87  fDirectory->cd();
89 
90  LOG(logINFO) << "PixTestSetVana::doTest() target Ia " << fTargetIa << " mA/ROC";
91 
92  fApi->_dut->testAllPixels(false);
93 
94  bookHist( "empty" );
95 
96  int vana16[16] = {0};
97 
98  uint32_t nRocs = fPixSetup->getConfigParameters()->getNrocs();
99 
100  // set all ROCs to minimum:
101 
102  for( uint32_t roc = 0; roc < nRocs; ++roc ) {
103  vana16[roc] = fApi->_dut->getDAC( roc, "vana" ); // remember as start value
104  fApi->setDAC( "vana", 0, roc ); // off
105  } // rocs
106 
107  double i016 = fApi->getTBia()*1E3;
108 
109  TStopwatch sw;
110 
111  sw.Start(kTRUE); // reset
112  do {
113  sw.Start(kFALSE); // continue
114  i016 = fApi->getTBia()*1E3;
115  }
116  while( sw.RealTime() < 0.1 );
117 
118  LOG(logINFO) << "delay " << sw.RealTime() << " s"
119  << ", Stopwatch counter " << sw.Counter();
120 
121  // i016 = nRocs * current01
122  // subtract one ROC to get the offset from the other Rocs (on average):
123 
124  double i015 = (nRocs-1) * i016 / nRocs; // = 0 for single chip tests
125 
126  LOG(logINFO) << "offset current from other " << nRocs-1 << " ROCs is "
127  << i015 << " mA";
128 
129  // tune per ROC:
130 
131  const double extra = 0.1; // [mA] besser zu viel als zu wenig
132  const double eps = 0.25; // [mA] convergence
133  const double slope = 6; // 255 DACs / 40 mA
134 
135  for( uint32_t roc = 0; roc < nRocs; ++roc ) {
136 
137  int vana = vana16[roc];
138  fApi->setDAC( "vana", vana, roc ); // start value
139  // delay
140 
141  double ia = fApi->getTBia()*1E3; // [mA], just to be sure to flush usb
142 
143  sw.Start(kTRUE); // reset
144  do {
145  sw.Start(kFALSE); // continue
146  ia = fApi->getTBia()*1E3; // [mA]
147  }
148  while( sw.RealTime() < 0.1 );
149 
150  LOG(logINFO) << "delay " << sw.RealTime() << " s"
151  << ", Stopwatch counter " << sw.Counter();
152 
153  double diff = fTargetIa + extra - (ia - i015);
154 
155  int iter = 0;
156  LOG(logINFO) << "ROC " << roc << " iter " << iter
157  << " Vana " << vana
158  << " Ia " << ia-i015 << " mA";
159 
160  while( fabs(diff) > eps && iter < 11 && vana > 0 && vana < 255 ) {
161 
162  int stp = int( fabs(slope*diff) );
163  if( stp == 0 ) stp = 1;
164  if( diff < 0 ) stp = -stp;
165 
166  vana += stp;
167 
168  if( vana < 0 )
169  vana = 0;
170  else
171  if( vana > 255 )
172  vana = 255;
173 
174  fApi->setDAC( "vana", vana, roc );
175  iter++;
176 
177  sw.Start(kTRUE); // reset
178  do {
179  sw.Start(kFALSE); // continue
180  ia = fApi->getTBia()*1E3; // [mA]
181  }
182  while( sw.RealTime() < 0.1 );
183 
184  diff = fTargetIa + extra - (ia - i015);
185 
186  LOG(logINFO) << "ROC " << roc << " iter " << iter
187  << " Vana " << vana
188  << " Ia " << ia-i015 << " mA";
189  } // iter
190 
191  vana16[roc] = vana; // remember best
192  fApi->setDAC( "vana", 0, roc ); // switch off for next ROC
193 
194  } // rocs
195 
196  // set all ROCs to optimum:
197 
198  for( uint32_t roc = 0; roc < nRocs; ++roc ) {
199  fApi->setDAC( "vana", vana16[roc], roc );
200  LOG(logINFO) << "ROC " << setw(2) << roc
201  << " Vana " << vana16[roc];
202  }
203 
204  double ia16 = fApi->getTBia()*1E3; // [mA]
205 
206  sw.Start(kTRUE); // reset
207  do {
208  sw.Start(kFALSE); // continue
209  ia16 = fApi->getTBia()*1E3; // [mA]
210  }
211  while( sw.RealTime() < 0.1 );
212 
213  LOG(logINFO) << "Module Ia " << ia16 << " mA = " << ia16/nRocs << " mA/ROC";
214 }
bool setDAC(std::string dacName, uint8_t dacValue, uint8_t rocI2C)
Definition: api.cc:546
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
virtual bool setParameter(std::string parName, std::string sval)
set the string value of a parameter
TDirectory * fDirectory
where the root histograms will end up
Definition: PixTest.hh:306
dut * _dut
Definition: api.h:728
void testAllPixels(bool enable)
Definition: dut.cc:503
std::list< TH1 * > fHistList
list of histograms available in PixTab::next and PixTab::previous
Definition: PixTest.hh:307
virtual void setToolTips()
implement this to provide updated tool tips if the user changes test parameters
Definition: PixTest.cc:85
double getTBia()
Definition: api.cc:424
void doTest()
function connected to "DoTest" button of PixTab
void update()
signal to PixTab to update the canvas
Definition: PixTest.cc:569
uint8_t getDAC(size_t rocId, std::string dacName)
Definition: dut.cc:314
pxar::pxarCore * fApi
pointer to the API
Definition: PixTest.hh:289
void init()
sets all test parameters
Definition: PixTest.cc:62