ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/VHbbAnalysis/VHbbDataFormats/bin/Ntupler.cc
Revision: 1.19
Committed: Tue Sep 13 11:19:48 2011 UTC (13 years, 8 months ago) by arizzi
Content type: text/plain
Branch: MAIN
Changes since 1.18: +4 -3 lines
Log Message:
fix bug (thanks to Michele) in trigger weight.
HVMET in ntuple

File Contents

# User Rev Content
1 arizzi 1.1 #include <TH1F.h>
2     #include "PhysicsTools/Utilities/interface/LumiReWeighting.h"
3     #include <TH2F.h>
4     #include <TROOT.h>
5     #include <TFile.h>
6     #include <TTree.h>
7     #include <TSystem.h>
8     #include "DataFormats/FWLite/interface/Event.h"
9     #include "DataFormats/FWLite/interface/Handle.h"
10     #include "FWCore/FWLite/interface/AutoLibraryLoader.h"
11     #include "DataFormats/MuonReco/interface/Muon.h"
12     #include "DataFormats/PatCandidates/interface/Muon.h"
13     #include "PhysicsTools/FWLite/interface/TFileService.h"
14     #include "FWCore/ParameterSet/interface/ProcessDesc.h"
15     #include "FWCore/PythonParameterSet/interface/PythonProcessDesc.h"
16     #include "DataFormats/Math/interface/deltaR.h"
17     #include "DataFormats/Math/interface/deltaPhi.h"
18    
19     #include "DataFormats/FWLite/interface/LuminosityBlock.h"
20     #include "DataFormats/FWLite/interface/Run.h"
21     #include "DataFormats/Luminosity/interface/LumiSummary.h"
22    
23 arizzi 1.4 #include "VHbbAnalysis/VHbbDataFormats/interface/HbbCandidateFinderAlgo.h"
24     #include "VHbbAnalysis/VHbbDataFormats/src/HbbCandidateFinderAlgo.cc"
25 arizzi 1.1
26     #include "VHbbAnalysis/VHbbDataFormats/interface/VHbbEvent.h"
27     #include "VHbbAnalysis/VHbbDataFormats/interface/VHbbEventAuxInfo.h"
28     #include "VHbbAnalysis/VHbbDataFormats/interface/VHbbCandidate.h"
29     #include "VHbbAnalysis/VHbbDataFormats/interface/TriggerReader.h"
30 nmohr 1.5 #include "VHbbAnalysis/VHbbDataFormats/interface/TopMassReco.h"
31 arizzi 1.1
32 arizzi 1.16
33     //Move class definition to Ntupler.h ?
34     //#include "VHbbAnalysis/VHbbDataFormats/interface/Ntupler.h"
35    
36     #include "VHbbAnalysis/VHbbDataFormats/interface/BTagWeight.h"
37     #include "VHbbAnalysis/VHbbDataFormats/interface/TriggerWeight.h"
38    
39 arizzi 1.1 #include <sstream>
40     #include <string>
41    
42     #define MAXJ 30
43     #define MAXL 10
44    
45 arizzi 1.4
46 nmohr 1.7 bool jsonContainsEvent (const std::vector< edm::LuminosityBlockRange > &jsonVec,
47     const edm::EventBase &event)
48     {
49     // if the jsonVec is empty, then no JSON file was provided so all
50     // events should pass
51     if (jsonVec.empty())
52     {
53     return true;
54     }
55     bool (* funcPtr) (edm::LuminosityBlockRange const &,
56     edm::LuminosityBlockID const &) = &edm::contains;
57     edm::LuminosityBlockID lumiID (event.id().run(),
58     event.id().luminosityBlock());
59     std::vector< edm::LuminosityBlockRange >::const_iterator iter =
60     std::find_if (jsonVec.begin(), jsonVec.end(),
61     boost::bind(funcPtr, _1, lumiID) );
62     return jsonVec.end() != iter;
63    
64     }
65    
66 arizzi 1.1
67    
68     typedef struct
69     {
70     float mass; //MT in case of W
71     float pt;
72     float eta;
73     float phi;
74 arizzi 1.9 } TrackInfo;
75 arizzi 1.1
76    
77 arizzi 1.9 struct LeptonInfo
78 arizzi 1.1 {
79     void reset()
80     {
81     for(int i =0; i < MAXL;i++){ mass[i]=-99; pt[i]=-99; eta[i]=-99; phi[i]=-99; aodCombRelIso[i]=-99; pfCombRelIso[i]=-99; photonIso[i]=-99; neutralHadIso[i]=-99; chargedHadIso[i]=-99; particleIso[i]=-99; dxy[i]=-99; dz[i]=-99; type[i]=-99; }
82     }
83    
84     template <class Input> void set(const Input & i, int j,int t)
85     {
86     type[j]=t;
87     pt[j]=i.p4.Pt();
88     mass[j]=i.p4.M();
89     eta[j]=i.p4.Eta();
90     phi[j]=i.p4.Phi();
91     aodCombRelIso[j]=(i.hIso+i.eIso+i.tIso)/i.p4.Pt();
92     pfCombRelIso[j]=(i.pfChaIso+i.pfPhoIso+i.pfNeuIso)/i.p4.Pt();
93     photonIso[j]=i.pfPhoIso;
94     neutralHadIso[j]=i.pfNeuIso;
95     chargedHadIso[j]=i.pfChaIso;
96 arizzi 1.4 setID(i,j);
97 arizzi 1.1 }
98 arizzi 1.4 template <class Input> void setID(const Input & i, int j)
99     {
100     id[j]=-99;
101     }
102    
103    
104    
105 arizzi 1.1
106     float mass[MAXL]; //MT in case of W
107     float pt[MAXL];
108     float eta[MAXL];
109     float phi[MAXL];
110     float aodCombRelIso[MAXL];
111     float pfCombRelIso[MAXL];
112     float photonIso[MAXL];
113     float neutralHadIso[MAXL];
114     float chargedHadIso[MAXL];
115     float particleIso[MAXL];
116     float dxy[MAXL];
117     float dz[MAXL];
118     int type[MAXL];
119 arizzi 1.4 float id[MAXL];
120 arizzi 1.1 };
121    
122 arizzi 1.9 template <> void LeptonInfo::setID<VHbbEvent::ElectronInfo>(const VHbbEvent::ElectronInfo & i, int j){
123 arizzi 1.4 id[j]=i.id80r;
124     }
125    
126 arizzi 1.1 typedef struct
127     {
128     float et;
129     float sumet;
130     float sig;
131     float phi;
132 arizzi 1.9 } METInfo;
133 arizzi 1.1
134     typedef struct
135     {
136     float mht;
137     float ht;
138     float sig;
139     float phi;
140 arizzi 1.9 } MHTInfo;
141 nmohr 1.5
142     typedef struct
143     {
144     float mass;
145     float pt;
146     float wMass;
147 arizzi 1.9 } TopInfo;
148 arizzi 1.1
149 nmohr 1.7 typedef struct
150 arizzi 1.1 {
151     int run;
152     int lumi;
153     int event;
154 nmohr 1.7 int json;
155 arizzi 1.9 } EventInfo;
156 arizzi 1.1
157     typedef struct
158     {
159     void set(const VHbbEvent::SimpleJet & j, int i)
160     {
161     pt[i]=j.p4.Pt();
162     eta[i]=j.p4.Eta();
163     phi[i]=j.p4.Phi();
164     csv[i]=j.csv;
165 arizzi 1.3 //FIXME: numTracksSV (NEED EDM FIX)
166     //FIXME: chf; float nhf; float cef; float nef; float nch; nconstituents; (NEED EDM FIX)
167    
168 arizzi 1.1 flavour[i]=j.flavour;
169 arizzi 1.3 if(j.bestMCp4.Pt() > 0)
170     {
171     genPt[i]=j.bestMCp4.Pt();
172     genEta[i]=j.bestMCp4.Eta();
173     genPhi[i]=j.bestMCp4.Phi();
174     }
175     //FIXME JECUnc (NEED EDM FIX)
176    
177 arizzi 1.1 }
178     void reset()
179     {
180     for(int i=0;i<MAXJ;i++) {
181     pt[i]=-99; eta[i]=-99; phi[i]=-99; csv[i]=-99; cosTheta[i]=-99; numTracksSV[i]=-99; chf[i]=-99; nhf[i]=-99; cef[i]=-99; nef[i]=-99; nch[i]=-99; nconstituents[i]=-99; flavour[i]=-99; genPt[i]=-99; genEta[i]=-99; genPhi[i]=-99; JECUnc[i]=-99;
182     }
183     }
184     float pt[MAXJ];
185     float eta[MAXJ];
186     float phi[MAXJ];
187     float csv[MAXJ];
188     float cosTheta[MAXJ];
189     int numTracksSV[MAXJ];
190     float chf[MAXJ];
191     float nhf[MAXJ];
192     float cef[MAXJ];
193     float nef[MAXJ];
194     float nch[MAXJ];
195     float nconstituents[MAXJ];
196     float flavour[MAXJ];
197     float genPt[MAXJ];
198     float genEta[MAXJ];
199     float genPhi[MAXJ];
200     float JECUnc[MAXJ];
201    
202 arizzi 1.9 } JetInfo;
203 arizzi 1.1
204     int main(int argc, char* argv[])
205     {
206     gROOT->Reset();
207    
208     TTree *_outTree;
209 arizzi 1.9 METInfo MET;
210     MHTInfo MHT;
211     TopInfo top;
212     EventInfo EVENT;
213     // JetInfo jet1,jet2, addJet1, addJet2;
214     // lepton1,lepton2;
215     JetInfo hJets, aJets;
216     LeptonInfo vLeptons, aLeptons;
217 arizzi 1.1 int naJets=0, nhJets=0;
218 arizzi 1.9 TrackInfo H;
219     TrackInfo V;
220     int nvlep=0,nalep=0;
221 arizzi 1.1
222 arizzi 1.19 float jjdr,jjdPhi,jjdEta,HVdPhi,HMETdPhi,VMt,deltaPullAngle,deltaPullAngleAK7,gendrcc,gendrbb, genZpt, genWpt, weightTrig, minDeltaPhijetMET, jetPt_minDeltaPhijetMET , PUweight;
223 arizzi 1.3 int nofLeptons15,nofLeptons20, Vtype,numJets,numBJets,eventFlav;
224 arizzi 1.1 // bool isMET80_CJ80, ispfMHT150, isMET80_2CJ20,isMET65_2CJ20, isJETID,isIsoMu17;
225 arizzi 1.18 bool triggerFlags[500],hbhe;
226 arizzi 1.17 float btag1T2CSF=1.,btag2TSF=1.,btag1TSF=1.;
227 arizzi 1.1 // ----------------------------------------------------------------------
228     // First Part:
229     //
230     // * enable the AutoLibraryLoader
231     // * book the histograms of interest
232     // * open the input file
233     // ----------------------------------------------------------------------
234    
235     // load framework libraries
236     gSystem->Load( "libFWCoreFWLite" );
237     gSystem->Load("libDataFormatsFWLite");
238     AutoLibraryLoader::enable();
239    
240     // parse arguments
241     if ( argc < 2 ) {
242     return 0;
243     }
244    
245 arizzi 1.4 std::vector<VHbbCandidate> * candZlocal = new std::vector<VHbbCandidate>;
246     std::vector<VHbbCandidate> * candWlocal = new std::vector<VHbbCandidate>;
247    
248 arizzi 1.1 // get the python configuration
249     PythonProcessDesc builder(argv[1]);
250     const edm::ParameterSet& in = builder.processDesc()->getProcessPSet()->getParameter<edm::ParameterSet>("fwliteInput" );
251     const edm::ParameterSet& out = builder.processDesc()->getProcessPSet()->getParameter<edm::ParameterSet>("fwliteOutput");
252     const edm::ParameterSet& ana = builder.processDesc()->getProcessPSet()->getParameter<edm::ParameterSet>("Analyzer");
253 nmohr 1.7 std::vector<edm::LuminosityBlockRange> jsonVector;
254     if ( in.exists("lumisToProcess") )
255     {
256     std::vector<edm::LuminosityBlockRange> const & lumisTemp =
257     in.getUntrackedParameter<std::vector<edm::LuminosityBlockRange> > ("lumisToProcess");
258     jsonVector.resize( lumisTemp.size() );
259     copy( lumisTemp.begin(), lumisTemp.end(), jsonVector.begin() );
260     }
261 arizzi 1.1
262     // now get each parameter
263     int maxEvents_( in.getParameter<int>("maxEvents") );
264     unsigned int outputEvery_( in.getParameter<unsigned int>("outputEvery") );
265     std::string outputFile_( out.getParameter<std::string>("fileName" ) );
266    
267    
268     std::vector<std::string> triggers( ana.getParameter<std::vector<std::string> >("triggers") );
269 arizzi 1.6 double btagThr = ana.getParameter<double>("bJetCountThreshold" );
270 arizzi 1.4 bool fromCandidate = ana.getParameter<bool>("readFromCandidates");
271 arizzi 1.17
272     bool useHighestPtHiggsZ = ana.getParameter<bool>("useHighestPtHiggsZ");
273     bool useHighestPtHiggsW = ana.getParameter<bool>("useHighestPtHiggsW");
274     HbbCandidateFinderAlgo * algoZ = new HbbCandidateFinderAlgo(ana.getParameter<bool>("verbose"), ana.getParameter<double>("jetPtThresholdZ"),useHighestPtHiggsZ );
275     HbbCandidateFinderAlgo * algoW = new HbbCandidateFinderAlgo(ana.getParameter<bool>("verbose"), ana.getParameter<double>("jetPtThresholdW"),useHighestPtHiggsW);
276 arizzi 1.4
277 arizzi 1.16 TriggerWeight triggerWeight(ana);
278 arizzi 1.17 BTagWeight btag(2); // 2 operating points "Custom" = 0.5 and "Tight = 0.898"
279     BTagSampleEfficiency btagEff( ana.getParameter<std::string>("btagEffFileName" ).c_str() );
280 arizzi 1.1
281     std::vector<std::string> inputFiles_( in.getParameter<std::vector<std::string> >("fileNames") );
282     // std::string inputFile( in.getParameter<std::string> ("fileName") );
283    
284    
285 arizzi 1.3 std::string PUmcfileName_ = in.getParameter<std::string> ("PUmcfileName") ;
286     std::string PUdatafileName_ = in.getParameter<std::string> ("PUdatafileName") ;
287 arizzi 1.1 bool isMC_( ana.getParameter<bool>("isMC") );
288     TriggerReader trigger(isMC_);
289 arizzi 1.18 TriggerReader patFilters(false);
290 arizzi 1.1
291 arizzi 1.6 // TFile *_outPUFile = new TFile((outputFile_+"_PU").c_str(), "recreate");
292     // TH1F * pu = new TH1F("pileup","",-0.5,24.5,25);
293 arizzi 1.1 TFile *_outFile = new TFile(outputFile_.c_str(), "recreate");
294 arizzi 1.16 TH1F * count = new TH1F("Count","Count", 1,0,2 );
295     TH1F * countWithPU = new TH1F("CountWithPU","CountWithPU", 1,0,2 );
296 arizzi 1.1 _outTree = new TTree("tree", "myTree");
297    
298     _outTree->Branch("H" , &H , "mass/F:pt/F:eta:phi/F");
299     _outTree->Branch("V" , &V , "mass/F:pt/F:eta:phi/F");
300     _outTree->Branch("nhJets" , &nhJets , "nhJets/I");
301     _outTree->Branch("naJets" , &naJets , "naJets/I");
302 arizzi 1.2
303     _outTree->Branch("hJet_pt",hJets.pt ,"pt[nhJets]/F");
304     _outTree->Branch("hJet_eta",hJets.eta ,"eta[nhJets]/F");
305     _outTree->Branch("hJet_phi",hJets.phi ,"phi[nhJets]/F");
306     _outTree->Branch("hJet_csv",hJets.csv ,"csv[nhJets]/F");
307     _outTree->Branch("hJet_cosTheta",hJets.cosTheta ,"cosTheta[nhJets]/F");
308     _outTree->Branch("hJet_numTracksSV",hJets.numTracksSV ,"numTracksSV[nhJets]/I");
309     _outTree->Branch("hJet_chf",hJets.chf ,"chf[nhJets]/F");
310     _outTree->Branch("hJet_nhf",hJets.nhf ,"nhf[nhJets]/F");
311     _outTree->Branch("hJet_cef",hJets.cef ,"cef[nhJets]/F");
312     _outTree->Branch("hJet_nef",hJets.nef ,"nef[nhJets]/F");
313     _outTree->Branch("hJet_nch",hJets.nch ,"nch[nhJets]/F");
314     _outTree->Branch("hJet_nconstituents",hJets.nconstituents ,"nconstituents[nhJets]");
315     _outTree->Branch("hJet_flavour",hJets.flavour ,"flavour[nhJets]/F");
316     _outTree->Branch("hJet_genPt",hJets.genPt ,"genPt[nhJets]/F");
317     _outTree->Branch("hJet_genEta",hJets.genEta ,"genEta[nhJets]/F");
318     _outTree->Branch("hJet_genPhi",hJets.genPhi ,"genPhi[nhJets]/F");
319     _outTree->Branch("hJet_JECUnc",hJets.JECUnc ,"JECUnc[nhJets]/F");
320    
321     _outTree->Branch("aJet_pt",aJets.pt ,"pt[naJets]/F");
322     _outTree->Branch("aJet_eta",aJets.eta ,"eta[naJets]/F");
323     _outTree->Branch("aJet_phi",aJets.phi ,"phi[naJets]/F");
324     _outTree->Branch("aJet_csv",aJets.csv ,"csv[naJets]/F");
325     _outTree->Branch("aJet_cosTheta",aJets.cosTheta ,"cosTheta[naJets]/F");
326     _outTree->Branch("aJet_numTracksSV",aJets.numTracksSV ,"numTracksSV[naJets]/I");
327     _outTree->Branch("aJet_chf",aJets.chf ,"chf[naJets]/F");
328     _outTree->Branch("aJet_nhf",aJets.nhf ,"nhf[naJets]/F");
329     _outTree->Branch("aJet_cef",aJets.cef ,"cef[naJets]/F");
330     _outTree->Branch("aJet_nef",aJets.nef ,"nef[naJets]/F");
331     _outTree->Branch("aJet_nch",aJets.nch ,"nch[naJets]/F");
332     _outTree->Branch("aJet_nconstituents",aJets.nconstituents ,"nconstituents[naJets]");
333     _outTree->Branch("aJet_flavour",aJets.flavour ,"flavour[naJets]/F");
334     _outTree->Branch("aJet_genPt",aJets.genPt ,"genPt[naJets]/F");
335     _outTree->Branch("aJet_genEta",aJets.genEta ,"genEta[naJets]/F");
336     _outTree->Branch("aJet_genPhi",aJets.genPhi ,"genPhi[naJets]/F");
337     _outTree->Branch("aJet_JECUnc",aJets.JECUnc ,"JECUnc[naJets]/F");
338    
339 arizzi 1.1
340     _outTree->Branch("jjdr" , &jjdr , "jjdr/F" );
341     _outTree->Branch("jjdPhi" , &jjdPhi , "jjdPhi/F" );
342 arizzi 1.6 _outTree->Branch("jjdEta" , &jjdEta , "jjdEta/F" );
343 arizzi 1.1 _outTree->Branch("numJets" , &numJets , "numJets/I" );
344     _outTree->Branch("numBJets" , &numBJets , "numBJets/I" );
345     _outTree->Branch("nofLeptons15" , &nofLeptons15 , "nofLeptons15/I" );
346     _outTree->Branch("nofLeptons20" , &nofLeptons20 , "nofLeptons20/I" );
347     _outTree->Branch("deltaPullAngle", &deltaPullAngle , "deltaPullAngle/F");
348     _outTree->Branch("gendrcc" , &gendrcc , "gendrcc/F");
349     _outTree->Branch("gendrbb" , &gendrbb , "gendrbb/F");
350     _outTree->Branch("genZpt" , &genZpt , "genZpt/F");
351     _outTree->Branch("genWpt" , &genWpt , "genWpt/F");
352     _outTree->Branch("weightTrig" , &weightTrig , "weightTrig/F");
353     _outTree->Branch("deltaPullAngleAK7", &deltaPullAngleAK7 , "deltaPullAngleAK7/F");
354     _outTree->Branch("PUweight", &PUweight , "PUweight/F");
355 arizzi 1.3 _outTree->Branch("eventFlav", &eventFlav , "eventFlav/I");
356 arizzi 1.1
357    
358    
359    
360     _outTree->Branch("Vtype" , &Vtype , "Vtype/I" );
361     _outTree->Branch("HVdPhi" , &HVdPhi , "HVdPhi/F" );
362 arizzi 1.19 _outTree->Branch("HMETdPhi" , &HMETdPhi , "HMETdPhi/F" );
363 arizzi 1.1 _outTree->Branch("VMt" , &VMt , "VMt/F" );
364    
365 arizzi 1.9 _outTree->Branch("nvlep" , &nvlep , "nvlep/I");
366     _outTree->Branch("nalep" , &nalep , "nalep/I");
367 arizzi 1.3
368 arizzi 1.9 _outTree->Branch("vLepton_mass",vLeptons.mass ,"mass[nvlep]/F");
369     _outTree->Branch("vLepton_pt",vLeptons.pt ,"pt[nvlep]/F");
370     _outTree->Branch("vLepton_eta",vLeptons.eta ,"eta[nvlep]");
371     _outTree->Branch("vLepton_phi",vLeptons.phi ,"phi[nvlep]/F");
372     _outTree->Branch("vLepton_aodCombRelIso",vLeptons.aodCombRelIso ,"aodCombRelIso[nvlep]/F");
373     _outTree->Branch("vLepton_pfCombRelIso",vLeptons.pfCombRelIso ,"pfCombRelIso[nvlep]/F");
374     _outTree->Branch("vLepton_photonIso",vLeptons.photonIso ,"photonIso[nvlep]/F");
375     _outTree->Branch("vLepton_neutralHadIso",vLeptons.neutralHadIso ,"neutralHadIso[nvlep]/F");
376     _outTree->Branch("vLepton_chargedHadIso",vLeptons.chargedHadIso ,"chargedHadIso[nvlep]/F");
377     _outTree->Branch("vLepton_particleIso",vLeptons.particleIso ,"particleIso[nvlep]/F");
378     _outTree->Branch("vLepton_dxy",vLeptons.dxy ,"dxy[nvlep]/F");
379     _outTree->Branch("vLepton_dz",vLeptons.dz ,"dz[nvlep]/F");
380     _outTree->Branch("vLepton_type",vLeptons.type ,"type[nvlep]/I");
381     _outTree->Branch("vLepton_id",vLeptons.id ,"id[nvlep]/F");
382    
383     _outTree->Branch("aLepton_mass",aLeptons.mass ,"mass[nalep]/F");
384     _outTree->Branch("aLepton_pt",aLeptons.pt ,"pt[nalep]/F");
385     _outTree->Branch("aLepton_eta",aLeptons.eta ,"eta[nalep]");
386     _outTree->Branch("aLepton_phi",aLeptons.phi ,"phi[nalep]/F");
387     _outTree->Branch("aLepton_aodCombRelIso",aLeptons.aodCombRelIso ,"aodCombRelIso[nalep]/F");
388     _outTree->Branch("aLepton_pfCombRelIso",aLeptons.pfCombRelIso ,"pfCombRelIso[nalep]/F");
389     _outTree->Branch("aLepton_photonIso",aLeptons.photonIso ,"photonIso[nalep]/F");
390     _outTree->Branch("aLepton_neutralHadIso",aLeptons.neutralHadIso ,"neutralHadIso[nalep]/F");
391     _outTree->Branch("aLepton_chargedHadIso",aLeptons.chargedHadIso ,"chargedHadIso[nalep]/F");
392     _outTree->Branch("aLepton_particleIso",aLeptons.particleIso ,"particleIso[nalep]/F");
393     _outTree->Branch("aLepton_dxy",aLeptons.dxy ,"dxy[nalep]/F");
394     _outTree->Branch("aLepton_dz",aLeptons.dz ,"dz[nalep]/F");
395     _outTree->Branch("aLepton_type",aLeptons.type ,"type[nalep]/I");
396     _outTree->Branch("aLepton_id",aLeptons.id ,"id[nalep]/F");
397    
398 arizzi 1.8 _outTree->Branch("top" , &top , "mass/F:pt/F:wMass/F");
399 arizzi 1.1
400     _outTree->Branch("MET" , &MET , "et/F:sumet:sig/F:phi/F");
401     _outTree->Branch("MHT" , &MHT , "mht/F:ht:sig/F:phi/F");
402     _outTree->Branch("minDeltaPhijetMET" , &minDeltaPhijetMET , "minDeltaPhijetMET/F");
403     _outTree->Branch("jetPt_minDeltaPhijetMET" , &jetPt_minDeltaPhijetMET , "jetPt_minDeltaPhijetMET/F");
404    
405     std::stringstream s;
406     s << "triggerFlags[" << triggers.size() << "]/b";
407     _outTree->Branch("triggerFlags", triggerFlags, s.str().c_str());
408 nmohr 1.7
409 arizzi 1.17
410 nmohr 1.7 _outTree->Branch("EVENT" , &EVENT , "run/I:lumi/I:event/I:json/I");
411 arizzi 1.18 _outTree->Branch("hbhe" , &hbhe , "hbhe/b");
412 arizzi 1.17 _outTree->Branch("btag1TSF" , &btag1TSF , "btag1TSF/F");
413     _outTree->Branch("btag2TSF" , &btag2TSF , "btag2TSF/F");
414     _outTree->Branch("btag1T2CSF" , &btag1T2CSF , "btag1T2CSF/F");
415 arizzi 1.1
416     int ievt=0;
417     int totalcount=0;
418    
419     // TFile* inFile = new TFile(inputFile.c_str(), "read");
420     for(unsigned int iFile=0; iFile<inputFiles_.size(); ++iFile) {
421     std::cout << iFile << std::endl;
422     TFile* inFile = TFile::Open(inputFiles_[iFile].c_str());
423     if(inFile==0) continue;
424    
425     // loop the events
426    
427     fwlite::Event ev(inFile);
428     for(ev.toBegin(); !ev.atEnd(); ++ev, ++ievt)
429     {
430 arizzi 1.16 count->Fill(1.);
431 arizzi 1.1
432     if(isMC_){
433     // PU weights
434 arizzi 1.3
435 arizzi 1.9 // edm::LumiReWeighting LumiWeights_ = edm::LumiReWeighting(PUmcfileName_,PUdatafileName_ , "pileup", "pileup");
436     // double avg=0;
437 arizzi 1.3 //FIXME: PU (NEED EDM FIX)
438 arizzi 1.1 // if( PUintimeSizes.isValid() && PUouttime1minusSizes.isValid() && PUouttime1plusSizes.isValid()){
439     // avg = (double)( *PUintimeSizes );
440     // }
441 arizzi 1.3 PUweight = 1.0; // FIXME: LumiWeights_.weight3BX( avg /3.); (NEED EDM FIX)
442 arizzi 1.1 }
443 arizzi 1.16 countWithPU->Fill(PUweight);
444 nmohr 1.7
445     //Write event info
446     EVENT.run = ev.id().run();
447     EVENT.lumi = ev.id().luminosityBlock();
448     EVENT.event = ev.id().event();
449     EVENT.json = jsonContainsEvent (jsonVector, ev);
450 arizzi 1.3
451 arizzi 1.4
452     const std::vector<VHbbCandidate> * candZ ;
453     const std::vector<VHbbCandidate> * candW ;
454 arizzi 1.13 const VHbbEvent * iEvent =0;
455 arizzi 1.4 if(fromCandidate)
456     {
457 arizzi 1.3 fwlite::Handle< std::vector<VHbbCandidate> > vhbbCandHandleZ;
458     vhbbCandHandleZ.getByLabel(ev,"hbbBestCSVPt20Candidates");
459 arizzi 1.4 candZ = vhbbCandHandleZ.product();
460 arizzi 1.3
461     fwlite::Handle< std::vector<VHbbCandidate> > vhbbCandHandle;
462     vhbbCandHandle.getByLabel(ev,"hbbHighestPtHiggsPt30Candidates");
463 arizzi 1.4 candW = vhbbCandHandle.product();
464     }
465     else
466     {
467     candZlocal->clear();
468     candWlocal->clear();
469     fwlite::Handle< VHbbEvent > vhbbHandle;
470     vhbbHandle.getByLabel(ev,"HbbAnalyzerNew");
471 arizzi 1.13 iEvent = vhbbHandle.product();
472 arizzi 1.4 algoZ->run(vhbbHandle.product(),*candZlocal);
473     algoW->run(vhbbHandle.product(),*candWlocal);
474     candZ= candZlocal;
475     candW= candWlocal;
476    
477    
478 arizzi 1.1
479 arizzi 1.4 }
480 arizzi 1.1
481 arizzi 1.3 const std::vector<VHbbCandidate> * cand = candZ;
482 arizzi 1.1
483    
484     fwlite::Handle< VHbbEventAuxInfo > vhbbAuxHandle;
485     vhbbAuxHandle.getByLabel(ev,"HbbAnalyzerNew");
486     const VHbbEventAuxInfo & aux = *vhbbAuxHandle.product();
487    
488     /* fwlite::Handle< VHbbEvent > vhbbHandle;
489     vhbbHandle.getByLabel(ev,"HbbAnalyzerNew");
490     const VHbbEvent iEvent = *vhbbHandle.product();
491     */
492    
493     // std::clog << "Filling tree "<< std::endl;
494 arizzi 1.17 bool isW=false;
495    
496 arizzi 1.3 if(cand->size() == 0 or cand->at(0).H.jets.size() < 2) continue;
497     if(cand->size() > 1 )
498     {
499     std::cout << "MULTIPLE CANDIDATES: " << cand->size() << std::endl;
500     }
501 arizzi 1.17 if(cand->at(0).candidateType == VHbbCandidate::Wmun || cand->at(0).candidateType == VHbbCandidate::Wen ) { cand=candW; isW=true; }
502 arizzi 1.3 if(cand->size() == 0)
503     {
504     // std::cout << "W event loss due to tigther cuts" << std::endl;
505     continue;
506     }
507     const VHbbCandidate & vhCand = cand->at(0);
508 arizzi 1.18 patFilters.setEvent(&ev,"VH");
509     hbhe = patFilters.accept("hbhe");
510    
511 arizzi 1.4 trigger.setEvent(&ev);
512     for(size_t j=0;j < triggers.size();j++)
513     triggerFlags[j]=trigger.accept(triggers[j]);
514 arizzi 1.3
515     eventFlav=0;
516     if(aux.mcBbar.size() > 0 || aux.mcB.size() > 0) eventFlav=5;
517     else if(aux.mcC.size() > 0) eventFlav=4;
518 arizzi 1.8
519 arizzi 1.3
520 arizzi 1.1 H.mass = vhCand.H.p4.M();
521     H.pt = vhCand.H.p4.Pt();
522     H.eta = vhCand.H.p4.Eta();
523     H.phi = vhCand.H.p4.Phi();
524     V.mass = vhCand.V.p4.M();
525     V.pt = vhCand.V.p4.Pt();
526     V.eta = vhCand.V.p4.Eta();
527     V.phi = vhCand.V.p4.Phi();
528     nhJets=2;
529     hJets.set(vhCand.H.jets[0],0);
530     hJets.set(vhCand.H.jets[1],1);
531     aJets.reset();
532     naJets=vhCand.additionalJets.size();
533 arizzi 1.6 numBJets=0;
534     if(vhCand.H.jets[0].csv> btagThr) numBJets++;
535     if(vhCand.H.jets[1].csv> btagThr) numBJets++;
536     for( int j=0; j < naJets && j < MAXJ; j++ )
537     {
538     aJets.set(vhCand.additionalJets[j],j);
539     if(vhCand.additionalJets[j].csv> btagThr) numBJets++;
540     }
541 arizzi 1.1 numJets = vhCand.additionalJets.size()+2;
542     jjdr = deltaR(vhCand.H.jets[0].p4.Eta(),vhCand.H.jets[0].p4.Phi(),vhCand.H.jets[1].p4.Eta(),vhCand.H.jets[1].p4.Phi());
543     jjdPhi = deltaPhi(vhCand.H.jets[0].p4.Phi(),vhCand.H.jets[1].p4.Phi());
544 arizzi 1.6 jjdEta= TMath::Abs( vhCand.H.jets[0].p4.Eta() - vhCand.H.jets[1].p4.Eta() );
545 arizzi 1.19 HVdPhi = fabs( deltaPhi(vhCand.H.p4.Phi(),vhCand.V.p4.Phi()) ) ;
546     HMETdPhi = fabs( deltaPhi(vhCand.H.p4.Phi(),vhCand.mets.at(0).p4.Phi()) ) ;
547 nmohr 1.14 VMt = vhCand.Mt() ;
548 arizzi 1.1 deltaPullAngle = vhCand.H.deltaTheta;
549 arizzi 1.16
550 arizzi 1.1
551 arizzi 1.3 hJets.cosTheta[0]= vhCand.H.helicities[0];
552     hJets.cosTheta[1]= vhCand.H.helicities[1];
553 arizzi 1.1
554     MET.et = vhCand.V.mets.at(0).p4.Pt();
555     MET.phi = vhCand.V.mets.at(0).p4.Phi();
556     MET.sumet = vhCand.V.mets.at(0).sumEt;
557     MET.sig = vhCand.V.mets.at(0).metSig;
558 arizzi 1.3 //FIXME add MHT _outTree->Branch("MHT" , &MHT , "mht/F:ht:sig/F:phi/F"); (NEED EDM FIX)
559 arizzi 1.1 Vtype = vhCand.candidateType;
560 arizzi 1.9 vLeptons.reset();
561 arizzi 1.15 weightTrig = 1.; // better to default to 1
562 arizzi 1.8 TLorentzVector leptonForTop;
563 arizzi 1.9 size_t firstAddMu=0;
564     size_t firstAddEle=0;
565 arizzi 1.1 if(Vtype == VHbbCandidate::Zmumu ){
566 arizzi 1.13 vLeptons.set(vhCand.V.muons[0],0,13);
567     vLeptons.set(vhCand.V.muons[1],1,13);
568 arizzi 1.16 float cweightID = triggerWeight.scaleMuID(vLeptons.pt[0],vLeptons.eta[0]) * triggerWeight.scaleMuID(vLeptons.pt[1],vLeptons.eta[1]) ;
569     float weightTrig1 = triggerWeight.scaleMuIsoHLT(vLeptons.pt[0],vLeptons.eta[0]);
570     float weightTrig2 = triggerWeight.scaleMuIsoHLT(vLeptons.pt[1],vLeptons.eta[1]);
571 arizzi 1.1 float cweightTrig = weightTrig1 + weightTrig2 - weightTrig1*weightTrig2;
572     weightTrig = cweightID * cweightTrig;
573 arizzi 1.9 nvlep=2;
574     firstAddMu=2;
575 arizzi 1.1 }
576     if( Vtype == VHbbCandidate::Zee ){
577 arizzi 1.9 vLeptons.set(vhCand.V.electrons[0],0,11);
578     vLeptons.set(vhCand.V.electrons[1],1,11);
579     nvlep=2;
580     firstAddEle=2;
581 arizzi 1.3 //FIXME: trigger weights for electrons
582 arizzi 1.1 }
583     if(Vtype == VHbbCandidate::Wmun ){
584 arizzi 1.8 leptonForTop=vhCand.V.muons[0].p4;
585 arizzi 1.13 vLeptons.set(vhCand.V.muons[0],0,13);
586 arizzi 1.16 float cweightID = triggerWeight.scaleMuID(vLeptons.pt[0],vLeptons.eta[0]);
587     float weightTrig1 = triggerWeight.scaleMuIsoHLT(vLeptons.pt[0],vLeptons.eta[0]);
588 arizzi 1.1 float cweightTrig = weightTrig1;
589     weightTrig = cweightID * cweightTrig;
590 arizzi 1.9 nvlep=1;
591     firstAddMu=1;
592 arizzi 1.1 }
593     if( Vtype == VHbbCandidate::Wen ){
594 arizzi 1.8 leptonForTop=vhCand.V.electrons[0].p4;
595 arizzi 1.9 vLeptons.set(vhCand.V.electrons[0],0,11);
596     nvlep=1;
597     firstAddEle=1;
598 arizzi 1.1 }
599 arizzi 1.3 if( Vtype == VHbbCandidate::Znn ){
600 arizzi 1.9 nvlep=0;
601 arizzi 1.3 //FIXME: trigger weights for Znn
602    
603     }
604 arizzi 1.9
605     aLeptons.reset();
606     nalep=0;
607 arizzi 1.13 for(size_t j=firstAddMu;j< vhCand.V.muons.size();j++) aLeptons.set(vhCand.V.muons[j],nalep++,13);
608 arizzi 1.11 for(size_t j=firstAddEle;j< vhCand.V.electrons.size();j++) aLeptons.set(vhCand.V.electrons[j],nalep++,11);
609 arizzi 1.8
610    
611 arizzi 1.17 //Loop on jets
612    
613 arizzi 1.8 double maxBtag=-99999;
614 arizzi 1.16 minDeltaPhijetMET = 999;
615 arizzi 1.8 TLorentzVector bJet;
616 arizzi 1.17 std::vector<std::vector<BTagWeight::JetInfo> > btagJetInfos;
617 arizzi 1.8 for(unsigned int j=0; j < vhCand.H.jets.size(); j++ ){
618     if (vhCand.H.jets[j].csv > maxBtag) { bJet=vhCand.H.jets[j].p4 ; maxBtag =vhCand.H.jets[j].csv; }
619 arizzi 1.16 if (deltaPhi( vhCand.V.mets.at(0).p4.Phi(), vhCand.H.jets[j].p4.Phi()) < minDeltaPhijetMET)
620     {
621     minDeltaPhijetMET=deltaPhi( vhCand.V.mets.at(0).p4.Phi(), vhCand.H.jets[j].p4.Phi());
622     jetPt_minDeltaPhijetMET=vhCand.H.jets[j].p4.Pt();
623     }
624 arizzi 1.17 btagJetInfos.push_back(btagEff.jetInfo(vhCand.H.jets[j]));
625 arizzi 1.8 }
626     for(unsigned int j=0; j < vhCand.additionalJets.size(); j++ ){
627     if (vhCand.additionalJets[j].csv > maxBtag) { bJet=vhCand.additionalJets[j].p4 ; maxBtag =vhCand.additionalJets[j].csv; }
628 arizzi 1.16 if (deltaPhi( vhCand.V.mets.at(0).p4.Phi(), vhCand.additionalJets[j].p4.Phi()) < minDeltaPhijetMET)
629     {
630     minDeltaPhijetMET=deltaPhi( vhCand.V.mets.at(0).p4.Phi(), vhCand.additionalJets[j].p4.Phi());
631     jetPt_minDeltaPhijetMET=vhCand.additionalJets[j].p4.Pt();
632     }
633 arizzi 1.17 if( ( isW && ! useHighestPtHiggsW ) || ( ! isW && ! useHighestPtHiggsZ ) ) // btag SF computed using only H-jets if best-H made with dijetPt rather than best CSV
634     {
635     btagJetInfos.push_back(btagEff.jetInfo(vhCand.additionalJets[j]));
636     }
637     }
638     if(isMC_)
639     {
640     //std::cout << "BTAGSF " << btagJetInfos.size() << " " << btag.weight<BTag1Tight2CustomFilter>(btagJetInfos) << std::endl;
641     btag1T2CSF = btag.weight<BTag1Tight2CustomFilter>(btagJetInfos);
642     btag2TSF = btag.weight<BTag1Tight2CustomFilter>(btagJetInfos);
643     btag1TSF = btag.weight<BTag1Tight2CustomFilter>(btagJetInfos);;
644 arizzi 1.16 }
645 arizzi 1.17
646 arizzi 1.8 if(maxBtag > -99999)
647     {
648     TopHypo topQuark = TopMassReco::topMass(leptonForTop,bJet,vhCand.V.mets.at(0).p4);
649     top.mass = topQuark.p4.M();
650     top.pt = topQuark.p4.Pt();
651     top.wMass = topQuark.p4W.M();
652     } else {
653     top.mass = -99;
654     top.pt = -99;
655     top.wMass = -99;
656     }
657    
658    
659    
660 arizzi 1.1 //FIXME _outTree->Branch("nofLeptons15" , &nofLeptons15 , "nofLeptons15/I" );
661     nofLeptons20= vhCand.additionalLeptons();
662     // if(aux.mcC.size() >=2)
663     // std::cout << "C Must not be zero and it is ... " << aux.mcC[1].p4.Pt() << std::endl;
664     // if(aux.mcB.size() >=1)
665     // std::cout << "B Must not be zero and it is ... " << aux.mcB[0].p4.Pt() << std::endl;
666    
667 arizzi 1.3 // FIXME gendrcc=aux.genCCDeltaR(); (NEED EDM FIX)
668    
669     // FIXME gendrbb=aux.genBBDeltaR(); (NEED EDM FIX)
670 arizzi 1.1 genZpt=aux.mcZ.size() > 0 ? aux.mcZ[0].p4.Pt():-99;
671     genWpt=aux.mcW.size() > 0 ? aux.mcW[0].p4.Pt():-99;
672 arizzi 1.3
673 arizzi 1.1 //FIXME: _outTree->Branch("deltaPullAngleAK7", &deltaPullAngleAK7 , "deltaPullAngleAK7/F");
674    
675    
676    
677 arizzi 1.17
678 arizzi 1.1 _outTree->Fill();
679 arizzi 1.4
680 arizzi 1.1 }// closed event loop
681    
682     std::cout << "closing the file: " << inputFiles_[iFile] << std::endl;
683     inFile->Close();
684     // close input file
685     } // loop on files
686    
687    
688     std::cout << "Events: " << ievt <<std::endl;
689     std::cout << "TotalCount: " << totalcount <<std::endl;
690    
691    
692    
693     _outFile->cd();
694    
695     _outTree->Write();
696     _outFile->Write();
697     _outFile->Close();
698     return 0;
699     }
700    
701