Last active
May 6, 2019 08:13
-
-
Save cvson/dbdb19f631f1a548b667 to your computer and use it in GitHub Desktop.
[Football score] illustration football score #tutorials #MC #ROOT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// football_score.C | |
//////////////////////////////////////////////////// | |
// | |
// Simple Monte Carlo | |
// | |
/////////////////////////////////////////////////// | |
// Created by @visionlib.postach.io | |
{ | |
const Int_t nteam = 4; | |
//number of match (combination) | |
Int_t nmatch = TMath::Factorial(nteam)/(TMath::Factorial(nteam-2)*TMath::Factorial(2)); | |
cout<<"Number of match "<<nmatch<<endl;//should be six if 4 teams per group | |
Int_t nwin=0;//number win = number lost | |
Int_t ndraw=0; | |
//number of iterations | |
const Int_t kIteration = 10000; | |
//floating from 2 to 4 with 100 steps | |
const Float_t winPointMin = 1.5;//or 3 for later change | |
const Float_t winPointMax = 3.5; | |
Float_t winPoint; | |
const Int_t nstep = 100; | |
//match 0: team1 - team2 | |
//match 1: team3 - team4 | |
//match 2: team1 - team3 | |
//match 3: team2 - team4 | |
//match 4: team1 - team4 | |
//match 5: team2 - team3 | |
Float_t ipoint1[nmatch];//point of match for 1st team | |
Float_t ipoint2[nmatch];// point of match for 2nd team | |
Float_t arrayPointTeam[4];//to store total point of four teams | |
Int_t arraySortIndex[4];//for sorting | |
Int_t numTeamEqualPoint;//number of team have equal point | |
Int_t numberTieBreaker=0;//number of trial require tie breaker | |
Float_t tieBreakerPercentage; | |
TH2F* hwinpoint_vs_tiebreaker = new TH2F("hwinpoint_vs_tiebreaker","",100,winPointMin,winPointMax,100,20,40); | |
gRandom->SetSeed(); | |
//loop over nstep (different winner point) | |
for (Int_t istep = 0; istep<=nstep; istep++){ | |
winPoint = winPointMin+istep*(winPointMax-winPointMin)/float(nstep); | |
numberTieBreaker = 0; | |
//loop over iterations | |
for (Int_t iIter = 0; iIter<kIteration; iIter++ ){ | |
//reset number of team has equal point | |
//if (iIter%1000==0) printf("Processing %d/%d\n",iIter,kIteration); | |
numTeamEqualPoint=0; | |
//loop over nmatch to randomize result | |
for (Int_t imatch = 0; imatch<nmatch; imatch++){ | |
int valueRandom = (int)(gRandom->Uniform(0.0,1.0)*3); | |
if (valueRandom==0) {//win | |
ipoint1[imatch] = winPoint; | |
ipoint2[imatch] = 0; | |
} | |
else if (valueRandom==1) {//draw | |
ipoint1[imatch] = 1; | |
ipoint2[imatch] = 1; | |
} | |
else {//lose | |
ipoint1[imatch] = 0; | |
ipoint2[imatch] = winPoint; | |
} | |
}//end for imatch | |
//calculate total point for each team | |
//TODO: this mapping can be done in better way | |
arrayPointTeam[0] = ipoint1[0]+ipoint1[2]+ipoint1[4]; | |
arrayPointTeam[1] = ipoint2[0]+ipoint1[3]+ipoint1[5]; | |
arrayPointTeam[2] = ipoint1[1]+ipoint2[2]+ipoint2[5]; | |
arrayPointTeam[3] = ipoint2[1]+ipoint2[3]+ipoint2[4]; | |
TMath::Sort(4,arrayPointTeam,arraySortIndex); | |
//tie breaker | |
//happen when the 2nd and 3rd team have same point | |
if (arrayPointTeam[arraySortIndex[1]] == arrayPointTeam[arraySortIndex[2]]){ | |
++numTeamEqualPoint; | |
} | |
//count number of trial with tie-breaker requirement | |
if (numTeamEqualPoint>0) ++numberTieBreaker; | |
}//end for iIter | |
//printf("%d times per %d require tie breaker, i.e %.3g percentage\n",numberTieBreaker,kIteration,numberTieBreaker*100/kIteration); | |
tieBreakerPercentage = numberTieBreaker*100/float(kIteration); | |
hwinpoint_vs_tiebreaker->Fill(winPoint,tieBreakerPercentage); | |
cout<<" win/draw "<<winPoint<<" tiebreak "<<tieBreakerPercentage<<endl; | |
}//end for istep | |
new TCanvas; | |
gStyle->SetOptStat(0); | |
gStyle->SetLineWidth(2); | |
hwinpoint_vs_tiebreaker->GetXaxis()->SetTitle("Winner point / Draw point"); | |
hwinpoint_vs_tiebreaker->GetYaxis()->SetTitle("Percentage of Tie Breaker"); | |
hwinpoint_vs_tiebreaker->GetYaxis()->CenterTitle(); | |
hwinpoint_vs_tiebreaker->GetXaxis()->CenterTitle(); | |
hwinpoint_vs_tiebreaker->GetXaxis()->SetLabelSize(hwinpoint_vs_tiebreaker->GetXaxis()->GetTitleSize()*1.2); | |
hwinpoint_vs_tiebreaker->GetYaxis()->SetLabelSize(hwinpoint_vs_tiebreaker->GetYaxis()->GetTitleSize()*1.2); | |
hwinpoint_vs_tiebreaker->GetXaxis()->SetTitleSize(hwinpoint_vs_tiebreaker->GetXaxis()->GetLabelSize()*1.2); | |
hwinpoint_vs_tiebreaker->GetYaxis()->SetTitleSize(hwinpoint_vs_tiebreaker->GetYaxis()->GetLabelSize()*1.2); | |
hwinpoint_vs_tiebreaker->GetYaxis()->SetTitleOffset(0.9); | |
hwinpoint_vs_tiebreaker->GetXaxis()->SetTitleOffset(0.9); | |
hwinpoint_vs_tiebreaker->SetMarkerStyle(8); | |
hwinpoint_vs_tiebreaker->SetMarkerSize(1.2); | |
Int_t ci; | |
ci = TColor::GetColor("#B45F04"); | |
hwinpoint_vs_tiebreaker->SetMarkerColor(ci); | |
hwinpoint_vs_tiebreaker->Draw(); | |
gPad->Print("football_score.png"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment