My intent is to write a code that takes as input the absorption spectrum of water generated by a hypothetical source that fires the same number of photons for each wavelength and rescales that spectrum by knowing the spectrum of a real xenon lamp (which has several peaks).
What I want to obtain is the absorption spectrum of water that would be obtained in the case where the source was not ideal but was real, specifically a xenon lamp. To do this, I wrote the following code:
{
// Apro il file di input e lo leggo in un TGraphErrors
TGraphErrors gInput("Absorption_Spectrum_water_48mL.txt", "%lg %lg %lg %lg");
// Apro il file di normalizzazione e lo leggo in un TGraphErrors
TGraphErrors gNorm("PX-2_spectra250nm_400nm.txt", "%lg %lg %lg %lg");
// Cerco il valore di normalizzazione nella seconda colonna del TGraphErrors di normalizzazione
double normValue = 0;
for (int i = 0; i < gNorm.GetN(); i++) {
double xNorm, yNorm;
gNorm.GetPoint(i, xNorm, yNorm);
double xInput, yInput;
gInput.GetPoint(i, xInput, yInput);
if (xInput == xNorm) {
normValue = yNorm;
break;
}
}
// Se il valore di normalizzazione non è stato trovato, esco dalla macro
if (normValue == 0) {
std::cout << "Valore di normalizzazione non trovato." << std::endl;
return;
}
// Creo un nuovo TGraphErrors con i dati normalizzati
TGraphErrors gOutput(gInput.GetN());
// Normalizzo i dati del TGraphErrors di input e li copio nel TGraphErrors di output
for (int i = 0; i < gInput.GetN(); i++) {
double x, y;
gInput.GetPoint(i, x, y);
gOutput.SetPoint(i, x, (y/1.e7)*normValue);
}
// Salvo i dati normalizzati in un file di output
std::ofstream output("Absorption_Spectrum_water_48mL_norm_XenonLamp.txt");
for (int i = 0; i < gOutput.GetN(); i++) {
double x, y;
gOutput.GetPoint(i, x, y);
output << x << "\t" << y << std::endl;
}
}
Warning: your “Absorption_Spectrum_water_48mL” has 2 to 6 points for every single wavelength in the range 250 to 268 nm. You need to “clean” it.
{
TGraph gInput("Absorption_Spectrum_water_48mL.txt", "%lg %lg");
gInput.SetTitle("Absorption Spectrum water 48 mL (hypothetical source);wavelength [ nm ];PX2 irradiance spectra [ #muW / cm^{2} / nm ]");
gInput.Sort(); // just a precaution
TGraphErrors gNorm("PX-2_spectra250nm_400nm.txt", "%lg %lg %lg %lg");
gNorm.SetTitle("xenon lamp source;wavelength [ nm ];number of photons");
TGraphErrors gOutput(gNorm);
for (int i = 0; i < gOutput.GetN(); i++) {
Double_t normValue = 1.e-7 * gInput.Eval(gOutput.GetX()[i]);
gOutput.GetY()[i] *= normValue;
gOutput.GetEY()[i] *= normValue;
// gOutput.GetEX()[i] = 0.;
}
delete gOutput.GetHistogram(); gOutput.SetHistogram(0); // delete the old one
gOutput.SetTitle("Absorption Spectrum water 48 mL (xenon lamp source);wavelength [ nm ];PX2 irradiance spectra [ #muW / cm^{2} / nm ]");
TCanvas *c = new TCanvas("c", "c", 500, 750);
c->Divide(1, 3);
c->cd(1); gInput.Draw("APL");
c->cd(2); gNorm.Draw("APL");
c->cd(3); gOutput.Draw("APL");
c->cd(0);
}