Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-06-10 01:50:15

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