11 const int nParticles = 5;
13 TString testFileName =
14 "/data/t2k/phsxvg/DUNEPid/data/4APA/EventSamples/train_muon_realflux_split_2.root";
16 TString trainFileName[nParticles] = {
17 "/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] = {
24 "/data/t2k/phsxvg/DUNEPid/data/4APA/weights/muon_all_BDT.weights.xml",
25 "/data/t2k/phsxvg/DUNEPid/data/4APA/weights/electron_all_BDT.weights.xml",
26 "/data/t2k/phsxvg/DUNEPid/data/4APA/weights/proton_all_BDT.weights.xml",
27 "/data/t2k/phsxvg/DUNEPid/data/4APA/weights/pich_all_BDT.weights.xml",
28 "/data/t2k/phsxvg/DUNEPid/data/4APA/weights/photon_all_BDT.weights.xml"};
30 TFile* fileForBinning = TFile::Open(trainFileName[0]);
31 fileForBinning->cd(
"Method_BDT/BDT");
33 int numbins = MVA_BDT_Train_S->GetNbinsX();
34 const int nbins = numbins;
36 fileForBinning->Close();
38 double likeRatioS[nParticles][nbins] = {0};
40 double binWidth[nParticles], MVAMin[nParticles], MVAMax[nParticles];
42 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) {
51 cerr <<
"MVA_BDT_Train_S->GetNbinsX() not equal to nbins in file " << trainFileName[p]
55 if (MVA_BDT_Train_B->GetNbinsX() != nbins) {
56 cerr <<
"MVA_BDT_Train_B->GetNbinsX() not equal to nbins in file " << trainFileName[p]
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]) {
66 cerr <<
"MVA_BDT_Train_B->GetBinWidth(1) not equal to binWidth in file " << trainFileName[p]
70 if (MVA_BDT_Train_B->GetBinLowEdge(1) != MVAMin[p]) {
71 cerr <<
"MVA_BDT_Train_B->GetBinLowEdge(1) not equal to MVAMin in file " << trainFileName[p]
75 if (MVA_BDT_Train_B->GetBinLowEdge(nbins) + MVA_BDT_Train_B->GetBinWidth(nbins) != MVAMax[p]) {
76 cerr <<
"MVA_BDT_Train_B->GetBinLowEdge(nbins) + MVA_BDT_Train_B->GetBinWidth(nbins) not " 77 "equal to MVAMax in file " 78 << trainFileName[p] << endl;
82 for (
int i = 1; i <= nbins; i++) {
84 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,
108 vector<double> likeRatio[nParticles];
111 TH1D* hMVAValue[nParticles] = {0};
112 for (
int p = 0; p < nParticles; p++)
113 hMVAValue[p] =
new TH1D(Form(
"hMVAValue%d", p),
"", nbins, -0.5, 0.5);
115 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) {
134 evalRatioR = evalRatio;
135 coreHaloRatioR = coreHaloRatio;
136 concentrationR = concentration;
137 conicalnessR = conicalness;
138 dEdxStartR = dEdxStart;
140 dEdxEndRatioR = dEdxEndRatio;
142 MVAValue = reader->EvaluateMVA(
"BDT");
144 MVABin = 1 + int((MVAValue - MVAMin[p]) / binWidth[p]);
146 hMVAValue[p]->Fill(MVAValue);
148 likeRatio[p].push_back(likeRatioS[p][MVABin]);
154 TCanvas* MVAVal =
new TCanvas(
"MVAVal",
"MVAVal", 800, 600);
156 hMVAValue[0]->GetXaxis()->SetTitle(
"BDT variable");
157 hMVAValue[0]->GetYaxis()->SetRangeUser(0, 250);
159 hMVAValue[0]->Draw();
161 hMVAValue[1]->Draw(
"same");
163 hMVAValue[2]->Draw(
"same");
165 hMVAValue[3]->Draw(
"same");
167 hMVAValue[4]->Draw(
"same");
169 TLegend* lM =
new TLegend(0.54, 0.57, 0.84, 0.87);
170 lM->AddEntry(hMVAValue[0],
"Muon weights",
"L");
171 lM->AddEntry(hMVAValue[1],
"Electron weights",
"L");
172 lM->AddEntry(hMVAValue[2],
"Proton weights",
"L");
173 lM->AddEntry(hMVAValue[3],
"#pi^{#pm} weights",
"L");
174 lM->AddEntry(hMVAValue[4],
"Photon weights",
"L");
175 lM->SetBorderSize(0);
178 int likeVectorSize = likeRatio[0].size();
179 for (
int p = 1; p < nParticles; p++) {
180 if (likeRatio[p].
size() != likeVectorSize) {
181 cerr <<
"Likelihood ratio vector sizes not equal" << endl;
188 double particleLikeRatio[nParticles] = {0};
189 double totalLikeRatio = 0.0;
191 TH1D* hParticlePLR[nParticles] = {0};
193 for (
int p = 0; p < nParticles; p++)
194 hParticlePLR[p] =
new TH1D(Form(
"hParticlePLR%d", p),
"", nProbBins, 0.0, 1.0);
196 TH1D* hLikeRatio[nParticles] = {0};
198 for (
int p = 0; p < nParticles; p++)
199 hLikeRatio[p] =
new TH1D(Form(
"hLikeRatio%d", p),
"", 40, -10.0, 10.0);
201 while (vectorIndex < likeRatio[0].
size()) {
202 totalLikeRatio = 0.0;
204 for (
int p = 0; p < nParticles; p++) {
205 likeIter = likeRatio[p].begin() + vectorIndex;
206 particleLikeRatio[p] = (*likeIter);
207 totalLikeRatio += (*likeIter);
210 for (
int p = 0; p < nParticles; p++) {
213 hParticlePLR[p]->Fill(TMath::Min(0.9999, particleLikeRatio[p] / totalLikeRatio));
219 TCanvas* ParticlePLR =
new TCanvas(
"ParticlePLR",
"ParticlePLR", 800, 600);
221 hParticlePLR[0]->GetXaxis()->SetTitle(
"Proportion of likelihood ratio");
222 hParticlePLR[0]->GetYaxis()->SetRangeUser(0, 800);
224 hParticlePLR[0]->Draw();
226 hParticlePLR[1]->Draw(
"same");
228 hParticlePLR[2]->Draw(
"same");
230 hParticlePLR[3]->Draw(
"same");
232 hParticlePLR[4]->Draw(
"same");
234 TLegend* lP =
new TLegend(0.6, 0.55, 0.83, 0.85);
235 lP->AddEntry(hParticlePLR[0],
"Muon",
"L");
236 lP->AddEntry(hParticlePLR[1],
"Electron",
"L");
237 lP->AddEntry(hParticlePLR[2],
"Proton",
"L");
238 lP->AddEntry(hParticlePLR[3],
"#pi^{#pm}",
"L");
239 lP->AddEntry(hParticlePLR[4],
"Photon",
"L");
240 lP->SetBorderSize(0);
249 gPad->SetLeftMargin(0.18);
250 gPad->SetBottomMargin(0.15);
251 gPad->SetRightMargin(0.15);
257 hist->SetLineColor(colour);
258 hist->SetLineWidth(2);
259 hist->SetLineStyle(style);
261 hist->GetXaxis()->SetTitleOffset(1.2);
262 hist->GetYaxis()->SetTitleOffset(1.4);
263 hist->GetXaxis()->CenterTitle();
264 hist->GetYaxis()->CenterTitle();
270 hist->SetLineColor(colour);
271 hist->SetLineWidth(2);
272 hist->SetLineStyle(style);
void CalcPropLikeRatio(const int nProbBins=25)
void FormatCanvas(TCanvas *canvas)
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
void FormatHistLong(TH1D *hist, int colour, int style)
void FormatHist(TH1D *hist, int colour, int style)