confusionMatrixProng.C
Go to the documentation of this file.
1 #include <iostream>
2 #include <iomanip>
3 #include <sstream>
4 #include <algorithm>
5 #include <math.h>
6 #include <stdlib.h>
7 #include <string>
8 
9 #include "TChain.h"
10 #include "TROOT.h"
11 #include "TStyle.h"
12 #include "TLegend.h"
13 
14 #include "TH1D.h"
15 #include "TH2D.h"
16 #include "TLatex.h"
17 #include "TCanvas.h"
18 
19 int maxID(double arrayInput[]){
20  int max=0;
21  double maxVal=0.;
22 
23  for(int i=0;i<5;i++){
24  if(arrayInput[i]>maxVal){
25  max=i;
26  maxVal=arrayInput[i];
27  }
28 
29  }
30 
31  return max;
32 }
33 
34 std::string to_stringP(double T, int n)
35 {
36  std::ostringstream out;
37  out << setprecision(n) << T;
38  return out.str();
39 }
40 
41 void confusionMatrixProng(string input, string outputname, bool pur) {
42  gROOT->ForceStyle();
43 
44  TChain *ch=new TChain("t");
45  ch->SetMakeClass(1); //<===== important
46  ch->Add(input.c_str());
47 
48  int truelabelall=0;
49  int selectedlabel=0;
50  double pion=0;
51  double proton=0;
52  double electron=0;
53  double muon=0;
54  double gamma=0;
55  int chosenlabel=0;
56 
57  double idOut[5]={0.,0.,0.,0.,0.};
58 
59  ch->SetBranchAddress("truelabelall",&truelabelall);
60  ch->SetBranchAddress("selectedlabel",&selectedlabel);
61  ch->SetBranchAddress("pion",&pion);
62  ch->SetBranchAddress("proton",&proton);
63  ch->SetBranchAddress("electron",&electron);
64  ch->SetBranchAddress("muon",&muon);
65  ch->SetBranchAddress("gamma",&gamma);
66 
67  TH2D *heatmap= new TH2D( "heatmap", "", 5,0,5,5,0,5);
68  TH1D *heatmap1d = new TH1D("heatmap1d","",5,0,5);
69  TH1D *compareplot = new TH1D("compareplot","",6,0,6);
70  TH1D *compareplot2 = new TH1D("compareplot2","",6,0,6);
71  std::string interaction[5] =
72  {"Electron", "Gamma","Muon","Pion","Proton"};
73  std::string interaction2[6] =
74  {"Electron", "Gamma","Muon","Pion","Proton","OtherPDG"};
75 
76  //heatmap->SetBit(TH1::kCanRebin);
77  Int_t nevent =ch->GetEntries();
78  for(Int_t i=0;i<nevent;i++){
79  ch->GetEntry(i);
80  idOut[0]=electron;
81  idOut[1]=gamma;
82  idOut[2]=muon;
83  idOut[3]=pion;
84  idOut[4]=proton;
85  chosenlabel=maxID(idOut);
86  int filllabel = 7; int filllabelp = 7;
87  if(truelabelall == 0) filllabel = 0;
88  if(selectedlabel == 0) filllabelp = 0;
89  if(truelabelall == 1) filllabel = 2;
90  if(selectedlabel == 1) filllabelp = 2;
91  if(truelabelall == 2) filllabel = 4;
92  if(selectedlabel == 2) filllabelp = 4;
93  if(truelabelall == 4) filllabel = 3;
94  if(selectedlabel == 4) filllabelp = 3;
95  if(truelabelall == 6) filllabel = 1;
96  if(selectedlabel == 6) filllabelp = 1;
97  if(truelabelall == 7) filllabel = 5;
98  if(selectedlabel == 7) filllabelp = 5;
99  heatmap1d->Fill(double(filllabel));
100  compareplot->Fill(double(filllabel));
101  compareplot2->Fill(double(filllabelp));
102  heatmap->Fill(double(filllabel),double(chosenlabel));
103 
104  }
105  //j i eff
106  //i j pur
107  for (int j=1;j<7;j++){
108  double scale=0;
109 
110  for (int i=1;i<7;i++){
111  if(pur) scale=scale+heatmap->GetBinContent(i,j);
112  else scale=scale+heatmap->GetBinContent(j, i);
113  }
114 
115  std::cout<<"how many events do I see? "<<scale<<std::endl;
116 
117  for (int i=1;i<7;i++){
118  int y =j;
119  int x =i;
120 
121  if(heatmap->GetBinContent(i,j)/scale>0.0000001)
122  {
123  if (pur) heatmap->SetBinContent(i,j,(heatmap->GetBinContent(i,j))/scale);
124  else heatmap->SetBinContent(j, i, (heatmap->GetBinContent(j, i))/scale);
125  }
126  else{
127  if (pur) heatmap->SetBinContent(i,j,.0000001);
128  else heatmap->SetBinContent(j, i, .0000001);
129  }
130  }
131  }
132 
133 
134 
135  string title="heatmap";
136 
137  TCanvas c3("c3",title.c_str(),900,900);
138 
139  heatmap->SetTitle("");
140  heatmap->SetXTitle("True");
141  heatmap->SetYTitle("Selected");
142  for(int i=1;i<7;i++){
143  heatmap->GetYaxis()->SetBinLabel(i, interaction[i-1].c_str());
144  heatmap->GetXaxis()->SetBinLabel(i, interaction[i-1].c_str());
145  heatmap1d->GetXaxis()->SetBinLabel(i, interaction[i-1].c_str());
146  }
147  for(int i=1; i<7; i++){
148  compareplot->GetXaxis()->SetBinLabel(i, interaction2[i-1].c_str());
149  compareplot2->GetXaxis()->SetBinLabel(i, interaction2[i-1].c_str());
150  }
151  heatmap1d->GetXaxis()->SetTickLength(0);
152  heatmap->GetXaxis()->SetTickLength(0);
153  heatmap->GetYaxis()->SetTickLength(0);
154  heatmap->GetYaxis()->SetTitleSize(0.06);
155  heatmap->GetXaxis()->SetTitleSize(0.06);
156  heatmap->GetYaxis()->SetTitleOffset(1);
157  heatmap->GetXaxis()->SetTitleOffset(1);
158  heatmap->GetYaxis()->SetLabelSize(0.04);
159  heatmap->GetXaxis()->SetLabelSize(0.04);
160  heatmap1d->GetXaxis()->SetLabelSize(0.04);
161  heatmap->GetZaxis()->SetLabelSize(0.03);
162  compareplot->GetXaxis()->SetLabelSize(0.04);
163  compareplot2->GetXaxis()->SetLabelSize(0.04);
164 
165 
166  heatmap->GetXaxis()->CenterTitle();
167  heatmap->GetYaxis()->CenterTitle();
168  gStyle->SetOptStat(0);
169  heatmap->SetMaximum(1.0);
170 
171  if(pur) heatmap->SetTitle("Purity");
172  else heatmap->SetTitle("Efficiency");
173 
174  heatmap->Draw("colz");
175  gStyle->SetPalette(51);
176  TLatex* binval[5][5];
177  for ( unsigned int binx = 1; binx <= heatmap->GetNbinsX(); ++binx ){
178  for ( unsigned int biny = 1; biny <= heatmap->GetNbinsY(); ++biny ){
179  double content = heatmap->GetBinContent(binx,biny);
180  if ( content > 0 ){
181  TString val = to_stringP(content,2);
182  binval[binx-1][biny-1] = new TLatex(binx-0.6,biny-0.6,val);
183  binval[binx-1][biny-1]->SetTextSize(0.023);
184  if ( content < 0.5 )
185  binval[binx-1][biny-1]->SetTextColor(kWhite);
186  binval[binx-1][biny-1]->Draw();
187  }
188  }
189  }
190  //gStyle->SetOptStat(0);
191  //c3->SetLogz();
192  //heatmap->SetMinimum(0.6);
193  //heatmap->SetMaximum(1.0);
194 // Sleep(5000);
195  if(!pur) outputname+="Eff";
196  else outputname += "Pur";
197  string output = "plots/"+outputname+"_"+title+".gif";
198 // c3.Print(output.c_str());
199 // output = "plots/"+outputname+"_"+title+".C";
200 // c3.Print(output.c_str());
201  output = "plots/"+outputname+"_"+title+".pdf";
202  c3.Print(output.c_str());
203 
204 
205 
206  TCanvas c4("c4",title.c_str(),900,900);
207  heatmap1d->Draw();
208  output = "plots/"+outputname+"_truelabels.pdf";
209  c4.Print(output.c_str());
210 
211  TCanvas c5("c5",title.c_str(),900,900);
212  compareplot->SetLineColor(kBlue);
213  compareplot2->SetLineColor(kBlack);
214  compareplot->SetLineWidth(3);
215  compareplot2->SetLineWidth(3);
216  compareplot2->Draw();
217  compareplot->Draw("same");
218 
219  TLegend *mylegend = new TLegend(0.35,0.6,0.55,0.8);
220  mylegend->AddEntry(compareplot,"True ID","L");
221  mylegend->AddEntry(compareplot2,"CVN ID","L");
222  mylegend->SetFillStyle(0);
223  mylegend->Draw("same");
224 
225  output = "plots/"+outputname+"_comparison.pdf";
226  c5.Print(output.c_str());
227  return;
228 }
229 
ofstream output
void confusionMatrixProng(string input, string outputname, bool pur)
std::string to_stringP(double T, int n)
Double_t scale
Definition: plot.C:25
int maxID(double arrayInput[])
const double j
Definition: BetheBloch.cxx:29
OStream cout
Definition: OStream.cxx:6
Int_t nevent
Definition: macro.C:10
double T
Definition: Xdiff_gwt.C:5
T max(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Definition: statistics.h:68
enum BeamMode kBlue
enum BeamMode string