Commit 6b2a356f authored by Keerthi Nakkalil's avatar Keerthi Nakkalil
Browse files

The fit parameters are calculated and stored in a txt file for all the 6gk pixels

parent 3d0253a8
void TreeEnergyWrite(){
int Ecol1;
double Ecol2,Ecol3,Ecol4;
string line_E;
TFile* Efile = new TFile("Energy_table.root","recreate");
TTree* ETree = new TTree("ETree","Energy tree");
ETree->Branch("Ecol1",&Ecol1,"Ecol1/I"); // ECol1 = Energy values = {200,300.....14000}
ETree->Branch("Ecol4",&Ecol4,"Ecol4/D"); // Voltage values
std::ifstream Energy_file("Energy_table.dat");
if(Energy_file.is_open()){
while(getline(Energy_file,line_E)){
istringstream ssE(line_E);
ssE >> Ecol1 >> Ecol2 >> Ecol3 >> Ecol4;
ETree->Fill();
}
}
ETree->Write();
Efile->Close();
}
void TreeTPWrite(){
int energy, col, row, pc;
double voltage,tot, tot_var, toa, toa_var, time_ref,toa_cut,mean,RMS;
string line_TP;
std::ofstream skippixels;
skippixels.open("skip_pixels.txt",std::ofstream::app);
TFile* Efile = new TFile("Energy_table.root");
TTree* ETree = (TTree*)Efile->Get("ETree");
ETree->SetBranchAddress("Ecol1",&energy);
ETree->SetBranchAddress("Ecol4",&voltage);
TFile* TPfile = new TFile("TP.root","update");
TTree* TPtree = new TTree("TPtree"," TP tree");
TPtree->Branch("col",&col,"col/I");
TPtree->Branch("row",&row,"row/I");
TPtree->Branch("pc",&pc,"pc/I");
TPtree->Branch("tot",&tot,"tot/D");
TPtree->Branch("tot_var",&tot_var,"tot_var/D");
TPtree->Branch("toa",&toa,"toa/D");
TPtree->Branch("toa_var",&toa_var,"toa_var/D");
TPtree->Branch("time_ref",&time_ref,"time_ref/D");
auto voltageBranch = TPtree->Branch("voltage",&voltage,"voltage/D");// make a new branch and connect it to the same variable as the branch address of the energy tree
auto Branch_toacut = TPtree->Branch("toa_cut",&toa_cut,"toa_cut/D");
for(int i = 0; i < ETree->GetEntries(); i++){
ETree->GetEntry(i);
// std:: cout << energy << " " << voltage << std::endl;
std::string TPfilename = "tp_dat_" + std::to_string(energy) + ".dat";
//std:: cout << " The TPfilenames are : " << TPfilename.c_str() <<std::endl;
std::ifstream TPdatafile(TPfilename.c_str());
voltageBranch->Fill();
if(TPdatafile.is_open()){
std::cout << " The data file " << TPfilename.c_str() << " is opened ..." <<std::endl;
// reading through individial tp_dat_ files
while(getline(TPdatafile,line_TP)){
if(line_TP.rfind("#", 0) == 0) {
continue;
}
istringstream ssTP(line_TP);
ssTP >> col >> row >> pc >> tot >> tot_var >> toa >> toa_var >> time_ref;
TPtree->Fill();
} //While loop getline
}//if statement TPdatafile open
TPtree->Draw("toa",Form("voltage==(%f)",voltage),"goff");
mean = TMath::Mean(TPtree->GetSelectedRows(), TPtree->GetV1());
RMS = TMath::RMS(TPtree->GetSelectedRows(), TPtree->GetV1());
// std::cout <<" mean " << mean << " RMS " << RMS <<std::endl;
/* for (int isigma=0; isigma < 15; isigma++){
std::cout << " cut at " << isigma << " times the RMS leaves : "
<< 100.0*TPtree->GetEntries(Form("toa<(%f) && voltage==(%f)",mean+isigma*RMS,voltage))/TPtree->GetEntries(Form("voltage==(%f)",voltage))
<< " percentage of entries " << std::endl;
}*/
toa_cut = mean + 6*RMS;
Branch_toacut->Fill();
}//For loop GetEntries
//Save only the new version of the tree
TPtree->Write("",TObject::kOverwrite);
TPfile->Close();
}
void timewalkfit(){
int col, row, pc ;
double tot, tot_var, toa, toa_var,voltage;
TFile* TPfile = new TFile("TP.root");
TTree* TPtree = (TTree*)TPfile->Get("TPtree");
TPtree->SetBranchAddress("col",&col);
TPtree->SetBranchAddress("row",&row);
TPtree->SetBranchAddress("pc",&pc);
TPtree->SetBranchAddress("tot",&tot);
TPtree->SetBranchAddress("tot_var",&tot_var);
TPtree->SetBranchAddress("toa",&toa);
TPtree->SetBranchAddress("toa_var",&toa_var);
TPtree->SetBranchAddress("voltage",&voltage);
for(int irow = 0; irow < 256; irow++){
for (int icol = 0; icol <256; icol++){
std::cout << icol << " " << irow << " " << std::endl;
auto N = TPtree->Draw("voltage:tot:sqrt(tot_var)",Form("col==(%i) && row==(%i) && tot>0", icol, irow),"goff");
// cout << N <<std::endl;
std::ofstream fitpars;
if(N==0)
{
std::cout << "Dead pixel or no enough points for fitting. Skipping pixel " << icol << "_" << irow << std::endl;
std::ofstream skippixels;
skippixels.open("tot_skip_pixels_1190.txt",std::ofstream::app);
skippixels << "Skipping the pixel " << icol << "_" << irow <<std::endl;
fitpars.open("tot_fit_parameters_1190.txt",std::ofstream::app);
fitpars << icol << setw(10) << irow << setw(10) << "0.0"
<< "0.0" << setw(10)
<< "0.0" << setw(10) << "0.0" << setw(10)<< "0.0" << std::endl;
}
else{
TGraphErrors *graph = new TGraphErrors(N,TPtree->GetV1(),TPtree->GetV2(),0,TPtree->GetV3());
auto fitmin = TMath::MinElement(graph->GetN(),graph->GetX());
// std::cout << "fitmin" << fitmin << std::endl;
auto fitmax = TMath::MaxElement(graph->GetN(),graph->GetX());
// std::cout << "fitmax " << fitmax << std::endl;
auto start = 0.5*fitmax;
TF1* f1 = new TF1("f1", "([0]*x + [1])",start,fitmax);
f1->SetParNames("gain","offset");
graph->Fit(f1,"R");
auto a = f1->GetParameter(0);
auto b = f1->GetParameter(1);
// cout << " a : " << a << std::endl;
// cout << " b : " << b << std::endl;
TF1 *fitfuncTOT = new TF1("fitfuncTOT","([0]*x +[1] - ([3]/(x-[2])))",fitmin,fitmax);
fitfuncTOT->SetParNames("gain","offset","asymtote","curvature");
int iymin=TMath::LocMin(graph->GetN(),graph->GetY());
// cout << "iymin " << iymin << std::endl;
auto t = 0.5*graph->GetPointX(iymin);
//cout << "t :" << " " << t << std::endl;;
auto testx = 0.5*fitmax;
auto ctot_y = graph->Eval(testx);
auto c = (a*testx + b - ctot_y)*(testx - t);
// cout << "c :" << " " << c <<std::endl;
// cout << a << " " << b << " " << t << " " << c << std::endl;
fitfuncTOT->SetParLimits(3,0,t*10); // so as to have no negetive values
fitfuncTOT->SetParameters(a,b,t,c);
graph->Fit(fitfuncTOT,"R");
gStyle->SetOptFit(1111);
fitfuncTOT->SetLineColor(kRed);
/*graph->GetXaxis()->SetTitle("Volatge[mV]");
graph->GetYaxis()->SetTitle ("Tot[LSB]");
graph->SetTitle(" ");
graph->SetMarkerStyle(7);
graph->Draw("AP");*/
// std::ofstream fitpars;
fitpars.open("tot_fit_parameters_1190.txt",std::ofstream::app);
fitpars << icol << setw(10) << irow << setw(10) << fitfuncTOT->GetParameter(0)
<< setw(10) << fitfuncTOT->GetParameter(1) << setw(10)
<< fitfuncTOT->GetParameter(3) << setw(10) << fitfuncTOT->GetParameter(2) << setw(10) << (fitfuncTOT->GetChisquare()/fitfuncTOT->GetNDF()) << std::endl; // chi2/ndf included for the time-being!
}
}
}
TPfile->Close();
}
void tot_calib(){
TreeEnergyWrite();
TreeTPWrite();
timewalkfit();
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment