CMS 3D CMS Logo

ZMuPtScaleAnalyzer.cc
Go to the documentation of this file.
5 #include "TH1.h"
6 #include "TRandom3.h"
7 
8 
10 public:
12 private:
13  void analyze(const edm::Event& event, const edm::EventSetup& setup) override;
14  void endJob() override;
16  unsigned int nbinsMass_, nbinsPt_, nbinsAng_;
21  TH1F *hardpt, *softpt, * hardeta, *softeta;
24 };
25 
30 
31 #include "HepMC/WeightContainer.h"
32 #include "HepMC/GenEvent.h"
38 #include <cmath>
39 #include <iostream>
40 
41 using namespace std;
42 using namespace reco;
43 using namespace edm;
44 
46  genToken_(consumes<GenParticleCollection>(pset.getParameter<InputTag>("genParticles"))),
47  nbinsMass_(pset.getUntrackedParameter<unsigned int>("nbinsMass")),
48  nbinsPt_(pset.getUntrackedParameter<unsigned int>("nbinsPt")),
49  nbinsAng_(pset.getUntrackedParameter<unsigned int>("nbinsAng")),
50  massMax_(pset.getUntrackedParameter<double>("massMax")),
51  ptMax_(pset.getUntrackedParameter<double>("ptMax")),
52  angMax_(pset.getUntrackedParameter<double>("angMax")),
53  accPtMin_(pset.getUntrackedParameter<double>("accPtMin")),
54  accMassMin_(pset.getUntrackedParameter<double>("accMassMin")),
55  accMassMax_(pset.getUntrackedParameter<double>("accMassMax")),
56  accMassMinDen_(pset.getUntrackedParameter<double>("accMassMinDen")),
57  accMassMaxDen_(pset.getUntrackedParameter<double>("accMassMaxDen")),
58  accEtaMin_(pset.getUntrackedParameter<double>("accEtaMin")),
59  accEtaMax_(pset.getUntrackedParameter<double>("accEtaMax")),
60  ptScale_(pset.getUntrackedParameter<double>("ptScale")),
61  muPdgStatus_(pset.getUntrackedParameter<int>("muPdgStatus")){
62 
63  cout << ">>> Z Histogrammer constructor" << endl;
65  TFileDirectory ZMCHisto = fs->mkdir( "ZMCHisto" );
66 
67  h_mZMC_ = ZMCHisto.make<TH1F>("ZMCMass", "Z MC mass (GeV/c^{2})", nbinsMass_, 0, massMax_);
68  h_ptZMC_ = ZMCHisto.make<TH1F>("ZMCPt", "Z MC p_{t} (GeV/c)", nbinsPt_, 0, ptMax_);
69  hardpt = ZMCHisto.make<TH1F>("hardpt", "hard muon p_{t} (GeV/c)", nbinsPt_, 0, ptMax_);
70  softpt = ZMCHisto.make<TH1F>("softpt", "soft muon p_{t} (GeV/c)", nbinsPt_, 0, ptMax_);
71 
72 
73  h_phiZMC_ = ZMCHisto.make<TH1F>("ZMCPhi", "Z MC #phi", nbinsAng_, -angMax_, angMax_);
74  h_thetaZMC_ = ZMCHisto.make<TH1F>("ZMCTheta", "Z MC #theta", nbinsAng_, 0, angMax_);
75  h_etaZMC_ = ZMCHisto.make<TH1F>("ZMCEta", "Z MC #eta", nbinsAng_, -angMax_, angMax_);
76  h_rapidityZMC_ = ZMCHisto.make<TH1F>("ZMCRapidity", "Z MC y", nbinsAng_, -angMax_, angMax_);
77 
78  hardeta = ZMCHisto.make<TH1F>("hard muon eta", "hard muon #eta", nbinsAng_, -angMax_, angMax_);
79  softeta = ZMCHisto.make<TH1F>("soft muon eta", "soft muon #eta", nbinsAng_, -angMax_, angMax_);
80  nAcc_=0;
81  nAccPtScaleP_=0;
82  nAccPtScaleN_=0;
86 
87 
88 
90  cout << ">>> Z HistogrammerZLONLOHistogrammer.cc analyze" << endl;
91 
94 
95  event.getByToken(genToken_, gen);
96 
97 
98 
99 
100  // get weight and fill it to histogram
101 
102 
103 
104 
105  std::vector<GenParticle> muons;
106 
107  double mZGen = -100;
108 
109  for(unsigned int i = 0; i < gen->size(); ++i){
110  const GenParticle & muMC = (*gen)[i];
111  // filliScaledPng only muons coming form Z
112  if (abs(muMC.pdgId())==13 && muMC.status()==muPdgStatus_ && muMC.numberOfMothers()>0) {
113 
114  cout << "I'm getting a muon \n"
115  << "with " << "muMC.numberOfMothers() " << muMC.numberOfMothers() << "\n the first mother has pdgId " << muMC.mother()->pdgId()
116  << "with " << "muMC.mother()->numberOfMothers() " << muMC.mother()->numberOfMothers()<< "\n the first grandma has pdgId " << muMC.mother()->mother()->pdgId()<<endl;
117  cout << "with muMC.eta() " << muMC.eta()<<endl;
118  muons.push_back(muMC);
119  }
120  // introducing here the gen mass cut......................
121  /*
122  if (muPdgStatus_ ==1) {
123  mZGen = muMC.mother()->mother()->mass();
124  if (muMC.mother()->mother()->pdgId() ==23 && mZGen>accMassMinDen_ && mZGen<accMassMaxDen_ ) muons.push_back(muMC);}
125  }
126  if (muPdgStatus_ ==3) {
127  mZGen = muMC.mother()->mass();
128  if (muMC.mother()->pdgId() ==23 && mZGen>accMassMinDen_ && mZGen<accMassMaxDen_ ) muons.push_back(muMC);}
129  }
130 */
131 
132 
133  const GenParticle & zMC = (*gen)[i];
134  if (zMC.pdgId()==23 && zMC.status()==3 &&zMC.numberOfDaughters()>1 ) {
135  mZGen = zMC.mass();
136  cout << "I'm selecting a Z MC with mass " << mZGen << endl;
137  if(mZGen>accMassMinDen_ && mZGen<accMassMaxDen_ ) h_mZMC_->Fill(mZGen);
138 
139 
140 
141  }
142  }
143 
144 
145  cout << "finally I selected " << muons.size() << " muons" << endl;
146 
147 
148 
149 
150 
151 // if there are at least two muons,
152  // calculate invarant mass of first two and fill it into histogram
153 
154 
155 
156  double inv_mass = 0.0;
157  double Zpt_ = 0.0;
158  double Zeta_ = 0.0;
159  double Ztheta_ = 0.0;
160  double Zphi_ = 0.0;
161  double Zrapidity_ = 0.0;
162 
163  if(muons.size()>1) {
164 
165 
166  if (muons[0].mother()->mother()->pdgId()==23 && muons[1].mother()->mother()->pdgId()==23) nBothMuHasZHasGrandMa_ ++;
167  math::XYZTLorentzVector tot_momentum(muons[0].p4());
168  math::XYZTLorentzVector mom2(muons[1].p4());
169  tot_momentum += mom2;
170  inv_mass = sqrt(tot_momentum.mass2());
171  Zpt_=tot_momentum.pt();
172  Zeta_ = tot_momentum.eta();
173  Ztheta_ = tot_momentum.theta();
174  Zphi_ = tot_momentum.phi();
175  Zrapidity_ = tot_momentum.Rapidity();
176 
177 
178 
179 
180  double weight_sign = 1. ;
181 
182  //h_mZMC_->Fill(inv_mass);
183  h_ptZMC_->Fill(Zpt_,weight_sign);
184  h_etaZMC_->Fill(Zeta_,weight_sign);
185  h_thetaZMC_->Fill(Ztheta_,weight_sign);
186  h_phiZMC_->Fill(Zphi_,weight_sign);
187  h_rapidityZMC_-> Fill (Zrapidity_,weight_sign );
188 
189  double pt1 = muons[0].pt();
190  double pt2 = muons[1].pt();
191  double eta1 = muons[0].eta();
192  double eta2 = muons[1].eta();
193 
194 
195 
196 
197 
198  if(pt1>pt2) {
199  hardpt->Fill(pt1,weight_sign);
200  softpt->Fill(pt2,weight_sign);
201  hardeta->Fill(eta1,weight_sign);
202  softeta->Fill(eta2,weight_sign);
203  } else {
204  hardpt->Fill(pt2,weight_sign);
205  softpt->Fill(pt1,weight_sign);
206  hardeta->Fill(eta2,weight_sign);
207  softeta->Fill(eta1,weight_sign);
208  }
209 
210  //evaluating the geometric acceptance
211  if ( pt1 >= accPtMin_ && pt2 >= accPtMin_ && fabs(eta1)>= accEtaMin_ && fabs(eta2) >= accEtaMin_ && fabs(eta1)<= accEtaMax_ && fabs(eta2) <= accEtaMax_ && inv_mass>= accMassMin_ && inv_mass<= accMassMax_) nAcc_++;
212 
213 
214  cout << "pt1" << pt1 << endl;
215 
216  // scaling the muon pt
217  double pt1ScaledP = pt1* ( 1. + ptScale_);
218  cout << "pt1 ScaledP of " << ( 1. + ptScale_) << endl;
219  cout << "pt1ScaledP" << pt1ScaledP << endl;
220 
221  double pt2ScaledP = pt2 * ( 1. + ptScale_);
222 
223 
224  //evaluating the geometric acceptance
225  if ( pt1ScaledP >= accPtMin_ && pt2ScaledP >= accPtMin_ && fabs(eta1)>= accEtaMin_ && fabs(eta2) >= accEtaMin_ && fabs(eta1)<= accEtaMax_ && fabs(eta2) <= accEtaMax_ && inv_mass>= accMassMin_ && inv_mass<= accMassMax_)
226 nAccPtScaleP_++;
227 
228 
229 
230 
231  // scaling the muon pt
232  double pt1ScaledN = pt1* ( 1. - ptScale_);
233  double pt2ScaledN = pt2 * ( 1. - ptScale_);
234 
235 
236  //evaluating the geometric acceptance
237  if ( pt1ScaledN >= accPtMin_ && pt2ScaledN >= accPtMin_ && fabs(eta1)>= accEtaMin_ && fabs(eta2) >= accEtaMin_ && fabs(eta1)<= accEtaMax_ && fabs(eta2) <= accEtaMax_ && inv_mass>= accMassMin_ && inv_mass<= accMassMax_)
238 nAccPtScaleN_++;
239 
240 
241  // scaling the muon pt
242  TRandom3 f;
243  f.SetSeed(123456789);
244  double pt1SmearedFlat = pt1* ( 1. + ptScale_ * f.Uniform() );
245  double pt2SmearedFlat = pt2 * ( 1. + ptScale_ * f.Uniform() ) ;
246 
247 
248  //evaluating the geometric acceptance
249  if ( pt1SmearedFlat >= accPtMin_ && pt2SmearedFlat >= accPtMin_ && fabs(eta1)>= accEtaMin_ && fabs(eta2) >= accEtaMin_ && fabs(eta1)<= accEtaMax_ && fabs(eta2) <= accEtaMax_ && inv_mass>= accMassMin_ && inv_mass<= accMassMax_)
251 
252 
253 // scaling the muon pt
254  TRandom3 ff;
255  ff.SetSeed(123456789);
256  double pt1SmearedGaus = pt1* ( 1. + ptScale_ * f.Gaus() );
257  double pt2SmearedGaus = pt2 * ( 1. + ptScale_ * f.Gaus() ) ;
258 
259 
260  //evaluating the geometric acceptance
261  if ( pt1SmearedGaus >= accPtMin_ && pt2SmearedGaus >= accPtMin_ && fabs(eta1)>= accEtaMin_ && fabs(eta2) >= accEtaMin_ && fabs(eta1)<= accEtaMax_ && fabs(eta2) <= accEtaMax_ && inv_mass>= accMassMin_ && inv_mass<= accMassMax_)
263 
264 
265 
266 
267  }}
268 
269 
270 
271 
273  cout << " number of events accepted :" << nAcc_ << endl;
274  cout << " number of total events :" << h_mZMC_->GetEntries() << endl;
275  cout << " number of cases in which BothMuHasZHasGrandMa :" << nBothMuHasZHasGrandMa_ << endl;
276  cout << " number of events pt scaled positively accepted :" << nAccPtScaleP_ << endl;
277 
278  cout << " number of events pt scaled negatively accepted :" << nAccPtScaleN_ << endl;
279 
280 cout << " number of events pt scaled smeared flattely accepted :" << nAccPtScaleSmearedFlat_ << endl;
281 
282 cout << " number of events pt scaled smeared gaussianely accepted :" << nAccPtScaleSmearedGaus_ << endl;
283 
284 
285  double eff = (double)nAcc_ / (double) h_mZMC_->GetEntries();
286  double err = sqrt( eff * (1. - eff) / (double) h_mZMC_->GetEntries() );
287  cout << " geometric acceptance: " << eff << "+/-" << err << endl;
288 
289  double effScaledP = (double)nAccPtScaleP_ / (double) h_mZMC_->GetEntries();
290  double errScaledP = sqrt( effScaledP * (1. - effScaledP) / (double) h_mZMC_->GetEntries() );
291  cout << " geometric acceptance when pt muon is positively scaled: " << effScaledP << "+/-" << errScaledP << endl;
292 
293  double effScaledN = (double)nAccPtScaleN_ / (double) h_mZMC_->GetEntries();
294  double errScaledN = sqrt( effScaledN * (1. - effScaledN) / (double) h_mZMC_->GetEntries() );
295  cout << " geometric acceptance when pt muon is negatively scaled: " << effScaledN << "+/-" << errScaledN << endl;
296 
297  double effSmearedFlat = (double) nAccPtScaleSmearedFlat_ / (double) h_mZMC_->GetEntries();
298  double errSmearedFlat = sqrt( effSmearedFlat * (1. - effSmearedFlat) / (double) h_mZMC_->GetEntries() );
299  cout << " geometric acceptance when pt muon is scaled with a flat smaering: " << effSmearedFlat << "+/-" << errSmearedFlat << endl;
300 
301  double effSmearedGaus = (double) nAccPtScaleSmearedGaus_ / (double) h_mZMC_->GetEntries();
302  double errSmearedGaus = sqrt( effSmearedGaus * (1. - effSmearedGaus) / (double) h_mZMC_->GetEntries() );
303  cout << " geometric acceptance when pt muon is scaled with a gaussian smearing: " << effSmearedGaus << "+/-" << errSmearedGaus << endl;
304 
305 
306 }
308 
310 
std::vector< GenParticle > GenParticleCollection
collection of GenParticles
int pdgId() const final
PDG identifier.
ZMuPtScaleAnalyzer(const edm::ParameterSet &pset)
double eta() const final
momentum pseudorapidity
unsigned int nAccPtScaleP_
size_t numberOfMothers() const override
number of mothers
def setup(process, global_tag, zero_tesla=False)
Definition: GeneralSetup.py:2
virtual const Candidate * mother(size_type i=0) const =0
return pointer to mother
edm::EDGetTokenT< reco::GenParticleCollection > genToken_
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
XYZTLorentzVectorD XYZTLorentzVector
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:29
unsigned int nBothMuHasZHasGrandMa_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
unsigned int nAccPtScaleN_
void Fill(HcalDetId &id, double val, std::vector< TH2F > &depth)
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)
void endJob() override
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
double f[11][100]
T * make(const Args &...args) const
make new ROOT object
def gen(fragment, howMuch)
Production test section ####.
TFileDirectory mkdir(const std::string &dir, const std::string &descr="")
create a new subdirectory
Definition: TFileService.h:69
size_t numberOfDaughters() const override
number of daughters
unsigned int nAccPtScaleSmearedFlat_
fixed size matrix
HLT enums.
int status() const final
status word
void analyze(const edm::Event &event, const edm::EventSetup &setup) override
unsigned int nAccPtScaleSmearedGaus_
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
double mass() const final
mass