9 const int nProbBins = 25
13 const int nParticles = 5;
15 TString testFileName =
"/data/t2k/phsxvg/DUNEPid/data/4APA/EventSamples/train_muon_realflux_split_2.root";
17 TString trainFileName[nParticles] = {
"/data/t2k/phsxvg/DUNEPid/data/4APA/TrainMVA/mvaPlots_muon_all.root",
18 "/data/t2k/phsxvg/DUNEPid/data/4APA/TrainMVA/mvaPlots_electron_all.root",
19 "/data/t2k/phsxvg/DUNEPid/data/4APA/TrainMVA/mvaPlots_proton_all.root",
20 "/data/t2k/phsxvg/DUNEPid/data/4APA/TrainMVA/mvaPlots_pich_all.root",
21 "/data/t2k/phsxvg/DUNEPid/data/4APA/TrainMVA/mvaPlots_photon_all.root"};
23 TString weightFileName[nParticles] = {
"/data/t2k/phsxvg/DUNEPid/data/4APA/weights/muon_all_BDT.weights.xml",
24 "/data/t2k/phsxvg/DUNEPid/data/4APA/weights/electron_all_BDT.weights.xml",
25 "/data/t2k/phsxvg/DUNEPid/data/4APA/weights/proton_all_BDT.weights.xml",
26 "/data/t2k/phsxvg/DUNEPid/data/4APA/weights/pich_all_BDT.weights.xml",
27 "/data/t2k/phsxvg/DUNEPid/data/4APA/weights/photon_all_BDT.weights.xml"};
29 TFile *fileForBinning = TFile::Open(trainFileName[0]);
30 fileForBinning->cd(
"Method_BDT/BDT");
32 int numbins = MVA_BDT_Train_S->GetNbinsX();
33 const int nbins = numbins;
35 fileForBinning->Close();
37 double likeRatioS[nParticles][nbins] = {0};
39 double binWidth[nParticles], MVAMin[nParticles], MVAMax[nParticles];
41 for(
int p=0; p<nParticles; p++)
44 cout<<
"Calculating likelihood ratios from file "<<trainFileName[p]<<endl;
47 TFile *trainFile = TFile::Open(trainFileName[p]);
48 trainFile->cd(
"Method_BDT/BDT");
50 if(MVA_BDT_Train_S->GetNbinsX() != nbins)
52 cerr<<
"MVA_BDT_Train_S->GetNbinsX() not equal to nbins in file "<<trainFileName[p]<<endl;
55 if(MVA_BDT_Train_B->GetNbinsX() != nbins)
57 cerr<<
"MVA_BDT_Train_B->GetNbinsX() not equal to nbins in file "<<trainFileName[p]<<endl;
61 binWidth[p] = MVA_BDT_Train_S->GetBinWidth(1);
62 MVAMin[p] = MVA_BDT_Train_S->GetBinLowEdge(1);
63 MVAMax[p] = MVA_BDT_Train_S->GetBinLowEdge(nbins) + MVA_BDT_Train_S->GetBinWidth(nbins);
65 if(MVA_BDT_Train_B->GetBinWidth(1) != binWidth[p])
67 cerr<<
"MVA_BDT_Train_B->GetBinWidth(1) not equal to binWidth in file "<<trainFileName[p]<<endl;
70 if(MVA_BDT_Train_B->GetBinLowEdge(1) != MVAMin[p])
72 cerr<<
"MVA_BDT_Train_B->GetBinLowEdge(1) not equal to MVAMin in file "<<trainFileName[p]<<endl;
75 if(MVA_BDT_Train_B->GetBinLowEdge(nbins) + MVA_BDT_Train_B->GetBinWidth(nbins) != MVAMax[p])
77 cerr<<
"MVA_BDT_Train_B->GetBinLowEdge(nbins) + MVA_BDT_Train_B->GetBinWidth(nbins) not equal to MVAMax in file "<<trainFileName[p]<<endl;
81 for(
int i=1; i<=nbins; i++)
83 likeRatioS[p][i] = MVA_BDT_Train_S->GetBinContent(i) / TMath::Max(0.01, MVA_BDT_Train_B->GetBinContent(i));
88 TFile *testfile = TFile::Open(testFileName);
90 TTree* PIDVar = (TTree*)testfile->Get(
"mvaTree");
92 float evalRatio, coreHaloRatio, concentration, conicalness, dEdxStart, dEdxEnd, dEdxEndRatio;
94 PIDVar->SetBranchAddress(
"evalRatio", &evalRatio);
95 PIDVar->SetBranchAddress(
"coreHaloRatio", &coreHaloRatio);
96 PIDVar->SetBranchAddress(
"concentration", &concentration);
97 PIDVar->SetBranchAddress(
"conicalness", &conicalness);
98 PIDVar->SetBranchAddress(
"dEdxStart", &dEdxStart);
99 PIDVar->SetBranchAddress(
"dEdxEnd", &dEdxEnd);
100 PIDVar->SetBranchAddress(
"dEdxEndRatio", &dEdxEndRatio);
105 float evalRatioR, coreHaloRatioR, concentrationR, conicalnessR, dEdxStartR, dEdxEndR, dEdxEndRatioR;
107 vector<double> likeRatio[nParticles];
110 TH1D *hMVAValue[nParticles] = {0};
111 for(
int p=0; p<nParticles; p++)
112 hMVAValue[p] =
new TH1D(Form(
"hMVAValue%d", p),
"", nbins, -0.5, 0.5);
114 for(
int p=0; p<nParticles; p++)
117 cout<<
"Reading weights from file "<<weightFileName[p]<<endl;
119 TMVA::Reader* reader =
new TMVA::Reader();
121 reader->AddVariable(
"evalRatio", &evalRatioR);
122 reader->AddVariable(
"coreHaloRatio", &coreHaloRatioR);
123 reader->AddVariable(
"concentration", &concentrationR);
124 reader->AddVariable(
"conicalness", &conicalnessR);
125 reader->AddVariable(
"dEdxStart", &dEdxStartR);
126 reader->AddVariable(
"dEdxEnd", &dEdxEndR);
127 reader->AddVariable(
"dEdxEndRatio", &dEdxEndRatioR);
129 reader->BookMVA(
"BDT", weightFileName[p]);
131 for(
int i=0;i<PIDVar->GetEntries();++i)
135 evalRatioR = evalRatio;
136 coreHaloRatioR = coreHaloRatio;
137 concentrationR = concentration;
138 conicalnessR = conicalness;
139 dEdxStartR = dEdxStart;
141 dEdxEndRatioR = dEdxEndRatio;
143 MVAValue = reader->EvaluateMVA(
"BDT");
145 MVABin = 1 + int((MVAValue - MVAMin[p]) / binWidth[p]);
147 hMVAValue[p]->Fill(MVAValue);
149 likeRatio[p].push_back(likeRatioS[p][MVABin]);
157 TCanvas *MVAVal =
new TCanvas(
"MVAVal",
"MVAVal", 800, 600);
159 hMVAValue[0]->GetXaxis()->SetTitle(
"BDT variable");
160 hMVAValue[0]->GetYaxis()->SetRangeUser(0, 250);
162 hMVAValue[0]->Draw();
164 hMVAValue[1]->Draw(
"same");
166 hMVAValue[2]->Draw(
"same");
168 hMVAValue[3]->Draw(
"same");
170 hMVAValue[4]->Draw(
"same");
172 TLegend *lM =
new TLegend(0.54, 0.57, 0.84, 0.87);
173 lM->AddEntry(hMVAValue[0],
"Muon weights",
"L");
174 lM->AddEntry(hMVAValue[1],
"Electron weights",
"L");
175 lM->AddEntry(hMVAValue[2],
"Proton weights",
"L");
176 lM->AddEntry(hMVAValue[3],
"#pi^{#pm} weights",
"L");
177 lM->AddEntry(hMVAValue[4],
"Photon weights",
"L");
178 lM->SetBorderSize(0);
181 int likeVectorSize = likeRatio[0].size();
182 for(
int p=1; p<nParticles; p++)
184 if(likeRatio[p].size() != likeVectorSize)
186 cerr<<
"Likelihood ratio vector sizes not equal"<<endl;
193 double particleLikeRatio[nParticles] = {0};
194 double totalLikeRatio = 0.0;
196 TH1D *hParticlePLR[nParticles] = {0};
198 for(
int p=0; p<nParticles; p++)
199 hParticlePLR[p] =
new TH1D(Form(
"hParticlePLR%d", p),
"", nProbBins, 0.0, 1.0);
201 TH1D *hLikeRatio[nParticles] = {0};
203 for(
int p=0; p<nParticles; p++)
204 hLikeRatio[p] =
new TH1D(Form(
"hLikeRatio%d", p),
"", 40, -10.0, 10.0);
206 while(vectorIndex < likeRatio[0].size())
208 totalLikeRatio = 0.0;
210 for(
int p=0; p<nParticles; p++)
212 likeIter = likeRatio[p].begin() + vectorIndex;
213 particleLikeRatio[p] = (*likeIter);
214 totalLikeRatio += (*likeIter);
217 for(
int p=0; p<nParticles; p++)
221 hParticlePLR[p]->Fill(TMath::Min(0.9999, particleLikeRatio[p] / totalLikeRatio));
227 TCanvas *ParticlePLR =
new TCanvas(
"ParticlePLR",
"ParticlePLR", 800, 600);
229 hParticlePLR[0]->GetXaxis()->SetTitle(
"Proportion of likelihood ratio");
230 hParticlePLR[0]->GetYaxis()->SetRangeUser(0, 800);
232 hParticlePLR[0]->Draw();
234 hParticlePLR[1]->Draw(
"same");
236 hParticlePLR[2]->Draw(
"same");
238 hParticlePLR[3]->Draw(
"same");
240 hParticlePLR[4]->Draw(
"same");
242 TLegend *lP =
new TLegend(0.6, 0.55, 0.83, 0.85);
243 lP->AddEntry(hParticlePLR[0],
"Muon",
"L");
244 lP->AddEntry(hParticlePLR[1],
"Electron",
"L");
245 lP->AddEntry(hParticlePLR[2],
"Proton",
"L");
246 lP->AddEntry(hParticlePLR[3],
"#pi^{#pm}",
"L");
247 lP->AddEntry(hParticlePLR[4],
"Photon",
"L");
248 lP->SetBorderSize(0);
257 gPad->SetLeftMargin(0.18);
258 gPad->SetBottomMargin(0.15);
259 gPad->SetRightMargin(0.15);
265 hist->SetLineColor(colour);
266 hist->SetLineWidth(2);
267 hist->SetLineStyle(style);
269 hist->GetXaxis()->SetTitleOffset(1.2);
270 hist->GetYaxis()->SetTitleOffset(1.4);
271 hist->GetXaxis()->CenterTitle();
272 hist->GetYaxis()->CenterTitle();
278 hist->SetLineColor(colour);
279 hist->SetLineWidth(2);
280 hist->SetLineStyle(style);
void CalcPropLikeRatio(const int nProbBins=25)
void FormatCanvas(TCanvas *canvas)
void FormatHistLong(TH1D *hist, int colour, int style)
void FormatHist(TH1D *hist, int colour, int style)