ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitHzz4l/Angles/src/fillBdtTree.cc
Revision: 1.7
Committed: Mon Jan 7 19:38:38 2013 UTC (12 years, 4 months ago) by andrey
Content type: text/plain
Branch: MAIN
CVS Tags: compiled
Changes since 1.6: +6 -5 lines
Log Message:
temporarily comment interference weighting ...

File Contents

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