27 #include "TGraphErrors.h" 36 TF1
likeAbs(
"likeAbs",
"abs(x)",-10,10);
40 void Test1D(Int_t bootStrapIter=400);
63 for (Int_t iDim=0; iDim<
kNDim; iDim++){
64 xxx[iDim]=2.*(gRandom->Rndm()-0.5);
66 Double_t noiseG=gRandom->Gaus();
67 Double_t noiseL=gRandom->Landau();
80 TFile *
f =
TFile::Open(
"AliTMinuiToolkitTestInput.root");
106 TCanvas * canvas =
new TCanvas(
"AliTMinuitToolkit_TestHistogram",
"AliTMinuitToolkit_TestHistogram",1200,1000);
107 canvas->Divide(3,3,0,0);
109 initParam(0, 0) = 20000;
110 initParam(0, 1) = 100;
112 initParam(0, 3) = 100000;
116 initParam(1, 3) = 10;
119 std::map<string,TF1*> fitMap;
121 likeGausCachy->SetParameters(0.9, 1);
123 likePseudoHuber->SetParameter(0,3);
125 for (Int_t iter=0; iter<nIter; iter++) {
126 Double_t slope=1+gRandom->Gaus(0,0.1);
127 if (iter <= 9) canvas->cd(iter+1);
129 TH1F *hist =
new TH1F(
"test",
"AliTMinuitToolkit: Test histogram fit (e^{-x}) with outliers", 50, 0, 4);
130 hist->SetMarkerStyle(25);
131 TRandom *random =
new TRandom();
132 for (Int_t i = 0; i < 20000; i++) {
133 hist->Fill(random->Exp(slope));
135 for (Int_t iOutlier = 0; iOutlier < 10; iOutlier++) {
136 Double_t position = gRandom->Rndm() * 4;
137 Double_t value = gRandom->Rndm() * hist->GetEntries();
138 hist->Fill(position, value);
140 hist->SetMinimum(0);hist->SetMaximum(2500);
142 hist->Draw(
"same LHist");
145 TF1 *finput =
new TF1(
"funFit1",
"[0]*TMath::Exp(-[1]*x)", 0, 6);
146 TF1 *funFit1 =
new TF1(
"funFit1",
"[0]*TMath::Exp(-[1]*x)", 0, 6);
147 TF1 *funFit2 =
new TF1(
"funFit2",
"[0]*TMath::Exp(-[1]*x)", 0, 6);
148 TF1 *funFit3 =
new TF1(
"funFit3",
"[0]*TMath::Exp(-[1]*x)", 0, 6);
152 TF1 *aFormExp =
new TF1(
"formExp",
"[0]*TMath::Exp(-[1]*x)");
156 funFit1->SetLineColor(1);
157 funFit1->SetParameters(fitter->
GetParameters()->GetMatrixArray());
158 funFit1->Draw(
"same");
159 fitMap[
"chi2"]=(TF1*)fitter->
GetFormula()->Clone();
164 funFit2->SetLineColor(2);
165 funFit2->SetParameters(fitter->
GetParameters()->GetMatrixArray());
166 funFit2->Draw(
"same");
172 funFit3->SetLineColor(4);
173 funFit3->SetParameters(fitter->
GetParameters()->GetMatrixArray());
174 funFit3->Draw(
"same");
175 fitMap[
"huber"]=(TF1*)fitter->
GetFormula()->Clone();
181 fitMap[
"pseudoHuber"]=(TF1*)fitter->
GetFormula()->Clone();
183 fitMap[
"misacH(10,50)"]=(TF1*)fitter->
GetFormula()->Clone();
185 fitMap[
"misacH(10,100)"]=(TF1*)fitter->
GetFormula()->Clone();
187 fitMap[
"bootstrapH50"]=(TF1*)fitter->
GetFormula()->Clone();
191 fitMap[
"misacGC(10,50)"]=(TF1*)fitter->
GetFormula()->Clone();
193 fitMap[
"misacGC(10,100)"]=(TF1*)fitter->
GetFormula()->Clone();
195 fitMap[
"bootstrapGC50"]=(TF1*)fitter->
GetFormula()->Clone();
199 TLegend *legend =
new TLegend(0.4, 0.7, 0.89, 0.89,
"Test AliTMinuitToolkit - Exponential fit with outliers");
200 legend->SetBorderSize(0);
201 legend->AddEntry(hist,
"Histogram");
202 legend->AddEntry(funFit1,
"Default chi2 minimization");
203 legend->AddEntry(funFit2,
"User defined likelihood (0.95*gaus+0.05*cachy)");
204 legend->AddEntry(funFit3,
"Huber likelihood");
207 (*pcstream)<<
"test"<<
210 "chi2.="<<fitMap[
"chi2"]<<
211 "huber.="<<fitMap[
"huber"]<<
212 "pseudoHuber.="<<fitMap[
"pseudoHuber"]<<
213 "misacH1050.="<<fitMap[
"misacH(10,50)"]<<
214 "misacH10100.="<<fitMap[
"misacH(10,100)"]<<
215 "bootstrapH50.="<<fitMap[
"bootstrapH50"]<<
216 "misacGC1050.="<<fitMap[
"misacGC(10,50)"]<<
217 "misacGC10100.="<<fitMap[
"misacGC(10,100)"]<<
218 "bootstrapGC50.="<<fitMap[
"bootstrapGC50"]<<
222 canvas->SaveAs(
"AliTMinuitToolkit_TestHistogram.png");
236 TVectorD oParam(2); oParam[0]=0; oParam[1]=2;
238 initParam(0,0)=1; initParam(0,1)=1; initParam(0,2)=0; initParam(0,3)=100000;
239 initParam(1,0)=1; initParam(1,1)=1; initParam(1,2)=0; initParam(1,3)=20;
241 TF1 *fitFunctions[5];
242 TH1 *resHistograms[5];
243 TF1 formula1D(
"formula1D",
"[0]+[1]*x[0]",-1,1);
244 inputTree->SetAlias(
"test1D",
"2*x.fElements[0]");
245 inputTree->SetAlias(
"noise",
"(rndm<0.7)?noiseG:4*noiseL");
246 inputTree->SetAlias(
"X0",
"x.fElements[0]");
248 TString selection=
"1";
255 formula1D.SetParameters(oParam.GetMatrixArray());
256 fitFunctions[0]= (TF1*)formula1D.DrawClone(
"same");
263 formula1D.SetParameters(tool1D->
GetParameters()->GetMatrixArray());
264 fitFunctions[1]= (TF1*)formula1D.DrawClone(
"same");
265 tool1D->
Bootstrap(bootStrapIter,
"bootstrapChi2Norm");
272 formula1D.SetParameters(tool1D->
GetParameters()->GetMatrixArray());
273 fitFunctions[2]= (TF1*)formula1D.DrawClone(
"same");
274 tool1D->
Bootstrap(bootStrapIter,
"bootstrapHuberNorm");
280 formula1D.SetParameters(tool1D->
GetParameters()->GetMatrixArray());
281 fitFunctions[3]= (TF1*)formula1D.DrawClone(
"same");
282 tool1D->
Bootstrap(bootStrapIter,
"bootstrapLikeAbs");
289 formula1D.SetParameters(tool1D->
GetParameters()->GetMatrixArray());
290 fitFunctions[4]= (TF1*)formula1D.DrawClone(
"same");
291 tool1D->
Bootstrap(bootStrapIter,
"bootstrapUserGausAndCachy");
299 TCanvas *canvasTest1D =
new TCanvas(
"Test1D",
"Test1D",1400,1000);
300 canvasTest1D->Divide(1,3);
302 latex.SetTextSize(0.055);
306 gr0->SetMaximum(30); gr0->SetMinimum(-30);
308 gr1->SetMaximum(5); gr1->SetMinimum(-5);
311 for (Int_t iFit=0; iFit<5; iFit++){
312 fitFunctions[iFit]->SetLineColor(
kLineColors[iFit]);
313 fitFunctions[iFit]->SetLineStyle(
kLineStyle[iFit]);
314 fitFunctions[iFit]->SetLineWidth(3);
315 fitFunctions[iFit]->Draw(
"same");
317 latex.SetTextSize(0.07);
318 latex.DrawLatexNDC(0.11,0.8,
"Input y=0+2*x+#epsilon ");
319 latex.DrawLatexNDC(0.11,0.7,
" #epsilon=(0.8 Gaus +0.2 Landau)");
320 latex.SetTextSize(0.055);
324 for (Int_t iFit=0; iFit<5; iFit++){
325 fitFunctions[iFit]->SetLineColor(
kLineColors[iFit]);
326 fitFunctions[iFit]->SetLineStyle(
kLineStyle[iFit]);
327 fitFunctions[iFit]->SetLineWidth(3);
328 fitFunctions[iFit]->Draw(
"same");
330 TLegend * legend =
new TLegend(0.11,0.7,0.6,0.89,
"Unbinned1D fit for different cost function (ZOOM)");
331 legend->SetBorderSize(0); legend->SetNColumns(2);
332 legend->AddEntry(gr0,
"Input data+noise",
"pl");
333 legend->AddEntry(fitFunctions[0],
"MC input",
"l");
334 legend->AddEntry(fitFunctions[1],
"Fit: Chi2 cost",
"l");
335 legend->AddEntry(fitFunctions[2],
"Fit: Huber norm",
"l");
336 legend->AddEntry(fitFunctions[3],
"Fit: LogLikeAbs",
"l");
337 legend->AddEntry(fitFunctions[4],
"Fit: Log(80%Gaus+20%Cauchy)",
"l");
342 TVirtualPad *pad =canvasTest1D->cd(3);
344 TFile *fReport =
TFile::Open(
"AliTMinutiTookitTest1D.root");
345 TTree *tBootstrap = (TTree*)fReport->Get(
"bootstrap");
346 TTree *tCrossValidation = (TTree*)fReport->Get(
"crossValidation");
351 tCrossValidation->SetAlias(
"dP0",
"(param0.fElements[0]-param1.fElements[0])");
352 tCrossValidation->SetAlias(
"dP1",
"(param0.fElements[1]-param1.fElements[1])");
353 tCrossValidation->Draw(
"dP0:reportName.GetName()>>hCrosValidDPar0(4,0,4,200,-1,1)",
"",
"colz");
354 h2D=(TH2*)(tCrossValidation->GetHistogram());
358 tCrossValidation->Draw(
"dP1:reportName.GetName()>>hCrosValidDPar1(4,0,4,200,-1,1)",
"",
"colz");
359 h2D=(TH2*)(tCrossValidation->GetHistogram());
363 latex.DrawLatexNDC(0.3,0.85,
"Two fold cross validation. Param0");
365 latex.DrawLatexNDC(0.3,0.85,
"Two fold cross validation. Param1");
366 for (Int_t iFit=0; iFit<4; iFit++){
369 latex.DrawLatexNDC(0.35,0.85-0.05*(iFit+1),TString::Format(
"%s: RMS %.2f ",h2D->GetXaxis()->GetBinLabel(1+iFit), grRMS0->GetY()[iFit]).Data());
372 latex.DrawLatexNDC(0.35,0.85-0.05*(iFit+1),TString::Format(
"%s: RMS %.2f ",h2D->GetXaxis()->GetBinLabel(1+iFit), grRMS1->GetY()[iFit]).Data());
374 canvasTest1D->SaveAs(
"AliTMinuitToolkitTest.Test1D.png");
375 canvasTest1D->SaveAs(
"AliTMinuitToolkitTest.Test1D.pdf");
static void ApplyStyle(const char *styleName)
TFile * Open(const char *filename, Long64_t &nevents)
TTreeSRedirector * pcstream
static void SetDefaults()
SetDefault call RegisterDefaultLatexSymbols(), RegisterDefaultStyle(), RegisterDefaultMarkers();.
class TVectorT< Double_t > TVectorD
TTree * GetTree(Int_t ievent)
class TMatrixT< Double_t > TMatrixD