Skip to content

Commit ad0ce2a

Browse files
committedMay 6, 2022
Improvement of PU weight procedure
1 parent 2e27878 commit ad0ce2a

File tree

4 files changed

+1581
-1574
lines changed

4 files changed

+1581
-1574
lines changed
 

‎NanoCORE/Tools/puWeight.cc

+20-21
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,38 @@
11
#include "puWeight.h"
22

3-
double get_puWeight(const unsigned int nTrueInt, const std::string year, const std::string var="central")
3+
double get_puWeight(const unsigned int nTrueInt, const TString year, const TString var="central")
44
{
5-
setWeights();
65
unsigned int npu = nTrueInt;
76
if ( npu < 0 ) npu = 0;
8-
if ( npu >= puweight_central_2018.size() ) npu = puweight_central_2018.size()-1;
9-
if ( var.compare("central")==0 ) {
10-
if ( year.compare("2016nonAPV")==0 ) return puweight_central_2016nonAPV.at(npu);
11-
else if ( year.compare("2016APV")==0 ) return puweight_central_2016APV.at(npu);
12-
else if ( year.compare("2017")==0 ) return puweight_central_2017.at(npu);
13-
else if ( year.compare("2018")==0 ) return puweight_central_2018.at(npu);
14-
else if ( year.compare("2018B")==0 ) return puweight_central_2018B.at(npu);
7+
if ( npu >= nPUbins ) npu = nPUbins-1;
8+
if ( var=="central" ) {
9+
if ( year=="2016nonAPV" ) return puweight_central_2016nonAPV[npu];
10+
else if ( year=="2016APV" ) return puweight_central_2016APV[npu];
11+
else if ( year=="2017" ) return puweight_central_2017[npu];
12+
else if ( year=="2018" ) return puweight_central_2018[npu];
13+
else if ( year=="2018B" ) return puweight_central_2018B[npu];
1514
else {
1615
std::cout << "WARNING: unknown year, returning unity piluep weight!" << std::endl;
1716
return 1.0;
1817
}
1918
}
20-
else if ( var.compare("up")==0 ) {
21-
if ( year.compare("2016nonAPV")==0 ) return puweight_up_2016nonAPV.at(npu);
22-
else if ( year.compare("2016APV")==0 ) return puweight_up_2016APV.at(npu);
23-
else if ( year.compare("2017")==0 ) return puweight_up_2017.at(npu);
24-
else if ( year.compare("2018")==0 ) return puweight_up_2018.at(npu);
25-
else if ( year.compare("2018B")==0 ) return puweight_up_2018B.at(npu);
19+
else if ( var=="up" ) {
20+
if ( year=="2016nonAPV" ) return puweight_up_2016nonAPV[npu];
21+
else if ( year=="2016APV" ) return puweight_up_2016APV[npu];
22+
else if ( year=="2017" ) return puweight_up_2017[npu];
23+
else if ( year=="2018" ) return puweight_up_2018[npu];
24+
else if ( year=="2018B" ) return puweight_up_2018B[npu];
2625
else {
2726
std::cout << "WARNING: unknown year, returning unity piluep weight!" << std::endl;
2827
return 1.0;
2928
}
3029
}
31-
else if ( var.compare("down")==0 ) {
32-
if ( year.compare("2016nonAPV")==0 ) return puweight_down_2016nonAPV.at(npu);
33-
else if ( year.compare("2016APV")==0 ) return puweight_down_2016APV.at(npu);
34-
else if ( year.compare("2017")==0 ) return puweight_down_2017.at(npu);
35-
else if ( year.compare("2018")==0 ) return puweight_down_2018.at(npu);
36-
else if ( year.compare("2018B")==0 ) return puweight_down_2018B.at(npu);
30+
else if ( var=="down" ) {
31+
if ( year=="2016nonAPV" ) return puweight_down_2016nonAPV[npu];
32+
else if ( year=="2016APV" ) return puweight_down_2016APV[npu];
33+
else if ( year=="2017" ) return puweight_down_2017[npu];
34+
else if ( year=="2018" ) return puweight_down_2018[npu];
35+
else if ( year=="2018B" ) return puweight_down_2018B[npu];
3736
else {
3837
std::cout << "WARNING: unknown year, returning unity piluep weight!" << std::endl;
3938
return 1.0;

‎NanoCORE/Tools/puWeight.h

+1,508-1,505
Large diffs are not rendered by default.

‎cpp/ScanChain_Zp.C

+9-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "../NanoCORE/Tools/puWeight.h"
2222

2323
#include <iostream>
24-
#include <string>
2524
#include <iomanip>
2625
#include <sys/stat.h>
2726

@@ -47,7 +46,7 @@ bool useTuneP = true;
4746
bool usePuppiMET = false;
4847
bool removeSpikes = true;
4948
bool removeDataDuplicates = false;
50-
bool applyTopPtWeight = false;
49+
bool applyTopPtWeight = true;
5150
bool applyPUWeight = true;
5251
bool varyPUWeightUp = false;
5352
bool varyPUWeightDown = false;
@@ -628,6 +627,9 @@ int ScanChain(TChain *ch, double genEventSumw, TString year, TString process) {
628627
TIter fileIter(listOfFiles);
629628
tqdm bar;
630629

630+
if ( applyPUWeight )
631+
set_puWeights();
632+
631633
while ( (currentFile = (TFile*)fileIter.Next()) ) {
632634
TFile *file = TFile::Open( currentFile->GetTitle() );
633635
TTree *tree = (TTree*)file->Get("Events");
@@ -670,11 +672,11 @@ int ScanChain(TChain *ch, double genEventSumw, TString year, TString process) {
670672
weight *= nt.L1PreFiringWeight_Muon_Nom();
671673

672674
if ( applyPUWeight ) {
673-
int nTrueInt = nt.Pileup_nTrueInt();
674-
std::string whichPUWeight = "central";
675+
unsigned int nTrueInt = nt.Pileup_nTrueInt();
676+
TString whichPUWeight = "central";
675677
if ( varyPUWeightUp ) whichPUWeight = "up";
676678
else if ( varyPUWeightDown ) whichPUWeight = "down";
677-
std::string puyear = year.Data();
679+
TString puyear = year;
678680
if ( useOnlyRun2018B ) puyear = "2018B";
679681
weight *= get_puWeight(nTrueInt, puyear, whichPUWeight);
680682
}
@@ -1750,8 +1752,9 @@ int ScanChain(TChain *ch, double genEventSumw, TString year, TString process) {
17501752
}
17511753
}
17521754
}
1753-
1755+
17541756
fout->Write();
17551757
fout->Close();
1758+
17561759
return 0;
17571760
}

‎python/make_puWeight.py

+44-42
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,34 @@ def print_header(fout):
66
fout.write('#ifndef PUWEIGHT_H\n')
77
fout.write('#define PUWEIGHT_H\n')
88
fout.write('\n')
9-
fout.write('#include<vector>\n')
10-
fout.write('#include<iostream>\n')
11-
fout.write('#include<string>\n')
9+
fout.write('#include <iostream>\n')
10+
fout.write('#include <TString.h>\n')
1211
fout.write('\n')
13-
fout.write('std::vector<double> puweight_central_2016nonAPV = { };\n')
14-
fout.write('std::vector<double> puweight_central_2016APV = { };\n')
15-
fout.write('std::vector<double> puweight_central_2017 = { };\n')
16-
fout.write('std::vector<double> puweight_central_2018 = { };\n')
17-
fout.write('std::vector<double> puweight_central_2018B = { };\n')
18-
fout.write('std::vector<double> puweight_up_2016nonAPV = { };\n')
19-
fout.write('std::vector<double> puweight_up_2016APV = { };\n')
20-
fout.write('std::vector<double> puweight_up_2017 = { };\n')
21-
fout.write('std::vector<double> puweight_up_2018 = { };\n')
22-
fout.write('std::vector<double> puweight_up_2018B = { };\n')
23-
fout.write('std::vector<double> puweight_down_2016nonAPV = { };\n')
24-
fout.write('std::vector<double> puweight_down_2016APV = { };\n')
25-
fout.write('std::vector<double> puweight_down_2017 = { };\n')
26-
fout.write('std::vector<double> puweight_down_2018 = { };\n')
27-
fout.write('std::vector<double> puweight_down_2018B = { };\n')
12+
fout.write('#define nPUbins 99\n')
13+
fout.write('\n')
14+
fout.write('using namespace std;\n')
15+
fout.write('\n')
16+
fout.write('double *puweight_central_2016nonAPV = new double[nPUbins];\n')
17+
fout.write('double *puweight_central_2016APV = new double[nPUbins];\n')
18+
fout.write('double *puweight_central_2017 = new double[nPUbins];\n')
19+
fout.write('double *puweight_central_2018 = new double[nPUbins];\n')
20+
fout.write('double *puweight_central_2018B = new double[nPUbins];\n')
21+
fout.write('double *puweight_up_2016nonAPV = new double[nPUbins];\n')
22+
fout.write('double *puweight_up_2016APV = new double[nPUbins];\n')
23+
fout.write('double *puweight_up_2017 = new double[nPUbins];\n')
24+
fout.write('double *puweight_up_2018 = new double[nPUbins];\n')
25+
fout.write('double *puweight_up_2018B = new double[nPUbins];\n')
26+
fout.write('double *puweight_down_2016nonAPV = new double[nPUbins];\n')
27+
fout.write('double *puweight_down_2016APV = new double[nPUbins];\n')
28+
fout.write('double *puweight_down_2017 = new double[nPUbins];\n')
29+
fout.write('double *puweight_down_2018 = new double[nPUbins];\n')
30+
fout.write('double *puweight_down_2018B = new double[nPUbins];\n')
2831
fout.write('\n')
2932

3033
def print_function_h(fout):
31-
fout.write('double get_puWeight(const unsigned int nTrueInt, const std::string year, const std::string var);\n')
34+
fout.write('double get_puWeight(const unsigned int nTrueInt, const TString year, const TString var);\n')
3235
fout.write('\n')
33-
fout.write('inline void setWeights() {\n')
36+
fout.write('inline void set_puWeights() {\n')
3437

3538
def print_footer(fout):
3639
fout.write('}\n')
@@ -40,40 +43,39 @@ def print_footer(fout):
4043
def print_cc(fout,fname):
4144
fout.write('#include "%s.h"\n'%fname)
4245
fout.write('\n')
43-
fout.write('double get_puWeight(const unsigned int nTrueInt, const std::string year, const std::string var="central")\n')
46+
fout.write('double get_puWeight(const unsigned int nTrueInt, const TString year, const TString var="central")\n')
4447
fout.write('{\n')
45-
fout.write('\tsetWeights();\n')
4648
fout.write('\tunsigned int npu = nTrueInt;\n')
4749
fout.write('\tif ( npu < 0 ) npu = 0;\n')
48-
fout.write('\tif ( npu >= puweight_central_2018.size() ) npu = puweight_central_2018.size()-1;\n')
49-
fout.write('\tif ( var.compare("central")==0 ) {\n')
50-
fout.write('\t\tif ( year.compare("2016nonAPV")==0 ) return puweight_central_2016nonAPV.at(npu);\n')
51-
fout.write('\t\telse if ( year.compare("2016APV")==0 ) return puweight_central_2016APV.at(npu);\n')
52-
fout.write('\t\telse if ( year.compare("2017")==0 ) return puweight_central_2017.at(npu);\n')
53-
fout.write('\t\telse if ( year.compare("2018")==0 ) return puweight_central_2018.at(npu);\n')
54-
fout.write('\t\telse if ( year.compare("2018B")==0 ) return puweight_central_2018B.at(npu);\n')
50+
fout.write('\tif ( npu >= nPUbins ) npu = nPUbins-1;\n')
51+
fout.write('\tif ( var=="central" ) {\n')
52+
fout.write('\t\tif ( year=="2016nonAPV" ) return puweight_central_2016nonAPV[npu];\n')
53+
fout.write('\t\telse if ( year=="2016APV" ) return puweight_central_2016APV[npu];\n')
54+
fout.write('\t\telse if ( year=="2017" ) return puweight_central_2017[npu];\n')
55+
fout.write('\t\telse if ( year=="2018" ) return puweight_central_2018[npu];\n')
56+
fout.write('\t\telse if ( year=="2018B" ) return puweight_central_2018B[npu];\n')
5557
fout.write('\t\telse {\n')
5658
fout.write('\t\t\tstd::cout << "WARNING: unknown year, returning unity piluep weight!" << std::endl;\n')
5759
fout.write('\t\t\treturn 1.0;\n')
5860
fout.write('\t\t}\n')
5961
fout.write('\t}\n')
60-
fout.write('\telse if ( var.compare("up")==0 ) {\n')
61-
fout.write('\t\tif ( year.compare("2016nonAPV")==0 ) return puweight_up_2016nonAPV.at(npu);\n')
62-
fout.write('\t\telse if ( year.compare("2016APV")==0 ) return puweight_up_2016APV.at(npu);\n')
63-
fout.write('\t\telse if ( year.compare("2017")==0 ) return puweight_up_2017.at(npu);\n')
64-
fout.write('\t\telse if ( year.compare("2018")==0 ) return puweight_up_2018.at(npu);\n')
65-
fout.write('\t\telse if ( year.compare("2018B")==0 ) return puweight_up_2018B.at(npu);\n')
62+
fout.write('\telse if ( var=="up" ) {\n')
63+
fout.write('\t\tif ( year=="2016nonAPV" ) return puweight_up_2016nonAPV[npu];\n')
64+
fout.write('\t\telse if ( year=="2016APV" ) return puweight_up_2016APV[npu];\n')
65+
fout.write('\t\telse if ( year=="2017" ) return puweight_up_2017[npu];\n')
66+
fout.write('\t\telse if ( year=="2018" ) return puweight_up_2018[npu];\n')
67+
fout.write('\t\telse if ( year=="2018B" ) return puweight_up_2018B[npu];\n')
6668
fout.write('\t\telse {\n')
6769
fout.write('\t\t\tstd::cout << "WARNING: unknown year, returning unity piluep weight!" << std::endl;\n')
6870
fout.write('\t\t\treturn 1.0;\n')
6971
fout.write('\t\t}\n')
7072
fout.write('\t}\n')
71-
fout.write('\telse if ( var.compare("down")==0 ) {\n')
72-
fout.write('\t\tif ( year.compare("2016nonAPV")==0 ) return puweight_down_2016nonAPV.at(npu);\n')
73-
fout.write('\t\telse if ( year.compare("2016APV")==0 ) return puweight_down_2016APV.at(npu);\n')
74-
fout.write('\t\telse if ( year.compare("2017")==0 ) return puweight_down_2017.at(npu);\n')
75-
fout.write('\t\telse if ( year.compare("2018")==0 ) return puweight_down_2018.at(npu);\n')
76-
fout.write('\t\telse if ( year.compare("2018B")==0 ) return puweight_down_2018B.at(npu);\n')
73+
fout.write('\telse if ( var=="down" ) {\n')
74+
fout.write('\t\tif ( year=="2016nonAPV" ) return puweight_down_2016nonAPV[npu];\n')
75+
fout.write('\t\telse if ( year=="2016APV" ) return puweight_down_2016APV[npu];\n')
76+
fout.write('\t\telse if ( year=="2017" ) return puweight_down_2017[npu];\n')
77+
fout.write('\t\telse if ( year=="2018" ) return puweight_down_2018[npu];\n')
78+
fout.write('\t\telse if ( year=="2018B" ) return puweight_down_2018B[npu];\n')
7779
fout.write('\t\telse {\n')
7880
fout.write('\t\t\tstd::cout << "WARNING: unknown year, returning unity piluep weight!" << std::endl;\n')
7981
fout.write('\t\t\treturn 1.0;\n')
@@ -116,7 +118,7 @@ def get_weights(fout, year = "2018", var = "central", mcsample="ttbar"):
116118

117119
for b in range(1, hdata.GetNbinsX()+1):
118120
tw = hdata.GetBinContent(b);
119-
fout.write("\t"+wname+".push_back("+str(tw)+");\n")
121+
fout.write("\t"+wname+"["+str(b-1)+"] = "+str(tw)+";\n")
120122
fout.write("\n")
121123

122124
indir = "./data/"

0 commit comments

Comments
 (0)
Please sign in to comment.