CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
zPdfUnc.cc
Go to the documentation of this file.
3 #include "TH1.h"
4 
5 class zPdfUnc : public edm::EDAnalyzer {
6 public:
7  zPdfUnc(const edm::ParameterSet& pset);
8 private:
9  virtual void analyze(const edm::Event& event, const edm::EventSetup& setup) override;
10  virtual void endJob() override;
15  TH1F *h_nZ_;
17  TH1F *hardpt, *softpt, * hardeta, *softeta;
18  TH1F * h_weight_histo;
19  bool isMCatNLO_;
22 
23 };
24 
31 
32 #include "HepMC/WeightContainer.h"
33 #include "HepMC/GenEvent.h"
39 #include <cmath>
40 #include <iostream>
41 #include <fstream>
42 
43 using namespace std;
44 using namespace reco;
45 using namespace edm;
46 
48  gen_(pset.getParameter<InputTag>("genParticles")),
49  pdfweights_(pset.getParameter<InputTag>("pdfweights")),
50  pdfmember_(pset.getUntrackedParameter<unsigned int>("pdfmember")),
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  accMassMinDenominator_(pset.getUntrackedParameter<double>("accMassMinDenominator")),
63  isMCatNLO_(pset.getUntrackedParameter<bool>("isMCatNLO")),
64  filename_(pset.getUntrackedParameter<std::string>("outfilename")) {
65  cout << ">>> Z Histogrammer constructor" << endl;
67 
68  TFileDirectory ZMCHisto = fs->mkdir( "ZMCHisto" );
69  h_nZ_ = ZMCHisto.make<TH1F>("ZNumber", "number of Z particles", 11, -0.5, 10.5);
70  h_weight_histo = ZMCHisto.make<TH1F>("weight_histo","weight_histo",20,-10,10);
71 
72  h_mZMC_ = ZMCHisto.make<TH1F>("ZMCMass", "Z MC mass (GeV/c^{2})", nbinsMass_, 0, massMax_);
73  h_ptZMC_ = ZMCHisto.make<TH1F>("ZMCPt", "Z MC p_{t} (GeV/c)", nbinsPt_, 0, ptMax_);
74  hardpt = ZMCHisto.make<TH1F>("hardpt", "hard muon p_{t} (GeV/c)", nbinsPt_, 0, ptMax_);
75  softpt = ZMCHisto.make<TH1F>("softpt", "soft muon p_{t} (GeV/c)", nbinsPt_, 0, ptMax_);
76 
77 
78  h_phiZMC_ = ZMCHisto.make<TH1F>("ZMCPhi", "Z MC #phi", nbinsAng_, -angMax_, angMax_);
79  h_thetaZMC_ = ZMCHisto.make<TH1F>("ZMCTheta", "Z MC #theta", nbinsAng_, 0, angMax_);
80  h_etaZMC_ = ZMCHisto.make<TH1F>("ZMCEta", "Z MC #eta", nbinsAng_, -angMax_, angMax_);
81  h_rapidityZMC_ = ZMCHisto.make<TH1F>("ZMCRapidity", "Z MC y", nbinsAng_, -angMax_, angMax_);
82 
83  hardeta = ZMCHisto.make<TH1F>("hard muon eta", "hard muon #eta", nbinsAng_, -angMax_, angMax_);
84  softeta = ZMCHisto.make<TH1F>("soft muon eta", "soft muon #eta", nbinsAng_, -angMax_, angMax_);
85  nAcc_=0.;
86  nAccReW_ =0;
88 
89 }
90 
92  cout << ">>> Z Histogrammer analyze" << endl;
93 
95  Handle< vector<double> > pdfweights;
96 
97  event.getByLabel(gen_, gen);
98  event.getByLabel(pdfweights_, pdfweights );
99 
100 
101 
102  // get weight and fill it to histogram
103  std::vector<double> weights = (*pdfweights);
104 
105  unsigned int nmembers = weights.size();
106 
107 
108  double weight = weights[pdfmember_];
109 
110  h_weight_histo->Fill(weight);
111 
112  cout << "found nmember: " << nmembers << endl;
113  cout << "taken the member n " << pdfmember_ << " weight= " << weight << endl;
114 
115 
116 
117  std::vector<GenParticle> muons;
118 
119 
120  for(unsigned int i = 0; i < gen->size(); ++i){
121  const GenParticle & muMC = (*gen)[i];
122  // filling only muons coming form Z
123  if (abs(muMC.pdgId())==13 && muMC.status()==1 && muMC.numberOfMothers()>0) {
124  if (muMC.mother()->numberOfMothers()> 0 ){
125  cout << "I'm getting a muon \n"
126  << "with " << "muMC.numberOfMothers() " << muMC.numberOfMothers() << "\n the first mother has pdgId " << muMC.mother()->pdgId()
127  << "with " << "muMC.mother()->numberOfMothers() " << muMC.mother()->numberOfMothers()<< "\n the first grandma has pdgId " << muMC.mother()->mother()->pdgId()<<endl;
128  if (muMC.mother()->mother()->pdgId() ==23 ) muons.push_back(muMC);
129  }
130  }
131  }
132 
133  cout << "finally I selected " << muons.size() << " muons" << endl;
134 
135 // if there are at least two muons,
136  // calculate invarant mass of first two and fill it into histogram
137 
138  double inv_mass = 0.0;
139  double Zpt_ = 0.0;
140  double Zeta_ = 0.0;
141  double Ztheta_ = 0.0;
142  double Zphi_ = 0.0;
143  double Zrapidity_ = 0.0;
144 
145  if(muons.size()>1) {
146  if (muons[0].mother()->mother()->pdgId()==23 && muons[1].mother()->mother()->pdgId()==23) nBothMuHasZHasGrandMa_ ++;
147  math::XYZTLorentzVector tot_momentum(muons[0].p4());
148  math::XYZTLorentzVector mom2(muons[1].p4());
149  tot_momentum += mom2;
150  inv_mass = sqrt(tot_momentum.mass2());
151 
152  Zpt_=tot_momentum.pt();
153  Zeta_ = tot_momentum.eta();
154  Ztheta_ = tot_momentum.theta();
155  Zphi_ = tot_momentum.phi();
156  Zrapidity_ = tot_momentum.Rapidity();
157 
158 
159 
160  // IMPORTANT: use the weight of the event ...
161 
162  double weight_sign = weight;
163  //double weight_sign = 1. ;
164  // fill the denominator numbers only if mass>accMassMinDenominator and go on in that case
165  if (inv_mass > accMassMinDenominator_ ) {
166  h_mZMC_->Fill(inv_mass,weight_sign);
167  h_ptZMC_->Fill(Zpt_,weight_sign);
168  h_etaZMC_->Fill(Zeta_,weight_sign);
169  h_thetaZMC_->Fill(Ztheta_,weight_sign);
170  h_phiZMC_->Fill(Zphi_,weight_sign);
171  h_rapidityZMC_-> Fill (Zrapidity_,weight_sign );
172 
173  double pt1 = muons[0].pt();
174  double pt2 = muons[1].pt();
175  double eta1 = muons[0].eta();
176  double eta2 = muons[1].eta();
177  if(pt1>pt2) {
178  hardpt->Fill(pt1,weight_sign);
179  softpt->Fill(pt2,weight_sign);
180  hardeta->Fill(eta1,weight_sign);
181  softeta->Fill(eta2,weight_sign);
182  } else {
183  hardpt->Fill(pt2,weight_sign);
184  softpt->Fill(pt1,weight_sign);
185  hardeta->Fill(eta2,weight_sign);
186  softeta->Fill(eta1,weight_sign);
187  }
188 
189  //evaluating the geometric acceptance
190  if ( pt1 >= accPtMin_ && pt2 >= accPtMin_ && fabs(eta1)>= accEtaMin_ && fabs(eta2) >= accEtaMin_ && fabs(eta1)<= accEtaMax_ && fabs(eta2) <= accEtaMax_ && inv_mass>= accMassMin_ && inv_mass<= accMassMax_) {
191  nAcc_ ++;
192  nAccReW_ +=weight;
193  }
194  }
195  }
196 }
197 
198 
200  cout << " number of events accepted :" << nAcc_ << endl;
201  cout << " number of events accepted reweigthed :" << nAccReW_ << endl;
202  double nev = h_mZMC_->GetEntries();
203  double nev_weigthed = h_mZMC_->Integral( ) ;
204  cout << " number of total events :" << nev << endl;
205  cout << " number of total weighted events :" << nev_weigthed << endl;
206  cout << " number of cases in which BothMuHasZHasGrandMa :" << nBothMuHasZHasGrandMa_ << endl;
207  double eff = (double)nAcc_ / (double) h_mZMC_->GetEntries();
208  double eff_rew = (double)nAccReW_ / (double) h_mZMC_->Integral(0,nbinsMass_ +1 );
209  double err = sqrt( eff * (1. - eff) / (double) h_mZMC_->GetEntries() );
210  double err_rew = sqrt( eff_rew * (1. - eff_rew) / (double) h_mZMC_->Integral(0,nbinsMass_ +1 ) );
211  cout << " geometric acceptance: " << eff << "+/-" << err << endl;
212  cout << " geometric acceptance reweighted: " << eff_rew << "+/-" << err_rew << endl;
213 
214  ofstream myfile;
215  myfile.open(filename_.c_str(), std::ios::app);
216  myfile<< eff << " "<< eff_rew << " " << nev << " " << nev_weigthed << endl ;
217  myfile.close();
218 }
220 
222 
int i
Definition: DBlmapReader.cc:9
double ptMax_
Definition: zPdfUnc.cc:13
edm::InputTag gen_
Definition: zPdfUnc.cc:11
TH1F * h_weight_histo
Definition: zPdfUnc.cc:18
double nBothMuHasZHasGrandMa_
Definition: zPdfUnc.cc:20
virtual const Candidate * mother(size_type i=0) const =0
return pointer to mother
TH1F * h_ptZMC_
Definition: zPdfUnc.cc:16
double accMassMinDenominator_
Definition: zPdfUnc.cc:14
virtual void analyze(const edm::Event &event, const edm::EventSetup &setup) override
Definition: zPdfUnc.cc:91
virtual int pdgId() const GCC11_FINAL
PDG identifier.
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
double accEtaMax_
Definition: zPdfUnc.cc:14
virtual size_type numberOfMothers() const =0
number of mothers (zero or one in most of but not all the cases)
TH1F * h_nZ_
Definition: zPdfUnc.cc:15
double accPtMin_
Definition: zPdfUnc.cc:14
double nAccReW_
Definition: zPdfUnc.cc:20
TH1F * softeta
Definition: zPdfUnc.cc:17
zPdfUnc(const edm::ParameterSet &pset)
Definition: zPdfUnc.cc:47
unsigned int nbinsAng_
Definition: zPdfUnc.cc:12
XYZTLorentzVectorD XYZTLorentzVector
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:29
double angMax_
Definition: zPdfUnc.cc:13
virtual int status() const GCC11_FINAL
status word
void Fill(HcalDetId &id, double val, std::vector< TH2F > &depth)
virtual size_t numberOfMothers() const
number of mothers
T sqrt(T t)
Definition: SSEVec.h:48
double p4[4]
Definition: TauolaWrapper.h:92
TH1F * h_etaZMC_
Definition: zPdfUnc.cc:16
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
bool isMCatNLO_
Definition: zPdfUnc.cc:19
T * make(const Args &...args) const
make new ROOT object
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger but the state exists so we define the behavior If all triggers are the negative crieriion will lead to accepting the event(this again matches the behavior of"!*"before the partial wildcard feature was incorporated).The per-event"cost"of each negative criterion with multiple relevant triggers is about the same as!*was in the past
double accMassMax_
Definition: zPdfUnc.cc:14
virtual int pdgId() const =0
PDG identifier.
TH1F * hardeta
Definition: zPdfUnc.cc:17
unsigned int nbinsPt_
Definition: zPdfUnc.cc:12
TH1F * h_phiZMC_
Definition: zPdfUnc.cc:16
std::string filename_
Definition: zPdfUnc.cc:21
TFileDirectory mkdir(const std::string &dir, const std::string &descr="")
create a new subdirectory
Definition: TFileService.h:69
TH1F * h_mZMC_
Definition: zPdfUnc.cc:16
double nAcc_
Definition: zPdfUnc.cc:20
tuple muons
Definition: patZpeak.py:38
TH1F * h_thetaZMC_
Definition: zPdfUnc.cc:16
double accEtaMin_
Definition: zPdfUnc.cc:14
tuple cout
Definition: gather_cfg.py:121
TH1F * softpt
Definition: zPdfUnc.cc:17
TH1F * hardpt
Definition: zPdfUnc.cc:17
int weight
Definition: histoStyle.py:50
edm::InputTag pdfweights_
Definition: zPdfUnc.cc:11
unsigned int nbinsMass_
Definition: zPdfUnc.cc:12
virtual const Candidate * mother(size_type=0) const
return mother at a given position, i = 0, ... numberOfMothers() - 1 (read only mode) ...
void setup(std::vector< TH2F > &depth, std::string name, std::string units="")
TH1F * h_rapidityZMC_
Definition: zPdfUnc.cc:16
virtual void endJob() override
Definition: zPdfUnc.cc:199
double massMax_
Definition: zPdfUnc.cc:13
double accMassMin_
Definition: zPdfUnc.cc:14
unsigned int pdfmember_
Definition: zPdfUnc.cc:12