#include "TMath.h"
#include "TString.h"
#include "TGraph.h"
+#include "TGraphErrors.h"
+#include "TMultiGraph.h"
+#include "TLegend.h"
#include "TH1D.h"
#include "TH1I.h"
#include "TH2I.h"
#include "TFile.h"
#include "TText.h"
#include "TCanvas.h"
+#include "TColor.h"
+#include "TStyle.h"
#include "analysis.hpp"
#include "hadaq/api.h"
+#ifndef SLEEPVAL
+#define SLEEPVAL 1000
+#endif
+
// TH1I* split(std::string text, char delim, int stepsize, int start, int end) {
// using namespace std;
}
}
+float GetBBV(std::string text, char delim) {
+
+ using namespace std;
+
+ string line;
+
+ stringstream ss(text);
+ Int_t counter = 0;
+ while(getline(ss, line, delim)) {
+ TString lline (line);
+ if (counter == 0){
+ //cout << lline << endl;
+ return lline.Atof();
+
+ }
+ counter++;
+ }
+}
+
+float GetBBI(std::string text, char delim) {
+
+ using namespace std;
+
+ string line;
+
+ stringstream ss(text);
+ Int_t counter = 0;
+ while(getline(ss, line, delim)) {
+ TString lline (line);
+ if (counter == 1){
+ //cout << lline << endl;
+ return lline.Atof();
+
+ }
+ counter++;
+ }
+}
+
+char FindMatrix(std::string text) {
+
+ using namespace std;
+
+ char c = text[5];
+ return c;
+}
+
+Int_t GetVCASN(std::string text, char delim) {
+
+ using namespace std;
+
+ string line;
+
+ stringstream ss(text);
+ Int_t counter = 0;
+ while(getline(ss, line, delim)) {
+ TString lline (line);
+ if (counter == 0){
+ //cout << lline << endl;
+ string sline(lline.Data());
+ string rsline = sline.substr(sline.find('-') + 1);
+ TString rline(rsline);
+ return rline.Atoi();
+
+ }
+ counter++;
+ }
+}
+
+float GetThresh(std::string text, char delim) {
+
+ using namespace std;
+
+ string line;
+
+ stringstream ss(text);
+ Int_t counter = 0;
+ while(getline(ss, line, delim)) {
+ TString lline (line);
+ if (counter == 1){
+ //cout << lline << endl;
+ return lline.Atof();
+
+ }
+ counter++;
+ }
+}
+
+float GetFPN(std::string text, char delim) {
+
+ using namespace std;
+
+ string line;
+
+ stringstream ss(text);
+ Int_t counter = 0;
+ while(getline(ss, line, delim)) {
+ TString lline (line);
+ if (counter == 2){
+ //cout << lline << endl;
+ return lline.Atof();
+
+ }
+ counter++;
+ }
+}
+
void
//
// }
-void
+const char*
analysis::
make_quick_fit(
char* file,
vector<Float_t> v_x(255/stepsize); // Make x axis for hists
TH1D* Fpn = new TH1D("FPNHist", "FPNHist", 255, 0, 255);
- TH1D* ThN = new TH1D("ThermNoiseHist", "ThermNoiseHist", 40, 0, 20);
+ TH1D* ThN = new TH1D("ThermNoiseHist", "ThermNoiseHist", 70, 0, 35);
cout << "Reading data from CSV..." << endl;
float minThrLimit = 20;
float maxThrLimit = 230;
float minNoiseLim = 0.5;
- float maxNoiseLim = 20;
+ float maxNoiseLim = 35;
ofstream outfile("BadFittedPixels.txt");
cout << "Fitting Threshold and Noise Histograms." << endl;
TCanvas *t1 = new TCanvas();
- TF1 *gausThn = new TF1("gausThn", "gaus", 1, 20);
+ TF1 *gausThn = new TF1("gausThn", "gaus", 0.5, 35);
TF1 *gausFpn = new TF1("gausFpn", "gaus", 30, 255);
gausFpn->SetParameter(1, Fpn->GetMean(1));
gausFpn->SetParameter(2, Fpn->GetStdDev(1));
t->WriteObject(Fpn, "FPN");
t->WriteObject(ThN, "THN");
t->Close();
+
+ string* retStr = new string(to_string(gausFpn->GetParameter(1) * slopex + offset) + "\t" + to_string(gausFpn->GetParameter(2) * slopex) + "\t" + to_string(gausThn->GetParameter(1) * slopex) + "\t" + to_string(badFit));
+ // //sretStr &= to_string(gausFpn->GetParameter(1) * slopex + offset) + "\t" + to_string(gausFpn->GetParameter(2) * slopex) + "\t" + to_string(gausThn->GetParameter(1) * slopex) + "\t" + to_string(badFit);
+
+ const char* retChars = retStr->c_str();
+
+ return retChars;
+
}
TFile* t = new TFile("PixelMatrix.root", "recreate");
t->WriteObject(PixelMatrix, "PixelMatrix");
t->Close();
-
TCanvas *t2 = new TCanvas();
+ gStyle->SetPalette(kDeepSea);
PixelMatrix->Draw();
t2->SaveAs("NoiseHist.png","png");
TH2I* PixelMatrix = nullptr;
inputFile.GetObject("PixelMatrix",PixelMatrix);
string noisyPix = "x-coordinate\ty-coordinate\tRate\n";
+ TH2I* NoiseMatrix = new TH2I("NoiseMatrix", "NoiseMatrix", 1024, 0, 1023, 504, 0, 503);
for (Int_t x = 0; x < 1024; ++x) {
if (PixelMatrix->GetBinContent(PixelMatrix->GetBin(x,y)) >= frameLimit * noiseLimit) {
noisyPix.append(to_string(x) + "\t" + to_string(y) + "\t" + to_string((PixelMatrix->GetBinContent(PixelMatrix->GetBin(x,y)) + 0.) / frameLimit) + "\n");
+ NoiseMatrix->Fill(x,y);
}
ofstream outfile("noisyPixels.txt");
outfile << noisyPix;
outfile.close();
+ TCanvas *t2 = new TCanvas();
+ NoiseMatrix->SetMaximum(2);
+ gStyle->SetPalette(kDeepSea);
+ NoiseMatrix->Draw();
+
+ t2->SaveAs("NoisyPixels.png", "png");
+
+}
+
+void
+analysis::
+plot_bb( char* file )
+{
+ using namespace std;
+
+ ifstream ifs(file);
+
+ string line;
+ Int_t linecounter = 0;
+
+ TGraph* gr = new TGraph();
+
+ while (getline(ifs, line)) {
+
+ if (linecounter > 0) {
+
+ gr->AddPoint(GetBBV(line, '\t'), 1000. * GetBBI(line, '\t'));
+
+ }
+
+ ++linecounter;
+
+ }
+
+ gr->SetTitle("Backbias V/I Plot;Back Bias (V);Current (mA)");
+
+ TCanvas *t2 = new TCanvas();
+ gr->SetMarkerStyle(21);
+ gr->Draw();
+
+ t2->SaveAs("BB.png", "png");
+
+}
+
+void
+analysis::
+plot_thresh_fpn( char* file)
+{
+ using namespace std;
+
+ ifstream ifs(file);
+
+ string line;
+ Int_t linecounter = 0;
+
+ TGraphErrors* grA = new TGraphErrors();
+ TGraphErrors* grB = new TGraphErrors();
+ TGraphErrors* grC = new TGraphErrors();
+ TGraphErrors* grD = new TGraphErrors();
+
+ grA->SetMarkerStyle(21);
+ grB->SetMarkerStyle(21);
+ grC->SetMarkerStyle(21);
+ grD->SetMarkerStyle(21);
+
+ grA->SetMarkerColor(2);
+ grB->SetMarkerColor(3);
+ grC->SetMarkerColor(4);
+ grD->SetMarkerColor(6);
+
+
+
+ grA->SetLineColor(2);
+ grB->SetLineColor(3);
+ grC->SetLineColor(4);
+ grD->SetLineColor(6);
+
+ Int_t a = 0;
+ Int_t b = 0;
+ Int_t c = 0;
+ Int_t d = 0;
+
+ while (getline(ifs, line)) {
+
+ if (linecounter > 1) {
+
+ if (FindMatrix(line) == 'A') {
+ grA->AddPoint(GetVCASN(line, '\t'), GetThresh(line, '\t'));
+ grA->SetPointError(a, 0, GetFPN(line, '\t'));
+ ++a;
+ }
+ if (FindMatrix(line) == 'B') {
+ grB->AddPoint(GetVCASN(line, '\t'), GetThresh(line, '\t'));
+ grB->SetPointError(b, 0, GetFPN(line, '\t'));
+ ++b;
+ }
+ if (FindMatrix(line) == 'C') {
+ grC->AddPoint(GetVCASN(line, '\t'), GetThresh(line, '\t'));
+ grC->SetPointError(c, 0, GetFPN(line, '\t'));
+ ++c;
+ }
+ if (FindMatrix(line) == 'D') {
+ grD->AddPoint(GetVCASN(line, '\t'), GetThresh(line, '\t'));
+ grD->SetPointError(d, 0, GetFPN(line, '\t'));
+ ++d;
+ }
+
+ }
+
+ ++linecounter;
+
+ }
+
+
+ TMultiGraph* mg = new TMultiGraph();
+ mg->SetTitle("VCASN/Threshold and FPN plot;VCASN (LSB);Threshold and FPN (mV/e)");
+ mg->Add(grA);
+ mg->Add(grB);
+ mg->Add(grC);
+ mg->Add(grD);
+
+ TCanvas *t2 = new TCanvas();
+ mg->Draw("ALP");
+
+ TLegend* legend = new TLegend(0.1,0.7,0.48,0.9);
+ legend->SetHeader("Legend","C"); // option "C" allows to center the header
+ legend->AddEntry(grA,"Matrix A","ep");
+ legend->AddEntry(grB,"Matrix B","ep");
+ legend->AddEntry(grC,"Matrix C","ep");
+ legend->AddEntry(grD,"Matrix D","ep");
+ legend->Draw();
+ t2->SaveAs("VCASN.png", "png");
}
char* matD
) {
using namespace std;
- TH2I* PixelMatrix = new TH2I("PixelMatrix", "PixelMatrix", 1024, 0, 1023, 504, 0, 503);
+ TH2I* PixelMatrix = new TH2I("DeadPixelMatrix", "DeadPixelMatrix", 1024, 0, 1023, 504, 0, 503);
ifstream ifsA(matA);
string lineA;
}
TCanvas *t2 = new TCanvas();
+ PixelMatrix->SetMaximum(2);
+ gStyle->SetPalette(kDeepSea);
PixelMatrix->Draw();
-
t2->SaveAs("DeadPixels.png", "png");
}
-
-int
+void
analysis::
-find_rough_threshold(
- int fpga,
- bool singleaccess,
- int threshElectron,
- int lowerLimitVCASN,
- int higherLimitVCASN,
- int VCASNStepSize,
- int numberOfFrames,
- char* matrix)
-{
+plot_ineff_pixels(
+ char* matA,
+ char* matB,
+ char* matC,
+ char* matD
+) {
using namespace std;
+ TH2I* PixelMatrix = new TH2I("IneffPixelMatrix", "IneffPixelMatrix", 1024, 0, 1023, 504, 0, 503);
+
+ ifstream ifsA(matA);
+ string lineA;
- mimosis::register_write_sec(fpga, 0x46, threshElectron, singleaccess);
-
- if (matrix == "A") {
+ while (getline(ifsA, lineA)) {
- mimosis::register_write_sec(fpga, 0x49, 1, singleaccess);
- mimosis::register_write_sec(fpga, 0x4A, 1, singleaccess);
- mimosis::register_write_sec(fpga, 0x4B, 1, singleaccess);
+ stringstream ss(lineA);
+ Int_t counter = 0;
+ Int_t x;
+ Int_t y;
+ while(getline(ss, lineA, '\t')) {
+ TString llineA (lineA);
+ if (counter == 0) {x = llineA.Atoi();}
+ if (counter == 1) {y = llineA.Atoi();}
+ ++counter;
+ }
+ PixelMatrix->Fill(x, y);
}
- else if (matrix == "B") {
+ ifstream ifsB(matB);
+ string lineB;
+
+ while (getline(ifsB, lineB)) {
- mimosis::register_write_sec(fpga, 0x48, 1, singleaccess);
- mimosis::register_write_sec(fpga, 0x4A, 1, singleaccess);
- mimosis::register_write_sec(fpga, 0x4B, 1, singleaccess);
+ stringstream ss(lineB);
+ Int_t counter = 0;
+ Int_t x;
+ Int_t y;
+ while(getline(ss, lineB, '\t')) {
+ TString llineB (lineB);
+ if (counter == 0) {x = llineB.Atoi();}
+ if (counter == 1) {y = llineB.Atoi();}
+ ++counter;
+ }
+ PixelMatrix->Fill(x, y);
}
- else if (matrix == "C") {
+ ifstream ifsC(matC);
+ string lineC;
+
+ while (getline(ifsC, lineC)) {
- mimosis::register_write_sec(fpga, 0x48, 1, singleaccess);
- mimosis::register_write_sec(fpga, 0x49, 1, singleaccess);
- mimosis::register_write_sec(fpga, 0x4B, 1, singleaccess);
+ stringstream ss(lineC);
+ Int_t counter = 0;
+ Int_t x;
+ Int_t y;
+ while(getline(ss, lineC, '\t')) {
+ TString llineC (lineC);
+ if (counter == 0) {x = llineC.Atoi();}
+ if (counter == 1) {y = llineC.Atoi();}
+ ++counter;
+ }
+ PixelMatrix->Fill(x, y);
}
- else if (matrix == "D") {
+ ifstream ifsD(matD);
+ string lineD;
+
+ while (getline(ifsD, lineD)) {
- mimosis::register_write_sec(fpga, 0x48, 1, singleaccess);
- mimosis::register_write_sec(fpga, 0x49, 1, singleaccess);
- mimosis::register_write_sec(fpga, 0x4A, 1, singleaccess);
+ stringstream ss(lineD);
+ Int_t counter = 0;
+ Int_t x;
+ Int_t y;
+ while(getline(ss, lineD, '\t')) {
+ TString llineD (lineD);
+ if (counter == 0) {x = llineD.Atoi();}
+ if (counter == 1) {y = llineD.Atoi();}
+ ++counter;
+ }
+ PixelMatrix->Fill(x, y);
}
- else {return -1;}
+ TCanvas *t2 = new TCanvas();
+ PixelMatrix->SetMaximum(2);
+ gStyle->SetPalette(kDeepSea);
+ PixelMatrix->Draw();
+ t2->SaveAs("IneffPixels.png", "png");
+
- vector<double> time (((higherLimitVCASN - lowerLimitVCASN) / VCASNStepSize) + 1);
+}
+
+
+// int
+// analysis::
+// find_rough_threshold(
+// char* source,
+// int fpga = 0xc200,
+// bool singleaccess = false,
+// int threshElectron = 120,
+// float precision = 0.1,
+// int lowerLimitVCASN = 50,
+// int higherLimitVCASN = 210,
+// int VCASNStepSize = 2,
+// int numberOfFrames = 500,
+// int matrix = 1,
+// int xLow = 0,
+// int xHigh = 127,
+// int yLow = 250,
+// int yHigh = 251,
+// int mod = 2,
+// int exp = 3)
+// {
+// using namespace std;
//
-// for (int iter = 0; iter <= (((higherLimitVCASN - lowerLimitVCASN) / VCASNStepSize) + 1); ++iter) {
-//
-// if (matrix == "A") {register_write_sec(fpga, 0x48, (lowerLimitVCASN + iter * VCASNStepSize), singleaccess);}
-// if (matrix == "B") {register_write_sec(fpga, 0x49, (lowerLimitVCASN + iter * VCASNStepSize), singleaccess);}
-// if (matrix == "C") {register_write_sec(fpga, 0x4A, (lowerLimitVCASN + iter * VCASNStepSize), singleaccess);}
-// if (matrix == "D") {register_write_sec(fpga, 0x4B, (lowerLimitVCASN + iter * VCASNStepSize), singleaccess);}
+// mimosis::register_write(fpga, 0x46, threshElectron, singleaccess);
+//
+// int maxResponses;
+//
+// if (matrix == 1) {
+//
+// mimosis::register_write(fpga, 0x49, 1, singleaccess);
+// mimosis::register_write(fpga, 0x4A, 1, singleaccess);
+// mimosis::register_write(fpga, 0x4B, 1, singleaccess);
+//
+// maxResponses = (xHigh - xLow + 1) * (yHigh - yLow + 1) * numberOfFrames;
+//
+// }
+//
+// else if (matrix == 2) {
+//
+// mimosis::register_write(fpga, 0x48, 1, singleaccess);
+// mimosis::register_write(fpga, 0x4A, 1, singleaccess);
+// mimosis::register_write(fpga, 0x4B, 1, singleaccess);
+//
+// maxResponses = (xHigh - xLow + 1) * (yHigh - yLow + 1) * numberOfFrames;
+//
+// }
+//
+// else if (matrix == 3) {
+//
+// mimosis::register_write(fpga, 0x48, 1, singleaccess);
+// mimosis::register_write(fpga, 0x49, 1, singleaccess);
+// mimosis::register_write(fpga, 0x4B, 1, singleaccess);
//
+// maxResponses = (xHigh - xLow + 1) * (yHigh - yLow + 1) * numberOfFrames;
+//
+// }
+//
+// else if (matrix == 4) {
+//
+// mimosis::register_write(fpga, 0x48, 1, singleaccess);
+// mimosis::register_write(fpga, 0x49, 1, singleaccess);
+// mimosis::register_write(fpga, 0x4A, 1, singleaccess);
+//
+// maxResponses = (xHigh - xLow + 1) * (yHigh - yLow + 1) * numberOfFrames;
+//
+// }
+//
+// else {return -1;}
+//
+//
+// for (int iter = lowerLimitVCASN; iter <= higherLimitVCASN; iter += VCASNStepSize) {
+//
+// if (matrix == 1) {mimosis::register_write(fpga, 0x48, (lowerLimitVCASN + iter * VCASNStepSize), singleaccess);}
+// if (matrix == 2) {mimosis::register_write(fpga, 0x49, (lowerLimitVCASN + iter * VCASNStepSize), singleaccess);}
+// if (matrix == 3) {mimosis::register_write(fpga, 0x4A, (lowerLimitVCASN + iter * VCASNStepSize), singleaccess);}
+// if (matrix == 4) {mimosis::register_write(fpga, 0x4B, (lowerLimitVCASN + iter * VCASNStepSize), singleaccess);}
+//
// trb_register_write(fpga, 0x9209, iter);
-// usleep(SLEEPVAL);
-// auto start = high_resolution_clock::now();
+// usleep(SLEEPVAL);
//
+// int cntResponse = 0;
// int mimTra = 0;
+//
+// int pulse = static_cast<int>(std::pow(2.0,static_cast<double>(exp)));
+//
+// hadaq::ReadoutHandle ref = hadaq::ReadoutHandle::Connect(source);
+// if (ref.null()) return -1;
+// hadaq::RawEvent *evnt = nullptr;
//
// while( mimTra<numberOfFrames) {
//
//
// } else {
//
-// if( (data & 0xFF000000) == 0xFD000000 ) {
+// decode_pixel(data>>16,column,row,region);
+//
+// if(column >= xLow &&
+// column <= xHigh &&
+// row >= yLow &&
+// row <= yHigh &&
+// mimFraCnt%pulse == mod &&
+// mimFraCntOpt == iter) {
+//
+// cntResponse += 1;
//
-// int tmp = (data>>16) & 0xFF;
-// if(tmp > 63) continue;
-// region = tmp;
+// // printf("%d %d %d %d %d %d\n",
+// // column, column-xLow, row, row-yLow, vphfine, SUPERINDEX(column,xLow,xSpan,row,yLow,vphfine));
+// }
+//
+// int code = (data >> 16) & 0b111;
+//
+// if ( code & 0b1) {
+//
+// int codeTmp = 1;
+// decode_pixel(((data >> 16) + (codeTmp << 6)), column, row, region);
+//
+// if(column >= xLow && column <= xHigh &&
+// row >= yLow && row <= yHigh &&
+// mimFraCnt%pulse == mod &&
+// mimFraCntOpt == iter) {
//
+// cntResponse += 1;
+// }
// }
//
-// if(mimTra >= maxCounts) {
-// goto MAXCOUNTS;
+// if ( code & 0b10) {
+//
+// int codeTmp = 2;
+// decode_pixel(((data >> 16) + (codeTmp << 6)), column, row, region);
+//
+// if(column >= xLow && column <= xHigh &&
+// row >= yLow && row <= yHigh &&
+// mimFraCnt%pulse == mod &&
+// mimFraCntOpt == iter) {
+//
+// cntResponse += 1;
+// }
+// }
+//
+// if ( code & 0b100) {
+//
+// int codeTmp = 3;
+// decode_pixel(((data >> 16) + (codeTmp << 6)), column, row, region);
+//
+// if(column >= xLow && column <= xHigh &&
+// row >= yLow && row <= yHigh &&
+// mimFraCnt%pulse == mod &&
+// mimFraCntOpt == iter) {
+//
+// cntResponse += 1;
+// }
+// }
// }
-// } // End loop over data in sub event
+//
+// if((data & 0x0000FF00) != 0x0000FC00) {
+//
+// decode_pixel(data&0xFFFF,column,row,region);
+//
+// if(column >= xLow &&
+// column <= xHigh &&
+// row >= yLow &&
+// row <= yHigh &&
+// mimFraCnt%pulse == mod &&
+// mimFraCntOpt == threshElectron) {
+//
+// cntResponse += 1;
+//
+// // printf("%d %d %d %d %d %d\n",
+// // column, column-xLow, row, row-yLow, vphfine, SUPERINDEX(column,xLow,xSpan,row,yLow,vphfine));
+// }
+//
+// int code = (data & 0xFFFF) & 0b111;
+//
+// if ( code & 0b1) {
+//
+// int codeTmp = 1;
+// decode_pixel(((data & 0xFFFF) + (codeTmp << 6)), column, row, region);
+//
+// if(column >= xLow && column <= xHigh &&
+// row >= yLow && row <= yHigh &&
+// mimFraCnt%pulse == mod &&
+// mimFraCntOpt == iter) {
+//
+// cntResponse += 1;
+// }
+// }
+//
+// if ( code & 0b10) {
+//
+// int codeTmp = 2;
+// decode_pixel(((data & 0xFFFF) + (codeTmp << 6)), column, row, region);
+//
+// if(column >= xLow && column <= xHigh &&
+// row >= yLow && row <= yHigh &&
+// mimFraCnt%pulse == mod &&
+// mimFraCntOpt == iter) {
+//
+// cntResponse += 1;
+// }
+// }
+//
+// if ( code & 0b100) {
+//
+// int codeTmp = 3;
+// decode_pixel(((data & 0xFFFF) + (codeTmp << 6)), column, row, region);
+//
+// if(column >= xLow && column <= xHigh &&
+// row >= yLow && row <= yHigh &&
+// mimFraCnt%pulse == mod &&
+// mimFraCntOpt == iter) {
+//
+// cntResponse += 1;
+// }
+// }
+// }
+// }
// }
// } // End loop over sub-events in event
// }
// MAXCOUNTS:
-// auto stop = high_resolution_clock::now();
// trb_register_write(fpga, 0x9209, 0xaaaaaaaa);
-// }
-//
-// time[iter] = duration_cast<std::chrono::microseconds>(stop-start);
-//
-// }
-//
-// double timeDiff = time[((higherLimitVCASN - lowerLimitVCASN) / VCASNStepSize) + 1] - time[0];
-//
-// if (timeDiff <= 500) {return -1;}
-//
-// else {
-//
-// int lowestVCASN;
-// double timediff = numeric_limits<double>::max();
-//
-// for (int iter = 0; iter <= ((higherLimitVCASN - lowerLimitVCASN) / VCASNStepSize) + 1; ++iter) {
-//
-// if (abs(time[iter] - timeDiff) < timediff) {
-//
-// lowestVCASN = lowerLimitVCASN + iter * VCASNStepSize;
-// timediff = abs(time[iter] - timeDiff);
-//
-// }
+// if ( (cntResponse + 0.) / maxResponses >= 0.5 - 0.5 * precision || (cntResponse + 0.) / maxResponses <= 0.5 + 0.5 * precision ) {return iter;}
//
// }
-//
-// return lowestVCASN;
-//
-// }
+//
+// return -2;
+//
+// }
+void
+analysis::
+try_write_sec() {
+ using namespace std;
+
+ mimosis::register_write_sec(0xc200, 0x46, 120, false);
+
}
+
+int
+analysis::
+find_rough_threshold(
+ char* source,
+ int fpga = 0xc200,
+ bool singleaccess = false,
+ int threshElectron = 120,
+ float precision = 0.1,
+ int lowerLimitVCASN = 50,
+ int higherLimitVCASN = 210,
+ int VCASNStepSize = 2,
+ int numberOfFrames = 500,
+ int matrix = 1,
+ int xLow = 0,
+ int xHigh = 127,
+ int yLow = 250,
+ int yHigh = 251,
+ int mod = 2,
+ int exp = 3)
+{
+ using namespace std;
+
+ mimosis::register_write_sec(fpga, 0x46, threshElectron, singleaccess);
+
+ float maxResponses;
+
+ if (matrix == 1) {
+
+ mimosis::register_write_sec(fpga, 0x49, 1, singleaccess);
+ mimosis::register_write_sec(fpga, 0x4A, 1, singleaccess);
+ mimosis::register_write_sec(fpga, 0x4B, 1, singleaccess);
+
+ maxResponses = (xHigh - xLow + 1) * (yHigh - yLow + 1) * numberOfFrames;
+
+ }
+
+ else if (matrix == 2) {
+
+ mimosis::register_write_sec(fpga, 0x48, 1, singleaccess);
+ mimosis::register_write_sec(fpga, 0x4A, 1, singleaccess);
+ mimosis::register_write_sec(fpga, 0x4B, 1, singleaccess);
+
+ maxResponses = (xHigh - xLow + 1) * (yHigh - yLow + 1) * numberOfFrames;
+
+ }
+
+ else if (matrix == 3) {
+
+ mimosis::register_write_sec(fpga, 0x48, 1, singleaccess);
+ mimosis::register_write_sec(fpga, 0x49, 1, singleaccess);
+ mimosis::register_write_sec(fpga, 0x4B, 1, singleaccess);
+
+ maxResponses = (xHigh - xLow + 1) * (yHigh - yLow + 1) * numberOfFrames;
+
+ }
+
+ else if (matrix == 4) {
+
+ mimosis::register_write_sec(fpga, 0x48, 1, singleaccess);
+ mimosis::register_write_sec(fpga, 0x49, 1, singleaccess);
+ mimosis::register_write_sec(fpga, 0x4A, 1, singleaccess);
+
+ maxResponses = (xHigh - xLow + 1) * (yHigh - yLow + 1) * numberOfFrames;
+
+ }
+
+ else {return -1;}
+
+
+ for (int iter = lowerLimitVCASN; iter <= higherLimitVCASN; iter += VCASNStepSize) {
+
+ if (matrix == 1) {mimosis::register_write_sec(fpga, 0x48, (iter), singleaccess);}
+ if (matrix == 2) {mimosis::register_write_sec(fpga, 0x49, (iter), singleaccess);}
+ if (matrix == 3) {mimosis::register_write_sec(fpga, 0x4A, (iter), singleaccess);}
+ if (matrix == 4) {mimosis::register_write_sec(fpga, 0x4B, (iter), singleaccess);}
+
+ trb_register_write(fpga, 0x9209, iter);
+ usleep(SLEEPVAL);
+
+ int cntResponse = 0;
+ int mimTra = 0;
+
+ int pulse = static_cast<int>(std::pow(2.0,static_cast<double>(exp)));
+
+ hadaq::ReadoutHandle ref = hadaq::ReadoutHandle::Connect(source);
+ if (ref.null()) return -1;
+ hadaq::RawEvent *evnt = nullptr;
+
+ while( mimTra<numberOfFrames) {
+
+ evnt = ref.NextEvent(1.,-1);
+ if (!evnt) continue;
+
+ hadaq::RawSubevent* sub = nullptr;
+
+ // while (((sub = evnt->NextSubevent(sub)) != nullptr) && (sub->GetId() == fpga)) // Does not work!!
+ while ((sub = evnt->NextSubevent(sub)) != nullptr)
+ {
+ if (sub->GetId() == fpga)
+ {
+ unsigned size = sub->GetNrOfDataWords();
+
+ int headerNow = 0;
+
+ uint32_t mimFraCnt;
+ uint32_t mimFraCntOpt;
+
+ unsigned region = 0, column = 0, row = 0;
+
+ for( unsigned i = 0; i<size; i++) {
+
+ uint32_t data = static_cast<uint32_t>(sub->Data(i));
+
+ if((data & 0xFF000000) == 0xFE000000) {
+
+ ++headerNow;
+
+ if(headerNow == 1) {
+
+ mimFraCnt = (data & 0xFF0000) >> 16;
+ mimFraCnt += (data & 0xFF) << 8;
+
+ } else if(headerNow == 2) {
+
+ mimFraCnt += data & 0xFF0000;
+ mimFraCnt += (data & 0xFF) << 24;
+
+ // std::printf("%x\n",mimFraCnt);
+
+ } else if(headerNow == 3) {
+ mimFraCntOpt = (data & 0xFF0000) >> 16;
+ mimFraCntOpt += (data & 0xFF) << 8;
+ } else if(headerNow == 4) {
+ mimFraCntOpt += data & 0xFF0000;
+ mimFraCntOpt += (data & 0xFF) << 24;
+ }
+
+ } else if((data & 0xFF000000) == 0xFF000000) {
+
+ headerNow = 0;
+ if(mimFraCnt%pulse == mod &&
+ mimFraCntOpt == iter) { mimTra++;}
+ // cout << "Next counter" << endl;}
+
+ }
+
+
+
+ else {
+
+ if( (data & 0xFF000000) == 0xFD000000 ) {
+
+ int tmp = (data>>16) & 0xFF;
+ if(tmp > 63) continue;
+ region = tmp;
+
+ } else {
+
+ decode_pixel(data>>16,column,row,region);
+
+ // cout << column << " " << row << " " << region << endl;
+
+ if(column >= xLow &&
+ column <= xHigh &&
+ row >= yLow &&
+ row <= yHigh &&
+ mimFraCnt%pulse == mod &&
+ mimFraCntOpt == iter) {
+
+ cntResponse += 1;
+
+ // printf("%d %d %d %d %d %d\n",
+ // column, column-xLow, row, row-yLow, vphfine, SUPERINDEX(column,xLow,xSpan,row,yLow,vphfine));
+ }
+
+ int code = (data >> 16) & 0b111;
+
+ if ( code & 0b1) {
+
+ int codeTmp = 1;
+ decode_pixel(((data >> 16) + (codeTmp << 6)), column, row, region);
+
+ if(column >= xLow && column <= xHigh &&
+ row >= yLow && row <= yHigh &&
+ mimFraCnt%pulse == mod &&
+ mimFraCntOpt == iter) {
+
+ cntResponse += 1;
+ }
+ }
+
+ if ( code & 0b10) {
+
+ int codeTmp = 2;
+ decode_pixel(((data >> 16) + (codeTmp << 6)), column, row, region);
+
+ if(column >= xLow && column <= xHigh &&
+ row >= yLow && row <= yHigh &&
+ mimFraCnt%pulse == mod &&
+ mimFraCntOpt == iter) {
+
+ cntResponse += 1;
+ }
+ }
+
+ if ( code & 0b100) {
+
+ int codeTmp = 3;
+ decode_pixel(((data >> 16) + (codeTmp << 6)), column, row, region);
+
+ if(column >= xLow && column <= xHigh &&
+ row >= yLow && row <= yHigh &&
+ mimFraCnt%pulse == mod &&
+ mimFraCntOpt == iter) {
+
+ cntResponse += 1;
+ }
+ }
+ }}
+
+ if((data & 0x0000FF00) != 0x0000FC00) {
+
+ decode_pixel(data&0xFFFF,column,row,region);
+
+ if(column >= xLow &&
+ column <= xHigh &&
+ row >= yLow &&
+ row <= yHigh &&
+ mimFraCnt%pulse == mod &&
+ mimFraCntOpt == iter) {
+
+ cntResponse += 1;
+
+ // printf("%d %d %d %d %d %d\n",
+ // column, column-xLow, row, row-yLow, vphfine, SUPERINDEX(column,xLow,xSpan,row,yLow,vphfine));
+ }
+
+ int code = (data & 0xFFFF) & 0b111;
+
+ if ( code & 0b1) {
+
+ int codeTmp = 1;
+ decode_pixel(((data & 0xFFFF) + (codeTmp << 6)), column, row, region);
+
+ if(column >= xLow && column <= xHigh &&
+ row >= yLow && row <= yHigh &&
+ mimFraCnt%pulse == mod &&
+ mimFraCntOpt == iter) {
+
+ cntResponse += 1;
+ }
+ }
+
+ if ( code & 0b10) {
+
+ int codeTmp = 2;
+ decode_pixel(((data & 0xFFFF) + (codeTmp << 6)), column, row, region);
+
+ if(column >= xLow && column <= xHigh &&
+ row >= yLow && row <= yHigh &&
+ mimFraCnt%pulse == mod &&
+ mimFraCntOpt == iter) {
+
+ cntResponse += 1;
+ }
+ }
+
+ if ( code & 0b100) {
+
+ int codeTmp = 3;
+ decode_pixel(((data & 0xFFFF) + (codeTmp << 6)), column, row, region);
+
+ if(column >= xLow && column <= xHigh &&
+ row >= yLow && row <= yHigh &&
+ mimFraCnt%pulse == mod &&
+ mimFraCntOpt == iter) {
+
+ cntResponse += 1;
+ }
+ }
+ }
+
+
+ }
+ }// if(mimTra >= numberOfFrames) {
+ // goto MAXCOUNTS;
+ // }
+ } // End loop over sub-events in event
+ }
+ //MAXCOUNTS:
+ ref.Disconnect();
+ trb_register_write(fpga, 0x9209, 0xaaaaaaaa);/*
+ cout << "Trying out VCASN " << iter << "Response Counter " << cntResponse << "Response Rate " << (cntResponse + 0.)/ maxResponses << endl;*/
+ std::printf("Trying out VCASN: %3d, Response Counter: %3d, Response Rate: %3f\r",iter, cntResponse, (cntResponse + 0.)/ maxResponses);
+ fflush(stdout);
+ if ( (((cntResponse + 0.) / maxResponses) >= (0.5 - 0.5 * precision)) && (((cntResponse + 0.) / maxResponses) <= (0.5 + 0.5 * precision) )) {
+ std::printf("\n");
+ return iter;
+ }
+
+ }
+ std::printf("\n");
+
+ return -1;
+
+}
+
+