-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfitResult.h
161 lines (127 loc) · 4.81 KB
/
fitResult.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <TH1.h>
#include <TNtuple.h>
#include <TCut.h>
#include <TF1.h>
class fitResult {
public:
Double_t nSig;
Double_t nSigErr;
Double_t purity;
Double_t chisq;
Double_t sigMeanShift;
TH1F *sigPdf;
TH1F *bckPdf;
TH1D *data;
};
class histFunction2
{
public:
histFunction2(TH1D *h, TH1D *h2);
~histFunction2(){
delete histBck;
delete histSig;
};
// Int_t GetBinNumber(Double_t c);
Double_t evaluate(Double_t *x, Double_t *par);
TH1D *histSig;
TH1D *histBck;
Double_t lowEdge;
Double_t highEdge;
Double_t nbin;
};
histFunction2::histFunction2(TH1D *h,TH1D *h2)
{
histSig=(TH1D*)h->Clone();
histBck=(TH1D*)h2->Clone();
nbin=h->GetNbinsX();
lowEdge=h->GetBinLowEdge(1);
highEdge=h->GetBinLowEdge(nbin+1);
histSig->SetName("hSig");
histSig->Scale(1./histSig->Integral(1,histSig->GetNbinsX()+1));
histBck->SetName("hBck");
histBck->Scale(1./histBck->Integral(1,histBck->GetNbinsX()+1));
}
Double_t histFunction2::evaluate(Double_t *x, Double_t *par) {
Double_t xx = x[0];
Int_t binNum=histSig->FindBin(xx); //
return par[0]*(histSig->GetBinContent(binNum)*par[1]+histBck->GetBinContent(binNum)*(1-par[1]));
}
fitResult doFit(TH1D* hSig=0, TH1D* hBkg=0, TH1D* hData1=0,
Float_t varLow=0.001, Float_t varHigh=0.028,
Double_t purityBinVal=0.00999)
{
TH1D* hDatatmp = (TH1D*)hData1->Clone(Form("%s_datatmp",hData1->GetName()));
Int_t nBins = hDatatmp->GetNbinsX();
histFunction2 *myFits = new histFunction2(hSig,hBkg);
TF1 *f = new TF1("f",myFits,&histFunction2::evaluate,varLow,varHigh,2);
f->SetParameters( hDatatmp->Integral(1,nBins+1), 0.3);
f->SetParLimits(1,0,1);
hDatatmp->Fit("f","WL M 0 Q","",varLow,varHigh);
hDatatmp->Fit("f","WL M 0 Q","",varLow,varHigh);
fitResult res;
res.nSig =0;
Double_t nev = f->GetParameter(0);
Double_t ratio = f->GetParameter(1);
Double_t ratioErr = f->GetParError(1);
res.nSig = nev * ratio;
res.nSigErr = nev * ratioErr;
res.chisq = (Double_t)f->GetChisquare()/ f->GetNDF();
TH1F *hSigPdf = (TH1F*)hSig->Clone(Form("%s_tmp",hSig->GetName()));
hSigPdf->Scale(res.nSig/hSigPdf->Integral(1,nBins+1));
TH1F *hBckPdf = (TH1F*)hBkg->Clone(Form("%s_tmp",hBkg->GetName()));
hBckPdf->Scale((nev-res.nSig)/hBckPdf->Integral(1,nBins+1));
Double_t ss1 = hSigPdf->Integral(1, hSigPdf->FindBin(purityBinVal),"width");
Double_t bb1 = hBckPdf->Integral(1, hBckPdf->FindBin(purityBinVal),"width");
res.purity = ss1/(ss1+bb1);
res.sigPdf = (TH1F*)hSigPdf->Clone(Form("%s_sig",hSig->GetName()));
res.bckPdf = (TH1F*)hBckPdf->Clone(Form("%s_bck",hBkg->GetName()));
res.data = (TH1D*)hData1->Clone(Form("%s_cand",hData1->GetName()));
return res;
}
void mcStyle(TH1* h=0) {
h->SetLineColor(kPink);
h->SetFillColor(kOrange+7);
h->SetFillStyle(3004);
}
void sbStyle(TH1* h=0) {
h->SetLineColor(kGreen+4);
h->SetFillColor(kGreen+1);
h->SetFillStyle(3001);
}
//const Int_t nSIGMABINS = 100; // number of bins in sigmaIetaIeta dist
const Int_t nSIGMABINS = 25;
const Double_t maxSIGMA = 0.025; // x-axis max of sigmaIetaIeta dist
fitResult getPurity(TTree *dataTree, TTree *mcTree,
TCut dataCandidateCut, TCut sidebandCut,
TCut mcSignalCut, Double_t signalShift=0.0,
Double_t backgroundShift=0.0, Double_t purityBinVal=0.00999)
{
TH1D* hCand = new TH1D("cand","",nSIGMABINS,0,maxSIGMA);
TH1D* hBkg = (TH1D*)hCand->Clone("bkg");
TH1D* hSig = (TH1D*)hCand->Clone("sig");
TString sigshift = "+";
sigshift += signalShift;
TString bkgshift = "+";
bkgshift += backgroundShift;
//Int_t dEntries = dataTree->Project(hCand->GetName(), "sigmaIetaIeta", dataCandidateCut, "");
//Int_t sbEntries = dataTree->Project(hBkg->GetName(), "sigmaIetaIeta"+bkgshift, sidebandCut, "");
//Int_t mcEntries = mcTree->Project(hSig->GetName(), "sigmaIetaIeta"+sigshift, "mcweight"*mcSignalCut, "");
//cout << "# Candidates: " << dEntries << endl;
//cout << "# Sideband: " << sbEntries << endl;
//cout << "# MC Signal: " << mcEntries << endl;
dataTree->Project(hCand->GetName(), "sigmaIetaIeta", dataCandidateCut, "");
//dataTree->Project(hBkg->GetName(), "sigmaIetaIeta"+bkgshift, "(1-40*(sigmaIetaIeta-0.01))"*sidebandCut, "");
dataTree->Project(hBkg->GetName(), "sigmaIetaIeta"+bkgshift, sidebandCut, "");
//TFile *bkgFile = TFile::Open("gammaJets_pp_MC_EmEnrichedDijet.root");
//TTree *bkgTree = (TTree*)bkgFile->Get("photonTree");
//bkgTree->Project(hBkg->GetName(), "sigmaIetaIeta"+bkgshift, "mcweight"*sidebandCut, "");
mcTree->Project(hSig->GetName(), "sigmaIetaIeta"+sigshift, "photonTree.mcweight"*mcSignalCut, "");
//TCanvas *fakeCanvas = new TCanvas("fake","fake");
fitResult fitr = doFit(hSig, hBkg, hCand, 0.005, 0.035, purityBinVal);
//delete fakeCanvas;
delete hSig;
delete hBkg;
delete hCand;
fitr.sigMeanShift = signalShift;
return fitr;
}