CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
EWKSystUnc.cc
Go to the documentation of this file.
3 #include "TH1.h"
4 
5 class EWKSystUnc : public edm::EDAnalyzer {
6 public:
8 private:
9  virtual void analyze(const edm::Event& event, const edm::EventSetup& setup);
10  virtual void endJob();
12  unsigned int nbinsMass_, nbinsPt_, nbinsAng_ ;
15  TH1F *h_nZ_;
17  TH1F *hardpt, *softpt, * hardeta, *softeta;
18  TH1F * h_weight_histo;
19  bool isMCatNLO_;
21  std::string filename_;
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  weights_(pset.getParameter<InputTag>("weights")),
50  nbinsMass_(pset.getUntrackedParameter<unsigned int>("nbinsMass")),
51  nbinsPt_(pset.getUntrackedParameter<unsigned int>("nbinsPt")),
52  nbinsAng_(pset.getUntrackedParameter<unsigned int>("nbinsAng")),
53  massMax_(pset.getUntrackedParameter<double>("massMax")),
54  ptMax_(pset.getUntrackedParameter<double>("ptMax")),
55  angMax_(pset.getUntrackedParameter<double>("angMax")),
56  accPtMin_(pset.getUntrackedParameter<double>("accPtMin")),
57  accMassMin_(pset.getUntrackedParameter<double>("accMassMin")),
58  accMassMax_(pset.getUntrackedParameter<double>("accMassMax")),
59  accEtaMin_(pset.getUntrackedParameter<double>("accEtaMin")),
60  accEtaMax_(pset.getUntrackedParameter<double>("accEtaMax")),
61  isMCatNLO_(pset.getUntrackedParameter<bool>("isMCatNLO")),
62  filename_(pset.getUntrackedParameter<std::string>("outfilename")) {
63  cout << ">>> Z Histogrammer constructor" << endl;
65 
66  TFileDirectory ZMCHisto = fs->mkdir( "ZMCHisto" );
67  h_nZ_ = ZMCHisto.make<TH1F>("ZNumber", "number of Z particles", 11, -0.5, 10.5);
68  h_weight_histo = ZMCHisto.make<TH1F>("weight_histo","weight_histo",20,-10,10);
69 
70  h_mZMC_ = ZMCHisto.make<TH1F>("ZMCMass", "Z MC mass (GeV/c^{2})", nbinsMass_, 0, massMax_);
71  h_ptZMC_ = ZMCHisto.make<TH1F>("ZMCPt", "Z MC p_{t} (GeV/c)", nbinsPt_, 0, ptMax_);
72  hardpt = ZMCHisto.make<TH1F>("hardpt", "hard muon p_{t} (GeV/c)", nbinsPt_, 0, ptMax_);
73  softpt = ZMCHisto.make<TH1F>("softpt", "soft muon p_{t} (GeV/c)", nbinsPt_, 0, ptMax_);
74 
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 }
88 
90  cout << ">>> Z Histogrammer analyze" << endl;
91 
93  Handle<double > weights;
94 
95  event.getByLabel(gen_, gen);
96  event.getByLabel(weights_, weights );
97 
98 
99 
100  // get weight and fill it to histogram
101  double weight = (*weights);
102 
103  // protection...
104 if (weight> 2. || weight < 0.1) {
105 
106  std::cout << "weight = " << weight << ", something strange...." << std::endl;
107 weight =1;
108 }
109  h_weight_histo->Fill(weight);
110 
111  std::vector<GenParticle> muons;
112 
113 
114  for(unsigned int i = 0; i < gen->size(); ++i){
115  const GenParticle & muMC = (*gen)[i];
116  // filling only muons coming form Z
117  if (abs(muMC.pdgId())==13 && muMC.status()==1 && muMC.numberOfMothers()>0) {
118  if (muMC.mother()->numberOfMothers()> 0 ){
119  cout << "I'm getting a muon \n"
120  << "with " << "muMC.numberOfMothers() " << muMC.numberOfMothers() << "\n the first mother has pdgId " << muMC.mother()->pdgId()
121  << "with " << "muMC.mother()->numberOfMothers() " << muMC.mother()->numberOfMothers()<< "\n the first grandma has pdgId " << muMC.mother()->mother()->pdgId()<<endl;
122  if (muMC.mother()->mother()->pdgId() ==23 ) muons.push_back(muMC);
123  }
124  }
125  }
126 
127  cout << "finally I selected " << muons.size() << " muons" << endl;
128 
129 
130 
131 
132 
133 // if there are at least two muons,
134  // calculate invarant mass of first two and fill it into histogram
135 
136 
137 
138 
139 
140 
141  double inv_mass = 0.0;
142  double Zpt_ = 0.0;
143  double Zeta_ = 0.0;
144  double Ztheta_ = 0.0;
145  double Zphi_ = 0.0;
146  double Zrapidity_ = 0.0;
147 
148  if(muons.size()>1) {
149  if (muons[0].mother()->mother()->pdgId()==23 && muons[1].mother()->mother()->pdgId()==23) nBothMuHasZHasGrandMa_ ++;
150  math::XYZTLorentzVector tot_momentum(muons[0].p4());
151  math::XYZTLorentzVector mom2(muons[1].p4());
152  tot_momentum += mom2;
153  inv_mass = sqrt(tot_momentum.mass2());
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  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 
178 
179 
180  if(pt1>pt2) {
181  hardpt->Fill(pt1,weight_sign);
182  softpt->Fill(pt2,weight_sign);
183  hardeta->Fill(eta1,weight_sign);
184  softeta->Fill(eta2,weight_sign);
185  } else {
186  hardpt->Fill(pt2,weight_sign);
187  softpt->Fill(pt1,weight_sign);
188  hardeta->Fill(eta2,weight_sign);
189  softeta->Fill(eta1,weight_sign);
190  }
191 
192 
193  //evaluating the geometric acceptance
194  if ( pt1 >= accPtMin_ && pt2 >= accPtMin_ && fabs(eta1)>= accEtaMin_ && fabs(eta2) >= accEtaMin_ && fabs(eta1)<= accEtaMax_ && fabs(eta2) <= accEtaMax_ && inv_mass>= accMassMin_ && inv_mass<= accMassMax_) {
195 nAcc_ ++;
196 nAccReW_ +=weight;
197  }
198 
199  }
200 
201 }
202 
203 
205  cout << " number of events accepted :" << nAcc_ << endl;
206  cout << " number of events accepted reweigthed :" << nAccReW_ << endl;
207  double nev = h_mZMC_->GetEntries();
208  double nev_weigthed = h_mZMC_->Integral(0,nbinsMass_ +1 ) ;
209  cout << " number of total events :" << nev << endl;
210  cout << " number of total weighted events :" << nev_weigthed << endl;
211  cout << " number of cases in which BothMuHasZHasGrandMa :" << nBothMuHasZHasGrandMa_ << endl;
212  double eff = (double)nAcc_ / (double) h_mZMC_->GetEntries();
213  double eff_rew = (double)nAccReW_ / (double) h_mZMC_->Integral(0,nbinsMass_ +1 );
214  double err = sqrt( eff * (1. - eff) / (double) h_mZMC_->GetEntries() );
215  double err_rew = sqrt( eff_rew * (1. - eff_rew) / (double) h_mZMC_->Integral(0,nbinsMass_ +1 ) );
216  cout << " geometric acceptance: " << eff << "+/-" << err << endl;
217  cout << " geometric acceptance reweighted: " << eff_rew << "+/-" << err_rew << endl;
218 
219  ofstream myfile;
220  myfile.open(filename_.c_str(), std::ios::app);
221  myfile<< eff << " "<< eff_rew << " " << nev << " " << nev_weigthed << endl ;
222  myfile.close();
223 }
225 
227 
std::string filename_
Definition: EWKSystUnc.cc:21
TH1F * softpt
Definition: EWKSystUnc.cc:17
int i
Definition: DBlmapReader.cc:9
virtual int pdgId() const
PDG identifier.
double nBothMuHasZHasGrandMa_
Definition: EWKSystUnc.cc:20
TH1F * h_ptZMC_
Definition: EWKSystUnc.cc:16
double accMassMin_
Definition: EWKSystUnc.cc:14
bool isMCatNLO_
Definition: EWKSystUnc.cc:19
virtual const Candidate * mother(size_type i=0) const =0
return pointer to mother
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
virtual int status() const
status word
TH1F * h_phiZMC_
Definition: EWKSystUnc.cc:16
double accMassMax_
Definition: EWKSystUnc.cc:14
double accEtaMin_
Definition: EWKSystUnc.cc:14
virtual size_type numberOfMothers() const =0
number of mothers (zero or one in most of but not all the cases)
TH1F * hardeta
Definition: EWKSystUnc.cc:17
#define abs(x)
Definition: mlp_lapack.h:159
TH1F * hardpt
Definition: EWKSystUnc.cc:17
double nAccReW_
Definition: EWKSystUnc.cc:20
TH1F * h_rapidityZMC_
Definition: EWKSystUnc.cc:16
TH1F * h_mZMC_
Definition: EWKSystUnc.cc:16
edm::InputTag weights_
Definition: EWKSystUnc.cc:11
XYZTLorentzVectorD XYZTLorentzVector
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:30
unsigned int nbinsMass_
Definition: EWKSystUnc.cc:12
double accPtMin_
Definition: EWKSystUnc.cc:14
TH1F * softeta
Definition: EWKSystUnc.cc:17
virtual void endJob()
Definition: EWKSystUnc.cc:204
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:28
double p4[4]
Definition: TauolaWrapper.h:92
TH1F * h_weight_histo
Definition: EWKSystUnc.cc:18
unsigned int nbinsPt_
Definition: EWKSystUnc.cc:12
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 angMax_
Definition: EWKSystUnc.cc:13
virtual int pdgId() const =0
PDG identifier.
double accEtaMax_
Definition: EWKSystUnc.cc:14
TFileDirectory mkdir(const std::string &dir, const std::string &descr="")
create a new subdirectory
double nAcc_
Definition: EWKSystUnc.cc:20
virtual void analyze(const edm::Event &event, const edm::EventSetup &setup)
Definition: EWKSystUnc.cc:89
tuple muons
Definition: patZpeak.py:38
edm::InputTag gen_
Definition: EWKSystUnc.cc:11
TH1F * h_thetaZMC_
Definition: EWKSystUnc.cc:16
T * make() const
make new ROOT object
tuple cout
Definition: gather_cfg.py:41
double ptMax_
Definition: EWKSystUnc.cc:13
TH1F * h_nZ_
Definition: EWKSystUnc.cc:15
double massMax_
Definition: EWKSystUnc.cc:13
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:12
void setup(std::vector< TH2F > &depth, std::string name, std::string units="")
EWKSystUnc(const edm::ParameterSet &pset)
Definition: EWKSystUnc.cc:47
TH1F * h_etaZMC_
Definition: EWKSystUnc.cc:16