-
Notifications
You must be signed in to change notification settings - Fork 0
/
escaleFit.C
395 lines (299 loc) · 14.8 KB
/
escaleFit.C
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#if !defined(__CINT__) || defined(__MAKECINT__)
#include <TROOT.h> // access to gROOT, entry point to ROOT system
#include <TSystem.h> // interface to OS
#include <TStyle.h> // class to handle ROOT plotting style
#include <TFile.h> // file handle class
#include <TTree.h> // class to access ntuples
#include <TCanvas.h> // class for drawing
#include <TH1D.h> // 1D histograms
#include <TH2D.h> // 2D histograms
#include <TLatex.h> //
#include <TGaxis.h> //
#include <TPavesText.h> //
#include <TBenchmark.h> // class to track macro running statistics
#include <TEfficiency.h> // class to handle efficiency calculations
#include <TMath.h>
#include <vector> // STL vector class
#include <iostream> // standard I/O
#include <iomanip> // functions to format standard I/O
#include <fstream> // functions for file I/O
#include <string> // C++ string class
#include <sstream> // class for parsing strings
#include "CPlot.hh" // helper class for plots
#include "MitStyleRemix.hh" // style settings for drawing
//#include "MyTools.hh" // miscellaneous helper functions
#include "CEffUser1D.hh" // class for handling efficiency graphs
#include "CEffUser2D.hh" // class for handling efficiency tables
//#include "ZSignals.hh"
//#include "ZBackgrounds.hh"
#include "RooBreitWigner.h"
#include "RooCBShape.h"
#include "RooFFTConvPdf.h"
#endif
// RooFit headers
#include "RooRealVar.h"
#include "RooCategory.h"
#include "RooDataSet.h"
#include "RooDataHist.h"
#include "RooFormulaVar.h"
#include "RooSimultaneous.h"
#include "RooAddPdf.h"
#include "RooFitResult.h"
#include "RooExtendPdf.h"
#include "RooGenericPdf.h"
#include "RooProdPdf.h"
#include "RooPolynomial.h"
#include "../MitHzz4l/macros/EnergyCorrectionData.hh"
using namespace RooFit;
TH1F *puWeights;
float puweight(float npu) {
int npuxbin = puWeights->GetXaxis()->FindFixBin(TMath::Min(double(npu), 49.499));
float puwgt = puWeights->GetBinContent(npuxbin);
return puwgt;
}
void escaleFit(int cat=2, TString fileDir = "/data/blue/anlevin/") {
assert(cat == 1 || cat == 2);
string label = "";
if(cat == 1) label = "barrel";
else if (cat == 2) label == "endcap";
else
assert(0);
const TString pileupReweightFile = "/data/blue/vdutta/htt/pileup/PUWeights_S12To2012_2968ipb.root";
TFile *pufile = new TFile(pileupReweightFile.Data());
puWeights = (TH1F*)pufile->Get("puWeights");
RooCategory sample("sample","") ;
sample.defineType("Data", 1) ;
sample.defineType("MC", 2) ;
//gROOT->Macro("MitStyleRemix.cc");
TLatex* text=new TLatex(3.5,23.5,label.c_str());
text->SetNDC();
text->SetTextAlign(13);
text->SetX(0.8);//(0.940);
text->SetY(0.95);
text->SetTextFont(42);
text->SetTextSize(0.035);// dflt=28
TString fileNameMC;
TString fileNameData;
fileNameMC = "electron_energy_all_s12-zllm50-2-v9.root";
fileNameData = "electron_energy_all_r12a_and_r12b-del-pr-v1.root";
TFile* mcFile = new TFile( (fileDir+fileNameMC).Data() );
TFile* dataFile = new TFile( (fileDir+fileNameData).Data() );
TTree* mcTree;
TTree* dataTree;
mcTree = (TTree*) mcFile ->FindObjectAny("Events");
dataTree = (TTree*) dataFile->FindObjectAny("Events");
TString basesel = "pt1>10.0 && pt2>10.0";
TString isebeb = " && abs(eta1) < 1.479 && abs(eta2) < 1.479";
TString iseeee = " && abs(eta1) > 1.479 && abs(eta2) > 1.479";
TString catsel;
if(cat==1) catsel = isebeb;
else if (cat==2) catsel = iseeee;
RooRealVar pt1("pt1","pT_1",0.0,10000.0,"GeV");
RooRealVar pt2("pt2","pT_2",0.0,10000.0,"GeV");
RooRealVar eta1("eta1","eta1",-1000.0,1000.0);
RooRealVar eta2("eta2","eta2",-1000.0,1000.0);
RooRealVar mass("mass","m_{ee}",60.0,120.0,"GeV");
RooRealVar npu("npu","npu",0,50);
RooRealVar wgt("wgt","wgt",-5,1000);
mass.setRange("zrange",60.0,120.0);
mass.setBins(120);
mass.setBins(10000,"cache");
RooArgSet treeSet(RooArgList(mass,pt1,pt2,eta1,eta2,wgt));
RooFormulaVar puweightf("puweightv","puweightv","puweight(npu)",RooArgList(npu));
TH1D* hData = new TH1D("hData","",120,60.,120.);
hData->GetYaxis()->SetTitle("# Events");
hData->GetXaxis()->SetTitle("m [GeV]");
TH1D* hMC = new TH1D("hMC","",120,60.,120.);
hMC->GetYaxis()->SetTitle("# MC Events");
hMC->GetXaxis()->SetTitle("m [GeV]");
dataTree->Draw("mass>>hData", (basesel+catsel).Data());
hData = (TH1D*) gPad->GetPrimitive("hData");
RooDataSet* dataDS = new RooDataSet("dataDS","",treeSet,"wgt");
EnergyCorrectionData electron_energy_data;
dataTree->SetBranchAddress("Events",&electron_energy_data);
for(UInt_t i = 0; i < dataTree->GetEntries(); i++){
dataTree->GetEntry(i);
if(electron_energy_data.mass < 60 || electron_energy_data.mass > 120) continue;
if(cat == 1 && (fabs(electron_energy_data.eta1) > 1.479 || fabs(electron_energy_data.eta2) > 1.479)) continue;
if(cat == 2 && (fabs(electron_energy_data.eta1) < 1.479 || fabs(electron_energy_data.eta2) < 1.479)) continue;
if(electron_energy_data.pt1 < 10 || electron_energy_data.pt2 < 10) continue;
mass.setVal(electron_energy_data.mass);
pt1.setVal(electron_energy_data.pt1);
pt2.setVal(electron_energy_data.pt2);
eta1.setVal(electron_energy_data.eta1);
eta2.setVal(electron_energy_data.eta2);
//wgt.setVal(1);
dataDS->add(treeSet,1);
}
RooPlot* frameData = mass.frame(Bins(120),Range("zrange"));
//RooPlot* frameData = mass.frame(Bins(120),Range(87,97));
mcTree->Draw("mass>>hMC", (basesel+catsel).Data());
hMC = (TH1D*) gPad->GetPrimitive("hMC");
hMC->SetFillColor(kYellow-10);
RooDataSet* mcDS = new RooDataSet("mcDS","",treeSet,"wgt");
mcTree->SetBranchAddress("Events",&electron_energy_data);
cout << "mcDS->isWeighted() = " << mcDS->isWeighted() << endl;
for(UInt_t i = 0; i < mcTree->GetEntries(); i++){
mcTree->GetEntry(i);
if(electron_energy_data.mass < 60 || electron_energy_data.mass > 120) continue;
if(cat == 1 && ((fabs(electron_energy_data.eta1) > 1.479 || fabs(electron_energy_data.eta2) > 1.479))) continue;
if(cat == 2 && ((fabs(electron_energy_data.eta1) < 1.479 || fabs(electron_energy_data.eta2) < 1.479))) continue;
if(electron_energy_data.pt1 < 10 || electron_energy_data.pt2 < 10) continue;
mass.setVal(electron_energy_data.mass);
pt1.setVal(electron_energy_data.pt1);
pt2.setVal(electron_energy_data.pt2);
eta1.setVal(electron_energy_data.eta1);
eta2.setVal(electron_energy_data.eta2);
assert(puweight(electron_energy_data.npu) > -5 && puweight(electron_energy_data.npu) < 1000);
//wgt.setVal(puweight(electron_energy_data.npu));
mcDS->add(treeSet,puweight(electron_energy_data.npu));
}
//for(UInt_t i = 0; i < mcTree->100; i++){
// mcTree->GetEntry(i);
// cout << "mcDS->weight() = " << mcDS->weight() << endl;
//}
//mcDS->addColumn(w::wgt) ;
//dataDS = new RooDataSet("dataDS","",dataDS,treeSet,(basesel+catsel).Data(),"wgt");
//mcDS = new RooDataSet("mcDS","",mcDS,treeSet,(basesel+catsel).Data(),"wgt");
cout << "mcDS->numEntries() = " << mcDS->numEntries() << endl;
cout << "mcDS->sumEntries() = " << mcDS->sumEntries() << endl;
//return;
//mcDS->addColumn(puweightf);
//RooDataSet* mcDSW = new RooDataSet("mcDSW","",*(mcDS->get()),RooFit::Import("MC",*mcDS), RooFit::Index(sample),RooFit::WeightVar("puweightv"));
RooPlot* frameMC = mass.frame(Bins(120),Range("zrange"));
//RooPlot* frameMC = mass.frame(Bins(120),Range(88,98));
RooRealVar numData("numData","", dataDS->sumEntries(),0,10000000.);
RooRealVar numMC("numMC","", mcDS->sumEntries(),0,10000000.);
RooRealVar meanMC("meanMC","",0.0,-5.0,5.0);
meanMC.removeRange();
RooRealVar sigmaMC("sigmaMC","",1.4,0.0,100.0);
sigmaMC.removeRange();
RooRealVar alphaMC("alphaMC","",1.0,0.0,10.0);
alphaMC.removeRange();
RooRealVar nMC("nMC","",1.0,0.0,1000.0);
nMC.removeRange();
const double widthzpdg = 2.4952;
const double masszpdg = 91.1876;
//when you don't give a range
//this automatically makes these roo real vars constant
RooRealVar mz("mz","mz",masszpdg);
RooRealVar widthz("wz","wz",widthzpdg);
RooBreitWigner zbwMC("zbwMC","zbwMC",mass,mz,widthz);
RooCBShape zcbMC("zcbMC","zcbMC",mass,meanMC,sigmaMC,alphaMC,nMC);
RooFFTConvPdf zcbbwMC("zcbbwMC","zcbbwMC",mass,zbwMC,zcbMC);
zcbbwMC.setBufferFraction(2.5);
RooRealVar meanData("meanData","",0.0,-5.0,5.0);
meanData.removeRange();
RooRealVar sigmaData("sigmaData","",1.4,0.0,100.0);
sigmaData.removeRange();
RooRealVar alphaData("alphaData","",1.0,0.0,10.0);
alphaData.removeRange();
RooRealVar nData("nData","",1.0,0.0,1000.0);
nData.removeRange();
RooBreitWigner zbwData("zbwData","zbwData",mass,mz,widthz);
RooCBShape zcbData("zcbData","zcbData",mass,meanData,sigmaData,alphaData,nData);
RooFFTConvPdf zcbbwData("zcbbwData","zcbbwData",mass,zbwData,zcbData);
zcbbwData.setBufferFraction(2.5);
RooDataSet combinedDS("combinedDS","combinedDS", RooArgList(mass,wgt), RooFit::Index(sample), RooFit::Import("Data",*dataDS), RooFit::Import("MC",*mcDS),RooFit::WeightVar(wgt));
//RooDataSet combinedDS("combinedDS","combinedDS", RooArgList(mass,npu), RooFit::Index(sample), RooFit::Import("Data",*dataDS), RooFit::Import("MC",*mcDSW),RooFit::WeightVar("puweightv"));
//RooDataSet combinedDS("combinedDS","combinedDS", RooArgList(mass,npu), RooFit::Import("MC",*mcDSW),RooFit::WeightVar("puweightv"));
cout << "mcDS->numEntries() = " << mcDS->numEntries() << endl;
cout << "mcDS->sumEntries() = " << mcDS->sumEntries() << endl;
cout << "dataDS->numEntries() = " << dataDS->numEntries() << endl;
cout << "dataDS->sumEntries() = " << dataDS->sumEntries() << endl;
//RooDataSet * combinedDS = (RooDataSet*)mcDSW->Clone();
//combinedDS->append(*dataDS);
cout << "combinedDS.numEntries() = " << combinedDS.numEntries() << endl;
cout << "combinedDS.sumEntries() = " << combinedDS.sumEntries() << endl;
//return;
// PDF for simultaneous fit
RooSimultaneous totalPdf("totalPdf","totalPdf", sample);
totalPdf.addPdf(zcbbwData,"Data");
totalPdf.addPdf(zcbbwMC,"MC");
RooFormulaVar delE("delE","(meanData-meanMC)/(mz+meanMC)", RooArgList(meanData,meanMC,mz));
RooFormulaVar delS2("delS2","2*(sigmaData*sigmaData-sigmaMC*sigmaMC)/(mz+meanMC)/(mz+meanMC)", RooArgList(sigmaData,sigmaMC,mz,meanMC));
RooFitResult* fitR_mc = zcbbwMC.fitTo(*mcDS, RooFit::Save(),RooFit::Strategy(1),RooFit::SumW2Error(kFALSE));
cout << "alphaMC.getVal() = " << alphaMC.getVal() << endl;
cout << "nMC.getVal() = " << nMC.getVal() << endl;
alphaData.setVal(alphaMC.getVal());
nData.setVal(nMC.getVal());
alphaData.setConstant();
nData.setConstant();
//return;
//RooFitResult* fitR_data = zcbbwData.fitTo(*dataDS, RooFit::Save(),RooFit::Strategy(2),RooFit::SumW2Error(kFALSE));
RooFitResult* fitR = totalPdf.fitTo(combinedDS, RooFit::Save(true),RooFit::Strategy(2),RooFit::SumW2Error(false));
TCanvas *czfit = new TCanvas("fitcanvas","",1200,600);
czfit->Divide(2,1);
czfit->cd(1);
mcDS->plotOn(frameMC);
//totalPdf.plotOn(frameMC,Slice(sample,"MC"),ProjWData(sample,combinedDS),RooFit::LineColor(kBlue+2)) ;
zcbbwMC.plotOn(frameMC,RooFit::LineColor(kBlue+2));
frameMC->SetTitle("MC");
frameMC->GetYaxis()->SetLabelSize(0.03);
frameMC->GetYaxis()->SetTitleOffset(1.3);
frameMC->Draw();
TLegend *legmc = new TLegend(0.72,0.75,0.92,0.9);
legmc->AddEntry(frameMC->getObject(0),"MC","LPE");
legmc->AddEntry(frameMC->getObject(1),"Fit to MC","L");
legmc->SetBorderSize(0);
legmc->SetFillStyle(0);
legmc->Draw();
text->Draw();
double fitalpha = alphaMC.getVal();
double fitn = nMC.getVal();
//alphaData.setVal(fitalpha);
//nData.setVal(fitn);
//alphaData.setConstant();
//nData.setConstant();
//RooFitResult* fitR_data = zcbbwData.fitTo(*dataDS, RooFit::Save(),RooFit::Strategy(2),RooFit::SumW2Error(kFALSE));
cout << "meanData.getVal() = " << meanData.getVal() << endl;
cout << "meanMC.getVal() = " << meanMC.getVal() << endl;
czfit->cd(2);
dataDS->plotOn(frameData);
//totalPdf.plotOn(frameData,Slice(sample,"Data"),ProjWData(sample,combinedDS),RooFit::LineColor(kRed+2)) ;
zcbbwData.plotOn(frameData,RooFit::LineColor(kRed+2));
frameData->GetYaxis()->SetLabelSize(0.03);
frameData->GetYaxis()->SetTitle("");
frameData->SetTitle("Data");
//zcbbwData.paramOn(frameData,Layout(0.2,0.5,0.9));
//frameData->getAttText()->SetTextSize(0.025);
frameData->Draw();
TLegend *legdata = new TLegend(0.62,0.75,0.92,0.9);
legdata->AddEntry(frameData->getObject(0),"Data, Full2012","LPE");
legdata->AddEntry(frameData->getObject(1),"Fit to Data","L");
legdata->SetBorderSize(0);
legdata->SetFillStyle(0);
legdata->Draw();
text->Draw();
TString plotname = TString("zfit") + label + TString("_Full2012.png");
czfit->SaveAs(plotname);
double sM = delS2.getVal();
double cbsigmc = sigmaMC.getVal();
double cbsigdata = sigmaData.getVal();
double cbsigmcerr = sigmaMC.getError();
double cbsigdataerr = sigmaData.getError();
double cbsmear = sqrt(cbsigdata*cbsigdata-cbsigmc*cbsigmc);
double cbsmearerr = (1.0/cbsmear)*sqrt(cbsigdata*cbsigdata*cbsigdataerr*cbsigdataerr + cbsigmc*cbsigmc*cbsigmcerr*cbsigmcerr);
double emc = masszpdg+meanMC.getVal();
double emcerr = meanMC.getError();
double edataerr = meanData.getError();
double delEerr = sqrt(edataerr*edataerr*emc*emc + emcerr*emcerr*(emc+1)*(emc+1));
double sMerr = (1.0/cbsmear*emc*emc)*sqrt(2*(emc*emc*(cbsigdata*cbsigdata*cbsigdataerr*cbsigdataerr + cbsigmc*cbsigmc*cbsigmcerr*cbsigmcerr) + 2*cbsmear*cbsmear*cbsmear*cbsmear*emcerr*emcerr));
cout << "delE.getVal() = (meanData-meanMC)/(mz+meanMC) = " << delE.getVal() << endl;
cout << "delE.getPropagatedError(*fitR) = " << delE.getPropagatedError(*fitR) << endl;
double sME = TMath::Sqrt(TMath::Abs(delS2.getPropagatedError(*fitR)));
cout << "sM = abs(2*(sigmaData*sigmaData-sigmaMC*sigmaMC)/(mz+meanMC)/(mz+meanMC)) = " << sM << endl;
cout << "sME = " << sME << endl;
cout << "cbsmear = sqrt(cbsigdata*cbsigdata-cbsigmc*cbsigmc) = " << cbsmear << endl;
cout << "cmsmearerr = " << cbsmearerr << endl;
cout << "alphaData.getVal() = " << alphaData.getVal() << endl;
cout << "alphaMC.getVal() = " << alphaMC.getVal() << endl;
cout << "nData.getVal() = " << nData.getVal() << endl;
cout << "nMC.getVal() = " << nMC.getVal() << endl;
cout << "sigmaData.getVal() = " << sigmaData.getVal() << endl;
cout << "sigmaMC.getVal() = " << sigmaMC.getVal() << endl;
cout << "meanData.getVal() = " << meanData.getVal() << endl;
cout << "meanMC.getVal() = " << meanMC.getVal() << endl;
cout << "mz.getVal() = " << mz.getVal() << endl;
cout << "widthz.getVal() = " << widthz.getVal() << endl;
}