Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:31:06

0001 #include "TauAnalysis/MCEmbeddingTools/plugins/CollectionMerger.h"
0002 #include "FWCore/Framework/interface/MakerMacros.h"
0003 
0004 #include "DataFormats/SiPixelCluster/interface/SiPixelCluster.h"
0005 #include "DataFormats/SiStripCluster/interface/SiStripCluster.h"
0006 
0007 #include "DataFormats/EcalRecHit/interface/EcalRecHit.h"
0008 #include "DataFormats/Common/interface/SortedCollection.h"
0009 
0010 #include "DataFormats/MuonDetId/interface/DTLayerId.h"
0011 #include "DataFormats/DTRecHit/interface/DTSLRecCluster.h"
0012 #include "DataFormats/DTRecHit/interface/DTRecHit1DPair.h"
0013 
0014 #include "DataFormats/MuonDetId/interface/CSCDetId.h"
0015 #include "DataFormats/CSCRecHit/interface/CSCRecHit2D.h"
0016 #include "DataFormats/MuonDetId/interface/RPCDetId.h"
0017 #include "DataFormats/RPCRecHit/interface/RPCRecHit.h"
0018 #include "DataFormats/HcalRecHit/interface/ZDCRecHit.h"
0019 
0020 #include "DataFormats/Common/interface/RangeMap.h"
0021 #include "DataFormats/Common/interface/OwnVector.h"
0022 
0023 typedef CollectionMerger<edmNew::DetSetVector<SiPixelCluster>, SiPixelCluster> PixelColMerger;
0024 typedef CollectionMerger<edmNew::DetSetVector<SiStripCluster>, SiStripCluster> StripColMerger;
0025 
0026 typedef CollectionMerger<edm::SortedCollection<EcalRecHit>, EcalRecHit> EcalRecHitColMerger;
0027 typedef CollectionMerger<edm::SortedCollection<HBHERecHit>, HBHERecHit> HBHERecHitColMerger;
0028 typedef CollectionMerger<edm::SortedCollection<HFRecHit>, HFRecHit> HFRecHitColMerger;
0029 typedef CollectionMerger<edm::SortedCollection<HORecHit>, HORecHit> HORecHitColMerger;
0030 typedef CollectionMerger<edm::SortedCollection<CastorRecHit>, CastorRecHit> CastorRecHitColMerger;
0031 typedef CollectionMerger<edm::SortedCollection<ZDCRecHit>, ZDCRecHit> ZDCRecHitColMerger;
0032 
0033 typedef CollectionMerger<edm::RangeMap<DTLayerId, edm::OwnVector<DTRecHit1DPair> >, DTRecHit1DPair> DTRecHitColMerger;
0034 typedef CollectionMerger<edm::RangeMap<CSCDetId, edm::OwnVector<CSCRecHit2D> >, CSCRecHit2D> CSCRecHitColMerger;
0035 typedef CollectionMerger<edm::RangeMap<RPCDetId, edm::OwnVector<RPCRecHit> >, RPCRecHit> RPCRecHitColMerger;
0036 
0037 // -------- Here Tracker Merger -----------
0038 template <typename T1, typename T2>
0039 void CollectionMerger<T1, T2>::fill_output_obj_tracker(std::unique_ptr<MergeCollection> &output,
0040                                                        std::vector<edm::Handle<MergeCollection> > &inputCollections,
0041                                                        bool print_pixel) {
0042   std::map<uint32_t, std::vector<BaseHit> > output_map;
0043   // First merge the collections with the help of the output map
0044   for (auto const &inputCollection : inputCollections) {
0045     for (typename MergeCollection::const_iterator clustSet = inputCollection->begin();
0046          clustSet != inputCollection->end();
0047          ++clustSet) {
0048       DetId detIdObject(clustSet->detId());
0049       for (typename edmNew::DetSet<BaseHit>::const_iterator clustIt = clustSet->begin(); clustIt != clustSet->end();
0050            ++clustIt) {
0051         output_map[detIdObject.rawId()].push_back(*clustIt);
0052       }
0053     }
0054   }
0055   // Now save it into the standard CMSSW format, with the standard Filler
0056   for (typename std::map<uint32_t, std::vector<BaseHit> >::const_iterator outHits = output_map.begin();
0057        outHits != output_map.end();
0058        ++outHits) {
0059     DetId detIdObject(outHits->first);
0060     typename MergeCollection::FastFiller spc(*output, detIdObject);
0061     for (const auto &Hit : outHits->second) {
0062       spc.push_back(Hit);
0063     }
0064   }
0065 }
0066 
0067 template <typename T1, typename T2>
0068 void CollectionMerger<T1, T2>::fill_output_obj_calo(std::unique_ptr<MergeCollection> &output,
0069                                                     std::vector<edm::Handle<MergeCollection> > &inputCollections) {
0070   std::map<uint32_t, BaseHit> output_map;
0071   // First merge the two collections again
0072   for (auto const &inputCollection : inputCollections) {
0073     for (typename MergeCollection::const_iterator recHit = inputCollection->begin(); recHit != inputCollection->end();
0074          ++recHit) {
0075       DetId detIdObject(recHit->detid().rawId());
0076       T2 *akt_calo_obj = &output_map[detIdObject.rawId()];
0077       float new_energy = akt_calo_obj->energy() + recHit->energy();
0078       T2 newRecHit(*recHit);
0079       newRecHit.setEnergy(new_energy);
0080       *akt_calo_obj = newRecHit;
0081     }
0082   }
0083   // Now save it into the standard CMSSW format
0084   for (typename std::map<uint32_t, BaseHit>::const_iterator outHits = output_map.begin(); outHits != output_map.end();
0085        ++outHits) {
0086     output->push_back(outHits->second);
0087   }
0088   output->sort();  //Do a sort for this collection
0089 }
0090 
0091 // -------- Here Muon Chamber Merger -----------
0092 template <typename T1, typename T2>
0093 void CollectionMerger<T1, T2>::fill_output_obj_muonchamber(
0094     std::unique_ptr<MergeCollection> &output, std::vector<edm::Handle<MergeCollection> > &inputCollections) {
0095   std::map<uint32_t, std::vector<BaseHit> > output_map;
0096   // First merge the collections with the help of the output map
0097   for (auto const &inputCollection : inputCollections) {
0098     for (typename MergeCollection::const_iterator recHit = inputCollection->begin(); recHit != inputCollection->end();
0099          ++recHit) {
0100       DetId detIdObject(recHit->geographicalId());
0101       output_map[detIdObject].push_back(*recHit);
0102     }
0103   }
0104   // Now save it into the standard CMSSW format, with the standard Filler
0105   for (typename std::map<uint32_t, std::vector<BaseHit> >::const_iterator outHits = output_map.begin();
0106        outHits != output_map.end();
0107        ++outHits) {
0108     output->put((typename T1::id_iterator::value_type)outHits->first,
0109                 outHits->second.begin(),
0110                 outHits->second.end());  // The DTLayerId misses the automatic type cast
0111   }
0112 }
0113 
0114 // Here some overloaded functions, which are needed such that the right merger function is called for the indivudal Collections
0115 template <typename T1, typename T2>
0116 void CollectionMerger<T1, T2>::fill_output_obj(std::unique_ptr<MergeCollection> &output,
0117                                                std::vector<edm::Handle<MergeCollection> > &inputCollections) {
0118   assert(0);  // CV: make sure general function never gets called;
0119               //     always use template specializations
0120 }
0121 
0122 // Start with the Tracker collections
0123 template <>
0124 void CollectionMerger<edmNew::DetSetVector<SiPixelCluster>, SiPixelCluster>::fill_output_obj(
0125     std::unique_ptr<MergeCollection> &output, std::vector<edm::Handle<MergeCollection> > &inputCollections) {
0126   fill_output_obj_tracker(output, inputCollections, true);
0127 }
0128 
0129 template <>
0130 void CollectionMerger<edmNew::DetSetVector<SiStripCluster>, SiStripCluster>::fill_output_obj(
0131     std::unique_ptr<MergeCollection> &output, std::vector<edm::Handle<MergeCollection> > &inputCollections) {
0132   fill_output_obj_tracker(output, inputCollections);
0133 }
0134 
0135 // Next are the Calo entries
0136 template <>
0137 void CollectionMerger<edm::SortedCollection<EcalRecHit>, EcalRecHit>::fill_output_obj(
0138     std::unique_ptr<MergeCollection> &output, std::vector<edm::Handle<MergeCollection> > &inputCollections) {
0139   fill_output_obj_calo(output, inputCollections);
0140 }
0141 
0142 template <>
0143 void CollectionMerger<edm::SortedCollection<HBHERecHit>, HBHERecHit>::fill_output_obj(
0144     std::unique_ptr<MergeCollection> &output, std::vector<edm::Handle<MergeCollection> > &inputCollections) {
0145   fill_output_obj_calo(output, inputCollections);
0146 }
0147 
0148 template <>
0149 void CollectionMerger<edm::SortedCollection<HFRecHit>, HFRecHit>::fill_output_obj(
0150     std::unique_ptr<MergeCollection> &output, std::vector<edm::Handle<MergeCollection> > &inputCollections) {
0151   fill_output_obj_calo(output, inputCollections);
0152 }
0153 
0154 template <>
0155 void CollectionMerger<edm::SortedCollection<HORecHit>, HORecHit>::fill_output_obj(
0156     std::unique_ptr<MergeCollection> &output, std::vector<edm::Handle<MergeCollection> > &inputCollections) {
0157   fill_output_obj_calo(output, inputCollections);
0158 }
0159 
0160 template <>
0161 void CollectionMerger<edm::SortedCollection<CastorRecHit>, CastorRecHit>::fill_output_obj(
0162     std::unique_ptr<MergeCollection> &output, std::vector<edm::Handle<MergeCollection> > &inputCollections) {
0163   fill_output_obj_calo(output, inputCollections);
0164 }
0165 
0166 template <>
0167 void CollectionMerger<edm::SortedCollection<ZDCRecHit>, ZDCRecHit>::fill_output_obj(
0168     std::unique_ptr<MergeCollection> &output, std::vector<edm::Handle<MergeCollection> > &inputCollections) {
0169   fill_output_obj_calo(output, inputCollections);
0170 }
0171 
0172 // Here the Muon Chamber
0173 template <>
0174 void CollectionMerger<edm::RangeMap<DTLayerId, edm::OwnVector<DTRecHit1DPair> >, DTRecHit1DPair>::fill_output_obj(
0175     std::unique_ptr<MergeCollection> &output, std::vector<edm::Handle<MergeCollection> > &inputCollections) {
0176   fill_output_obj_muonchamber(output, inputCollections);
0177 }
0178 
0179 template <>
0180 void CollectionMerger<edm::RangeMap<CSCDetId, edm::OwnVector<CSCRecHit2D> >, CSCRecHit2D>::fill_output_obj(
0181     std::unique_ptr<MergeCollection> &output, std::vector<edm::Handle<MergeCollection> > &inputCollections) {
0182   fill_output_obj_muonchamber(output, inputCollections);
0183 }
0184 
0185 template <>
0186 void CollectionMerger<edm::RangeMap<RPCDetId, edm::OwnVector<RPCRecHit> >, RPCRecHit>::fill_output_obj(
0187     std::unique_ptr<MergeCollection> &output, std::vector<edm::Handle<MergeCollection> > &inputCollections) {
0188   fill_output_obj_muonchamber(output, inputCollections);
0189 }
0190 
0191 DEFINE_FWK_MODULE(PixelColMerger);
0192 DEFINE_FWK_MODULE(StripColMerger);
0193 
0194 DEFINE_FWK_MODULE(EcalRecHitColMerger);
0195 DEFINE_FWK_MODULE(HBHERecHitColMerger);
0196 DEFINE_FWK_MODULE(HFRecHitColMerger);
0197 DEFINE_FWK_MODULE(HORecHitColMerger);
0198 DEFINE_FWK_MODULE(CastorRecHitColMerger);
0199 DEFINE_FWK_MODULE(ZDCRecHitColMerger);
0200 
0201 DEFINE_FWK_MODULE(DTRecHitColMerger);
0202 DEFINE_FWK_MODULE(CSCRecHitColMerger);
0203 DEFINE_FWK_MODULE(RPCRecHitColMerger);