-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmergeParSub_rooKeys.cc
143 lines (119 loc) · 6.01 KB
/
mergeParSub_rooKeys.cc
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
#include "RooRealVar.h"
#include "RooDataSet.h"
#include "TCanvas.h"
#include "TAxis.h"
#include "TMath.h"
using namespace RooFit ;
using namespace std ;
// Use this flag to avoid writing a KDE histogram when there are failed parallel jobs
// (the efficiency would be correct in any case and could be used in a fit, but the result is reproducible only revoving exactly the same file(s))
bool safeFilling = true;
static const int nBins = 9;
// q2-bin format: [0-8] for one bin
// [-1] for each bin recursively
// effIndx format: [0] GEN no-filter
// [1] GEN filtered
// [2] GEN filtered from full MC sample
// [3] correct-tag RECO candidates
// [4] wrong-tag RECO candidates
// [5] RECO candidates
// [-1] for each effIndx recursively
// parity format: [0] even
// [1] odd
// [-1] for each parity recursively
void mergeParSub_rooKeysBin(int q2Bin, int effIndx, int parity, float widthCTK, float widthCTL, float widthPHI, int xbins, int ybins, int zbins, int totdiv, int year)
{
string shortString = Form("b%ie%ip%i",q2Bin,effIndx,parity);
cout<<"Conf: "<<shortString<<endl;
// string containing the path and begin of the filename of output from parallel jobs
string confString = Form("tmp/KDEhist_%s_rooKeys_m_w0-%.2f_w1-%.2f_w2-%.2f_%i_%i_%i",shortString.c_str(),widthCTK,widthCTL,widthPHI,xbins,ybins,zbins);
// full histogram to fill
TH3D* KDEhist = new TH3D(Form("KDEhist_%s",shortString.c_str()),Form("KDEhist_%s",shortString.c_str()),xbins,-1,1,ybins,-1,1,zbins,-TMath::Pi(),TMath::Pi());
KDEhist->Sumw2();
// import partial histograms
string inFileName;
int goodHistCnt = 0;
for (int ndiv=0; ndiv<totdiv; ++ndiv) {
// add final part of filename and open file
inFileName = confString+Form("_%i-frac-%i_%i.root",ndiv,totdiv,year);
TFile* fin = TFile::Open( inFileName.c_str() );
if ( !fin || !fin->IsOpen() ) {
cout<<"File not found: "<<inFileName<<endl;
continue;
}
// extract and check output histogram from parallel job
TH3D* partHist = (TH3D*)fin->Get(("KDEhist_"+shortString+"__ctK_ctL_phi").c_str());
if ( !partHist || partHist->IsZombie() ) {
cout<<"Histogram not found in file: "<<inFileName<<endl;
continue;
}
// add it to the full histogram
KDEhist->Add(partHist);
// count partial histogram successfully merged
++goodHistCnt;
delete partHist;
fin->Close();
delete fin;
}
if ( goodHistCnt!=totdiv ) {
// if not all the partial histograms are found, give a warning and correct the normalisation
// (or make the macro fail, in case the flag is activated)
cout<<"Warning! Not all partial histograms found: "<<goodHistCnt<<" of "<<totdiv<<endl;
if (safeFilling) return;
KDEhist->Scale(1.0*totdiv/goodHistCnt);
}
// check histogram against empty or negative bin contents, which would make the fit fail
// (this is not expected from KDE description and should never happen,
// since the tails of gaussian kernels are always at values greater than zero)
double minVal = KDEhist->GetMinimum();
if ( minVal<=0 ) {
cout<<"Histogram has empty bins, this is bad. Abort!"<<endl;
return;
}
// save histograms in files
// to facilitate plotting same terms with different configurations (SF and sampling bins) for comparisons, thay are saved in the same file
// to reduce the number of files produced to ~10/20, different terms of the same efficiency are saved in the same file
// (this also allows a single extractEff call to access a single file)
TFile* fout = TFile::Open( Form((parity==0?"files/KDEhist_b%i_ev.root":"files/KDEhist_b%i_od_%i.root"),q2Bin,year), "UPDATE" );
KDEhist->Write( Form("hist_indx%i_w0-%1.2f_w1-%1.2f_w2-%1.2f_%i_%i_%i",effIndx,widthCTK,widthCTL,widthPHI,xbins,ybins,zbins), TObject::kWriteDelete );
fout->Close();
// Remind user to delete partial files
cout<<endl<<"Please, remove partial files running:\nrm "<<confString<<Form("_*-frac-%i_%i.root",totdiv,year)<<endl<<endl;
}
void mergeParSub_rooKeysBin2(int q2Bin, int effIndx, int parity, float widthCTK, float widthCTL, float widthPHI, int xbins, int ybins, int zbins, int totdiv, int year)
{
if ( parity==-1 )
for (parity=0; parity<2; ++parity)
mergeParSub_rooKeysBin(q2Bin, effIndx, parity, widthCTK, widthCTL, widthPHI, xbins, ybins, zbins, totdiv, year);
else
mergeParSub_rooKeysBin(q2Bin, effIndx, parity, widthCTK, widthCTL, widthPHI, xbins, ybins, zbins, totdiv, year);
}
void mergeParSub_rooKeysBin1(int q2Bin, int effIndx, int parity, float widthCTK, float widthCTL, float widthPHI, int xbins, int ybins, int zbins, int totdiv, int year)
{
if ( effIndx==-1 )
for (effIndx=0; effIndx<6; ++effIndx)
mergeParSub_rooKeysBin2(q2Bin, effIndx, parity, widthCTK, widthCTL, widthPHI, xbins, ybins, zbins, totdiv, year);
else
mergeParSub_rooKeysBin2(q2Bin, effIndx, parity, widthCTK, widthCTL, widthPHI, xbins, ybins, zbins, totdiv, year);
}
void mergeParSub_rooKeys(int q2Bin = -1, int effIndx = -1, int parity = -1, float widthCTK = 0.3, float widthCTL = 0.3, float widthPHI = 0.3, int xbins=50, int ybins = 50, int zbins = 50, int totdiv = 50, int year = 2016)
{
if ( q2Bin<-1 || q2Bin>=nBins ) return;
if ( effIndx<-1 || effIndx>5 ) return;
if ( parity<-1 || parity>1 ) return;
if ( widthCTK<=0 ) return;
if ( widthCTL<=0 ) return;
if ( widthPHI<=0 ) return;
if ( xbins<1 ) return;
if ( ybins<1 ) ybins = xbins;
if ( zbins<1 ) zbins = xbins;
if ( totdiv<1 ) return;
if ( q2Bin==-1 ) cout<<"Running all the q2 bins"<<endl;
if ( effIndx==-1 ) cout<<"Running all the efficiency terms"<<endl;
if ( parity==-1 ) cout<<"Running both the parity datasets"<<endl;
if ( q2Bin==-1 )
for (q2Bin=0; q2Bin<nBins; ++q2Bin)
mergeParSub_rooKeysBin1(q2Bin, effIndx, parity, widthCTK, widthCTL, widthPHI, xbins, ybins, zbins, totdiv, year);
else
mergeParSub_rooKeysBin1(q2Bin, effIndx, parity, widthCTK, widthCTL, widthPHI, xbins, ybins, zbins, totdiv, year);
}