Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:29:58

0001 /** \class PythiaFilterGammaJetWithBg
0002  *
0003  *  PythiaFilterGammaJet filter implements generator-level preselections
0004  *  for photon+jet like events to be used in jet energy calibration.
0005  *  Ported from fortran code written by V.Konoplianikov.
0006  *
0007  * \author A.Ulyanov, ITEP
0008  *
0009  ************************************************************/
0010 
0011 #include "DataFormats/Common/interface/Handle.h"
0012 #include "FWCore/Framework/interface/Event.h"
0013 #include "FWCore/Framework/interface/EventSetup.h"
0014 #include "FWCore/Framework/interface/Frameworkfwd.h"
0015 #include "FWCore/Framework/interface/global/EDFilter.h"
0016 #include "FWCore/Framework/interface/MakerMacros.h"
0017 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0018 #include "FWCore/Utilities/interface/EDGetToken.h"
0019 #include "FWCore/Utilities/interface/ESGetToken.h"
0020 #include "FWCore/Utilities/interface/InputTag.h"
0021 #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
0022 #include "SimGeneral/HepPDTRecord/interface/ParticleDataTable.h"
0023 #include "SimGeneral/HepPDTRecord/interface/PDTRecord.h"
0024 
0025 #include <cmath>
0026 #include <cstdlib>
0027 #include <list>
0028 #include <string>
0029 
0030 class PythiaFilterGammaJetWithBg : public edm::global::EDFilter<> {
0031 public:
0032   explicit PythiaFilterGammaJetWithBg(const edm::ParameterSet&);
0033 
0034   bool filter(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
0035 
0036 private:
0037   const edm::EDGetTokenT<edm::HepMCProduct> token_;
0038   const edm::ESGetToken<ParticleDataTable, PDTRecord> particleDataTableToken_;
0039 
0040   const double etaMax;
0041   const double ptSeed;
0042   const double ptMin;
0043   const double ptMax;
0044   const double dphiMin;
0045   const double detaMax;
0046   const double etaPhotonCut2;
0047 
0048   const double cone;
0049   const double ebEtaMax;
0050   const double deltaEB;
0051   const double deltaEE;
0052 };
0053 
0054 namespace {
0055 
0056   double deltaR2(double eta0, double phi0, double eta, double phi) {
0057     double dphi = phi - phi0;
0058     if (dphi > M_PI)
0059       dphi -= 2 * M_PI;
0060     else if (dphi <= -M_PI)
0061       dphi += 2 * M_PI;
0062     return dphi * dphi + (eta - eta0) * (eta - eta0);
0063   }
0064 
0065   double deltaPhi(double phi0, double phi) {
0066     double dphi = phi - phi0;
0067     if (dphi > M_PI)
0068       dphi -= 2 * M_PI;
0069     else if (dphi <= -M_PI)
0070       dphi += 2 * M_PI;
0071     return dphi;
0072   }
0073 
0074   class ParticlePtGreater {
0075   public:
0076     int operator()(const HepMC::GenParticle* p1, const HepMC::GenParticle* p2) const {
0077       return p1->momentum().perp() > p2->momentum().perp();
0078     }
0079   };
0080 }  // namespace
0081 
0082 PythiaFilterGammaJetWithBg::PythiaFilterGammaJetWithBg(const edm::ParameterSet& iConfig)
0083     : token_(consumes<edm::HepMCProduct>(
0084           edm::InputTag(iConfig.getUntrackedParameter("moduleLabel", std::string("generator")), "unsmeared"))),
0085       particleDataTableToken_(esConsumes<ParticleDataTable, PDTRecord>()),
0086       etaMax(iConfig.getUntrackedParameter<double>("MaxPhotonEta", 2.8)),
0087       ptSeed(iConfig.getUntrackedParameter<double>("PhotonSeedPt", 5.)),
0088       ptMin(iConfig.getUntrackedParameter<double>("MinPhotonPt")),
0089       ptMax(iConfig.getUntrackedParameter<double>("MaxPhotonPt")),
0090       dphiMin(iConfig.getUntrackedParameter<double>("MinDeltaPhi", -1) / 180 * M_PI),
0091       detaMax(iConfig.getUntrackedParameter<double>("MaxDeltaEta", 10.)),
0092       etaPhotonCut2(iConfig.getUntrackedParameter<double>("MinPhotonEtaForwardJet", 1.3)),
0093       cone(0.5),
0094       ebEtaMax(1.479),
0095       deltaEB(0.01745 / 2 * 5),       // delta_eta, delta_phi
0096       deltaEE(2.93 / 317 / 2 * 5) {}  // delta_x/z, delta_y/z
0097 
0098 bool PythiaFilterGammaJetWithBg::filter(edm::StreamID, edm::Event& iEvent, const edm::EventSetup& iSetup) const {
0099   bool accepted = false;
0100   edm::Handle<edm::HepMCProduct> evt;
0101   iEvent.getByToken(token_, evt);
0102 
0103   std::list<const HepMC::GenParticle*> seeds;
0104   const HepMC::GenEvent* myGenEvent = evt->GetEvent();
0105 
0106   for (HepMC::GenEvent::particle_const_iterator p = myGenEvent->particles_begin(); p != myGenEvent->particles_end();
0107        ++p) {
0108     if ((*p)->pdg_id() == 22 && (*p)->status() == 1 && (*p)->momentum().perp() > ptSeed &&
0109         std::abs((*p)->momentum().eta()) < etaMax)
0110       seeds.push_back(*p);
0111   }
0112 
0113   seeds.sort(ParticlePtGreater());
0114 
0115   //  std::cout<<" Number of seeds "<<seeds.size()<<" ProccessID "<<myGenEvent->signal_process_id()<<std::endl;
0116   //  std::cout<<" ParticleId 7= "<<myGenEvent->particle(7)->pdg_id()
0117   //  <<" pT "<<myGenEvent->particle(7)->momentum().perp()
0118   //  <<" Eta "<<myGenEvent->particle(7)->momentum().eta()
0119   //  <<" Phi "<<myGenEvent->particle(7)->momentum().phi()<<std::endl;
0120   //  std::cout<<" ParticleId 8= "<<myGenEvent->particle(8)->pdg_id()<<" pT "<<myGenEvent->particle(8)->momentum().perp()<<" Eta "<<myGenEvent->particle(8)->momentum().eta()<<" Phi "<<myGenEvent->particle(8)->momentum().phi()<<std::endl;
0121 
0122   for (std::list<const HepMC::GenParticle*>::const_iterator is = seeds.begin(); is != seeds.end(); is++) {
0123     double etaPhoton = (*is)->momentum().eta();
0124     double phiPhoton = (*is)->momentum().phi();
0125 
0126     /*
0127     double dphi7=std::abs(deltaPhi(phiPhoton,
0128                    myGenEvent->particle(7)->momentum().phi()));
0129     double dphi=std::abs(deltaPhi(phiPhoton,
0130                   myGenEvent->particle(8)->momentum().phi()));
0131     */
0132 
0133     HepMC::GenEvent::particle_const_iterator ppp = myGenEvent->particles_begin();
0134     for (int i = 0; i < 6; ++i)
0135       ppp++;
0136     HepMC::GenParticle* particle7 = (*ppp);
0137     ppp++;
0138     HepMC::GenParticle* particle8 = (*ppp);
0139 
0140     double dphi7 = std::abs(deltaPhi(phiPhoton, particle7->momentum().phi()));
0141     double dphi = std::abs(deltaPhi(phiPhoton, particle8->momentum().phi()));
0142 
0143     int jetline = 8;
0144     if (dphi7 > dphi) {
0145       dphi = dphi7;
0146       jetline = 7;
0147     }
0148 
0149     //    std::cout<<" Dphi "<<dphi<<" "<<dphiMin<<std::endl;
0150     //    if(dphi<dphiMin) {
0151     //      std::cout<<"Reject dphi"<<std::endl;
0152     //      continue;
0153     //    }
0154 
0155     //double etaJet= myGenEvent->particle(jetline)->momentum().eta();
0156     double etaJet = 0.0;
0157     if (jetline == 8)
0158       etaJet = particle8->momentum().eta();
0159     else
0160       etaJet = particle7->momentum().eta();
0161 
0162     double eta1 = etaJet - detaMax;
0163     double eta2 = etaJet + detaMax;
0164     if (eta1 > etaPhotonCut2)
0165       eta1 = etaPhotonCut2;
0166     if (eta2 < -etaPhotonCut2)
0167       eta2 = -etaPhotonCut2;
0168     //    std::cout<<" Etaphoton "<<etaPhoton<<" "<<eta1<<" "<<eta2<<std::endl;
0169     if (etaPhoton < eta1 || etaPhoton > eta2) {
0170       //       std::cout<<"Reject eta"<<std::endl;
0171       continue;
0172     }
0173     bool inEB(false);
0174     double tgx(0);
0175     double tgy(0);
0176     if (std::abs(etaPhoton) < ebEtaMax)
0177       inEB = true;
0178     else {
0179       tgx = (*is)->momentum().px() / (*is)->momentum().pz();
0180       tgy = (*is)->momentum().py() / (*is)->momentum().pz();
0181     }
0182 
0183     double etPhoton = 0;
0184     double etPhotonCharged = 0;
0185     double etCone = 0;
0186     double etConeCharged = 0;
0187     double ptMaxHadron = 0;
0188 
0189     for (HepMC::GenEvent::particle_const_iterator p = myGenEvent->particles_begin(); p != myGenEvent->particles_end();
0190          ++p) {
0191       if ((*p)->status() != 1)
0192         continue;
0193       int pid = (*p)->pdg_id();
0194       int apid = std::abs(pid);
0195       if (apid > 11 && apid < 21)
0196         continue;  //get rid of muons and neutrinos
0197       double eta = (*p)->momentum().eta();
0198       double phi = (*p)->momentum().phi();
0199       if (deltaR2(etaPhoton, phiPhoton, eta, phi) > cone * cone)
0200         continue;
0201       double pt = (*p)->momentum().perp();
0202 
0203       ParticleDataTable const& pdt = iSetup.getData(particleDataTableToken_);
0204 
0205       // double charge=(*p)->particledata().charge();
0206       // int charge3=(*p)->particleID().threeCharge();
0207 
0208       int charge3 = ((pdt.particle((*p)->pdg_id()))->ID().threeCharge());
0209 
0210       etCone += pt;
0211       if (charge3 && pt < 2)
0212         etConeCharged += pt;
0213 
0214       //select particles matching a crystal array centered on photon
0215       if (inEB) {
0216         if (std::abs(eta - etaPhoton) > deltaEB || std::abs(deltaPhi(phi, phiPhoton)) > deltaEB)
0217           continue;
0218       } else if (std::abs((*p)->momentum().px() / (*p)->momentum().pz() - tgx) > deltaEE ||
0219                  std::abs((*p)->momentum().py() / (*p)->momentum().pz() - tgy) > deltaEE)
0220         continue;
0221 
0222       etPhoton += pt;
0223       if (charge3 && pt < 2)
0224         etPhotonCharged += pt;
0225       if (apid > 100 && apid != 310 && pt > ptMaxHadron)
0226         ptMaxHadron = pt;
0227     }
0228     //    std::cout<<" etPhoton "<<etPhoton<<" "<<ptMin<<" "<<ptMax<<std::endl;
0229 
0230     if (etPhoton < ptMin || etPhoton > ptMax) {
0231       //     std::cout<<" Reject etPhoton "<<std::endl;
0232       continue;
0233     }
0234     //isolation cuts
0235 
0236     //    double isocut1 = 5+etPhoton/20-etPhoton*etPhoton/1e4;
0237     double isocut2 = 3 + etPhoton / 20 - etPhoton * etPhoton * etPhoton / 1e6;
0238     double isocut3 = 4.5 + etPhoton / 40;
0239     if (etPhoton > 165.) {
0240       //     isocut1 = 5.+165./20.-165.*165./1e4;
0241       isocut2 = 3. + 165. / 20. - 165. * 165. * 165. / 1e6;
0242       isocut3 = 4.5 + 165. / 40.;
0243     }
0244 
0245     //    std::cout<<" etCone "<<etCone<<" "<<etPhoton<<" "<<etCone-etPhoton<<" "<<isocut1<<std::endl;
0246     //    std::cout<<"Second cut on iso "<<etCone-etPhoton-(etConeCharged-etPhotonCharged)<<" cut value "<<isocut2<<" etPhoton "<<etPhoton<<std::endl;
0247     //    std::cout<<" PtHadron "<<ptMaxHadron<<" "<<4.5+etPhoton/40<<std::endl;
0248 
0249     if (etCone - etPhoton > 5 + etPhoton / 20 - etPhoton * etPhoton / 1e4)
0250       continue;
0251     //    std::cout<<" Accept 1"<<std::endl;
0252     if (etCone - etPhoton - (etConeCharged - etPhotonCharged) > isocut2)
0253       continue;
0254     //     std::cout<<" Accept 2"<<std::endl;
0255     if (ptMaxHadron > isocut3)
0256       continue;
0257 
0258     //    std::cout<<"Accept event "<<std::endl;
0259     accepted = true;
0260     break;
0261 
0262   }  //loop over seeds
0263   return accepted;
0264 }
0265 
0266 DEFINE_FWK_MODULE(PythiaFilterGammaJetWithBg);