CMS 3D CMS Logo

ZMCHistogrammer.cc
Go to the documentation of this file.
8 #include "TH1.h"
9 
11 public:
13 
14 private:
15  void analyze(const edm::Event &event, const edm::EventSetup &setup) override;
23  TH1F *h_invmMuMu_;
26  //TH1F *h_mZ2vs3MC_, *h_ptZ2vs3MC_, *h_phiZ2vs3MC_, *h_thetaZ2vs3MC_, *h_etaZ2vs3MC_, *h_rapidityZ2vs3MC_;
30  bool isMCatNLO_;
31 };
32 
34 
35 #include "HepMC/WeightContainer.h"
36 #include "HepMC/GenEvent.h"
42 #include <cmath>
43 #include <iostream>
44 
45 using namespace std;
46 using namespace reco;
47 using namespace edm;
48 
50  : zToken_(consumes<CandidateView>(pset.getParameter<InputTag>("z"))),
51  genToken_(consumes<CandidateView>(pset.getParameter<InputTag>("gen"))),
52  matchToken_(consumes<std::vector<GenParticleRef> >(pset.getParameter<InputTag>("match"))),
53  hepMCProductToken_(consumes<HepMCProduct>(pset.getParameter<InputTag>("hepMCProductTag"))),
54  nbinsMass_(pset.getUntrackedParameter<unsigned int>("nbinsMass")),
55  nbinsPt_(pset.getUntrackedParameter<unsigned int>("nbinsPt")),
56  nbinsAng_(pset.getUntrackedParameter<unsigned int>("nbinsAng")),
57  nbinsMassRes_(pset.getUntrackedParameter<unsigned int>("nbinsMassRes")),
58  massMax_(pset.getUntrackedParameter<double>("massMax")),
59  ptMax_(pset.getUntrackedParameter<double>("ptMax")),
60  angMax_(pset.getUntrackedParameter<double>("angMax")),
61  massResMax_(pset.getUntrackedParameter<double>("massResMax")),
62  isMCatNLO_(pset.getUntrackedParameter<bool>("isMCatNLO")) {
63  cout << ">>> Z Histogrammer constructor" << endl;
65  TFileDirectory ZHisto = fs->mkdir("ZRecoHisto");
66  TFileDirectory ZMCHisto = fs->mkdir("ZMCHisto");
67  TFileDirectory ZResHisto = fs->mkdir("ZResolutionHisto");
68  //TFileDirectory Z2vs3MCHisto = fs->mkdir( "Z2vs3MCHisto" );
69  h_nZ_ = ZHisto.make<TH1F>("ZNumber", "number of Z particles", 11, -0.5, 10.5);
70  h_mZ_ = ZHisto.make<TH1F>("ZMass", "Z mass (GeV/c^{2})", nbinsMass_, 0, massMax_);
71  h_ptZ_ = ZHisto.make<TH1F>("ZPt", "Z p_{t} (GeV/c)", nbinsPt_, 0, ptMax_);
72  h_phiZ_ = ZHisto.make<TH1F>("ZPhi", "Z #phi", nbinsAng_, -angMax_, angMax_);
73  h_thetaZ_ = ZHisto.make<TH1F>("Ztheta", "Z #theta", nbinsAng_, 0, angMax_);
74  h_etaZ_ = ZHisto.make<TH1F>("ZEta", "Z #eta", nbinsAng_, -angMax_, angMax_);
75  h_rapidityZ_ = ZHisto.make<TH1F>("ZRapidity", "Z rapidity", nbinsAng_, -angMax_, angMax_);
76  h_invmMuMu_ = ZHisto.make<TH1F>("MuMuMass", "#mu #mu invariant mass", nbinsMass_, 0, massMax_);
77  h_weight_histo = ZHisto.make<TH1F>("weight_histo", "weight_histo", 20, -10, 10);
78  h_nZMC_ = ZMCHisto.make<TH1F>("ZMCNumber", "number of Z MC particles", 11, -0.5, 10.5);
79  h_mZMC_ = ZMCHisto.make<TH1F>("ZMCMass", "Z MC mass (GeV/c^{2})", nbinsMass_, 0, massMax_);
80  h_ptZMC_ = ZMCHisto.make<TH1F>("ZMCPt", "Z MC p_{t} (GeV/c)", nbinsPt_, 0, ptMax_);
81  h_phiZMC_ = ZMCHisto.make<TH1F>("ZMCPhi", "Z MC #phi", nbinsAng_, -angMax_, angMax_);
82  h_thetaZMC_ = ZMCHisto.make<TH1F>("ZMCTheta", "Z MC #theta", nbinsAng_, 0, angMax_);
83  h_etaZMC_ = ZMCHisto.make<TH1F>("ZMCEta", "Z MC #eta", nbinsAng_, -angMax_, angMax_);
84  h_rapidityZMC_ = ZMCHisto.make<TH1F>("ZMCRapidity", "Z MC rapidity", nbinsAng_, -angMax_, angMax_);
85  h_invmMuMuMC_ = ZMCHisto.make<TH1F>("MuMuMCMass", "#mu #mu MC invariant mass", nbinsMass_, 0, massMax_);
86  /*
87  h_mZ2vs3MC_ = Z2vs3MCHisto.make<TH1F>("Z2vs3MCMass", "Z MC st 2 vs st 3 mass (GeV/c^{2})",
88  nbinsMassRes_, -massResMax_, massResMax_);
89  h_ptZ2vs3MC_ = Z2vs3MCHisto.make<TH1F>("Z2vs3MCPt", "Z MC st 2 vs st 3 p_{t} (GeV/c)",
90  nbinsPt_, -ptMax_, ptMax_);
91  h_phiZ2vs3MC_ = Z2vs3MCHisto.make<TH1F>("Z2vs3MCPhi", "Z MC st 2 vs st 3 #phi",
92  nbinsAng_, -angMax_, angMax_);
93  h_thetaZ2vs3MC_ = Z2vs3MCHisto.make<TH1F>("Z2vs3MCTheta", "Z MC st 2 vs st 3 #theta",
94  nbinsAng_, -angMax_, angMax_);
95  h_etaZ2vs3MC_ = Z2vs3MCHisto.make<TH1F>("Z2vs3MCEta", "Z MC st 2 vs st 3 #eta",
96  nbinsAng_, -angMax_, angMax_);
97  h_rapidityZ2vs3MC_ = Z2vs3MCHisto.make<TH1F>("Z2vs3MCRapidity", "Z MC st 2 vs st 3 rapidity",
98  nbinsAng_, -angMax_, angMax_);
99  */
100  h_mResZ_ = ZResHisto.make<TH1F>(
101  "ZMassResolution", "Z mass Resolution (GeV/c^{2})", nbinsMassRes_, -massResMax_, massResMax_);
102  h_ptResZ_ = ZResHisto.make<TH1F>("ZPtResolution", "Z p_{t} Resolution (GeV/c)", nbinsPt_, -ptMax_, ptMax_);
103  h_phiResZ_ = ZResHisto.make<TH1F>("ZPhiResolution", "Z #phi Resolution", nbinsAng_, -angMax_, angMax_);
104  h_thetaResZ_ = ZResHisto.make<TH1F>("ZThetaResolution", "Z #theta Resolution", nbinsAng_, -angMax_, angMax_);
105  h_etaResZ_ = ZResHisto.make<TH1F>("ZEtaResolution", "Z #eta Resolution", nbinsAng_, -angMax_, angMax_);
106  h_rapidityResZ_ = ZResHisto.make<TH1F>("ZRapidityResolution", "Z rapidity Resolution", nbinsAng_, -angMax_, angMax_);
107  h_mResZMuMu_ = ZResHisto.make<TH1F>("ZToMuMuRecoMassResolution",
108  "Z Reco vs matched final state #mu #mu mass Difference (GeV/c^{2})",
110  -massResMax_,
111  massResMax_);
113  ZResHisto.make<TH1F>("ZToMuMuRecoMassRatio", "Z Reco vs matched final state #mu #mu mass Ratio", 4000, 0, 2);
114  h_mResZMuMuMC_ = ZResHisto.make<TH1F>("ZToMuMuMCMassResolution",
115  "Z vs final state #mu #mu MC mass Difference (GeV/c^{2})",
116  nbinsMassRes_ / 2 + 1,
117  -2 * massResMax_ / nbinsMassRes_,
118  massResMax_);
120  ZResHisto.make<TH1F>("ZToMuMuMCMassRatio", "Z vs final state #mu #mu MC mass Ratio", 2002, 0.999, 2);
121 }
122 
124  cout << ">>> Z Histogrammer analyze" << endl;
128  event.getByToken(zToken_, z);
129  event.getByToken(genToken_, gen);
130  event.getByToken(matchToken_, match);
131  h_nZ_->Fill(z->size());
132 
133  // get HepMC::GenEvent ...
134  Handle<HepMCProduct> evt_h;
135  event.getByToken(hepMCProductToken_, evt_h);
136  HepMC::GenEvent *evt = new HepMC::GenEvent(*(evt_h->GetEvent()));
137 
138  // get weight and fill it to histogram
139  HepMC::WeightContainer weights = evt->weights();
140  double weight = weights.front();
141  if (!weight)
142  weight = 1.;
143  h_weight_histo->Fill(weight);
144 
145  if (isMCatNLO_) {
146  weight > 0 ? weight = 1. : weight = -1.;
147  }
148 
149  for (unsigned int i = 0; i < z->size(); ++i) {
150  const Candidate &zCand = (*z)[i];
151  h_mZ_->Fill(zCand.mass(), weight);
152  h_ptZ_->Fill(zCand.pt(), weight);
153  h_phiZ_->Fill(zCand.phi(), weight);
154  h_thetaZ_->Fill(zCand.theta(), weight);
155  h_etaZ_->Fill(zCand.eta(), weight);
156  h_rapidityZ_->Fill(zCand.rapidity(), weight);
157  CandidateBaseRef zCandRef = z->refAt(i);
158 
159  GenParticleRef zMCMatch = (*match)[i];
160  if (zMCMatch.isNonnull() && zMCMatch->pdgId() == 23) {
161  h_mResZ_->Fill(zCandRef->mass() - zMCMatch->mass());
162  h_ptResZ_->Fill(zCandRef->pt() - zMCMatch->pt());
163  h_phiResZ_->Fill(zCandRef->phi() - zMCMatch->phi());
164  h_thetaResZ_->Fill(zCandRef->theta() - zMCMatch->theta());
165  h_etaResZ_->Fill(zCandRef->eta() - zMCMatch->eta());
166  h_rapidityResZ_->Fill(zCandRef->rapidity() - zMCMatch->rapidity());
167  const Candidate *dau0 = zMCMatch->daughter(0);
168  const Candidate *dau1 = zMCMatch->daughter(1);
169  for (unsigned int i0 = 0; i0 < dau0->numberOfDaughters(); ++i0) {
170  const Candidate *ddau0 = dau0->daughter(i0);
171  if (abs(ddau0->pdgId()) == 13 && ddau0->status() == 1) {
172  dau0 = ddau0;
173  break;
174  }
175  }
176  for (unsigned int i1 = 0; i1 < dau1->numberOfDaughters(); ++i1) {
177  const Candidate *ddau1 = dau1->daughter(i1);
178  if (abs(ddau1->pdgId()) == 13 && ddau1->status() == 1) {
179  dau1 = ddau1;
180  break;
181  }
182  }
183  assert(abs(dau0->pdgId()) == 13 && dau0->status() == 1);
184  assert(abs(dau1->pdgId()) == 13 && dau1->status() == 1);
185  double invMass = (dau0->p4() + dau1->p4()).mass();
186  h_invmMuMu_->Fill(invMass, weight);
187  h_mResZMuMu_->Fill(zCand.mass() - invMass);
188  h_mRatioZMuMu_->Fill(zCand.mass() / invMass);
189  }
190  }
191  h_nZMC_->Fill(gen->size());
192  for (unsigned int i = 0; i < gen->size(); ++i) {
193  const Candidate &genCand = (*gen)[i];
194  if ((genCand.pdgId() == 23) && (genCand.status() == 2)) //this is an intermediate Z0
195  cout << ">>> intermediate Z0 found, with " << genCand.numberOfDaughters() << " daughters" << endl;
196  if ((genCand.pdgId() == 23) && (genCand.status() == 3)) { //this is a Z0
197  cout << ">>> Z0 found, with " << genCand.numberOfDaughters() << " daughters" << endl;
198  h_mZMC_->Fill(genCand.mass(), weight);
199  h_ptZMC_->Fill(genCand.pt(), weight);
200  h_phiZMC_->Fill(genCand.phi(), weight);
201  h_thetaZMC_->Fill(genCand.theta(), weight);
202  h_etaZMC_->Fill(genCand.eta(), weight);
203  h_rapidityZMC_->Fill(genCand.rapidity(), weight);
204  Particle::LorentzVector pZ(0, 0, 0, 0);
205  int nMu = 0;
206  for (unsigned int j = 0; j < genCand.numberOfDaughters(); ++j) {
207  const Candidate *dauGen = genCand.daughter(j);
208  /*
209  if((dauGen->pdgId() == 23) && (dauGen->status() == 2)) {
210  h_mZ2vs3MC_->Fill(genCand.mass() - dauGen->mass());
211  h_ptZ2vs3MC_->Fill(genCand.pt() - dauGen->pt());
212  h_phiZ2vs3MC_->Fill(genCand.phi() - dauGen->phi());
213  h_thetaZ2vs3MC_->Fill(genCand.theta() - dauGen->theta());
214  h_etaZ2vs3MC_->Fill(genCand.eta() - dauGen->eta());
215  h_rapidityZ2vs3MC_->Fill(genCand.rapidity() - dauGen->rapidity());
216  }
217  */
218  if ((abs(dauGen->pdgId()) == 13) && (dauGen->numberOfDaughters() != 0)) {
219  //we are looking for photons of final state radiation
220  cout << ">>> The muon " << j << " has " << dauGen->numberOfDaughters() << " daughters" << endl;
221  for (unsigned int k = 0; k < dauGen->numberOfDaughters(); ++k) {
222  const Candidate *dauMuGen = dauGen->daughter(k);
223  cout << ">>> Mu " << j << " daughter MC " << k << " PDG Id " << dauMuGen->pdgId() << ", status "
224  << dauMuGen->status() << ", charge " << dauMuGen->charge() << endl;
225  if (abs(dauMuGen->pdgId()) == 13 && dauMuGen->status() == 1) {
226  pZ += dauMuGen->p4();
227  nMu++;
228  }
229  }
230  }
231  }
232  assert(nMu == 2);
233  double mZ = pZ.mass();
234  h_invmMuMuMC_->Fill(mZ, weight);
235  h_mResZMuMuMC_->Fill(genCand.mass() - mZ);
236  h_mRatioZMuMuMC_->Fill(genCand.mass() / mZ);
237  }
238  }
239 }
240 
242 
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:238
void analyze(const edm::Event &event, const edm::EventSetup &setup) override
ZMCHistogrammer(const edm::ParameterSet &pset)
virtual const Candidate * daughter(size_type i) const =0
return daughter at a given position, i = 0, ... numberOfDaughters() - 1 (read only mode) ...
size_type size() const
Definition: weight.py:1
edm::EDGetTokenT< std::vector< reco::GenParticleRef > > matchToken_
edm::EDGetTokenT< reco::CandidateView > zToken_
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
RefToBase< value_type > refAt(size_type i) const
virtual int status() const =0
status word
unsigned int nbinsAng_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
virtual const LorentzVector & p4() const =0
four-momentum Lorentz vector
virtual int pdgId() const =0
PDG identifier.
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
T * make(const Args &...args) const
make new ROOT object
def gen(fragment, howMuch)
Production test section ####.
unsigned int nbinsMass_
virtual double theta() const =0
momentum polar angle
virtual double eta() const =0
momentum pseudorapidity
TFileDirectory mkdir(const std::string &dir, const std::string &descr="")
create a new subdirectory
Definition: TFileService.h:69
const HepMC::GenEvent * GetEvent() const
Definition: HepMCProduct.h:34
virtual double pt() const =0
transverse momentum
virtual double mass() const =0
mass
edm::EDGetTokenT< reco::CandidateView > genToken_
virtual double rapidity() const =0
rapidity
virtual int charge() const =0
electric charge
fixed size matrix
HLT enums.
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.
Definition: Utils.h:10
virtual size_type numberOfDaughters() const =0
number of daughters
unsigned int nbinsPt_
virtual double phi() const =0
momentum azimuthal angle
edm::EDGetTokenT< edm::HepMCProduct > hepMCProductToken_
Definition: event.py:1
math::PtEtaPhiELorentzVectorF LorentzVector
unsigned int nbinsMassRes_