Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-05-20 02:13:53

0001 #ifndef DataFormats_L1TParticleFlow_layer1_emulator_h
0002 #define DataFormats_L1TParticleFlow_layer1_emulator_h
0003 
0004 #include <fstream>
0005 #include <vector>
0006 #include "DataFormats/L1TParticleFlow/interface/layer1_objs.h"
0007 #include "DataFormats/L1TParticleFlow/interface/pf.h"
0008 #include "DataFormats/L1TParticleFlow/interface/puppi.h"
0009 #include "DataFormats/L1TParticleFlow/interface/egamma.h"
0010 #include "DataFormats/L1TParticleFlow/interface/emulator_io.h"
0011 
0012 namespace l1t {
0013   class PFTrack;
0014   class PFCluster;
0015   class PFCandidate;
0016   class SAMuon;
0017 }  // namespace l1t
0018 
0019 namespace l1ct {
0020 
0021   struct HadCaloObjEmu : public HadCaloObj {
0022     const l1t::PFCluster *src = nullptr;
0023     bool read(std::fstream &from);
0024     bool write(std::fstream &to) const;
0025     void clear() {
0026       HadCaloObj::clear();
0027       src = nullptr;
0028     }
0029   };
0030 
0031   struct EmCaloObjEmu : public EmCaloObj {
0032     const l1t::PFCluster *src = nullptr;
0033     bool read(std::fstream &from);
0034     bool write(std::fstream &to) const;
0035     void clear() {
0036       EmCaloObj::clear();
0037       src = nullptr;
0038     }
0039   };
0040 
0041   struct TkObjEmu : public TkObj {
0042     uint16_t hwChi2;
0043     float simPt, simCaloEta, simCaloPhi, simVtxEta, simVtxPhi, simZ0, simD0;
0044     const l1t::PFTrack *src = nullptr;
0045     bool read(std::fstream &from);
0046     bool write(std::fstream &to) const;
0047     void clear() {
0048       TkObj::clear();
0049       src = nullptr;
0050       hwChi2 = 0;
0051       simPt = 0;
0052       simCaloEta = 0;
0053       simCaloPhi = 0;
0054       simVtxEta = 0;
0055       simVtxPhi = 0;
0056       simZ0 = 0;
0057       simD0 = 0;
0058     }
0059   };
0060 
0061   struct MuObjEmu : public MuObj {
0062     const l1t::SAMuon *src = nullptr;
0063     bool read(std::fstream &from);
0064     bool write(std::fstream &to) const;
0065     void clear() {
0066       MuObj::clear();
0067       src = nullptr;
0068     }
0069   };
0070 
0071   struct PFChargedObjEmu : public PFChargedObj {
0072     const l1t::PFCluster *srcCluster = nullptr;
0073     const l1t::PFTrack *srcTrack = nullptr;
0074     const l1t::SAMuon *srcMu = nullptr;
0075     const l1t::PFCandidate *srcCand = nullptr;
0076     bool read(std::fstream &from);
0077     bool write(std::fstream &to) const;
0078     void clear() {
0079       PFChargedObj::clear();
0080       srcCluster = nullptr;
0081       srcTrack = nullptr;
0082       srcMu = nullptr;
0083       srcCand = nullptr;
0084     }
0085   };
0086 
0087   struct PFNeutralObjEmu : public PFNeutralObj {
0088     const l1t::PFCluster *srcCluster = nullptr;
0089     const l1t::PFCandidate *srcCand = nullptr;
0090     bool read(std::fstream &from);
0091     bool write(std::fstream &to) const;
0092     void clear() {
0093       PFNeutralObj::clear();
0094       srcCluster = nullptr;
0095       srcCand = nullptr;
0096     }
0097   };
0098 
0099   struct PFRegionEmu : public PFRegion {
0100     PFRegionEmu() : PFRegion() {}
0101     PFRegionEmu(float etaCenter, float phicenter);
0102     PFRegionEmu(float etamin, float etamax, float phicenter, float phiwidth, float etaextra, float phiextra);
0103 
0104     // global coordinates
0105     bool contains(float eta, float phi) const;
0106     bool containsHw(glbeta_t glbeta, glbphi_t phi) const;
0107     float localEta(float globalEta) const;
0108     float localPhi(float globalPhi) const;
0109 
0110     bool read(std::fstream &from);
0111     bool write(std::fstream &to) const;
0112   };
0113 
0114   struct PuppiObjEmu : public PuppiObj {
0115     const l1t::PFCluster *srcCluster = nullptr;
0116     const l1t::PFTrack *srcTrack = nullptr;
0117     const l1t::SAMuon *srcMu = nullptr;
0118     const l1t::PFCandidate *srcCand = nullptr;
0119     bool read(std::fstream &from);
0120     bool write(std::fstream &to) const;
0121     void clear() {
0122       PuppiObj::clear();
0123       srcCluster = nullptr;
0124       srcTrack = nullptr;
0125       srcMu = nullptr;
0126       srcCand = nullptr;
0127     }
0128     inline void fill(const PFRegionEmu &region, const PFChargedObjEmu &src) {
0129       PuppiObj::fill(region, src);
0130       srcCluster = src.srcCluster;
0131       srcTrack = src.srcTrack;
0132       srcMu = src.srcMu;
0133       srcCand = src.srcCand;
0134     }
0135     inline void fill(const PFRegionEmu &region, const PFNeutralObjEmu &src, pt_t puppiPt, puppiWgt_t puppiWgt) {
0136       PuppiObj::fill(region, src, puppiPt, puppiWgt);
0137       srcCluster = src.srcCluster;
0138       srcTrack = nullptr;
0139       srcMu = nullptr;
0140       srcCand = src.srcCand;
0141     }
0142     inline void fill(const PFRegionEmu &region, const HadCaloObjEmu &src, pt_t puppiPt, puppiWgt_t puppiWgt) {
0143       PuppiObj::fill(region, src, puppiPt, puppiWgt);
0144       srcCluster = src.src;
0145       srcTrack = nullptr;
0146       srcMu = nullptr;
0147       srcCand = nullptr;
0148     }
0149   };
0150 
0151   struct EGObjEmu : public EGIsoObj {
0152     const l1t::PFCluster *srcCluster = nullptr;
0153     void clear() {
0154       srcCluster = nullptr;
0155       EGIsoObj::clear();
0156     }
0157   };
0158 
0159   struct EGIsoObjEmu : public EGIsoObj {
0160     const l1t::PFCluster *srcCluster;
0161     // we use an index to the standalone object needed to retrieve a Ref when putting
0162     int sta_idx;
0163     bool read(std::fstream &from);
0164     bool write(std::fstream &to) const;
0165     void clear() {
0166       EGIsoObj::clear();
0167       srcCluster = nullptr;
0168       sta_idx = -1;
0169       clearIsoVars();
0170     }
0171 
0172     void clearIsoVars() {
0173       hwIsoVars[0] = 0;
0174       hwIsoVars[1] = 0;
0175       hwIsoVars[2] = 0;
0176       hwIsoVars[3] = 0;
0177       hwIsoVars[4] = 0;
0178       hwIsoVars[5] = 0;
0179     }
0180 
0181     using EGIsoObj::floatIso;
0182 
0183     enum IsoType { TkIso = 0, PfIso = 1, TkIsoPV = 2, PfIsoPV = 3, PuppiIso = 4, PuppiIsoPV = 5 };
0184 
0185     float floatIso(IsoType type) const { return Scales::floatIso(hwIsoVars[type]); }
0186     float floatRelIso(IsoType type) const { return Scales::floatIso(hwIsoVars[type]) / floatPt(); }
0187     float hwIsoVar(IsoType type) const { return hwIsoVars[type]; }
0188     void setHwIso(IsoType type, iso_t value) { hwIsoVars[type] = value; }
0189 
0190     iso_t hwIsoVars[6];
0191   };
0192 
0193   struct EGIsoEleObjEmu : public EGIsoEleObj {
0194     const l1t::PFCluster *srcCluster = nullptr;
0195     const l1t::PFTrack *srcTrack = nullptr;
0196     // we use an index to the standalone object needed to retrieve a Ref when putting
0197     int sta_idx;
0198     float idScore;
0199     bool read(std::fstream &from);
0200     bool write(std::fstream &to) const;
0201     void clear() {
0202       EGIsoEleObj::clear();
0203       srcCluster = nullptr;
0204       srcTrack = nullptr;
0205       sta_idx = -1;
0206       idScore = -999;
0207       clearIsoVars();
0208     }
0209 
0210     void clearIsoVars() {
0211       hwIsoVars[0] = 0;
0212       hwIsoVars[1] = 0;
0213       hwIsoVars[2] = 0;
0214     }
0215 
0216     using EGIsoEleObj::floatIso;
0217 
0218     enum IsoType { TkIso = 0, PfIso = 1, PuppiIso = 2 };
0219 
0220     float floatIso(IsoType type) const { return Scales::floatIso(hwIsoVars[type]); }
0221     float floatRelIso(IsoType type) const { return Scales::floatIso(hwIsoVars[type]) / floatPt(); }
0222     float hwIsoVar(IsoType type) const { return hwIsoVars[type]; }
0223     void setHwIso(IsoType type, iso_t value) { hwIsoVars[type] = value; }
0224 
0225     iso_t hwIsoVars[3];
0226   };
0227 
0228   struct PVObjEmu : public PVObj {
0229     bool read(std::fstream &from);
0230     bool write(std::fstream &to) const;
0231   };
0232 
0233   template <typename T>
0234   struct DetectorSector {
0235     PFRegionEmu region;
0236     std::vector<T> obj;
0237     DetectorSector() {}
0238     DetectorSector(float etamin, float etamax, float phicenter, float phiwidth, float etaextra = 0, float phiextra = 0)
0239         : region(etamin, etamax, phicenter, phiwidth, etaextra, phiextra) {}
0240     // convenience forwarding of some methods
0241     typedef typename std::vector<T>::const_iterator const_iterator;
0242     typedef typename std::vector<T>::iterator iterator;
0243     inline const T &operator[](unsigned int i) const { return obj[i]; }
0244     inline T &operator[](unsigned int i) { return obj[i]; }
0245     inline const_iterator begin() const { return obj.begin(); }
0246     inline iterator begin() { return obj.begin(); }
0247     inline const_iterator end() const { return obj.end(); }
0248     inline iterator end() { return obj.end(); }
0249     inline unsigned int size() const { return obj.size(); }
0250     inline void resize(unsigned int size) { obj.resize(size); }
0251     inline void clear() { obj.clear(); }
0252   };
0253 
0254   struct RawInputs {
0255     std::vector<DetectorSector<ap_uint<96>>> track;
0256     DetectorSector<ap_uint<64>> muon;  // muons are global
0257     std::vector<DetectorSector<ap_uint<256>>> hgcalcluster;
0258 
0259     bool read(std::fstream &from);
0260     bool write(std::fstream &to) const;
0261     void clear();
0262   };
0263 
0264   struct RegionizerDecodedInputs {
0265     std::vector<DetectorSector<HadCaloObjEmu>> hadcalo;
0266     std::vector<DetectorSector<EmCaloObjEmu>> emcalo;
0267     std::vector<DetectorSector<TkObjEmu>> track;
0268     DetectorSector<MuObjEmu> muon;  // muons are global
0269 
0270     bool read(std::fstream &from);
0271     bool write(std::fstream &to) const;
0272     void clear();
0273   };
0274 
0275   struct PFInputRegion {
0276     PFRegionEmu region;
0277     std::vector<HadCaloObjEmu> hadcalo;
0278     std::vector<EmCaloObjEmu> emcalo;
0279     std::vector<TkObjEmu> track;
0280     std::vector<MuObjEmu> muon;
0281 
0282     PFInputRegion() {}
0283     PFInputRegion(float etamin, float etamax, float phicenter, float phiwidth, float etaextra, float phiextra)
0284         : region(etamin, etamax, phicenter, phiwidth, etaextra, phiextra) {}
0285     bool read(std::fstream &from);
0286     bool write(std::fstream &to) const;
0287     void clear();
0288   };
0289 
0290   struct OutputRegion {
0291     std::vector<PFChargedObjEmu> pfcharged;
0292     std::vector<PFNeutralObjEmu> pfphoton;
0293     std::vector<PFNeutralObjEmu> pfneutral;
0294     std::vector<PFChargedObjEmu> pfmuon;
0295     std::vector<PuppiObjEmu> puppi;
0296     std::vector<EGObjEmu> egsta;
0297     std::vector<EGIsoObjEmu> egphoton;
0298     std::vector<EGIsoEleObjEmu> egelectron;
0299 
0300     bool read(std::fstream &from);
0301     bool write(std::fstream &to) const;
0302     void clear();
0303 
0304     // for multiplicities
0305     enum ObjType {
0306       anyType = 0,
0307       chargedType = 1,
0308       neutralType = 2,
0309       electronType = 3,
0310       muonType = 4,
0311       chargedHadronType = 5,
0312       neutralHadronType = 6,
0313       photonType = 7,
0314       nPFTypes = 8,
0315       egisoType = 8,
0316       egisoeleType = 9,
0317       nObjTypes = 10
0318     };
0319     static constexpr const char *objTypeName[nObjTypes] = {
0320         "", "Charged", "Neutral", "Electron", "Muon", "ChargedHadron", "NeutralHadron", "Photon", "EGIso", "EGIsoEle"};
0321     unsigned int nObj(ObjType type, bool puppi) const;
0322   };
0323 
0324   struct OutputBoard {
0325     float eta;
0326     float phi;
0327     // NOTE: region_index is not written to the dump file
0328     std::vector<unsigned int> region_index;
0329     std::vector<EGIsoObjEmu> egphoton;
0330     std::vector<EGIsoEleObjEmu> egelectron;
0331 
0332     bool read(std::fstream &from);
0333     bool write(std::fstream &to) const;
0334     void clear();
0335   };
0336 
0337   struct Event {
0338     enum { VERSION = 12 };
0339     uint32_t run, lumi;
0340     uint64_t event;
0341     RawInputs raw;
0342     RegionizerDecodedInputs decoded;
0343     std::vector<PFInputRegion> pfinputs;
0344     std::vector<PVObjEmu> pvs;
0345     std::vector<ap_uint<64>> pvs_emu;
0346     std::vector<OutputRegion> out;
0347     std::vector<OutputBoard> board_out;
0348 
0349     Event() : run(0), lumi(0), event(0) {}
0350 
0351     bool read(std::fstream &from);
0352     bool write(std::fstream &to) const;
0353     void clear();
0354     void init(uint32_t run, uint32_t lumi, uint64_t event);
0355     inline l1ct::PVObjEmu pv(unsigned int ipv = 0) const {
0356       l1ct::PVObjEmu ret;
0357       if (ipv < pvs.size())
0358         ret = pvs[ipv];
0359       else
0360         ret.clear();
0361       return ret;
0362     }
0363     inline ap_uint<64> pv_emu(unsigned int ipv = 0) const {
0364       ap_uint<64> ret = 0;
0365       if (ipv < pvs_emu.size())
0366         ret = pvs_emu[ipv];
0367       return ret;
0368     }
0369   };
0370 
0371   template <typename T1, typename T2>
0372   void toFirmware(const std::vector<T1> &in, unsigned int NMAX, T2 out[/*NMAX*/]) {
0373     unsigned int n = std::min<unsigned>(in.size(), NMAX);
0374     for (unsigned int i = 0; i < n; ++i)
0375       out[i] = in[i];
0376     for (unsigned int i = n; i < NMAX; ++i)
0377       out[i].clear();
0378   }
0379 
0380 }  // namespace l1ct
0381 
0382 #endif