pxar
 All Classes Namespaces Functions Variables Typedefs Friends
phOpt.C
1 // -- Usage:
2 // ---------
3 // ../bin/pXar -c '../scripts/phOpt.C()'
4 
5 // ----------------------------------------------------------------------
6 // create PH vs VCal scans for a grid of phscale and phoffset values
7 void phOpt(string rootfile = "phOpt.root", string cfgdirectory = "testROC") {
8  ConfigParameters *configParameters = ConfigParameters::Singleton();
9 
10  configParameters->setDirectory(cfgdirectory);
11  string cfgFile = configParameters->getDirectory() + string("/configParameters.dat");
12  configParameters->readConfigParameterFile(cfgFile);
13 
14 
15  PixTestParameters *ptp = new PixTestParameters(configParameters->getDirectory() + "/" + configParameters->getTestParameterFileName());
16 
17  PixSetup *ap = new PixSetup("DEBUG", ptp, configParameters);
18 
19  cout << "pxar: dumping results into " << rootfile << endl;
20  TFile *rfile = TFile::Open(rootfile.c_str(), "RECREATE");
21 
22  PixTestFactory *factory = PixTestFactory::instance();
23 
24  PixTest *pt = factory->createTest("DacScan", ap);
25  pt->setDAC("ctrlreg", 4);
26  pt->setParameter("PHmap", "1");
27  pt->setParameter("DAC", "Vcal");
28  pt->setParameter("DACLO", "0");
29  pt->setParameter("DACHI", "255");
30 
31  int cycle(0);
32  TH1D *h1(0);
33  for (unsigned int io = 0; io < 26; ++io) {
34  for (unsigned int is = 0; is < 52; ++is) {
35  pt->setDAC("phoffset", io*10);
36  pt->setDAC("phscale", is*5);
37  pt->doTest();
38  h1 = (TH1D*)rfile->Get(Form("DacScan/ph_Vcal_c11_r20_C0_V%d", cycle));
39  h1->SetTitle(Form("ph_Vcal_c11_r20_C0_V%d phscale=%d phoffset=%d", cycle, is*5, io*10));
40  ++cycle;
41  }
42  }
43  rfile->Print();
44  delete pt;
45 
46  rfile->Close();
47 
48  ap->killApi();
49 
50 }
51 
52 
53 
54 // ----------------------------------------------------------------------
55 void ana(string rootfile = "phOpt.root") {
56  TFile *f = TFile::Open(rootfile.c_str());
57 
58  f->cd("DacScan");
59  char bla;
60  int cnt = 0;
61  TH1D *h;
62 
63  double x(0.);
64  TIter next(gDirectory->GetListOfKeys(););
65  TObject *obj;
66  double maxPh(0.), minVcal(60);
67  TH1D *hmax(0), *hmin(0);
68  TCanvas *c0 = new TCanvas("C0");
69  c0->Clear();
70  while ((obj = (TObject*)next())) {
71  h = (TH1D*)gDirectory->Get(obj->GetName());
72 
73  if (!h) {
74  cout << "problem with " << obj->GetName() << endl;
75  continue;
76  }
77 
78  double integral = h->Integral();
79  if (integral < 100.) continue;
80 
81  double plateau = h->GetMaximum();
82  double vcal = h->FindFirstBinAbove(1.);
83 
84  if (plateau > maxPh && vcal < minVcal+10) {
85  maxPh = plateau;
86  hmax = h;
87  }
88 
89  if (vcal > 10 && vcal < minVcal) {
90  minVcal = vcal;
91  hmin = h;
92  }
93 
94  cout << "h = " << h << " obj = " << obj << " name: " << h->GetName() << " integral: " << integral << endl;
95  h->Draw("");
96  c0->Modified();
97  c0->Update();
98  }
99 
100  c0->Clear();
101  c0->Divide(1,2);
102  c0->cd(1);
103  hmin->Draw();
104 
105  c0->cd(2);
106  hmax->Draw();
107 
108 
109 }
110 
111 void Opt_fit(string rootfile = "phOpt.root") {
112  TFile *f = TFile::Open(rootfile.c_str());
113 
114  TH1D *h;
115  double x(0.);
116  int OptDistance(255);
117  double maxPh(0.);
118  TH1D *hopt(0);
119  TCanvas *c0 = new TCanvas("C0");
120  c0->Clear();
121  unsigned int io, is, is_opt=999;
122  int safety_margin=0;
123  int bin_min;
124  double bestDist=255;
125  double dist =255;
126  double upEd_dist=255, lowEd_dist=255;
127  unsigned int io_opt=999;
128 
129  //set midrange value for offset and scan scale, until PH curve is well inside the range
130  io=20;
131  io_opt=io;
132  safety_margin=50;
133  for(is=0; is<52; is++){
134  h = (TH1D*)gDirectory->Get(Form("DacScan/ph_Vcal_c11_r20_C0_V%d", 52*io+is));
135  cout<<h->GetTitle()<<endl;
136  h->Draw();
137  //erasing first garbage bin
138  h->SetBinContent(1,0.);
139  bin_min = h->FindFirstBinAbove(1.);
140  upEd_dist = abs(h->GetBinContent(h->FindFixBin(255)) - (255 - safety_margin));
141  lowEd_dist = abs(h->GetBinContent(bin_min) - safety_margin);
142  dist = (upEd_dist > lowEd_dist ) ? (upEd_dist) : (lowEd_dist);
143  if(dist < bestDist){
144  is_opt = is;
145  bestDist=dist;
146  }
147  }
148 
149  if(is_opt==999){
150  cout<<"PH optimization failed"<<endl;
151  return;
152  }
153 
154  cout<<"PH Scale value chosen: " << is_opt << "with distance " << bestDist << endl;
155 
156  //centring PH curve adjusting ph offset
157  bestDist=255;
158  io_opt=999;
159  for(io=0; io<26; io++){
160  h = (TH1D*)gDirectory->Get(Form("DacScan/ph_Vcal_c11_r20_C0_V%d", 52*io+is_opt));
161  cout<<h->GetTitle()<<endl;
162  //erasing first garbage bin
163  h->SetBinContent(1,0.);
164  double plateau = h->GetMaximum();
165  bin_min = h->FindFirstBinAbove(1.);
166  double minPH = h->GetBinContent(bin_min);
167 
168  dist = abs(minPH - (255 - plateau));
169 
170  cout<<"offset = " << io << ", plateau = " << plateau << ", minPH = " << minPH << ", distance = " << dist << endl;
171 
172  if (dist < bestDist){
173  io_opt = io;
174  bestDist = dist;
175  }
176  }
177 
178  //stretching curve to maximally exploit range
179  is_opt=999;
180  safety_margin=15;
181  bestDist=255;
182  dist =0;
183  upEd_dist=255;
184  lowEd_dist=255;
185  for(is=0; is<52; is++){
186  h = (TH1D*)gDirectory->Get(Form("DacScan/ph_Vcal_c11_r20_C0_V%d", 52*io_opt+is));
187  cout<<h->GetTitle()<<endl;
188  h->Draw();
189  //erasing first garbage bin
190  h->SetBinContent(1,0.);
191  bin_min = h->FindFirstBinAbove(1.);
192  upEd_dist = abs(h->GetBinContent(h->FindFixBin(255)) - (255 - safety_margin));
193  lowEd_dist = abs(h->GetBinContent(bin_min) - safety_margin);
194  dist = (upEd_dist < lowEd_dist ) ? (upEd_dist) : (lowEd_dist);
195  if(dist<bestDist){
196  is_opt = is;
197  bestDist=dist;
198  //break;
199  }
200  }
201 
202  if(is_opt==999){
203  cout<<"PH optimization failed (stretching)"<<endl;
204  return;
205  }
206 
207 
208  hopt = (TH1D*)gDirectory->Get(Form("DacScan/ph_Vcal_c11_r20_C0_V%d", 52*io_opt+is_opt));
209  cout << "h = " << hopt << " name: " << h->GetName() << endl;
210  cout << "optimal PH parameters:" << endl << "PH scale = " << is_opt << endl << "PH offset = " << io_opt << endl << "Best Distance " << bestDist << endl ;
211  hopt->Draw("");
212 
213 }
214 
215 void riseLengthMap(string rootfile = "phOpt.root") {
216  TFile *f = TFile::Open(rootfile.c_str());
217 
218 
219  TH1D *h;
220  TH2F *hmap = new TH2F("hmap", "PH rise lenght map", 26, -5., 255, 52, -2.5, 257.5);
221  TCanvas *c0 = new TCanvas("C0");
222  c0->Clear();
223  unsigned int io, is;
224  int bin_min, bin_max;
225  double lenght, plateau, minPH;
226 
227  for(io=0; io<26; io++){
228  for(is=0; is<52; is++){
229  h = (TH1D*)gDirectory->Get(Form("DacScan/ph_Vcal_c11_r20_C0_V%d", 52*io+is));
230  cout<<h->GetTitle()<<endl;
231  h->Draw();
232  //erasing first garbage bin
233  h->SetBinContent(1,0.);
234  bin_min = h->FindFirstBinAbove(1.);
235  plateau = h->GetMaximum();
236  bin_min = h->FindFirstBinAbove(1.);
237  bin_max = h->FindFirstBinAbove(0.9*plateau);
238  double minPH = h->GetBinContent(bin_min);
239 
240  lenght = sqrt( pow((h->GetBinCenter(bin_max) - h->GetBinCenter(bin_min) ),2) + pow((plateau*0.9 - h->GetBinContent(bin_min) ),2) );
241 
242  hmap->Fill(io*10, is*5, lenght);
243  }
244  }
245  hmap->GetXaxis()->SetTitle("PH offset [DAC]");
246  hmap->GetYaxis()->SetTitle("PH scale [DAC]");
247  gStyle->SetPalette(1);
248  set_plot_style();
249  gPad->SetTickx();
250  gPad->SetTicky();
251  hmap->Draw("colz");
252 
253 }
254 
255 
256 void
257 set_plot_style()
258 {
259  const Int_t NRGBs = 5;
260  const Int_t NCont = 255;
261 
262  Double_t stops[NRGBs] = { 0.00, 0.34, 0.61, 0.84, 1.00 };
263  Double_t red[NRGBs] = { 0.00, 0.00, 0.87, 1.00, 0.51 };
264  Double_t green[NRGBs] = { 0.00, 0.81, 1.00, 0.20, 0.00 };
265  Double_t blue[NRGBs] = { 0.51, 1.00, 0.12, 0.00, 0.00 };
266  TColor::CreateGradientColorTable(NRGBs, stops, red, green, blue, NCont);
267  gStyle->SetNumberContours(NCont);
268 }
virtual void doTest()
function connected to "DoTest" button of PixTab
Definition: PixTest.cc:602
virtual void setDAC(std::string parName, uint8_t val)
allow setting DACs in scripts for entire DUT
Definition: PixTest.hh:239
virtual bool setParameter(std::string parName, std::string sval)
set the string value of a parameter
Definition: PixTest.cc:433