CMS 3D CMS Logo

EWKSystUnc.cc
Go to the documentation of this file.
5 #include "TH1.h"
6 
7 class EWKSystUnc : public edm::EDAnalyzer {
8 public:
10 
11 private:
12  void analyze(const edm::Event& event, const edm::EventSetup& setup) override;
13  void endJob() override;
16  unsigned int nbinsMass_, nbinsPt_, nbinsAng_;
19  TH1F* h_nZ_;
23  bool isMCatNLO_;
26 };
27 
32 
33 #include "HepMC/WeightContainer.h"
34 #include "HepMC/GenEvent.h"
40 #include <cmath>
41 #include <iostream>
42 #include <fstream>
43 
44 using namespace std;
45 using namespace reco;
46 using namespace edm;
47 
49  : genToken_(consumes<GenParticleCollection>(pset.getParameter<InputTag>("genParticles"))),
50  weightsToken_(consumes<double>(pset.getParameter<InputTag>("weights"))),
51  nbinsMass_(pset.getUntrackedParameter<unsigned int>("nbinsMass")),
52  nbinsPt_(pset.getUntrackedParameter<unsigned int>("nbinsPt")),
53  nbinsAng_(pset.getUntrackedParameter<unsigned int>("nbinsAng")),
54  massMax_(pset.getUntrackedParameter<double>("massMax")),
55  ptMax_(pset.getUntrackedParameter<double>("ptMax")),
56  angMax_(pset.getUntrackedParameter<double>("angMax")),
57  accPtMin_(pset.getUntrackedParameter<double>("accPtMin")),
58  accMassMin_(pset.getUntrackedParameter<double>("accMassMin")),
59  accMassMax_(pset.getUntrackedParameter<double>("accMassMax")),
60  accEtaMin_(pset.getUntrackedParameter<double>("accEtaMin")),
61  accEtaMax_(pset.getUntrackedParameter<double>("accEtaMax")),
62  isMCatNLO_(pset.getUntrackedParameter<bool>("isMCatNLO")),
63  filename_(pset.getUntrackedParameter<std::string>("outfilename")) {
64  cout << ">>> Z Histogrammer constructor" << endl;
66 
67  TFileDirectory ZMCHisto = fs->mkdir("ZMCHisto");
68  h_nZ_ = ZMCHisto.make<TH1F>("ZNumber", "number of Z particles", 11, -0.5, 10.5);
69  h_weight_histo = ZMCHisto.make<TH1F>("weight_histo", "weight_histo", 20, -10, 10);
70 
71  h_mZMC_ = ZMCHisto.make<TH1F>("ZMCMass", "Z MC mass (GeV/c^{2})", nbinsMass_, 0, massMax_);
72  h_ptZMC_ = ZMCHisto.make<TH1F>("ZMCPt", "Z MC p_{t} (GeV/c)", nbinsPt_, 0, ptMax_);
73  hardpt = ZMCHisto.make<TH1F>("hardpt", "hard muon p_{t} (GeV/c)", nbinsPt_, 0, ptMax_);
74  softpt = ZMCHisto.make<TH1F>("softpt", "soft muon p_{t} (GeV/c)", nbinsPt_, 0, ptMax_);
75 
76  h_phiZMC_ = ZMCHisto.make<TH1F>("ZMCPhi", "Z MC #phi", nbinsAng_, -angMax_, angMax_);
77  h_thetaZMC_ = ZMCHisto.make<TH1F>("ZMCTheta", "Z MC #theta", nbinsAng_, 0, angMax_);
78  h_etaZMC_ = ZMCHisto.make<TH1F>("ZMCEta", "Z MC #eta", nbinsAng_, -angMax_, angMax_);
79  h_rapidityZMC_ = ZMCHisto.make<TH1F>("ZMCRapidity", "Z MC y", nbinsAng_, -angMax_, angMax_);
80 
81  hardeta = ZMCHisto.make<TH1F>("hard muon eta", "hard muon #eta", nbinsAng_, -angMax_, angMax_);
82  softeta = ZMCHisto.make<TH1F>("soft muon eta", "soft muon #eta", nbinsAng_, -angMax_, angMax_);
83  nAcc_ = 0.;
84  nAccReW_ = 0;
86 }
87 
89  cout << ">>> Z Histogrammer analyze" << endl;
90 
93 
94  event.getByToken(genToken_, gen);
95  event.getByToken(weightsToken_, weights);
96 
97  // get weight and fill it to histogram
98  double weight = (*weights);
99 
100  // protection...
101  if (weight > 2. || weight < 0.1) {
102  std::cout << "weight = " << weight << ", something strange...." << std::endl;
103  weight = 1;
104  }
105  h_weight_histo->Fill(weight);
106 
107  std::vector<GenParticle> muons;
108 
109  for (unsigned int i = 0; i < gen->size(); ++i) {
110  const GenParticle& muMC = (*gen)[i];
111  // filling only muons coming form Z
112  if (abs(muMC.pdgId()) == 13 && muMC.status() == 1 && muMC.numberOfMothers() > 0) {
113  if (muMC.mother()->numberOfMothers() > 0) {
114  cout << "I'm getting a muon \n"
115  << "with "
116  << "muMC.numberOfMothers() " << muMC.numberOfMothers() << "\n the first mother has pdgId "
117  << muMC.mother()->pdgId() << "with "
118  << "muMC.mother()->numberOfMothers() " << muMC.mother()->numberOfMothers()
119  << "\n the first grandma has pdgId " << muMC.mother()->mother()->pdgId() << endl;
120  if (muMC.mother()->mother()->pdgId() == 23)
121  muons.push_back(muMC);
122  }
123  }
124  }
125 
126  cout << "finally I selected " << muons.size() << " muons" << endl;
127 
128  // if there are at least two muons,
129  // calculate invarant mass of first two and fill it into histogram
130 
131  double inv_mass = 0.0;
132  double Zpt_ = 0.0;
133  double Zeta_ = 0.0;
134  double Ztheta_ = 0.0;
135  double Zphi_ = 0.0;
136  double Zrapidity_ = 0.0;
137 
138  if (muons.size() > 1) {
139  if (muons[0].mother()->mother()->pdgId() == 23 && muons[1].mother()->mother()->pdgId() == 23)
141  math::XYZTLorentzVector tot_momentum(muons[0].p4());
142  math::XYZTLorentzVector mom2(muons[1].p4());
143  tot_momentum += mom2;
144  inv_mass = sqrt(tot_momentum.mass2());
145  Zpt_ = tot_momentum.pt();
146  Zeta_ = tot_momentum.eta();
147  Ztheta_ = tot_momentum.theta();
148  Zphi_ = tot_momentum.phi();
149  Zrapidity_ = tot_momentum.Rapidity();
150 
151  // IMPORTANT: use the weight of the event ...
152 
153  double weight_sign = weight;
154  //double weight_sign = 1. ;
155  h_mZMC_->Fill(inv_mass, weight_sign);
156  h_ptZMC_->Fill(Zpt_, weight_sign);
157  h_etaZMC_->Fill(Zeta_, weight_sign);
158  h_thetaZMC_->Fill(Ztheta_, weight_sign);
159  h_phiZMC_->Fill(Zphi_, weight_sign);
160  h_rapidityZMC_->Fill(Zrapidity_, weight_sign);
161 
162  double pt1 = muons[0].pt();
163  double pt2 = muons[1].pt();
164  double eta1 = muons[0].eta();
165  double eta2 = muons[1].eta();
166 
167  if (pt1 > pt2) {
168  hardpt->Fill(pt1, weight_sign);
169  softpt->Fill(pt2, weight_sign);
170  hardeta->Fill(eta1, weight_sign);
171  softeta->Fill(eta2, weight_sign);
172  } else {
173  hardpt->Fill(pt2, weight_sign);
174  softpt->Fill(pt1, weight_sign);
175  hardeta->Fill(eta2, weight_sign);
176  softeta->Fill(eta1, weight_sign);
177  }
178 
179  //evaluating the geometric acceptance
180  if (pt1 >= accPtMin_ && pt2 >= accPtMin_ && fabs(eta1) >= accEtaMin_ && fabs(eta2) >= accEtaMin_ &&
181  fabs(eta1) <= accEtaMax_ && fabs(eta2) <= accEtaMax_ && inv_mass >= accMassMin_ && inv_mass <= accMassMax_) {
182  nAcc_++;
183  nAccReW_ += weight;
184  }
185  }
186 }
187 
189  cout << " number of events accepted :" << nAcc_ << endl;
190  cout << " number of events accepted reweigthed :" << nAccReW_ << endl;
191  double nev = h_mZMC_->GetEntries();
192  double nev_weigthed = h_mZMC_->Integral(0, nbinsMass_ + 1);
193  cout << " number of total events :" << nev << endl;
194  cout << " number of total weighted events :" << nev_weigthed << endl;
195  cout << " number of cases in which BothMuHasZHasGrandMa :" << nBothMuHasZHasGrandMa_ << endl;
196  double eff = (double)nAcc_ / (double)h_mZMC_->GetEntries();
197  double eff_rew = (double)nAccReW_ / (double)h_mZMC_->Integral(0, nbinsMass_ + 1);
198  double err = sqrt(eff * (1. - eff) / (double)h_mZMC_->GetEntries());
199  double err_rew = sqrt(eff_rew * (1. - eff_rew) / (double)h_mZMC_->Integral(0, nbinsMass_ + 1));
200  cout << " geometric acceptance: " << eff << "+/-" << err << endl;
201  cout << " geometric acceptance reweighted: " << eff_rew << "+/-" << err_rew << endl;
202 
203  ofstream myfile;
204  myfile.open(filename_.c_str(), std::ios::app);
205  myfile << eff << " " << eff_rew << " " << nev << " " << nev_weigthed << endl;
206  myfile.close();
207 }
209 
std::string filename_
Definition: EWKSystUnc.cc:25
TH1F * softpt
Definition: EWKSystUnc.cc:21
std::vector< GenParticle > GenParticleCollection
collection of GenParticles
int pdgId() const final
PDG identifier.
edm::EDGetTokenT< double > weightsToken_
Definition: EWKSystUnc.cc:15
double nBothMuHasZHasGrandMa_
Definition: EWKSystUnc.cc:24
TH1F * h_ptZMC_
Definition: EWKSystUnc.cc:20
double accMassMin_
Definition: EWKSystUnc.cc:18
bool isMCatNLO_
Definition: EWKSystUnc.cc:23
TH1F * h_phiZMC_
Definition: EWKSystUnc.cc:20
double accMassMax_
Definition: EWKSystUnc.cc:18
edm::EDGetTokenT< reco::GenParticleCollection > genToken_
Definition: EWKSystUnc.cc:14
size_t numberOfMothers() const override
number of mothers
double accEtaMin_
Definition: EWKSystUnc.cc:18
TH1F * hardeta
Definition: EWKSystUnc.cc:21
Definition: weight.py:1
virtual const Candidate * mother(size_type i=0) const =0
return pointer to mother
TH1F * hardpt
Definition: EWKSystUnc.cc:21
double nAccReW_
Definition: EWKSystUnc.cc:24
TH1F * h_rapidityZMC_
Definition: EWKSystUnc.cc:20
TH1F * h_mZMC_
Definition: EWKSystUnc.cc:20
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
XYZTLorentzVectorD XYZTLorentzVector
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:29
unsigned int nbinsMass_
Definition: EWKSystUnc.cc:16
double accPtMin_
Definition: EWKSystUnc.cc:18
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
TH1F * softeta
Definition: EWKSystUnc.cc:21
virtual int pdgId() const =0
PDG identifier.
T sqrt(T t)
Definition: SSEVec.h:19
double p4[4]
Definition: TauolaWrapper.h:92
TH1F * h_weight_histo
Definition: EWKSystUnc.cc:22
void endJob() override
Definition: EWKSystUnc.cc:188
virtual size_type numberOfMothers() const =0
number of mothers (zero or one in most of but not all the cases)
unsigned int nbinsPt_
Definition: EWKSystUnc.cc:16
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
T * make(const Args &...args) const
make new ROOT object
double angMax_
Definition: EWKSystUnc.cc:17
def gen(fragment, howMuch)
Production test section ####.
double accEtaMax_
Definition: EWKSystUnc.cc:18
TFileDirectory mkdir(const std::string &dir, const std::string &descr="")
create a new subdirectory
Definition: TFileService.h:69
double nAcc_
Definition: EWKSystUnc.cc:24
void analyze(const edm::Event &event, const edm::EventSetup &setup) override
Definition: EWKSystUnc.cc:88
fixed size matrix
HLT enums.
TH1F * h_thetaZMC_
Definition: EWKSystUnc.cc:20
int status() const final
status word
double ptMax_
Definition: EWKSystUnc.cc:17
TH1F * h_nZ_
Definition: EWKSystUnc.cc:19
double massMax_
Definition: EWKSystUnc.cc:17
unsigned int nbinsAng_
Definition: EWKSystUnc.cc:16
EWKSystUnc(const edm::ParameterSet &pset)
Definition: EWKSystUnc.cc:48
TH1F * h_etaZMC_
Definition: EWKSystUnc.cc:20
const Candidate * mother(size_type=0) const override
return mother at a given position, i = 0, ... numberOfMothers() - 1 (read only mode) ...
Definition: event.py:1