FRIB_e21062/compareTH2D.C

144 lines
3.9 KiB
C++
Raw Normal View History

2022-06-22 19:33:20 -04:00
#include "armory/AnalysisLibrary.h"
double xMin1, xMax1;
TGraph * fitGraph1 = new TGraph();
Double_t fitFunc1(Double_t *x, Double_t *par) {
double pos = par[1]*x[0] + par[0];
return (xMax1 > pos && pos > xMin1 ? par[2] * fitGraph1->Eval(pos): 0);
}
TF1 * FitHist1(TH1F * hist1, TH1F * hist2, double a0, double a1, double scale){
if( hist1->GetNbinsX() != hist2->GetNbinsX() ) return NULL;
xMin1 = hist1->GetXaxis()->GetXmin();
xMax1 = hist2->GetXaxis()->GetXmax();
int nBin = hist1->GetNbinsX();
for( int i = 0; i < nBin; i++){
fitGraph1->AddPoint(hist1->GetBinCenter(i), hist1->GetBinContent(i));
}
double par[3] = {a0, a1, scale};
TF1 * fit = new TF1("fit", fitFunc1, xMin1, xMax1, 3);
fit->SetParameters(par);
fit->SetLineWidth(2);
fit->SetLineColor(4);
fit->SetNpx(1000);
hist2->Fit("fit", "Rq");
return fit;
}
double xMin2, xMax2;
TGraph * fitGraph2 = new TGraph();
Double_t fitFunc2(Double_t *x, Double_t *par) {
double pos = par[1]*x[0] + par[0];
//return (xMax2 > pos && pos > xMin2 ? par[2] * fitGraph2->Eval(pos) * exp(pos/1000./par[3]): 0);
return (xMax2 > pos && pos > xMin2 ? par[2] * fitGraph2->Eval(pos) : 0);
//if( x[0] < 2200 ){
// return (xMax2 > pos && pos > xMin2 ? par[2] * fitGraph2->Eval(pos): 0);
//}else{
// return (xMax2 > pos && pos > xMin2 ? par[3] * fitGraph2->Eval(pos): 0);
//}
}
TF1 * FitHist2(TH1F * hist1, TH1F * hist2, double a0, double a1, double scale, double scale2){
if( hist1->GetNbinsX() != hist2->GetNbinsX() ) return NULL;
xMin2 = hist1->GetXaxis()->GetXmin();
xMax2 = hist2->GetXaxis()->GetXmax();
int nBin = hist1->GetNbinsX();
for( int i = 0; i < nBin; i++){
fitGraph2->AddPoint(hist1->GetBinCenter(i), hist1->GetBinContent(i));
}
double par[4] = {a0, a1, scale, scale2};
TF1 * fit = new TF1("fit", fitFunc2, xMin2, xMax2, 4);
fit->SetParameters(par);
fit->SetLineWidth(2);
fit->SetLineColor(4);
fit->SetNpx(1000);
hist2->Fit("fit", "Rq");
return fit;
}
void compareTH2D(int run1, int run2){
TFile * f1 = new TFile(Form("PID_%d.root", run1));
TH1F * hTOF1 = (TH1F*) f1->Get("hTOF");
TH1F * hdE1 = (TH1F*) f1->Get("hdE");
TH2F * hPID1 = (TH2F*) f1->Get("hPID0");
TFile * f2 = new TFile(Form("PID_%d.root", run2));
TH1F * hTOF2 = (TH1F*) f2->Get("hTOF");
TH1F * hdE2 = (TH1F*) f2->Get("hdE");
TH2F * hPID2 = (TH2F*) f2->Get("hPID0");
TCanvas * cc = new TCanvas("cc", Form("cc %d, %d", run1, run2), 3000, 1000);
cc->Divide(3,1);
//================= TOF
cc->cd(1);
hTOF1->Draw("");
hTOF2->SetLineColor(2);
hTOF2->Draw("same");
TF1 * fitTOF = FitHist1(hTOF1, hTOF2, 6, 1.0, 0.01);
fitTOF->Draw("same");
//TPad * chaha = (TPad*) (cc->cd(1))->Clone();
//cc->cd();
//chaha->Draw();
const Double_t* paraE_TOF = fitTOF->GetParErrors();
const Double_t* paraA_TOF = fitTOF->GetParameters();
printf("=================== TOF \n");
printf("a0 : %7.4f +- %7.4f \n", paraA_TOF[0], paraE_TOF[0]);
printf("a1 : %7.4f +- %7.4f \n", paraA_TOF[1], paraE_TOF[1]);
printf(" B : %7.4f +- %7.4f \n", paraA_TOF[2], paraE_TOF[2]);
//============== dE
cc->cd(2); cc->cd(2)->SetLogy();
hdE1->Draw("");
hdE2->SetLineColor(2);
hdE2->Draw("same");
TF1 * fitdE = FitHist2(hdE1, hdE2, -1, 1, 1, -2);
fitdE->Draw("same");
const Double_t* paraE_dE = fitdE->GetParErrors();
const Double_t* paraA_dE = fitdE->GetParameters();
printf("=================== dE \n");
printf("a0 : %7.4f +- %7.4f \n", paraA_dE[0], paraE_dE[0]);
printf("a1 : %7.4f +- %7.4f \n", paraA_dE[1], paraE_dE[1]);
printf(" B : %7.4f +- %7.4f \n", paraA_dE[2], paraE_dE[2]);
printf("B2 : %7.4f +- %7.4f \n", paraA_dE[3], paraE_dE[3]);
printf("################################\n");
printf("%3d %9.6f %9.6f %9.6f %9.6f\n", run2, paraA_TOF[0], paraA_TOF[1], paraA_dE[0], paraA_dE[1]);
printf("\n\n\n");
cc->cd(3);
hPID1->Draw("colz");
hPID2->Draw("same");
}