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 
PDWG_BPHSkim_cff.muons
muons
Definition: PDWG_BPHSkim_cff.py:47
EWKSystUnc::endJob
void endJob() override
Definition: EWKSystUnc.cc:188
Handle.h
electrons_cff.bool
bool
Definition: electrons_cff.py:393
mps_fire.i
i
Definition: mps_fire.py:428
reco::LeafCandidate::status
int status() const final
status word
Definition: LeafCandidate.h:180
EWKSystUnc::EWKSystUnc
EWKSystUnc(const edm::ParameterSet &pset)
Definition: EWKSystUnc.cc:48
edm::EDGetTokenT< reco::GenParticleCollection >
reco::CompositeRefCandidateT::mother
const Candidate * mother(size_type=0) const override
return mother at a given position, i = 0, ... numberOfMothers() - 1 (read only mode)
TFileDirectory::make
T * make(const Args &... args) const
make new ROOT object
Definition: TFileDirectory.h:53
edm
HLT enums.
Definition: AlignableModifier.h:19
EWKSystUnc::accPtMin_
double accPtMin_
Definition: EWKSystUnc.cc:18
mps_merge.weight
weight
Definition: mps_merge.py:88
gather_cfg.cout
cout
Definition: gather_cfg.py:144
reco::GenParticleCollection
std::vector< GenParticle > GenParticleCollection
collection of GenParticles
Definition: GenParticleFwd.h:13
EWKSystUnc::hardpt
TH1F * hardpt
Definition: EWKSystUnc.cc:21
EWKSystUnc::nAcc_
double nAcc_
Definition: EWKSystUnc.cc:24
reco::CompositeRefCandidateT::numberOfMothers
size_t numberOfMothers() const override
number of mothers
EDAnalyzer.h
TFileDirectory
Definition: TFileDirectory.h:24
reco
fixed size matrix
Definition: AlignmentAlgorithmBase.h:45
EWKSystUnc::h_weight_histo
TH1F * h_weight_histo
Definition: EWKSystUnc.cc:22
reco::Candidate::mother
virtual const Candidate * mother(size_type i=0) const =0
return pointer to mother
EWKSystUnc::h_ptZMC_
TH1F * h_ptZMC_
Definition: EWKSystUnc.cc:20
edm::Handle
Definition: AssociativeIterator.h:50
EWKSystUnc::isMCatNLO_
bool isMCatNLO_
Definition: EWKSystUnc.cc:23
GenParticle
Definition: GenParticle.py:1
singleTopDQM_cfi.setup
setup
Definition: singleTopDQM_cfi.py:37
EWKSystUnc::nbinsAng_
unsigned int nbinsAng_
Definition: EWKSystUnc.cc:16
CandMatchMap.h
edm::EDAnalyzer
Definition: EDAnalyzer.h:28
EWKSystUnc::accEtaMax_
double accEtaMax_
Definition: EWKSystUnc.cc:18
GenParticle.h
CandidateFwd.h
EWKSystUnc::hardeta
TH1F * hardeta
Definition: EWKSystUnc.cc:21
MakerMacros.h
reco::Candidate::numberOfMothers
virtual size_type numberOfMothers() const =0
number of mothers (zero or one in most of but not all the cases)
EWKSystUnc::accEtaMin_
double accEtaMin_
Definition: EWKSystUnc.cc:18
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
EWKSystUnc::h_mZMC_
TH1F * h_mZMC_
Definition: EWKSystUnc.cc:20
Service.h
EWKSystUnc::nBothMuHasZHasGrandMa_
double nBothMuHasZHasGrandMa_
Definition: EWKSystUnc.cc:24
EWKSystUnc::h_etaZMC_
TH1F * h_etaZMC_
Definition: EWKSystUnc.cc:20
HLT_FULL_cff.weights
weights
Definition: HLT_FULL_cff.py:99178
mathSSE::sqrt
T sqrt(T t)
Definition: SSEVec.h:19
HLT_FULL_cff.eta2
eta2
Definition: HLT_FULL_cff.py:9542
GenParticleFwd.h
EWKSystUnc::accMassMin_
double accMassMin_
Definition: EWKSystUnc.cc:18
HLT_FULL_cff.pt1
pt1
Definition: HLT_FULL_cff.py:9870
gen
Definition: PythiaDecays.h:13
EWKSystUnc::softeta
TH1F * softeta
Definition: EWKSystUnc.cc:21
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
TFileService.h
HLT_FULL_cff.eta1
eta1
Definition: HLT_FULL_cff.py:9541
TFileService::mkdir
TFileDirectory mkdir(const std::string &dir, const std::string &descr="")
create a new subdirectory
Definition: TFileService.h:69
edm::ParameterSet
Definition: ParameterSet.h:47
EWKSystUnc::massMax_
double massMax_
Definition: EWKSystUnc.cc:17
Event.h
reco::LeafCandidate::pdgId
int pdgId() const final
PDG identifier.
Definition: LeafCandidate.h:176
EWKSystUnc::ptMax_
double ptMax_
Definition: EWKSystUnc.cc:17
edm::Service< TFileService >
createfilelist.int
int
Definition: createfilelist.py:10
EWKSystUnc::h_rapidityZMC_
TH1F * h_rapidityZMC_
Definition: EWKSystUnc.cc:20
p4
double p4[4]
Definition: TauolaWrapper.h:92
edm::EventSetup
Definition: EventSetup.h:57
EWKSystUnc::h_nZ_
TH1F * h_nZ_
Definition: EWKSystUnc.cc:19
EWKSystUnc::softpt
TH1F * softpt
Definition: EWKSystUnc.cc:21
HLT_FULL_cff.pt2
pt2
Definition: HLT_FULL_cff.py:9872
submitPVResolutionJobs.err
err
Definition: submitPVResolutionJobs.py:85
EWKSystUnc::nbinsMass_
unsigned int nbinsMass_
Definition: EWKSystUnc.cc:16
reco::Candidate::pdgId
virtual int pdgId() const =0
PDG identifier.
InputTag.h
EWKSystUnc::accMassMax_
double accMassMax_
Definition: EWKSystUnc.cc:18
EWKSystUnc::h_phiZMC_
TH1F * h_phiZMC_
Definition: EWKSystUnc.cc:20
EWKSystUnc::nAccReW_
double nAccReW_
Definition: EWKSystUnc.cc:24
std
Definition: JetResolutionObject.h:76
math::XYZTLorentzVector
XYZTLorentzVectorD XYZTLorentzVector
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:29
EWKSystUnc::angMax_
double angMax_
Definition: EWKSystUnc.cc:17
relval_steps.gen
def gen(fragment, howMuch)
Production test section ####.
Definition: relval_steps.py:509
Candidate.h
EWKSystUnc::analyze
void analyze(const edm::Event &event, const edm::EventSetup &setup) override
Definition: EWKSystUnc.cc:88
funct::abs
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
EWKSystUnc::filename_
std::string filename_
Definition: EWKSystUnc.cc:25
ParameterSet.h
HepMCProduct.h
EWKSystUnc::genToken_
edm::EDGetTokenT< reco::GenParticleCollection > genToken_
Definition: EWKSystUnc.cc:14
event
Definition: event.py:1
edm::Event
Definition: Event.h:73
EWKSystUnc::nbinsPt_
unsigned int nbinsPt_
Definition: EWKSystUnc.cc:16
edm::InputTag
Definition: InputTag.h:15
EWKSystUnc::h_thetaZMC_
TH1F * h_thetaZMC_
Definition: EWKSystUnc.cc:20
weight
Definition: weight.py:1
EWKSystUnc::weightsToken_
edm::EDGetTokenT< double > weightsToken_
Definition: EWKSystUnc.cc:15
EWKSystUnc
Definition: EWKSystUnc.cc:7
muonDTDigis_cfi.pset
pset
Definition: muonDTDigis_cfi.py:27