CMS 3D CMS Logo

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