Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:32:44

0001 // system include files
0002 #include <memory>
0003 
0004 // user include files
0005 #include "FWCore/Framework/interface/Frameworkfwd.h"
0006 #include "FWCore/Framework/interface/stream/EDProducer.h"
0007 
0008 #include "FWCore/Framework/interface/Event.h"
0009 
0010 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0011 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0012 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0013 
0014 #include "DataFormats/PatCandidates/interface/Jet.h"
0015 #include "DataFormats/Math/interface/deltaR.h"
0016 
0017 #include "DataFormats/NanoAOD/interface/FlatTable.h"
0018 
0019 #include "CommonTools/Utils/interface/StringCutObjectSelector.h"
0020 #include "CommonTools/Utils/interface/StringObjectFunction.h"
0021 
0022 class EnergyRingsTableProducer : public edm::stream::EDProducer<> {
0023 public:
0024   explicit EnergyRingsTableProducer(const edm::ParameterSet& iConfig)
0025       : name_(iConfig.getParameter<std::string>("name")),
0026         srcJet_(consumes<edm::View<pat::Jet>>(iConfig.getParameter<edm::InputTag>("src"))) {
0027     produces<nanoaod::FlatTable>();
0028   }
0029 
0030   ~EnergyRingsTableProducer() override{};
0031 
0032   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0033     edm::ParameterSetDescription desc;
0034     desc.add<edm::InputTag>("src")->setComment("input Jet collection");
0035     desc.add<std::string>("name")->setComment("name of the Jet FlatTable we are extending with energy rings");
0036     descriptions.add("EnergyRingsTable", desc);
0037   }
0038 
0039 private:
0040   void produce(edm::Event&, edm::EventSetup const&) override;
0041 
0042   std::string name_;
0043   edm::EDGetTokenT<edm::View<pat::Jet>> srcJet_;
0044 };
0045 
0046 // ------------ method called to produce the data  ------------
0047 void EnergyRingsTableProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0048   edm::Handle<edm::View<pat::Jet>> srcJet;
0049   iEvent.getByToken(srcJet_, srcJet);
0050 
0051   float cone_boundaries[] = {0.05, 0.1, 0.2, 0.3, 0.4};
0052   size_t ncone_boundaries = sizeof(cone_boundaries) / sizeof(float);
0053   unsigned int nJet = srcJet->size();
0054   unsigned int ncand = 0;
0055   std::vector<float> numdaughterspt03;
0056   std::vector<std::vector<float>> EmFractionEnergyRings(ncone_boundaries + 1, std::vector<float>(nJet, 0.));
0057   std::vector<std::vector<float>> ChFractionEnergyRings(ncone_boundaries + 1, std::vector<float>(nJet, 0.));
0058   std::vector<std::vector<float>> NeFractionEnergyRings(ncone_boundaries + 1, std::vector<float>(nJet, 0.));
0059   std::vector<std::vector<float>> MuFractionEnergyRings(ncone_boundaries + 1, std::vector<float>(nJet, 0.));
0060 
0061   for (unsigned int ij = 0; ij < nJet; ij++) {
0062     ++ncand;
0063     auto jet = srcJet->ptrAt(ij);
0064     int numDaughtersPt03 = 0;
0065     for (unsigned int ijcone = 0; ijcone < ncone_boundaries; ijcone++) {
0066       EmFractionEnergyRings[ijcone][ij] = 0;
0067       MuFractionEnergyRings[ijcone][ij] = 0;
0068       ChFractionEnergyRings[ijcone][ij] = 0;
0069       NeFractionEnergyRings[ijcone][ij] = 0;
0070     }
0071     for (const auto& d : jet->daughterPtrVector()) {
0072       float candDr = Geom::deltaR(d->p4(), jet->p4());
0073       size_t icone =
0074           std::lower_bound(&cone_boundaries[0], &cone_boundaries[ncone_boundaries], candDr) - &cone_boundaries[0];
0075       float candEnergy = d->energy();
0076       int pdgid = abs(d->pdgId());
0077       if (pdgid == 22 || pdgid == 11) {
0078         EmFractionEnergyRings[icone][ij] += candEnergy;
0079       } else if (pdgid == 13) {
0080         MuFractionEnergyRings[icone][ij] += candEnergy;
0081       } else if (d->charge() != 0) {
0082         ChFractionEnergyRings[icone][ij] += candEnergy;
0083       } else {
0084         NeFractionEnergyRings[icone][ij] += candEnergy;
0085       }
0086       if (d->pt() > 0.3)
0087         numDaughtersPt03 += 1;
0088     }  // end of jet daughters loop
0089     numdaughterspt03.push_back(numDaughtersPt03);
0090   }                                                                            //end of jet loop
0091   auto tab = std::make_unique<nanoaod::FlatTable>(ncand, name_, false, true);  //extension to Jet collection set to true
0092   tab->addColumn<int>("numDaughtersPt03", numdaughterspt03, "number of jet daughters with pT>0.3 GeV");
0093 
0094   tab->addColumn<float>("EmFractionEnergyRing0", EmFractionEnergyRings[0], "Em energy fraction in ring in dR 0-0.05");
0095   tab->addColumn<float>("EmFractionEnergyRing1", EmFractionEnergyRings[1], "Em energy fraction in ring in dR 0.05-0.1");
0096   tab->addColumn<float>("EmFractionEnergyRing2", EmFractionEnergyRings[2], "Em energy fraction in ring in dR 0.1-0.2");
0097   tab->addColumn<float>("EmFractionEnergyRing3", EmFractionEnergyRings[3], "Em energy fraction in ring in dR 0.2-0.3");
0098   tab->addColumn<float>("EmFractionEnergyRing4", EmFractionEnergyRings[4], "Em energy fraction in ring in dR 0.3-0.4");
0099   tab->addColumn<float>(
0100       "EmFractionEnergyRing5", EmFractionEnergyRings[5], "Em energy fraction in ring in dR 0.4 overflow");
0101 
0102   tab->addColumn<float>("ChFractionEnergyRing0", ChFractionEnergyRings[0], "Ch energy fraction in ring in dR 0-0.05");
0103   tab->addColumn<float>("ChFractionEnergyRing1", ChFractionEnergyRings[1], "Ch energy fraction in ring in dR 0.05-0.1");
0104   tab->addColumn<float>("ChFractionEnergyRing2", ChFractionEnergyRings[2], "Ch energy fraction in ring in dR 0.1-0.2");
0105   tab->addColumn<float>("ChFractionEnergyRing3", ChFractionEnergyRings[3], "Ch energy fraction in ring in dR 0.2-0.3");
0106   tab->addColumn<float>("ChFractionEnergyRing4", ChFractionEnergyRings[4], "Ch energy fraction in ring in dR 0.3-0.4");
0107   tab->addColumn<float>(
0108       "ChFractionEnergyRing5", ChFractionEnergyRings[5], "Ch energy fraction in ring in dR 0.4 overflow");
0109 
0110   tab->addColumn<float>("MuFractionEnergyRing0", MuFractionEnergyRings[0], "Mu energy fraction in ring in dR 0-0.05");
0111   tab->addColumn<float>("MuFractionEnergyRing1", MuFractionEnergyRings[1], "Mu energy fraction in ring in dR 0.05-0.1");
0112   tab->addColumn<float>("MuFractionEnergyRing2", MuFractionEnergyRings[2], "Mu energy fraction in ring in dR 0.1-0.2");
0113   tab->addColumn<float>("MuFractionEnergyRing3", MuFractionEnergyRings[3], "Mu energy fraction in ring in dR 0.2-0.3");
0114   tab->addColumn<float>("MuFractionEnergyRing4", MuFractionEnergyRings[4], "Mu energy fraction in ring in dR 0.3-0.4");
0115   tab->addColumn<float>(
0116       "MuFractionEnergyRing5", MuFractionEnergyRings[5], "Mu energy fraction in ring in dR 0.4 overflow");
0117 
0118   tab->addColumn<float>("NeFractionEnergyRing0", NeFractionEnergyRings[0], "Ne energy fraction in ring in dR 0-0.05");
0119   tab->addColumn<float>("NeFractionEnergyRing1", NeFractionEnergyRings[1], "Ne energy fraction in ring in dR 0.05-0.1");
0120   tab->addColumn<float>("NeFractionEnergyRing2", NeFractionEnergyRings[2], "Ne energy fraction in ring in dR 0.1-0.2");
0121   tab->addColumn<float>("NeFractionEnergyRing3", NeFractionEnergyRings[3], "Ne energy fraction in ring in dR 0.2-0.3");
0122   tab->addColumn<float>("NeFractionEnergyRing4", NeFractionEnergyRings[4], "Ne energy fraction in ring in dR 0.3-0.4");
0123   tab->addColumn<float>(
0124       "NeFractionEnergyRing5", NeFractionEnergyRings[5], "Ne energy fraction in ring in dR 0.4 overflow");
0125 
0126   iEvent.put(std::move(tab));
0127 }
0128 
0129 #include "FWCore/Framework/interface/MakerMacros.h"
0130 //define this as a plug-in
0131 DEFINE_FWK_MODULE(EnergyRingsTableProducer);