Rescaling a TGraphErrors

Hi ROOTers,

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;
  }
}

Absorption_Spectrum_water_48mL.txt (2,6 KB)
PX-2_spectra250nm_400nm.txt (2,6 KB)

But i don’t obtain the exepect result, because i didn’t see any xenon peaks. Where is the error in my code?

  • Best Regards.

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);
}
1 Like

Thanks @Wile_E_Coyote. You are right, I uploaded the right Xenon Lamp Spectrum:

PX-2_spectra.txt (71,3 KB)

Following your script, Can I modulate the final spectra with the PDE (and plot it as you did with xenon spectra lamp) of my sensor?

PDE_S13360-50CS.txt (9,7 KB)

{
  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_spectra.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.;
  }
    TGraphErrors gPDE("PDE_S13360-50CS.txt", "%lg %lg %lg %lg");
  gPDE.SetTitle("PDE Hamamatsu S13;wavelength [ nm ]; probability");
  TGraphErrors gOutput(gPDE);
  for (int i = 0; i < gOutput.GetN(); i++) {
    Double_t normValue = gInput.Eval(gOutput.GetX()[i]);
    gOutput.GetY()[i] *= normValue;
    gOutput.GetEY()[i] *= normValue;
  }
  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");
  cd->cd(3); gPDE.Draw("APL"); 
  c->cd(4); gOutput.Draw("APL");
  c->cd(0);
}

Can I also put in the final graph the errors contained in the input file (column 3 and 4)?

I receive the following errors:


16: error: redefinition of 'gOutput'
  TGraphErrors gOutput(gPDE);
               ^
8:16: note: previous definition is here
  TGraphErrors gOutput(gNorm);
               ^
29:3: error: use of undeclared identifier 'cd'
  cd->cd(3); gPDE.Draw("APL"); 
  ^

Thanks for your help.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.