Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-10-01 01:02:11

0001 #ifndef PFTKEGALGO_REF_H
0002 #define PFTKEGALGO_REF_H
0003 
0004 #include "DataFormats/L1TParticleFlow/interface/layer1_emulator.h"
0005 #include "DataFormats/L1TParticleFlow/interface/egamma.h"
0006 #include "DataFormats/L1TParticleFlow/interface/pf.h"
0007 
0008 namespace edm {
0009   class ParameterSet;
0010 }
0011 
0012 namespace l1ct {
0013 
0014   struct PFTkEGAlgoEmuConfig {
0015     unsigned int nTRACK;
0016     unsigned int nTRACK_EGIN;
0017     unsigned int nEMCALO_EGIN;
0018     unsigned int nEM_EGOUT;
0019 
0020     bool filterHwQuality;
0021     bool doBremRecovery;
0022     bool writeBeforeBremRecovery;
0023     int caloHwQual;
0024     bool doEndcapHwQual;
0025     float emClusterPtMin;  // GeV
0026     float dEtaMaxBrem;
0027     float dPhiMaxBrem;
0028 
0029     std::vector<double> absEtaBoundaries;
0030     std::vector<double> dEtaValues;
0031     std::vector<double> dPhiValues;
0032     float trkQualityPtMin;  // GeV
0033     bool writeEgSta;
0034 
0035     struct IsoParameters {
0036       IsoParameters(const edm::ParameterSet &);
0037       IsoParameters(float tkQualityPtMin, float dZ, float dRMin, float dRMax)
0038           : tkQualityPtMin(Scales::makePtFromFloat(tkQualityPtMin)),
0039             dZ(Scales::makeZ0(dZ)),
0040             dRMin2(Scales::makeDR2FromFloatDR(dRMin)),
0041             dRMax2(Scales::makeDR2FromFloatDR(dRMax)) {}
0042       pt_t tkQualityPtMin;
0043       ap_int<z0_t::width + 1> dZ;
0044       int dRMin2;
0045       int dRMax2;
0046     };
0047 
0048     IsoParameters tkIsoParams_tkEle;
0049     IsoParameters tkIsoParams_tkEm;
0050     IsoParameters pfIsoParams_tkEle;
0051     IsoParameters pfIsoParams_tkEm;
0052     bool doTkIso;
0053     bool doPfIso;
0054     EGIsoEleObjEmu::IsoType hwIsoTypeTkEle;
0055     EGIsoObjEmu::IsoType hwIsoTypeTkEm;
0056     int debug = 0;
0057 
0058     PFTkEGAlgoEmuConfig(const edm::ParameterSet &iConfig);
0059     PFTkEGAlgoEmuConfig(unsigned int nTrack,
0060                         unsigned int nTrack_in,
0061                         unsigned int nEmCalo_in,
0062                         unsigned int nEmOut,
0063                         bool filterHwQuality,
0064                         bool doBremRecovery,
0065                         bool writeBeforeBremRecovery = false,
0066                         int caloHwQual = 4,
0067                         bool doEndcapHwQual = false,
0068                         float emClusterPtMin = 2.,
0069                         float dEtaMaxBrem = 0.02,
0070                         float dPhiMaxBrem = 0.1,
0071                         const std::vector<double> &absEtaBoundaries = {0.0, 1.5},
0072                         const std::vector<double> &dEtaValues = {0.015, 0.01},
0073                         const std::vector<double> &dPhiValues = {0.07, 0.07},
0074                         float trkQualityPtMin = 10.,
0075                         bool writeEgSta = false,
0076                         const IsoParameters &tkIsoParams_tkEle = {2., 0.6, 0.03, 0.2},
0077                         const IsoParameters &tkIsoParams_tkEm = {2., 0.6, 0.07, 0.3},
0078                         const IsoParameters &pfIsoParams_tkEle = {1., 0.6, 0.03, 0.2},
0079                         const IsoParameters &pfIsoParams_tkEm = {1., 0.6, 0.07, 0.3},
0080                         bool doTkIso = true,
0081                         bool doPfIso = false,
0082                         EGIsoEleObjEmu::IsoType hwIsoTypeTkEle = EGIsoEleObjEmu::IsoType::TkIso,
0083                         EGIsoObjEmu::IsoType hwIsoTypeTkEm = EGIsoObjEmu::IsoType::TkIsoPV,
0084                         int debug = 0)
0085 
0086         : nTRACK(nTrack),
0087           nTRACK_EGIN(nTrack_in),
0088           nEMCALO_EGIN(nEmCalo_in),
0089           nEM_EGOUT(nEmOut),
0090           filterHwQuality(filterHwQuality),
0091           doBremRecovery(doBremRecovery),
0092           writeBeforeBremRecovery(writeBeforeBremRecovery),
0093           caloHwQual(caloHwQual),
0094           doEndcapHwQual(doEndcapHwQual),
0095           emClusterPtMin(emClusterPtMin),
0096           dEtaMaxBrem(dEtaMaxBrem),
0097           dPhiMaxBrem(dPhiMaxBrem),
0098           absEtaBoundaries(absEtaBoundaries),
0099           dEtaValues(dEtaValues),
0100           dPhiValues(dPhiValues),
0101           trkQualityPtMin(trkQualityPtMin),
0102           writeEgSta(writeEgSta),
0103           tkIsoParams_tkEle(tkIsoParams_tkEle),
0104           tkIsoParams_tkEm(tkIsoParams_tkEm),
0105           pfIsoParams_tkEle(pfIsoParams_tkEle),
0106           pfIsoParams_tkEm(pfIsoParams_tkEm),
0107           doTkIso(doTkIso),
0108           doPfIso(doPfIso),
0109           hwIsoTypeTkEle(hwIsoTypeTkEle),
0110           hwIsoTypeTkEm(hwIsoTypeTkEm),
0111           debug(debug) {}
0112   };
0113 
0114   class PFTkEGAlgoEmulator {
0115   public:
0116     PFTkEGAlgoEmulator(const PFTkEGAlgoEmuConfig &config) : cfg(config), debug_(cfg.debug) {}
0117 
0118     virtual ~PFTkEGAlgoEmulator() {}
0119 
0120     void toFirmware(const PFInputRegion &in, PFRegion &region, EmCaloObj calo[/*nCALO*/], TkObj track[/*nTRACK*/]) const;
0121     void toFirmware(const OutputRegion &out, EGIsoObj out_egphs[], EGIsoEleObj out_egeles[]) const;
0122     void toFirmware(const PFInputRegion &in,
0123                     const l1ct::PVObjEmu &pvin,
0124                     PFRegion &region,
0125                     TkObj track[/*nTRACK*/],
0126                     PVObj &pv) const;
0127 
0128     void run(const PFInputRegion &in, OutputRegion &out) const;
0129     void runIso(const PFInputRegion &in, const std::vector<l1ct::PVObjEmu> &pvs, OutputRegion &out) const;
0130 
0131     void setDebug(int verbose) { debug_ = verbose; }
0132 
0133     bool writeEgSta() const { return cfg.writeEgSta; }
0134 
0135   private:
0136     void link_emCalo2emCalo(const std::vector<EmCaloObjEmu> &emcalo, std::vector<int> &emCalo2emCalo) const;
0137 
0138     void link_emCalo2tk(const PFRegionEmu &r,
0139                         const std::vector<EmCaloObjEmu> &emcalo,
0140                         const std::vector<TkObjEmu> &track,
0141                         std::vector<int> &emCalo2tk) const;
0142 
0143     //FIXME: still needed
0144     float deltaPhi(float phi1, float phi2) const;
0145 
0146     void sel_emCalo(unsigned int nmax_sel,
0147                     const std::vector<EmCaloObjEmu> &emcalo,
0148                     std::vector<EmCaloObjEmu> &emcalo_sel) const;
0149 
0150     void eg_algo(const PFRegionEmu &region,
0151                  const std::vector<EmCaloObjEmu> &emcalo,
0152                  const std::vector<TkObjEmu> &track,
0153                  const std::vector<int> &emCalo2emCalo,
0154                  const std::vector<int> &emCalo2tk,
0155                  std::vector<EGObjEmu> &egstas,
0156                  std::vector<EGIsoObjEmu> &egobjs,
0157                  std::vector<EGIsoEleObjEmu> &egeleobjs) const;
0158 
0159     void addEgObjsToPF(std::vector<EGObjEmu> &egstas,
0160                        std::vector<EGIsoObjEmu> &egobjs,
0161                        std::vector<EGIsoEleObjEmu> &egeleobjs,
0162                        const std::vector<EmCaloObjEmu> &emcalo,
0163                        const std::vector<TkObjEmu> &track,
0164                        const int calo_idx,
0165                        const unsigned int hwQual,
0166                        const pt_t ptCorr,
0167                        const int tk_idx,
0168                        const std::vector<unsigned int> &components = {}) const;
0169 
0170     EGObjEmu &addEGStaToPF(std::vector<EGObjEmu> &egobjs,
0171                            const EmCaloObjEmu &calo,
0172                            const unsigned int hwQual,
0173                            const pt_t ptCorr,
0174                            const std::vector<unsigned int> &components) const;
0175 
0176     EGIsoObjEmu &addEGIsoToPF(std::vector<EGIsoObjEmu> &egobjs,
0177                               const EmCaloObjEmu &calo,
0178                               const unsigned int hwQual,
0179                               const pt_t ptCorr) const;
0180 
0181     EGIsoEleObjEmu &addEGIsoEleToPF(std::vector<EGIsoEleObjEmu> &egobjs,
0182                                     const EmCaloObjEmu &calo,
0183                                     const TkObjEmu &track,
0184                                     const unsigned int hwQual,
0185                                     const pt_t ptCorr) const;
0186 
0187     // FIXME: reimplemented from PFAlgoEmulatorBase
0188     template <typename T>
0189     void ptsort_ref(int nIn, int nOut, const std::vector<T> &in, std::vector<T> &out) const {
0190       out.resize(nOut);
0191       for (int iout = 0; iout < nOut; ++iout) {
0192         out[iout].clear();
0193       }
0194       for (int it = 0; it < nIn; ++it) {
0195         for (int iout = 0; iout < nOut; ++iout) {
0196           if (in[it].hwPt >= out[iout].hwPt) {
0197             for (int i2 = nOut - 1; i2 > iout; --i2) {
0198               out[i2] = out[i2 - 1];
0199             }
0200             out[iout] = in[it];
0201             break;
0202           }
0203         }
0204       }
0205     }
0206 
0207     template <typename T>
0208     int deltaR2(const T &charged, const EGIsoObjEmu &egphoton) const {
0209       // NOTE: we compare Tk at vertex against the calo variable...
0210       return dr2_int(charged.hwVtxEta(), charged.hwVtxPhi(), egphoton.hwEta, egphoton.hwPhi);
0211     }
0212 
0213     template <typename T>
0214     int deltaR2(const T &charged, const EGIsoEleObjEmu &egele) const {
0215       return dr2_int(charged.hwVtxEta(), charged.hwVtxPhi(), egele.hwVtxEta(), egele.hwVtxPhi());
0216     }
0217 
0218     int deltaR2(const PFNeutralObjEmu &neutral, const EGIsoObjEmu &egphoton) const {
0219       return dr2_int(neutral.hwEta, neutral.hwPhi, egphoton.hwEta, egphoton.hwPhi);
0220     }
0221 
0222     int deltaR2(const PFNeutralObjEmu &neutral, const EGIsoEleObjEmu &egele) const {
0223       // NOTE: we compare Tk at vertex against the calo variable...
0224       return dr2_int(neutral.hwEta, neutral.hwPhi, egele.hwVtxEta(), egele.hwVtxPhi());
0225     }
0226 
0227     template <typename T>
0228     ap_int<z0_t::width + 1> deltaZ0(const T &charged, const EGIsoObjEmu &egphoton, z0_t z0) const {
0229       ap_int<z0_t::width + 1> delta = charged.hwZ0 - z0;
0230       if (delta < 0)
0231         delta = -delta;
0232       return delta;
0233     }
0234 
0235     template <typename T>
0236     ap_int<z0_t::width + 1> deltaZ0(const T &charged, const EGIsoEleObjEmu &egele, z0_t z0) const {
0237       ap_int<z0_t::width + 1> delta = charged.hwZ0 - egele.hwZ0;
0238       if (delta < 0)
0239         delta = -delta;
0240       return delta;
0241     }
0242 
0243     template <typename TCH, typename TEG>
0244     void compute_sumPt(iso_t &sumPt,
0245                        iso_t &sumPtPV,
0246                        const std::vector<TCH> &objects,
0247                        unsigned int nMaxObj,
0248                        const TEG &egobj,
0249                        const PFTkEGAlgoEmuConfig::IsoParameters &params,
0250                        z0_t z0) const {
0251       for (unsigned int itk = 0; itk < std::min<unsigned>(objects.size(), nMaxObj); ++itk) {
0252         const auto &obj = objects[itk];
0253 
0254         if (obj.hwPt < params.tkQualityPtMin)
0255           continue;
0256 
0257         int dR2 = deltaR2(obj, egobj);
0258 
0259         if (dR2 > params.dRMin2 && dR2 < params.dRMax2) {
0260           sumPt += obj.hwPt;
0261           if (deltaZ0(obj, egobj, z0) < params.dZ) {
0262             sumPtPV += obj.hwPt;
0263           }
0264         }
0265       }
0266     }
0267 
0268     template <typename TEG>
0269     void compute_sumPt(iso_t &sumPt,
0270                        iso_t &sumPtPV,
0271                        const std::vector<PFNeutralObjEmu> &objects,
0272                        unsigned int nMaxObj,
0273                        const TEG &egobj,
0274                        const PFTkEGAlgoEmuConfig::IsoParameters &params,
0275                        z0_t z0) const {
0276       for (unsigned int itk = 0; itk < std::min<unsigned>(objects.size(), nMaxObj); ++itk) {
0277         const auto &obj = objects[itk];
0278 
0279         if (obj.hwPt < params.tkQualityPtMin)
0280           continue;
0281 
0282         int dR2 = deltaR2(obj, egobj);
0283 
0284         if (dR2 > params.dRMin2 && dR2 < params.dRMax2) {
0285           sumPt += obj.hwPt;
0286           // PF neutrals are not constrained by PV (since their Z0 is 0 by design)
0287           sumPtPV += obj.hwPt;
0288         }
0289       }
0290     }
0291 
0292     void compute_isolation(std::vector<EGIsoObjEmu> &egobjs,
0293                            const std::vector<TkObjEmu> &objects,
0294                            const PFTkEGAlgoEmuConfig::IsoParameters &params,
0295                            z0_t z0) const;
0296     void compute_isolation(std::vector<EGIsoEleObjEmu> &egobjs,
0297                            const std::vector<TkObjEmu> &objects,
0298                            const PFTkEGAlgoEmuConfig::IsoParameters &params,
0299                            z0_t z0) const;
0300     void compute_isolation(std::vector<EGIsoObjEmu> &egobjs,
0301                            const std::vector<PFChargedObjEmu> &charged,
0302                            const std::vector<PFNeutralObjEmu> &neutrals,
0303                            const PFTkEGAlgoEmuConfig::IsoParameters &params,
0304                            z0_t z0) const;
0305     void compute_isolation(std::vector<EGIsoEleObjEmu> &egobjs,
0306                            const std::vector<PFChargedObjEmu> &charged,
0307                            const std::vector<PFNeutralObjEmu> &neutrals,
0308                            const PFTkEGAlgoEmuConfig::IsoParameters &params,
0309                            z0_t z0) const;
0310 
0311     PFTkEGAlgoEmuConfig cfg;
0312     int debug_;
0313   };
0314 }  // namespace l1ct
0315 
0316 #endif