cmd_analyzer.cpp 10.1 KB
Newer Older
Stephan Wiederkehr's avatar
Stephan Wiederkehr committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* -------------------------------------------------------------
 *
 *  file:        command.cpp
 *
 *  description: command line interpreter for Chip/Wafer tester
 *
 *  author:      Beat Meier
 *  modified:    31.8.2007
 *
 *  rev:
 *
 * -------------------------------------------------------------
 */


#include "cmd.h"
#include "datalink.h"

#define IMG_WIDTH 157
#define IMG_HEIGHT 163
#define R4S_VALUE_OR 4096

class R4sImg
{
25 26 27 28 29 30 31 32 33 34 35
  int *data;
  std::vector<int> dataNEW;
  
 public:
 R4sImg() : data(0) {}
  bool CreateRaw(const vector<uint16_t> &rawdata);
  void Clear() { if (data) delete[] data; data = 0; dataNEW.clear();}
  void Save(const string &filename);
  void Print(unsigned int count);
  int Get(int x, int y);
  std::vector<int> getData() {return dataNEW;}
Stephan Wiederkehr's avatar
Stephan Wiederkehr committed
36 37
};

38 39 40 41 42 43 44
int R4sImg::Get(int x, int y) {

  if ( y>IMG_HEIGHT || x>IMG_WIDTH )
    {printf("ERROR: Out of range");return -9999;}

 return data[y*IMG_WIDTH + x]; 
}
Stephan Wiederkehr's avatar
Stephan Wiederkehr committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

bool R4sImg::CreateRaw(const vector<uint16_t> &rawdata)
{
	Clear();
	if (rawdata.size() < IMG_WIDTH*IMG_HEIGHT) return false;
	data = new int[IMG_WIDTH*IMG_HEIGHT];

	for (int pos=0; pos<IMG_WIDTH*IMG_HEIGHT; pos++)
	{
		int value = rawdata[pos];

		if (value & 0x1000) // ADC overrange
			value = R4S_VALUE_OR;
		else if (value & 0x0800) // negative
			value -= 0x1000;

		data[pos] = value;
62
		dataNEW.push_back(value);
Stephan Wiederkehr's avatar
Stephan Wiederkehr committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
	}
	return true;
};


void R4sImg::Print(unsigned int count)
{
	if (!data) 
	{
		printf("<<empty>>\n");
		return;
	}

	if (count > IMG_WIDTH*IMG_HEIGHT) count = IMG_WIDTH*IMG_HEIGHT;

	for (unsigned int i=0; i<count; i++)
	{
		int v = data[i];
		if (v == R4S_VALUE_OR) printf("   or"); else printf(" %4i", v);
	}
	printf("\n");

	tb.Daq_Close();
}


void R4sImg::Save(const string &filename)
{
91 92 93 94 95 96 97

	if (!data) 
	{
		printf("<<empty>>\n");
		return;
	}

Stephan Wiederkehr's avatar
Stephan Wiederkehr committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
	FILE *f = fopen(filename.c_str(), "wt");
	if (!f) return;

	int i = 0, x, y;

	for (y=IMG_HEIGHT-1; y>=0; y--)
	{
		for (x=0; x<IMG_WIDTH; x++)
		{
			int	v = Get(x,y);
			if (v == R4S_VALUE_OR) fprintf(f, "   or"); else fprintf(f, " %4i", v);
		}
		fputs("\n", f);
	}

	fclose(f);
}

116
bool ReadImage(R4sImg &map, bool verbose=true)
Stephan Wiederkehr's avatar
Stephan Wiederkehr committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
{
	tb.Daq_Open(50000);

	// prepare ADC
	tb.SignalProbeADC(PROBEA_SDATA1, GAIN_1);
//	tb.r4s_AdcDelay(0);
	tb.r4s_Enable(3); // slow readout
	tb.uDelay(100);

	// take data
	tb.Daq_Start();
	tb.r4s_Start();
	tb.uDelay(3000);
	tb.Daq_Stop();

	// stop ADC
	tb.r4s_Enable(0);

	// read buffer
	vector<uint16_t> data;
	unsigned int ret = tb.Daq_Read(data);

	unsigned int n = data.size();
140
	if (verbose) {printf("--- status = %u; n = %u\n", ret, (unsigned int)(data.size()));}
Stephan Wiederkehr's avatar
Stephan Wiederkehr committed
141 142 143

	tb.Daq_Close();

144
        return map.CreateRaw(data);
Stephan Wiederkehr's avatar
Stephan Wiederkehr committed
145 146
}

147 148 149 150
bool configure() {

  //need to have a function configuring the chip from a file
}
Stephan Wiederkehr's avatar
Stephan Wiederkehr committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

CMD_PROC(getimg)
{
	R4sImg map;
	ReadImage(map);
	map.Print(200);
	map.Save("map.txt");
}


CMD_PROC(seqreadout)
{
	tb.r4s_SetSeqReadout();
	DO_FLUSH
}


CMD_PROC(seqcalscan)
{
	tb.r4s_SetSeqCalScan();
	DO_FLUSH
}


175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
// CMD_PROC(gui)
// {
// 	printf("Connect to GUI ...");
// 	CDataLink link(1024, 65536);
// //	ShellExecute(NULL, "open", "file.exe", NULL, NULL, SW_SHOWDEFAULT);

// 	system ("start roc4sens_view\\bin\\Release\\roc4sens_view.exe");
// //	system ("start roc4sens_view.exe");

// 	link.Run();
// 	printf("Connection to GUI closed.\n");
// }

////////// ANALYSIS


//#include "pixelAlive.h"
#include "TROOT.h"
#include "TH1D.h"
#include "TH2D.h"
#include "TCanvas.h"
#include "TFile.h"
#include "TString.h"
#include "TTree.h"
#include "TBranch.h"
#include "TLeaf.h"
201 202
#include "TGraph.h"

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237

CMD_PROC(pixelAlive)
{
  R4sImg map;
  ReadImage(map);
  map.Print(200);

  TFile *rootFile = new TFile("pixelAlive.root","recreate");
  // TH1D *h = new TH1D("h","title",10,0,10);
  // h->Fill(1);
  TH2D *pixelMap = new TH2D("pixelMap","pixel map",IMG_WIDTH,0,IMG_WIDTH,IMG_HEIGHT,0,IMG_HEIGHT);

  //debugging
  FILE *f = fopen("debug.txt", "wt");
  
  std::vector<int> data = map.getData();
  printf("data size: %d // should be %d \n",data.size(),IMG_HEIGHT*IMG_WIDTH);

  for (unsigned int i=0;i<data.size();i++)
    {
      fprintf(f,"DEBUG: Filling %d : %d/%d \n",i,i%IMG_WIDTH,i/IMG_WIDTH);
      pixelMap->Fill(i%IMG_WIDTH,i/IMG_WIDTH,data[i]);
    }
  fclose(f);

  // int *i;
  // char **ch;
  // TApplication app("app",i,ch);
  // TCanvas *c = new TCanvas("c","canvas",200,100);
  // h->Draw();
  // app.Run();
  pixelMap->Write();
  rootFile->Close();
  
}
Stephan Wiederkehr's avatar
Stephan Wiederkehr committed
238

239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
//pixel Distribution
//Distribution of adc values for a given Vcal per pixel
CMD_PROC(pixelDist) {

  int iterations;
  PAR_INT(iterations, 0, 2147483647) //INT_MAX

  TFile *rootFile = new TFile("pixelDist.root","recreate");
  TTree *tPixDist = new TTree("pixelDist","pixelDist");
  int col(0),row(0),iteration(0),value(0);
  tPixDist->Branch("col",&col,"col/I");
  tPixDist->Branch("row",&row,"row/I");
  tPixDist->Branch("it",&iteration,"it/I");
  tPixDist->Branch("value",&value,"value/I");

  std::vector< std::vector<int> > data;
  R4sImg map;
  
  //get the data
  printf("Taking data...");fflush(stdout);
  for (int i=0;i<iterations;i++)
    {
261
      if (!ReadImage(map,false)) {break;}
262 263 264 265 266 267
      std::vector<int> data_single = map.getData();
      printf("Iteration %d: data size: %d // should be %d \n",i+1,data_single.size(),IMG_HEIGHT*IMG_WIDTH);
      data.push_back(data_single);
    }
  printf("Done.\n");

268
  //filling the data into the tree
269 270 271 272 273 274 275 276 277 278 279 280 281
  printf("Writing the data into a TTree...");fflush(stdout);
  for (int j=0;j<IMG_HEIGHT*IMG_WIDTH;j++)
    {
      for (unsigned int k=0;k<data.size();k++)
	{
	  col = j%IMG_WIDTH;
	  row = j/IMG_WIDTH;
	  iteration = k;
	  value = data[k][j];
	  tPixDist->Fill();
	}
    }
  printf("Done.\n");
Stephan Wiederkehr's avatar
Stephan Wiederkehr committed
282

283 284
  tPixDist->Write();
  rootFile->Close();
Stephan Wiederkehr's avatar
Stephan Wiederkehr committed
285
}
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311


//pulse shape analysis
CMD_PROC(pulseShape) {

  int calX,calY;
  PAR_INT(calX,0,255)
  PAR_INT(calY,0,255)

  TFile *rootFile = new TFile("pulseShape.root","recreate");
  TTree *tPulseShape = new TTree("pulseShape","pulseShape");
  int col(0),row(0),hold(0),value(0);
  tPulseShape->Branch("col",&col,"col/I");
  tPulseShape->Branch("row",&row,"row/I");
  tPulseShape->Branch("hold",&hold,"hold/I");
  tPulseShape->Branch("value",&value,"value/I");

  std::vector< std::vector<int> > data;
  R4sImg map;

  const unsigned int hold_max = 255;

  //get the data
  printf("Setting cal for pixel x:%d y:%d\n",calX,calY);
  tb.r4s_SetPixCal(calX, calY);
  printf("Taking data...");fflush(stdout);
312
  
313 314 315
  for (unsigned int i=0;i<hold_max;i++)
    {
      tb.r4s_SetHoldPos(i);
316
      if (!ReadImage(map,false)) {break;}
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
      std::vector<int> data_single = map.getData();
      printf("Hold %d: data size: %d // should be %d \n",i+1,data_single.size(),IMG_HEIGHT*IMG_WIDTH);
      data.push_back(data_single);
    }
  printf("Done.\n");

  //filling the data into the tree
  printf("Writing the data into a TTree...");fflush(stdout);
  for (int j=0;j<IMG_HEIGHT*IMG_WIDTH;j++)
    {
      for (unsigned int k=0;k<data.size();k++)
	{
	  col = j%IMG_WIDTH;
	  row = j/IMG_WIDTH;
	  hold = k;
	  value = data[k][j];
	  tPulseShape->Fill();
	}
    }
  printf("Done.\n");

  tPulseShape->Write();
  rootFile->Close();

}
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399


//pulse shape analysis
//As it is, it takes too long...
// CMD_PROC(pulseShapeAll) {

//   TFile *rootFile = new TFile("pulseShapeAll.root","recreate");
//   TTree *tPulseShape = new TTree("pulseShape","pulseShape");
//   int col(0),row(0),hold(0),value(0);
//   tPulseShape->Branch("col",&col,"col/I");
//   tPulseShape->Branch("row",&row,"row/I");
//   tPulseShape->Branch("hold",&hold,"hold/I");
//   tPulseShape->Branch("value",&value,"value/I");

//   std::vector< std::vector<int> > data;
//   R4sImg map;

//   const unsigned int hold_max = 255;

//   //get the data
//   printf("Taking data...");fflush(stdout);
//   for (int j=0;j<IMG_HEIGHT*IMG_WIDTH;j++)
//     {
//       tb.r4s_SetPixCal(j%IMG_WIDTH, j/IMG_WIDTH);
//       std::vector<int> readings;
//       printf("pixel %d\n",j);
//       for (unsigned int i=0;i<hold_max;i++)
// 	{
// 	  tb.r4s_SetHoldPos(i);
// 	  if (!ReadImage(map,false)) {break;}
// 	  std::vector<int> data_single = map.getData();
// 	  int reading = data_single[j];
// 	  readings.push_back(reading);
// 	}
//       data.push_back(readings);
//     }
//   printf("Done.\n");
      

//   //filling the data into the tree
//   printf("Writing the data into a TTree...");fflush(stdout);
//   for (int j=0;j<IMG_HEIGHT*IMG_WIDTH;j++)
//     {
//       for (unsigned int k=0;k<data[j].size();k++)
// 	{
// 	  col = j%IMG_WIDTH;
// 	  row = j/IMG_WIDTH;
// 	  hold = k;
// 	  value = data[j][k];
// 	  tPulseShape->Fill();
// 	}
//     }
//   printf("Done.\n");

//   tPulseShape->Write();
//   rootFile->Close();

// }
Stephan Wiederkehr's avatar
Stephan Wiederkehr committed
400

401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
//pulse shape, creating a graph
CMD_PROC(pulseShapeGraph) {
  int calX,calY, Rg, Ia;
  PAR_INT(calX,0,255);
  PAR_INT(calY,0,255);
  PAR_INT(Rg,0,2500);
  PAR_INT(Ia,0,2000);

  
  
  TFile *rootFile = new TFile("data/pulseShapeGraph.root","update");
 
  // create arrays with values
  int t[256], pulseHight[256];
    
  R4sImg map;

  const unsigned int hold_max = 255;

  //set parameters and get the data
  tb._SetIA(Ia*10);
  printf("Set Ia to %d mA\n",Ia);
  tb.r4s_SetRgsh(Rg);
  tb.r4s_SetRgpr(Rg);
  printf("Set Rgpr and Rgsh to %d mV\n",Rg);
  tb.r4s_SetPixCal(calX, calY);
  printf("Set cal for pixel x:%d y:%d\n",calX,calY);

  printf("Taking data...");fflush(stdout);

  // loo over all values for hold
  for (unsigned int i=0;i<hold_max;i++)
    {
      tb.r4s_SetHoldPos(i);
      ReadImage(map,false);
      //write values into arrays
      t[i] =  (i*5);//one unit in hold is 5ns
      pulseHight[i] = map.Get(calX,calY);
    }
    
  printf("Done.\n");  
 
  // t, pulseHight arrays
  TGraph *gr = new TGraph( (hold_max), t, pulseHight);
  
  char GraphName[60];
  sprintf(GraphName, "PulseShapeX%dY%dRg%dIa%d",calX,calY,Rg,Ia);
rohe's avatar
test  
rohe committed
448

449 450 451 452 453 454 455
  gr->Write(GraphName);
  rootFile->Close();
    
  
}