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 private:
11  virtual void analyze(const edm::Event& event, const edm::EventSetup& setup) override;
12  virtual void endJob() override;
15  unsigned int nbinsMass_, nbinsPt_, nbinsAng_ ;
18  TH1F *h_nZ_;
20  TH1F *hardpt, *softpt, * hardeta, *softeta;
22  bool isMCatNLO_;
25 
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 
77  h_phiZMC_ = ZMCHisto.make<TH1F>("ZMCPhi", "Z MC #phi", nbinsAng_, -angMax_, angMax_);
78  h_thetaZMC_ = ZMCHisto.make<TH1F>("ZMCTheta", "Z MC #theta", nbinsAng_, 0, angMax_);
79  h_etaZMC_ = ZMCHisto.make<TH1F>("ZMCEta", "Z MC #eta", nbinsAng_, -angMax_, angMax_);
80  h_rapidityZMC_ = ZMCHisto.make<TH1F>("ZMCRapidity", "Z MC y", nbinsAng_, -angMax_, angMax_);
81 
82  hardeta = ZMCHisto.make<TH1F>("hard muon eta", "hard muon #eta", nbinsAng_, -angMax_, angMax_);
83  softeta = ZMCHisto.make<TH1F>("soft muon eta", "soft muon #eta", nbinsAng_, -angMax_, angMax_);
84  nAcc_=0.;
85  nAccReW_ =0;
87 
88 }
89 
91  cout << ">>> Z Histogrammer analyze" << endl;
92 
95 
96  event.getByToken(genToken_, gen);
97  event.getByToken(weightsToken_, weights );
98 
99 
100 
101  // get weight and fill it to histogram
102  double weight = (*weights);
103 
104  // protection...
105 if (weight> 2. || weight < 0.1) {
106 
107  std::cout << "weight = " << weight << ", something strange...." << std::endl;
108 weight =1;
109 }
110  h_weight_histo->Fill(weight);
111 
112  std::vector<GenParticle> muons;
113 
114 
115  for(unsigned int i = 0; i < gen->size(); ++i){
116  const GenParticle & muMC = (*gen)[i];
117  // filling only muons coming form Z
118  if (abs(muMC.pdgId())==13 && muMC.status()==1 && muMC.numberOfMothers()>0) {
119  if (muMC.mother()->numberOfMothers()> 0 ){
120  cout << "I'm getting a muon \n"
121  << "with " << "muMC.numberOfMothers() " << muMC.numberOfMothers() << "\n the first mother has pdgId " << muMC.mother()->pdgId()
122  << "with " << "muMC.mother()->numberOfMothers() " << muMC.mother()->numberOfMothers()<< "\n the first grandma has pdgId " << muMC.mother()->mother()->pdgId()<<endl;
123  if (muMC.mother()->mother()->pdgId() ==23 ) muons.push_back(muMC);
124  }
125  }
126  }
127 
128  cout << "finally I selected " << muons.size() << " muons" << endl;
129 
130 
131 
132 
133 
134 // if there are at least two muons,
135  // calculate invarant mass of first two and fill it into histogram
136 
137 
138 
139 
140 
141 
142  double inv_mass = 0.0;
143  double Zpt_ = 0.0;
144  double Zeta_ = 0.0;
145  double Ztheta_ = 0.0;
146  double Zphi_ = 0.0;
147  double Zrapidity_ = 0.0;
148 
149  if(muons.size()>1) {
150  if (muons[0].mother()->mother()->pdgId()==23 && muons[1].mother()->mother()->pdgId()==23) nBothMuHasZHasGrandMa_ ++;
151  math::XYZTLorentzVector tot_momentum(muons[0].p4());
152  math::XYZTLorentzVector mom2(muons[1].p4());
153  tot_momentum += mom2;
154  inv_mass = sqrt(tot_momentum.mass2());
155  Zpt_=tot_momentum.pt();
156  Zeta_ = tot_momentum.eta();
157  Ztheta_ = tot_momentum.theta();
158  Zphi_ = tot_momentum.phi();
159  Zrapidity_ = tot_momentum.Rapidity();
160 
161 
162 
163  // IMPORTANT: use the weight of the event ...
164 
165  double weight_sign = weight;
166  //double weight_sign = 1. ;
167  h_mZMC_->Fill(inv_mass,weight_sign);
168  h_ptZMC_->Fill(Zpt_,weight_sign);
169  h_etaZMC_->Fill(Zeta_,weight_sign);
170  h_thetaZMC_->Fill(Ztheta_,weight_sign);
171  h_phiZMC_->Fill(Zphi_,weight_sign);
172  h_rapidityZMC_-> Fill (Zrapidity_,weight_sign );
173 
174  double pt1 = muons[0].pt();
175  double pt2 = muons[1].pt();
176  double eta1 = muons[0].eta();
177  double eta2 = muons[1].eta();
178 
179 
180 
181  if(pt1>pt2) {
182  hardpt->Fill(pt1,weight_sign);
183  softpt->Fill(pt2,weight_sign);
184  hardeta->Fill(eta1,weight_sign);
185  softeta->Fill(eta2,weight_sign);
186  } else {
187  hardpt->Fill(pt2,weight_sign);
188  softpt->Fill(pt1,weight_sign);
189  hardeta->Fill(eta2,weight_sign);
190  softeta->Fill(eta1,weight_sign);
191  }
192 
193 
194  //evaluating the geometric acceptance
195  if ( pt1 >= accPtMin_ && pt2 >= accPtMin_ && fabs(eta1)>= accEtaMin_ && fabs(eta2) >= accEtaMin_ && fabs(eta1)<= accEtaMax_ && fabs(eta2) <= accEtaMax_ && inv_mass>= accMassMin_ && inv_mass<= accMassMax_) {
196 nAcc_ ++;
197 nAccReW_ +=weight;
198  }
199 
200  }
201 
202 }
203 
204 
206  cout << " number of events accepted :" << nAcc_ << endl;
207  cout << " number of events accepted reweigthed :" << nAccReW_ << endl;
208  double nev = h_mZMC_->GetEntries();
209  double nev_weigthed = h_mZMC_->Integral(0,nbinsMass_ +1 ) ;
210  cout << " number of total events :" << nev << endl;
211  cout << " number of total weighted events :" << nev_weigthed << endl;
212  cout << " number of cases in which BothMuHasZHasGrandMa :" << nBothMuHasZHasGrandMa_ << endl;
213  double eff = (double)nAcc_ / (double) h_mZMC_->GetEntries();
214  double eff_rew = (double)nAccReW_ / (double) h_mZMC_->Integral(0,nbinsMass_ +1 );
215  double err = sqrt( eff * (1. - eff) / (double) h_mZMC_->GetEntries() );
216  double err_rew = sqrt( eff_rew * (1. - eff_rew) / (double) h_mZMC_->Integral(0,nbinsMass_ +1 ) );
217  cout << " geometric acceptance: " << eff << "+/-" << err << endl;
218  cout << " geometric acceptance reweighted: " << eff_rew << "+/-" << err_rew << endl;
219 
220  ofstream myfile;
221  myfile.open(filename_.c_str(), std::ios::app);
222  myfile<< eff << " "<< eff_rew << " " << nev << " " << nev_weigthed << endl ;
223  myfile.close();
224 }
226 
228 
std::string filename_
Definition: EWKSystUnc.cc:24
TH1F * softpt
Definition: EWKSystUnc.cc:20
std::vector< GenParticle > GenParticleCollection
collection of GenParticles
edm::EDGetTokenT< double > weightsToken_
Definition: EWKSystUnc.cc:14
double nBothMuHasZHasGrandMa_
Definition: EWKSystUnc.cc:23
TH1F * h_ptZMC_
Definition: EWKSystUnc.cc:19
double accMassMin_
Definition: EWKSystUnc.cc:17
bool isMCatNLO_
Definition: EWKSystUnc.cc:22
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
TH1F * h_phiZMC_
Definition: EWKSystUnc.cc:19
double accMassMax_
Definition: EWKSystUnc.cc:17
edm::EDGetTokenT< reco::GenParticleCollection > genToken_
Definition: EWKSystUnc.cc:13
double accEtaMin_
Definition: EWKSystUnc.cc:17
TH1F * hardeta
Definition: EWKSystUnc.cc:20
def setup(process, global_tag, zero_tesla=False)
Definition: GeneralSetup.py:1
Definition: weight.py:1
virtual const Candidate * mother(size_type i=0) const =0
return pointer to mother
virtual int status() const final
status word
TH1F * hardpt
Definition: EWKSystUnc.cc:20
double nAccReW_
Definition: EWKSystUnc.cc:23
TH1F * h_rapidityZMC_
Definition: EWKSystUnc.cc:19
TH1F * h_mZMC_
Definition: EWKSystUnc.cc:19
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:15
double accPtMin_
Definition: EWKSystUnc.cc:17
TH1F * softeta
Definition: EWKSystUnc.cc:20
void Fill(HcalDetId &id, double val, std::vector< TH2F > &depth)
virtual size_t numberOfMothers() const
number of mothers
virtual int pdgId() const =0
PDG identifier.
T sqrt(T t)
Definition: SSEVec.h:18
double p4[4]
Definition: TauolaWrapper.h:92
TH1F * h_weight_histo
Definition: EWKSystUnc.cc:21
virtual void endJob() override
Definition: EWKSystUnc.cc:205
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:15
virtual int pdgId() const final
PDG identifier.
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:16
def gen(fragment, howMuch)
Production test section ####.
double accEtaMax_
Definition: EWKSystUnc.cc:17
TFileDirectory mkdir(const std::string &dir, const std::string &descr="")
create a new subdirectory
Definition: TFileService.h:69
double nAcc_
Definition: EWKSystUnc.cc:23
virtual void analyze(const edm::Event &event, const edm::EventSetup &setup) override
Definition: EWKSystUnc.cc:90
fixed size matrix
HLT enums.
TH1F * h_thetaZMC_
Definition: EWKSystUnc.cc:19
double ptMax_
Definition: EWKSystUnc.cc:16
TH1F * h_nZ_
Definition: EWKSystUnc.cc:18
double massMax_
Definition: EWKSystUnc.cc:16
virtual const Candidate * mother(size_type=0) const
return mother at a given position, i = 0, ... numberOfMothers() - 1 (read only mode) ...
unsigned int nbinsAng_
Definition: EWKSystUnc.cc:15
EWKSystUnc(const edm::ParameterSet &pset)
Definition: EWKSystUnc.cc:48
TH1F * h_etaZMC_
Definition: EWKSystUnc.cc:19
Definition: event.py:1