DetectorSector

EGIsoEleObjEmu

EGIsoObjEmu

EGObjEmu

EmCaloObjEmu

Event

HadCaloObjEmu

IsoType

IsoType

MuObjEmu

ObjType

OutputBoard

OutputRegion

PFChargedObjEmu

PFInputRegion

PFNeutralObjEmu

PFRegionEmu

PVObjEmu

PuppiObjEmu

RawInputs

RegionizerDecodedInputs

TkObjEmu

Macros

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
#ifndef DataFormats_L1TParticleFlow_layer1_emulator_h
#define DataFormats_L1TParticleFlow_layer1_emulator_h

#include <fstream>
#include <vector>
#include "DataFormats/L1TParticleFlow/interface/layer1_objs.h"
#include "DataFormats/L1TParticleFlow/interface/pf.h"
#include "DataFormats/L1TParticleFlow/interface/puppi.h"
#include "DataFormats/L1TParticleFlow/interface/egamma.h"
#include "DataFormats/L1TParticleFlow/interface/emulator_io.h"

namespace l1t {
  class PFTrack;
  class PFCluster;
  class PFCandidate;
  class SAMuon;
}  // namespace l1t

namespace l1ct {

  struct HadCaloObjEmu : public HadCaloObj {
    const l1t::PFCluster *src = nullptr;
    bool read(std::fstream &from);
    bool write(std::fstream &to) const;
    void clear() {
      HadCaloObj::clear();
      src = nullptr;
    }
  };

  struct EmCaloObjEmu : public EmCaloObj {
    const l1t::PFCluster *src = nullptr;
    bool read(std::fstream &from);
    bool write(std::fstream &to) const;
    void clear() {
      EmCaloObj::clear();
      src = nullptr;
    }
  };

  struct TkObjEmu : public TkObj {
    uint16_t hwChi2;
    float simPt, simCaloEta, simCaloPhi, simVtxEta, simVtxPhi, simZ0, simD0;
    const l1t::PFTrack *src = nullptr;
    bool read(std::fstream &from);
    bool write(std::fstream &to) const;
    void clear() {
      TkObj::clear();
      src = nullptr;
      hwChi2 = 0;
      simPt = 0;
      simCaloEta = 0;
      simCaloPhi = 0;
      simVtxEta = 0;
      simVtxPhi = 0;
      simZ0 = 0;
      simD0 = 0;
    }
  };

  struct MuObjEmu : public MuObj {
    const l1t::SAMuon *src = nullptr;
    bool read(std::fstream &from);
    bool write(std::fstream &to) const;
    void clear() {
      MuObj::clear();
      src = nullptr;
    }
  };

  struct PFChargedObjEmu : public PFChargedObj {
    const l1t::PFCluster *srcCluster = nullptr;
    const l1t::PFTrack *srcTrack = nullptr;
    const l1t::SAMuon *srcMu = nullptr;
    const l1t::PFCandidate *srcCand = nullptr;
    bool read(std::fstream &from);
    bool write(std::fstream &to) const;
    void clear() {
      PFChargedObj::clear();
      srcCluster = nullptr;
      srcTrack = nullptr;
      srcMu = nullptr;
      srcCand = nullptr;
    }
  };

  struct PFNeutralObjEmu : public PFNeutralObj {
    const l1t::PFCluster *srcCluster = nullptr;
    const l1t::PFCandidate *srcCand = nullptr;
    bool read(std::fstream &from);
    bool write(std::fstream &to) const;
    void clear() {
      PFNeutralObj::clear();
      srcCluster = nullptr;
      srcCand = nullptr;
    }
  };

  struct PFRegionEmu : public PFRegion {
    PFRegionEmu() : PFRegion() {}
    PFRegionEmu(float etaCenter, float phicenter);
    PFRegionEmu(float etamin, float etamax, float phicenter, float phiwidth, float etaextra, float phiextra);

    // global coordinates
    bool contains(float eta, float phi) const;
    bool containsHw(glbeta_t glbeta, glbphi_t phi) const;
    float localEta(float globalEta) const;
    float localPhi(float globalPhi) const;

    bool read(std::fstream &from);
    bool write(std::fstream &to) const;
  };

  struct PuppiObjEmu : public PuppiObj {
    const l1t::PFCluster *srcCluster = nullptr;
    const l1t::PFTrack *srcTrack = nullptr;
    const l1t::SAMuon *srcMu = nullptr;
    const l1t::PFCandidate *srcCand = nullptr;
    bool read(std::fstream &from);
    bool write(std::fstream &to) const;
    void clear() {
      PuppiObj::clear();
      srcCluster = nullptr;
      srcTrack = nullptr;
      srcMu = nullptr;
      srcCand = nullptr;
    }
    inline void fill(const PFRegionEmu &region, const PFChargedObjEmu &src) {
      PuppiObj::fill(region, src);
      srcCluster = src.srcCluster;
      srcTrack = src.srcTrack;
      srcMu = src.srcMu;
      srcCand = src.srcCand;
    }
    inline void fill(const PFRegionEmu &region, const PFNeutralObjEmu &src, pt_t puppiPt, puppiWgt_t puppiWgt) {
      PuppiObj::fill(region, src, puppiPt, puppiWgt);
      srcCluster = src.srcCluster;
      srcTrack = nullptr;
      srcMu = nullptr;
      srcCand = src.srcCand;
    }
    inline void fill(const PFRegionEmu &region, const HadCaloObjEmu &src, pt_t puppiPt, puppiWgt_t puppiWgt) {
      PuppiObj::fill(region, src, puppiPt, puppiWgt);
      srcCluster = src.src;
      srcTrack = nullptr;
      srcMu = nullptr;
      srcCand = nullptr;
    }
  };

  struct EGObjEmu : public EGIsoObj {
    const l1t::PFCluster *srcCluster = nullptr;
    void clear() {
      srcCluster = nullptr;
      EGIsoObj::clear();
    }
  };

  struct EGIsoObjEmu : public EGIsoObj {
    const l1t::PFCluster *srcCluster;

    // NOTE: we use an index to the persistable RefPtr when we reshuffle collections
    // this way we avoid complex object in the object interface which needs to be used in standalone programs
    int src_idx;
    bool read(std::fstream &from);
    bool write(std::fstream &to) const;
    void clear() {
      EGIsoObj::clear();
      srcCluster = nullptr;
      src_idx = -1;
      clearIsoVars();
    }

    void clearIsoVars() {
      hwIsoVars[0] = 0;
      hwIsoVars[1] = 0;
      hwIsoVars[2] = 0;
      hwIsoVars[3] = 0;
      hwIsoVars[4] = 0;
      hwIsoVars[5] = 0;
    }

    using EGIsoObj::floatIso;

    enum IsoType { TkIso = 0, PfIso = 1, TkIsoPV = 2, PfIsoPV = 3, PuppiIso = 4, PuppiIsoPV = 5 };

    float floatIso(IsoType type) const { return Scales::floatIso(hwIsoVars[type]); }
    float floatRelIso(IsoType type) const { return Scales::floatIso(hwIsoVars[type]) / floatPt(); }
    float hwIsoVar(IsoType type) const { return hwIsoVars[type]; }
    void setHwIso(IsoType type, iso_t value) { hwIsoVars[type] = value; }

    iso_t hwIsoVars[6];
  };

  struct EGIsoEleObjEmu : public EGIsoEleObj {
    const l1t::PFCluster *srcCluster = nullptr;
    const l1t::PFTrack *srcTrack = nullptr;

    // NOTE: we use an index to the persistable RefPtr when we reshuffle collections
    // this way we avoid complex object in the object interface which needs to be used in standalone programs
    int src_idx;
    bool read(std::fstream &from);
    bool write(std::fstream &to) const;
    void clear() {
      EGIsoEleObj::clear();
      srcCluster = nullptr;
      srcTrack = nullptr;
      src_idx = -1;
      clearIsoVars();
    }

    void clearIsoVars() {
      hwIsoVars[0] = 0;
      hwIsoVars[1] = 0;
      hwIsoVars[2] = 0;
    }

    using EGIsoEleObj::floatIso;

    enum IsoType { TkIso = 0, PfIso = 1, PuppiIso = 2 };

    float floatIso(IsoType type) const { return Scales::floatIso(hwIsoVars[type]); }
    float floatRelIso(IsoType type) const { return Scales::floatIso(hwIsoVars[type]) / floatPt(); }
    float hwIsoVar(IsoType type) const { return hwIsoVars[type]; }
    void setHwIso(IsoType type, iso_t value) { hwIsoVars[type] = value; }

    iso_t hwIsoVars[3];
  };

  struct PVObjEmu : public PVObj {
    bool read(std::fstream &from);
    bool write(std::fstream &to) const;
  };

  template <typename T>
  struct DetectorSector {
    PFRegionEmu region;
    std::vector<T> obj;
    DetectorSector() {}
    DetectorSector(float etamin, float etamax, float phicenter, float phiwidth, float etaextra = 0, float phiextra = 0)
        : region(etamin, etamax, phicenter, phiwidth, etaextra, phiextra) {}
    // convenience forwarding of some methods
    typedef typename std::vector<T>::const_iterator const_iterator;
    typedef typename std::vector<T>::iterator iterator;
    inline const T &operator[](unsigned int i) const { return obj[i]; }
    inline T &operator[](unsigned int i) { return obj[i]; }
    inline const_iterator begin() const { return obj.begin(); }
    inline iterator begin() { return obj.begin(); }
    inline const_iterator end() const { return obj.end(); }
    inline iterator end() { return obj.end(); }
    inline unsigned int size() const { return obj.size(); }
    inline void resize(unsigned int size) { obj.resize(size); }
    inline void clear() { obj.clear(); }
  };

  struct RawInputs {
    std::vector<DetectorSector<ap_uint<96>>> track;
    DetectorSector<ap_uint<64>> muon;  // muons are global
    std::vector<DetectorSector<ap_uint<256>>> hgcalcluster;

    bool read(std::fstream &from);
    bool write(std::fstream &to) const;
    void clear();
  };

  struct RegionizerDecodedInputs {
    std::vector<DetectorSector<HadCaloObjEmu>> hadcalo;
    std::vector<DetectorSector<EmCaloObjEmu>> emcalo;
    std::vector<DetectorSector<TkObjEmu>> track;
    DetectorSector<MuObjEmu> muon;  // muons are global

    bool read(std::fstream &from);
    bool write(std::fstream &to) const;
    void clear();
  };

  struct PFInputRegion {
    PFRegionEmu region;
    std::vector<HadCaloObjEmu> hadcalo;
    std::vector<EmCaloObjEmu> emcalo;
    std::vector<TkObjEmu> track;
    std::vector<MuObjEmu> muon;

    PFInputRegion() {}
    PFInputRegion(float etamin, float etamax, float phicenter, float phiwidth, float etaextra, float phiextra)
        : region(etamin, etamax, phicenter, phiwidth, etaextra, phiextra) {}
    bool read(std::fstream &from);
    bool write(std::fstream &to) const;
    void clear();
  };

  struct OutputRegion {
    std::vector<PFChargedObjEmu> pfcharged;
    std::vector<PFNeutralObjEmu> pfphoton;
    std::vector<PFNeutralObjEmu> pfneutral;
    std::vector<PFChargedObjEmu> pfmuon;
    std::vector<PuppiObjEmu> puppi;
    std::vector<EGObjEmu> egsta;
    std::vector<EGIsoObjEmu> egphoton;
    std::vector<EGIsoEleObjEmu> egelectron;

    bool read(std::fstream &from);
    bool write(std::fstream &to) const;
    void clear();

    // for multiplicities
    enum ObjType {
      anyType = 0,
      chargedType = 1,
      neutralType = 2,
      electronType = 3,
      muonType = 4,
      chargedHadronType = 5,
      neutralHadronType = 6,
      photonType = 7,
      nPFTypes = 8,
      egisoType = 8,
      egisoeleType = 9,
      nObjTypes = 10
    };
    static constexpr const char *objTypeName[nObjTypes] = {
        "", "Charged", "Neutral", "Electron", "Muon", "ChargedHadron", "NeutralHadron", "Photon", "EGIso", "EGIsoEle"};
    unsigned int nObj(ObjType type, bool puppi) const;
  };

  struct OutputBoard {
    float eta;
    float phi;
    // NOTE: region_index is not written to the dump file
    std::vector<unsigned int> region_index;
    std::vector<EGIsoObjEmu> egphoton;
    std::vector<EGIsoEleObjEmu> egelectron;

    bool read(std::fstream &from);
    bool write(std::fstream &to) const;
    void clear();
  };

  struct Event {
    enum { VERSION = 13 };
    uint32_t run, lumi;
    uint64_t event;
    RawInputs raw;
    RegionizerDecodedInputs decoded;
    std::vector<PFInputRegion> pfinputs;
    std::vector<PVObjEmu> pvs;
    std::vector<ap_uint<64>> pvs_emu;
    std::vector<OutputRegion> out;
    std::vector<OutputBoard> board_out;

    Event() : run(0), lumi(0), event(0) {}

    bool read(std::fstream &from);
    bool write(std::fstream &to) const;
    void clear();
    void init(uint32_t run, uint32_t lumi, uint64_t event);
    inline l1ct::PVObjEmu pv(unsigned int ipv = 0) const {
      l1ct::PVObjEmu ret;
      if (ipv < pvs.size())
        ret = pvs[ipv];
      else
        ret.clear();
      return ret;
    }
    inline ap_uint<64> pv_emu(unsigned int ipv = 0) const {
      ap_uint<64> ret = 0;
      if (ipv < pvs_emu.size())
        ret = pvs_emu[ipv];
      return ret;
    }
  };

  template <typename T1, typename T2>
  void toFirmware(const std::vector<T1> &in, unsigned int NMAX, T2 out[/*NMAX*/]) {
    unsigned int n = std::min<unsigned>(in.size(), NMAX);
    for (unsigned int i = 0; i < n; ++i)
      out[i] = in[i];
    for (unsigned int i = n; i < NMAX; ++i)
      out[i].clear();
  }

}  // namespace l1ct

#endif