forked from amissert/atmFitTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistoFactory.cxx
367 lines (320 loc) · 10.9 KB
/
histoFactory.cxx
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
#include "histoFactory.h"
/////////////////////////////////////////////////
//construct from parameter file
histoFactory::histoFactory(const char* parfile, bool separateneutmode)
: separateNeutMode(separateneutmode)
{
//read in parameters
runpars = new sharedPars(parfile);
runpars->readParsFromFile();
parFileName = parfile;
//setup factory
nameTag = runpars->globalRootName;
// nameTag.Append("_histograms.root");
nSamples = runpars->nSamples;
nComponents = runpars->nComponents;
nAttributes = runpars->nAttributes;
nBins = runpars->nFVBins;
if (separateNeutMode) {
nModes = NMODE;
hManager = new histoManager(nSamples,nBins,nComponents,nameTag.Data(), nModes, separateNeutMode);
} else {
nModes = 0;
hManager = new histoManager(nSamples,nBins,nComponents,nameTag.Data());
}
}
////////////////////////////////////////////////////////////////
//run histogram factory using the parameter file
void histoFactory::runHistoFactory(){
//setup trees and chains for data and mc
cout<<"histoFactory: Setting up chains for data and MC.."<<endl;
TChain *chmc = new TChain("h1");
TChain *chdata = new TChain("h1");
chmc->Add(runpars->hFactoryMCFiles.Data());
setMCTree((TChain*)chmc);
chdata->Add(runpars->hFactoryDataFiles.Data());
setDataTree((TChain*)chdata);
cout<<"histoFactory: Initializing histograms..."<<endl;
//initialize histograms
init();
//loop over events and fill histograms
cout<<"histoFactory: Filling all histograms"<<endl;
fillHistos();
//normalize all of the histograms
normalizeHistos();
cout<<"histoFactory: Saving histograms"<<endl;
//save all filled histograms
saveToFile();
chmc->Delete();
chdata->Delete();
///////////////////
return;
}
////////////////////////////////////////////////////////////////
//constructor to re-created a histogram factory from a file
histoFactory::histoFactory(int nsampl,int nbins,int ncomp,const char* name){
nameTag = name;
nameTag.Append("_hFactoryOutput");
nSamples = nsampl;
nComponents = ncomp;
nAttributes = 0;
nBins = nbins;
//create an empty histogram manager
hManager = new histoManager(nsampl,nbins,ncomp,name);
return;
}
histoFactory::histoFactory(int nsampl, int nbins, int ncomp, int nmode, bool separateneutmode, const std::string name)
: nSamples(nsampl)
, nComponents(ncomp)
, nBins(nbins)
, nModes(nmode)
, separateNeutMode(separateneutmode)
{
nameTag = name.c_str();
nameTag.Append("_hFactoryOutPut");
nAttributes = 0;
hManager = new histoManager(nsampl, nbins, ncomp, name.c_str(), nmode, separateneutmode);
}
//void histoFactory::addAttribute(int iatt){
//adds an attribute to the attribute list
//the attribute type is a code for the fiTQun output being used
//this code is used to determine histogram binning and names
//list of codes:
//0 - e/mu likelihood ratio for subev 1
//1 - e/mu likelihood ratio for subev 2
//
// attType[nAttributes]=iatt;
// nAttributes++;
// return;
//}
/////////////////////////////////////////////////////////////////////////////
//builds all new histograms for data and MC. Call after all attributes are specified
void histoFactory::init(){
//////////////////////////////////////
//setup name for output file
// if (!outputFileName.CompareTo("")){
// outputFileName = "histoFactoryOutput.root";
// }
// outputFileName = runpars->hFactoryOuputDir.Data();
outputFileName = runpars->hFactoryOutput.Data();
// outputFileName.Append( nameTag.Data() );
fout = new TFile(outputFileName.Data(),"RECREATE");
///////////////////////////////////////////
//make sure to set sumw2 to proporly calculate errors (?)
TH1D* hsetsum = new TH1D();
hsetsum->SetDefaultSumw2(kTRUE);
if (!separateNeutMode) {
////////////////////////////////////////
//setup data histos
for (int isamp=0;isamp<nSamples;isamp++){
for (int ibin=0;ibin<nBins;ibin++){
for (int iatt=0;iatt<nAttributes;iatt++){
TString hname = "hdata_";
hname.Append(Form("samp%d_bin%d_att%d",isamp,ibin,iatt));
//cout<<"Making histogram: "<<hname.Data()<<endl;
hManager->setHistogram(isamp,ibin,0,iatt,1,getHistogramData(iatt,hname.Data()));
}
}
}
//setup mc histos
for (int isamp=0;isamp<nSamples;isamp++){
for (int ibin=0;ibin<nBins;ibin++){
for (int icomp=0;icomp<nComponents;icomp++){
for (int iatt=0;iatt<nAttributes;iatt++){
TString hname = "hmc_";
hname.Append(Form("samp%d_bin%d_comp%d_att%d",isamp,ibin,icomp,iatt));
// cout<<"Making histogram: "<<hname.Data()<<endl;
hManager->setHistogram(isamp,ibin,icomp,iatt,0,getHistogram(iatt,hname.Data()));
}
}
}
}
} else {
// setup data histos
for (int isamp=0;isamp<nSamples;isamp++){
for (int ibin=0;ibin<nBins;ibin++){
for (int iatt = 0; iatt < nAttributes; ++iatt) {
TString hname = "hdata_";
hname.Append(Form("samp%d_bin%d_att%d",isamp,ibin,iatt));
//cout<<"Making histogram: "<<hname.Data()<<endl;
hManager->setHistogram(isamp,ibin,0,iatt,1,getHistogramData(iatt,hname.Data()));
}
}
}
//setup mc histos
for (int isamp=0;isamp<nSamples;isamp++){
for (int ibin=0;ibin<nBins;ibin++){
for (int icomp=0;icomp<nComponents;icomp++){
for (int imode = 0; imode < nModes; ++imode) {
for (int iatt=0;iatt<nAttributes;iatt++){
TString hname = "hmc_";
hname.Append(Form("samp%d_bin%d_comp%d_mode%d_att%d",isamp,ibin,icomp,imode,iatt));
// cout<<"Making histogram: "<<hname.Data()<<endl;
hManager->setHistogram(isamp,ibin,icomp,imode,iatt,0,getHistogram(iatt,hname.Data()));
hname.Append("nom");
hManager->setNominalHistogram(isamp,ibin,icomp,imode,iatt,getHistogram(iatt,hname.Data()));
}
}
}
}
}
}
return;
}
////////////////////////////////////////////////////////////////////
//use this function to build histograms
TH1D* histoFactory::getHistogramData(int iatt, const char* thename){
//setup binning
TString parname = Form("nBinsAtt%d",iatt);
int nbins = runpars->getParI(parname.Data());
parname = Form("xMinAtt%d",iatt);
double xmin = runpars->getParD(parname.Data());
parname = Form("xMaxAtt%d",iatt);
double xmax = runpars->getParD(parname.Data());
//histogram building
cout<<"histoFactory: Creating histogram: "<<endl;
cout<<" name: "<<thename<<endl;
cout<<" nbins: "<<nbins<<endl;
cout<<" xmin: "<<xmin<<endl;
cout<<" xmax: "<<xmax<<endl;
TH1D* hnew = new TH1D(thename,thename,nbins,xmin,xmax);
// int nBinsNllEMu = 50;
// if (iatt==0){
// hnew = new TH1D(thename,thename,nBinsNllEMu,-3000,6000);
// }
// if (iatt==1){
// hnew = new TH1D(thename,thename,nBinsNllEMu,-3000,6000);
// }
return hnew;
}
TH1D* histoFactory::getHistogram(int iatt, const char* thename){
//setup binning
TString parname = Form("nBinsAtt%d",iatt);
int nbins = runpars->getParI(parname.Data());
parname = Form("xMinAtt%d",iatt);
double xmin = runpars->getParD(parname.Data());
parname = Form("xMaxAtt%d",iatt);
double xmax = runpars->getParD(parname.Data());
//histogram building
cout<<"histoFactory: Creating histogram: "<<endl;
cout<<" name: "<<thename<<endl;
cout<<" nbins: "<<nbins<<endl;
cout<<" xmin: "<<xmin<<endl;
cout<<" xmax: "<<xmax<<endl;
TH1D* hnew = new TH1D(thename,thename,nbins,xmin,xmax);
// int nBinsNllEMu = 50;
// if (iatt==0){
// hnew = new TH1D(thename,thename,nBinsNllEMu,-3000,6000);
// }
// if (iatt==1){
// hnew = new TH1D(thename,thename,nBinsNllEMu,-3000,6000);
// }
return hnew;
// int nBinsNllEMu = 50;
// int nBinsNllEMuData = 50;
// if (iatt==0){
// hnew = new TH1D(thename,thename,nBinsNllEMu,-3000,6000);
// }
// if (iatt==1){
// hnew = new TH1D(thename,thename,nBinsNllEMu,-3000,6000);
// }
//return hnew;
}
//calculates attributes from the raw fiTQun output
void histoFactory::fillAttributesData(){
att[0] = fqData->fq1rnll[0][2]-fqData->fq1rnll[0][1];
att[1] = fqData->fq1rnll[1][2]-fqData->fq1rnll[1][1];
return;
}
void histoFactory::fillAttributesMC(){
att[0] = fqMC->fq1rnll[0][2]-fqMC->fq1rnll[0][1];
att[1] = fqMC->fq1rnll[1][2]-fqMC->fq1rnll[1][1];
return;
}
void histoFactory::normalizeHistos(double scale){
//scale all MC histograms by some scaling factor
if (scale < 0.){
scale = (double)nDataEvents/(double)nMCEvents;
}
hnorm = new TH1D("hnorm","hnorm",1,0,1);
scale = (double)nDataEvents/(double)nMCEvents;
hnorm->SetBinContent(1,scale);
/*for (int ibin=0;ibin<nBins;ibin++){
for (int isamp=0;isamp<nSamples;isamp++){
for (int iatt=0;iatt<nAttributes;iatt++){
for (int icomp=0;icomp<nComponents;icomp++){
// hManager->getHistogram(isamp,ibin,icomp,iatt)->Scale(scale);
}
}
}
}*/
return;
}
void histoFactory::fillHistos(){
int nevdata = dataTree->GetEntries();
int nevmc = mcTree->GetEntries();
cout<<"histoFactory: Number of Data entries: "<<nevdata<<endl;
cout<<"histoFactory: Number of MC entries: "<<nevmc<<endl;
//fill data histos
for (int i=0;i<nevdata;i++){
dataTree->GetEntry(i);
fillAttributesData();
if (i%100==0) {
std::cout<<i<<" "<<fqData->nsample<<" "<<fqData->nbin<<" "<<fqData->attribute[0]<<" "<<fqData->attribute[1]<<std::endl;
}
for (int iatt=0;iatt<nAttributes;iatt++){
hManager->fillHistogramData(fqData->nsample,fqData->nbin,iatt,
fqData->attribute[iatt],fqData->evtweight);
}
}
//fill MC histos
for (int j=0;j<nevmc;j++){
mcTree->GetEntry(j);
fillAttributesMC();
for (int jatt=0;jatt<nAttributes;jatt++){
if (!separateNeutMode) {
hManager->fillHistogram(fqMC->nsample,fqMC->nbin,fqMC->ncomponent,
jatt,fqMC->attribute[jatt],fqMC->rfgweight);
} else {
hManager->fillHistogram(fqMC->nsample, fqMC->nbin, fqMC->ncomponent, fqMC->nmode, jatt, fqMC->attribute[jatt], fqMC->rfgweight);
hManager->fillNominalHistogram(fqMC->nsample, fqMC->nbin, fqMC->ncomponent, fqMC->nmode, jatt, fqMC->attribute[jatt], fqMC->evtweight);
}
// hManager->fillHistogram(fqMC->nsample,fqMC->nbin,fqMC->ncomponent,jatt,att[jatt],fqMC->evtweight);
}
}
return;
}
void histoFactory::setDataTree(TChain* ch){
dataTree=ch;
fqData = new fQreader(dataTree);
nDataEvents = dataTree->GetEntries();
dataTree->GetEntry(0);
std::cout<<"got data entry 0"<<std::endl;
return;
}
void histoFactory::setDataTree(TTree* tr){
dataTree=(TChain*)tr;
fqData = new fQreader(dataTree);
nDataEvents = dataTree->GetEntries();
return;
}
void histoFactory::setMCTree(TTree* tr){
mcTree=(TChain*)tr;
fqMC = new fQreader(mcTree);
fqMC->FillMap();
nMCEvents = mcTree->GetEntries();
return;
}
void histoFactory::setMCTree(TChain* ch){
mcTree=ch;
fqMC = new fQreader(mcTree);
fqMC->FillMap();
nMCEvents = mcTree->GetEntries();
return;
}
//writes all histograms to output file
void histoFactory::saveToFile(){
cout<<"histoFactory: Saving file: "<<fout->GetPath()<<endl;
fout->Write();
return;
}