ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitHzz4l/Selection/src/applySelectionZmumu.cc
Revision: 1.1
Committed: Mon Feb 13 09:47:12 2012 UTC (13 years, 3 months ago) by khahn
Content type: text/plain
Branch: MAIN
CVS Tags: synced_FSR_2, synced_FSR, synched2, synched
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 khahn 1.1 //
2     // System headers
3     //
4     #include <vector> // STL vector class
5     //#include <vector> // STL pair
6     #include <iostream> // standard I/O
7     #include <iomanip> // functions to format standard I/O
8     #include <fstream> // functions for file I/O
9     #include <string> // C++ string class
10     #include <sstream> // class for parsing strings
11     #include <assert.h>
12     #include <stdlib.h>
13     #include <getopt.h>
14     using namespace std;
15    
16     //
17     // ROOT headers
18     //
19     #include <TROOT.h> // access to gROOT, entry point to ROOT system
20     #include <TSystem.h> // interface to OS
21     #include <TFile.h> // file handle class
22     #include <TNtuple.h>
23     #include <TTree.h> // class to access ntuples
24     #include <TChain.h> //
25     #include <TBranch.h> // class to access branches in TTree
26     #include <TClonesArray.h> // ROOT array class
27     #include <TCanvas.h> // class for drawing
28     #include <TH1F.h> // 1D histograms
29     #include <TBenchmark.h> // class to track macro running statistics
30     #include <TLorentzVector.h> // 4-vector class
31     #include <TVector3.h> // 3D vector class
32    
33     //
34     // TMVA
35     //
36     #include "TMVA/Reader.h"
37     #include "TMVA/Tools.h"
38     #include "TMVA/Config.h"
39     #include "TMVA/MethodBDT.h"
40    
41     //
42     // ntuple format headers
43     //
44     #include "HiggsAnaDefs.hh"
45     #include "TEventInfo.hh"
46     #include "TElectron.hh"
47     #include "TMuon.hh"
48     #include "TJet.hh"
49     #include "TPhoton.hh"
50     #include "SiMVAElectronSelection.h"
51     #include "RunLumiRangeMap.h"
52     #include "EfficiencyWeightsInterface.h"
53    
54     //
55     // utility headers
56     //
57     #include "ParseArgs.h"
58     #include "SampleWeight.h"
59     #include "Selection.h"
60     #include "WZSelection.h"
61     #include "HZZCiCElectronSelection.h"
62     #include "HZZLikelihoodElectronSelection.h"
63     #include "HZZBDTElectronSelection.h"
64     #include "SampleWeight.h"
65     #include "Angles.h"
66    
67     //#define BDTFAIL_THEIR_EVENTS
68     //#define THEIR_EVENTS
69    
70     Double_t etaFromY(Double_t pt, Double_t y, Double_t phi, Double_t m)
71     {
72     Double_t a = (1+exp(2*y))/(exp(2*y)-1); // intermediate term
73     if(a*a<1) { cout << "a too small" << endl; assert(0); }
74     Double_t E = sqrt( a*a*(pt*pt+m*m)/(a*a-1) );
75     Double_t pz = E*E - pt*pt - m*m;
76     if(pz<0) { cout << "imag. pz" << endl; assert(0); }
77     pz = sqrt(pz);
78     if(y<0) pz *= -1;
79     TLorentzVector v;
80     v.SetPxPyPzE(pt*cos(phi),pt*sin(phi),pz,E);
81     Double_t th = v.Theta();
82     return -log(tan(th/2));
83     };
84    
85     extern float calc_mT(float iPt1,float iPt2,float iPhi1,float iPhi2);
86    
87     int getChannel(mithep::TGenInfo * ginfo) {
88    
89     int gchannel=-1;
90    
91     if( abs(ginfo->id_1_a) == EGenType::kElectron && abs(ginfo->id_1_b) == EGenType::kElectron ) gchannel=0;
92     else if( abs(ginfo->id_1_a) == EGenType::kMuon && abs(ginfo->id_1_b) == EGenType::kMuon ) gchannel=1;
93     else if( (abs(ginfo->id_1_a) == EGenType::kElectron && abs(ginfo->id_1_b) == EGenType::kMuon) ||
94     (abs(ginfo->id_1_a) == EGenType::kMuon && abs(ginfo->id_1_b) == EGenType::kElectron) ) gchannel=2;
95    
96     // if( gchannel < 0 ) {
97     // cout << "ginfo->id_1_a: " << ginfo->id_1_a << "\t"
98     // << "ginfo->id_2_a: " << ginfo->id_2_a << "\t"
99     // << "ginfo->id_1_b: " << ginfo->id_1_b << "\t"
100     // << "ginfo->id_2_b: " << ginfo->id_2_b << "\t"
101     // << endl;
102     // }
103     return gchannel;
104     }
105    
106    
107     //=== MAIN =================================================================================================
108     int main(int argc, char** argv)
109     {
110    
111    
112     vector<pair<unsigned,unsigned> > runevtvec;
113     vector<vector<string> > inputFiles;
114     inputFiles.push_back(vector<string>());
115    
116     //
117     // args
118     //--------------------------------------------------------------------------------------------------------------
119     ControlFlags ctrl;
120     parse_args( argc, argv, ctrl );
121     if( ctrl.inputfile.empty() ||
122     ctrl.inputfile.empty() ) {
123     cerr << "usage: applySelection.exe <flags> " << endl;
124     cerr << "\tmandoatory flags : " << endl;
125     cerr << "\t--inputfile | file containing a list of ntuples to run over" << endl;
126     cerr << "\t--outputfile | file to store selected evet" << endl;
127     return 1;
128     }
129     ctrl.dump();
130     assert( ctrl.mH != 0 );
131    
132     map<string,double> entrymap; // number of unskimmed entries in each file
133    
134     //
135     // File I/O
136     //--------------------------------------------------------------------------------------------------------------
137     TChain * chain = new TChain("Events");
138     ifstream f(ctrl.inputfile.c_str());
139     string fname;
140     while (f >> fname) {
141     if( !(strncmp( fname.c_str(), "#", 1 ) ) ) continue; // skip commented lines
142     cout << "adding inputfile : " << fname.c_str() << endl;
143     entrymap[string(fname.c_str())] = unskimmedEntries(fname.c_str());
144     chain->AddFile(fname.c_str());
145     }
146    
147     // table of cross section values
148     SimpleTable xstab("./data/xs.dat");
149    
150     //
151     // Setup
152     //--------------------------------------------------------------------------------------------------------------
153     const char * ofname;
154     if( strcmp( ctrl.outputfile.c_str(), "") ) {
155     ofname = ctrl.outputfile.c_str();
156     } else {
157     ofname = "tmp.root";
158     }
159     TFile *file = new TFile(ofname, "RECREATE");
160     TH1F * h_evt = new TH1F("hevt", "hevt", 2, 0, 2 );
161     TH1F * h_evtfail = new TH1F("hevtfail", "hevtfail", 1024, 0, 1024 );
162    
163     unsigned run, evt, lumi, channel, gchannel;
164     float mZ1;
165     unsigned tZ1, tl3;
166     double w, woff, werroff, won, werron;
167     unsigned npu; double npuw;
168     float met, metphi, mt, m3l, mt3l;
169     float gm4l, gm3l, gmt3l, gmZ1, gmZ2;
170     float l3isorel, l3ipsig, l3pt, l3eta, l3phi;
171     float pt1, pt2, eta1, eta2, phi1, phi2;
172     unsigned qual1, qual2;
173     unsigned nothers;
174     float iso1, iso2;
175     float gpt, geta, gphi;
176    
177     TTree * passtuple = new TTree("passtuple", "passtuple" );
178     passtuple->Branch("mZ1", &mZ1);
179     passtuple->Branch("pt1", &pt1);
180     passtuple->Branch("pt2", &pt2);
181     passtuple->Branch("eta1", &eta1);
182     passtuple->Branch("eta2", &eta2);
183     passtuple->Branch("phi1", &phi1);
184     passtuple->Branch("phi2", &phi2);
185     passtuple->Branch("qual1", &qual1);
186     passtuple->Branch("qual2", &qual2);
187     passtuple->Branch("iso1", &iso1);
188     passtuple->Branch("iso2", &iso2);
189     passtuple->Branch("gpt", &gpt);
190     passtuple->Branch("geta", &geta);
191     passtuple->Branch("gphi", &gphi);
192    
193    
194     initCiCSelection();
195     if(ctrl.eleSele=="lik") initLikSelection();
196     if(ctrl.eleSele=="bdt") initBDTSelection();
197     // if(ctrl.eleSele=="si" ) initSiMVAElectronSelection();
198     initRunLumiRangeMap();
199     initEfficiencyWeights();
200    
201    
202     //
203     // Loop !!!!!!!!! should alternate events here and +1 in the training ...
204     //--------------------------------------------------------------------------------------------------------------
205     if(ctrl.fakeScheme!="none") {cout << "wrong executable!" << endl; assert(0);}
206    
207     //
208     // Access samples and fill histograms
209     TFile *inputFile=0;
210     TTree *eventTree=0;
211    
212     // Data structures to store info from TTrees
213     mithep::TEventInfo *info = new mithep::TEventInfo();
214     mithep::TGenInfo *ginfo = new mithep::TGenInfo();
215     TClonesArray *electronArr = new TClonesArray("mithep::TElectron");
216     TClonesArray *muonArr = new TClonesArray("mithep::TMuon");
217     TClonesArray *photonArr = new TClonesArray("mithep::TPhoton");
218    
219    
220     chain->SetBranchAddress("Info", &info);
221     chain->SetBranchAddress("Electron", &electronArr);
222     chain->SetBranchAddress("Muon", &muonArr);
223     chain->SetBranchAddress("Photon", &photonArr);
224     if( ctrl.mc ) { chain->SetBranchAddress("Gen", &ginfo);}
225     //ginfo = NULL;
226    
227     int count=0, pass=0;
228     float passcorr=0., passcorr_errup=0., passcorr_errdown=0.;
229     float denom[3]={0.,0.,0.};
230     float numer[3]={0.,0.,0.};
231     float numercorr[3]={0.,0.,0.};
232    
233     UInt_t imax = chain->GetEntries();
234     cout << "nEntries: " << imax << endl;
235    
236     for(UInt_t ientry=0; ientry<imax; ientry++) {
237     float bestM=0;
238     chain->GetEntry(ientry);
239     if(!(ientry%100000)) cerr << "entry: " << ientry << endl;
240    
241     for( int i=0; i<muonArr->GetEntries(); i++ ) {
242     const mithep::TMuon * mu1 = (mithep::TMuon*)((*muonArr)[i]);
243     for( int j=i+1; j<muonArr->GetEntries(); j++ ) {
244     const mithep::TMuon * mu2 = (mithep::TMuon*)((*muonArr)[j]);
245    
246     if( mu1->pt < 5 || mu2->pt < 5 ) continue;
247     if( mu1->q*mu2->q > 0 ) continue;
248     // if( !(mu1->typeBits&kGlobal) || !(mu2->typeBits&kGlobal) ) continue;
249    
250     TLorentzVector v1, v2;
251     v1.SetPtEtaPhiM(mu1->pt, mu1->eta, mu1->phi, 105.658369e-3);
252     v2.SetPtEtaPhiM(mu2->pt, mu2->eta, mu2->phi, 105.658369e-3);
253     float tmpM = (v1+v2).M();
254     if( tmpM<60 || tmpM>120 ) continue;
255    
256     if( tmpM > bestM ) {
257     bestM = tmpM;
258     mZ1 = tmpM;
259     pt1 = mu1->pt;
260     pt2 = mu2->pt;
261     eta1 = mu1->eta;
262     eta2 = mu2->eta;
263     phi1 = mu1->phi;
264     phi2 = mu2->phi;
265     qual1 = mu1->qualityBits;
266     qual2 = mu2->qualityBits;
267     iso1 = mu1->pfIso03;
268     iso2 = mu2->pfIso03;
269     }
270    
271     }
272     }
273    
274    
275     if( bestM>0 ) {
276    
277     float highest_gamma_pt=-1, highest_gamma_eta=-999, highest_gamma_phi=-999;
278     for( int i=0; i<photonArr->GetEntries(); i++ ) {
279     const mithep::TPhoton * p1 = (mithep::TPhoton*)((*photonArr)[i]);
280     TVector3 pvec,mu1vec,mu2vec;
281     pvec.SetPtEtaPhi(p1->pt,p1->eta,p1->phi);
282     mu1vec.SetPtEtaPhi(pt1,eta1,phi1);
283     mu2vec.SetPtEtaPhi(pt2,eta2,phi2);
284     if( pvec.DrEtaPhi(mu1vec) < 1.0 ||
285     pvec.DrEtaPhi(mu2vec) < 1.0 ) {
286     if( p1->pt > highest_gamma_pt ) {
287     highest_gamma_pt = p1->pt;
288     highest_gamma_eta = p1->eta;
289     highest_gamma_phi = p1->phi;
290     }
291     }
292     }
293    
294     gpt = highest_gamma_pt;
295     geta = highest_gamma_eta;
296     gphi = highest_gamma_phi;
297    
298     // cout << "bestM: " << bestM << endl;
299     count++;
300     passtuple->Fill();
301     }
302     // if( count > 100000 ) break;
303    
304    
305     } //end loop over data
306    
307    
308     file->cd();
309     passtuple->Write();
310     file->Close();
311    
312     delete info;
313     delete electronArr;
314     delete muonArr;
315    
316     }
317    
318    
319