CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC4_patch1/src/ElectroWeakAnalysis/ZMuMu/plugins/ZMCHistogrammer.cc

Go to the documentation of this file.
00001 #include "FWCore/Framework/interface/EDAnalyzer.h"
00002 #include "FWCore/Utilities/interface/InputTag.h"
00003 #include "TH1.h"
00004 
00005 class ZMCHistogrammer : public edm::EDAnalyzer {
00006 public:
00007   ZMCHistogrammer(const edm::ParameterSet& pset); 
00008 private:
00009   virtual void analyze(const edm::Event& event, const edm::EventSetup& setup);
00010   edm::InputTag  z_, gen_, match_;
00011   edm::InputTag hepMCProductTag_;
00012   unsigned int nbinsMass_, nbinsPt_, nbinsAng_, nbinsMassRes_;
00013   double massMax_, ptMax_, angMax_, massResMax_;
00014   TH1F *h_nZ_, *h_mZ_, *h_ptZ_, *h_phiZ_, *h_thetaZ_, *h_etaZ_, *h_rapidityZ_;
00015   TH1F *h_invmMuMu_;
00016   TH1F *h_nZMC_, *h_mZMC_, *h_ptZMC_, *h_phiZMC_, *h_thetaZMC_, *h_etaZMC_, *h_rapidityZMC_;
00017   TH1F *h_invmMuMuMC_ , * h_weight_histo; 
00018   //TH1F *h_mZ2vs3MC_, *h_ptZ2vs3MC_, *h_phiZ2vs3MC_, *h_thetaZ2vs3MC_, *h_etaZ2vs3MC_, *h_rapidityZ2vs3MC_;
00019   TH1F *h_mResZ_, *h_ptResZ_, *h_phiResZ_, *h_thetaResZ_, *h_etaResZ_, *h_rapidityResZ_;
00020   TH1F *h_mResZMuMu_, *h_mRatioZMuMu_;
00021   TH1F *h_mResZMuMuMC_, *h_mRatioZMuMuMC_;
00022   bool isMCatNLO_; 
00023 };
00024 
00025 #include "DataFormats/Candidate/interface/Candidate.h"
00026 #include "DataFormats/Candidate/interface/CandidateFwd.h"
00027 #include "DataFormats/Candidate/interface/CandMatchMap.h"
00028 #include "DataFormats/HepMCCandidate/interface/GenParticle.h"
00029 #include "DataFormats/HepMCCandidate/interface/GenParticleFwd.h"
00030 #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
00031 
00032 #include "HepMC/WeightContainer.h"
00033 #include "HepMC/GenEvent.h"
00034 #include "FWCore/Framework/interface/Event.h"
00035 #include "DataFormats/Common/interface/Handle.h"
00036 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00037 #include "FWCore/ServiceRegistry/interface/Service.h"
00038 #include "CommonTools/UtilAlgos/interface/TFileService.h"
00039 #include <cmath>
00040 #include <iostream>
00041 
00042 using namespace std;
00043 using namespace reco;
00044 using namespace edm;
00045 
00046 ZMCHistogrammer::ZMCHistogrammer(const ParameterSet& pset) :
00047   z_(pset.getParameter<InputTag>("z")),
00048   gen_(pset.getParameter<InputTag>("gen")), 
00049   match_(pset.getParameter<InputTag>("match")),
00050   hepMCProductTag_(pset.getParameter<InputTag>("hepMCProductTag")),
00051   nbinsMass_(pset.getUntrackedParameter<unsigned int>("nbinsMass")),
00052   nbinsPt_(pset.getUntrackedParameter<unsigned int>("nbinsPt")),
00053   nbinsAng_(pset.getUntrackedParameter<unsigned int>("nbinsAng")),
00054   nbinsMassRes_(pset.getUntrackedParameter<unsigned int>("nbinsMassRes")),
00055   massMax_(pset.getUntrackedParameter<double>("massMax")),
00056   ptMax_(pset.getUntrackedParameter<double>("ptMax")),
00057   angMax_(pset.getUntrackedParameter<double>("angMax")), 
00058   massResMax_(pset.getUntrackedParameter<double>("massResMax")),
00059   isMCatNLO_(pset.getUntrackedParameter<bool>("isMCatNLO"))  { 
00060   cout << ">>> Z Histogrammer constructor" << endl;
00061   Service<TFileService> fs;
00062   TFileDirectory ZHisto = fs->mkdir( "ZRecoHisto" );
00063   TFileDirectory ZMCHisto = fs->mkdir( "ZMCHisto" );
00064   TFileDirectory ZResHisto = fs->mkdir( "ZResolutionHisto" );
00065   //TFileDirectory Z2vs3MCHisto = fs->mkdir( "Z2vs3MCHisto" );
00066   h_nZ_ = ZHisto.make<TH1F>("ZNumber", "number of Z particles", 11, -0.5, 10.5);
00067   h_mZ_ = ZHisto.make<TH1F>("ZMass", "Z mass (GeV/c^{2})", nbinsMass_,  0, massMax_);
00068   h_ptZ_ = ZHisto.make<TH1F>("ZPt", "Z p_{t} (GeV/c)", nbinsPt_, 0, ptMax_);
00069   h_phiZ_ = ZHisto.make<TH1F>("ZPhi", "Z #phi", nbinsAng_,  -angMax_, angMax_);
00070   h_thetaZ_ = ZHisto.make<TH1F>("Ztheta", "Z #theta", nbinsAng_,  0, angMax_);
00071   h_etaZ_ = ZHisto.make<TH1F>("ZEta", "Z #eta", nbinsAng_,  -angMax_, angMax_);
00072   h_rapidityZ_ = ZHisto.make<TH1F>("ZRapidity", "Z rapidity", nbinsAng_,  -angMax_, angMax_);
00073   h_invmMuMu_ = ZHisto.make<TH1F>("MuMuMass", "#mu #mu invariant mass",nbinsMass_,  0, massMax_);
00074   h_weight_histo  = ZHisto.make<TH1F>("weight_histo","weight_histo",20,-10,10); 
00075   h_nZMC_ = ZMCHisto.make<TH1F>("ZMCNumber", "number of Z MC particles", 11, -0.5, 10.5);
00076   h_mZMC_ = ZMCHisto.make<TH1F>("ZMCMass", "Z MC mass (GeV/c^{2})", nbinsMass_,  0, massMax_);
00077   h_ptZMC_ = ZMCHisto.make<TH1F>("ZMCPt", "Z MC p_{t} (GeV/c)", nbinsPt_, 0, ptMax_);
00078   h_phiZMC_ = ZMCHisto.make<TH1F>("ZMCPhi", "Z MC #phi", nbinsAng_,  -angMax_, angMax_);
00079   h_thetaZMC_ = ZMCHisto.make<TH1F>("ZMCTheta", "Z MC #theta", nbinsAng_,  0, angMax_);
00080   h_etaZMC_ = ZMCHisto.make<TH1F>("ZMCEta", "Z MC #eta", nbinsAng_,  -angMax_, angMax_);
00081   h_rapidityZMC_ = ZMCHisto.make<TH1F>("ZMCRapidity", "Z MC rapidity",  nbinsAng_,  -angMax_, angMax_);
00082   h_invmMuMuMC_ = ZMCHisto.make<TH1F>("MuMuMCMass", "#mu #mu MC invariant mass", 
00083                                       nbinsMass_,  0, massMax_);
00084   /*
00085   h_mZ2vs3MC_ = Z2vs3MCHisto.make<TH1F>("Z2vs3MCMass", "Z MC st 2 vs st 3 mass (GeV/c^{2})", 
00086                                         nbinsMassRes_, -massResMax_, massResMax_);
00087   h_ptZ2vs3MC_ = Z2vs3MCHisto.make<TH1F>("Z2vs3MCPt", "Z MC st 2 vs st 3 p_{t} (GeV/c)", 
00088                                          nbinsPt_, -ptMax_, ptMax_);
00089   h_phiZ2vs3MC_ = Z2vs3MCHisto.make<TH1F>("Z2vs3MCPhi", "Z MC st 2 vs st 3 #phi", 
00090                                           nbinsAng_,  -angMax_, angMax_);
00091   h_thetaZ2vs3MC_ = Z2vs3MCHisto.make<TH1F>("Z2vs3MCTheta", "Z MC st 2 vs st 3 #theta", 
00092                                             nbinsAng_,  -angMax_, angMax_);
00093   h_etaZ2vs3MC_ = Z2vs3MCHisto.make<TH1F>("Z2vs3MCEta", "Z MC st 2 vs st 3 #eta", 
00094                                           nbinsAng_,  -angMax_, angMax_);
00095   h_rapidityZ2vs3MC_ = Z2vs3MCHisto.make<TH1F>("Z2vs3MCRapidity", "Z MC st 2 vs st 3 rapidity", 
00096                                        nbinsAng_,  -angMax_, angMax_);
00097   */
00098   h_mResZ_ = ZResHisto.make<TH1F>("ZMassResolution", "Z mass Resolution (GeV/c^{2})", 
00099                                   nbinsMassRes_, -massResMax_, massResMax_);
00100   h_ptResZ_ = ZResHisto.make<TH1F>("ZPtResolution", "Z p_{t} Resolution (GeV/c)", 
00101                                    nbinsPt_, -ptMax_, ptMax_);
00102   h_phiResZ_ = ZResHisto.make<TH1F>("ZPhiResolution", "Z #phi Resolution", 
00103                                     nbinsAng_,  -angMax_, angMax_);
00104   h_thetaResZ_ = ZResHisto.make<TH1F>("ZThetaResolution", "Z #theta Resolution", 
00105                                       nbinsAng_, -angMax_, angMax_);
00106   h_etaResZ_ = ZResHisto.make<TH1F>("ZEtaResolution", "Z #eta Resolution", 
00107                                     nbinsAng_,  -angMax_, angMax_);
00108   h_rapidityResZ_ = ZResHisto.make<TH1F>("ZRapidityResolution", "Z rapidity Resolution", 
00109                                          nbinsAng_,  -angMax_, angMax_);
00110   h_mResZMuMu_ = ZResHisto.make<TH1F>("ZToMuMuRecoMassResolution", 
00111                                       "Z Reco vs matched final state #mu #mu mass Difference (GeV/c^{2})", 
00112                                       nbinsMassRes_, -massResMax_, massResMax_);
00113   h_mRatioZMuMu_ = ZResHisto.make<TH1F>("ZToMuMuRecoMassRatio", 
00114                                         "Z Reco vs matched final state #mu #mu mass Ratio", 
00115                                         4000, 0, 2);
00116   h_mResZMuMuMC_ = ZResHisto.make<TH1F>("ZToMuMuMCMassResolution", 
00117                                         "Z vs final state #mu #mu MC mass Difference (GeV/c^{2})", 
00118                                         nbinsMassRes_/2 + 1, -2*massResMax_/nbinsMassRes_, massResMax_);
00119   h_mRatioZMuMuMC_ = ZResHisto.make<TH1F>("ZToMuMuMCMassRatio", 
00120                                           "Z vs final state #mu #mu MC mass Ratio", 
00121                                           2002, 0.999, 2);
00122 }
00123 
00124 void ZMCHistogrammer::analyze(const edm::Event& event, const edm::EventSetup& setup) { 
00125   cout << ">>> Z Histogrammer analyze" << endl;
00126   Handle<CandidateView> z;
00127   Handle<CandidateView> gen;
00128   Handle<std::vector<GenParticleRef> > match;
00129   event.getByLabel(z_, z);
00130   event.getByLabel(gen_, gen);
00131   event.getByLabel(match_, match);
00132   h_nZ_->Fill(z->size());
00133  
00134 // get HepMC::GenEvent ...
00135    Handle<HepMCProduct> evt_h;
00136    event.getByLabel(hepMCProductTag_, evt_h);
00137    HepMC::GenEvent* evt = new  HepMC::GenEvent(*(evt_h->GetEvent()));
00138 
00139 
00140    // get weight and fill it to histogram
00141   HepMC::WeightContainer weights = evt->weights();   
00142   double weight = weights.front();
00143   if(!weight) weight=1.;
00144   h_weight_histo->Fill(weight); 
00145   
00146   if(isMCatNLO_) {
00147     weight > 0 ?  weight=1. : weight=-1.;
00148   }
00149 
00150   for(unsigned int i = 0; i < z->size(); ++i) {
00151     const Candidate &zCand = (*z)[i];
00152     h_mZ_->Fill(zCand.mass(),weight );
00153     h_ptZ_->Fill(zCand.pt(),weight);
00154     h_phiZ_->Fill(zCand.phi(),weight);
00155     h_thetaZ_->Fill(zCand.theta(),weight);
00156     h_etaZ_->Fill(zCand.eta(),weight);
00157     h_rapidityZ_->Fill(zCand.rapidity(),weight);
00158     CandidateBaseRef zCandRef = z->refAt(i);   
00159    
00160  GenParticleRef zMCMatch = (*match)[i];
00161     if(zMCMatch.isNonnull() && zMCMatch->pdgId()==23) {
00162       h_mResZ_->Fill(zCandRef->mass() - zMCMatch->mass());
00163       h_ptResZ_->Fill(zCandRef->pt() - zMCMatch->pt());
00164       h_phiResZ_->Fill(zCandRef->phi() - zMCMatch->phi());
00165       h_thetaResZ_->Fill(zCandRef->theta() - zMCMatch->theta());
00166       h_etaResZ_->Fill(zCandRef->eta() - zMCMatch->eta());
00167       h_rapidityResZ_->Fill(zCandRef->rapidity() - zMCMatch->rapidity());
00168       const Candidate * dau0 = zMCMatch->daughter(0);
00169       const Candidate * dau1 = zMCMatch->daughter(1);
00170       for(unsigned int i0 = 0; i0 < dau0->numberOfDaughters(); ++i0) {
00171         const Candidate * ddau0 = dau0->daughter(i0);
00172         if(abs(ddau0->pdgId())==13 && ddau0->status()==1) {
00173           dau0 = ddau0; break;
00174         }
00175       }
00176       for(unsigned int i1 = 0; i1 < dau1->numberOfDaughters(); ++i1) {
00177         const Candidate * ddau1 = dau1->daughter(i1);
00178         if(abs(ddau1->pdgId())==13 && ddau1->status()==1) {
00179           dau1 = ddau1; break;
00180         }
00181       }
00182       assert(abs(dau0->pdgId())==13 && dau0->status()==1);
00183       assert(abs(dau1->pdgId())==13 && dau1->status()==1);
00184       double invMass = (dau0->p4()+dau1->p4()).mass();
00185       h_invmMuMu_->Fill(invMass,weight);
00186       h_mResZMuMu_->Fill(zCand.mass() - invMass);
00187       h_mRatioZMuMu_->Fill(zCand.mass()/invMass);
00188     }
00189   }
00190   h_nZMC_->Fill(gen->size());
00191   for(unsigned int i = 0; i < gen->size(); ++i) {
00192     const Candidate &genCand = (*gen)[i];
00193     if((genCand.pdgId() == 23) && (genCand.status() == 2)) //this is an intermediate Z0
00194       cout << ">>> intermediate Z0 found, with " << genCand.numberOfDaughters() 
00195            << " daughters" << endl;
00196     if((genCand.pdgId() == 23)&&(genCand.status() == 3)) { //this is a Z0
00197       cout << ">>> Z0 found, with " << genCand.numberOfDaughters() 
00198            << " daughters" << endl;
00199       h_mZMC_->Fill(genCand.mass(),weight);
00200       h_ptZMC_->Fill(genCand.pt(),weight);
00201       h_phiZMC_->Fill(genCand.phi(),weight);
00202       h_thetaZMC_->Fill(genCand.theta(),weight);
00203       h_etaZMC_->Fill(genCand.eta(),weight);
00204       h_rapidityZMC_->Fill(genCand.rapidity(),weight);
00205       Particle::LorentzVector pZ(0, 0, 0, 0);
00206       int nMu = 0;
00207       for(unsigned int j = 0; j < genCand.numberOfDaughters(); ++j) { 
00208         const Candidate *dauGen = genCand.daughter(j);
00209         /*
00210         if((dauGen->pdgId() == 23) && (dauGen->status() == 2)) { 
00211           h_mZ2vs3MC_->Fill(genCand.mass() - dauGen->mass());
00212           h_ptZ2vs3MC_->Fill(genCand.pt() - dauGen->pt());
00213           h_phiZ2vs3MC_->Fill(genCand.phi() - dauGen->phi());
00214           h_thetaZ2vs3MC_->Fill(genCand.theta() - dauGen->theta());
00215           h_etaZ2vs3MC_->Fill(genCand.eta() - dauGen->eta());
00216           h_rapidityZ2vs3MC_->Fill(genCand.rapidity() - dauGen->rapidity());
00217         }
00218         */
00219         if((abs(dauGen->pdgId()) == 13) && (dauGen->numberOfDaughters() != 0)) {
00220           //we are looking for photons of final state radiation
00221           cout << ">>> The muon " << j 
00222                << " has " << dauGen->numberOfDaughters() << " daughters" <<endl;
00223           for(unsigned int k = 0; k < dauGen->numberOfDaughters(); ++k) {
00224             const Candidate * dauMuGen = dauGen->daughter(k);
00225             cout << ">>> Mu " << j 
00226                  << " daughter MC " << k 
00227                  << " PDG Id " << dauMuGen->pdgId() 
00228                  << ", status " << dauMuGen->status() 
00229                  << ", charge " << dauMuGen->charge() 
00230                  << endl;
00231             if(abs(dauMuGen->pdgId()) == 13 && dauMuGen->status() ==1) {
00232               pZ += dauMuGen->p4();
00233               nMu ++;
00234             }
00235           }
00236         }
00237       }
00238       assert(nMu == 2);
00239       double mZ = pZ.mass();
00240       h_invmMuMuMC_->Fill(mZ,weight);
00241       h_mResZMuMuMC_->Fill(genCand.mass() - mZ);
00242       h_mRatioZMuMuMC_->Fill(genCand.mass()/mZ);
00243     }
00244   }
00245 }
00246 
00247 #include "FWCore/Framework/interface/MakerMacros.h"
00248 
00249 DEFINE_FWK_MODULE(ZMCHistogrammer);
00250