ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Vuko/WZAnalysis/src/MuonAnalyzer.cc
Revision: 1.1
Committed: Thu Mar 27 09:35:54 2008 UTC (17 years, 1 month ago) by senka
Content type: text/plain
Branch: MAIN
CVS Tags: keti_Apr9_01, vuko-4apr08
Log Message:
muon fake rate analyzer

File Contents

# User Rev Content
1 senka 1.1 // -*- C++ -*-
2     //
3     // Package: WZAnalyzer
4     // Class: WZAnalyzer
5     //
6     /**\class WZAnalyzer WZAnalyzer.cc Vuko/WZAnalysis/src/WZAnalyzer.cc
7    
8     Description: <one line class summary>
9    
10     Implementation:
11     <Notes on implementation>
12     */
13     //
14     // Original Author: Vuko Brigljevic
15     // Created: Fri Nov 9 11:07:27 CET 2007
16     // $Id: WZAnalyzer.cc,v 1.48 2008/02/28 13:21:04 smorovic Exp $
17     //
18     //
19    
20    
21    
22     // system include files
23     #include <memory>
24    
25     // user include files
26     #include "FWCore/Framework/interface/Frameworkfwd.h"
27     #include "FWCore/Framework/interface/EDAnalyzer.h"
28    
29     #include "FWCore/Framework/interface/Event.h"
30     #include "FWCore/Framework/interface/MakerMacros.h"
31    
32     #include "FWCore/ParameterSet/interface/ParameterSet.h"
33    
34     #include "Vuko/WZAnalysis/interface/MuonAnalyzer.h"
35     #include "DataFormats/Candidate/interface/OverlapChecker.h"
36    
37     #include "Vuko/WZAnalysis/interface/ElectronProperties.h"
38     #include "Vuko/WZAnalysis/interface/MuonProperties.h"
39    
40     #include "DataFormats/Common/interface/TriggerResults.h"
41    
42     //--- muon AOD:
43     #include "DataFormats/JetReco/interface/CaloJetCollection.h"
44     #include "DataFormats/EgammaCandidates/interface/Electron.h"
45     #include "DataFormats/EgammaCandidates/interface/ElectronFwd.h"
46     #include "DataFormats/MuonReco/interface/MuonFwd.h"
47     #include "DataFormats/MuonReco/interface/Muon.h"
48     #include "DataFormats/MuonReco/interface/MuIsoDeposit.h"
49     #include "DataFormats/EgammaReco/interface/BasicCluster.h"
50     #include "DataFormats/EgammaReco/interface/BasicClusterFwd.h"
51     #include "DataFormats/EgammaCandidates/interface/PixelMatchGsfElectron.h"
52    
53     #include "DataFormats/METReco/interface/CaloMET.h"
54    
55    
56     #include "TFile.h"
57     #include "TH1F.h"
58     #include "TH2F.h"
59     #include "TTree.h"
60    
61     #include <map>
62    
63     //
64     // constants, enums and typedefs
65     //
66    
67     //
68     // static data member definitions
69     //
70    
71     //
72     // constructors and destructor
73     //
74     MuonAnalyzer::MuonAnalyzer(const edm::ParameterSet& iConfig)
75    
76     {
77    
78     //now do what ever initialization is needed
79    
80     fOutputFileName = iConfig.getUntrackedParameter<string>("HistOutFile");
81     theMCTruthCollection = iConfig.getParameter<edm::InputTag>("genParticles");
82    
83     // theMCTruthCollection = iConfig.getParameter<edm::InputTag>("genParticleCollection");
84    
85     theLooseMuonCollection = iConfig.getParameter<edm::InputTag>("LooseMuons");
86     theGoodMuonCollection = iConfig.getParameter<edm::InputTag>("GoodMuons");
87     theLooseElectronCollection = iConfig.getParameter<edm::InputTag>("LooseElectrons");
88     theGoodElectronCollection = iConfig.getParameter<edm::InputTag>("GoodElectrons");
89     theTightLeptonCollection = iConfig.getParameter<edm::InputTag>("TightLeptons");
90     // theMediumElectronCollection = iConfig.getParameter<edm::InputTag>("LooseElectrons");
91     // theTightElectronCollection = iConfig.getParameter<edm::InputTag>("TightElectrons");
92     // Z's
93     theZeeCollection = iConfig.getParameter<edm::InputTag>("ZtoEE");
94     theZmumuCollection = iConfig.getParameter<edm::InputTag>("ZtoMuMu");
95     theZllCollection = iConfig.getParameter<edm::InputTag>("ZtoLL");
96    
97     theJetCollection = iConfig.getParameter<edm::InputTag>("Jets");
98    
99     theWeightCollection = iConfig.getParameter<edm::InputTag>("EventWeight");
100     alpgenIDTag = iConfig.getParameter<edm::InputTag>("AlpgenIDTag");
101    
102     getAlpgenID = false;
103     getAlpgenID = iConfig.getParameter<bool>("getAlpgenID");
104    
105     storeHLTresults=false;
106     storeHLTresults = iConfig.getParameter<bool>("storeHLTresults");
107    
108     getEventWeight = false;
109     getEventWeight = iConfig.getParameter<bool>("getEventWeight");
110    
111     triggerResultsTag = iConfig.getParameter<edm::InputTag>("TriggerResults");
112     firstTriggerResult = true;
113     }
114    
115    
116     MuonAnalyzer::~MuonAnalyzer()
117     {
118    
119     // do anything here that needs to be done at desctruction time
120     // (e.g. close files, deallocate resources etc.)
121    
122     }
123    
124    
125     //
126     // member functions
127     //
128    
129     // ------------ method called to for each event ------------
130     void
131     MuonAnalyzer::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
132     {
133    
134     cout <<" ulazi u analyze metodu" << endl;
135     using namespace edm;
136     using namespace reco;
137     using namespace std;
138    
139     ///////////////////////////////////////
140     //
141     /// EVENT INITIALISATION
142     ///
143    
144     //read run number and event ID number
145     RunNumber = iEvent.id().run();
146     EventID = iEvent.id().event();
147    
148     // Reset a few things
149     for (map<string,wzana::LeptonProperties* >::iterator iter = leptonProperties.begin();
150     iter!=leptonProperties.end(); iter++)
151     {
152     iter->second->ResetValues();
153     }
154    
155    
156     const Candidate * theZCandidate = 0;
157     const Candidate * theWlepton = 0;
158    
159    
160     ////////////////////////////////////////
161     // Get lists
162    
163     // Z->mumu
164     Handle<CandidateCollection> zmumuCands;
165     iEvent.getByLabel( theZmumuCollection , zmumuCands);
166    
167    
168     // Z->ee
169     Handle<CandidateCollection> zeeCands;
170     iEvent.getByLabel( theZeeCollection , zeeCands);
171    
172     // Z->ee
173     Handle<CandidateCollection> zllCands;
174     iEvent.getByLabel( theZllCollection , zllCands);
175    
176     // loose electrons
177     Handle<CandidateCollection> looseElectronCands;
178     iEvent.getByLabel( theLooseElectronCollection , looseElectronCands);
179    
180     // good electrons
181     Handle<CandidateCollection> goodElectronCands;
182     iEvent.getByLabel( theGoodElectronCollection , goodElectronCands);
183    
184     // loose muons
185     Handle<CandidateCollection> looseMuonCands;
186     iEvent.getByLabel( theLooseMuonCollection , looseMuonCands);
187    
188     // good muons
189     Handle<CandidateCollection> goodMuonCands;
190     iEvent.getByLabel( theGoodMuonCollection , goodMuonCands);
191    
192    
193     // tight leptons
194     Handle<CandidateCollection> tightLeptonCands;
195     iEvent.getByLabel( theTightLeptonCollection , tightLeptonCands);
196    
197     // jets
198    
199     Handle<CaloJetCollection> jetCands;
200     iEvent.getByLabel( theJetCollection , jetCands);
201    
202    
203     // event weights (CSA07 soups...)
204     eventWeight = -1;
205    
206     Handle< double> weightHandle;
207     Handle<int> genProcessID;
208     Handle<int> alpgenProcessID;
209     Handle<double> genEventScale;
210    
211     if (getEventWeight) {
212    
213     iEvent.getByLabel (theWeightCollection, weightHandle);
214     if (weightHandle.isValid()) {
215     eventWeight = * weightHandle;
216     }
217    
218     iEvent.getByLabel( "genEventProcID", genProcessID );
219     if (genProcessID.isValid()) {
220     processID = *genProcessID;
221     }
222    
223     if (processID==4 && getAlpgenID) {
224     iEvent.getByLabel( alpgenIDTag , alpgenProcessID );
225     if (alpgenProcessID.isValid()) {
226     alpgenID = *alpgenProcessID;
227     }
228     } else alpgenID=0;
229    
230    
231     iEvent.getByLabel( "genEventScale", genEventScale );
232     if (genEventScale.isValid()) {
233     eventScale = *genEventScale;
234     }
235     }
236    
237    
238     // get hold of TriggerResults
239     Handle<TriggerResults> HLTR; //moja promjena: ovo prebaceno 2 reda nize!
240     if (storeHLTresults) {
241     // Handle<TriggerResults> HLTR;
242     iEvent.getByLabel(triggerResultsTag,HLTR);
243     if (firstTriggerResult) {
244     firstTriggerResult = false;
245     trigNames.init((edm::TriggerResults&)*HLTR);
246     numTriggers = trigNames.size();
247     }
248     }
249    
250    
251     // MET corrected (P_muons, P_muons_calo_deposit)
252    
253     reco::Particle::LorentzVector MET=computeMET(iEvent, iSetup, looseMuonCands);
254     // MET_energy=MET.energy();
255     MET_energy=MET.energy(); // this is Et!!
256     MET_pt=MET.pt();
257     MET_px=MET.px();
258     MET_py=MET.py();
259     MET_eta=MET.eta();
260    
261    
262     // selected muons
263    
264     // cout << "\t # loose mu : " << looseMuonCands->size()
265     // << "\t # tight mu : " << goodMuonCands->size()
266     // << "\t # loose e : " << looseElectronCands->size()
267     // << "\t # tight e : " << goodElectronCands->size()
268     // << "\t # tight l : " << tightLeptonCands->size()
269     // << "\t # Z->mumu : " << zmumuCands->size()
270     // << "\t # Z->ee : " << zeeCands->size()
271     // << "\t # Z->ll : " << zllCands->size()
272     // << endl;
273    
274     N_looseMuons=0;
275     N_looseElectrons=0;
276     N_goodMuonCands=0;
277     N_looseMuons = looseMuonCands->size();
278     N_looseElectrons = looseElectronCands->size();
279     N_goodMuonCands=goodMuonCands->size();
280    
281    
282     //
283     // Find best Z (in Z->ll collection (merged Z->ee & Z->mumu)
284     //
285     ::OverlapChecker overlap;
286    
287    
288     float dzmass_min = 100.;
289    
290     n=1; // number of non overlaping Zs
291     for(CandidateCollection::const_iterator z1 = zllCands->begin();
292     z1 != zllCands->end(); ++ z1 ) {
293    
294     // Check that two leptons used to build the Z are not overlapping
295     if (overlap(*(z1->daughter(0)),*(z1->daughter(1))) ) {
296     continue;
297     }
298    
299     float dzmass = fabs( z1->mass() - 91.188);
300     if (dzmass < dzmass_min) {
301     theZCandidate = &(*z1);
302     dzmass_min = dzmass;
303     }
304     //
305     //Get back Electrons from Z and W
306     for(CandidateCollection::const_iterator z2 = z1;
307     z2 != zllCands->end(); ++ z2 ) {
308     if (z1 != z2) {
309     if (overlap(*z1,*z2)) {
310     } else {
311     n++;
312     }
313     }
314     }
315     }
316    
317    
318     zFlavour = 0;
319     wlFlavour = 0;
320     zMass = -10;
321     zPt = -10;
322     zEta = -10;
323     zPhi = -10;
324    
325     dPhi_WlZ_reco=-15.;
326     dPhi_WZ_reco=-15.;
327    
328    
329     //---------------
330     // matching(iEvent,looseMuonCands, 13);
331     // matching(iEvent,looseElectronCands, 11);
332     // MatchedGenParticle(&(*looseMuonCands)[0]);
333     //-------------------
334    
335     int nwl_candidates=0;
336     if (theZCandidate) {
337    
338    
339     zMass = theZCandidate->mass();
340     zFlavour = abs(theZCandidate->daughter(0)->pdgId());
341     zPt = theZCandidate->pt();
342     zEta = theZCandidate->eta();
343     zPhi = theZCandidate->phi();
344    
345     // Sanity check: can we identify the Z daughters with leptons from the loose lists
346    
347     Handle<CandidateCollection> looseLeptonCands;
348     if (zFlavour == 11) {
349     looseLeptonCands = looseElectronCands;
350     } else if (zFlavour == 13) {
351     looseLeptonCands = looseMuonCands;
352     }
353     for (int i=0; i<2; i++) {
354     int noverlaps=0;
355     for(CandidateCollection::const_iterator l = looseLeptonCands->begin();
356     l != looseLeptonCands->end(); l++) {
357     if (overlap(*(theZCandidate->daughter(i)), *l) ) {
358     noverlaps++;
359     }
360     }
361     }
362    
363     matching(iEvent,looseMuonCands, 13);
364     matching(iEvent,looseElectronCands, 11);
365    
366     for (map< int,
367     std::vector< matchedParticles > > ::iterator iter = leptonMatching.begin();
368     iter!=leptonMatching.end(); iter++) // entire map
369     {
370     // for (unsigned int j=0; j<iter->second.size(); j++){ // entire vector<class<>>
371     // const reco::Candidate * nesto=iter->second[j].pair.second;
372    
373     if ((iter->first)==13)
374     cout <<"IDE PO CIJELOJ MAPI 13 za WZ TREE" << endl;
375     // }
376     }
377    
378     if (zFlavour == 11) {
379     //--------------
380     leptonProperties["ZEl1"]->FillProperties(theZCandidate->daughter(0), iEvent, iSetup,
381     MatchedGenParticle(theZCandidate->daughter(0)),dR(theZCandidate->daughter(0)));
382     leptonProperties["ZEl2"]->FillProperties(theZCandidate->daughter(1), iEvent, iSetup,
383     MatchedGenParticle(theZCandidate->daughter(1)),dR(theZCandidate->daughter(1)));
384     } else if (zFlavour == 13) {
385     //--------------
386     const reco::Candidate * mcpart = MatchedGenParticle(theZCandidate->daughter(0));
387     //--------------
388     leptonProperties["Zmu1"]->FillProperties(theZCandidate->daughter(0), iEvent, iSetup,
389     MatchedGenParticle(theZCandidate->daughter(0)),dR(theZCandidate->daughter(0)));
390     leptonProperties["Zmu2"]->FillProperties(theZCandidate->daughter(1), iEvent, iSetup,
391     MatchedGenParticle(theZCandidate->daughter(1)),dR(theZCandidate->daughter(1)));
392    
393     }
394    
395     float max_pt = 0.;
396    
397     /// Find W lepton
398    
399    
400     // Now find lepton that will be associated to W
401     // among leptons not used for the Z reconstruction
402     for(CandidateCollection::const_iterator lepton = tightLeptonCands->begin();
403     lepton != tightLeptonCands->end(); lepton++) {
404    
405    
406     if ( overlap(*theZCandidate, *lepton) ) continue; // Ignore if lepton used for the Z
407    
408     nwl_candidates++;
409    
410     // If more than one candidate: choose leading pt
411     if (lepton->pt() > max_pt) {
412     theWlepton = &(*lepton);
413     max_pt = lepton->pt();
414     }
415    
416     if(lepton->hasMasterClone()) {
417     // cout << "SHOUT: TIGHT LEPTON IS SHALLOW COPY !!! (SHOULD NOT BE!) \n";
418     }
419     //
420     }
421     if (theWlepton) {
422     wlFlavour = theWlepton->pdgId();
423     wlCharge = theWlepton->charge();
424     wlPt = theWlepton->pt();
425     wlEta = theWlepton->eta();
426     wlPhi = theWlepton->phi();
427    
428     if (abs(wlFlavour) == 11) {
429     leptonProperties["Wel"]->FillProperties(theWlepton, iEvent, iSetup, MatchedGenParticle(theWlepton),dR(theWlepton));
430     } else if (abs(wlFlavour) == 13) {
431     leptonProperties["Wmu"]->FillProperties(theWlepton, iEvent, iSetup, MatchedGenParticle(theWlepton),dR(theWlepton));
432     }
433    
434     dPhi_WlZ_reco=acos(cos(theWlepton->phi()-theZCandidate->phi()));
435     dPhi_WZ_reco=acos(cos((theWlepton->p4()+MET).phi()-theZCandidate->phi()));
436    
437     }
438    
439     }
440    
441     // Handle <reco::GenParticleCandidateCollection> mccands;
442     Handle<CandidateCollection> mccands;
443     iEvent.getByLabel( theMCTruthCollection,mccands );
444    
445     collectMCsummary(mccands);
446    
447     if (storeHLTresults) {
448    
449     //def: 1:single electron 2:single relaxed 3:double electron 4: double relaxed 5,6,7,8: muon e+mu: 9
450    
451     triggerBitmask=0;
452    
453     for (size_t i=0; i<numTriggers; ++i) {
454    
455     if (!HLTR->wasrun(i)) {
456     continue;
457     }
458     if (HLTR->error(i) ) {
459     continue;
460     }
461     if (HLTR->accept(i)) {
462     //if (triggerNames[i].compare("mcpath")!=0)
463     //TRaccept=true;
464     }else {
465     continue;
466     }
467     if (trigNames.triggerName(i).compare("HLT1Electron")==0) triggerBitmask |= 1;
468     if (trigNames.triggerName(i).compare("HLT1ElectronRelaxed")==0) triggerBitmask |= 2;
469     if (trigNames.triggerName(i).compare("HLT2Electron")==0) triggerBitmask |= 4;
470     if (trigNames.triggerName(i).compare("HLT2ElectronRelaxed")==0) triggerBitmask |= 8;
471    
472     if (trigNames.triggerName(i).compare("HLT1MuonIso")==0) triggerBitmask |= 16;
473     if (trigNames.triggerName(i).compare("HLT1MuonNonIso")==0) triggerBitmask |= 32;
474     if (trigNames.triggerName(i).compare("HLT2MuonNonIso")==0) triggerBitmask |= 64;
475     if (trigNames.triggerName(i).compare("HLT2MuonZ")==0) triggerBitmask |= 128;
476    
477     if (trigNames.triggerName(i).compare("HLTXElectronMuon")==0) triggerBitmask |= 256;
478     if (trigNames.triggerName(i).compare("HLTXElectronMuonRelaxed")==0) triggerBitmask |= 512;
479    
480     if (trigNames.triggerName(i).compare("CandHLT2MuonIso")==0) triggerBitmask |= 1024;
481     }
482     }
483    
484     // Jet properties
485    
486     float max_et = 0.;
487     float max2_et = 0.;
488     const Candidate * leadingJet = 0;
489     const Candidate * secondJet = 0;
490    
491     bool matchedLept[3]={false,false,false};
492    
493     // for(CandidateCollection::const_iterator jet = jetCands->begin();
494     NbJets = 0;
495     NbJetsWithLeptons = 0;
496     NbJets_forMuonFakeRate=0;
497    
498     for(CaloJetCollection::const_iterator jet = jetCands->begin();
499     jet != jetCands->end(); jet++) {
500    
501     //ignore jets which are within dR 0.15 to a WZ lepton
502     {
503     float dPhi[3]={-1.0,-1.0,-1.0};
504     float dEta[3]={-1.0,-1.0,-1.0};
505     bool skipJet=false;
506    
507     for (int ct=0;ct<2;ct++) {
508     if (theZCandidate)
509     if (theZCandidate->daughter(ct)->pt()>0
510     && (abs(theZCandidate->daughter(ct)->pdgId())==13
511     || abs(theZCandidate->daughter(ct)->pdgId())==11) ) {
512     dPhi[ct]=fabs(theZCandidate->daughter(ct)->phi() - jet->phi());
513     dEta[ct]=fabs(theZCandidate->daughter(ct)->eta() - jet->eta());
514     }
515     }
516     if (theWlepton)
517     if (theWlepton->pt()>0 && (abs(theWlepton->pdgId())==13 || abs(theWlepton->pdgId())==11 )) {
518     dPhi[2]=fabs(theWlepton->phi() - jet->phi());
519     dEta[2]=fabs(theWlepton->eta() - jet->eta());
520     }
521     for (int k=0;k<3;k++) {
522     if (matchedLept[k] || dPhi[k]<0.0 || dEta[k]<0.0) continue;
523    
524     while (dPhi[k]>6.28318531) dPhi[k]-= 6.28318531;
525     if (dPhi[k]>3.14159265) dPhi[k]= 6.28318531-dPhi[k];
526    
527     float deltaR=sqrt(dPhi[k]*dPhi[k]+dEta[k]*dEta[k]);
528     if (deltaR<0.15) {
529     skipJet=true;
530     matchedLept[k]=true;
531     break;
532     }
533     }
534     if(jet->pt() > 10 && fabs(jet->eta())<2.5 )NbJetsWithLeptons++;
535     if (fabs(jet->eta())<2.5 ) NbJets_forMuonFakeRate++;
536     if (skipJet) continue;
537     if(jet->pt() > 10 && fabs(jet->eta())<2.5 )NbJets++;
538     }
539    
540     if (jet->et() > max_et ) {
541     if (leadingJet) {
542     secondJet = leadingJet;
543     max2_et = max_et;
544     }
545     leadingJet = &(*jet);
546     max_et = jet->et();
547    
548     } else {
549     if (jet->et() > max2_et ) {
550     secondJet = &(*jet);
551     max2_et = jet->et();
552     }
553     }
554     }
555     if (leadingJet) {
556     jet1Et = leadingJet->et();
557     jet1Phi = leadingJet->phi();
558     jet1Eta = leadingJet->eta();
559    
560     } else {
561     jet1Et = -10;
562     jet1Phi = 0.;
563     jet1Eta = 0.;
564     }
565     if (secondJet) {
566     jet2Et = secondJet->et();
567     jet2Phi = secondJet->phi();
568     jet2Eta = secondJet->eta();
569     } else {
570     jet2Et = -10;
571     jet2Phi = 0.;
572     jet2Eta = 0.;
573    
574     }
575    
576     // matching:
577    
578     matching(iEvent,looseMuonCands, 13);
579     matching(iEvent,looseElectronCands, 11);
580    
581     getMother(&(*mccands)[1]);
582    
583     numberOfElectronTrees = 0;
584    
585     //count number of electron treee entries which will be added
586     // for(CandidateCollection::const_iterator el = looseElectronCands->begin();
587     // el != looseElectronCands->end(); ++ el ) {
588     // if (el->pt()>10 && fabs(el->eta())<2.5) {
589     // numberOfElectronTrees++;
590     // }
591     // }
592    
593    
594    
595     // dodatak za muon fake rate:
596     matching_JetsWithMuons(iEvent, looseMuonCands, jetCands);
597     matching_JetsWithB(iEvent, jetCands);
598     matching_JetsWithC(iEvent, jetCands);
599    
600     // for(CaloJetCollection::const_iterator jet = jetCands->begin();
601     // jet != jetCands->end(); jet++) {
602    
603     nb_jet=0;
604     nb_jet_eta_cut=0;
605     nb_jet_eta_and_pt_cut=0;
606     for (int i=0;i<jetCands->size();i++){
607     nb_jet++;
608     if (fabs((*jetCands)[i].eta())<2.5 ){
609     nb_jet_eta_cut++;
610     if ((*jetCands)[i].pt()<10){
611     nb_jet_eta_and_pt_cut++;
612     }
613     }
614     }
615    
616     nb_muon=0;
617     for (int i=0;i<looseMuonCands->size();i++){
618     if (fabs((*looseMuonCands)[i].eta())<2.5 ){
619     nb_muon++;
620     }
621     }
622    
623     for (int i=0;i<jetCands->size();i++){
624    
625     // cout <<" # jets ="<<jetCands->size() <<endl;
626     JetmatchedMuon=0;
627     JetmatchedB=0;
628     JetmatchedC=0;
629     MuonmatchedGenMuon=0;
630     dr_muon=0.;
631     dr_B=0.;
632     dr_C=0.;
633     // cout <<" dr na pocetku="<<dr << endl;
634     Jet_Et=-10.;
635     Jet_eta=-10.;
636     Jet_phi=-10.;
637     Jet_Pt=-10.;
638    
639     Jet_maxEInEmTowers=-10.;
640     Jet_maxEInHadTowers=-10.;
641     Jet_energyFractionHadronic=-10.;
642     Jet_emEnergyFraction=-10.;
643     Jet_hadEnergyInHB=-10.;
644     Jet_hadEnergyInHO=-10.;
645     Jet_hadEnergyInHE=-10.;
646     Jet_hadEnergyInHF=-10.;
647     Jet_emEnergyInEB=-10.;
648     Jet_emEnergyInEE=-10.;
649     Jet_emEnergyInHF=-10.;
650     Jet_towersArea=-10.;
651     Jet_n90=-10.;
652     Jet_n60=-10.;
653    
654     muon_Et=-10.;
655     muon_eta=-10.;
656     muon_Pt=-10.;
657     B_Et=-10.;
658     B_eta=-10.;
659     B_Pt=-10.;
660     C_Et=-10.;
661     C_eta=-10.;
662     C_Pt=-10.;
663     gen_muon_Et=-10.;
664     gen_muon_eta=-10.;
665     gen_muon_Pt=-10.;
666     muon_mother=0;
667     muon_mother_id=0;
668    
669     // if (fabs((*jetCands)[i].eta())<2.5) {
670     // cout <<" jet passing eta cut" <<endl;
671     // if jet has matched muon and eta(muon)<2.5
672     if (MatchedMuonWithJet(&(*jetCands)[i])) {
673     cout <<" jet IMA matched muon !!!!!!!!!!!!!!" <<endl;
674     JetmatchedMuon=1;
675     dr_muon=dR_Muon_Jet(&(*jetCands)[i]);
676     cout <<" dr="<< dr_muon << " dR_Muon_Jet(&(*jetCands)[i])="<<dR_Muon_Jet(&(*jetCands)[i])<< endl;
677     muon_Et=MatchedMuonWithJet(&(*jetCands)[i])->et();
678     muon_eta=MatchedMuonWithJet(&(*jetCands)[i])->eta();
679     muon_Pt=MatchedMuonWithJet(&(*jetCands)[i])->pt();
680     // if (fabs(MatchedMuonWithJet(&(*jetCands)[i])->eta())<2.5) {
681     // cout <<" muon prolazi eta cut" <<endl;
682    
683     for (map< int,
684     std::vector< matchedParticles > > ::iterator iter = leptonMatching.begin();
685     iter!=leptonMatching.end(); iter++) // entire map
686     {
687     // for (unsigned int j=0; j<iter->second.size(); j++){ // entire vector<class<>>
688     // const reco::Candidate * nesto=iter->second[j].pair.second;
689    
690     if ((iter->first)==13)
691     cout <<"IDE PO CIJELOJ MAPI 13" << endl;
692     // }
693     }
694    
695     // ako muon (matched with jet) ima matched gen_muon
696     if (MatchedGenParticle(MatchedMuonWithJet(&(*jetCands)[i]))){
697     cout <<" muon has matched gen muon !!!!!!!!!!" <<endl;
698     MuonmatchedGenMuon=1;
699     gen_muon_Et=MatchedGenParticle(MatchedMuonWithJet(&(*jetCands)[i]))->et();
700     gen_muon_eta=MatchedGenParticle(MatchedMuonWithJet(&(*jetCands)[i]))->eta();
701     gen_muon_Pt=MatchedGenParticle(MatchedMuonWithJet(&(*jetCands)[i]))->pt();
702     MuonAnalyzer::LeptonOrigin origin = getMother(MatchedGenParticle(MatchedMuonWithJet(&(*jetCands)[i])));
703    
704     if (origin == MuonAnalyzer::wboson ){
705     muon_mother=1;
706     cout << "MatchedGenFromW=true" <<endl;
707     }
708     else if (origin == MuonAnalyzer::zboson ){
709     muon_mother=2;
710     cout << "MatchedGenFromZ=true" <<endl;
711     }
712     else if (origin == MuonAnalyzer::bdecay ) {
713     muon_mother = 3;
714     cout << "MatchedGenFromB=true" <<endl;
715     }
716     else if (origin == MuonAnalyzer::cdecay ) {
717     muon_mother = 4;
718     cout << "MatchedGenFromC=true" <<endl;
719     }
720     muon_mother_id=getMother_Id(MatchedGenParticle(MatchedMuonWithJet(&(*jetCands)[i])));
721    
722    
723     // }
724     }
725     // }
726     cout <<" jet nema matched muon" <<endl;
727     }
728    
729     if (MatchedBhadronWithJet(&(*jetCands)[i])) {
730     cout <<" ima matched B hadron !!!!!!!!" << endl;
731     /////////////////
732    
733     JetmatchedB=1;
734     dr_B=dR_B_Jet(&(*jetCands)[i]);
735     cout <<" dr="<< dr_B << " dR_B_Jet(&(*jetCands)[i])="<<dR_B_Jet(&(*jetCands)[i])<< endl;
736     B_Et=MatchedBhadronWithJet(&(*jetCands)[i])->et();
737     B_eta=MatchedBhadronWithJet(&(*jetCands)[i])->eta();
738     B_Pt=MatchedBhadronWithJet(&(*jetCands)[i])->pt();
739    
740     }
741    
742     if (MatchedChadronWithJet(&(*jetCands)[i])) {
743     cout <<" ima matched C hadron !!!!!!!!" << endl;
744     /////////////////
745    
746     JetmatchedC=1;
747     dr_C=dR_C_Jet(&(*jetCands)[i]);
748     cout <<" dr="<< dr_C << " dR_C_Jet(&(*jetCands)[i])="<<dR_C_Jet(&(*jetCands)[i])<< endl;
749     C_Et=MatchedChadronWithJet(&(*jetCands)[i])->et();
750     C_eta=MatchedChadronWithJet(&(*jetCands)[i])->eta();
751     C_Pt=MatchedChadronWithJet(&(*jetCands)[i])->pt();
752    
753     }
754    
755     Jet_Et=(*jetCands)[i].et();
756     Jet_eta=(*jetCands)[i].eta();
757     Jet_phi=(*jetCands)[i].phi();
758     Jet_Pt=(*jetCands)[i].pt();
759    
760     Jet_maxEInEmTowers=(*jetCands)[i].maxEInEmTowers();
761     Jet_maxEInHadTowers=(*jetCands)[i].maxEInHadTowers();
762     Jet_energyFractionHadronic=(*jetCands)[i].energyFractionHadronic();
763     Jet_emEnergyFraction=(*jetCands)[i].emEnergyFraction();
764     Jet_hadEnergyInHB=(*jetCands)[i].hadEnergyInHB();
765     Jet_hadEnergyInHO=(*jetCands)[i].hadEnergyInHO();
766     Jet_hadEnergyInHE=(*jetCands)[i].hadEnergyInHE();
767     Jet_hadEnergyInHF=(*jetCands)[i].hadEnergyInHF();
768     Jet_emEnergyInEB=(*jetCands)[i].emEnergyInEB();
769     Jet_emEnergyInEE=(*jetCands)[i].emEnergyInEE();
770     Jet_emEnergyInHF=(*jetCands)[i].emEnergyInHF();
771     Jet_towersArea=(*jetCands)[i].towersArea();
772     Jet_n90=(*jetCands)[i].n90();
773     Jet_n60=(*jetCands)[i].n60();
774    
775     // cout <<" muon mother="<< muon_mother <<endl;
776    
777     muonTree->Fill();
778     }
779    
780     // for (int i=0;i<N_looseMuons; i++){
781     // if (MatchedGenParticle(&(*looseMuonCands)[i])==0) matchedMuon=0;
782     // else matchedMuon=1;
783     // // (&(*looseMuonCands)[0])
784     // muonTree->Fill();
785     // }
786    
787     JetMuTree->Fill();
788     wzTree->Fill();
789    
790    
791     // Fill electrons in a tree
792     for(CandidateCollection::const_iterator el = looseElectronCands->begin();
793     el != looseElectronCands->end(); ++ el ) {
794    
795     electronUse = 0;
796    
797     if (theZCandidate) {
798    
799     if (overlap(*theZCandidate, *el) ) {
800     electronUse = 23;
801     } else if (theWlepton) {
802     if (overlap(*theWlepton, *el) ) {
803     electronUse = 24;
804     }
805     }
806     }
807     if (el->pt()>10 && fabs(el->eta())<2.5) {
808     leptonProperties["electron"]->FillProperties(&(*el),
809     iEvent, iSetup,
810     MatchedGenParticle(&(*el)),dR(&(*el)));
811     electronTree->Fill();
812    
813     }
814    
815     }
816    
817    
818     }
819    
820    
821     // ------------ method called once each job just before starting event loop ------------
822     void
823     MuonAnalyzer::beginJob(const edm::EventSetup&)
824     {
825    
826     using namespace wzana;
827    
828     // theFile = new TFile( "wz.root", "RECREATE" ) ;
829     theFile = new TFile( fOutputFileName.c_str(), "RECREATE" ) ;
830    
831    
832     wzTree = new TTree("WZTree","WZ informations");
833    
834     electronTree = new TTree("ElTree","electron informations");
835     muonTree = new TTree("JetMuon_Tree","muon informations for fake rate");
836     JetMuTree = new TTree("JetMuEvent_Tree","event info");
837    
838     leptonProperties["electron"] = new ElectronProperties();
839    
840     leptonProperties["Wel"] = new ElectronProperties();
841     leptonProperties["ZEl1"] = new ElectronProperties();
842     leptonProperties["ZEl2"] = new ElectronProperties();
843    
844     leptonProperties["Wmu"] = new MuonProperties();
845     leptonProperties["Zmu1"] = new MuonProperties();
846     leptonProperties["Zmu2"] = new MuonProperties();
847    
848    
849     leptonProperties["Wel"]->CreateBranches(wzTree, "WEl_");
850     leptonProperties["ZEl1"]->CreateBranches(wzTree, "ZEl1_");
851     leptonProperties["ZEl2"]->CreateBranches(wzTree, "ZEl2_");
852    
853     leptonProperties["Wmu"] ->CreateBranches(wzTree, "Wmu_");
854     leptonProperties["Zmu1"] ->CreateBranches(wzTree, "Zmul_");
855     leptonProperties["Zmu2"] ->CreateBranches(wzTree, "Zmu2_");
856     leptonProperties["electron"] ->CreateBranches(electronTree);
857    
858     initialiseTree();
859    
860     //prepare HLT TriggerResults branch
861     if (storeHLTresults) {
862     wzTree->Branch("WZ_hltBitmask",&triggerBitmask,"WZ_hltBitmask/I");
863    
864     }
865    
866     // MET branch
867     wzTree->Branch("MET_Et",&MET_energy,"MET_Et/F");
868     wzTree->Branch("MET_pt",&MET_pt,"MET_pt/F");
869     wzTree->Branch("MET_eta",&MET_eta,"MET_eta/F");
870     wzTree->Branch("dPhi_WlZ_reco",&dPhi_WlZ_reco,"dPhi_WlZ_reco/F");
871     wzTree->Branch("dPhi_WZ_reco",&dPhi_WZ_reco,"dPhi_WZ_reco/F");
872    
873     // # of non overlaping Zs
874     wzTree->Branch("N_nonOverlaping_Z",&n,"N_nonOverlaping_Z/I");
875     wzTree->Branch("N_looseMuons",&N_looseMuons,"N_looseMuons/I");
876     wzTree->Branch("N_looseElectrons",&N_looseElectrons,"N_looseElectrons/I");
877    
878     }
879    
880     // ------------ method called once each job just after ending the event loop ------------
881     void
882     MuonAnalyzer::endJob() {
883    
884     theFile->Write();
885     theFile->Close();
886    
887     }
888    
889    
890     void MuonAnalyzer::initialiseTree() {
891     // Event properties
892     wzTree->Branch("weight", &eventWeight, "weight/F");
893     wzTree->Branch("processID", &processID, "processID/I");
894     wzTree->Branch("alpgenID", &alpgenID, "alpgenID/I");
895     wzTree->Branch("genEventScale", &eventScale, "genEventScale/F");
896     wzTree->Branch("RunNumber", &RunNumber, "RunNumber/I");
897     wzTree->Branch("EventID", &EventID, "EventID/I");
898    
899     // Z properties
900     wzTree->Branch("Zmass", &zMass, "Zmass/F");
901     wzTree->Branch("ZId", &zFlavour, "Zid/I");
902     wzTree->Branch("ZPt", &zPt, "ZPt/F");
903     wzTree->Branch("ZEta", &zEta, "ZEta/F");
904     wzTree->Branch("ZPhi", &zPhi, "ZPhi/F");
905    
906     // W Properties
907     wzTree->Branch("WlId", &wlFlavour, "Wlid/I");
908     wzTree->Branch("WlCharge", &wlCharge, "WlCharge/I");
909     wzTree->Branch("WlPt", &wlPt, "WlPt/F");
910    
911     wzTree->Branch("MC_leptonZ1_pt", &MCleptZ1_pt,"MC_leptonZ1_pt/F");
912     wzTree->Branch("MC_leptonZ2_pt", &MCleptZ2_pt,"MC_leptonZ2_pt/F");
913     wzTree->Branch("MC_leptonW_pt", &MCleptW_pt, "MC_leptonW_pt/F");
914    
915     wzTree->Branch("MC_leptonZ1_eta", &MCleptZ1_eta,"MC_leptonZ1_eta/F");
916     wzTree->Branch("MC_leptonZ2_eta", &MCleptZ2_eta,"MC_leptonZ2_eta/F");
917     wzTree->Branch("MC_leptonW_eta", &MCleptW_eta, "MC_leptonW_eta/F");
918    
919     wzTree->Branch("MC_leptonZ1_phi", &MCleptZ1_phi,"MC_leptonZ1_phi/F");
920     wzTree->Branch("MC_leptonZ2_phi", &MCleptZ2_phi,"MC_leptonZ2_phi/F");
921     wzTree->Branch("MC_leptonW_phi", &MCleptW_phi, "MC_leptonW_phi/F");
922    
923     wzTree->Branch("MC_leptonZ1_pdgid", &MCleptZ1_pdgid,"MC_leptonZ1_pdgid/I");
924     wzTree->Branch("MC_leptonZ2_pdgid", &MCleptZ2_pdgid,"MC_leptonZ2_pdgid/I");
925     wzTree->Branch("MC_leptonW_pdgid", &MCleptW_pdgid, "MC_leptonW_pdgid/I");
926    
927     /*
928     wzTree->Branch("MC_leptonW_origin", &MCleptW_pdgid, "MC_leptonW_origin/I");
929     wzTree->Branch("MC_leptonZ1_origin", &MCleptW_pdgid, "MC_leptonZ1_origin/I");
930     wzTree->Branch("MC_leptonZ2_origin", &MCleptW_pdgid, "MC_leptonZ2_origin/I");
931     */
932    
933     wzTree->Branch("MC_TauDecayType_fromZ1", &MC_tauDecayTypeZ1,"MC_TauDecayType_fromZ1/I");
934     wzTree->Branch("MC_TauDecayType_fromZ2", &MC_tauDecayTypeZ2,"MC_TauDecayType_fromZ2/I");
935     wzTree->Branch("MC_TauDecayType_fromW", &MC_tauDecayTypeW, "MC_TauDecayType_fromW/I");
936    
937     // MET properties
938    
939     // Jet properties
940     wzTree->Branch("NbJets", &NbJets, "NbJets/I");
941    
942     wzTree->Branch("Jet1Et", &jet1Et, "Jet1Et/F");
943     wzTree->Branch("Jet1Phi", &jet1Phi, "Jet1Phi/F");
944     wzTree->Branch("Jet1Eta", &jet1Eta, "Jet1Eta/F");
945    
946     wzTree->Branch("Jet2Et", &jet2Et, "Jet2Et/F");
947     wzTree->Branch("Jet2Phi", &jet2Phi, "Jet2Phi/F");
948     wzTree->Branch("Jet2Eta", &jet2Eta, "Jet2Eta/F");
949     wzTree->Branch("ElectronTreesInEvent", &numberOfElectronTrees, "ElectronTreesInEvent/I");
950     //
951     electronTree->Branch("used", &electronUse, "used/I");
952     electronTree->Branch("weight", &eventWeight, "weight/F");
953     electronTree->Branch("processID", &processID, "processID/I");
954     electronTree->Branch("alpgenID", &alpgenID, "alpgenID/I");
955     electronTree->Branch("genEventScale", &eventScale, "genEventScale/F");
956     electronTree->Branch("ElectronTreesInEvent", &numberOfElectronTrees, "ElectronTreesInEvent/I");
957    
958     electronTree->Branch("RunNumber", &RunNumber, "RunNumber/I");
959     electronTree->Branch("EventID", &EventID, "EventID/I");
960     electronTree->Branch("NbJetsWithLeptons", &NbJetsWithLeptons, "NbJetsWithLeptons/I");
961    
962     // muon properties for fake rate
963     // muonTree->Branch("RunNumber", &RunNumber, "RunNumber/I");
964     // muonTree->Branch("EventID", &EventID, "EventID/I");
965     // muonTree->Branch("NbJets_forMuonFakeRate", &NbJets_forMuonFakeRate, "NbJets_forMuonFakeRate/I");
966     // muonTree->Branch("N_looseMuons",&N_looseMuons,"N_looseMuons/I");
967     // muonTree->Branch("N_goodMuonCands",&N_goodMuonCands,"N_goodMuonCands/I");
968     // muonTree->Branch("matchedMuon",&matchedMuon,"matchedMuon/I");
969    
970     // muonTree->Branch("nb_jet",&nb_jet,"nb_jet/I");
971    
972     // muonTree->Branch("NbJets",&,"/I");
973     muonTree->Branch("JetmatchedMuon",&JetmatchedMuon,"JetmatchedMuon/I");
974     muonTree->Branch("JetmatchedB",&JetmatchedB,"JetmatchedB/I");
975     muonTree->Branch("JetmatchedC",&JetmatchedC,"JetmatchedC/I");
976     muonTree->Branch("MuonmatchedGenMuon",&MuonmatchedGenMuon,"MuonmatchedGenMuon/I");
977    
978     muonTree->Branch("Jet_Et",&Jet_Et,"Jet_Et/F");
979     muonTree->Branch("Jet_eta",&Jet_eta,"Jet_eta/F");
980     muonTree->Branch("Jet_Pt",&Jet_Pt,"Jet_Pt/F");
981     muonTree->Branch("Jet_maxEInEmTowers",&Jet_maxEInEmTowers,"Jet_maxEInEmTowers/F");
982     muonTree->Branch("Jet_maxEInHadTowers",&Jet_maxEInHadTowers,"Jet_maxEInHadTowers/F");
983     muonTree->Branch("Jet_energyFractionHadronic",&Jet_energyFractionHadronic,"Jet_energyFractionHadronic/F");
984     muonTree->Branch("Jet_emEnergyFraction",&Jet_emEnergyFraction,"Jet_emEnergyFraction/F");
985     muonTree->Branch("Jet_hadEnergyInHB",&Jet_hadEnergyInHB,"Jet_hadEnergyInHB/F");
986     muonTree->Branch("Jet_hadEnergyInHO",&Jet_hadEnergyInHO,"Jet_hadEnergyInHO/F");
987     muonTree->Branch("Jet_hadEnergyInHE",&Jet_hadEnergyInHE,"Jet_hadEnergyInHE/F");
988     muonTree->Branch("Jet_hadEnergyInHF",&Jet_hadEnergyInHF,"Jet_hadEnergyInHF/F");
989     muonTree->Branch("Jet_emEnergyInEB",&Jet_emEnergyInEB,"Jet_emEnergyInEB/F");
990     muonTree->Branch("Jet_emEnergyInEE",&Jet_emEnergyInEE,"Jet_emEnergyInEE/F");
991     muonTree->Branch("Jet_emEnergyInHF",&Jet_emEnergyInHF,"Jet_emEnergyInHF/F");
992     muonTree->Branch("Jet_towersArea",&Jet_towersArea,"Jet_towersArea/F");
993     muonTree->Branch("Jet_n90",&Jet_n90,"Jet_n90/F");
994     muonTree->Branch("Jet_n60",&Jet_n60,"Jet_n60/F");
995    
996     muonTree->Branch("muon_Et",&muon_Et,"muon_Et/F");
997     muonTree->Branch("muon_eta",&muon_eta,"muon_eta/F");
998     muonTree->Branch("muon_Pt",&muon_Pt,"muon_Pt/F");
999    
1000     muonTree->Branch("B_Et",&B_Et,"B_Et/F");
1001     muonTree->Branch("B_eta",&B_eta,"B_eta/F");
1002     muonTree->Branch("B_Pt",&B_Pt,"B_Pt/F");
1003    
1004     muonTree->Branch("C_Et",&C_Et,"C_Et/F");
1005     muonTree->Branch("C_eta",&C_eta,"C_eta/F");
1006     muonTree->Branch("C_Pt",&C_Pt,"C_Pt/F");
1007    
1008     muonTree->Branch("gen_muon_Et",&gen_muon_Et,"gen_muon_Et/F");
1009     muonTree->Branch("gen_muon_eta",&gen_muon_eta,"gen_muon_eta/F");
1010     muonTree->Branch("gen_muon_Pt",&gen_muon_Pt,"gen_muon_Pt/F");
1011    
1012     muonTree->Branch("dr_muon",&dr_muon,"dr_muon/F");
1013     muonTree->Branch("dr_B",&dr_B,"dr_B/F");
1014     muonTree->Branch("dr_C",&dr_C,"dr_C/F");
1015    
1016     muonTree->Branch("muon_mother",&muon_mother,"muon_mother/I");
1017     muonTree->Branch("muon_mother_id",&muon_mother_id,"muon_mother_id/I");
1018    
1019     JetMuTree->Branch("nb_jet",&nb_jet,"nb_jet/I");
1020     JetMuTree->Branch("nb_jet_eta_cut",&nb_jet_eta_cut,"nb_jet_eta_cut/I");
1021     JetMuTree->Branch("nb_jet_eta_and_pt_cut",&nb_jet_eta_and_pt_cut,"nb_jet_eta_and_pt_cut/I");
1022     JetMuTree->Branch("nb_muon",&nb_muon,"nb_muon/I");
1023    
1024     }
1025    
1026    
1027     //////////////////////////////////////////
1028     // MET & MET corrections
1029     // MET=MET-sum_p(muons)-sum_p(muons calos depositions)
1030    
1031     reco::Particle::LorentzVector MuonAnalyzer::computeMET(const edm::Event& iEvent, const edm::EventSetup& iSetup, Handle<reco::CandidateCollection> MuonCands) {
1032    
1033     using namespace edm;
1034     using namespace reco;
1035     using namespace std;
1036    
1037     double px_MET=0, py_MET=0, Ex_MET=0, Ey_MET=0;
1038     double px,py,pz,pt,Et,Ex,Ey;
1039     double px_muons=0;
1040     double py_muons=0;
1041     double Ex_muons=0;
1042     double Ey_muons=0;
1043     double Et_muons=0;
1044     double Et_ecal;
1045     double Et_hcal;
1046     double Et_ho;
1047     double Et_cal;
1048     double Ex_ecal;
1049     double Ex_hcal;
1050     double Ex_ho;
1051     double Ey_ecal;
1052     double Ey_hcal;
1053     double Ey_ho;
1054     double eta_cal;
1055     double phi_cal;
1056     double tg_theta_pola_ecal;
1057     double tg_theta_pola_hcal;
1058     double tg_theta_pola_ho;
1059     double tg_theta_ecal;
1060     double tg_theta_hcal;
1061     double tg_theta_ho;
1062     double E_ecal;
1063     double E_hcal;
1064     double E_ho;
1065     double me=0.0005;
1066     double sin_theta_ecal, sin_theta_hcal, sin_theta_ho, cos_theta_ecal,cos_theta_hcal,cos_theta_ho;
1067     double p_ecal, p_hcal,p_ho, pt_ecal,pt_hcal,pt_ho, px_ecal,px_hcal,px_ho, py_ecal,py_hcal,py_ho, pz_ecal,pz_hcal,pz_ho;
1068     double Ex_cal=0;
1069     double Ey_cal=0;
1070     double px_cal=0;
1071     double py_cal=0;
1072     double Ex_kon=0;
1073     double Ey_kon=0;
1074    
1075     reco::Particle::LorentzVector Muon_p_4v(0,0,0,0); // sum of muons 4-vectors in (px,py,0,Et), since MET looks like that
1076     reco::Particle::LorentzVector Muon_p_cal_4v(0,0,0,0);
1077    
1078     Handle<CaloMETCollection> mets;
1079     iEvent.getByLabel("met", mets);
1080     // cout << "# METs=" << mets->size() << endl;
1081    
1082     if (mets->size() != 1) {
1083     cout << "ALARM: # METS is not 1: !" << endl;
1084     return reco::Particle::LorentzVector(0.,0.,0.,0.);
1085     }
1086    
1087     for (CaloMETCollection::const_iterator met = mets->begin();
1088     met!= mets->end(); met++) {
1089     px_MET=met->px();
1090     py_MET=met->py();
1091     Ex_MET=met->et()*TMath::Cos(met->phi());
1092     Ey_MET=met->et()*TMath::Sin(met->phi());
1093    
1094     }
1095    
1096     // sum of p of muons
1097     for (unsigned int i=0; i<MuonCands->size();i++){
1098     px=(*MuonCands)[i].px();
1099     py=(*MuonCands)[i].py();
1100     pz=(*MuonCands)[i].pz();
1101     pt=(*MuonCands)[i].pt();
1102     Et=(*MuonCands)[i].et();
1103     Ex=Et*TMath::Cos((*MuonCands)[i].phi());
1104     Ey=Et*TMath::Sin((*MuonCands)[i].phi());
1105     px_muons=px_muons+px;
1106     py_muons=py_muons+py;
1107     Ex_muons=Ex_muons+Ex;
1108     Ey_muons=Ey_muons+Ey;
1109    
1110     }
1111     Et_muons=TMath::Sqrt(Ex_muons*Ex_muons+Ey_muons*Ey_muons);
1112     Muon_p_4v=reco::Particle::LorentzVector(px_muons,py_muons,0,Et_muons);
1113    
1114     // sum of p of muons left in calorimeters
1115     for (unsigned int i=0; i<MuonCands->size();i++){
1116     cout<<"" << endl;
1117     const reco::Muon* muons=dynamic_cast<const reco::Muon *> (&(*MuonCands)[i]);
1118     Et_ecal=muons->getCalEnergy().emS9;
1119     Et_hcal=muons->getCalEnergy().hadS9;
1120     Et_ho=muons->getCalEnergy().hoS9;
1121     eta_cal=muons->eta();
1122     phi_cal=muons->phi();
1123     tg_theta_pola_ecal=TMath::Exp(-eta_cal);
1124     tg_theta_pola_hcal=TMath::Exp(-eta_cal);
1125     tg_theta_pola_ho=TMath::Exp(-eta_cal);
1126     tg_theta_ecal=2*tg_theta_pola_ecal/(1-tg_theta_pola_ecal*tg_theta_pola_ecal);
1127     tg_theta_hcal=2*tg_theta_pola_hcal/(1-tg_theta_pola_hcal*tg_theta_pola_hcal);
1128     tg_theta_ho=2*tg_theta_pola_ho/(1-tg_theta_pola_ho*tg_theta_pola_ho);
1129     E_ecal=TMath::Abs(Et_ecal*TMath::Sqrt(1+tg_theta_ecal*tg_theta_ecal)/tg_theta_ecal);
1130     E_hcal=TMath::Abs(Et_hcal*TMath::Sqrt(1+tg_theta_hcal*tg_theta_hcal)/tg_theta_hcal);
1131     E_ho=TMath::Abs(Et_ho*TMath::Sqrt(1+tg_theta_ho*tg_theta_ho)/tg_theta_ho);
1132     Ex_ecal=Et_ecal*TMath::Cos(phi_cal);
1133     Ex_hcal=Et_hcal*TMath::Cos(phi_cal);
1134     Ex_ho=Et_ho*TMath::Cos(phi_cal);
1135     Ey_ecal=Et_ecal*TMath::Sin(phi_cal);
1136     Ey_hcal=Et_hcal*TMath::Sin(phi_cal);
1137     Ey_ho=Et_ho*TMath::Sin(phi_cal);
1138     sin_theta_ecal=tg_theta_ecal/TMath::Sqrt(1+tg_theta_ecal*tg_theta_ecal);
1139     sin_theta_hcal=tg_theta_hcal/TMath::Sqrt(1+tg_theta_hcal*tg_theta_hcal);
1140     sin_theta_ho=tg_theta_ho/TMath::Sqrt(1+tg_theta_ho*tg_theta_ho);
1141     cos_theta_ecal=TMath::Sqrt(1-sin_theta_ecal*sin_theta_ecal);
1142     cos_theta_hcal=TMath::Sqrt(1-sin_theta_hcal*sin_theta_hcal);
1143     cos_theta_ho=TMath::Sqrt(1-sin_theta_ho*sin_theta_ho);
1144     if (E_ecal<me){
1145     p_ecal=0;
1146     }
1147     else {
1148     p_ecal=TMath::Sqrt(E_ecal*E_ecal-me*me);
1149     }
1150     if (E_hcal<me){
1151     p_hcal=0;
1152     }
1153     else {
1154     p_hcal=TMath::Sqrt(E_hcal*E_hcal-me*me);
1155     }
1156     if (E_ho<me){
1157     p_ho=0;
1158     }
1159     else {
1160     p_ho=TMath::Sqrt(E_ho*E_ho-me*me);
1161     }
1162    
1163     pz_ecal=p_ecal*cos_theta_ecal;
1164     pz_hcal=p_hcal*cos_theta_hcal;
1165     pz_ho=p_ho*cos_theta_ho;
1166     pt_ecal=TMath::Abs(p_ecal*sin_theta_ecal);
1167    
1168     pt_hcal=TMath::Abs(p_hcal*sin_theta_hcal);
1169    
1170     pt_ho=TMath::Abs(p_ho*sin_theta_ho);
1171    
1172     px_ecal=pt_ecal*TMath::Cos(phi_cal);
1173     px_hcal=pt_hcal*TMath::Cos(phi_cal);
1174     px_ho=pt_ho*TMath::Cos(phi_cal);
1175     py_ecal=pt_ecal*TMath::Sin(phi_cal);
1176     py_hcal=pt_hcal*TMath::Sin(phi_cal);
1177     py_ho=pt_ho*TMath::Sin(phi_cal);
1178    
1179    
1180     Ex_cal=Ex_cal+Ex_ecal+Ex_hcal+Ex_ho;
1181     Ey_cal=Ey_cal+Ey_ecal+Ey_hcal+Ey_ho;
1182    
1183     px_cal=px_cal+px_ecal+px_hcal+px_ho;
1184     py_cal=py_cal+py_ecal+py_hcal+py_ho;
1185    
1186    
1187     }
1188     Et_cal=TMath::Sqrt(Ex_cal*Ex_cal+Ey_cal*Ey_cal);
1189     Muon_p_cal_4v=reco::Particle::LorentzVector(px_cal,py_cal,0, Et_cal);
1190    
1191     Ex_kon=Ex_MET-Ex_muons+Ex_cal;
1192     Ey_kon=Ey_MET-Ey_muons+Ey_cal;
1193    
1194     reco::Particle::LorentzVector MET= reco::Particle::LorentzVector(px_MET-px_muons+px_cal,py_MET-py_muons+py_cal,0,TMath::Sqrt(Ex_kon*Ex_kon+Ey_kon*Ey_kon)); // substract p from muons and p of calo muon deposit from MET
1195    
1196     return MET;
1197    
1198     }
1199    
1200     ////////////////////////
1201     // matching
1202     //
1203    
1204     class MatchingInfo {
1205     public:
1206    
1207     MatchingInfo(float dr, int gen, int reco) : deltaR(dr), genMuon(gen),recoMuon(reco) {}
1208    
1209     bool operator<(const MatchingInfo m) { return deltaR < m.deltaR;}
1210    
1211     float deltaR;
1212     int genMuon;
1213     int recoMuon;
1214    
1215     };
1216    
1217    
1218     bool betterMatch2(MatchingInfo m1, MatchingInfo m2)
1219     { return m1.deltaR < m2.deltaR;}
1220    
1221    
1222     // method that gives the vector<MatchingInfo> matching_Infos containing (dR,i,j); i=genParticle index, j=ParticleCollection index:
1223    
1224     void MuonAnalyzer::matching(const edm::Event& iEvent, Handle<reco::CandidateCollection> cands, int pdgid){
1225    
1226     using namespace edm;
1227     using namespace reco;
1228     using namespace std;
1229    
1230     //-------------------------
1231     Handle<CandidateCollection> mccands;
1232     iEvent.getByLabel( theMCTruthCollection,mccands );
1233    
1234     vector <GenParticleCandidate*> genparticles;
1235    
1236     for(CandidateCollection::const_iterator p = mccands->begin();
1237     p != mccands->end(); p++) {
1238    
1239     // reco::Candidate* p_tmp = &(*p);
1240     reco::GenParticleCandidate* ptr = (reco::GenParticleCandidate*) &(*p);
1241    
1242     if ( (ptr)->status() == 1
1243     /*&& (ptr)->momentum().perp() > 2.*/ ) { // stable particles with pt>2 only
1244     if ( abs((ptr)->pdgId()) == pdgid ) {
1245     genparticles.push_back((ptr));
1246     //cout << "electron MCT\n";
1247     }
1248     }
1249     }
1250    
1251    
1252     int n1=0;
1253     vector<MatchingInfo> matching_Infos;
1254     for ( unsigned int i=0; i<genparticles.size(); i++ ) // po svim MC muonima
1255     {
1256    
1257     for (unsigned int j = 0; j < cands->size(); j++)// po svim reco muonima
1258     {
1259    
1260     double d_eta2=(*cands)[j].eta()-(genparticles)[i]->eta();
1261     double d_phi=fabs((*cands)[j].phi()-(genparticles)[i]->phi());
1262     double dR_byhand= sqrt(d_eta2*d_eta2+d_phi*d_phi);
1263     double dR=dR_byhand;
1264    
1265     if (dR<0.15){
1266     n1++;
1267     matching_Infos.push_back(MatchingInfo(dR,i,j));
1268     }
1269    
1270     }
1271     }
1272    
1273     sort(matching_Infos.begin(),matching_Infos.end(),betterMatch2); // sorting (dR,i,j)
1274    
1275     if (genparticles.size()!=0 && cands->size()!=0){
1276     // Now exclude combinations using same gen or rec muons
1277     for (std::vector<MatchingInfo>::iterator it=matching_Infos.begin();
1278     it != matching_Infos.end(); it++) {
1279     for (std::vector<MatchingInfo>::iterator it2=matching_Infos.begin();
1280     it2!=it; it2++) {
1281     if ( (it->genMuon == it2->genMuon) || (it->recoMuon == it2->recoMuon) ) {
1282     matching_Infos.erase(it);
1283     it=matching_Infos.begin();
1284     break;
1285     }
1286     }
1287     }
1288     }
1289    
1290     // Now put result in vector of pairs
1291    
1292     leptonMatching[pdgid].clear();
1293    
1294     for (vector<MatchingInfo>::iterator match=matching_Infos.begin();
1295     match !=matching_Infos.end(); match++) { // po tablici dr,i,j
1296    
1297     pair<const GenParticleCandidate*, const reco::Candidate *> pair;
1298     // vector<pair,float > matchedParticles;
1299     pair.first = genparticles[match->genMuon];
1300     pair.second = & (*cands)[match->recoMuon];
1301    
1302     matchedParticles cestice(genparticles[match->genMuon] ,& (*cands)[match->recoMuon] , match->deltaR);
1303    
1304     leptonMatching[pdgid].push_back(cestice);
1305    
1306     }
1307    
1308     }
1309    
1310     //---------------------------------------------------
1311     /////////////////////////////////////////////////////////////////////
1312     // made for muon fake rate
1313     // method that gives the vector<MatchingInfo> matching_Infos containing (dR,i,j); i=genParticle index, j=ParticleCollection index:
1314    
1315     void MuonAnalyzer::matching_JetsWithMuons(const edm::Event& iEvent, Handle<reco::CandidateCollection> cands, Handle<reco::CaloJetCollection> jets){
1316    
1317     using namespace edm;
1318     using namespace reco;
1319     using namespace std;
1320    
1321     cout <<" ulazi u matching_JetsWithMuons" <<endl;
1322    
1323     int n1=0;
1324     vector<MatchingInfo> matching_Infos;
1325     cout <<"# jets="<<jets->size()<< " # muons="<<cands->size() << endl;
1326     for ( unsigned int i=0; i<jets->size(); i++ ) // po svim jetovima
1327     {
1328     // cout <<" next jet" <<endl;
1329     for (unsigned int j = 0; j < cands->size(); j++)// po svim reco muonima
1330     {
1331     // cout <<" next muon" <<endl;
1332     double d_eta2=(*cands)[j].eta()-(*jets)[i].eta();
1333     double d_phi=fabs((*cands)[j].phi()-(*jets)[i].phi());
1334     double dR_byhand= sqrt(d_eta2*d_eta2+d_phi*d_phi);
1335     double dR=dR_byhand;
1336     // cout <<"dR="<< dR <<endl;
1337     // if (dR<0.15){
1338     n1++;
1339     matching_Infos.push_back(MatchingInfo(dR,i,j));
1340     // }
1341    
1342     }
1343     }
1344    
1345     sort(matching_Infos.begin(),matching_Infos.end(),betterMatch2); // sorting (dR,i,j)
1346    
1347     if (jets->size()!=0 && cands->size()!=0){
1348     // Now exclude combinations using same gen or rec muons
1349     for (std::vector<MatchingInfo>::iterator it=matching_Infos.begin();
1350     it != matching_Infos.end(); it++) {
1351     for (std::vector<MatchingInfo>::iterator it2=matching_Infos.begin();
1352     it2!=it; it2++) {
1353     if ( (it->genMuon == it2->genMuon) || (it->recoMuon == it2->recoMuon) ) {
1354     matching_Infos.erase(it);
1355     it=matching_Infos.begin();
1356     break;
1357     }
1358     }
1359     }
1360     }
1361    
1362     // Now t in vector of pairs
1363    
1364     Muon_Jet_Matching[1].clear();
1365    
1366     for (vector<MatchingInfo>::iterator match=matching_Infos.begin();
1367     match !=matching_Infos.end(); match++) { // po tablici dr,i,j
1368    
1369     pair<const CaloJet*, const reco::Candidate *> pair;
1370     // vector<pair,float > matchedParticles;
1371     pair.first = &(*jets)[match->genMuon];
1372     pair.second = & (*cands)[match->recoMuon];
1373    
1374     matchedJets_and_Muons cestice(&(*jets)[match->genMuon] ,& (*cands)[match->recoMuon] , match->deltaR);
1375    
1376     Muon_Jet_Matching[1].push_back(cestice);
1377    
1378     }
1379    
1380     cout <<" nakon pisanja u Muon_Jet_Matching[1]" << endl;
1381     for (vector<MatchingInfo>::iterator match=matching_Infos.begin();
1382     match !=matching_Infos.end(); match++) { // po tablici dr,i,j
1383     // cout <<" nakon pisanja u Muon_Jet_Matching[1]" << endl;
1384     for (map< int,
1385     std::vector< matchedJets_and_Muons > > ::iterator iter = Muon_Jet_Matching.begin();
1386     iter!=Muon_Jet_Matching.end(); iter++) // entire map
1387     {
1388     for (unsigned int j=0; j<iter->second.size(); j++){ // entire vector<class<>>
1389     const reco::Candidate * nesto=iter->second[j].pair.second;
1390     // ::OverlapChecker overlap;
1391     // if (overlap(*nesto,*daughter)){
1392     cout <<"*****dr="<< iter->second[j].delta_R<<endl;
1393     // cout <<"*****dr="<< iter->second[j].delta_R<<endl;
1394     // }
1395     }
1396    
1397    
1398    
1399     }
1400    
1401    
1402    
1403     }
1404    
1405     }
1406    
1407     /////////////////////////////////////////////////////////////////////
1408     // made for muon fake rate
1409     // method that gives the vector<MatchingInfo> matching_Infos containing (dR,i,j); i=genParticle index, j=ParticleCollection index:
1410    
1411     void MuonAnalyzer::matching_JetsWithB(const edm::Event& iEvent, Handle<reco::CaloJetCollection> jets){
1412    
1413     cout <<" ulazi u matching_JetsWithB" <<endl;
1414    
1415     using namespace edm;
1416     using namespace reco;
1417     using namespace std;
1418    
1419     Handle<CandidateCollection> mccands;
1420     iEvent.getByLabel( theMCTruthCollection,mccands );
1421    
1422     vector <GenParticleCandidate*> genBparticles; // vector u kojem su svi B hadroni cija majka nije B hadron (b kvark)
1423     // vector <GenParticleCandidate*> genCparticles; // vector u kojem su svi D hadroni cija majka nije D niti B hadron (c kvark)
1424    
1425    
1426     for(CandidateCollection::const_iterator p = mccands->begin();
1427     p != mccands->end(); p++) {
1428    
1429     reco::GenParticleCandidate* ptr = (reco::GenParticleCandidate*) &(*p);
1430    
1431     ///////////////////////////////////////////////////////
1432     if ((((abs(ptr->pdgId()))/100)%10 ==5)||(((abs(ptr->pdgId()))/1000)%10 ==5 )) { // b
1433     if ((((abs(ptr->mother()->pdgId()))/100)%10 !=5)&&(((abs(ptr->mother()->pdgId()))/1000)%10 !=5 )) genBparticles.push_back(ptr); // mother is not b
1434     }
1435    
1436     ///////////////////////////////////////////////////////
1437    
1438    
1439     // int pdg_id=ptr->pdgId();
1440     // // cout << "particle id : " << pdg_id << endl;
1441    
1442     // while ( ptr ->mother()!=0 ) { //izvrsava se sve dok ne dodje do nule tj. dok ne trazi majku od protona
1443     // // cout << "Going up: ";
1444     // // cout << "Mother id : " << genParticle->pdgId() << endl;
1445     // ptr= (reco::GenParticleCandidate*) ptr->mother();
1446    
1447     // if (abs(ptr->pdgId())!=abs(pdg_id)) {
1448     // // cout<< " good mother " <<endl;
1449    
1450     // if ((((abs(ptr->pdgId()))/100)%10 ==5)||(((abs(ptr->pdgId()))/1000)%10 ==5 )) { // mother is b
1451     // // while (ptr = const_cast<reco::GenParticleCandidate *>(dynamic_cast<const reco::GenParticleCandidate *>(ptr->mother()))) {
1452     // while (ptr ->mother()!=0) {
1453    
1454     // // ptr= (reco::GenParticleCandidate*) ptr->mother();
1455     // // if ((((abs(ptr->pdgId()))/100)%10 !=5)||(((abs(ptr->pdgId()))/1000)%10 !=5 )){ // ako majka nije b
1456     // if ((((abs(ptr ->mother()->pdgId()))/100)%10 !=5)||(((abs(ptr ->mother()->pdgId()))/1000)%10 !=5 )){ // ako majka nije b
1457     // genBparticles.push_back(ptr);
1458     // break;
1459     // }
1460     // ptr= (reco::GenParticleCandidate*) ptr->mother();
1461     // }
1462    
1463     // }
1464    
1465     // }
1466    
1467     // }
1468    
1469     }
1470    
1471     int n1=0;
1472     vector<MatchingInfo> matching_Infos;
1473     cout <<"# jets="<<jets->size()<< " # B hadrons="<<genBparticles.size() << endl;
1474     for ( unsigned int i=0; i<genBparticles.size(); i++ ) // po svim MC B hadronima
1475     {
1476     // cout <<" next B hadron" <<endl;
1477     for (unsigned int j = 0; j < jets->size(); j++)// po svim jetovima
1478     {
1479     // cout <<" next jet" <<endl;
1480     double d_eta2=(*jets)[j].eta()-(genBparticles)[i]->eta();
1481     double d_phi=fabs((*jets)[j].phi()-(genBparticles)[i]->phi());
1482     double dR_byhand= sqrt(d_eta2*d_eta2+d_phi*d_phi);
1483     double dR=dR_byhand;
1484     // cout <<"dR="<< dR <<endl;
1485     // if (dR<0.15){
1486     n1++;
1487     matching_Infos.push_back(MatchingInfo(dR,i,j));
1488     // }
1489    
1490     }
1491     }
1492    
1493     sort(matching_Infos.begin(),matching_Infos.end(),betterMatch2); // sorting (dR,i,j)
1494    
1495     if (jets->size()!=0 && genBparticles.size()!=0){
1496     // Now exclude combinations using same gen or rec muons
1497     for (std::vector<MatchingInfo>::iterator it=matching_Infos.begin();
1498     it != matching_Infos.end(); it++) {
1499     for (std::vector<MatchingInfo>::iterator it2=matching_Infos.begin();
1500     it2!=it; it2++) {
1501     if ( (it->genMuon == it2->genMuon) || (it->recoMuon == it2->recoMuon) ) {
1502     matching_Infos.erase(it);
1503     it=matching_Infos.begin();
1504     break;
1505     }
1506     }
1507     }
1508     }
1509    
1510     // Now t in vector of pairs
1511    
1512     B_Jet_Matching[1].clear();
1513    
1514     for (vector<MatchingInfo>::iterator match=matching_Infos.begin();
1515     match !=matching_Infos.end(); match++) { // po tablici dr,i,j
1516    
1517     pair<const GenParticleCandidate*, const CaloJet*> pair;
1518     // vector<pair,float > matchedParticles;
1519     pair.first = genBparticles[match->genMuon];
1520     pair.second = &(*jets)[match->recoMuon];
1521    
1522     matchedJets_and_B cestice(genBparticles[match->genMuon],&(*jets)[match->recoMuon] , match->deltaR);
1523    
1524     B_Jet_Matching[1].push_back(cestice);
1525    
1526     }
1527    
1528     cout <<" nakon pisanja u B_Jet_Matching[1]" << endl;
1529     for (vector<MatchingInfo>::iterator match=matching_Infos.begin();
1530     match !=matching_Infos.end(); match++) { // po tablici dr,i,j
1531     // int j=0;
1532     for (map< int,
1533     std::vector< matchedJets_and_B > > ::iterator iter = B_Jet_Matching.begin();
1534     iter!=B_Jet_Matching.end(); iter++) // entire map
1535     {
1536     for (unsigned int j=0; j<iter->second.size(); j++){ // entire vector<class<>>
1537     const reco::Candidate * nesto=iter->second[j].pair.second;
1538     // ::OverlapChecker overlap;
1539     // if (overlap(*nesto,*daughter)){
1540     cout <<"*****dr="<< iter->second[j].delta_R<<endl;
1541     // cout <<"*****dr="<< iter->second[j].delta_R<<endl;
1542     // }
1543     }
1544    
1545     }
1546    
1547     }
1548    
1549     }
1550    
1551     /////////////////////////////////////////////////////////////////////
1552     // made for muon fake rate
1553     // method that gives the vector<MatchingInfo> matching_Infos containing (dR,i,j); i=genParticle index, j=ParticleCollection index:
1554    
1555     void MuonAnalyzer::matching_JetsWithC(const edm::Event& iEvent, Handle<reco::CaloJetCollection> jets){
1556    
1557     cout <<" ulazi u matching_JetsWithC" <<endl;
1558    
1559     using namespace edm;
1560     using namespace reco;
1561     using namespace std;
1562    
1563     Handle<CandidateCollection> mccands;
1564     iEvent.getByLabel( theMCTruthCollection,mccands );
1565    
1566     vector <GenParticleCandidate*> genCparticles; // vector u kojem su svi D hadroni cija majka nije D niti B hadron (c kvark)
1567    
1568     for(CandidateCollection::const_iterator p = mccands->begin();
1569     p != mccands->end(); p++) {
1570    
1571     reco::GenParticleCandidate* ptr = (reco::GenParticleCandidate*) &(*p);
1572    
1573     if ((((abs(ptr->pdgId()))/100)%10 ==4)||(((abs(ptr->pdgId()))/1000)%10 ==4 )) { // c
1574     if ((((abs(ptr->mother()->pdgId()))/100)%10 !=4)&&(((abs(ptr->mother()->pdgId()))/1000)%10 !=4 )
1575     && (((abs(ptr->mother()->pdgId()))/100)%10 !=5)&&(((abs(ptr->mother()->pdgId()))/1000)%10 !=5 ))
1576     genCparticles.push_back(ptr); // mother is not b or c
1577     }
1578    
1579     // int pdg_id=ptr->pdgId();
1580     // // cout << "particle id : " << pdg_id << endl;
1581    
1582     // while (ptr->mother()!=0) { //izvrsava se sve dok ne dodje do nule tj. dok ne trazi majku od protona
1583     // // cout << "Going up: ";
1584     // // cout << "Mother id : " << genParticle->pdgId() << endl;
1585     // ptr= (reco::GenParticleCandidate*) ptr->mother();
1586     // if (abs(ptr->pdgId())!=abs(pdg_id)) {
1587     // // cout<< " good mother " <<endl;
1588    
1589    
1590     // if ((((abs(ptr->pdgId()))/100)%10 ==4)||(((abs(ptr->pdgId()))/1000)%10 ==4 )) { // mother is c
1591     // while (ptr->mother()!=0) {
1592     // // ptr= (reco::GenParticleCandidate*) ptr->mother();
1593     // if (((((abs(ptr->mother()->pdgId()))/100)%10 !=4)||(((abs(ptr->mother()->pdgId()))/1000)%10 !=4 )) &&
1594     // ((((abs(ptr->mother()->pdgId()))/100)%10 !=5)||(((abs(ptr->mother()->pdgId()))/1000)%10 !=5 ))){ // ako majka nije c niti b
1595     // genCparticles.push_back(ptr);
1596     // break;
1597     // }
1598     // ptr= (reco::GenParticleCandidate*) ptr->mother();
1599     // }
1600     // }
1601    
1602    
1603     // }
1604     // }
1605    
1606     }
1607    
1608     int n1=0;
1609     vector<MatchingInfo> matching_Infos;
1610     cout <<"# jets="<<jets->size()<< " # C hadrons="<<genCparticles.size() << endl;
1611     for ( unsigned int i=0; i<genCparticles.size(); i++ ) // po svim MC B hadronima
1612     {
1613     // cout <<" next B hadron" <<endl;
1614     for (unsigned int j = 0; j < jets->size(); j++)// po svim jetovima
1615     {
1616     // cout <<" next jet" <<endl;
1617     double d_eta2=(*jets)[j].eta()-(genCparticles)[i]->eta();
1618     double d_phi=fabs((*jets)[j].phi()-(genCparticles)[i]->phi());
1619     double dR_byhand= sqrt(d_eta2*d_eta2+d_phi*d_phi);
1620     double dR=dR_byhand;
1621     // cout <<"dR="<< dR <<endl;
1622     // if (dR<0.15){
1623     n1++;
1624     matching_Infos.push_back(MatchingInfo(dR,i,j));
1625     // }
1626    
1627     }
1628     }
1629    
1630     sort(matching_Infos.begin(),matching_Infos.end(),betterMatch2); // sorting (dR,i,j)
1631    
1632     if (jets->size()!=0 && genCparticles.size()!=0){
1633     // Now exclude combinations using same gen or rec muons
1634     for (std::vector<MatchingInfo>::iterator it=matching_Infos.begin();
1635     it != matching_Infos.end(); it++) {
1636     for (std::vector<MatchingInfo>::iterator it2=matching_Infos.begin();
1637     it2!=it; it2++) {
1638     if ( (it->genMuon == it2->genMuon) || (it->recoMuon == it2->recoMuon) ) {
1639     matching_Infos.erase(it);
1640     it=matching_Infos.begin();
1641     break;
1642     }
1643     }
1644     }
1645     }
1646    
1647     // Now t in vector of pairs
1648    
1649     C_Jet_Matching[1].clear();
1650    
1651     for (vector<MatchingInfo>::iterator match=matching_Infos.begin();
1652     match !=matching_Infos.end(); match++) { // po tablici dr,i,j
1653    
1654     pair<const GenParticleCandidate*, const CaloJet*> pair;
1655     // vector<pair,float > matchedParticles;
1656     pair.first = genCparticles[match->genMuon];
1657     pair.second = &(*jets)[match->recoMuon];
1658    
1659     matchedJets_and_C cestice(genCparticles[match->genMuon],&(*jets)[match->recoMuon] , match->deltaR);
1660    
1661     C_Jet_Matching[1].push_back(cestice);
1662    
1663     }
1664    
1665     cout <<" nakon pisanja u C_Jet_Matching[1]" << endl;
1666     for (vector<MatchingInfo>::iterator match=matching_Infos.begin();
1667     match !=matching_Infos.end(); match++) { // po tablici dr,i,j
1668     for (map< int,
1669     std::vector< matchedJets_and_B > > ::iterator iter = B_Jet_Matching.begin();
1670     iter!=B_Jet_Matching.end(); iter++) // entire map
1671     {
1672     for (unsigned int j=0; j<iter->second.size(); j++){ // entire vector<class<>>
1673     const reco::Candidate * nesto=iter->second[j].pair.second;
1674     // ::OverlapChecker overlap;
1675     // if (overlap(*nesto,*daughter)){
1676     cout <<"*****dr="<< iter->second[j].delta_R<<endl;
1677     // cout <<"*****dr="<< iter->second[j].delta_R<<endl;
1678     // }
1679     }
1680    
1681     }
1682    
1683    
1684    
1685     }
1686    
1687     }
1688    
1689    
1690     //---------------------------------------------------
1691    
1692    
1693     // get mother of particle
1694     // returns mother = 1 if mother is Z boson
1695     // returns mother = 2 if mother is W boson
1696     // returns mother = 4 if mother is b
1697     // returns mother = 0 else
1698    
1699     MuonAnalyzer::LeptonOrigin MuonAnalyzer::getMother(const reco::Candidate* genParticle){
1700    
1701     int pdg_id=genParticle->pdgId();
1702     // cout << "particle id : " << pdg_id << endl;
1703    
1704     while (genParticle = genParticle->mother() ) { //izvrsava se sve dok ne dodje do nule tj. dok ne trazi majku od protona
1705     // cout << "Going up: ";
1706     // cout << "Mother id : " << genParticle->pdgId() << endl;
1707     if (abs(genParticle->pdgId())!=abs(pdg_id)) {
1708     // cout<< " good mother " <<endl;
1709     if (abs(genParticle->pdgId())==23){ // mother is Z
1710     return zboson;
1711     }
1712     if (abs(genParticle->pdgId())==24) { // mother is W
1713     return wboson;
1714     }
1715     // if (abs(genParticle->pdgId())==23 || abs(genParticle->pdgId())==24) { // mother is W or Z
1716     // WZ_matched=1;
1717     // mother=3;
1718     // }
1719     if ((((abs(genParticle->pdgId()))/100)%10 ==5)||(((abs(genParticle->pdgId()))/1000)%10 ==5 )) { // mother is b
1720     return bdecay;
1721     }
1722     if ((((abs(genParticle->pdgId()))/100)%10 ==4)||(((abs(genParticle->pdgId()))/1000)%10 ==4 )) { // mother is c
1723     return cdecay;
1724     }
1725     return other;
1726     }
1727     }
1728    
1729     return other;
1730     }
1731    
1732     ///////////////////
1733     int MuonAnalyzer::getMother_Id(const reco::Candidate* genParticle){
1734    
1735     int mother_id=0;
1736    
1737     int pdg_id=genParticle->pdgId();
1738     // cout << "particle id : " << pdg_id << endl;
1739    
1740     while (genParticle = genParticle->mother() ) { //izvrsava se sve dok ne dodje do nule tj. dok ne trazi majku od protona
1741     if (abs(genParticle->pdgId())!=abs(pdg_id)) {
1742     mother_id=genParticle->pdgId();
1743     return mother_id;
1744     }
1745     }
1746    
1747     return mother_id;
1748     }
1749    
1750     /////////////////
1751    
1752    
1753     const reco::Candidate * MuonAnalyzer::MatchedGenParticle(const reco::Candidate * daughter){
1754     ::OverlapChecker overlap;
1755     matched_genParticle=0;
1756    
1757     for (map< int,
1758     std::vector< matchedParticles > > ::iterator iter = leptonMatching.begin();
1759     iter!=leptonMatching.end(); iter++) // entire map
1760     {
1761     for (unsigned int j=0; j<iter->second.size(); j++){ // entire vector<class<>>
1762     const reco::Candidate * nesto=iter->second[j].pair.second;
1763     if (overlap(*nesto,*daughter)){
1764     matched_genParticle=iter->second[j].pair.first;
1765     }
1766     }
1767     }
1768     return matched_genParticle;
1769     }
1770    
1771     /////////////////////////////////////////
1772     // for muon fake rate
1773    
1774     const reco::Candidate * MuonAnalyzer::MatchedMuonWithJet(const reco::CaloJet * daughter){
1775     ::OverlapChecker overlap;
1776     matched_Muon=0;
1777    
1778     for (map< int,
1779     std::vector< matchedJets_and_Muons > > ::iterator iter = Muon_Jet_Matching.begin();
1780     iter!=Muon_Jet_Matching.end(); iter++) // entire map
1781     {
1782     // cout <<" ide preko cijele mape za jet" <<endl;
1783     for (unsigned int j=0; j<iter->second.size(); j++){ // entire vector<class<>>
1784     const reco::CaloJet * nesto=iter->second[j].pair.first;
1785     if (overlap(*nesto,*daughter)){
1786     matched_Muon=iter->second[j].pair.second; /// tu je bila greska
1787     }
1788     }
1789     }
1790     return matched_Muon;
1791     }
1792    
1793    
1794     /////////////////////////////
1795     // Jet and B hadron matching
1796    
1797     const reco::Candidate * MuonAnalyzer::MatchedBhadronWithJet(const reco::CaloJet * daughter){
1798     ::OverlapChecker overlap;
1799     matched_B=0;
1800    
1801     for (map< int,
1802     std::vector< matchedJets_and_B > > ::iterator iter = B_Jet_Matching.begin();
1803     iter!=B_Jet_Matching.end(); iter++) // entire map
1804     {
1805     // cout <<" ide preko cijele mape za jet" <<endl;
1806     for (unsigned int j=0; j<iter->second.size(); j++){ // entire vector<class<>>
1807     const reco::CaloJet * nesto=iter->second[j].pair.second;
1808     if (overlap(*nesto,*daughter)){
1809     matched_B=iter->second[j].pair.first; /// tu je bila greska
1810     }
1811     }
1812     }
1813     return matched_B;
1814     }
1815    
1816     /////////////////////////////
1817     // Jet and C hadron matching
1818    
1819     const reco::Candidate * MuonAnalyzer::MatchedChadronWithJet(const reco::CaloJet * daughter){
1820     ::OverlapChecker overlap;
1821     matched_C=0;
1822    
1823     for (map< int,
1824     std::vector< matchedJets_and_C > > ::iterator iter = C_Jet_Matching.begin();
1825     iter!=C_Jet_Matching.end(); iter++) // entire map
1826     {
1827     // cout <<" ide preko cijele mape za jet" <<endl;
1828     for (unsigned int j=0; j<iter->second.size(); j++){ // entire vector<class<>>
1829     const reco::CaloJet * nesto=iter->second[j].pair.second;
1830     if (overlap(*nesto,*daughter)){
1831     matched_C=iter->second[j].pair.first; /// tu je bila greska
1832     }
1833     }
1834     }
1835     return matched_C;
1836     }
1837    
1838    
1839     float MuonAnalyzer::dR(const reco::Candidate * daughter){
1840    
1841     ::OverlapChecker overlap;
1842    
1843     float delta_r = -10;
1844     for (map< int,
1845     std::vector< matchedParticles > > ::iterator iter = leptonMatching.begin();
1846     iter!=leptonMatching.end(); iter++) // entire map
1847     {
1848     for (unsigned int j=0; j<iter->second.size(); j++){ // entire vector<class<>>
1849     const reco::Candidate * nesto=iter->second[j].pair.second;
1850     if (overlap(*nesto,*daughter)){
1851     delta_r=iter->second[j].delta_R;
1852     }
1853     }
1854     }
1855     return delta_r;
1856     }
1857    
1858     float MuonAnalyzer::dR_Muon_Jet(const reco::CaloJet * daughter){
1859    
1860     ::OverlapChecker overlap;
1861    
1862     float delta_r = -10;
1863     for (map< int,
1864     std::vector<matchedJets_and_Muons > > ::iterator iter = Muon_Jet_Matching.begin();
1865     iter!=Muon_Jet_Matching.end(); iter++) // entire map
1866     {
1867     for (unsigned int j=0; j<iter->second.size(); j++){ // entire vector<class<>>
1868     const reco::CaloJet * nesto=iter->second[j].pair.first;
1869     if (overlap(*nesto,*daughter)){
1870     delta_r=iter->second[j].delta_R;
1871     }
1872     }
1873     }
1874     return delta_r;
1875     }
1876    
1877     float MuonAnalyzer::dR_B_Jet(const reco::CaloJet * daughter){
1878    
1879     ::OverlapChecker overlap;
1880    
1881     float delta_r = -10;
1882     for (map< int,
1883     std::vector<matchedJets_and_B > > ::iterator iter = B_Jet_Matching.begin();
1884     iter!=B_Jet_Matching.end(); iter++) // entire map
1885     {
1886     for (unsigned int j=0; j<iter->second.size(); j++){ // entire vector<class<>>
1887     const reco::CaloJet * nesto=iter->second[j].pair.second;
1888     if (overlap(*nesto,*daughter)){
1889     delta_r=iter->second[j].delta_R;
1890     }
1891     }
1892     }
1893     return delta_r;
1894     }
1895    
1896     float MuonAnalyzer::dR_C_Jet(const reco::CaloJet * daughter){
1897    
1898     ::OverlapChecker overlap;
1899    
1900     float delta_r = -10;
1901     for (map< int,
1902     std::vector<matchedJets_and_C > > ::iterator iter = C_Jet_Matching.begin();
1903     iter!=C_Jet_Matching.end(); iter++) // entire map
1904     {
1905     for (unsigned int j=0; j<iter->second.size(); j++){ // entire vector<class<>>
1906     const reco::CaloJet * nesto=iter->second[j].pair.second;
1907     if (overlap(*nesto,*daughter)){
1908     delta_r=iter->second[j].delta_R;
1909     }
1910     }
1911     }
1912     return delta_r;
1913     }
1914    
1915    
1916    
1917     void MuonAnalyzer::collectMCsummary(Handle <reco::CandidateCollection> mccands) {
1918    
1919     using namespace reco;
1920    
1921     //collections
1922    
1923     MCleptZ1_pdgid = -1;
1924     MCleptZ2_pdgid = -1;
1925     MCleptW_pdgid = -1;
1926    
1927     MCleptZ1_pt = -1;
1928     MCleptZ2_pt = -1;
1929     MCleptW_pt = -1;
1930    
1931     MCleptZ1_eta = -1;
1932     MCleptZ2_eta = -1;
1933     MCleptW_eta = -1;
1934    
1935     MCleptZ1_phi = -1;
1936     MCleptZ2_phi = -1;
1937     MCleptW_phi = -1;
1938    
1939     vector<reco::GenParticleCandidate*> Tau;
1940     vector<reco::GenParticleCandidate*> StableMuons;
1941     vector<reco::GenParticleCandidate*> StableElectrons;
1942    
1943    
1944    
1945     for(CandidateCollection::const_iterator p = mccands->begin();
1946     p != mccands->end(); p++) {
1947    
1948     // reco::Candidate* p_tmp = &(*p);
1949     reco::GenParticleCandidate* ptr = (reco::GenParticleCandidate*) &(*p);
1950    
1951     if ( (ptr)->status() == 1
1952     /*&& (ptr)->momentum().perp() > 2.*/ ) { // stable particles with pt>2 only
1953     if ( abs((ptr)->pdgId()) == 11 ) {
1954    
1955     StableElectrons.push_back((ptr));
1956     //cout << "electron MCT\n";
1957     }
1958     else if ( abs((ptr)->pdgId()) == 13 ) {
1959    
1960     StableMuons.push_back((ptr)) ;
1961     //cout << "muon MCT\n";
1962     }
1963     }
1964     else if ((ptr)->status() == 2) {
1965     if ( abs((ptr)->pdgId()) == 15 ) {//tau
1966     Tau.push_back((ptr));
1967     }
1968     }
1969     }
1970    
1971     // std::cout << "# Electrons : " << StableElectrons.size()
1972     // << "# Muons : " << StableMuons.size() << std::endl
1973     // << "# Tau : " << Tau.size() << std::endl;
1974    
1975    
1976     vector<reco::GenParticleCandidate*> * StableLeptons[3] = {&StableElectrons, &StableMuons, &Tau};
1977    
1978     vector<reco::GenParticleCandidate*> TauChildLeptons;
1979    
1980    
1981     //erase electrons and muons from tau from GenparticleCandidate Vector and put to Tau Vector
1982     for (int i=1; i>=0; i--) {
1983     for ( vector<reco::GenParticleCandidate*>::iterator lepton = StableLeptons[i]->begin();
1984     lepton != StableLeptons[i]->end();lepton++) {
1985    
1986     reco::GenParticleCandidate* mcParticleRef = *lepton;
1987    
1988     int parentItr=0;
1989     while ( mcParticleRef->mother()!=0 && parentItr<2) {
1990    
1991     parentItr++;
1992    
1993     mcParticleRef = (reco::GenParticleCandidate*) mcParticleRef->mother();
1994     if (mcParticleRef==0) break;
1995    
1996     //if tau
1997     if (abs((mcParticleRef)->pdgId())==15 ) {
1998     //put into collection
1999     TauChildLeptons.push_back(*lepton);
2000     StableLeptons[i]->erase(lepton);
2001     lepton--;
2002     break;
2003     }
2004     }
2005     }
2006     }
2007    
2008    
2009     bool firstZlept = true;
2010     int MC_tauDecayTypeZ1 = 0;
2011     int MC_tauDecayTypeZ2 = 0;
2012     int MC_tauDecayTypeW = 0;
2013    
2014    
2015     for (int i=2; i>=0; i--) {
2016     while (StableLeptons[i]->size() > 0) {
2017     float maxPt = 0;
2018     vector<reco::GenParticleCandidate*>::iterator index = StableLeptons[i]->end();
2019    
2020     for ( vector<reco::GenParticleCandidate*>::iterator lepton = StableLeptons[i]->begin();
2021     lepton != StableLeptons[i]->end(); lepton++ ) {
2022    
2023     if ((*lepton)->pt() > maxPt) {
2024     maxPt = (*lepton)->pt();
2025     index = lepton;
2026     }
2027     }
2028     bool Zchild = false;
2029     bool Wchild = false;
2030     bool isTau = false;
2031    
2032    
2033     reco::GenParticleCandidate* mcParticleRef = *index;
2034    
2035     if (abs((*index)->pdgId()) == 15) isTau = true;
2036    
2037     //find W/Z mother
2038     int parentItr=0;
2039     while ( mcParticleRef->mother()!=0 && parentItr<2) {
2040    
2041     if (parentItr>=2) break;
2042     parentItr++;
2043     mcParticleRef = (reco::GenParticleCandidate*) mcParticleRef->mother();
2044     if (mcParticleRef==0) break;
2045    
2046     if (abs((mcParticleRef)->pdgId())==23 ) {
2047     Zchild=true;
2048     break;
2049     }
2050     if (abs((mcParticleRef)->pdgId())==24 ) {
2051     Wchild=true;
2052     break;
2053     }
2054     }
2055    
2056    
2057     if (maxPt >0) {
2058     int * MCtauDecayTypePtr = 0;
2059    
2060     if (Wchild) {
2061     MCleptW_pdgid=(*index)->pdgId();
2062     MCleptW_pt=(float)(*index)->pt();
2063     MCleptW_eta=(float)(*index)->eta();
2064     MCleptW_phi=(float)(*index)->phi();
2065     if (isTau) {
2066     MCtauDecayTypePtr = &MC_tauDecayTypeW;
2067     MC_tauDecayTypeW =3;//default to hadronic decay
2068     }
2069    
2070     }
2071     if (Zchild) {
2072     if (firstZlept) {
2073     firstZlept=false;
2074     MCleptZ1_pdgid=(*index)->pdgId();
2075     MCleptZ1_pt=(float)(*index)->pt();
2076     MCleptZ1_eta=(float)(*index)->eta();
2077     MCleptZ1_phi=(float)(*index)->phi();
2078     if (isTau) {
2079     MCtauDecayTypePtr = &MC_tauDecayTypeZ1;
2080     MC_tauDecayTypeZ1 =3;
2081     }
2082    
2083     }
2084     else {
2085     MCleptZ2_pdgid=(*index)->pdgId();
2086     MCleptZ2_pt=(float)(*index)->pt();
2087     MCleptZ2_eta=(float)(*index)->eta();
2088     MCleptZ2_phi=(float)(*index)->phi();
2089     if (isTau) {
2090     MCtauDecayTypePtr = &MC_tauDecayTypeZ2;
2091     MC_tauDecayTypeZ2 =3;
2092     }
2093    
2094     }
2095     }
2096     //check type of tau decay
2097     if (MCtauDecayTypePtr) {
2098     for( vector<reco::GenParticleCandidate*>::iterator p = TauChildLeptons.begin();p != TauChildLeptons.end(); p++) {
2099     int pitr = 0;
2100     reco::GenParticleCandidate* mcParticleRef = *p;
2101     while (mcParticleRef->mother() && pitr<2) {
2102     pitr++;
2103     mcParticleRef =(reco::GenParticleCandidate*) mcParticleRef->mother();
2104     if (mcParticleRef==0) break;
2105     if (mcParticleRef == *index) {
2106     if (abs((*p)->pdgId()) == 11) *MCtauDecayTypePtr = 1;
2107     if (abs((*p)->pdgId()) == 13) *MCtauDecayTypePtr = 2;
2108     }
2109     }
2110     }
2111     }
2112     }
2113     StableLeptons[i]->erase(index);
2114     }
2115     }
2116     }
2117     //define this as a plug-in
2118     // DEFINE_FWK_MODULE(WZAnalyzer);
2119