ContainerXXX

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
#ifndef ContainerXXX_h
#define ContainerXXX_h

/*
 *      file:           ContainerXXX.h
 *      Author:         Viktor Khristenko
 *
 *      Description:
 */

#include "DQM/HcalCommon/interface/Container1D.h"
#include <cmath>

namespace hcaldqm {
  typedef std::unordered_map<uint32_t, double> doubleCompactMap;
  typedef std::unordered_map<uint32_t, int> intCompactMap;
  typedef std::unordered_map<uint32_t, uint32_t> uintCompactMap;

  template <typename STDTYPE>
  class ContainerXXX {
  public:
    ContainerXXX() {}
    ContainerXXX(hashfunctions::HashType ht) : _hashmap(ht) {}
    ContainerXXX(ContainerXXX const &x);
    ContainerXXX &operator=(const ContainerXXX &other) = default;
    virtual ~ContainerXXX() { _cmap.clear(); }

    //  initialize, booking. booking is done from Electronicsmap.
    virtual void initialize(hashfunctions::HashType, int debug = 0);
    virtual void book(HcalElectronicsMap const *);
    virtual void book(HcalElectronicsMap const *, filter::HashFilter const &);

    //  setters
    virtual void set(HcalDetId const &, STDTYPE);
    virtual void set(HcalElectronicsId const &, STDTYPE);
    virtual void set(HcalTrigTowerDetId const &, STDTYPE);

    //  getters
    virtual STDTYPE &get(HcalDetId const &);
    virtual STDTYPE &get(HcalElectronicsId const &);
    virtual STDTYPE &get(HcalTrigTowerDetId const &);

    //  pushers/adders - not a push_back.
    //  ignored if already is present
    virtual void push(HcalDetId const &, STDTYPE);
    virtual void push(HcalElectronicsId const &, STDTYPE);
    virtual void push(HcalTrigTowerDetId const &, STDTYPE);

    //  finders, note true/false - no pointer to the actual guy...
    //  I know, I know not the best coding...
    virtual bool exists(HcalDetId const &);
    virtual bool exists(HcalElectronicsId const &);
    virtual bool exists(HcalTrigTowerDetId const &);

    virtual void dump(Container1D *);
    virtual void dump(std::vector<Container1D *> const &);

    virtual void load(Container1D *);
    virtual void reset();
    virtual uint32_t size();
    virtual void print();

  protected:
    typedef std::unordered_map<uint32_t, STDTYPE> CompactMap;
    CompactMap _cmap;
    mapper::HashMapper _hashmap;
    Logger _logger;

  public:
    virtual typename CompactMap::const_iterator begin() { return _cmap.begin(); }
    virtual typename CompactMap::const_iterator end() { return _cmap.end(); }
  };

  template <typename STDTYPE>
  ContainerXXX<STDTYPE>::ContainerXXX(ContainerXXX const &x) {
    for (auto &p : _cmap) {
      _cmap.insert(std::make_pair(p.first, p.second));
    }
  }

  template <typename STDTYPE>
  void ContainerXXX<STDTYPE>::initialize(hashfunctions::HashType ht, int debug) {
    _hashmap.initialize(ht);
    _logger.set("XXX", debug);
  }

  template <typename STDTYPE>
  void ContainerXXX<STDTYPE>::book(HcalElectronicsMap const *emap) {
    if (_hashmap.isDHash()) {
      std::vector<HcalGenericDetId> dids = emap->allPrecisionId();
      for (std::vector<HcalGenericDetId>::const_iterator it = dids.begin(); it != dids.end(); ++it) {
        if (!it->isHcalDetId())
          continue;

        HcalDetId did = HcalDetId(it->rawId());
        uint32_t hash = _hashmap.getHash(did);
        _logger.debug(_hashmap.getName(did));
        typename CompactMap::iterator mit = _cmap.find(hash);
        if (mit != _cmap.end())
          continue;

        _cmap.insert(std::make_pair(hash, STDTYPE(0)));
      }
    } else if (_hashmap.isEHash()) {
      std::vector<HcalElectronicsId> eids = emap->allElectronicsIdPrecision();
      for (std::vector<HcalElectronicsId>::const_iterator it = eids.begin(); it != eids.end(); ++it) {
        HcalElectronicsId eid = HcalElectronicsId(it->rawId());
        uint32_t hash = _hashmap.getHash(eid);
        _logger.debug(_hashmap.getName(eid));
        typename CompactMap::iterator mit = _cmap.find(hash);
        if (mit != _cmap.end())
          continue;

        _cmap.insert(std::make_pair(hash, STDTYPE(0)));
      }
    } else if (_hashmap.isTHash()) {
      std::vector<HcalTrigTowerDetId> tids = emap->allTriggerId();
      for (std::vector<HcalTrigTowerDetId>::const_iterator it = tids.begin(); it != tids.end(); ++it) {
        HcalTrigTowerDetId tid = HcalTrigTowerDetId(it->rawId());
        uint32_t hash = _hashmap.getHash(tid);
        _logger.debug(_hashmap.getName(tid));
        typename CompactMap::iterator mit = _cmap.find(hash);
        if (mit != _cmap.end())
          continue;

        _cmap.insert(std::make_pair(hash, STDTYPE(0)));
      }
    }
  }

  template <typename STDTYPE>
  void ContainerXXX<STDTYPE>::book(HcalElectronicsMap const *emap, filter::HashFilter const &filter) {
    if (_hashmap.isDHash()) {
      std::vector<HcalGenericDetId> dids = emap->allPrecisionId();
      for (std::vector<HcalGenericDetId>::const_iterator it = dids.begin(); it != dids.end(); ++it) {
        if (!it->isHcalDetId())
          continue;

        HcalDetId did = HcalDetId(it->rawId());
        uint32_t hash = _hashmap.getHash(did);
        typename CompactMap::iterator mit = _cmap.find(hash);
        if (mit != _cmap.end())
          continue;
        if (filter.filter(did))
          continue;

        _logger.debug(_hashmap.getName(did));

        _cmap.insert(std::make_pair(hash, STDTYPE(0)));
      }
    } else if (_hashmap.isEHash()) {
      std::vector<HcalElectronicsId> eids = emap->allElectronicsIdPrecision();
      for (std::vector<HcalElectronicsId>::const_iterator it = eids.begin(); it != eids.end(); ++it) {
        HcalElectronicsId eid = HcalElectronicsId(it->rawId());
        uint32_t hash = _hashmap.getHash(eid);
        typename CompactMap::iterator mit = _cmap.find(hash);
        if (filter.filter(eid))
          continue;
        if (mit != _cmap.end())
          continue;
        _logger.debug(eid);

        _cmap.insert(std::make_pair(hash, STDTYPE(0)));
      }
    } else if (_hashmap.isTHash()) {
      std::vector<HcalTrigTowerDetId> tids = emap->allTriggerId();
      for (std::vector<HcalTrigTowerDetId>::const_iterator it = tids.begin(); it != tids.end(); ++it) {
        HcalTrigTowerDetId tid = HcalTrigTowerDetId(it->rawId());
        uint32_t hash = _hashmap.getHash(tid);
        typename CompactMap::iterator mit = _cmap.find(hash);
        if (mit != _cmap.end())
          continue;
        if (filter.filter(tid))
          continue;
        _logger.debug(_hashmap.getName(tid));

        _cmap.insert(std::make_pair(hash, STDTYPE(0)));
      }
    }
  }

  template <typename STDTYPE>
  void ContainerXXX<STDTYPE>::set(HcalDetId const &did, STDTYPE x) {
    _cmap[_hashmap.getHash(did)] = x;
  }

  template <typename STDTYPE>
  void ContainerXXX<STDTYPE>::set(HcalElectronicsId const &did, STDTYPE x) {
    _cmap[_hashmap.getHash(did)] = x;
  }

  template <typename STDTYPE>
  void ContainerXXX<STDTYPE>::set(HcalTrigTowerDetId const &did, STDTYPE x) {
    _cmap[_hashmap.getHash(did)] = x;
  }

  template <typename STDTYPE>
  STDTYPE &ContainerXXX<STDTYPE>::get(HcalDetId const &did) {
    return _cmap[_hashmap.getHash(did)];
  }

  template <typename STDTYPE>
  STDTYPE &ContainerXXX<STDTYPE>::get(HcalElectronicsId const &eid) {
    return _cmap[_hashmap.getHash(eid)];
  }

  template <typename STDTYPE>
  STDTYPE &ContainerXXX<STDTYPE>::get(HcalTrigTowerDetId const &tid) {
    return _cmap[_hashmap.getHash(tid)];
  }

  template <typename STDTYPE>
  bool ContainerXXX<STDTYPE>::exists(HcalDetId const &id) {
    return _cmap.find(id.rawId()) != _cmap.end();
  }

  template <typename STDTYPE>
  bool ContainerXXX<STDTYPE>::exists(HcalElectronicsId const &id) {
    return _cmap.find(id.rawId()) != _cmap.end();
  }

  template <typename STDTYPE>
  bool ContainerXXX<STDTYPE>::exists(HcalTrigTowerDetId const &id) {
    return _cmap.find(id.rawId()) != _cmap.end();
  }

  template <typename STDTYPE>
  void ContainerXXX<STDTYPE>::push(HcalDetId const &did, STDTYPE x) {
    uint32_t hash = did.rawId();
    typename CompactMap::iterator mit = _cmap.find(hash);
    if (mit != _cmap.end())
      return;
    _cmap.insert(std::make_pair(hash, x));
  }

  template <typename STDTYPE>
  void ContainerXXX<STDTYPE>::push(HcalElectronicsId const &eid, STDTYPE x) {
    uint32_t hash = eid.rawId();
    typename CompactMap::iterator mit = _cmap.find(hash);
    if (mit != _cmap.end())
      return;
    _cmap.insert(std::make_pair(hash, x));
  }

  template <typename STDTYPE>
  void ContainerXXX<STDTYPE>::push(HcalTrigTowerDetId const &tid, STDTYPE x) {
    uint32_t hash = tid.rawId();
    typename CompactMap::iterator mit = _cmap.find(hash);
    if (mit != _cmap.end())
      return;
    _cmap.insert(std::make_pair(hash, x));
  }

  template <typename STDTYPE>
  uint32_t ContainerXXX<STDTYPE>::size() {
    return (uint32_t)(_cmap.size());
  }

  template <typename STDTYPE>
  void ContainerXXX<STDTYPE>::dump(Container1D *c) {
    for (auto &p : _cmap) {
      STDTYPE &x = p.second;
      uint32_t hash = p.first;
      c->fill(hash, (double)x);
    }
  }

  template <typename STDTYPE>
  void ContainerXXX<STDTYPE>::dump(std::vector<Container1D *> const &vc) {
    for (auto &p : _cmap) {
      STDTYPE &x = p.second;
      uint32_t hash = p.first;

      for (std::vector<Container1D *>::const_iterator it = vc.begin(); it != vc.end(); ++it)
        (*it)->fill(hash, (double)x);
    }
  }

  template <typename STDTYPE>
  void ContainerXXX<STDTYPE>::print() {
    std::cout << "Container by " << _hashmap.getHashTypeName() << std::endl;
    for (auto &p : _cmap) {
      if (_hashmap.isDHash())
        std::cout << HcalDetId(p.first) << p.second << std::endl;
      else if (_hashmap.isEHash())
        std::cout << HcalElectronicsId(p.first) << p.second << std::endl;
      else if (_hashmap.isTHash())
        std::cout << HcalTrigTowerDetId(p.first) << p.second << std::endl;
    }
  }

  template <typename STDTYPE>
  void ContainerXXX<STDTYPE>::reset() {
    for (auto &p : _cmap) {
      p.second = 0;
    }
  }

  template <typename STDTYPE>
  void ContainerXXX<STDTYPE>::load(Container1D *cont) {
    for (auto &p : _cmap) {
      STDTYPE &x = p.second;
      uint32_t hash = p.first;

      if (_hashmap.isDHash())
        x = cont->getBinContent(HcalDetId(hash));
      else if (_hashmap.isEHash())
        x = cont->getBinContent(HcalElectronicsId(hash));
      else if (_hashmap.isTHash())
        x = cont->getBinContent(HcalTrigTowerDetId(hash));
    }
  }
}  // namespace hcaldqm

#endif