17 #include "Math/WrappedTF1.h"
24 #include <TGraphAsymmErrors.h>
25 #include <TGraphErrors.h>
30 #include <TApplication.h>
78 const double SpectrumBins[15][15] = {{18.4, 18.5, 18.6, 18.7, 18.8, 18.9, 19.0, 19.1, 19.2, 19.3, 19.4, 19.5, 19.7, 19.9, 20.2},
79 {17.5, 17.6, 17.7, 17.8, 17.9, 18.0, 18.1, 18.2, 18.3, 18.4, 18.5, 18.7, 18.9, 19.2, 19.7}};
110 void FitFCN(Int_t&
npar, Double_t* gin, Double_t& f, Double_t* par, Int_t iflag);
117 gROOT->SetStyle(
"Plain");
118 gStyle->SetCanvasDefH(700);
119 gStyle->SetCanvasDefW(900);
120 gStyle->SetCanvasBorderMode(0);
121 gStyle->SetCanvasColor(0);
123 gStyle->SetPadTopMargin(0.05);
124 gStyle->SetPadBottomMargin(0.15);
125 gStyle->SetPadLeftMargin(0.18);
126 gStyle->SetPadRightMargin(0.05);
127 gStyle->SetPadColor(0);
128 gStyle->SetPadBorderMode(0);
130 gStyle->SetFrameBorderMode(0);
131 gStyle->SetTitleFillColor(0);
132 gStyle->SetTitleBorderSize(1);
133 gStyle->SetStatColor(0);
134 gStyle->SetStatBorderSize(1);
136 gStyle->SetOptTitle(0);
137 gStyle->SetOptStat(0);
139 gStyle->SetPalette(1, 0);
140 gStyle->SetTitleBorderSize(0);
141 gStyle->SetTitleX(0.1f);
142 gStyle->SetTitleW(0.8f);
144 gStyle->SetTitleFont(42,
"X");
145 gStyle->SetTitleFont(42,
"Y");
146 gStyle->SetLabelFont(42,
"X");
147 gStyle->SetLabelFont(42,
"Y");
149 gStyle->SetTitleAlign(21);
152 gStyle->SetMarkerStyle(8);
153 gStyle->SetMarkerSize(1.4);
155 gStyle->SetTitleXOffset(1.);
156 gStyle->SetTitleYOffset(1.4);
157 gStyle->SetTitleSize(0.06,
"X");
158 gStyle->SetTitleSize(0.06,
"Y");
160 gStyle->SetTextSize(20);
161 gStyle->SetStripDecimals(0);
163 gStyle->SetErrorX(0.);
166 double arrayExposure[2];
167 ifstream fOpenExposure(
"exposure");
169 fOpenExposure >> arrayExposure[
iarray];
170 fOpenExposure.close();
180 TH1D* thEnergySpectrum =
new TH1D(Form(
"thEnergySpectrum%d", iarray),
"", hnbins,
SpectrumMin[iarray],
SpectrumMax[iarray]);
182 TH1D* thEnergySpectrum_rebinned;
185 std::cerr <<
"Re-binning active for " << Form(
"%s", iarray ?
"Infill" :
"Regular") <<
" array data...\n";
186 thEnergySpectrum_rebinned =
new TH1D(Form(
"thEnergySpectrum_rebinned%d", iarray),
"",
BinNum[iarray],
SpectrumBins[iarray]);
197 double lgE, S1000, dS1000, theta, phi, dec, ra;
203 evtFile.getline(header, 1024);
205 std::cerr <<
"Reading " << Form(
"%s", iarray ?
"Infill" :
"Regular") <<
" array data...\n";
207 while (!evtFile.eof()) {
208 evtFile >> AugerId >> EventId >> lgE >> S1000 >> dS1000 >> nst >> theta >> phi >> dec >> ra;
211 lgE = log10(lgE) + 18;
214 thEnergySpectrum->Fill(lgE);
216 thEnergySpectrum_rebinned->Fill(lgE);
224 int Nbins = thEnergySpectrum->GetNbinsX();
227 TGraphAsymmErrors* tgEnergySpectrum =
new TGraphAsymmErrors();
228 for (
int i = 1; i <= Nbins; ++i) {
230 double binW = thEnergySpectrum->GetBinWidth(i);
231 double we =
pow(10, thEnergySpectrum->GetBinCenter(i) + binW / 2) -
pow(10, thEnergySpectrum->GetBinCenter(i) - binW / 2);
233 double lgE = thEnergySpectrum->GetBinCenter(i);
234 double content = thEnergySpectrum->GetBinContent(i) / factor;
236 tgEnergySpectrum->SetPoint(i - 1, lgE, content);
241 tgEnergySpectrum->SetPointError(i - 1, 0., 0., plow / factor, pup / factor);
245 raw_nevents[i - 1] = thEnergySpectrum->GetBinContent(i);
256 int Nbins_rebinned = thEnergySpectrum_rebinned->GetNbinsX();
259 for (
int i = 1; i <= Nbins_rebinned; ++i) {
261 double binW_rebinned = thEnergySpectrum_rebinned->GetBinWidth(i);
262 double we_rebinned =
pow(10, thEnergySpectrum_rebinned->GetBinCenter(i) + binW_rebinned / 2) -
pow(10, thEnergySpectrum_rebinned->GetBinCenter(i) - binW_rebinned / 2);
263 double factor_rebinned =
exposure * we_rebinned;
264 double lgE_rebinned = thEnergySpectrum_rebinned->GetBinCenter(i);
265 double content_rebinned = thEnergySpectrum_rebinned->GetBinContent(i) / factor_rebinned;
267 double plow_rebinned, pup_rebinned;
268 poisson_uncertainty(thEnergySpectrum_rebinned->GetBinContent(i), plow_rebinned, pup_rebinned);
290 FitFunction->SetParameter(0, -19.);
291 FitFunction->SetParameter(1, 18.7);
292 FitFunction->SetParameter(2, 3.3);
293 FitFunction->SetParameter(3, 2.8);
294 FitFunction->SetParameter(4, 19.8);
295 FitFunction->SetParameter(5, 0.15);
299 FitFunction->SetParameter(0, -22.5);
300 FitFunction->SetParameter(1, 18.7);
301 FitFunction->SetParameter(2, 19.15);
302 FitFunction->SetParameter(3, 19.65);
303 FitFunction->SetParameter(4, 3.3);
304 FitFunction->SetParameter(5, 2.6);
305 FitFunction->SetParameter(6, 3.0);
306 FitFunction->SetParameter(7, 5.0);
311 FitFunction->SetParameter(0, -19);
312 FitFunction->SetParameter(1, 18.7);
313 FitFunction->SetParameter(2, 3.3);
314 FitFunction->SetParameter(3, 2.8);
317 FitFunction->SetLineColor(1);
318 FitFunction->SetLineStyle(9);
319 FitFunction->SetLineWidth(1);
321 tgEnergySpectrum->Fit(FitFunction,
"q");
335 int nbins = round((
IntXmax[iarray] -
IntXmin[iarray]) / thEnergySpectrum->GetBinWidth(1));
341 for (
int i = 1; i <= N; ++i)
346 double resolutionOffset[2] = {0, 0};
353 double start[NParArray[
iarray]];
355 for (
int ipar = 1; ipar < NParArray[
iarray]; ipar++)
362 TVectorD unfoldstat(N);
365 for (
int i = 0; i < N; ++i) {
368 dlgE[i] =
thLgE->GetBinCenter(1) / 2.;
371 TGraph* tgUnfoldStatValue =
new TGraph(N, &
vecLgE[0], &unfoldstat[0]);
373 TCanvas* c1 =
new TCanvas();
377 TGraphAsymmErrors* tgUnfoldStat =
new TGraphAsymmErrors(N, &
vecLgE[0], &
UnfoldCorrectionFactor[0], &dlgE[0], &dlgE[0], &unfoldstat[0], &unfoldstat[0]);
378 tgUnfoldStat->Draw(
"a3");
379 tgUnfoldStat->SetMinimum(0);
380 tgUnfoldStat->SetMaximum(1.2);
381 tgUnfoldStat->SetFillColor(kGray);
385 tgUnfold->SetMarkerStyle(3);
387 tgUnfold->SetFillColor(kBlack);
388 tgUnfold->SetLineWidth(3);
390 tgUnfoldStat->GetYaxis()->CenterTitle(kTRUE);
391 tgUnfoldStat->GetXaxis()->CenterTitle(kTRUE);
393 tgUnfoldStat->GetYaxis()->SetTitle(
"Correction factor: J_{true}/J_{folded}");
394 tgUnfoldStat->GetXaxis()->SetTitle(
"log_{10}(E/eV)");
397 tgUnfoldStat->GetYaxis()->SetRangeUser(0.7, 1.05);
400 TGraphAsymmErrors* tgUnfoldSpectrum =
new TGraphAsymmErrors();
404 ofstream fOut(Form(
"%sSpectrum.dat", iarray ?
"Infill" :
"Regular"),
ios::out);
405 fOut <<
"# lg(E/eV) lgE_hw raw_counts exposure[km2 sr yr] J [km-2 sr-1 yr-1 eV-1] lower_sigma[J]_stat upper_sigma[J]_stat unfolding_correction sigma[unfolding_correction]_stat" << endl;
407 for (
int i = 0; i <
raw_lgEs.GetNoElements(); ++i) {
409 double factorunfold = tgUnfold->Eval(
raw_lgEs[i]);
410 double statunc_factorunfold = tgUnfoldStatValue->Eval(
raw_lgEs[i]);
411 double nmod =
raw_flux[i] * factorunfold;
415 fOut <<
raw_lgEs[i] <<
" " << thEnergySpectrum->GetBinWidth(i) / 2. <<
" " <<
raw_nevents[i] <<
" " <<
exposure <<
" " << nmod <<
" " << lowerror <<
" " << uperror <<
" " << factorunfold <<
" " << statunc_factorunfold << endl;
417 tgUnfoldSpectrum->SetPoint(i,
raw_lgEs[i], nmod);
418 tgUnfoldSpectrum->SetPointError(i, 0., 0., lowerror, uperror);
422 ofstream fOutRebinned(Form(
"%sSpectrum_rebinned.dat", iarray ?
"Infill" :
"Regular"),
ios::out);
423 fOutRebinned <<
"# lg(E/eV) lgE_hw raw_counts exposure[km2 sr yr] J [km-2 sr-1 yr-1 eV-1] lower_sigma[J]_stat upper_sigma[J]_stat unfolding_correction" << endl;
426 double statunc_factorunfold_rebinned = tgUnfoldStatValue->Eval(
raw_lgEs_rebinned[i]);
432 fOutRebinned <<
raw_lgEs_rebinned[i] <<
" " << thEnergySpectrum_rebinned->GetBinWidth(i+1) / 2. <<
" " <<
raw_nevents_rebinned[i] <<
" " <<
exposure <<
" " << nmod_rebinned <<
" " << lowerror_rebinned <<
" " << uperror_rebinned <<
" " << factorunfold_rebinned <<
" " << statunc_factorunfold_rebinned << endl;
435 fOutRebinned.close();
440 TCanvas* c2 =
new TCanvas();
442 tgEnergySpectrum->SetMarkerStyle(22);
443 tgEnergySpectrum->SetMarkerSize(2.2);
444 tgEnergySpectrum->SetMarkerColor(1);
445 tgEnergySpectrum->GetYaxis()->CenterTitle(kTRUE);
446 tgEnergySpectrum->GetXaxis()->CenterTitle(kTRUE);
447 tgEnergySpectrum->GetYaxis()->SetTitle(
"J(E) [eV^{-1} km^{-2} sr^{-1} yr^{-1}]");
448 tgEnergySpectrum->GetXaxis()->SetTitle(
"log_{10}(E/eV)");
449 tgEnergySpectrum->Draw(
"ap");
451 tgUnfoldSpectrum->SetMarkerStyle(20);
452 tgUnfoldSpectrum->SetMarkerSize(1.8);
453 tgUnfoldSpectrum->SetMarkerColor(2);
454 tgUnfoldSpectrum->SetLineColor(2);
455 tgUnfoldSpectrum->Draw(
"p");
457 double hymin = FitFunction->Eval(
SpectrumMax[iarray]) / 10.;
458 double hymax = FitFunction->Eval(
SpectrumMin[iarray]) * 10.;
461 tgEnergySpectrum->GetYaxis()->SetRangeUser(hymin, hymax);
463 FitFunction->SetLineColor(2);
464 FitFunction->SetLineStyle(9);
465 FitFunction->SetLineWidth(1);
466 tgUnfoldSpectrum->Fit(FitFunction,
"q");
468 TLegend* leg =
new TLegend(0.2, 0.3, 0.5, 0.45);
469 leg->SetFillColor(0);
470 leg->SetLineColor(0);
471 leg->SetBorderSize(1);
472 leg->SetTextFont(42);
473 leg->SetTextSize(0.04);
474 leg->AddEntry(tgEnergySpectrum,
"measured spectrum",
"p");
475 leg->AddEntry(tgUnfoldSpectrum,
"unfolded spectrum",
"p");
480 TImage* img1 = TImage::Create();
482 img1->WriteImage(Form(
"%sCorrectionFactor.png", iarray ?
"Infill" :
"Regular"));
484 TImage* img2 = TImage::Create();
486 img2->WriteImage(Form(
"%sSpectrum.png", iarray ?
"Infill" :
"Regular"));
507 gMinuit.SetPrintLevel(-1);
510 gMinuit.mnexcm(
"SET ERR", arglist, 1, ierflg);
514 gMinuit.mnparm(0,
"Norm", start[0], step, 0, 0, ierflg);
515 gMinuit.mnparm(1,
"lgEa", start[1], step, 0, 0, ierflg);
521 gMinuit.mnparm(2,
"g1", start[2], step, 0, 0, ierflg);
522 gMinuit.mnparm(3,
"g2", start[3], step, 0, 0, ierflg);
523 gMinuit.mnparm(4,
"lgEs", start[4], step, 0, 0, ierflg);
524 gMinuit.mnparm(5,
"Dgamma", start[5], step, 0, 0, ierflg);
528 gMinuit.mnparm(2,
"lgEx", start[2], step, 0, 0, ierflg);
529 gMinuit.mnparm(3,
"lgEs", start[3], step, 0, 0, ierflg);
530 gMinuit.mnparm(4,
"g1", start[4], step, 0, 0, ierflg);
531 gMinuit.mnparm(5,
"g2", start[5], step, 0, 0, ierflg);
532 gMinuit.mnparm(6,
"g3", start[6], step, 0, 0, ierflg);
533 gMinuit.mnparm(7,
"g4", start[7], step, 0, 0, ierflg);
539 gMinuit.mnparm(2,
"g1", start[2], step, 0, 0, ierflg);
540 gMinuit.mnparm(3,
"g2", start[3], step, 0, 0, ierflg);
548 gMinuit.mnexcm(
"MINIMIZE", arglist, 2, ierflg);
551 cout <<
"\t Spectrum fit failed during Minuit." << endl;
554 double fitParameters[N];
555 double fitParErrors[N];
556 for (
int i = 0; i < N; i++)
557 gMinuit.GetParameter(i, fitParameters[i], fitParErrors[i]);
561 for (
int i = 0; i < N; i++) {
566 int ncol =
vecLgE.GetNoElements();
567 TVectorD CorrFactor(ncol);
570 if (computeCovStat) {
573 gMinuit.mnemat(&cov[0][0], N);
581 void FitFCN(Int_t&
npar, Double_t* gin, Double_t& f, Double_t* par, Int_t iflag)
590 int ncol =
vecLgE.GetNoElements();
593 for (
int i = 0; i < ncol; ++i)
594 mwraws0[i] = mwraws0[i] *
pow(10.,
vecLgE[i]) * log(10);
596 TVectorD mwraws =
kmatrix * mwraws0;
597 mwraws *=
thLgE->GetBinWidth(1);
603 for (
int i = 0; i < ncol; ++i) {
615 mws[bin - 1] += mwraws[i] *
thLgE->GetBinWidth(1);
620 for (
int i = 0; i < N; ++i)
630 cout << setprecision(3) << endl;
640 cout <<
"\t Resulting parameters fitting the regular array flux model (old model): \n" << endl;
641 cout <<
"\t Flux normalization: " << J0 <<
" +- " << errorJ0 << endl;
642 cout <<
"\t Eankle: " <<
pow(10.,
parsFit[1]) <<
" +- " <<
pow(10.,
parsFit[1])*log(10)*parsErrorsFit[1] <<
" eV " << endl;
643 cout <<
"\t Esuppression: " <<
pow(10.,
parsFit[4]) <<
" +- " <<
pow(10.,
parsFit[4])*log(10)*parsErrorsFit[4] <<
" eV " << endl;
644 cout <<
"\t gamma1 " <<
parsFit[2] <<
" +- " << parsErrorsFit[2] << endl;
645 cout <<
"\t gamma2 " <<
parsFit[3] <<
" +- " << parsErrorsFit[3] << endl;
646 cout <<
"\t Delta_gamma " <<
parsFit[5] <<
" +- " << parsErrorsFit[5] << endl;
647 cout <<
"\n" << endl;
652 cout <<
"\t Resulting parameters fitting the ICRC 2017 folding model: \n" << endl;
653 cout <<
"\t Flux normalization: " << J0 <<
" +- " << errorJ0 << endl;
654 cout <<
"\t Eankle: " <<
pow(10.,
parsFit[1]) <<
" +- " <<
pow(10.,
parsFit[1])*log(10)*parsErrorsFit[1] <<
" eV " << endl;
655 cout <<
"\t Ex: " <<
pow(10.,
parsFit[2]) <<
" +- " <<
pow(10.,
parsFit[2])*log(10)*parsErrorsFit[2] <<
" eV " << endl;
656 cout <<
"\t Esuppression: " <<
pow(10.,
parsFit[3]) <<
" +- " <<
pow(10.,
parsFit[3])*log(10)*parsErrorsFit[3] <<
" eV " << endl;
657 cout <<
"\t gamma1 " <<
parsFit[4] <<
" +- " << parsErrorsFit[4] << endl;
658 cout <<
"\t gamma2 " <<
parsFit[5] <<
" +- " << parsErrorsFit[5] << endl;
659 cout <<
"\t gamma3 " <<
parsFit[6] <<
" +- " << parsErrorsFit[6] << endl;
660 cout <<
"\t gamma4 " <<
parsFit[7] <<
" +- " << parsErrorsFit[7] << endl;
661 cout <<
"\n" << endl;
668 cout <<
"\t Resulting parameters fitting the Infill array flux model: \n" << endl;
669 cout <<
"\t Flux normalization: " << J0 <<
" +- " << errorJ0 << endl;
670 cout <<
"\t Eankle: " <<
pow(10.,
parsFit[1]) <<
" +- " <<
pow(10.,
parsFit[1])*log(10)*parsErrorsFit[1] <<
" eV " << endl;
671 cout <<
"\t gamma1 " <<
parsFit[2] <<
" +- " << parsErrorsFit[2] << endl;
672 cout <<
"\t gamma2 " <<
parsFit[3] <<
" +- " << parsErrorsFit[3] << endl;
673 cout <<
"\n" << endl;
const char * RegularDataFileName
TVectorD raw_staterrup_rebinned
double FluxModel(const modeltype mtype, double *x, double *par)
const bool HeraldAnalysis
const char * InfillDataFileName
const double SpectrumBinSize[2]
TVectorD GetSDCalPars(bool infill=false)
void poisson_uncertainty(double x, double &plow, double &pup)
vector< t2list > out
output of the algorithm: a list of clusters
double Likelihood(double *pars)
TVectorD raw_staterrlow_rebinned
const double SpectrumBins[15][15]
double pow(const double x, const unsigned int i)
const modeltype modelfunctions[2]
TMatrixD UnfoldCorrectionFactorCoV
int main(int argc, char *argv[])
double fParFitErrors[Npar]
TVectorD MinuitMinimization(double *start, bool computeCovStat)
TVectorD raw_flux_rebinned
TVectorD UnfoldCorrectionFactorMax
const double SpectrumMin[2]
TMatrixD propagate_covariance(TVectorD(*f)(TVectorD, const modeltype), TVectorD x, TMatrixD xcov, const modeltype mtype)
const int IntegrationSteps
TVectorD UnfoldCorrectionFactorMin
double(*)(double *, double *) FluxModels(const modeltype mtype)
TMatrixD kResolutionMatrix(TVectorD pCal, TVectorD xos, double *resolutionOffset, const bool useResFromData, const bool infill)
void FitFCN(Int_t &npar, Double_t *gin, Double_t &f, Double_t *par, Int_t iflag)
const bool ResolutionFromData
TVectorD raw_lgEs_rebinned
TVectorD UnfoldCorrectionFactor
TVectorD raw_nevents_rebinned
const double SpectrumMax[2]
TVectorD GetCorrectionFactor(const TVectorD x, const TMatrixD m, double *par, const modeltype mtype)