<commit_before>/*
  Unit test for the  AliNDLocalRegression class:

  Tests:
  1.) Check consistency of the error estimates and bias for smaple with gausian noise
  2.) Check persistancy streamer
  3.) Check outlier handling


  gSystem->AddIncludePath("-I$ALICE_ROOT/../src/STAT/");
  .L $ALICE_ROOT/../src/STAT/AliNDLocalRegression.cxx+
  .L $ALICE_ROOT/../src/STAT/test/AliNDLocalRegressionTest.C+  
  AliNDLocalRegressionTest(5000);
 
*/


#include "THn.h"
#include "TCanvas.h"
#include "AliNDLocalRegression.h"

void UnitTestGaussNoise();
void UnitTestStreamer();



TTree * treeIn=0;
AliNDLocalRegression *pfitNDIdeal=0;       // ideal fit without noise
AliNDLocalRegression *pfitNDGaus0=0;       // fit with noisy data  - sample 0
AliNDLocalRegression *pfitNDGaus1=0;       // fit with noisy data  - sample 1
AliNDLocalRegression *pfitNDBreit0=0;       // fit with noisy data  - BreitWigner 0
AliNDLocalRegression *pfitNDBreit1=0;       // fit with noisy data  - BreitWigner 1

THn   *hN=0; 
TFormula *pformula= 0;


void UnitTestGaussNoise(){
  //
  // Unit Test pulls Gauss
  // Compare 2 Regression objects obtained from 2 indpendent training sample
  //
  // Test:
  //   Bias < 10  *error
  //   Pull < 1+ 3*error
  //   
  TCanvas * canvasUnitGausNoise = new TCanvas("canvasUnitGausNoise","canvasUnitGausNoise");
  treeIn->Draw("(AliNDLocalRegression::GetCorrND(3,xyz0,xyz1)-AliNDLocalRegression::GetCorrND(2,xyz0,xyz1))/sqrt(AliNDLocalRegression::GetCorrNDError(3,xyz0,xyz1)**2+AliNDLocalRegression::GetCorrNDError(2,xyz0,xyz1)**2)>>pullsGaus32(200,-20,20)","","");
  Double_t meanPullsGaus01 = treeIn->GetHistogram()->GetMean();
  Double_t meanPullsGaus01Err = treeIn->GetHistogram()->GetMeanError();
  Double_t rmsPullsGaus01 = treeIn->GetHistogram()->GetRMS();
  Double_t rmsPullsGaus01Err = treeIn->GetHistogram()->GetRMSError();
  if (TMath::Abs(meanPullsGaus01)<10*meanPullsGaus01Err) {
    ::Info( "AliNDLocalRegressionTest","mean pull OK %3.3f\t+-%3.3f", meanPullsGaus01, meanPullsGaus01Err);
  }else{
    ::Error( "AliNDLocalRegressionTest","mean pull NOT OK %3.3f\t+-%3.3f", meanPullsGaus01, meanPullsGaus01Err);
  }
  if (rmsPullsGaus01<1+rmsPullsGaus01Err) {
    ::Info( "AliNDLocalRegressionTest"," rms pull OK %3.3f\t+-%3.3f", rmsPullsGaus01, rmsPullsGaus01Err);
  }else{
    ::Error( "AliNDLocalRegressionTest"," rms pull NOT OK %3.3f\t+-%3.3f", rmsPullsGaus01, rmsPullsGaus01Err);
  }
  //
  //
  //
  canvasUnitGausNoise->SaveAs("AliNDLocalRegressionTest.canvasUnitTestGaussNoise.png");
}

void UnitTestBreitWignerNoise(){
  //
  // Unit Test pulls BreitWigner
  // Compare 2 Regression objects obtained from 2 independent training sample
  //
  // Test:
  //   Bias < 10  *error
  //   Pull < 1+ 3*error
  //   
  TCanvas * canvasUnitBreitWigner = new TCanvas("canvasUnitBretWigner","canvasUnitBreitWigner");
  
  treeIn->Draw("(AliNDLocalRegression::GetCorrND(5,xyz0,xyz1)-AliNDLocalRegression::GetCorrND(4,xyz0,xyz1))/sqrt(AliNDLocalRegression::GetCorrNDError(4,xyz0,xyz1)**2+AliNDLocalRegression::GetCorrNDError(5,xyz0,xyz1)**2)>>pullsBreiWigner54(200,-20,20)","","");
  Double_t meanPullsBreitWigner = treeIn->GetHistogram()->GetMean();
  Double_t meanPullsBreitWignerErr = treeIn->GetHistogram()->GetMeanError();
  Double_t rmsPullsBreitWigner = treeIn->GetHistogram()->GetRMS();
  Double_t rmsPullsBreitWignerErr = treeIn->GetHistogram()->GetRMSError();
  if (TMath::Abs(meanPullsBreitWigner)<10*meanPullsBreitWignerErr) {
    ::Info( "AliNDLocalRegressionTest::UnitTestBreitWignerNoise","mean pull OK %3.3f\t+-%3.3f", meanPullsBreitWigner, meanPullsBreitWignerErr);
  }else{
    ::Error( "AliNDLocalRegressionTest::UnitTestBreitWignerNoise","mean pull NOT OK %3.3f\t+-%3.3f", meanPullsBreitWigner, meanPullsBreitWignerErr);
  }
  if (rmsPullsBreitWigner<1+rmsPullsBreitWignerErr) {
    ::Info( "AliNDLocalRegressionTest::UnitTestBreitWignerNoise"," rms pull OK %3.3f\t+-%3.3f", rmsPullsBreitWigner, rmsPullsBreitWignerErr);
  }else{
    ::Error( "AliNDLocalRegressionTest::UnitTestBreitWignerNoise"," rms pull NOT OK %3.3f\t+-%3.3f", rmsPullsBreitWigner, rmsPullsBreitWignerErr);
  }
  canvasUnitBreitWigner->SaveAs("AliNDLocalRegressionTest.canvasUnitBreitWigner.png");
}



void UnitTestStreamer(){
  //
  // Check consistency of streamer
  //
  TCanvas * canvasUnitTestStreamer = new TCanvas;
  TFile * f = TFile::Open("fitNDLocalTestStreamer.root","recreate");
  pfitNDGaus0->Write("pfitNDGaus0");
  f->Close();
  f = TFile::Open("fitNDLocalTestStreamer.root");
  AliNDLocalRegression *streamTest1 = (AliNDLocalRegression *)f->Get("pfitNDGaus0");
  streamTest1->AddVisualCorrection(streamTest1,100);
  Int_t entries = treeIn->Draw("AliNDLocalRegression::GetCorrND(100,xyz0,xyz1)-AliNDLocalRegression::GetCorrND(2,xyz0,xyz1)","");
  Int_t rms = TMath::RMS(entries,treeIn->GetV1());
  if (rms!=0){
    ::Error( "AliNDLocalRegressionTest::UnitTestStreamer","Streamer problem");
  }else{
    ::Info( "AliNDLocalRegressionTest::UnitTestStreamer","Streamer OK");
  }
  canvasUnitTestStreamer->SaveAs(" AliNDLocalRegressionTest.UnitTestStreamer.png");
}



void AliNDLocalRegressionTest(Int_t npoints=10000, Int_t ndim=2, const char *sfromula="cos(7*x[0]/pi)*sin(19*x[1]/pi)", Double_t err=0.01){
  //
  // Local regression test method
  //
  // Int_t npoints=100000; Int_t ndim=2; const char *sfromula="cos(10*x[0])*cos(15*x[1])"; 
  //
  pformula=new TFormula("pformula", sfromula);
  pfitNDIdeal  = new  AliNDLocalRegression;
  pfitNDGaus0  = new  AliNDLocalRegression;
  pfitNDGaus1  = new  AliNDLocalRegression;
  pfitNDBreit0 = new  AliNDLocalRegression;
  pfitNDBreit1 = new  AliNDLocalRegression;
  //
  // 0.) Initialization of variables and THn
  // 
  TTreeSRedirector *pcstreamIn         = new TTreeSRedirector("fitNDLocalTestInput.root","recreate");
  TTreeSRedirector *pcstreamOutIdeal   = new TTreeSRedirector("fitNDLocalTestOutputIdeal.root","recreate");  
  TTreeSRedirector *pcstreamOutGaus0   = new TTreeSRedirector("fitNDLocalTestOutputGaus0.root","recreate");  
  TTreeSRedirector *pcstreamOutGaus1   = new TTreeSRedirector("fitNDLocalTestOutputGaus1.root","recreate");  
  TTreeSRedirector *pcstreamOutBreit0  = new TTreeSRedirector("fitNDLocalTestOutputBreit0.root","recreate");  
  TTreeSRedirector *pcstreamOutBreit1  = new TTreeSRedirector("fitNDLocalTestOutputBreit1.root","recreate");  
  
  Double_t *xyz     = new Double_t[ndim];
  Double_t *sxyz    = new Double_t[ndim];
  Int_t    *nbins   = new Int_t[ndim];<commit_msg>ATO-185, ATO-164 - Unit test. Nicer drawing (Johannes)<commit_after>/*
  Unit test for the  AliNDLocalRegression class:

  Tests:
  1.) Check consistency of the error estimates and bias for smaple with gausian noise
  2.) Check persistancy streamer
  3.) Check outlier handling


  gSystem->AddIncludePath("-I$ALICE_ROOT/../src/STAT/");
  .L $ALICE_ROOT/../src/STAT/AliNDLocalRegression.cxx+
  .L $ALICE_ROOT/../src/STAT/test/AliNDLocalRegressionTest.C+  
  AliNDLocalRegressionTest(5000);
 
*/


#include "THn.h"
#include "TCanvas.h"
#include "AliNDLocalRegression.h"
#include "TStyle.h"

void UnitTestGaussNoise();
void UnitTestStreamer();

TTree * treeIn=0;
AliNDLocalRegression *pfitNDIdeal=0;       // ideal fit without noise
AliNDLocalRegression *pfitNDGaus0=0;       // fit with noisy data  - sample 0
AliNDLocalRegression *pfitNDGaus1=0;       // fit with noisy data  - sample 1
AliNDLocalRegression *pfitNDBreit0=0;       // fit with noisy data  - BreitWigner 0
AliNDLocalRegression *pfitNDBreit1=0;       // fit with noisy data  - BreitWigner 1

THn   *hN=0; 
TFormula *pformula= 0;

void PlotData(TH1F *hData,TString xTitle="xTitle",TString yTitle="yTitle",Color_t color=kBlack,TString zTitle="zTitle"){
  //
  //
  //
  if(color==(kRed+2)){hData->SetMarkerStyle(20);}
  if(color==(kBlue+2)){hData->SetMarkerStyle(21);}
  if(color==(kGreen+2)){hData->SetMarkerStyle(22);hData->SetMarkerSize(1.3);}
  
  hData->SetMarkerColor(color);
  hData->SetLineColor(color);
  hData->GetXaxis()->SetTitle(xTitle.Data());
  hData->GetYaxis()->SetTitle(yTitle.Data());
  hData->GetZaxis()->SetTitle(zTitle.Data());
  hData->GetXaxis()->SetTitleOffset(1.2);
  hData->GetXaxis()->SetTitleSize(0.05);
  hData->GetYaxis()->SetTitleOffset(1.3);
  hData->GetYaxis()->SetTitleSize(0.05);
  hData->GetXaxis()->SetLabelSize(0.035);
  hData->GetYaxis()->SetLabelSize(0.035);
  hData->GetXaxis()->SetDecimals();
  hData->GetYaxis()->SetDecimals();
  hData->GetZaxis()->SetDecimals();
  hData->Sumw2();
  hData->Draw("pe1");
}

void UnitTestGaussNoise(){
  //
  // Unit Test pulls Gauss
  // Compare 2 Regression objects obtained from 2 indpendent training sample
  //
  // Test:
  //   Bias < 10  *error
  //   Pull < 1+ 3*error
  //   
  TCanvas * canvasUnitGausNoise = new TCanvas("canvasUnitGausNoise","canvasUnitGausNoise",800,800);
  treeIn->Draw("(AliNDLocalRegression::GetCorrND(3,xyz0,xyz1)-AliNDLocalRegression::GetCorrND(2,xyz0,xyz1))/sqrt(AliNDLocalRegression::GetCorrNDError(3,xyz0,xyz1)**2+AliNDLocalRegression::GetCorrNDError(2,xyz0,xyz1)**2)>>pullsGaus32(401,-20.5,20.5)","","");
	TH1F   *pullsGaus32 = (TH1F*)gPad->GetPrimitive("pullsGaus32");
  Double_t meanPullsGaus01 = treeIn->GetHistogram()->GetMean();
  Double_t meanPullsGaus01Err = treeIn->GetHistogram()->GetMeanError();
  Double_t rmsPullsGaus01 = treeIn->GetHistogram()->GetRMS();
  Double_t rmsPullsGaus01Err = treeIn->GetHistogram()->GetRMSError();
  if (TMath::Abs(meanPullsGaus01)<10*meanPullsGaus01Err) {
    ::Info( "AliNDLocalRegressionTest","mean pull OK %3.3f\t+-%3.3f", meanPullsGaus01, meanPullsGaus01Err);
  }else{
    ::Error( "AliNDLocalRegressionTest","mean pull NOT OK %3.3f\t+-%3.3f", meanPullsGaus01, meanPullsGaus01Err);
  }
  if (rmsPullsGaus01<1+rmsPullsGaus01Err) {
    ::Info( "AliNDLocalRegressionTest"," rms pull OK %3.3f\t+-%3.3f", rmsPullsGaus01, rmsPullsGaus01Err);
  }else{
    ::Error( "AliNDLocalRegressionTest"," rms pull NOT OK %3.3f\t+-%3.3f", rmsPullsGaus01, rmsPullsGaus01Err);
  }
  //
  //
  //
	PlotData(pullsGaus32,"Gaus pulls","counts (arb. units)",kRed+2,"zTitle");
  canvasUnitGausNoise->SaveAs("AliNDLocalRegressionTest.canvasUnitTestGaussNoise.png");
}

void UnitTestBreitWignerNoise(){
  //
  // Unit Test pulls BreitWigner
  // Compare 2 Regression objects obtained from 2 independent training sample
  //
  // Test:
  //   Bias < 10  *error
  //   Pull < 1+ 3*error
  //   
  TCanvas * canvasUnitBreitWigner = new TCanvas("canvasUnitBretWigner","canvasUnitBreitWigner",800,800);
  
  treeIn->Draw("(AliNDLocalRegression::GetCorrND(5,xyz0,xyz1)-AliNDLocalRegression::GetCorrND(4,xyz0,xyz1))/sqrt(AliNDLocalRegression::GetCorrNDError(4,xyz0,xyz1)**2+AliNDLocalRegression::GetCorrNDError(5,xyz0,xyz1)**2)>>pullsBreiWigner54(401,-20.5,20.5)","","");
	TH1F   *pullsBreiWigner54 = (TH1F*)gPad->GetPrimitive("pullsBreiWigner54");
  Double_t meanPullsBreitWigner = treeIn->GetHistogram()->GetMean();
  Double_t meanPullsBreitWignerErr = treeIn->GetHistogram()->GetMeanError();
  Double_t rmsPullsBreitWigner = treeIn->GetHistogram()->GetRMS();
  Double_t rmsPullsBreitWignerErr = treeIn->GetHistogram()->GetRMSError();
  if (TMath::Abs(meanPullsBreitWigner)<10*meanPullsBreitWignerErr) {
    ::Info( "AliNDLocalRegressionTest::UnitTestBreitWignerNoise","mean pull OK %3.3f\t+-%3.3f", meanPullsBreitWigner, meanPullsBreitWignerErr);
  }else{
    ::Error( "AliNDLocalRegressionTest::UnitTestBreitWignerNoise","mean pull NOT OK %3.3f\t+-%3.3f", meanPullsBreitWigner, meanPullsBreitWignerErr);
  }
  if (rmsPullsBreitWigner<1+rmsPullsBreitWignerErr) {
    ::Info( "AliNDLocalRegressionTest::UnitTestBreitWignerNoise"," rms pull OK %3.3f\t+-%3.3f", rmsPullsBreitWigner, rmsPullsBreitWignerErr);
  }else{
    ::Error( "AliNDLocalRegressionTest::UnitTestBreitWignerNoise"," rms pull NOT OK %3.3f\t+-%3.3f", rmsPullsBreitWigner, rmsPullsBreitWignerErr);
  }
	PlotData(pullsBreiWigner54,"BreitWigner pulls","counts (arb. units)",kBlue+2,"zTitle");
  canvasUnitBreitWigner->SaveAs("AliNDLocalRegressionTest.canvasUnitBreitWigner.png");
}



void UnitTestStreamer(){
  //
  // Check consistency of streamer
  //
  TCanvas * canvasUnitTestStreamer = new TCanvas("canvasUnitTestStreamer","canvasUnitTestStreamer",800,800);
  TFile * f = TFile::Open("fitNDLocalTestStreamer.root","recreate");
  pfitNDGaus0->Write("pfitNDGaus0");
  f->Close();
  f = TFile::Open("fitNDLocalTestStreamer.root");
  AliNDLocalRegression *streamTest1 = (AliNDLocalRegression *)f->Get("pfitNDGaus0");
  streamTest1->AddVisualCorrection(streamTest1,100);
  Int_t entries = treeIn->Draw("AliNDLocalRegression::GetCorrND(100,xyz0,xyz1)-AliNDLocalRegression::GetCorrND(2,xyz0,xyz1)>>streamerTest(201,-1.05,1.05)","");
	TH1F   *streamerTest = (TH1F*)gPad->GetPrimitive("streamerTest");
  Int_t rms = TMath::RMS(entries,treeIn->GetV1());
  if (rms!=0){
    ::Error( "AliNDLocalRegressionTest::UnitTestStreamer","Streamer problem");
  }else{
    ::Info( "AliNDLocalRegressionTest::UnitTestStreamer","Streamer OK");
  }
	PlotData(streamerTest,"streamer test","counts (arb. units)",kGreen+2,"zTitle");
  canvasUnitTestStreamer->SaveAs(" AliNDLocalRegressionTest.UnitTestStreamer.png");
}



void AliNDLocalRegressionTest(Int_t npoints=10000, Int_t ndim=2, const char *sfromula="cos(7*x[0]/pi)*sin(19*x[1]/pi)", Double_t err=0.01){
  //
  // Local regression test method
  //
  // Int_t npoints=100000; Int_t ndim=2; const char *sfromula="cos(10*x[0])*cos(15*x[1])"; 
  //
	gStyle->SetPadRightMargin(0.05);
	gStyle->SetPadTopMargin(0.05);
	gStyle->SetPadLeftMargin(0.14);
	gStyle->SetPadBottomMargin(0.12);
	gStyle->SetPadTickX(1);
	gStyle->SetPadTickY(1);
	gStyle->SetPadGridX(1);
	gStyle->SetPadGridY(1);
	gStyle->SetOptStat(0);
	//
  pformula=new TFormula("pformula", sfromula);
  pfitNDIdeal  = new  AliNDLocalRegression;
  pfitNDGaus0  = new  AliNDLocalRegression;
  pfitNDGaus1  = new  AliNDLocalRegression;
  pfitNDBreit0 = new  AliNDLocalRegression;
  pfitNDBreit1 = new  AliNDLocalRegression;
  //
  // 0.) Initialization of variables and THn
  // 
  TTreeSRedirector *pcstreamIn         = new TTreeSRedirector("fitNDLocalTestInput.root","recreate");
  TTreeSRedirector *pcstreamOutIdeal   = new TTreeSRedirector("fitNDLocalTestOutputIdeal.root","recreate");  
  TTreeSRedirector *pcstreamOutGaus0   = new TTreeSRedirector("fitNDLocalTestOutputGaus0.root","recreate");  
  TTreeSRedirector *pcstreamOutGaus1   = new TTreeSRedirector("fitNDLocalTestOutputGaus1.root","recreate");  
  TTreeSRedirector *pcstreamOutBreit0  = new TTreeSRedirector("fitNDLocalTestOutputBreit0.root","recreate");  
  TTreeSRedirector *pcstreamOutBreit1  = new TTreeSRedirector("fitNDLocalTestOutputBreit1.root","recreate");  
  
  Double_t *xyz     = new Double_t[ndim];
  Double_t *sxyz    = new Double_t[ndim];
  Int_t    *nbins   = new Int_t[ndim];
{'fraction_non_alphanumeric': 0.1483561087809498, 'fraction_numerical': 0.035651468001623596, 'mean_word_length': 5.611359570661897, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 113, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4651701', 'n_tokens_mistral': 6142, 'n_tokens_neox': 5637, 'n_words': 801}