Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:07:27

0001 #ifndef ContainerXXX_h
0002 #define ContainerXXX_h
0003 
0004 /*
0005  *      file:           ContainerXXX.h
0006  *      Author:         Viktor Khristenko
0007  *
0008  *      Description:
0009  */
0010 
0011 #include "DQM/HcalCommon/interface/Container1D.h"
0012 #include <cmath>
0013 
0014 namespace hcaldqm {
0015   typedef std::unordered_map<uint32_t, double> doubleCompactMap;
0016   typedef std::unordered_map<uint32_t, int> intCompactMap;
0017   typedef std::unordered_map<uint32_t, uint32_t> uintCompactMap;
0018 
0019   template <typename STDTYPE>
0020   class ContainerXXX {
0021   public:
0022     ContainerXXX() {}
0023     ContainerXXX(hashfunctions::HashType ht) : _hashmap(ht) {}
0024     ContainerXXX(ContainerXXX const &x);
0025     ContainerXXX &operator=(const ContainerXXX &other) = default;
0026     virtual ~ContainerXXX() { _cmap.clear(); }
0027 
0028     //  initialize, booking. booking is done from Electronicsmap.
0029     virtual void initialize(hashfunctions::HashType, int debug = 0);
0030     virtual void book(HcalElectronicsMap const *);
0031     virtual void book(HcalElectronicsMap const *, filter::HashFilter const &);
0032 
0033     //  setters
0034     virtual void set(HcalDetId const &, STDTYPE);
0035     virtual void set(HcalElectronicsId const &, STDTYPE);
0036     virtual void set(HcalTrigTowerDetId const &, STDTYPE);
0037 
0038     //  getters
0039     virtual STDTYPE &get(HcalDetId const &);
0040     virtual STDTYPE &get(HcalElectronicsId const &);
0041     virtual STDTYPE &get(HcalTrigTowerDetId const &);
0042 
0043     //  pushers/adders - not a push_back.
0044     //  ignored if already is present
0045     virtual void push(HcalDetId const &, STDTYPE);
0046     virtual void push(HcalElectronicsId const &, STDTYPE);
0047     virtual void push(HcalTrigTowerDetId const &, STDTYPE);
0048 
0049     //  finders, note true/false - no pointer to the actual guy...
0050     //  I know, I know not the best coding...
0051     virtual bool exists(HcalDetId const &);
0052     virtual bool exists(HcalElectronicsId const &);
0053     virtual bool exists(HcalTrigTowerDetId const &);
0054 
0055     virtual void dump(Container1D *);
0056     virtual void dump(std::vector<Container1D *> const &);
0057 
0058     virtual void load(Container1D *);
0059     virtual void reset();
0060     virtual uint32_t size();
0061     virtual void print();
0062 
0063   protected:
0064     typedef std::unordered_map<uint32_t, STDTYPE> CompactMap;
0065     CompactMap _cmap;
0066     mapper::HashMapper _hashmap;
0067     Logger _logger;
0068 
0069   public:
0070     virtual typename CompactMap::const_iterator begin() { return _cmap.begin(); }
0071     virtual typename CompactMap::const_iterator end() { return _cmap.end(); }
0072   };
0073 
0074   template <typename STDTYPE>
0075   ContainerXXX<STDTYPE>::ContainerXXX(ContainerXXX const &x) {
0076     for (auto &p : _cmap) {
0077       _cmap.insert(std::make_pair(p.first, p.second));
0078     }
0079   }
0080 
0081   template <typename STDTYPE>
0082   void ContainerXXX<STDTYPE>::initialize(hashfunctions::HashType ht, int debug) {
0083     _hashmap.initialize(ht);
0084     _logger.set("XXX", debug);
0085   }
0086 
0087   template <typename STDTYPE>
0088   void ContainerXXX<STDTYPE>::book(HcalElectronicsMap const *emap) {
0089     if (_hashmap.isDHash()) {
0090       std::vector<HcalGenericDetId> dids = emap->allPrecisionId();
0091       for (std::vector<HcalGenericDetId>::const_iterator it = dids.begin(); it != dids.end(); ++it) {
0092         if (!it->isHcalDetId())
0093           continue;
0094 
0095         HcalDetId did = HcalDetId(it->rawId());
0096         uint32_t hash = _hashmap.getHash(did);
0097         _logger.debug(_hashmap.getName(did));
0098         typename CompactMap::iterator mit = _cmap.find(hash);
0099         if (mit != _cmap.end())
0100           continue;
0101 
0102         _cmap.insert(std::make_pair(hash, STDTYPE(0)));
0103       }
0104     } else if (_hashmap.isEHash()) {
0105       std::vector<HcalElectronicsId> eids = emap->allElectronicsIdPrecision();
0106       for (std::vector<HcalElectronicsId>::const_iterator it = eids.begin(); it != eids.end(); ++it) {
0107         HcalElectronicsId eid = HcalElectronicsId(it->rawId());
0108         uint32_t hash = _hashmap.getHash(eid);
0109         _logger.debug(_hashmap.getName(eid));
0110         typename CompactMap::iterator mit = _cmap.find(hash);
0111         if (mit != _cmap.end())
0112           continue;
0113 
0114         _cmap.insert(std::make_pair(hash, STDTYPE(0)));
0115       }
0116     } else if (_hashmap.isTHash()) {
0117       std::vector<HcalTrigTowerDetId> tids = emap->allTriggerId();
0118       for (std::vector<HcalTrigTowerDetId>::const_iterator it = tids.begin(); it != tids.end(); ++it) {
0119         HcalTrigTowerDetId tid = HcalTrigTowerDetId(it->rawId());
0120         uint32_t hash = _hashmap.getHash(tid);
0121         _logger.debug(_hashmap.getName(tid));
0122         typename CompactMap::iterator mit = _cmap.find(hash);
0123         if (mit != _cmap.end())
0124           continue;
0125 
0126         _cmap.insert(std::make_pair(hash, STDTYPE(0)));
0127       }
0128     }
0129   }
0130 
0131   template <typename STDTYPE>
0132   void ContainerXXX<STDTYPE>::book(HcalElectronicsMap const *emap, filter::HashFilter const &filter) {
0133     if (_hashmap.isDHash()) {
0134       std::vector<HcalGenericDetId> dids = emap->allPrecisionId();
0135       for (std::vector<HcalGenericDetId>::const_iterator it = dids.begin(); it != dids.end(); ++it) {
0136         if (!it->isHcalDetId())
0137           continue;
0138 
0139         HcalDetId did = HcalDetId(it->rawId());
0140         uint32_t hash = _hashmap.getHash(did);
0141         typename CompactMap::iterator mit = _cmap.find(hash);
0142         if (mit != _cmap.end())
0143           continue;
0144         if (filter.filter(did))
0145           continue;
0146 
0147         _logger.debug(_hashmap.getName(did));
0148 
0149         _cmap.insert(std::make_pair(hash, STDTYPE(0)));
0150       }
0151     } else if (_hashmap.isEHash()) {
0152       std::vector<HcalElectronicsId> eids = emap->allElectronicsIdPrecision();
0153       for (std::vector<HcalElectronicsId>::const_iterator it = eids.begin(); it != eids.end(); ++it) {
0154         HcalElectronicsId eid = HcalElectronicsId(it->rawId());
0155         uint32_t hash = _hashmap.getHash(eid);
0156         typename CompactMap::iterator mit = _cmap.find(hash);
0157         if (filter.filter(eid))
0158           continue;
0159         if (mit != _cmap.end())
0160           continue;
0161         _logger.debug(eid);
0162 
0163         _cmap.insert(std::make_pair(hash, STDTYPE(0)));
0164       }
0165     } else if (_hashmap.isTHash()) {
0166       std::vector<HcalTrigTowerDetId> tids = emap->allTriggerId();
0167       for (std::vector<HcalTrigTowerDetId>::const_iterator it = tids.begin(); it != tids.end(); ++it) {
0168         HcalTrigTowerDetId tid = HcalTrigTowerDetId(it->rawId());
0169         uint32_t hash = _hashmap.getHash(tid);
0170         typename CompactMap::iterator mit = _cmap.find(hash);
0171         if (mit != _cmap.end())
0172           continue;
0173         if (filter.filter(tid))
0174           continue;
0175         _logger.debug(_hashmap.getName(tid));
0176 
0177         _cmap.insert(std::make_pair(hash, STDTYPE(0)));
0178       }
0179     }
0180   }
0181 
0182   template <typename STDTYPE>
0183   void ContainerXXX<STDTYPE>::set(HcalDetId const &did, STDTYPE x) {
0184     _cmap[_hashmap.getHash(did)] = x;
0185   }
0186 
0187   template <typename STDTYPE>
0188   void ContainerXXX<STDTYPE>::set(HcalElectronicsId const &did, STDTYPE x) {
0189     _cmap[_hashmap.getHash(did)] = x;
0190   }
0191 
0192   template <typename STDTYPE>
0193   void ContainerXXX<STDTYPE>::set(HcalTrigTowerDetId const &did, STDTYPE x) {
0194     _cmap[_hashmap.getHash(did)] = x;
0195   }
0196 
0197   template <typename STDTYPE>
0198   STDTYPE &ContainerXXX<STDTYPE>::get(HcalDetId const &did) {
0199     return _cmap[_hashmap.getHash(did)];
0200   }
0201 
0202   template <typename STDTYPE>
0203   STDTYPE &ContainerXXX<STDTYPE>::get(HcalElectronicsId const &eid) {
0204     return _cmap[_hashmap.getHash(eid)];
0205   }
0206 
0207   template <typename STDTYPE>
0208   STDTYPE &ContainerXXX<STDTYPE>::get(HcalTrigTowerDetId const &tid) {
0209     return _cmap[_hashmap.getHash(tid)];
0210   }
0211 
0212   template <typename STDTYPE>
0213   bool ContainerXXX<STDTYPE>::exists(HcalDetId const &id) {
0214     return _cmap.find(id.rawId()) != _cmap.end();
0215   }
0216 
0217   template <typename STDTYPE>
0218   bool ContainerXXX<STDTYPE>::exists(HcalElectronicsId const &id) {
0219     return _cmap.find(id.rawId()) != _cmap.end();
0220   }
0221 
0222   template <typename STDTYPE>
0223   bool ContainerXXX<STDTYPE>::exists(HcalTrigTowerDetId const &id) {
0224     return _cmap.find(id.rawId()) != _cmap.end();
0225   }
0226 
0227   template <typename STDTYPE>
0228   void ContainerXXX<STDTYPE>::push(HcalDetId const &did, STDTYPE x) {
0229     uint32_t hash = did.rawId();
0230     typename CompactMap::iterator mit = _cmap.find(hash);
0231     if (mit != _cmap.end())
0232       return;
0233     _cmap.insert(std::make_pair(hash, x));
0234   }
0235 
0236   template <typename STDTYPE>
0237   void ContainerXXX<STDTYPE>::push(HcalElectronicsId const &eid, STDTYPE x) {
0238     uint32_t hash = eid.rawId();
0239     typename CompactMap::iterator mit = _cmap.find(hash);
0240     if (mit != _cmap.end())
0241       return;
0242     _cmap.insert(std::make_pair(hash, x));
0243   }
0244 
0245   template <typename STDTYPE>
0246   void ContainerXXX<STDTYPE>::push(HcalTrigTowerDetId const &tid, STDTYPE x) {
0247     uint32_t hash = tid.rawId();
0248     typename CompactMap::iterator mit = _cmap.find(hash);
0249     if (mit != _cmap.end())
0250       return;
0251     _cmap.insert(std::make_pair(hash, x));
0252   }
0253 
0254   template <typename STDTYPE>
0255   uint32_t ContainerXXX<STDTYPE>::size() {
0256     return (uint32_t)(_cmap.size());
0257   }
0258 
0259   template <typename STDTYPE>
0260   void ContainerXXX<STDTYPE>::dump(Container1D *c) {
0261     for (auto &p : _cmap) {
0262       STDTYPE &x = p.second;
0263       uint32_t hash = p.first;
0264       c->fill(hash, (double)x);
0265     }
0266   }
0267 
0268   template <typename STDTYPE>
0269   void ContainerXXX<STDTYPE>::dump(std::vector<Container1D *> const &vc) {
0270     for (auto &p : _cmap) {
0271       STDTYPE &x = p.second;
0272       uint32_t hash = p.first;
0273 
0274       for (std::vector<Container1D *>::const_iterator it = vc.begin(); it != vc.end(); ++it)
0275         (*it)->fill(hash, (double)x);
0276     }
0277   }
0278 
0279   template <typename STDTYPE>
0280   void ContainerXXX<STDTYPE>::print() {
0281     std::cout << "Container by " << _hashmap.getHashTypeName() << std::endl;
0282     for (auto &p : _cmap) {
0283       if (_hashmap.isDHash())
0284         std::cout << HcalDetId(p.first) << p.second << std::endl;
0285       else if (_hashmap.isEHash())
0286         std::cout << HcalElectronicsId(p.first) << p.second << std::endl;
0287       else if (_hashmap.isTHash())
0288         std::cout << HcalTrigTowerDetId(p.first) << p.second << std::endl;
0289     }
0290   }
0291 
0292   template <typename STDTYPE>
0293   void ContainerXXX<STDTYPE>::reset() {
0294     for (auto &p : _cmap) {
0295       p.second = 0;
0296     }
0297   }
0298 
0299   template <typename STDTYPE>
0300   void ContainerXXX<STDTYPE>::load(Container1D *cont) {
0301     for (auto &p : _cmap) {
0302       STDTYPE &x = p.second;
0303       uint32_t hash = p.first;
0304 
0305       if (_hashmap.isDHash())
0306         x = cont->getBinContent(HcalDetId(hash));
0307       else if (_hashmap.isEHash())
0308         x = cont->getBinContent(HcalElectronicsId(hash));
0309       else if (_hashmap.isTHash())
0310         x = cont->getBinContent(HcalTrigTowerDetId(hash));
0311     }
0312   }
0313 }  // namespace hcaldqm
0314 
0315 #endif