00001 #include "FWCore/Framework/interface/EDAnalyzer.h"
00002 #include "FWCore/Utilities/interface/InputTag.h"
00003 #include "TH1.h"
00004
00005 class EWKSystUnc : public edm::EDAnalyzer {
00006 public:
00007 EWKSystUnc(const edm::ParameterSet& pset);
00008 private:
00009 virtual void analyze(const edm::Event& event, const edm::EventSetup& setup);
00010 virtual void endJob();
00011 edm::InputTag gen_, weights_;
00012 unsigned int nbinsMass_, nbinsPt_, nbinsAng_ ;
00013 double massMax_, ptMax_, angMax_;
00014 double accPtMin_,accMassMin_,accMassMax_, accEtaMin_, accEtaMax_;
00015 TH1F *h_nZ_;
00016 TH1F *h_mZMC_, *h_ptZMC_, *h_phiZMC_, *h_thetaZMC_, *h_etaZMC_, *h_rapidityZMC_;
00017 TH1F *hardpt, *softpt, * hardeta, *softeta;
00018 TH1F * h_weight_histo;
00019 bool isMCatNLO_;
00020 double nAcc_, nAccReW_, nBothMuHasZHasGrandMa_;
00021 std::string filename_;
00022
00023 };
00024
00025 #include "DataFormats/Candidate/interface/Candidate.h"
00026 #include "DataFormats/Candidate/interface/CandidateFwd.h"
00027 #include "DataFormats/Candidate/interface/CandMatchMap.h"
00028 #include "DataFormats/HepMCCandidate/interface/GenParticle.h"
00029 #include "DataFormats/HepMCCandidate/interface/GenParticleFwd.h"
00030 #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
00031
00032 #include "HepMC/WeightContainer.h"
00033 #include "HepMC/GenEvent.h"
00034 #include "FWCore/Framework/interface/Event.h"
00035 #include "DataFormats/Common/interface/Handle.h"
00036 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00037 #include "FWCore/ServiceRegistry/interface/Service.h"
00038 #include "CommonTools/UtilAlgos/interface/TFileService.h"
00039 #include <cmath>
00040 #include <iostream>
00041 #include <fstream>
00042
00043 using namespace std;
00044 using namespace reco;
00045 using namespace edm;
00046
00047 EWKSystUnc::EWKSystUnc(const ParameterSet& pset) :
00048 gen_(pset.getParameter<InputTag>("genParticles")),
00049 weights_(pset.getParameter<InputTag>("weights")),
00050 nbinsMass_(pset.getUntrackedParameter<unsigned int>("nbinsMass")),
00051 nbinsPt_(pset.getUntrackedParameter<unsigned int>("nbinsPt")),
00052 nbinsAng_(pset.getUntrackedParameter<unsigned int>("nbinsAng")),
00053 massMax_(pset.getUntrackedParameter<double>("massMax")),
00054 ptMax_(pset.getUntrackedParameter<double>("ptMax")),
00055 angMax_(pset.getUntrackedParameter<double>("angMax")),
00056 accPtMin_(pset.getUntrackedParameter<double>("accPtMin")),
00057 accMassMin_(pset.getUntrackedParameter<double>("accMassMin")),
00058 accMassMax_(pset.getUntrackedParameter<double>("accMassMax")),
00059 accEtaMin_(pset.getUntrackedParameter<double>("accEtaMin")),
00060 accEtaMax_(pset.getUntrackedParameter<double>("accEtaMax")),
00061 isMCatNLO_(pset.getUntrackedParameter<bool>("isMCatNLO")),
00062 filename_(pset.getUntrackedParameter<std::string>("outfilename")) {
00063 cout << ">>> Z Histogrammer constructor" << endl;
00064 Service<TFileService> fs;
00065
00066 TFileDirectory ZMCHisto = fs->mkdir( "ZMCHisto" );
00067 h_nZ_ = ZMCHisto.make<TH1F>("ZNumber", "number of Z particles", 11, -0.5, 10.5);
00068 h_weight_histo = ZMCHisto.make<TH1F>("weight_histo","weight_histo",20,-10,10);
00069
00070 h_mZMC_ = ZMCHisto.make<TH1F>("ZMCMass", "Z MC mass (GeV/c^{2})", nbinsMass_, 0, massMax_);
00071 h_ptZMC_ = ZMCHisto.make<TH1F>("ZMCPt", "Z MC p_{t} (GeV/c)", nbinsPt_, 0, ptMax_);
00072 hardpt = ZMCHisto.make<TH1F>("hardpt", "hard muon p_{t} (GeV/c)", nbinsPt_, 0, ptMax_);
00073 softpt = ZMCHisto.make<TH1F>("softpt", "soft muon p_{t} (GeV/c)", nbinsPt_, 0, ptMax_);
00074
00075
00076 h_phiZMC_ = ZMCHisto.make<TH1F>("ZMCPhi", "Z MC #phi", nbinsAng_, -angMax_, angMax_);
00077 h_thetaZMC_ = ZMCHisto.make<TH1F>("ZMCTheta", "Z MC #theta", nbinsAng_, 0, angMax_);
00078 h_etaZMC_ = ZMCHisto.make<TH1F>("ZMCEta", "Z MC #eta", nbinsAng_, -angMax_, angMax_);
00079 h_rapidityZMC_ = ZMCHisto.make<TH1F>("ZMCRapidity", "Z MC y", nbinsAng_, -angMax_, angMax_);
00080
00081 hardeta = ZMCHisto.make<TH1F>("hard muon eta", "hard muon #eta", nbinsAng_, -angMax_, angMax_);
00082 softeta = ZMCHisto.make<TH1F>("soft muon eta", "soft muon #eta", nbinsAng_, -angMax_, angMax_);
00083 nAcc_=0.;
00084 nAccReW_ =0;
00085 nBothMuHasZHasGrandMa_=0;
00086
00087 }
00088
00089 void EWKSystUnc::analyze(const edm::Event& event, const edm::EventSetup& setup) {
00090 cout << ">>> Z Histogrammer analyze" << endl;
00091
00092 Handle<GenParticleCollection> gen;
00093 Handle<double > weights;
00094
00095 event.getByLabel(gen_, gen);
00096 event.getByLabel(weights_, weights );
00097
00098
00099
00100
00101 double weight = (*weights);
00102
00103
00104 if (weight> 2. || weight < 0.1) {
00105
00106 std::cout << "weight = " << weight << ", something strange...." << std::endl;
00107 weight =1;
00108 }
00109 h_weight_histo->Fill(weight);
00110
00111 std::vector<GenParticle> muons;
00112
00113
00114 for(unsigned int i = 0; i < gen->size(); ++i){
00115 const GenParticle & muMC = (*gen)[i];
00116
00117 if (abs(muMC.pdgId())==13 && muMC.status()==1 && muMC.numberOfMothers()>0) {
00118 if (muMC.mother()->numberOfMothers()> 0 ){
00119 cout << "I'm getting a muon \n"
00120 << "with " << "muMC.numberOfMothers() " << muMC.numberOfMothers() << "\n the first mother has pdgId " << muMC.mother()->pdgId()
00121 << "with " << "muMC.mother()->numberOfMothers() " << muMC.mother()->numberOfMothers()<< "\n the first grandma has pdgId " << muMC.mother()->mother()->pdgId()<<endl;
00122 if (muMC.mother()->mother()->pdgId() ==23 ) muons.push_back(muMC);
00123 }
00124 }
00125 }
00126
00127 cout << "finally I selected " << muons.size() << " muons" << endl;
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141 double inv_mass = 0.0;
00142 double Zpt_ = 0.0;
00143 double Zeta_ = 0.0;
00144 double Ztheta_ = 0.0;
00145 double Zphi_ = 0.0;
00146 double Zrapidity_ = 0.0;
00147
00148 if(muons.size()>1) {
00149 if (muons[0].mother()->mother()->pdgId()==23 && muons[1].mother()->mother()->pdgId()==23) nBothMuHasZHasGrandMa_ ++;
00150 math::XYZTLorentzVector tot_momentum(muons[0].p4());
00151 math::XYZTLorentzVector mom2(muons[1].p4());
00152 tot_momentum += mom2;
00153 inv_mass = sqrt(tot_momentum.mass2());
00154 Zpt_=tot_momentum.pt();
00155 Zeta_ = tot_momentum.eta();
00156 Ztheta_ = tot_momentum.theta();
00157 Zphi_ = tot_momentum.phi();
00158 Zrapidity_ = tot_momentum.Rapidity();
00159
00160
00161
00162
00163
00164 double weight_sign = weight;
00165
00166 h_mZMC_->Fill(inv_mass,weight_sign);
00167 h_ptZMC_->Fill(Zpt_,weight_sign);
00168 h_etaZMC_->Fill(Zeta_,weight_sign);
00169 h_thetaZMC_->Fill(Ztheta_,weight_sign);
00170 h_phiZMC_->Fill(Zphi_,weight_sign);
00171 h_rapidityZMC_-> Fill (Zrapidity_,weight_sign );
00172
00173 double pt1 = muons[0].pt();
00174 double pt2 = muons[1].pt();
00175 double eta1 = muons[0].eta();
00176 double eta2 = muons[1].eta();
00177
00178
00179
00180 if(pt1>pt2) {
00181 hardpt->Fill(pt1,weight_sign);
00182 softpt->Fill(pt2,weight_sign);
00183 hardeta->Fill(eta1,weight_sign);
00184 softeta->Fill(eta2,weight_sign);
00185 } else {
00186 hardpt->Fill(pt2,weight_sign);
00187 softpt->Fill(pt1,weight_sign);
00188 hardeta->Fill(eta2,weight_sign);
00189 softeta->Fill(eta1,weight_sign);
00190 }
00191
00192
00193
00194 if ( pt1 >= accPtMin_ && pt2 >= accPtMin_ && fabs(eta1)>= accEtaMin_ && fabs(eta2) >= accEtaMin_ && fabs(eta1)<= accEtaMax_ && fabs(eta2) <= accEtaMax_ && inv_mass>= accMassMin_ && inv_mass<= accMassMax_) {
00195 nAcc_ ++;
00196 nAccReW_ +=weight;
00197 }
00198
00199 }
00200
00201 }
00202
00203
00204 void EWKSystUnc::endJob() {
00205 cout << " number of events accepted :" << nAcc_ << endl;
00206 cout << " number of events accepted reweigthed :" << nAccReW_ << endl;
00207 double nev = h_mZMC_->GetEntries();
00208 double nev_weigthed = h_mZMC_->Integral(0,nbinsMass_ +1 ) ;
00209 cout << " number of total events :" << nev << endl;
00210 cout << " number of total weighted events :" << nev_weigthed << endl;
00211 cout << " number of cases in which BothMuHasZHasGrandMa :" << nBothMuHasZHasGrandMa_ << endl;
00212 double eff = (double)nAcc_ / (double) h_mZMC_->GetEntries();
00213 double eff_rew = (double)nAccReW_ / (double) h_mZMC_->Integral(0,nbinsMass_ +1 );
00214 double err = sqrt( eff * (1. - eff) / (double) h_mZMC_->GetEntries() );
00215 double err_rew = sqrt( eff_rew * (1. - eff_rew) / (double) h_mZMC_->Integral(0,nbinsMass_ +1 ) );
00216 cout << " geometric acceptance: " << eff << "+/-" << err << endl;
00217 cout << " geometric acceptance reweighted: " << eff_rew << "+/-" << err_rew << endl;
00218
00219 ofstream myfile;
00220 myfile.open(filename_.c_str(), std::ios::app);
00221 myfile<< eff << " "<< eff_rew << " " << nev << " " << nev_weigthed << endl ;
00222 myfile.close();
00223 }
00224 #include "FWCore/Framework/interface/MakerMacros.h"
00225
00226 DEFINE_FWK_MODULE(EWKSystUnc);
00227