Hello!
I’m encountering an unusual issue while working with histograms in ROOT. When I scale my histogram using the command hist->Scale(1.0/maxBinContent);
, the chi-square value seems to change unexpectedly. However, as I understand it, scaling a histogram should not affect the chi-square calculation.
I’ve attached both the original and scaled histograms along with their respective chi-square values for your reference. FIrst one is without scaling and the second one is with scaling…
Could someone please take a look at my code and help me identify where the issue might be? Any guidance on why the chi-square is changing after scaling would be greatly appreciated!
Thank you in advance!
"#include <TFile.h>
#include <TTree.h>
#include <TH1F.h>
#include <TCanvas.h>
#include
#include
Double_t biExponential(Double_t *x, Double_t *par) {
Double_t t = x[0];
Double_t tau_d1 = par[0];
Double_t tau_r1 = par[1];
Double_t rho1 = par[2];
Double_t tau_d2 = par[3];
Double_t tau_r2 = par[4];
Double_t rho2 = par[5];
Double_t norm = par[6];
return norm * (
rho1 * (exp(-t / tau_d1) - exp(-t / tau_r1)) / (tau_d1 - tau_r1) +
rho2 * (exp(-t / tau_d2) - exp(-t / tau_r2)) / (tau_d2 - tau_r2)
);
}
void GLobalTimeVsPhotonCount() {
// Opening root file
TFile *file = TFile::Open("Outputtest.root");
if (!file || file->IsZombie()) {
std::cerr << "Error: Could not open file!" << std::endl;
return;
}
TTree *tree = dynamic_cast<TTree*>(file->Get("Photon_Hit"));
if (!tree) {
std::cerr << "Error: Could not find tree 'Photon_Hit' in file!" << std::endl;
file->Close();
return;
}
// Histogram creation and filling
double globalTime;
tree->SetBranchAddress("GlobalTime_ns", &globalTime);
double xmin = std::numeric_limits<double>::max();
double xmax = std::numeric_limits<double>::lowest();
TH1F *hist = new TH1F("hist", "Global Time Distribution", 100, xmin, xmax);
hist->GetXaxis()->SetTitle("Time [ns]");
hist->GetYaxis()->SetTitle("Number of Counts");
hist->SetLineColor(kBlack);
Long64_t nEntries = tree->GetEntries();
for (Long64_t i = 0; i < nEntries; ++i) {
tree->GetEntry(i);
hist->Fill(globalTime);
}
TCanvas *c1 = new TCanvas("c1", "Global Time vs Photon Count", 800, 600);
gPad->SetGridx();
gPad->SetGridy();
gPad->SetLogy();
Double_t maxBinContent = hist->GetMaximum();
std::cout << "Maximum bin content: " << maxBinContent << std::endl;
hist->Scale(1.0/maxBinContent);
hist->Draw("E");
// Define the bi-exponential function with normalization
TF1 *biExpFit = new TF1("biExpFit", biExponential, xmin, xmax, 7);
biExpFit->SetParNames("tau_d1", "tau_r1", "rho1", "tau_d2", "tau_r2", "rho2", "norm");
// Set initial parameters and limits with more reasonable values
biExpFit->SetParameter(0, 50); // tau_d1
biExpFit->SetParameter(1, 10); // tau_r1
biExpFit->SetParameter(2, 0.5); // rho1
biExpFit->SetParameter(3, 100); // tau_d2
biExpFit->SetParameter(4, 20); // tau_r2
biExpFit->SetParameter(5, 0.5); // rho2
biExpFit->SetParameter(6, 1.0); // norm
hist->Fit(biExpFit, "R");
Double_t chi2 = biExpFit->GetChisquare();
Double_t ndf = biExpFit->GetNDF();
Double_t chi2_ndf = chi2/ndf;
// Save results
c1->SaveAs("time_vs_photon.pdf");
TFile outFile("time_fit_results.root", "RECREATE");
c1->Write();
hist->Write();
biExpFit->Write();
outFile.Close();
}"
time_vs_photon.pdf (17.7 KB)
ROOT Version: 6.32.04
Platform: 22.04.1-Ubuntu
Compiler: C++