ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitHzz4l/Angles/src/fillBdtTree.cc
Revision: 1.3
Committed: Mon Dec 17 12:35:15 2012 UTC (12 years, 5 months ago) by dkralph
Content type: text/plain
Branch: MAIN
Changes since 1.2: +0 -0 lines
Log Message:
many updates

File Contents

# Content
1 #include <iostream>
2 #include <assert.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <getopt.h>
6 #include <string.h>
7 #include <utility>
8 #include <map>
9 // #include <boost/regex.hpp>
10
11 #include "TFile.h"
12 #include "TChain.h"
13 #include "TList.h"
14 #include "TRegexp.h"
15 #include "TTree.h"
16 #include "TBranch.h"
17
18 #include "ZZMatrixElement/MELA/interface/Mela.h"
19 #include "ZZMatrixElement/MELA/interface/PseudoMELA.h"
20
21 #include "PwhgWrapper.h"
22
23 #include "TMVA/Reader.h"
24 #include "TMVA/Tools.h"
25 #include "TMVA/Config.h"
26 #include "TMVA/MethodBDT.h"
27
28 #include "KinematicsStruct.h"
29 #include "AngleTuple.h"
30 #include "TH1F.h"
31 #include "filestuff.h"
32 #include "Angles.h"
33 #include "WeightStruct.h"
34 #include "GenInfoStruct.h"
35 #include "SampleWeight.h"
36 #include "varkeepter.h"
37
38 #ifndef CMSSW_BASE
39 #define CMSSW_BASE "../../"
40 #endif
41
42 using namespace std;
43
44 pwhegwrapper *wrap;
45
46 class CtrlFlags
47 {
48 public :
49 CtrlFlags():inputfile(""),
50 inputtree("zznt"),
51 weightfile(""),
52 output("test.root"),
53 mH_lo(-1),
54 mH_hi(-1),
55 wL(0),
56 wH(0),
57 varstring(""),
58 makeMela(false),
59 multiclass(false),
60 flattree(false),
61 multisigs(false),
62 era(-1),
63 withPtY(false),
64 mH(0),
65 hwidth(0)
66 { };
67 ~CtrlFlags() {};
68 void dump();
69
70 string inputfile;
71 string inputtree;
72 string weightfile;
73 string output;
74 float mH_lo,mH_hi;
75 float wL,wH;
76 TString varstring;
77 bool makeMela;
78 bool multiclass;
79 bool flattree;
80 bool multisigs;
81 int era;
82 bool withPtY;
83 float mH;
84 float hwidth;
85 };
86 void CtrlFlags::dump()
87 {
88 cout << "mH : " << mH << endl;
89 cout << "inputfile : " << inputfile << endl;
90 cout << "inputtree : " << inputtree << endl;
91 cout << "weightfile : " << weightfile << endl;
92 cout << "output : " << output << endl;
93 cout << "mH_lo : " << mH_lo << endl;
94 cout << "mH_hi : " << mH_hi << endl;
95 cout << "wL : " << wL << endl;
96 cout << "wH : " << wH << endl;
97 cout << "varstring : " << varstring << endl;
98 cout << "makeMela : " << makeMela << endl;
99 cout << "multiclass : " << multiclass << endl;
100 cout << "flattree : " << flattree << endl;
101 cout << "multisigs : " << multisigs << endl;
102 cout << "withPtY : " << withPtY << endl;
103 cout << "hwidth : " << hwidth << endl;
104 assert(era==2011 || era==2012);
105 cout << "era : " << era << endl;
106 }
107 PseudoMELA *psMela;
108 void parse_args( int, char**, CtrlFlags & );
109 void setFracs(float mH, float mH_lo, float mH_hi, float &wgt_lo, float &wgt_hi);
110 void setZzntVals(CtrlFlags &flags, varkeepter *flatoutput, filestuff *fs, TString cls, TMVA::Reader *reader=0, Mela *mela=0, TMVA::Reader *spinReader=0);
111 double getWeight(TH1F *hist, float val);
112 void setPtWgtHists(TString cls, TString fname, Long_t mH, TFile *wgtFile);
113 //----------------------------------------------------------------------------------------
114 map<TString,TH1F*> wgtHists;
115
116 //------------------------------------------------------------
117 int main(int argc, char ** argv)
118 //------------------------------------------------------------
119 {
120 CtrlFlags flags;
121 parse_args( argc, argv, flags);
122 flags.dump();
123 assert(flags.varstring!="");
124 assert(fopen(flags.weightfile.c_str(), "r") || flags.makeMela);
125
126 TFile *pt4lWgts = TFile::Open("data/pt4l/pt4l_weights.root");
127 wrap = new pwhegwrapper;
128
129 TString cmsswpath(CMSSW_BASE + TString("/src"));
130 string xspath = (string(cmsswpath)+"/MitPhysics/data/xs.dat");
131 SimpleTable xstab(xspath.c_str());
132
133 //
134 // setup input
135 //
136
137 cout << "flags: ----------------------------------------------------------------------------------------" << endl;
138 cout << "inputfile: " << flags.inputfile << endl;
139 cout << "output: " << flags.output << endl;
140
141 TString spinWeightFile("");//ntupledir;
142 vector<TString> classes,fileNames;
143 vector<double> fileFracs,fileMasses;
144 vector<TFile*> inFiles;
145 vector<TTree*> inTrees;
146 ifstream ifs(flags.inputfile.c_str());
147 assert(ifs.is_open());
148 string line;
149 while(getline(ifs,line)) {
150 if(line[0]=='#') continue;
151 stringstream ss(line);
152 if(line[0]=='^') {
153 TString dummy;
154 // if(TString(line).Contains("^ntupledir")) ss >> dummy >> ntupledir;
155 if(TString(line).Contains("^spinWeightFile")) { ss >> dummy >> spinWeightFile; cout << "spinWeightFile: " << spinWeightFile << endl; }
156 continue;
157 }
158
159 TString cls,fname,type;
160 double frac,fileMass;
161 ss >> cls >> type >> frac >> fname >> fileMass;
162 assert(cls=="gfH" || cls=="vbfH" || cls=="vttH" || cls=="qqH" || cls=="qqZZ" || cls=="ggZZ" || cls=="lljj" || cls=="data");
163 classes.push_back(cls);
164 fileNames.push_back(/*ntupledir+"/"+*/fname);
165 fileFracs.push_back(frac);
166 fileMasses.push_back(fileMass);
167 inFiles.push_back(TFile::Open(fname));
168 assert(inFiles.back()->IsOpen());
169 inTrees.push_back((TTree*)inFiles.back()->Get(flags.inputtree.c_str()));
170 assert(inTrees.back());
171 cout << " pushing back: " << cls << " " << setw(12) << frac << " " << fname << " " << fileMass << endl;
172 }
173 ifs.close();
174
175 // mass cut based on weightfile name ...
176 float mH = flags.mH;
177 // boost::regex expression2(".*_mH(.*)_.*");
178 // boost::cmatch match2;
179 // if(boost::regex_match(flags.weightfile.c_str(), match2, expression2))
180 // mH = std::strtof(match2[1].first,NULL);
181
182 // assert(flags.wL>=0 && flags.wL < 1000 && flags.wH >= 0 && flags.wH < 1000);
183 char cutstr[512];
184 sprintf(cutstr, "m4l>%.3f&&m4l<%.3f", flags.wL, flags.wH);
185 cout << "mH: " << setw(8) << mH
186 << " wL: " << setw(8) << flags.wL
187 << " wH: " << setw(8) << flags.wH
188 << " mH_lo: " << setw(8) << flags.mH_lo
189 << " mH_hi: " << setw(8) << flags.mH_hi
190 << endl;
191
192 float frac_lo=1,frac_hi=1;
193 map<double,double> mH_fracs;
194 mH_fracs[-1] = 1; // this means zz
195 if(flags.mH_lo != 0) {
196 setFracs(mH, flags.mH_lo, flags.mH_hi, frac_lo, frac_hi);
197 mH_fracs[flags.mH_lo] = frac_lo;
198 mH_fracs[flags.mH_hi] = frac_hi;
199 }
200 mH_fracs[mH] = 1;
201
202 unsigned nMin=9999999;
203 vector<double> passFracs;
204 for(unsigned ifile=0; ifile<fileNames.size(); ifile++) {
205 cout << classes[ifile] << ": "
206 << setw(8) << inTrees[ifile]->GetEntries(cutstr) << " / " << setw(8) << inTrees[ifile]->GetEntries() << " events pass cut in file: " << fileNames[ifile] << endl;
207 int nEntriesWithCut = inTrees[ifile]->GetEntries(cutstr);
208 if(nEntriesWithCut < nMin) nMin = nEntriesWithCut;
209 passFracs.push_back( double(nEntriesWithCut)/inTrees[ifile]->GetEntries() );
210 }
211
212 cout << "twice the min number of entries over all the files, 2*nMin: " << 2*nMin << endl;
213
214 TString tmpfname(flags.output);
215 tmpfname.ReplaceAll("/","-");
216 tmpfname = "/tmp/"+tmpfname+"-tmpfile.root";
217 TFile tmpfile(tmpfname,"recreate");
218 vector<TTree*> TreeCopies; // copies of the input trees, but with only the number of events we want
219 TList *treeList = new TList;
220 vector<long> nActuallyAdded,nActuallyAddedCumulative;
221 cout << "making tree copies: " << endl;
222 for(unsigned ifile=0; ifile<fileNames.size(); ifile++) {
223 if(mH_fracs.find(fileMasses[ifile]) == mH_fracs.end())
224 cout << "ERROR: filemasses[ifile]: " << fileMasses[ifile] << endl;
225 assert(mH_fracs.find(fileMasses[ifile]) != mH_fracs.end());
226 double fracThisFile,nTarget;
227 long nToRequest;
228 if(fileFracs[ifile] == 1) { // use all of this sample (eg for background)
229 fracThisFile = 1;
230 nTarget = passFracs[ifile]*inTrees[ifile]->GetEntries();
231 nToRequest = 1000000000; // use default value for TTree::CopyTree(): all of 'em
232 } else {
233 if(fileFracs[ifile] == -1)
234 fracThisFile = mH_fracs[fileMasses[ifile]]; // get fraction for this sample from function
235 else
236 fracThisFile = fileFracs[ifile]; // get fraction for this sample from txt file
237 nTarget = 2*fracThisFile*nMin;
238 nToRequest = nTarget / passFracs[ifile];
239 }
240 cout << " adding " << setw(12) << nTarget << " from file " << fileNames[ifile]
241 << ", which means asking for: " << nToRequest << endl;
242 TreeCopies.push_back(inTrees[ifile]->CopyTree(cutstr,"",nToRequest));
243
244 long nCumulative=0;
245 for(unsigned j=0; j<nActuallyAdded.size(); j++) nCumulative += nActuallyAdded[j];
246 nActuallyAddedCumulative.push_back(nCumulative);
247 nActuallyAdded.push_back(long(TreeCopies.back()->GetEntries()));
248 cout << "actually added : " << nActuallyAdded.back() << ", cumulative: " << nActuallyAddedCumulative.back() << endl;
249
250 treeList->Add(TreeCopies.back());
251 }
252
253 TTree * t_tmp = TTree::MergeTrees(treeList);
254 tmpfile.Write();
255 tmpfile.Close();
256
257 //
258 // setup output
259 //
260 filestuff fs(tmpfname,tmpfname,"",classes[0]=="data");
261 float f_bdt;
262 TBranch *br_bdt=0;
263 varkeepter *flatoutput=0;
264 TString varfname("zzntvars");
265 if(flags.multisigs) varfname += "-multisigs";
266 flatoutput = new varkeepter("Angles/data/"+varfname+".txt","w",flags.output.c_str(),42,"zznt");
267
268 //
269 // setup TMVA
270 //
271 float reduced_mZ1, reduced_mZ2;
272 float reduced_zzdotz1, reduced_zzdotz2;
273 float reduced_ZZpt, reduced_Z1pt, reduced_Z2pt;
274
275 map<string,bool> varmap;
276 char * dovar = strtok(const_cast<char *>(flags.varstring.Data()), ":");
277 if( dovar != NULL ) varmap[string(dovar)]=true;
278 while( (dovar = strtok(NULL, ":")) != NULL ) {
279 varmap[string(dovar)]=true;
280 }
281
282 int run,lumi,evt;
283 TMVA::Reader *reader=0,*spinReader=0;
284 Mela *mela=0;
285 psMela=0;
286 if(!flags.makeMela) {
287 reader = new TMVA::Reader( "V" );
288 if(varmap[string("costheta1")]) reader->AddVariable( "costheta1", &fs.angles->costheta1);
289 if(varmap[string("costheta2")]) reader->AddVariable( "costheta2", &fs.angles->costheta2);
290 if(varmap[string("costhetastar")]) reader->AddVariable( "costhetastar", &fs.angles->costhetastar);
291 if(varmap[string("Phi")]) reader->AddVariable( "Phi", &fs.angles->Phi);
292 if(varmap[string("Phi1")]) reader->AddVariable( "Phi1", &fs.angles->Phi1);
293 if(varmap[string("mZ1")]) reader->AddVariable( "mZ1", &reduced_mZ1);
294 if(varmap[string("mZ2")]) reader->AddVariable( "mZ2", &reduced_mZ2);
295 if(varmap[string("pt4l")]) reader->AddVariable( "ZZpt/m4l", &reduced_ZZpt);
296 if(varmap[string("zzdotz1")]) reader->AddVariable( "ZZdotZ1/(m4l*mZ1)", &reduced_zzdotz1 );
297 if(varmap[string("zzdotz2")]) reader->AddVariable( "ZZdotZ2/(m4l*mZ2)", &reduced_zzdotz2 );
298 if(varmap[string("dphi1")]) reader->AddVariable( "ZZptCosDphiZ1pt", &fs.kine->ZZptZ1ptCosDphi );
299 if(varmap[string("dphi2")]) reader->AddVariable( "ZZptCosDphiZ2pt", &fs.kine->ZZptZ2ptCosDphi );
300 if(varmap[string("Z1pt")]) reader->AddVariable( "Z1pt/m4l", &reduced_Z1pt );
301 if(varmap[string("Z2pt")]) reader->AddVariable( "Z2pt/m4l", &reduced_Z2pt );
302 if(varmap[string("y4l")]) reader->AddVariable( "ZZy", &fs.kine->ZZy);
303 if(varmap[string("m4l")]) reader->AddVariable( "m4l", &fs.kine->m4l);
304 else reader->AddSpectator("m4l", &fs.kine->m4l);
305 reader->BookMVA("BDTG", flags.weightfile.c_str());
306 if(spinWeightFile != "") {
307 spinReader = new TMVA::Reader( "V" );
308 if(varmap[string("costheta1")]) spinReader->AddVariable( "costheta1", &fs.angles->costheta1);
309 if(varmap[string("costheta2")]) spinReader->AddVariable( "costheta2", &fs.angles->costheta2);
310 if(varmap[string("costhetastar")]) spinReader->AddVariable( "costhetastar", &fs.angles->costhetastar);
311 if(varmap[string("Phi")]) spinReader->AddVariable( "Phi", &fs.angles->Phi);
312 if(varmap[string("Phi1")]) spinReader->AddVariable( "Phi1", &fs.angles->Phi1);
313 if(varmap[string("mZ1")]) spinReader->AddVariable( "mZ1", &reduced_mZ1);
314 if(varmap[string("mZ2")]) spinReader->AddVariable( "mZ2", &reduced_mZ2);
315 // if(varmap[string("pt4l")]) spinReader->AddVariable( "ZZpt/m4l", &reduced_ZZpt);
316 // if(varmap[string("zzdotz1")]) spinReader->AddVariable( "ZZdotZ1/(m4l*mZ1)", &reduced_zzdotz1 );
317 // if(varmap[string("zzdotz2")]) spinReader->AddVariable( "ZZdotZ2/(m4l*mZ2)", &reduced_zzdotz2 );
318 // if(varmap[string("dphi1")]) spinReader->AddVariable( "ZZptCosDphiZ1pt", &fs.kine->ZZptZ1ptCosDphi );
319 // if(varmap[string("dphi2")]) spinReader->AddVariable( "ZZptCosDphiZ2pt", &fs.kine->ZZptZ2ptCosDphi );
320 // if(varmap[string("Z1pt")]) spinReader->AddVariable( "Z1pt/m4l", &reduced_Z1pt );
321 // if(varmap[string("Z2pt")]) spinReader->AddVariable( "Z2pt/m4l", &reduced_Z2pt );
322 // if(varmap[string("y4l")]) spinReader->AddVariable( "ZZy", &fs.kine->ZZy);
323 if(varmap[string("m4l")]) spinReader->AddVariable( "m4l", &fs.kine->m4l);
324 else spinReader->AddSpectator("m4l", &fs.kine->m4l);
325 spinReader->AddSpectator("run", &run);
326 spinReader->AddSpectator("lumi", &lumi);
327 spinReader->AddSpectator("evt", &evt);
328 spinReader->BookMVA("BDTG", spinWeightFile);
329 }
330 } else {
331 // mela = new Mela::Mela(bool usePowhegTemplate=false, int LHCsqrts=8);
332 mela = new Mela(false, flags.era==2011 ? 7 : 8);
333 if(flags.multisigs)
334 psMela = new PseudoMELA();
335 }
336
337 //
338 // evaluate
339 //
340 vector<unsigned> runv,evtv; // for removing duplicate events
341 int ifile=-1;
342 int nent=fs.get_zz_chain()->GetEntries();
343 cout << "beginning tree from mass " << mH << "(" << flags.wL << "," << flags.wH << ") with " << nent << " entries" << endl;
344 for( int i=0; i<nent; i++ ) {
345 if( !(i%5000) ) cerr << "processed: " << i << "/" << nent << endl;
346 fs.getentry(i,"","zznt");
347
348 if(classes[0]=="data") { // the rest of the classes better be data as well...
349 bool isDuplic=false;
350 for(unsigned ievt=0; ievt<evtv.size(); ievt++) {
351 if((fs.info->run == runv[ievt]) && (fs.info->evt == evtv[ievt])) {
352 isDuplic = true;
353 break;
354 }
355 }
356 if(isDuplic) {
357 cout << " skipping duplicate event: " << fs.info->run << " " << fs.info->evt << endl;
358 continue;
359 }
360 }
361 runv.push_back(fs.info->run);
362 evtv.push_back(fs.info->evt);
363
364 if(ifile+1 < (int)nActuallyAddedCumulative.size() && i >= nActuallyAddedCumulative[ifile+1]) {
365 ifile++;
366 cout << " ientry: " << i << ", switching file to (" << classes[ifile] << "): " << fileMasses[ifile]
367 << " so weight multiplies by: " << ((classes[ifile]=="gfH" || classes[ifile]=="vbfH" || classes[ifile]=="qqH") ? mH_fracs[fileMasses[ifile]] : 1) << endl;
368 if(classes[ifile]=="gfH" || classes[ifile]=="qqZZ")
369 setPtWgtHists(classes[ifile],fileNames[ifile],mH,pt4lWgts);
370 }
371
372 if( fs.kine->m4l>flags.wH || fs.kine->m4l<flags.wL ) { cout << "ERROR! shouldn't be here (already applied the cut): " << fs.kine->m4l << endl; continue; }
373
374 reduced_mZ1 = fs.kine->mZ1;
375 reduced_mZ2 = fs.kine->mZ2;
376 reduced_ZZpt = fs.kine->ZZpt/fs.kine->m4l;
377 reduced_Z1pt = fs.kine->Z1pt/fs.kine->m4l;
378 reduced_Z2pt = fs.kine->Z2pt/fs.kine->m4l;
379 reduced_zzdotz1 = fs.kine->ZZdotZ1/(fs.kine->m4l*fs.kine->mZ1);
380 reduced_zzdotz2 = fs.kine->ZZdotZ2/(fs.kine->m4l*fs.kine->mZ2);
381
382 if(classes[ifile]=="gfH" || classes[ifile]=="vbfH" || classes[ifile]=="qqH")
383 fs.weights->w *= mH_fracs[fileMasses[ifile]];
384
385 if(!flags.makeMela && !flags.flattree) { // shouldn't ever use this at the point
386 assert(0);
387 fs.outtuple->Fill();
388 } else {
389 setZzntVals(flags, flatoutput, &fs, classes[ifile], reader, mela, spinReader);
390 flatoutput->fill();
391 }
392 }
393
394 if(!flags.makeMela && !flags.flattree) {
395 fs.outtuple->getTree()->Print();
396 fs.outtuple->WriteClose();
397 } else {
398 flatoutput->gettree()->Print();
399 flatoutput->writeclose();
400 }
401 }
402 //----------------------------------------------------------
403 void parse_args( int argc, char** argv, CtrlFlags &flags )
404 //----------------------------------------------------------
405 {
406 int c;
407 int digit_optind = 0;
408
409 while (1) {
410 int this_option_optind = optind ? optind : 1;
411 int option_index = 0;
412 static struct option long_options[] = {
413 {"inputfile", 1, 0, 'a'},
414 {"inputtree", 1, 0, 'b'},
415 {"weightfile", 1, 0, 'c'},
416 {"output", 1, 0, 'd'},
417 {"mH_lo", 1, 0, 'e'},
418 {"mH_hi", 1, 0, 'f'},
419 {"wL", 1, 0, 'g'},
420 {"wH", 1, 0, 'h'},
421 {"varstring", 1, 0, 'i'},
422 {"makeMela", 0, 0, 'j'},
423 {"multiclass", 0, 0, 'k'},
424 {"flattree", 0, 0, 'l'},
425 {"multisigs", 0, 0, 'm'},
426 {"era", 1, 0, 'n'},
427 {"withPtY", 0, 0, 'o'},
428 {"mH", 1, 0, 'p'},
429 {"hwidth", 1, 0, 'q'},
430 {0, 0, 0, 0}
431 };
432
433 c = getopt_long (argc, argv, "a:e",
434 long_options, &option_index);
435 if (c == -1)
436 break;
437
438 switch (c) {
439 case 0:
440 printf ("option %s", long_options[option_index].name);
441 if (optarg)
442 printf (" with arg %s", optarg);
443 printf ("\n");
444 break;
445
446 case 'a':
447 flags.inputfile = std::string(optarg);
448 break;
449 case 'b':
450 flags.inputtree = std::string(optarg);
451 break;
452 case 'c':
453 flags.weightfile = std::string(optarg);
454 break;
455 case 'd':
456 flags.output = std::string(optarg);
457 break;
458 case 'e':
459 flags.mH_lo = strtof(optarg,NULL);
460 break;
461 case 'f':
462 flags.mH_hi = strtof(optarg,NULL);
463 break;
464 case 'g':
465 flags.wL = strtof(optarg,NULL);
466 break;
467 case 'h':
468 flags.wH = strtof(optarg,NULL);
469 break;
470 case 'i':
471 flags.varstring = TString(optarg);
472 break;
473 case 'j':
474 flags.makeMela = true;
475 break;
476 case 'k':
477 flags.multiclass = true;
478 break;
479 case 'l':
480 flags.flattree = true;
481 break;
482 case 'm':
483 flags.multisigs = true;
484 break;
485 case 'n':
486 flags.era = atoi(optarg);
487 assert(flags.era==2011 || flags.era==2012);
488 break;
489 case 'o':
490 flags.withPtY = true;
491 break;
492 case 'p':
493 flags.mH = strtof(optarg,NULL);
494 break;
495 case 'q':
496 flags.hwidth = strtof(optarg,NULL);
497 break;
498 case '2':
499 if (digit_optind != 0 && digit_optind != this_option_optind)
500 printf ("digits occur in two different argv-elements.\n");
501 digit_optind = this_option_optind;
502 printf ("option %c\n", c);
503 break;
504
505 case '?':
506 break;
507
508 default:
509 printf ("?? getopt returned character code 0%o ??\n", c);
510 }
511 }
512
513 if( flags.inputfile.empty() ) {
514 cerr << "please specify an inputfile with --inputfile " << endl;
515 exit(1);
516 }
517
518 if (optind < argc) {
519 printf ("non-option ARGV-elements: ");
520 while (optind < argc)
521 printf ("%s ", argv[optind++]);
522 printf ("\n");
523 }
524 }
525 //----------------------------------------------------------------------------------------
526 void setFracs(float mH, float mH_lo, float mH_hi, float &frac_lo, float &frac_hi)
527 {
528 assert(mH_lo<=mH && mH_hi>=mH && mH_lo>0 && mH_hi>0);
529 double delta = mH_hi - mH_lo;
530 frac_lo = (mH_hi - mH)/delta;
531 frac_hi = (mH - mH_lo)/delta;
532 }
533 //----------------------------------------------------------------------------------------
534 void setZzntVals(CtrlFlags &flags, varkeepter *flatoutput, filestuff *fs, TString cls, TMVA::Reader *reader, Mela *mela, TMVA::Reader *spinReader)
535 {
536 flatoutput->setval("run", (UInt_t)fs->info->run);
537 flatoutput->setval("evt", (UInt_t)fs->info->evt);
538 flatoutput->setval("channel", (UInt_t)fs->kine->channel);
539 flatoutput->setval("ZZpt", (Double_t)fs->kine->ZZpt);
540 flatoutput->setval("m4l", (Double_t)fs->kine->m4l);
541 flatoutput->setval("w", (Double_t)fs->weights->w);
542 flatoutput->setval("won", (Double_t)fs->weights->won);
543 flatoutput->setval("woff", (Double_t)fs->weights->woff);
544 flatoutput->setval("npuw", (Double_t)fs->weights->npuw);
545
546 // set pt4l up/down weights
547 Double_t pt4lDown(1),pt4lUp(1),pt4lPdfDown(1),pt4lPdfUp(1),pt4lHresDown(1),pt4lHresUp(1);
548 if(cls=="qqZZ" || (cls=="gfH" && !fs->fname_.Contains(TRegexp("[02][mp]")))) {
549 assert(fs->get_zz_chain()->FindBranch("geninfo"));
550 assert(wgtHists["renfac_Lo"]);
551 assert(wgtHists["renfac_Hi"]);
552 assert(wgtHists["pdf_Lo"]);
553 assert(wgtHists["pdf_Hi"]);
554 pt4lDown = getWeight(wgtHists["renfac_Lo"], fs->gen->pt_zz);
555 pt4lUp = getWeight(wgtHists["renfac_Hi"], fs->gen->pt_zz);
556 pt4lPdfDown = getWeight(wgtHists["pdf_Lo"], fs->gen->pt_zz);
557 pt4lPdfUp = getWeight(wgtHists["pdf_Hi"], fs->gen->pt_zz);
558 // cout << "pt4l: " << fs->gen->pt_zz << " renfac: " << pt4lDown << " " << pt4lUp << " pdf: " << pt4lPdfDown << " " << pt4lUp;
559 if(cls != "qqZZ" ) {
560 assert(wgtHists["hres_Lo"]);
561 assert(wgtHists["hres_Hi"]);
562 pt4lHresDown = getWeight(wgtHists["hres_Lo"], fs->gen->pt_zz);
563 pt4lHresUp = getWeight(wgtHists["hres_Hi"], fs->gen->pt_zz);
564 // cout << " hres: " << pt4lHresDown << " " << pt4lHresUp;
565 }
566 // cout << endl;
567 }
568 flatoutput->setval("pt4lDown", (Double_t)pt4lDown);
569 flatoutput->setval("pt4lUp", (Double_t)pt4lUp);
570 flatoutput->setval("pt4lPdfDown", (Double_t)pt4lPdfDown);
571 flatoutput->setval("pt4lPdfUp", (Double_t)pt4lPdfUp);
572 flatoutput->setval("pt4lHresDown", (Double_t)pt4lHresDown);
573 flatoutput->setval("pt4lHresUp", (Double_t)pt4lHresUp);
574
575 // set mH weights
576 Double_t wmH(1);
577 if((cls=="gfH" && !fs->fname_.Contains(TRegexp("[02][mp]"))) || cls=="vbfH") {
578 assert(fs->get_zz_chain()->FindBranch("geninfo"));
579 GenInfoStruct gen(*fs->gen);
580 TLorentzVector genZ1,genZ2;
581 genZ1.SetPtEtaPhiM( gen.vpt_a, gen.veta_a, gen.vphi_a, gen.vmass_a);
582 genZ2.SetPtEtaPhiM( gen.vpt_b, gen.veta_b, gen.vphi_b, gen.vmass_b);
583 double genM4l = (genZ1 + genZ2).M();
584 wmH = wrap->getweight(flags.mH, flags.hwidth, 172.5, genM4l, 1);
585 // cout << "m4l weights: " << genM4l << " " << flatoutput->getval("wmH") << endl;
586 }
587 flatoutput->setval("wmH", (Double_t)wmH);
588
589 if(flags.makeMela) {
590 assert(mela);
591 TLorentzVector Z1_lept1, Z1_lept2, Z2_lept1, Z2_lept2;
592 KinematicsStruct kine(*(fs->kine));
593
594 Z1_lept1.SetPtEtaPhiM( kine.l1pt, kine.l1eta, kine.l1phi, abs(kine.l1type)==13 ? 105.658369e-3 : 0.51099892e-3);
595 Z1_lept2.SetPtEtaPhiM( kine.l2pt, kine.l2eta, kine.l2phi, abs(kine.l2type)==13 ? 105.658369e-3 : 0.51099892e-3);
596 Z2_lept1.SetPtEtaPhiM( kine.l3pt, kine.l3eta, kine.l3phi, abs(kine.l3type)==13 ? 105.658369e-3 : 0.51099892e-3);
597 Z2_lept2.SetPtEtaPhiM( kine.l4pt, kine.l4eta, kine.l4phi, abs(kine.l4type)==13 ? 105.658369e-3 : 0.51099892e-3);
598
599 // NOTE: it's p.d.g. ID, so opposite sign to the charge
600 int Z1_lept1Id(-1*kine.l1q*kine.l1type),Z1_lept2Id(-1*kine.l2q*kine.l2type),Z2_lept1Id(-1*kine.l3q*kine.l3type),Z2_lept2Id(-1*kine.l4q*kine.l4type);
601 float costhetastar,costheta1,costheta2,phi,phistar1,kd,psig,pbkg,spinKd;
602 mela->computeKD(Z1_lept1, Z1_lept1Id, Z1_lept2, Z1_lept2Id, Z2_lept1, Z2_lept1Id, Z2_lept2, Z2_lept2Id,
603 costhetastar, costheta1, costheta2, phi, phistar1, kd, psig, pbkg, flags.withPtY, flags.withPtY);
604
605 flatoutput->setval("melaLD", (Float_t)kd);
606
607 if(flags.multisigs) {
608 assert(psMela);
609 psMela->computeKD(Z1_lept1, Z1_lept1Id, Z1_lept2, Z1_lept2Id, Z2_lept1, Z2_lept1Id, Z2_lept2, Z2_lept2Id,
610 spinKd, psig, pbkg);
611 flatoutput->setval("SpinBDT", (Float_t)spinKd);
612 }
613 } else {
614 assert(reader);
615 if(flags.multiclass) {
616 flatoutput->setval("BDT", (Float_t)((reader->EvaluateMulticlass("BDTG"))[0])); // signal component
617 flatoutput->setval("bkgBDT", (Float_t)((reader->EvaluateMulticlass("BDTG"))[1])); // bkg (ZZ) component
618 flatoutput->setval("bkg2BDT", (Float_t)((reader->EvaluateMulticlass("BDTG"))[2])); // bkg2 (Z+jets) component
619 } else {
620 flatoutput->setval("BDT", (Float_t)(reader->EvaluateMVA("BDTG")));
621 }
622 if(flags.multisigs) {
623 assert(spinReader);
624 flatoutput->setval("SpinBDT", (Float_t)spinReader->EvaluateMVA("BDTG"));
625 }
626 }
627 }
628 //----------------------------------------------------------------------------------------
629 double getWeight(TH1F *hist, float val)
630 {
631 int bin = hist->FindBin(val);
632 // return 1 for out-of-bounds
633 if(bin<1 || bin>hist->GetNbinsX())
634 return 1;
635 return hist->GetBinContent(bin);
636 }
637 //----------------------------------------------------------------------------------------
638 TString findClosestPtWgts(TFile *wgtFile, int mH, TString scaleType) // scaleType is renfac, pdf, or hres
639 {
640 Long_t closestMass(-1);
641 TString closestMassStr;
642 TList *keys = wgtFile->GetListOfKeys();
643 for(unsigned ikey=0; ikey<keys->GetEntries(); ikey++) {
644 TString name(keys->At(ikey)->GetName());
645 TString tmpName(name(TRegexp("h[0-9][0-9]*zz4l_"+scaleType)));
646 TString massStr(tmpName(TRegexp("[0-9][0-9]*")));
647 int mass = atoi(massStr);
648 if(mass<100 || mass > 1200) continue;
649 if(abs(mass - mH) < abs(closestMass - mH)) {
650 closestMass = mass;
651 closestMassStr = massStr;
652 }
653 }
654
655 cout << " using closest mass of: " << closestMassStr << endl;
656 return closestMassStr;
657 }
658 //----------------------------------------------------------------------------------------
659 void findHist(TString cls, Long_t mH, TString fname, TFile *wgtFile, TString scaleType, TString hiLo)
660 {
661 assert(hiLo=="Lo" || hiLo=="Hi");
662 TString procStr;
663 if(cls=="gfH") {
664 procStr = TString("h")+mH+"zz4l";
665 } else if(cls=="qqZZ") {
666 if(fname.Contains("zz4e")) procStr = "zz4e";
667 if(fname.Contains("zz4m")) procStr = "zz4m";
668 if(fname.Contains("zz2e2m")) procStr = "zz2e2m";
669 } else assert(0);
670 assert(procStr!="");
671
672 TString histName(hiLo+"Wgts_"+procStr+"_"+scaleType);
673 TH1F *htest = (TH1F*)wgtFile->Get(histName);
674 TString closestMass;
675 if(!htest) {
676 cout << " hist name " << histName << " not found" << endl;
677 if(cls=="gfH") {
678 closestMass = findClosestPtWgts(wgtFile, mH, scaleType);
679 histName.ReplaceAll(TString("")+mH, closestMass);
680 } else {
681 histName.ReplaceAll("zz4e","zz2e2m"); // only made 2e2m for a few of them
682 histName.ReplaceAll("zz4m","zz2e2m");
683 }
684 cout << " trying with " << histName << endl;
685 }
686
687 wgtHists[scaleType+"_"+hiLo] = (TH1F*)wgtFile->Get(histName); assert(wgtHists[scaleType+"_"+hiLo]);
688 }
689 //----------------------------------------------------------------------------------------
690 void setPtWgtHists(TString cls, TString fname, Long_t mH, TFile *wgtFile)
691 {
692 assert(wgtFile->IsOpen());
693
694 findHist(cls, mH, fname, wgtFile, "renfac", "Lo");
695 findHist(cls, mH, fname, wgtFile, "renfac", "Hi");
696 findHist(cls, mH, fname, wgtFile, "pdf", "Lo");
697 findHist(cls, mH, fname, wgtFile, "pdf", "Hi");
698 if(cls=="gfH") {
699 findHist(cls, mH, fname, wgtFile, "hres", "Lo");
700 findHist(cls, mH, fname, wgtFile, "hres", "Hi");
701 }
702 }